2 63581804 2018-07-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 63581804 2018-07-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 63581804 2018-07-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 63581804 2018-07-09 stsp * copyright notice and this permission notice appear in all copies.
8 63581804 2018-07-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 63581804 2018-07-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 63581804 2018-07-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 63581804 2018-07-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 63581804 2018-07-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 63581804 2018-07-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 63581804 2018-07-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 63581804 2018-07-09 stsp #include <sys/queue.h>
19 5211b8c8 2019-03-19 stsp #include <errno.h>
20 63581804 2018-07-09 stsp #include <stdio.h>
21 63581804 2018-07-09 stsp #include <stdlib.h>
22 63581804 2018-07-09 stsp #include <string.h>
23 63581804 2018-07-09 stsp #include <sha1.h>
24 13b2bc37 2022-10-23 stsp #include <poll.h>
25 81a12da5 2020-09-09 naddy #include <unistd.h>
26 63581804 2018-07-09 stsp #include <zlib.h>
27 63581804 2018-07-09 stsp #include <time.h>
29 63581804 2018-07-09 stsp #include "got_error.h"
30 63581804 2018-07-09 stsp #include "got_object.h"
31 324d37e7 2019-05-11 stsp #include "got_path.h"
33 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
34 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
37 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 63581804 2018-07-09 stsp const struct got_error *
41 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
42 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
44 63581804 2018-07-09 stsp const struct got_error *err = NULL;
47 08603e79 2022-11-08 stsp memset(zb, 0, sizeof(*zb));
49 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
50 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
51 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
52 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
53 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
54 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
55 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
56 5211b8c8 2019-03-19 stsp errno = ENOMEM;
57 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
59 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
62 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
64 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
65 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
66 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
70 63581804 2018-07-09 stsp zb->flags = 0;
71 63581804 2018-07-09 stsp if (outbuf == NULL) {
72 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
73 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
74 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
77 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
79 63581804 2018-07-09 stsp zb->outbuf = outbuf;
81 6ad68bce 2020-03-24 stsp zb->csum = csum;
84 63581804 2018-07-09 stsp got_inflate_end(zb);
89 31e61ec1 2021-09-28 naddy csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
91 6ad68bce 2020-03-24 stsp if (csum->input_crc)
92 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
94 6ad68bce 2020-03-24 stsp if (csum->input_sha1)
95 6ad68bce 2020-03-24 stsp SHA1Update(csum->input_sha1, buf, len);
99 31e61ec1 2021-09-28 naddy csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
101 d5c81d44 2021-07-08 stsp if (csum->output_crc)
102 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
104 d5c81d44 2021-07-08 stsp if (csum->output_sha1)
105 d5c81d44 2021-07-08 stsp SHA1Update(csum->output_sha1, buf, len);
108 63581804 2018-07-09 stsp const struct got_error *
109 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
110 abc59930 2021-09-05 naddy size_t *consumed)
112 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
113 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
114 63581804 2018-07-09 stsp z_stream *z = &zb->z;
115 63581804 2018-07-09 stsp int ret = Z_ERRNO;
117 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
118 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
120 63581804 2018-07-09 stsp *outlenp = 0;
121 6fb3a497 2020-03-18 stsp if (consumed)
122 6fb3a497 2020-03-18 stsp *consumed = 0;
124 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
125 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
127 63581804 2018-07-09 stsp if (z->avail_in == 0) {
128 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
129 63581804 2018-07-09 stsp if (n == 0) {
130 63581804 2018-07-09 stsp if (ferror(f))
131 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
133 63581804 2018-07-09 stsp ret = Z_STREAM_END;
136 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
137 63581804 2018-07-09 stsp z->avail_in = n;
139 6ad68bce 2020-03-24 stsp if (zb->csum) {
140 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
141 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
142 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
143 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
145 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
146 d5c81d44 2021-07-08 stsp if (zb->csum) {
147 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
148 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
149 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
150 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
152 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
154 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
155 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
157 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
158 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
159 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
162 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
163 6fb3a497 2020-03-18 stsp if (consumed)
164 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
165 63581804 2018-07-09 stsp return NULL;
168 63581804 2018-07-09 stsp const struct got_error *
169 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
170 3ab5e33c 2020-03-18 stsp size_t *consumed)
172 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
173 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
174 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
175 63581804 2018-07-09 stsp z_stream *z = &zb->z;
176 63581804 2018-07-09 stsp int ret = Z_ERRNO;
178 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
179 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
181 63581804 2018-07-09 stsp *outlenp = 0;
182 3ab5e33c 2020-03-18 stsp if (consumed)
183 3ab5e33c 2020-03-18 stsp *consumed = 0;
185 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
186 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
188 63581804 2018-07-09 stsp if (z->avail_in == 0) {
190 13b2bc37 2022-10-23 stsp err = got_poll_fd(fd, POLLIN, INFTIM);
192 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF) {
193 13b2bc37 2022-10-23 stsp ret = Z_STREAM_END;
196 13b2bc37 2022-10-23 stsp return err;
198 13b2bc37 2022-10-23 stsp n = read(fd, zb->inbuf, zb->inlen);
200 638f9024 2019-05-13 stsp return got_error_from_errno("read");
201 63581804 2018-07-09 stsp else if (n == 0) {
203 63581804 2018-07-09 stsp ret = Z_STREAM_END;
206 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
207 63581804 2018-07-09 stsp z->avail_in = n;
209 6ad68bce 2020-03-24 stsp if (zb->csum) {
210 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
211 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
212 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
213 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
215 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
216 d5c81d44 2021-07-08 stsp if (zb->csum) {
217 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
218 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
219 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
220 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
222 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
224 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
225 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
227 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
228 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
229 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
232 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
233 3ab5e33c 2020-03-18 stsp if (consumed)
234 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
235 63581804 2018-07-09 stsp return NULL;
238 63581804 2018-07-09 stsp const struct got_error *
239 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
240 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
242 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
243 63581804 2018-07-09 stsp z_stream *z = &zb->z;
244 63581804 2018-07-09 stsp int ret = Z_ERRNO;
246 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
247 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
249 63581804 2018-07-09 stsp *outlenp = 0;
250 63581804 2018-07-09 stsp *consumed = 0;
253 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
254 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
255 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
257 63581804 2018-07-09 stsp if (z->avail_in == 0) {
258 63581804 2018-07-09 stsp if (len == 0) {
260 63581804 2018-07-09 stsp ret = Z_STREAM_END;
263 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
264 a9bd296d 2022-02-08 stsp if (len - *consumed > UINT_MAX)
265 a9bd296d 2022-02-08 stsp z->avail_in = UINT_MAX;
267 a9bd296d 2022-02-08 stsp z->avail_in = len - *consumed;
269 6ad68bce 2020-03-24 stsp if (zb->csum) {
270 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
271 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
272 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
273 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
275 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
276 d5c81d44 2021-07-08 stsp if (zb->csum) {
277 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
278 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
279 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
280 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
282 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
283 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
285 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
286 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
288 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
289 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
290 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
293 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
294 63581804 2018-07-09 stsp return NULL;
298 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
300 63581804 2018-07-09 stsp free(zb->inbuf);
301 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
302 63581804 2018-07-09 stsp free(zb->outbuf);
303 63581804 2018-07-09 stsp inflateEnd(&zb->z);
306 63581804 2018-07-09 stsp const struct got_error *
307 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
308 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
310 63581804 2018-07-09 stsp const struct got_error *err;
311 6fb3a497 2020-03-18 stsp size_t avail, consumed;
312 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
313 63581804 2018-07-09 stsp void *newbuf;
314 17d745b8 2018-07-23 stsp int nbuf = 1;
316 2decf4c6 2020-03-18 stsp if (outbuf) {
317 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
318 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
319 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
320 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
322 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
324 63581804 2018-07-09 stsp return err;
326 63581804 2018-07-09 stsp *outlen = 0;
327 6fb3a497 2020-03-18 stsp if (consumed_total)
328 6fb3a497 2020-03-18 stsp *consumed_total = 0;
331 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
334 63581804 2018-07-09 stsp *outlen += avail;
335 6fb3a497 2020-03-18 stsp if (consumed_total)
336 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
337 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
338 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
340 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
341 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
342 63581804 2018-07-09 stsp if (newbuf == NULL) {
343 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
344 63581804 2018-07-09 stsp free(*outbuf);
345 63581804 2018-07-09 stsp *outbuf = NULL;
346 63581804 2018-07-09 stsp *outlen = 0;
349 63581804 2018-07-09 stsp *outbuf = newbuf;
350 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
351 d75b4088 2022-02-08 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
353 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
356 63581804 2018-07-09 stsp got_inflate_end(&zb);
357 63581804 2018-07-09 stsp return err;
360 63581804 2018-07-09 stsp const struct got_error *
361 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
362 5e91dae4 2022-08-30 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
363 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
365 63581804 2018-07-09 stsp const struct got_error *err;
366 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
367 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
368 63581804 2018-07-09 stsp void *newbuf;
369 17d745b8 2018-07-23 stsp int nbuf = 1;
370 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
372 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
373 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
374 55fdd257 2020-03-18 stsp bufsize = expected_size;
376 2decf4c6 2020-03-18 stsp if (outbuf) {
377 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
378 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
379 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
380 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
382 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
386 63581804 2018-07-09 stsp *outlen = 0;
387 3ab5e33c 2020-03-18 stsp if (consumed_total)
388 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
391 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
394 63581804 2018-07-09 stsp *outlen += avail;
395 3ab5e33c 2020-03-18 stsp if (consumed_total)
396 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
397 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
398 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
400 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
401 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
402 63581804 2018-07-09 stsp if (newbuf == NULL) {
403 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
404 63581804 2018-07-09 stsp free(*outbuf);
405 63581804 2018-07-09 stsp *outbuf = NULL;
406 63581804 2018-07-09 stsp *outlen = 0;
409 63581804 2018-07-09 stsp *outbuf = newbuf;
410 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
411 d75b4088 2022-02-08 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
413 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
416 63581804 2018-07-09 stsp got_inflate_end(&zb);
417 63581804 2018-07-09 stsp return err;
420 63581804 2018-07-09 stsp const struct got_error *
421 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
422 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
423 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
425 63581804 2018-07-09 stsp const struct got_error *err;
426 37bd7602 2018-07-23 stsp size_t avail, consumed;
427 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
428 63581804 2018-07-09 stsp void *newbuf;
429 37bd7602 2018-07-23 stsp int nbuf = 1;
431 2e5a6fad 2020-03-18 stsp if (outbuf) {
432 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
433 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
434 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
435 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
437 2e5a6fad 2020-03-18 stsp free(*outbuf);
438 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
439 2e5a6fad 2020-03-18 stsp return err;
442 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
444 30ee8dc8 2022-01-18 stsp return err;
447 63581804 2018-07-09 stsp *outlen = 0;
448 2e5a6fad 2020-03-18 stsp if (consumed_total)
449 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
451 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
452 63581804 2018-07-09 stsp &consumed);
455 63581804 2018-07-09 stsp offset += consumed;
456 2e5a6fad 2020-03-18 stsp if (consumed_total)
457 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
458 63581804 2018-07-09 stsp len -= consumed;
459 63581804 2018-07-09 stsp *outlen += avail;
460 63581804 2018-07-09 stsp if (len == 0)
462 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
463 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
465 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
466 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
467 63581804 2018-07-09 stsp if (newbuf == NULL) {
468 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
469 63581804 2018-07-09 stsp free(*outbuf);
470 63581804 2018-07-09 stsp *outbuf = NULL;
471 63581804 2018-07-09 stsp *outlen = 0;
474 63581804 2018-07-09 stsp *outbuf = newbuf;
475 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
476 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
478 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
480 63581804 2018-07-09 stsp got_inflate_end(&zb);
481 63581804 2018-07-09 stsp return err;
484 63581804 2018-07-09 stsp const struct got_error *
485 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
486 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
488 63581804 2018-07-09 stsp const struct got_error *err = NULL;
489 63581804 2018-07-09 stsp size_t avail;
490 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
492 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
496 63581804 2018-07-09 stsp *outlen = 0;
499 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
502 63581804 2018-07-09 stsp if (avail > 0) {
504 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
505 63581804 2018-07-09 stsp if (n != avail) {
506 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
509 63581804 2018-07-09 stsp *outlen += avail;
511 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
514 63581804 2018-07-09 stsp if (err == NULL) {
515 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
516 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
518 63581804 2018-07-09 stsp got_inflate_end(&zb);
519 63581804 2018-07-09 stsp return err;
522 63581804 2018-07-09 stsp const struct got_error *
523 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
524 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
526 63581804 2018-07-09 stsp const struct got_error *err;
527 63581804 2018-07-09 stsp size_t avail;
528 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
530 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
534 63581804 2018-07-09 stsp *outlen = 0;
537 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
540 63581804 2018-07-09 stsp if (avail > 0) {
542 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
543 63581804 2018-07-09 stsp if (n != 1) {
544 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
547 63581804 2018-07-09 stsp *outlen += avail;
549 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
552 63581804 2018-07-09 stsp if (err == NULL)
553 63581804 2018-07-09 stsp rewind(outfile);
554 63581804 2018-07-09 stsp got_inflate_end(&zb);
555 63581804 2018-07-09 stsp return err;
558 63581804 2018-07-09 stsp const struct got_error *
559 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
560 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
562 63581804 2018-07-09 stsp const struct got_error *err;
563 4788f1ce 2020-03-18 stsp size_t avail, consumed;
564 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
566 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
570 63581804 2018-07-09 stsp *outlen = 0;
571 4788f1ce 2020-03-18 stsp if (consumed_total)
572 4788f1ce 2020-03-18 stsp *consumed_total = 0;
574 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
577 63581804 2018-07-09 stsp if (avail > 0) {
579 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
580 63581804 2018-07-09 stsp if (n != 1) {
581 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
584 63581804 2018-07-09 stsp *outlen += avail;
585 4788f1ce 2020-03-18 stsp if (consumed_total)
586 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
588 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
591 63581804 2018-07-09 stsp if (err == NULL)
592 63581804 2018-07-09 stsp rewind(outfile);
593 63581804 2018-07-09 stsp got_inflate_end(&zb);
594 63581804 2018-07-09 stsp return err;
597 63581804 2018-07-09 stsp const struct got_error *
598 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
599 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
600 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
602 63581804 2018-07-09 stsp const struct got_error *err;
603 4788f1ce 2020-03-18 stsp size_t avail, consumed;
604 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
606 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
610 63581804 2018-07-09 stsp *outlen = 0;
611 4788f1ce 2020-03-18 stsp if (consumed_total)
612 4788f1ce 2020-03-18 stsp *consumed_total = 0;
614 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
615 63581804 2018-07-09 stsp &consumed);
618 63581804 2018-07-09 stsp offset += consumed;
619 4788f1ce 2020-03-18 stsp if (consumed_total)
620 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
621 63581804 2018-07-09 stsp len -= consumed;
622 63581804 2018-07-09 stsp if (avail > 0) {
624 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
625 63581804 2018-07-09 stsp if (n != 1) {
626 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
629 63581804 2018-07-09 stsp *outlen += avail;
631 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
634 63581804 2018-07-09 stsp if (err == NULL)
635 63581804 2018-07-09 stsp rewind(outfile);
636 63581804 2018-07-09 stsp got_inflate_end(&zb);
637 63581804 2018-07-09 stsp return err;