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 81a12da5 2020-09-09 naddy #include <unistd.h>
25 63581804 2018-07-09 stsp #include <zlib.h>
26 63581804 2018-07-09 stsp #include <time.h>
28 63581804 2018-07-09 stsp #include "got_error.h"
29 63581804 2018-07-09 stsp #include "got_object.h"
30 324d37e7 2019-05-11 stsp #include "got_path.h"
32 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 63581804 2018-07-09 stsp const struct got_error *
39 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
40 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
42 63581804 2018-07-09 stsp const struct got_error *err = NULL;
45 63581804 2018-07-09 stsp memset(&zb->z, 0, sizeof(zb->z));
47 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
48 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
49 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
50 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
51 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
52 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
53 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
54 5211b8c8 2019-03-19 stsp errno = ENOMEM;
55 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
57 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
60 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
62 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
63 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
64 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
68 63581804 2018-07-09 stsp zb->flags = 0;
69 63581804 2018-07-09 stsp if (outbuf == NULL) {
70 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
71 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
72 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
75 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
77 63581804 2018-07-09 stsp zb->outbuf = outbuf;
79 6ad68bce 2020-03-24 stsp zb->csum = csum;
82 63581804 2018-07-09 stsp got_inflate_end(zb);
87 31e61ec1 2021-09-28 naddy csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
89 6ad68bce 2020-03-24 stsp if (csum->input_crc)
90 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
92 6ad68bce 2020-03-24 stsp if (csum->input_sha1)
93 6ad68bce 2020-03-24 stsp SHA1Update(csum->input_sha1, buf, len);
97 31e61ec1 2021-09-28 naddy csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
99 d5c81d44 2021-07-08 stsp if (csum->output_crc)
100 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
102 d5c81d44 2021-07-08 stsp if (csum->output_sha1)
103 d5c81d44 2021-07-08 stsp SHA1Update(csum->output_sha1, buf, len);
106 63581804 2018-07-09 stsp const struct got_error *
107 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
108 abc59930 2021-09-05 naddy size_t *consumed)
110 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
111 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
112 63581804 2018-07-09 stsp z_stream *z = &zb->z;
113 63581804 2018-07-09 stsp int ret = Z_ERRNO;
115 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
116 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
118 63581804 2018-07-09 stsp *outlenp = 0;
119 6fb3a497 2020-03-18 stsp if (consumed)
120 6fb3a497 2020-03-18 stsp *consumed = 0;
122 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
123 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
125 63581804 2018-07-09 stsp if (z->avail_in == 0) {
126 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
127 63581804 2018-07-09 stsp if (n == 0) {
128 63581804 2018-07-09 stsp if (ferror(f))
129 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
131 63581804 2018-07-09 stsp ret = Z_STREAM_END;
134 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
135 63581804 2018-07-09 stsp z->avail_in = n;
137 6ad68bce 2020-03-24 stsp if (zb->csum) {
138 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
139 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
140 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
141 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
143 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
144 d5c81d44 2021-07-08 stsp if (zb->csum) {
145 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
146 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
147 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
148 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
150 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
152 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
153 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
155 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
156 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
157 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
160 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
161 6fb3a497 2020-03-18 stsp if (consumed)
162 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
163 63581804 2018-07-09 stsp return NULL;
166 63581804 2018-07-09 stsp const struct got_error *
167 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
168 3ab5e33c 2020-03-18 stsp size_t *consumed)
170 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
171 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
172 63581804 2018-07-09 stsp z_stream *z = &zb->z;
173 63581804 2018-07-09 stsp int ret = Z_ERRNO;
175 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
176 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
178 63581804 2018-07-09 stsp *outlenp = 0;
179 3ab5e33c 2020-03-18 stsp if (consumed)
180 3ab5e33c 2020-03-18 stsp *consumed = 0;
182 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
183 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
185 63581804 2018-07-09 stsp if (z->avail_in == 0) {
186 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
188 638f9024 2019-05-13 stsp return got_error_from_errno("read");
189 63581804 2018-07-09 stsp else if (n == 0) {
191 63581804 2018-07-09 stsp ret = Z_STREAM_END;
194 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
195 63581804 2018-07-09 stsp z->avail_in = n;
197 6ad68bce 2020-03-24 stsp if (zb->csum) {
198 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
199 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
200 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
201 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
203 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
204 d5c81d44 2021-07-08 stsp if (zb->csum) {
205 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
206 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
207 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
208 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
210 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
212 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
213 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
215 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
216 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
217 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
220 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
221 3ab5e33c 2020-03-18 stsp if (consumed)
222 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
223 63581804 2018-07-09 stsp return NULL;
226 63581804 2018-07-09 stsp const struct got_error *
227 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
228 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
230 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
231 63581804 2018-07-09 stsp z_stream *z = &zb->z;
232 63581804 2018-07-09 stsp int ret = Z_ERRNO;
234 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
235 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
237 63581804 2018-07-09 stsp *outlenp = 0;
238 63581804 2018-07-09 stsp *consumed = 0;
241 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
242 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
243 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
245 63581804 2018-07-09 stsp if (z->avail_in == 0) {
246 63581804 2018-07-09 stsp if (len == 0) {
248 63581804 2018-07-09 stsp ret = Z_STREAM_END;
251 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
252 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
254 6ad68bce 2020-03-24 stsp if (zb->csum) {
255 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
256 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
257 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
258 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
260 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
261 d5c81d44 2021-07-08 stsp if (zb->csum) {
262 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
263 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
264 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
265 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
267 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
268 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
270 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
271 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
273 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
274 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
275 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
278 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
279 63581804 2018-07-09 stsp return NULL;
283 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
285 63581804 2018-07-09 stsp free(zb->inbuf);
286 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
287 63581804 2018-07-09 stsp free(zb->outbuf);
288 63581804 2018-07-09 stsp inflateEnd(&zb->z);
291 63581804 2018-07-09 stsp const struct got_error *
292 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
293 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
295 63581804 2018-07-09 stsp const struct got_error *err;
296 6fb3a497 2020-03-18 stsp size_t avail, consumed;
297 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
298 63581804 2018-07-09 stsp void *newbuf;
299 17d745b8 2018-07-23 stsp int nbuf = 1;
301 2decf4c6 2020-03-18 stsp if (outbuf) {
302 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
303 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
304 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
305 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
307 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
309 63581804 2018-07-09 stsp return err;
311 63581804 2018-07-09 stsp *outlen = 0;
312 6fb3a497 2020-03-18 stsp if (consumed_total)
313 6fb3a497 2020-03-18 stsp *consumed_total = 0;
316 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
319 63581804 2018-07-09 stsp *outlen += avail;
320 6fb3a497 2020-03-18 stsp if (consumed_total)
321 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
322 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
323 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
325 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
326 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
327 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
328 63581804 2018-07-09 stsp if (newbuf == NULL) {
329 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
330 63581804 2018-07-09 stsp free(*outbuf);
331 63581804 2018-07-09 stsp *outbuf = NULL;
332 63581804 2018-07-09 stsp *outlen = 0;
335 63581804 2018-07-09 stsp *outbuf = newbuf;
336 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
338 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
341 63581804 2018-07-09 stsp got_inflate_end(&zb);
342 63581804 2018-07-09 stsp return err;
345 63581804 2018-07-09 stsp const struct got_error *
346 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
347 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
348 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
350 63581804 2018-07-09 stsp const struct got_error *err;
351 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
352 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
353 63581804 2018-07-09 stsp void *newbuf;
354 17d745b8 2018-07-23 stsp int nbuf = 1;
355 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
357 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
358 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
359 55fdd257 2020-03-18 stsp bufsize = expected_size;
361 2decf4c6 2020-03-18 stsp if (outbuf) {
362 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
363 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
364 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
365 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
367 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
371 63581804 2018-07-09 stsp *outlen = 0;
372 3ab5e33c 2020-03-18 stsp if (consumed_total)
373 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
376 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
379 63581804 2018-07-09 stsp *outlen += avail;
380 3ab5e33c 2020-03-18 stsp if (consumed_total)
381 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
382 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
383 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
385 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
386 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
387 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
388 63581804 2018-07-09 stsp if (newbuf == NULL) {
389 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
390 63581804 2018-07-09 stsp free(*outbuf);
391 63581804 2018-07-09 stsp *outbuf = NULL;
392 63581804 2018-07-09 stsp *outlen = 0;
395 63581804 2018-07-09 stsp *outbuf = newbuf;
396 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
398 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
401 63581804 2018-07-09 stsp got_inflate_end(&zb);
402 63581804 2018-07-09 stsp return err;
405 63581804 2018-07-09 stsp const struct got_error *
406 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
407 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
408 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
410 63581804 2018-07-09 stsp const struct got_error *err;
411 37bd7602 2018-07-23 stsp size_t avail, consumed;
412 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
413 63581804 2018-07-09 stsp void *newbuf;
414 37bd7602 2018-07-23 stsp int nbuf = 1;
416 2e5a6fad 2020-03-18 stsp if (outbuf) {
417 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
418 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
419 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
420 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
422 2e5a6fad 2020-03-18 stsp free(*outbuf);
423 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
424 2e5a6fad 2020-03-18 stsp return err;
427 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
430 63581804 2018-07-09 stsp *outlen = 0;
431 2e5a6fad 2020-03-18 stsp if (consumed_total)
432 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
434 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
435 63581804 2018-07-09 stsp &consumed);
438 63581804 2018-07-09 stsp offset += consumed;
439 2e5a6fad 2020-03-18 stsp if (consumed_total)
440 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
441 63581804 2018-07-09 stsp len -= consumed;
442 63581804 2018-07-09 stsp *outlen += avail;
443 63581804 2018-07-09 stsp if (len == 0)
445 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
446 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
448 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
449 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
450 63581804 2018-07-09 stsp if (newbuf == NULL) {
451 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
452 63581804 2018-07-09 stsp free(*outbuf);
453 63581804 2018-07-09 stsp *outbuf = NULL;
454 63581804 2018-07-09 stsp *outlen = 0;
457 63581804 2018-07-09 stsp *outbuf = newbuf;
458 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
459 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
461 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
463 63581804 2018-07-09 stsp got_inflate_end(&zb);
464 63581804 2018-07-09 stsp return err;
467 63581804 2018-07-09 stsp const struct got_error *
468 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
469 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
471 63581804 2018-07-09 stsp const struct got_error *err = NULL;
472 63581804 2018-07-09 stsp size_t avail;
473 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
475 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
479 63581804 2018-07-09 stsp *outlen = 0;
482 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
485 63581804 2018-07-09 stsp if (avail > 0) {
487 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
488 63581804 2018-07-09 stsp if (n != avail) {
489 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
492 63581804 2018-07-09 stsp *outlen += avail;
494 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
497 63581804 2018-07-09 stsp if (err == NULL) {
498 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
499 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
501 63581804 2018-07-09 stsp got_inflate_end(&zb);
502 63581804 2018-07-09 stsp return err;
505 63581804 2018-07-09 stsp const struct got_error *
506 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
507 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
509 63581804 2018-07-09 stsp const struct got_error *err;
510 63581804 2018-07-09 stsp size_t avail;
511 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
513 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
517 63581804 2018-07-09 stsp *outlen = 0;
520 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
523 63581804 2018-07-09 stsp if (avail > 0) {
525 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
526 63581804 2018-07-09 stsp if (n != 1) {
527 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
530 63581804 2018-07-09 stsp *outlen += avail;
532 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
535 63581804 2018-07-09 stsp if (err == NULL)
536 63581804 2018-07-09 stsp rewind(outfile);
537 63581804 2018-07-09 stsp got_inflate_end(&zb);
538 63581804 2018-07-09 stsp return err;
541 63581804 2018-07-09 stsp const struct got_error *
542 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
543 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
545 63581804 2018-07-09 stsp const struct got_error *err;
546 4788f1ce 2020-03-18 stsp size_t avail, consumed;
547 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
549 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
553 63581804 2018-07-09 stsp *outlen = 0;
554 4788f1ce 2020-03-18 stsp if (consumed_total)
555 4788f1ce 2020-03-18 stsp *consumed_total = 0;
557 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
560 63581804 2018-07-09 stsp if (avail > 0) {
562 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
563 63581804 2018-07-09 stsp if (n != 1) {
564 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
567 63581804 2018-07-09 stsp *outlen += avail;
568 4788f1ce 2020-03-18 stsp if (consumed_total)
569 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
571 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
574 63581804 2018-07-09 stsp if (err == NULL)
575 63581804 2018-07-09 stsp rewind(outfile);
576 63581804 2018-07-09 stsp got_inflate_end(&zb);
577 63581804 2018-07-09 stsp return err;
580 63581804 2018-07-09 stsp const struct got_error *
581 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
582 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
583 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
585 63581804 2018-07-09 stsp const struct got_error *err;
586 4788f1ce 2020-03-18 stsp size_t avail, consumed;
587 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
589 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
593 63581804 2018-07-09 stsp *outlen = 0;
594 4788f1ce 2020-03-18 stsp if (consumed_total)
595 4788f1ce 2020-03-18 stsp *consumed_total = 0;
597 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
598 63581804 2018-07-09 stsp &consumed);
601 63581804 2018-07-09 stsp offset += consumed;
602 4788f1ce 2020-03-18 stsp if (consumed_total)
603 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
604 63581804 2018-07-09 stsp len -= consumed;
605 63581804 2018-07-09 stsp if (avail > 0) {
607 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
608 63581804 2018-07-09 stsp if (n != 1) {
609 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
612 63581804 2018-07-09 stsp *outlen += avail;
614 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
617 63581804 2018-07-09 stsp if (err == NULL)
618 63581804 2018-07-09 stsp rewind(outfile);
619 63581804 2018-07-09 stsp got_inflate_end(&zb);
620 63581804 2018-07-09 stsp return err;