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 69c6accf 2023-02-04 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 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
38 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
41 63581804 2018-07-09 stsp const struct got_error *
42 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
43 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
45 63581804 2018-07-09 stsp const struct got_error *err = NULL;
48 08603e79 2022-11-08 stsp memset(zb, 0, sizeof(*zb));
50 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
51 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
52 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
53 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
54 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
55 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
56 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
57 5211b8c8 2019-03-19 stsp errno = ENOMEM;
58 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
60 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
63 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
65 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
66 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
67 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
71 63581804 2018-07-09 stsp zb->flags = 0;
72 63581804 2018-07-09 stsp if (outbuf == NULL) {
73 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
74 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
75 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
78 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
80 63581804 2018-07-09 stsp zb->outbuf = outbuf;
82 6ad68bce 2020-03-24 stsp zb->csum = csum;
85 63581804 2018-07-09 stsp got_inflate_end(zb);
90 31e61ec1 2021-09-28 naddy csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
92 6ad68bce 2020-03-24 stsp if (csum->input_crc)
93 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
95 89720b41 2023-02-04 op if (csum->input_sha1)
96 89720b41 2023-02-04 op SHA1Update(csum->input_sha1, buf, len);
98 89720b41 2023-02-04 op if (csum->input_sha256)
99 89720b41 2023-02-04 op SHA256Update(csum->input_sha256, buf, len);
102 d5c81d44 2021-07-08 stsp static void
103 31e61ec1 2021-09-28 naddy csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
105 d5c81d44 2021-07-08 stsp if (csum->output_crc)
106 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
108 89720b41 2023-02-04 op if (csum->output_sha1)
109 89720b41 2023-02-04 op SHA1Update(csum->output_sha1, buf, len);
111 89720b41 2023-02-04 op if (csum->output_sha256)
112 89720b41 2023-02-04 op SHA256Update(csum->output_sha256, buf, len);
115 63581804 2018-07-09 stsp const struct got_error *
116 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
117 abc59930 2021-09-05 naddy size_t *consumed)
119 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
120 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
121 63581804 2018-07-09 stsp z_stream *z = &zb->z;
122 63581804 2018-07-09 stsp int ret = Z_ERRNO;
124 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
125 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
127 63581804 2018-07-09 stsp *outlenp = 0;
128 6fb3a497 2020-03-18 stsp if (consumed)
129 6fb3a497 2020-03-18 stsp *consumed = 0;
131 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
132 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
134 63581804 2018-07-09 stsp if (z->avail_in == 0) {
135 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
136 63581804 2018-07-09 stsp if (n == 0) {
137 63581804 2018-07-09 stsp if (ferror(f))
138 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
140 63581804 2018-07-09 stsp ret = Z_STREAM_END;
143 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
144 63581804 2018-07-09 stsp z->avail_in = n;
146 6ad68bce 2020-03-24 stsp if (zb->csum) {
147 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
148 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
149 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
150 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
152 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
153 d5c81d44 2021-07-08 stsp if (zb->csum) {
154 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
155 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
156 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
157 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
159 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
161 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
162 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
164 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
165 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
166 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
169 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
170 6fb3a497 2020-03-18 stsp if (consumed)
171 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
172 63581804 2018-07-09 stsp return NULL;
175 63581804 2018-07-09 stsp const struct got_error *
176 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
177 3ab5e33c 2020-03-18 stsp size_t *consumed)
179 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
180 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
181 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
182 63581804 2018-07-09 stsp z_stream *z = &zb->z;
183 63581804 2018-07-09 stsp int ret = Z_ERRNO;
185 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
186 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
188 63581804 2018-07-09 stsp *outlenp = 0;
189 3ab5e33c 2020-03-18 stsp if (consumed)
190 3ab5e33c 2020-03-18 stsp *consumed = 0;
192 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
193 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
195 63581804 2018-07-09 stsp if (z->avail_in == 0) {
197 13b2bc37 2022-10-23 stsp err = got_poll_fd(fd, POLLIN, INFTIM);
199 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF) {
200 13b2bc37 2022-10-23 stsp ret = Z_STREAM_END;
203 13b2bc37 2022-10-23 stsp return err;
205 13b2bc37 2022-10-23 stsp n = read(fd, zb->inbuf, zb->inlen);
207 638f9024 2019-05-13 stsp return got_error_from_errno("read");
208 63581804 2018-07-09 stsp else if (n == 0) {
210 63581804 2018-07-09 stsp ret = Z_STREAM_END;
213 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
214 63581804 2018-07-09 stsp z->avail_in = n;
216 6ad68bce 2020-03-24 stsp if (zb->csum) {
217 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
218 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
219 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
220 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
222 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
223 d5c81d44 2021-07-08 stsp if (zb->csum) {
224 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
225 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
226 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
227 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
229 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
231 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
232 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
234 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
235 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
236 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
239 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
240 3ab5e33c 2020-03-18 stsp if (consumed)
241 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
242 63581804 2018-07-09 stsp return NULL;
245 63581804 2018-07-09 stsp const struct got_error *
246 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
247 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
249 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
250 63581804 2018-07-09 stsp z_stream *z = &zb->z;
251 63581804 2018-07-09 stsp int ret = Z_ERRNO;
253 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
254 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
256 63581804 2018-07-09 stsp *outlenp = 0;
257 63581804 2018-07-09 stsp *consumed = 0;
260 31e61ec1 2021-09-28 naddy uint8_t *csum_in = NULL, *csum_out = NULL;
261 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
262 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
264 63581804 2018-07-09 stsp if (z->avail_in == 0) {
265 63581804 2018-07-09 stsp if (len == 0) {
267 63581804 2018-07-09 stsp ret = Z_STREAM_END;
270 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
271 a9bd296d 2022-02-08 stsp if (len - *consumed > UINT_MAX)
272 a9bd296d 2022-02-08 stsp z->avail_in = UINT_MAX;
274 a9bd296d 2022-02-08 stsp z->avail_in = len - *consumed;
276 6ad68bce 2020-03-24 stsp if (zb->csum) {
277 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
278 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
279 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
280 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
282 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
283 d5c81d44 2021-07-08 stsp if (zb->csum) {
284 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
285 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
286 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
287 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
289 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
290 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
292 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
293 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
295 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
296 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
297 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
300 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
301 63581804 2018-07-09 stsp return NULL;
305 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
307 63581804 2018-07-09 stsp free(zb->inbuf);
308 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
309 63581804 2018-07-09 stsp free(zb->outbuf);
310 63581804 2018-07-09 stsp inflateEnd(&zb->z);
313 63581804 2018-07-09 stsp const struct got_error *
314 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
315 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
317 63581804 2018-07-09 stsp const struct got_error *err;
318 6fb3a497 2020-03-18 stsp size_t avail, consumed;
319 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
320 63581804 2018-07-09 stsp void *newbuf;
321 17d745b8 2018-07-23 stsp int nbuf = 1;
323 2decf4c6 2020-03-18 stsp if (outbuf) {
324 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
325 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
326 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
327 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
329 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
331 63581804 2018-07-09 stsp return err;
333 63581804 2018-07-09 stsp *outlen = 0;
334 6fb3a497 2020-03-18 stsp if (consumed_total)
335 6fb3a497 2020-03-18 stsp *consumed_total = 0;
338 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
341 63581804 2018-07-09 stsp *outlen += avail;
342 6fb3a497 2020-03-18 stsp if (consumed_total)
343 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
344 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
345 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
347 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
348 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
349 63581804 2018-07-09 stsp if (newbuf == NULL) {
350 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
351 63581804 2018-07-09 stsp free(*outbuf);
352 63581804 2018-07-09 stsp *outbuf = NULL;
353 63581804 2018-07-09 stsp *outlen = 0;
356 63581804 2018-07-09 stsp *outbuf = newbuf;
357 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
358 d75b4088 2022-02-08 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
360 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
363 63581804 2018-07-09 stsp got_inflate_end(&zb);
364 63581804 2018-07-09 stsp return err;
367 63581804 2018-07-09 stsp const struct got_error *
368 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
369 5e91dae4 2022-08-30 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
370 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
372 63581804 2018-07-09 stsp const struct got_error *err;
373 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
374 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
375 63581804 2018-07-09 stsp void *newbuf;
376 17d745b8 2018-07-23 stsp int nbuf = 1;
377 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
379 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
380 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
381 55fdd257 2020-03-18 stsp bufsize = expected_size;
383 2decf4c6 2020-03-18 stsp if (outbuf) {
384 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
385 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
386 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
387 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
389 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
393 63581804 2018-07-09 stsp *outlen = 0;
394 3ab5e33c 2020-03-18 stsp if (consumed_total)
395 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
398 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
401 63581804 2018-07-09 stsp *outlen += avail;
402 3ab5e33c 2020-03-18 stsp if (consumed_total)
403 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
404 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
405 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
407 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
408 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
409 63581804 2018-07-09 stsp if (newbuf == NULL) {
410 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
411 63581804 2018-07-09 stsp free(*outbuf);
412 63581804 2018-07-09 stsp *outbuf = NULL;
413 63581804 2018-07-09 stsp *outlen = 0;
416 63581804 2018-07-09 stsp *outbuf = newbuf;
417 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
418 d75b4088 2022-02-08 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
420 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
423 63581804 2018-07-09 stsp got_inflate_end(&zb);
424 63581804 2018-07-09 stsp return err;
427 63581804 2018-07-09 stsp const struct got_error *
428 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
429 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
430 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
432 63581804 2018-07-09 stsp const struct got_error *err;
433 37bd7602 2018-07-23 stsp size_t avail, consumed;
434 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
435 63581804 2018-07-09 stsp void *newbuf;
436 37bd7602 2018-07-23 stsp int nbuf = 1;
438 2e5a6fad 2020-03-18 stsp if (outbuf) {
439 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
440 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
441 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
442 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
444 2e5a6fad 2020-03-18 stsp free(*outbuf);
445 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
446 2e5a6fad 2020-03-18 stsp return err;
449 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
451 30ee8dc8 2022-01-18 stsp return err;
454 63581804 2018-07-09 stsp *outlen = 0;
455 2e5a6fad 2020-03-18 stsp if (consumed_total)
456 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
458 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
459 63581804 2018-07-09 stsp &consumed);
462 63581804 2018-07-09 stsp offset += consumed;
463 2e5a6fad 2020-03-18 stsp if (consumed_total)
464 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
465 63581804 2018-07-09 stsp len -= consumed;
466 63581804 2018-07-09 stsp *outlen += avail;
467 63581804 2018-07-09 stsp if (len == 0)
469 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
470 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
472 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
473 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
474 63581804 2018-07-09 stsp if (newbuf == NULL) {
475 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
476 63581804 2018-07-09 stsp free(*outbuf);
477 63581804 2018-07-09 stsp *outbuf = NULL;
478 63581804 2018-07-09 stsp *outlen = 0;
481 63581804 2018-07-09 stsp *outbuf = newbuf;
482 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
483 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
485 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
487 63581804 2018-07-09 stsp got_inflate_end(&zb);
488 63581804 2018-07-09 stsp return err;
491 63581804 2018-07-09 stsp const struct got_error *
492 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
493 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
495 63581804 2018-07-09 stsp const struct got_error *err = NULL;
496 63581804 2018-07-09 stsp size_t avail;
497 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
499 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
503 63581804 2018-07-09 stsp *outlen = 0;
506 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
509 63581804 2018-07-09 stsp if (avail > 0) {
511 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
512 63581804 2018-07-09 stsp if (n != avail) {
513 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
516 63581804 2018-07-09 stsp *outlen += avail;
518 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
521 63581804 2018-07-09 stsp if (err == NULL) {
522 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
523 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
525 63581804 2018-07-09 stsp got_inflate_end(&zb);
526 63581804 2018-07-09 stsp return err;
529 63581804 2018-07-09 stsp const struct got_error *
530 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
531 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
533 63581804 2018-07-09 stsp const struct got_error *err;
534 63581804 2018-07-09 stsp size_t avail;
535 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
537 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
541 63581804 2018-07-09 stsp *outlen = 0;
544 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
547 63581804 2018-07-09 stsp if (avail > 0) {
549 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
550 63581804 2018-07-09 stsp if (n != 1) {
551 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
554 63581804 2018-07-09 stsp *outlen += avail;
556 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
559 63581804 2018-07-09 stsp if (err == NULL)
560 63581804 2018-07-09 stsp rewind(outfile);
561 63581804 2018-07-09 stsp got_inflate_end(&zb);
562 63581804 2018-07-09 stsp return err;
565 63581804 2018-07-09 stsp const struct got_error *
566 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
567 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
569 63581804 2018-07-09 stsp const struct got_error *err;
570 4788f1ce 2020-03-18 stsp size_t avail, consumed;
571 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
573 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
577 63581804 2018-07-09 stsp *outlen = 0;
578 4788f1ce 2020-03-18 stsp if (consumed_total)
579 4788f1ce 2020-03-18 stsp *consumed_total = 0;
581 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
584 63581804 2018-07-09 stsp if (avail > 0) {
586 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
587 63581804 2018-07-09 stsp if (n != 1) {
588 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
591 63581804 2018-07-09 stsp *outlen += avail;
592 4788f1ce 2020-03-18 stsp if (consumed_total)
593 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
595 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
598 63581804 2018-07-09 stsp if (err == NULL)
599 63581804 2018-07-09 stsp rewind(outfile);
600 63581804 2018-07-09 stsp got_inflate_end(&zb);
601 63581804 2018-07-09 stsp return err;
604 63581804 2018-07-09 stsp const struct got_error *
605 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
606 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
607 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
609 63581804 2018-07-09 stsp const struct got_error *err;
610 4788f1ce 2020-03-18 stsp size_t avail, consumed;
611 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
613 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
617 63581804 2018-07-09 stsp *outlen = 0;
618 4788f1ce 2020-03-18 stsp if (consumed_total)
619 4788f1ce 2020-03-18 stsp *consumed_total = 0;
621 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
622 63581804 2018-07-09 stsp &consumed);
625 63581804 2018-07-09 stsp offset += consumed;
626 4788f1ce 2020-03-18 stsp if (consumed_total)
627 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
628 63581804 2018-07-09 stsp len -= consumed;
629 63581804 2018-07-09 stsp if (avail > 0) {
631 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
632 63581804 2018-07-09 stsp if (n != 1) {
633 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
636 63581804 2018-07-09 stsp *outlen += avail;
638 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
641 63581804 2018-07-09 stsp if (err == NULL)
642 63581804 2018-07-09 stsp rewind(outfile);
643 63581804 2018-07-09 stsp got_inflate_end(&zb);
644 63581804 2018-07-09 stsp return err;