make output buffer optional for got_inflate_to_mem{,_fd}()
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
diff --git a/lib/inflate.c b/lib/inflate.c
index 75a81b0..d093fd1 100644
--- a/lib/inflate.c
+++ b/lib/inflate.c
@@ -230,10 +230,13 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
void *newbuf;
int nbuf = 1;
- *outbuf = malloc(GOT_INFLATE_BUFSIZE);
- if (*outbuf == NULL)
- return got_error_from_errno("malloc");
- err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
+ if (outbuf) {
+ *outbuf = malloc(GOT_INFLATE_BUFSIZE);
+ if (*outbuf == NULL)
+ return got_error_from_errno("malloc");
+ err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
+ } else
+ err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
if (err)
return err;
@@ -249,6 +252,9 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
if (consumed_total)
*consumed_total += consumed;
if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
+ zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
+ if (outbuf == NULL)
+ continue;
newbuf = reallocarray(*outbuf, ++nbuf,
GOT_INFLATE_BUFSIZE);
if (newbuf == NULL) {
@@ -260,7 +266,6 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
}
*outbuf = newbuf;
zb.outbuf = newbuf + *outlen;
- zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
}
} while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
@@ -279,10 +284,13 @@ got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
void *newbuf;
int nbuf = 1;
- *outbuf = malloc(GOT_INFLATE_BUFSIZE);
- if (*outbuf == NULL)
- return got_error_from_errno("malloc");
- err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
+ if (outbuf) {
+ *outbuf = malloc(GOT_INFLATE_BUFSIZE);
+ if (*outbuf == NULL)
+ return got_error_from_errno("malloc");
+ err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
+ } else
+ err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
if (err)
goto done;
@@ -298,6 +306,9 @@ got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
if (consumed_total)
*consumed_total += consumed;
if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
+ zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
+ if (outbuf == NULL)
+ continue;
newbuf = reallocarray(*outbuf, ++nbuf,
GOT_INFLATE_BUFSIZE);
if (newbuf == NULL) {
@@ -309,7 +320,6 @@ got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
}
*outbuf = newbuf;
zb.outbuf = newbuf + *outlen;
- zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
}
} while (zb.flags & GOT_INFLATE_F_HAVE_MORE);