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 5822e79e 2023-02-23 op #include <sha2.h>
25 13b2bc37 2022-10-23 stsp #include <poll.h>
26 81a12da5 2020-09-09 naddy #include <unistd.h>
27 63581804 2018-07-09 stsp #include <zlib.h>
28 63581804 2018-07-09 stsp #include <time.h>
30 63581804 2018-07-09 stsp #include "got_error.h"
31 63581804 2018-07-09 stsp #include "got_object.h"
32 324d37e7 2019-05-11 stsp #include "got_path.h"
34 ae25a666 2023-02-23 op #include "got_lib_hash.h"
35 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
36 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
39 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 d71015b3 2023-09-01 stsp static const struct got_error *
43 d71015b3 2023-09-01 stsp wrap_inflate_error(int zerr, const char *prefix)
45 d71015b3 2023-09-01 stsp if (zerr == Z_ERRNO)
46 d71015b3 2023-09-01 stsp return got_error_from_errno(prefix);
47 d71015b3 2023-09-01 stsp if (zerr == Z_MEM_ERROR)
48 d71015b3 2023-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
50 d71015b3 2023-09-01 stsp return got_error(GOT_ERR_DECOMPRESSION);
53 63581804 2018-07-09 stsp const struct got_error *
54 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
55 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
57 63581804 2018-07-09 stsp const struct got_error *err = NULL;
60 08603e79 2022-11-08 stsp memset(zb, 0, sizeof(*zb));
62 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
63 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
64 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
65 d71015b3 2023-09-01 stsp if (zerr != Z_OK)
66 d71015b3 2023-09-01 stsp return wrap_inflate_error(zerr, "inflateInit");
68 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
70 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
71 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
72 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
76 63581804 2018-07-09 stsp zb->flags = 0;
77 63581804 2018-07-09 stsp if (outbuf == NULL) {
78 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
79 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
80 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
83 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
85 63581804 2018-07-09 stsp zb->outbuf = outbuf;
87 6ad68bce 2020-03-24 stsp zb->csum = csum;
90 63581804 2018-07-09 stsp got_inflate_end(zb);
95 31e61ec1 2021-09-28 naddy csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
97 6ad68bce 2020-03-24 stsp if (csum->input_crc)
98 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
100 ae25a666 2023-02-23 op if (csum->input_ctx)
101 ae25a666 2023-02-23 op got_hash_update(csum->input_ctx, buf, len);
104 d5c81d44 2021-07-08 stsp static void
105 31e61ec1 2021-09-28 naddy csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
107 d5c81d44 2021-07-08 stsp if (csum->output_crc)
108 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
110 ae25a666 2023-02-23 op if (csum->output_ctx)
111 ae25a666 2023-02-23 op got_hash_update(csum->output_ctx, buf, len);
114 63581804 2018-07-09 stsp const struct got_error *
115 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
116 abc59930 2021-09-05 naddy size_t *consumed)
118 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
119 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
120 63581804 2018-07-09 stsp z_stream *z = &zb->z;
121 63581804 2018-07-09 stsp int ret = Z_ERRNO;
123 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
124 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
126 63581804 2018-07-09 stsp *outlenp = 0;
127 6fb3a497 2020-03-18 stsp if (consumed)
128 6fb3a497 2020-03-18 stsp *consumed = 0;
130 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
131 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
133 63581804 2018-07-09 stsp if (z->avail_in == 0) {
134 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
135 63581804 2018-07-09 stsp if (n == 0) {
136 63581804 2018-07-09 stsp if (ferror(f))
137 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
139 63581804 2018-07-09 stsp ret = Z_STREAM_END;
142 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
143 63581804 2018-07-09 stsp z->avail_in = n;
145 6ad68bce 2020-03-24 stsp if (zb->csum) {
146 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
147 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
148 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
149 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
151 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
152 d5c81d44 2021-07-08 stsp if (zb->csum) {
153 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
154 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
155 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
156 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
158 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
160 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
161 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
163 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
164 d71015b3 2023-09-01 stsp return wrap_inflate_error(ret, "inflate");
165 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
168 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
169 6fb3a497 2020-03-18 stsp if (consumed)
170 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
171 63581804 2018-07-09 stsp return NULL;
174 63581804 2018-07-09 stsp const struct got_error *
175 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
176 3ab5e33c 2020-03-18 stsp size_t *consumed)
178 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
179 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
180 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
181 63581804 2018-07-09 stsp z_stream *z = &zb->z;
182 63581804 2018-07-09 stsp int ret = Z_ERRNO;
184 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
185 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
187 63581804 2018-07-09 stsp *outlenp = 0;
188 3ab5e33c 2020-03-18 stsp if (consumed)
189 3ab5e33c 2020-03-18 stsp *consumed = 0;
191 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
192 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
194 63581804 2018-07-09 stsp if (z->avail_in == 0) {
196 13b2bc37 2022-10-23 stsp err = got_poll_fd(fd, POLLIN, INFTIM);
198 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF) {
199 13b2bc37 2022-10-23 stsp ret = Z_STREAM_END;
202 13b2bc37 2022-10-23 stsp return err;
204 13b2bc37 2022-10-23 stsp n = read(fd, zb->inbuf, zb->inlen);
206 638f9024 2019-05-13 stsp return got_error_from_errno("read");
207 63581804 2018-07-09 stsp else if (n == 0) {
209 63581804 2018-07-09 stsp ret = Z_STREAM_END;
212 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
213 63581804 2018-07-09 stsp z->avail_in = n;
215 6ad68bce 2020-03-24 stsp if (zb->csum) {
216 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
217 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
218 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
219 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
221 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
222 d5c81d44 2021-07-08 stsp if (zb->csum) {
223 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
224 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
225 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
226 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
228 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
230 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
231 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
233 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
234 d71015b3 2023-09-01 stsp return wrap_inflate_error(ret, "inflate");
235 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
238 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
239 3ab5e33c 2020-03-18 stsp if (consumed)
240 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
241 63581804 2018-07-09 stsp return NULL;
244 63581804 2018-07-09 stsp const struct got_error *
245 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
246 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
248 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
249 63581804 2018-07-09 stsp z_stream *z = &zb->z;
250 63581804 2018-07-09 stsp int ret = Z_ERRNO;
252 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
253 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
255 63581804 2018-07-09 stsp *outlenp = 0;
256 63581804 2018-07-09 stsp *consumed = 0;
259 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
260 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
261 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
263 63581804 2018-07-09 stsp if (z->avail_in == 0) {
264 63581804 2018-07-09 stsp if (len == 0) {
266 63581804 2018-07-09 stsp ret = Z_STREAM_END;
269 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
270 a9bd296d 2022-02-08 stsp if (len - *consumed > UINT_MAX)
271 a9bd296d 2022-02-08 stsp z->avail_in = UINT_MAX;
273 a9bd296d 2022-02-08 stsp z->avail_in = len - *consumed;
275 6ad68bce 2020-03-24 stsp if (zb->csum) {
276 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
277 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
278 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
279 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
281 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
282 d5c81d44 2021-07-08 stsp if (zb->csum) {
283 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
284 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
285 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
286 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
288 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
289 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
291 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
292 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
294 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
295 d71015b3 2023-09-01 stsp return wrap_inflate_error(ret, "inflate");
296 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
299 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
300 63581804 2018-07-09 stsp return NULL;
304 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
306 63581804 2018-07-09 stsp free(zb->inbuf);
307 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
308 63581804 2018-07-09 stsp free(zb->outbuf);
309 63581804 2018-07-09 stsp inflateEnd(&zb->z);
312 63581804 2018-07-09 stsp const struct got_error *
313 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
314 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
316 63581804 2018-07-09 stsp const struct got_error *err;
317 6fb3a497 2020-03-18 stsp size_t avail, consumed;
318 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
319 63581804 2018-07-09 stsp void *newbuf;
320 17d745b8 2018-07-23 stsp int nbuf = 1;
322 2decf4c6 2020-03-18 stsp if (outbuf) {
323 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
324 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
325 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
326 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
328 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
330 63581804 2018-07-09 stsp return err;
332 63581804 2018-07-09 stsp *outlen = 0;
333 6fb3a497 2020-03-18 stsp if (consumed_total)
334 6fb3a497 2020-03-18 stsp *consumed_total = 0;
337 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
340 63581804 2018-07-09 stsp *outlen += avail;
341 6fb3a497 2020-03-18 stsp if (consumed_total)
342 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
343 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
344 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
346 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
347 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
348 63581804 2018-07-09 stsp if (newbuf == NULL) {
349 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
350 63581804 2018-07-09 stsp free(*outbuf);
351 63581804 2018-07-09 stsp *outbuf = NULL;
352 63581804 2018-07-09 stsp *outlen = 0;
355 63581804 2018-07-09 stsp *outbuf = newbuf;
356 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
357 d75b4088 2022-02-08 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
359 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
362 63581804 2018-07-09 stsp got_inflate_end(&zb);
363 63581804 2018-07-09 stsp return err;
366 63581804 2018-07-09 stsp const struct got_error *
367 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
368 5e91dae4 2022-08-30 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
369 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
371 63581804 2018-07-09 stsp const struct got_error *err;
372 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
373 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
374 63581804 2018-07-09 stsp void *newbuf;
375 17d745b8 2018-07-23 stsp int nbuf = 1;
376 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
378 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
379 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
380 55fdd257 2020-03-18 stsp bufsize = expected_size;
382 2decf4c6 2020-03-18 stsp if (outbuf) {
383 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
384 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
385 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
386 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
388 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
392 63581804 2018-07-09 stsp *outlen = 0;
393 3ab5e33c 2020-03-18 stsp if (consumed_total)
394 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
397 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
400 63581804 2018-07-09 stsp *outlen += avail;
401 3ab5e33c 2020-03-18 stsp if (consumed_total)
402 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
403 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
404 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
406 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
407 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
408 63581804 2018-07-09 stsp if (newbuf == NULL) {
409 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
410 63581804 2018-07-09 stsp free(*outbuf);
411 63581804 2018-07-09 stsp *outbuf = NULL;
412 63581804 2018-07-09 stsp *outlen = 0;
415 63581804 2018-07-09 stsp *outbuf = newbuf;
416 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
417 d75b4088 2022-02-08 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
419 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
422 63581804 2018-07-09 stsp got_inflate_end(&zb);
423 63581804 2018-07-09 stsp return err;
426 63581804 2018-07-09 stsp const struct got_error *
427 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
428 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
429 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
431 63581804 2018-07-09 stsp const struct got_error *err;
432 37bd7602 2018-07-23 stsp size_t avail, consumed;
433 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
434 63581804 2018-07-09 stsp void *newbuf;
435 37bd7602 2018-07-23 stsp int nbuf = 1;
437 2e5a6fad 2020-03-18 stsp if (outbuf) {
438 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
439 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
440 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
441 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
443 2e5a6fad 2020-03-18 stsp free(*outbuf);
444 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
445 2e5a6fad 2020-03-18 stsp return err;
448 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
450 30ee8dc8 2022-01-18 stsp return err;
453 63581804 2018-07-09 stsp *outlen = 0;
454 2e5a6fad 2020-03-18 stsp if (consumed_total)
455 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
457 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
458 63581804 2018-07-09 stsp &consumed);
461 63581804 2018-07-09 stsp offset += consumed;
462 2e5a6fad 2020-03-18 stsp if (consumed_total)
463 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
464 63581804 2018-07-09 stsp len -= consumed;
465 63581804 2018-07-09 stsp *outlen += avail;
466 63581804 2018-07-09 stsp if (len == 0)
468 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
469 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
471 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
472 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
473 63581804 2018-07-09 stsp if (newbuf == NULL) {
474 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
475 63581804 2018-07-09 stsp free(*outbuf);
476 63581804 2018-07-09 stsp *outbuf = NULL;
477 63581804 2018-07-09 stsp *outlen = 0;
480 63581804 2018-07-09 stsp *outbuf = newbuf;
481 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
482 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
484 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
486 63581804 2018-07-09 stsp got_inflate_end(&zb);
487 63581804 2018-07-09 stsp return err;
490 63581804 2018-07-09 stsp const struct got_error *
491 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
492 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
494 63581804 2018-07-09 stsp const struct got_error *err = NULL;
495 63581804 2018-07-09 stsp size_t avail;
496 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
498 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
502 63581804 2018-07-09 stsp *outlen = 0;
505 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
508 63581804 2018-07-09 stsp if (avail > 0) {
510 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
511 63581804 2018-07-09 stsp if (n != avail) {
512 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
515 63581804 2018-07-09 stsp *outlen += avail;
517 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
520 63581804 2018-07-09 stsp if (err == NULL) {
521 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
522 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
524 63581804 2018-07-09 stsp got_inflate_end(&zb);
525 63581804 2018-07-09 stsp return err;
528 63581804 2018-07-09 stsp const struct got_error *
529 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
530 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
532 63581804 2018-07-09 stsp const struct got_error *err;
533 63581804 2018-07-09 stsp size_t avail;
534 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
536 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
540 63581804 2018-07-09 stsp *outlen = 0;
543 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
546 63581804 2018-07-09 stsp if (avail > 0) {
548 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
549 63581804 2018-07-09 stsp if (n != 1) {
550 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
553 63581804 2018-07-09 stsp *outlen += avail;
555 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
558 63581804 2018-07-09 stsp if (err == NULL)
559 63581804 2018-07-09 stsp rewind(outfile);
560 63581804 2018-07-09 stsp got_inflate_end(&zb);
561 63581804 2018-07-09 stsp return err;
564 63581804 2018-07-09 stsp const struct got_error *
565 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
566 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
568 63581804 2018-07-09 stsp const struct got_error *err;
569 4788f1ce 2020-03-18 stsp size_t avail, consumed;
570 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
572 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
576 63581804 2018-07-09 stsp *outlen = 0;
577 4788f1ce 2020-03-18 stsp if (consumed_total)
578 4788f1ce 2020-03-18 stsp *consumed_total = 0;
580 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
583 63581804 2018-07-09 stsp if (avail > 0) {
585 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
586 63581804 2018-07-09 stsp if (n != 1) {
587 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
590 63581804 2018-07-09 stsp *outlen += avail;
591 4788f1ce 2020-03-18 stsp if (consumed_total)
592 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
594 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
597 63581804 2018-07-09 stsp if (err == NULL)
598 63581804 2018-07-09 stsp rewind(outfile);
599 63581804 2018-07-09 stsp got_inflate_end(&zb);
600 63581804 2018-07-09 stsp return err;
603 63581804 2018-07-09 stsp const struct got_error *
604 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
605 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
606 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
608 63581804 2018-07-09 stsp const struct got_error *err;
609 4788f1ce 2020-03-18 stsp size_t avail, consumed;
610 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
612 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
616 63581804 2018-07-09 stsp *outlen = 0;
617 4788f1ce 2020-03-18 stsp if (consumed_total)
618 4788f1ce 2020-03-18 stsp *consumed_total = 0;
620 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
621 63581804 2018-07-09 stsp &consumed);
624 63581804 2018-07-09 stsp offset += consumed;
625 4788f1ce 2020-03-18 stsp if (consumed_total)
626 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
627 63581804 2018-07-09 stsp len -= consumed;
628 63581804 2018-07-09 stsp if (avail > 0) {
630 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
631 63581804 2018-07-09 stsp if (n != 1) {
632 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
635 63581804 2018-07-09 stsp *outlen += avail;
637 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
640 63581804 2018-07-09 stsp if (err == NULL)
641 63581804 2018-07-09 stsp rewind(outfile);
642 63581804 2018-07-09 stsp got_inflate_end(&zb);
643 63581804 2018-07-09 stsp return err;