Commit 6fb3a4970337c135644ac0ef619a7e96616e4230

Stefan Sperling 2020-03-18T16:11:29

add optional 'consumed' output parameter to got_inflate_to_mem()

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;