Skip to contentSkip to conversation

lib/decompress: track total bytes consumed with fill function

PATCH New Josh Law 1 participant 1 message Apr 19 Library code

lore
Share & tools
lore

Josh Law

author
#0 lore raw

PATCH 1/1 lib/decompress: track total bytes consumed with fill function

When using a fill function to read chunks, pos was only tracking the offset within the current buffer. It completely ignored all previously read chunks.

Track the consumed bytes across chunks so we report the actual total back to the caller.

---
 lib/decompress_inflate.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/decompress_inflate.c +3 -2
index e19199f4a684..f595fd4bb5b3 100644
@@ -48,6 +48,7 @@ static int INIT __gunzip(unsigned char *buf, long len,
 	u8 *zbuf;
 	struct z_stream_s *strm;
 	int rc;
+	size_t rcved = 0;
 
 	rc = -1;
 	if (flush) {
@@ -143,7 +144,7 @@ static int INIT __gunzip(unsigned char *buf, long len,
 
 	while (rc == Z_OK) {
 		if (strm->avail_in == 0) {
-			/* TODO: handle case where both pos and fill are set */
+			rcved += strm->next_in - zbuf;
 			len = fill(zbuf, GZIP_IOBUF_SIZE);
 			if (len < 0) {
 				rc = -1;
@@ -180,7 +181,7 @@ static int INIT __gunzip(unsigned char *buf, long len,
 	zlib_inflateEnd(strm);
 	if (pos)
 		/* add + 8 to skip over trailer */
-		*pos = strm->next_in - zbuf+8;
+		*pos = rcved + (strm->next_in - zbuf) + 8;
 
 gunzip_5:
 	free(strm->workspace);
Signature · 1 line
2.43.0