fix length calculations in mmap inflate; fixes garbage diffs
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
diff --git a/lib/inflate.c b/lib/inflate.c
index de31813..2dd1f31 100644
--- a/lib/inflate.c
+++ b/lib/inflate.c
@@ -165,19 +165,19 @@ got_inflate_read_mmap(struct got_zstream_buf *zb, uint8_t *map, size_t offset,
*consumed = 0;
do {
+ size_t last_total_in = zb->z.total_in;
if (z->avail_in == 0) {
if (len == 0) {
/* EOF */
ret = Z_STREAM_END;
break;
}
- z->next_in = map + offset;
+ z->next_in = map + offset + *consumed;
z->avail_in = MIN(zb->inlen, len);
- *consumed += z->avail_in;
- offset += z->avail_in;
len -= z->avail_in;
}
ret = inflate(z, Z_SYNC_FLUSH);
+ *consumed += z->total_in - last_total_in;
} while (ret == Z_OK && z->avail_out > 0);
if (ret == Z_OK) {
@@ -292,10 +292,10 @@ got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
size_t offset, size_t len)
{
const struct got_error *err;
- size_t avail;
+ size_t avail, consumed;
struct got_zstream_buf zb;
void *newbuf;
- size_t consumed;
+ int nbuf = 1;
*outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
if (*outbuf == NULL)
@@ -320,8 +320,8 @@ got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
if (len == 0)
break;
if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
- newbuf = reallocarray(*outbuf, 1,
- *outlen + GOT_ZSTREAM_BUFSIZE);
+ nbuf++;
+ newbuf = recallocarray(*outbuf, nbuf - 1, nbuf, GOT_ZSTREAM_BUFSIZE);
if (newbuf == NULL) {
err = got_error_from_errno();
free(*outbuf);
@@ -331,10 +331,9 @@ got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
}
*outbuf = newbuf;
zb.outbuf = newbuf + *outlen;
- zb.outlen = GOT_ZSTREAM_BUFSIZE;
+ zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
}
} while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
-
done:
got_inflate_end(&zb);
return err;