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 68069cf6 2023-03-08 thomas #include "got_compat.h"
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 5211b8c8 2019-03-19 stsp #include <errno.h>
22 63581804 2018-07-09 stsp #include <stdio.h>
23 63581804 2018-07-09 stsp #include <stdlib.h>
24 63581804 2018-07-09 stsp #include <string.h>
25 3efd8e31 2022-10-23 thomas #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 b16893ba 2023-02-24 thomas #include "got_lib_hash.h"
35 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
36 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
39 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 63581804 2018-07-09 stsp const struct got_error *
43 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
44 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
46 63581804 2018-07-09 stsp const struct got_error *err = NULL;
49 fedfac2c 2022-11-08 thomas memset(zb, 0, sizeof(*zb));
51 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
52 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
53 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
54 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
55 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
56 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
57 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
58 5211b8c8 2019-03-19 stsp errno = ENOMEM;
59 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
61 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
64 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
66 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
67 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
68 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
72 63581804 2018-07-09 stsp zb->flags = 0;
73 63581804 2018-07-09 stsp if (outbuf == NULL) {
74 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
75 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
76 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
79 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
81 63581804 2018-07-09 stsp zb->outbuf = outbuf;
83 6ad68bce 2020-03-24 stsp zb->csum = csum;
86 63581804 2018-07-09 stsp got_inflate_end(zb);
91 dbaa2362 2021-09-28 thomas csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
93 6ad68bce 2020-03-24 stsp if (csum->input_crc)
94 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
96 b16893ba 2023-02-24 thomas if (csum->input_ctx)
97 b16893ba 2023-02-24 thomas got_hash_update(csum->input_ctx, buf, len);
100 d5c81d44 2021-07-08 stsp static void
101 dbaa2362 2021-09-28 thomas csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
103 d5c81d44 2021-07-08 stsp if (csum->output_crc)
104 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
106 b16893ba 2023-02-24 thomas if (csum->output_ctx)
107 b16893ba 2023-02-24 thomas got_hash_update(csum->output_ctx, buf, len);
110 63581804 2018-07-09 stsp const struct got_error *
111 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
112 abc59930 2021-09-05 naddy size_t *consumed)
114 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
115 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
116 63581804 2018-07-09 stsp z_stream *z = &zb->z;
117 63581804 2018-07-09 stsp int ret = Z_ERRNO;
119 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
120 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
122 63581804 2018-07-09 stsp *outlenp = 0;
123 6fb3a497 2020-03-18 stsp if (consumed)
124 6fb3a497 2020-03-18 stsp *consumed = 0;
126 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
127 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
129 63581804 2018-07-09 stsp if (z->avail_in == 0) {
130 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
131 63581804 2018-07-09 stsp if (n == 0) {
132 63581804 2018-07-09 stsp if (ferror(f))
133 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
135 63581804 2018-07-09 stsp ret = Z_STREAM_END;
138 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
139 63581804 2018-07-09 stsp z->avail_in = n;
141 6ad68bce 2020-03-24 stsp if (zb->csum) {
142 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
143 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
144 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
145 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
147 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
148 d5c81d44 2021-07-08 stsp if (zb->csum) {
149 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
150 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
151 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
152 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
154 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
156 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
157 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
159 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
160 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
161 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
164 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
165 6fb3a497 2020-03-18 stsp if (consumed)
166 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
167 63581804 2018-07-09 stsp return NULL;
170 63581804 2018-07-09 stsp const struct got_error *
171 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
172 3ab5e33c 2020-03-18 stsp size_t *consumed)
174 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
175 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
176 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
177 63581804 2018-07-09 stsp z_stream *z = &zb->z;
178 63581804 2018-07-09 stsp int ret = Z_ERRNO;
180 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
181 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
183 63581804 2018-07-09 stsp *outlenp = 0;
184 3ab5e33c 2020-03-18 stsp if (consumed)
185 3ab5e33c 2020-03-18 stsp *consumed = 0;
187 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
188 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
190 63581804 2018-07-09 stsp if (z->avail_in == 0) {
191 3efd8e31 2022-10-23 thomas ssize_t n;
192 3efd8e31 2022-10-23 thomas err = got_poll_fd(fd, POLLIN, INFTIM);
193 3efd8e31 2022-10-23 thomas if (err) {
194 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_EOF) {
195 3efd8e31 2022-10-23 thomas ret = Z_STREAM_END;
198 3efd8e31 2022-10-23 thomas return err;
200 3efd8e31 2022-10-23 thomas n = read(fd, zb->inbuf, zb->inlen);
202 638f9024 2019-05-13 stsp return got_error_from_errno("read");
203 63581804 2018-07-09 stsp else if (n == 0) {
205 63581804 2018-07-09 stsp ret = Z_STREAM_END;
208 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
209 63581804 2018-07-09 stsp z->avail_in = n;
211 6ad68bce 2020-03-24 stsp if (zb->csum) {
212 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
213 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
214 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
215 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
217 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
218 d5c81d44 2021-07-08 stsp if (zb->csum) {
219 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
220 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
221 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
222 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
224 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
226 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
227 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
229 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
230 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
231 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
234 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
235 3ab5e33c 2020-03-18 stsp if (consumed)
236 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
237 63581804 2018-07-09 stsp return NULL;
240 63581804 2018-07-09 stsp const struct got_error *
241 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
242 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
244 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
245 63581804 2018-07-09 stsp z_stream *z = &zb->z;
246 63581804 2018-07-09 stsp int ret = Z_ERRNO;
248 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
249 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
251 63581804 2018-07-09 stsp *outlenp = 0;
252 63581804 2018-07-09 stsp *consumed = 0;
255 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
256 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
257 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
259 63581804 2018-07-09 stsp if (z->avail_in == 0) {
260 63581804 2018-07-09 stsp if (len == 0) {
262 63581804 2018-07-09 stsp ret = Z_STREAM_END;
265 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
266 f6a55b40 2022-02-12 thomas if (len - *consumed > UINT_MAX)
267 f6a55b40 2022-02-12 thomas z->avail_in = UINT_MAX;
269 f6a55b40 2022-02-12 thomas z->avail_in = len - *consumed;
271 6ad68bce 2020-03-24 stsp if (zb->csum) {
272 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
273 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
274 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
275 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
277 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
278 d5c81d44 2021-07-08 stsp if (zb->csum) {
279 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
280 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
281 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
282 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
284 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
285 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
287 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
288 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
290 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
291 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
292 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
295 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
296 63581804 2018-07-09 stsp return NULL;
300 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
302 63581804 2018-07-09 stsp free(zb->inbuf);
303 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
304 63581804 2018-07-09 stsp free(zb->outbuf);
305 63581804 2018-07-09 stsp inflateEnd(&zb->z);
308 63581804 2018-07-09 stsp const struct got_error *
309 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
310 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
312 63581804 2018-07-09 stsp const struct got_error *err;
313 6fb3a497 2020-03-18 stsp size_t avail, consumed;
314 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
315 63581804 2018-07-09 stsp void *newbuf;
316 17d745b8 2018-07-23 stsp int nbuf = 1;
318 2decf4c6 2020-03-18 stsp if (outbuf) {
319 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
320 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
321 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
322 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
324 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
326 63581804 2018-07-09 stsp return err;
328 63581804 2018-07-09 stsp *outlen = 0;
329 6fb3a497 2020-03-18 stsp if (consumed_total)
330 6fb3a497 2020-03-18 stsp *consumed_total = 0;
333 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
336 63581804 2018-07-09 stsp *outlen += avail;
337 6fb3a497 2020-03-18 stsp if (consumed_total)
338 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
339 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
340 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
342 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
343 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
344 63581804 2018-07-09 stsp if (newbuf == NULL) {
345 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
346 63581804 2018-07-09 stsp free(*outbuf);
347 63581804 2018-07-09 stsp *outbuf = NULL;
348 63581804 2018-07-09 stsp *outlen = 0;
351 63581804 2018-07-09 stsp *outbuf = newbuf;
352 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
353 9c8bf189 2022-02-12 thomas zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
355 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
358 63581804 2018-07-09 stsp got_inflate_end(&zb);
359 63581804 2018-07-09 stsp return err;
362 63581804 2018-07-09 stsp const struct got_error *
363 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
364 b6b86fd1 2022-08-30 thomas size_t *consumed_total, struct got_inflate_checksum *csum,
365 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
367 63581804 2018-07-09 stsp const struct got_error *err;
368 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
369 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
370 63581804 2018-07-09 stsp void *newbuf;
371 17d745b8 2018-07-23 stsp int nbuf = 1;
372 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
374 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
375 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
376 55fdd257 2020-03-18 stsp bufsize = expected_size;
378 2decf4c6 2020-03-18 stsp if (outbuf) {
379 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
380 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
381 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
382 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
384 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
388 63581804 2018-07-09 stsp *outlen = 0;
389 3ab5e33c 2020-03-18 stsp if (consumed_total)
390 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
393 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
396 63581804 2018-07-09 stsp *outlen += avail;
397 3ab5e33c 2020-03-18 stsp if (consumed_total)
398 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
399 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
400 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
402 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
403 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
404 63581804 2018-07-09 stsp if (newbuf == NULL) {
405 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
406 63581804 2018-07-09 stsp free(*outbuf);
407 63581804 2018-07-09 stsp *outbuf = NULL;
408 63581804 2018-07-09 stsp *outlen = 0;
411 63581804 2018-07-09 stsp *outbuf = newbuf;
412 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
413 9c8bf189 2022-02-12 thomas zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
415 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
418 63581804 2018-07-09 stsp got_inflate_end(&zb);
419 63581804 2018-07-09 stsp return err;
422 63581804 2018-07-09 stsp const struct got_error *
423 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
424 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
425 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
427 63581804 2018-07-09 stsp const struct got_error *err;
428 37bd7602 2018-07-23 stsp size_t avail, consumed;
429 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
430 63581804 2018-07-09 stsp void *newbuf;
431 37bd7602 2018-07-23 stsp int nbuf = 1;
433 2e5a6fad 2020-03-18 stsp if (outbuf) {
434 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
435 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
436 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
437 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
439 2e5a6fad 2020-03-18 stsp free(*outbuf);
440 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
441 2e5a6fad 2020-03-18 stsp return err;
444 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
446 d94c35b0 2022-01-23 thomas return err;
449 63581804 2018-07-09 stsp *outlen = 0;
450 2e5a6fad 2020-03-18 stsp if (consumed_total)
451 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
453 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
454 63581804 2018-07-09 stsp &consumed);
457 63581804 2018-07-09 stsp offset += consumed;
458 2e5a6fad 2020-03-18 stsp if (consumed_total)
459 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
460 63581804 2018-07-09 stsp len -= consumed;
461 63581804 2018-07-09 stsp *outlen += avail;
462 63581804 2018-07-09 stsp if (len == 0)
464 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
465 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
467 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
468 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
469 63581804 2018-07-09 stsp if (newbuf == NULL) {
470 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
471 63581804 2018-07-09 stsp free(*outbuf);
472 63581804 2018-07-09 stsp *outbuf = NULL;
473 63581804 2018-07-09 stsp *outlen = 0;
476 63581804 2018-07-09 stsp *outbuf = newbuf;
477 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
478 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
480 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
482 63581804 2018-07-09 stsp got_inflate_end(&zb);
483 63581804 2018-07-09 stsp return err;
486 63581804 2018-07-09 stsp const struct got_error *
487 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
488 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
490 63581804 2018-07-09 stsp const struct got_error *err = NULL;
491 63581804 2018-07-09 stsp size_t avail;
492 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
494 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
498 63581804 2018-07-09 stsp *outlen = 0;
501 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
504 63581804 2018-07-09 stsp if (avail > 0) {
506 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
507 63581804 2018-07-09 stsp if (n != avail) {
508 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
511 63581804 2018-07-09 stsp *outlen += avail;
513 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
516 63581804 2018-07-09 stsp if (err == NULL) {
517 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
518 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
520 63581804 2018-07-09 stsp got_inflate_end(&zb);
521 63581804 2018-07-09 stsp return err;
524 63581804 2018-07-09 stsp const struct got_error *
525 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
526 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
528 63581804 2018-07-09 stsp const struct got_error *err;
529 63581804 2018-07-09 stsp size_t avail;
530 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
532 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
536 63581804 2018-07-09 stsp *outlen = 0;
539 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
542 63581804 2018-07-09 stsp if (avail > 0) {
544 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
545 63581804 2018-07-09 stsp if (n != 1) {
546 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
549 63581804 2018-07-09 stsp *outlen += avail;
551 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
554 63581804 2018-07-09 stsp if (err == NULL)
555 63581804 2018-07-09 stsp rewind(outfile);
556 63581804 2018-07-09 stsp got_inflate_end(&zb);
557 63581804 2018-07-09 stsp return err;
560 63581804 2018-07-09 stsp const struct got_error *
561 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
562 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
564 63581804 2018-07-09 stsp const struct got_error *err;
565 4788f1ce 2020-03-18 stsp size_t avail, consumed;
566 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
568 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
572 63581804 2018-07-09 stsp *outlen = 0;
573 4788f1ce 2020-03-18 stsp if (consumed_total)
574 4788f1ce 2020-03-18 stsp *consumed_total = 0;
576 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
579 63581804 2018-07-09 stsp if (avail > 0) {
581 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
582 63581804 2018-07-09 stsp if (n != 1) {
583 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
586 63581804 2018-07-09 stsp *outlen += avail;
587 4788f1ce 2020-03-18 stsp if (consumed_total)
588 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
590 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
593 63581804 2018-07-09 stsp if (err == NULL)
594 63581804 2018-07-09 stsp rewind(outfile);
595 63581804 2018-07-09 stsp got_inflate_end(&zb);
596 63581804 2018-07-09 stsp return err;
599 63581804 2018-07-09 stsp const struct got_error *
600 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
601 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
602 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
604 63581804 2018-07-09 stsp const struct got_error *err;
605 4788f1ce 2020-03-18 stsp size_t avail, consumed;
606 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
608 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
612 63581804 2018-07-09 stsp *outlen = 0;
613 4788f1ce 2020-03-18 stsp if (consumed_total)
614 4788f1ce 2020-03-18 stsp *consumed_total = 0;
616 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
617 63581804 2018-07-09 stsp &consumed);
620 63581804 2018-07-09 stsp offset += consumed;
621 4788f1ce 2020-03-18 stsp if (consumed_total)
622 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
623 63581804 2018-07-09 stsp len -= consumed;
624 63581804 2018-07-09 stsp if (avail > 0) {
626 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
627 63581804 2018-07-09 stsp if (n != 1) {
628 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
631 63581804 2018-07-09 stsp *outlen += avail;
633 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
636 63581804 2018-07-09 stsp if (err == NULL)
637 63581804 2018-07-09 stsp rewind(outfile);
638 63581804 2018-07-09 stsp got_inflate_end(&zb);
639 63581804 2018-07-09 stsp return err;