add optional 'consumed' output parameter to got_inflate_to_mem()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
diff --git a/lib/got_lib_inflate.h b/lib/got_lib_inflate.h
index f90aa5f..90304d5 100644
--- a/lib/got_lib_inflate.h
+++ b/lib/got_lib_inflate.h
@@ -30,13 +30,14 @@ struct got_inflate_buf {
const struct got_error *got_inflate_init(struct got_inflate_buf *, uint8_t *,
size_t);
const struct got_error *got_inflate_read(struct got_inflate_buf *, FILE *,
- size_t *);
+ size_t *, size_t *);
const struct got_error *got_inflate_read_fd(struct got_inflate_buf *, int,
size_t *);
const struct got_error *got_inflate_read_mmap(struct got_inflate_buf *,
uint8_t *, size_t, size_t, size_t *, size_t *);
void got_inflate_end(struct got_inflate_buf *);
-const struct got_error *got_inflate_to_mem(uint8_t **, size_t *, FILE *);
+const struct got_error *got_inflate_to_mem(uint8_t **, size_t *, size_t *,
+ FILE *);
const struct got_error *got_inflate_to_mem_fd(uint8_t **, size_t *, int);
const struct got_error *got_inflate_to_mem_mmap(uint8_t **, size_t *, uint8_t *,
size_t, size_t);
diff --git a/lib/inflate.c b/lib/inflate.c
index 472aaa1..02b01b5 100644
--- a/lib/inflate.c
+++ b/lib/inflate.c
@@ -81,9 +81,11 @@ done:
}
const struct got_error *
-got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp)
+got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
+ size_t *consumed)
{
size_t last_total_out = zb->z.total_out;
+ size_t last_total_in = zb->z.total_in;
z_stream *z = &zb->z;
int ret = Z_ERRNO;
@@ -91,6 +93,8 @@ got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp)
z->avail_out = zb->outlen;
*outlenp = 0;
+ if (consumed)
+ *consumed = 0;
do {
if (z->avail_in == 0) {
size_t n = fread(zb->inbuf, 1, zb->inlen, f);
@@ -116,6 +120,8 @@ got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp)
}
*outlenp = z->total_out - last_total_out;
+ if (consumed)
+ *consumed += z->total_in - last_total_in;
return NULL;
}
@@ -209,10 +215,11 @@ got_inflate_end(struct got_inflate_buf *zb)
}
const struct got_error *
-got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
+got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
+ size_t *consumed_total, FILE *f)
{
const struct got_error *err;
- size_t avail;
+ size_t avail, consumed;
struct got_inflate_buf zb;
void *newbuf;
int nbuf = 1;
@@ -225,12 +232,16 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
return err;
*outlen = 0;
+ if (consumed_total)
+ *consumed_total = 0;
do {
- err = got_inflate_read(&zb, f, &avail);
+ err = got_inflate_read(&zb, f, &avail, &consumed);
if (err)
goto done;
*outlen += avail;
+ if (consumed_total)
+ *consumed_total += consumed;
if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
newbuf = reallocarray(*outbuf, ++nbuf,
GOT_INFLATE_BUFSIZE);
@@ -362,7 +373,7 @@ got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
*outlen = 0;
do {
- err = got_inflate_read(&zb, infile, &avail);
+ err = got_inflate_read(&zb, infile, &avail, NULL);
if (err)
goto done;
if (avail > 0) {
@@ -399,7 +410,7 @@ got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
*outlen = 0;
do {
- err = got_inflate_read(&zb, infile, &avail);
+ err = got_inflate_read(&zb, infile, &avail, NULL);
if (err)
goto done;
if (avail > 0) {
diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c
index c1ce38c..1e04463 100644
--- a/libexec/got-index-pack/got-index-pack.c
+++ b/libexec/got-index-pack/got-index-pack.c
@@ -530,7 +530,7 @@ readrdelta(FILE *f, Object *o, int nd, int flag)
goto error;
if(hasheq(&o->hash, &h))
goto error;
- if ((e = got_inflate_to_mem(&d, &n, f)) != NULL)
+ if ((e = got_inflate_to_mem(&d, &n, NULL, f)) != NULL)
goto error;
o->len = ftello(f) - o->off;
if(d == NULL || n != nd)
@@ -572,7 +572,7 @@ readodelta(FILE *f, Object *o, off_t nd, off_t p, int flag)
goto error;
}
- if (got_inflate_to_mem(&d, &n, f) != NULL)
+ if (got_inflate_to_mem(&d, &n, NULL, f) != NULL)
goto error;
o->len = ftello(f) - o->off;
if(d == NULL || n != nd)
@@ -632,7 +632,7 @@ readpacked(FILE *f, Object *o, int flag)
b.data = emalloc(b.sz);
n = snprintf(b.data, 64, "%s %lld", typestr(t), l) + 1;
b.len = n;
- e = got_inflate_to_mem(&data, &ndata, f);
+ e = got_inflate_to_mem(&data, &ndata, NULL, f);
if (e != NULL || n + ndata >= b.sz) {
free(b.data);
return -1;
@@ -674,7 +674,7 @@ readloose(FILE *f, Object *o, int flag)
size_t n;
int l;
- if (got_inflate_to_mem(&d, &n, f) != NULL)
+ if (got_inflate_to_mem(&d, &n, NULL, f) != NULL)
return -1;
s = (char *)d;
diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c
index 02a1037..259da2e 100644
--- a/libexec/got-read-blob/got-read-blob.c
+++ b/libexec/got-read-blob/got-read-blob.c
@@ -146,7 +146,7 @@ main(int argc, char *argv[])
if (obj->size + obj->hdrlen <=
GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
- err = got_inflate_to_mem(&buf, &size, f);
+ err = got_inflate_to_mem(&buf, &size, NULL, f);
if (err)
goto done;
} else {
diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c
index 546aa7c..24e3dce 100644
--- a/libexec/got-read-commit/got-read-commit.c
+++ b/libexec/got-read-commit/got-read-commit.c
@@ -55,7 +55,7 @@ read_commit_object(struct got_commit_object **commit, FILE *f)
size_t len;
uint8_t *p;
- err = got_inflate_to_mem(&p, &len, f);
+ err = got_inflate_to_mem(&p, &len, NULL, f);
if (err)
return err;
diff --git a/libexec/got-read-tag/got-read-tag.c b/libexec/got-read-tag/got-read-tag.c
index 5839f14..c9959de 100644
--- a/libexec/got-read-tag/got-read-tag.c
+++ b/libexec/got-read-tag/got-read-tag.c
@@ -55,7 +55,7 @@ read_tag_object(struct got_tag_object **tag, FILE *f)
size_t len;
uint8_t *p;
- err = got_inflate_to_mem(&p, &len, f);
+ err = got_inflate_to_mem(&p, &len, NULL, f);
if (err)
return err;
diff --git a/libexec/got-read-tree/got-read-tree.c b/libexec/got-read-tree/got-read-tree.c
index dc6d4d5..577f7c9 100644
--- a/libexec/got-read-tree/got-read-tree.c
+++ b/libexec/got-read-tree/got-read-tree.c
@@ -56,7 +56,7 @@ read_tree_object(struct got_pathlist_head *entries, int *nentries,
struct got_object *obj;
size_t len;
- err = got_inflate_to_mem(p, &len, f);
+ err = got_inflate_to_mem(p, &len, NULL, f);
if (err)
return err;