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 8b925c6c 2022-07-16 thomas #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 3efd8e31 2022-10-23 thomas #include <poll.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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
29 63581804 2018-07-09 stsp #include "got_error.h"
30 63581804 2018-07-09 stsp #include "got_object.h"
31 324d37e7 2019-05-11 stsp #include "got_path.h"
33 b16893ba 2023-02-24 thomas #include "got_lib_hash.h"
34 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 3efd8e31 2022-10-23 thomas #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 fedfac2c 2022-11-08 thomas 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 dbaa2362 2021-09-28 thomas 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 b16893ba 2023-02-24 thomas if (csum->input_ctx)
96 b16893ba 2023-02-24 thomas got_hash_update(csum->input_ctx, buf, len);
100 dbaa2362 2021-09-28 thomas csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
102 d5c81d44 2021-07-08 stsp if (csum->output_crc)
103 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
105 b16893ba 2023-02-24 thomas if (csum->output_ctx)
106 b16893ba 2023-02-24 thomas got_hash_update(csum->output_ctx, buf, len);
109 63581804 2018-07-09 stsp const struct got_error *
110 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
111 abc59930 2021-09-05 naddy size_t *consumed)
113 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
114 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
115 63581804 2018-07-09 stsp z_stream *z = &zb->z;
116 63581804 2018-07-09 stsp int ret = Z_ERRNO;
118 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
119 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
121 63581804 2018-07-09 stsp *outlenp = 0;
122 6fb3a497 2020-03-18 stsp if (consumed)
123 6fb3a497 2020-03-18 stsp *consumed = 0;
125 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
126 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
128 63581804 2018-07-09 stsp if (z->avail_in == 0) {
129 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
130 63581804 2018-07-09 stsp if (n == 0) {
131 63581804 2018-07-09 stsp if (ferror(f))
132 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
134 63581804 2018-07-09 stsp ret = Z_STREAM_END;
137 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
138 63581804 2018-07-09 stsp z->avail_in = n;
140 6ad68bce 2020-03-24 stsp if (zb->csum) {
141 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
142 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
143 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
144 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
146 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
147 d5c81d44 2021-07-08 stsp if (zb->csum) {
148 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
149 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
150 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
151 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
153 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
155 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
156 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
158 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
159 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
160 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
163 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
164 6fb3a497 2020-03-18 stsp if (consumed)
165 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
166 63581804 2018-07-09 stsp return NULL;
169 63581804 2018-07-09 stsp const struct got_error *
170 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
171 3ab5e33c 2020-03-18 stsp size_t *consumed)
173 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
174 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
175 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
176 63581804 2018-07-09 stsp z_stream *z = &zb->z;
177 63581804 2018-07-09 stsp int ret = Z_ERRNO;
179 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
180 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
182 63581804 2018-07-09 stsp *outlenp = 0;
183 3ab5e33c 2020-03-18 stsp if (consumed)
184 3ab5e33c 2020-03-18 stsp *consumed = 0;
186 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
187 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
189 63581804 2018-07-09 stsp if (z->avail_in == 0) {
190 3efd8e31 2022-10-23 thomas ssize_t n;
191 3efd8e31 2022-10-23 thomas err = got_poll_fd(fd, POLLIN, INFTIM);
192 3efd8e31 2022-10-23 thomas if (err) {
193 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_EOF) {
194 3efd8e31 2022-10-23 thomas ret = Z_STREAM_END;
197 3efd8e31 2022-10-23 thomas return err;
199 3efd8e31 2022-10-23 thomas n = read(fd, zb->inbuf, zb->inlen);
201 638f9024 2019-05-13 stsp return got_error_from_errno("read");
202 63581804 2018-07-09 stsp else if (n == 0) {
204 63581804 2018-07-09 stsp ret = Z_STREAM_END;
207 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
208 63581804 2018-07-09 stsp z->avail_in = n;
210 6ad68bce 2020-03-24 stsp if (zb->csum) {
211 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
212 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
213 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
214 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
216 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
217 d5c81d44 2021-07-08 stsp if (zb->csum) {
218 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
219 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
220 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
221 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
223 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
225 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
226 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
228 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
229 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
230 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
233 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
234 3ab5e33c 2020-03-18 stsp if (consumed)
235 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
236 63581804 2018-07-09 stsp return NULL;
239 63581804 2018-07-09 stsp const struct got_error *
240 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
241 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
243 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
244 63581804 2018-07-09 stsp z_stream *z = &zb->z;
245 63581804 2018-07-09 stsp int ret = Z_ERRNO;
247 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
248 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
250 63581804 2018-07-09 stsp *outlenp = 0;
251 63581804 2018-07-09 stsp *consumed = 0;
254 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
255 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
256 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
258 63581804 2018-07-09 stsp if (z->avail_in == 0) {
259 63581804 2018-07-09 stsp if (len == 0) {
261 63581804 2018-07-09 stsp ret = Z_STREAM_END;
264 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
265 f6a55b40 2022-02-12 thomas if (len - *consumed > UINT_MAX)
266 f6a55b40 2022-02-12 thomas z->avail_in = UINT_MAX;
268 f6a55b40 2022-02-12 thomas z->avail_in = len - *consumed;
270 6ad68bce 2020-03-24 stsp if (zb->csum) {
271 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
272 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
273 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
274 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
276 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
277 d5c81d44 2021-07-08 stsp if (zb->csum) {
278 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
279 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
280 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
281 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
283 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
284 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
286 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
287 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
289 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
290 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
291 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
294 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
295 63581804 2018-07-09 stsp return NULL;
299 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
301 63581804 2018-07-09 stsp free(zb->inbuf);
302 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
303 63581804 2018-07-09 stsp free(zb->outbuf);
304 63581804 2018-07-09 stsp inflateEnd(&zb->z);
307 63581804 2018-07-09 stsp const struct got_error *
308 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
309 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
311 63581804 2018-07-09 stsp const struct got_error *err;
312 6fb3a497 2020-03-18 stsp size_t avail, consumed;
313 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
314 63581804 2018-07-09 stsp void *newbuf;
315 17d745b8 2018-07-23 stsp int nbuf = 1;
317 2decf4c6 2020-03-18 stsp if (outbuf) {
318 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
319 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
320 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
321 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
323 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
325 63581804 2018-07-09 stsp return err;
327 63581804 2018-07-09 stsp *outlen = 0;
328 6fb3a497 2020-03-18 stsp if (consumed_total)
329 6fb3a497 2020-03-18 stsp *consumed_total = 0;
332 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
335 63581804 2018-07-09 stsp *outlen += avail;
336 6fb3a497 2020-03-18 stsp if (consumed_total)
337 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
338 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
339 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
341 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
342 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
343 63581804 2018-07-09 stsp if (newbuf == NULL) {
344 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
345 63581804 2018-07-09 stsp free(*outbuf);
346 63581804 2018-07-09 stsp *outbuf = NULL;
347 63581804 2018-07-09 stsp *outlen = 0;
350 63581804 2018-07-09 stsp *outbuf = newbuf;
351 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
352 9c8bf189 2022-02-12 thomas zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
354 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
357 63581804 2018-07-09 stsp got_inflate_end(&zb);
358 63581804 2018-07-09 stsp return err;
361 63581804 2018-07-09 stsp const struct got_error *
362 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
363 b6b86fd1 2022-08-30 thomas size_t *consumed_total, struct got_inflate_checksum *csum,
364 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
366 63581804 2018-07-09 stsp const struct got_error *err;
367 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
368 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
369 63581804 2018-07-09 stsp void *newbuf;
370 17d745b8 2018-07-23 stsp int nbuf = 1;
371 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
373 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
374 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
375 55fdd257 2020-03-18 stsp bufsize = expected_size;
377 2decf4c6 2020-03-18 stsp if (outbuf) {
378 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
379 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
380 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
381 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
383 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
387 63581804 2018-07-09 stsp *outlen = 0;
388 3ab5e33c 2020-03-18 stsp if (consumed_total)
389 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
392 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
395 63581804 2018-07-09 stsp *outlen += avail;
396 3ab5e33c 2020-03-18 stsp if (consumed_total)
397 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
398 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
399 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
401 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
402 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
403 63581804 2018-07-09 stsp if (newbuf == NULL) {
404 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
405 63581804 2018-07-09 stsp free(*outbuf);
406 63581804 2018-07-09 stsp *outbuf = NULL;
407 63581804 2018-07-09 stsp *outlen = 0;
410 63581804 2018-07-09 stsp *outbuf = newbuf;
411 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
412 9c8bf189 2022-02-12 thomas zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
414 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
417 63581804 2018-07-09 stsp got_inflate_end(&zb);
418 63581804 2018-07-09 stsp return err;
421 63581804 2018-07-09 stsp const struct got_error *
422 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
423 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
424 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
426 63581804 2018-07-09 stsp const struct got_error *err;
427 37bd7602 2018-07-23 stsp size_t avail, consumed;
428 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
429 63581804 2018-07-09 stsp void *newbuf;
430 37bd7602 2018-07-23 stsp int nbuf = 1;
432 2e5a6fad 2020-03-18 stsp if (outbuf) {
433 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
434 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
435 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
436 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
438 2e5a6fad 2020-03-18 stsp free(*outbuf);
439 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
440 2e5a6fad 2020-03-18 stsp return err;
443 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
445 d94c35b0 2022-01-23 thomas return err;
448 63581804 2018-07-09 stsp *outlen = 0;
449 2e5a6fad 2020-03-18 stsp if (consumed_total)
450 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
452 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
453 63581804 2018-07-09 stsp &consumed);
456 63581804 2018-07-09 stsp offset += consumed;
457 2e5a6fad 2020-03-18 stsp if (consumed_total)
458 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
459 63581804 2018-07-09 stsp len -= consumed;
460 63581804 2018-07-09 stsp *outlen += avail;
461 63581804 2018-07-09 stsp if (len == 0)
463 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
464 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
466 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
467 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
468 63581804 2018-07-09 stsp if (newbuf == NULL) {
469 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
470 63581804 2018-07-09 stsp free(*outbuf);
471 63581804 2018-07-09 stsp *outbuf = NULL;
472 63581804 2018-07-09 stsp *outlen = 0;
475 63581804 2018-07-09 stsp *outbuf = newbuf;
476 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
477 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
479 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
481 63581804 2018-07-09 stsp got_inflate_end(&zb);
482 63581804 2018-07-09 stsp return err;
485 63581804 2018-07-09 stsp const struct got_error *
486 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
487 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
489 63581804 2018-07-09 stsp const struct got_error *err = NULL;
490 63581804 2018-07-09 stsp size_t avail;
491 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
493 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
497 63581804 2018-07-09 stsp *outlen = 0;
500 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
503 63581804 2018-07-09 stsp if (avail > 0) {
505 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
506 63581804 2018-07-09 stsp if (n != avail) {
507 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
510 63581804 2018-07-09 stsp *outlen += avail;
512 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
515 63581804 2018-07-09 stsp if (err == NULL) {
516 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
517 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
519 63581804 2018-07-09 stsp got_inflate_end(&zb);
520 63581804 2018-07-09 stsp return err;
523 63581804 2018-07-09 stsp const struct got_error *
524 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
525 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
527 63581804 2018-07-09 stsp const struct got_error *err;
528 63581804 2018-07-09 stsp size_t avail;
529 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
531 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
535 63581804 2018-07-09 stsp *outlen = 0;
538 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
541 63581804 2018-07-09 stsp if (avail > 0) {
543 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
544 63581804 2018-07-09 stsp if (n != 1) {
545 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
548 63581804 2018-07-09 stsp *outlen += avail;
550 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
553 63581804 2018-07-09 stsp if (err == NULL)
554 63581804 2018-07-09 stsp rewind(outfile);
555 63581804 2018-07-09 stsp got_inflate_end(&zb);
556 63581804 2018-07-09 stsp return err;
559 63581804 2018-07-09 stsp const struct got_error *
560 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
561 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
563 63581804 2018-07-09 stsp const struct got_error *err;
564 4788f1ce 2020-03-18 stsp size_t avail, consumed;
565 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
567 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
571 63581804 2018-07-09 stsp *outlen = 0;
572 4788f1ce 2020-03-18 stsp if (consumed_total)
573 4788f1ce 2020-03-18 stsp *consumed_total = 0;
575 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
578 63581804 2018-07-09 stsp if (avail > 0) {
580 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
581 63581804 2018-07-09 stsp if (n != 1) {
582 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
585 63581804 2018-07-09 stsp *outlen += avail;
586 4788f1ce 2020-03-18 stsp if (consumed_total)
587 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
589 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
592 63581804 2018-07-09 stsp if (err == NULL)
593 63581804 2018-07-09 stsp rewind(outfile);
594 63581804 2018-07-09 stsp got_inflate_end(&zb);
595 63581804 2018-07-09 stsp return err;
598 63581804 2018-07-09 stsp const struct got_error *
599 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
600 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
601 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
603 63581804 2018-07-09 stsp const struct got_error *err;
604 4788f1ce 2020-03-18 stsp size_t avail, consumed;
605 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
607 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
611 63581804 2018-07-09 stsp *outlen = 0;
612 4788f1ce 2020-03-18 stsp if (consumed_total)
613 4788f1ce 2020-03-18 stsp *consumed_total = 0;
615 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
616 63581804 2018-07-09 stsp &consumed);
619 63581804 2018-07-09 stsp offset += consumed;
620 4788f1ce 2020-03-18 stsp if (consumed_total)
621 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
622 63581804 2018-07-09 stsp len -= consumed;
623 63581804 2018-07-09 stsp if (avail > 0) {
625 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
626 63581804 2018-07-09 stsp if (n != 1) {
627 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
630 63581804 2018-07-09 stsp *outlen += avail;
632 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
635 63581804 2018-07-09 stsp if (err == NULL)
636 63581804 2018-07-09 stsp rewind(outfile);
637 63581804 2018-07-09 stsp got_inflate_end(&zb);
638 63581804 2018-07-09 stsp return err;