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 81a12da5 2020-09-09 naddy #include <unistd.h>
24 63581804 2018-07-09 stsp #include <zlib.h>
25 63581804 2018-07-09 stsp #include <time.h>
27 63581804 2018-07-09 stsp #include "got_error.h"
28 63581804 2018-07-09 stsp #include "got_object.h"
29 324d37e7 2019-05-11 stsp #include "got_path.h"
31 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
34 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 63581804 2018-07-09 stsp const struct got_error *
38 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
39 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
41 63581804 2018-07-09 stsp const struct got_error *err = NULL;
44 63581804 2018-07-09 stsp memset(&zb->z, 0, sizeof(zb->z));
46 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
47 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
48 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
49 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
50 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
51 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
52 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
53 5211b8c8 2019-03-19 stsp errno = ENOMEM;
54 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
56 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
59 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
61 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
62 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
63 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
67 63581804 2018-07-09 stsp zb->flags = 0;
68 63581804 2018-07-09 stsp if (outbuf == NULL) {
69 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
70 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
71 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
74 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
76 63581804 2018-07-09 stsp zb->outbuf = outbuf;
78 6ad68bce 2020-03-24 stsp zb->csum = csum;
81 63581804 2018-07-09 stsp got_inflate_end(zb);
86 dbaa2362 2021-09-28 thomas csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
88 6ad68bce 2020-03-24 stsp if (csum->input_crc)
89 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
91 6ad68bce 2020-03-24 stsp if (csum->input_sha1)
92 6ad68bce 2020-03-24 stsp SHA1Update(csum->input_sha1, buf, len);
96 dbaa2362 2021-09-28 thomas csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
98 d5c81d44 2021-07-08 stsp if (csum->output_crc)
99 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
101 d5c81d44 2021-07-08 stsp if (csum->output_sha1)
102 d5c81d44 2021-07-08 stsp SHA1Update(csum->output_sha1, buf, len);
105 63581804 2018-07-09 stsp const struct got_error *
106 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
107 abc59930 2021-09-05 naddy size_t *consumed)
109 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
110 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
111 63581804 2018-07-09 stsp z_stream *z = &zb->z;
112 63581804 2018-07-09 stsp int ret = Z_ERRNO;
114 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
115 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
117 63581804 2018-07-09 stsp *outlenp = 0;
118 6fb3a497 2020-03-18 stsp if (consumed)
119 6fb3a497 2020-03-18 stsp *consumed = 0;
121 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
122 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
124 63581804 2018-07-09 stsp if (z->avail_in == 0) {
125 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
126 63581804 2018-07-09 stsp if (n == 0) {
127 63581804 2018-07-09 stsp if (ferror(f))
128 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
130 63581804 2018-07-09 stsp ret = Z_STREAM_END;
133 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
134 63581804 2018-07-09 stsp z->avail_in = n;
136 6ad68bce 2020-03-24 stsp if (zb->csum) {
137 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
138 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
139 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
140 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
142 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
143 d5c81d44 2021-07-08 stsp if (zb->csum) {
144 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
145 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
146 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
147 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
149 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
151 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
152 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
154 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
155 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
156 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
159 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
160 6fb3a497 2020-03-18 stsp if (consumed)
161 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
162 63581804 2018-07-09 stsp return NULL;
165 63581804 2018-07-09 stsp const struct got_error *
166 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
167 3ab5e33c 2020-03-18 stsp size_t *consumed)
169 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
170 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
171 63581804 2018-07-09 stsp z_stream *z = &zb->z;
172 63581804 2018-07-09 stsp int ret = Z_ERRNO;
174 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
175 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
177 63581804 2018-07-09 stsp *outlenp = 0;
178 3ab5e33c 2020-03-18 stsp if (consumed)
179 3ab5e33c 2020-03-18 stsp *consumed = 0;
181 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
182 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
184 63581804 2018-07-09 stsp if (z->avail_in == 0) {
185 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
187 638f9024 2019-05-13 stsp return got_error_from_errno("read");
188 63581804 2018-07-09 stsp else if (n == 0) {
190 63581804 2018-07-09 stsp ret = Z_STREAM_END;
193 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
194 63581804 2018-07-09 stsp z->avail_in = n;
196 6ad68bce 2020-03-24 stsp if (zb->csum) {
197 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
198 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
199 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
200 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
202 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
203 d5c81d44 2021-07-08 stsp if (zb->csum) {
204 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
205 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
206 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
207 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
209 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
211 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
212 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
214 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
215 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
216 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
219 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
220 3ab5e33c 2020-03-18 stsp if (consumed)
221 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
222 63581804 2018-07-09 stsp return NULL;
225 63581804 2018-07-09 stsp const struct got_error *
226 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
227 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
229 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
230 63581804 2018-07-09 stsp z_stream *z = &zb->z;
231 63581804 2018-07-09 stsp int ret = Z_ERRNO;
233 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
234 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
236 63581804 2018-07-09 stsp *outlenp = 0;
237 63581804 2018-07-09 stsp *consumed = 0;
240 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
241 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
242 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
244 63581804 2018-07-09 stsp if (z->avail_in == 0) {
245 63581804 2018-07-09 stsp if (len == 0) {
247 63581804 2018-07-09 stsp ret = Z_STREAM_END;
250 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
251 f6a55b40 2022-02-12 thomas if (len - *consumed > UINT_MAX)
252 f6a55b40 2022-02-12 thomas z->avail_in = UINT_MAX;
254 f6a55b40 2022-02-12 thomas z->avail_in = len - *consumed;
256 6ad68bce 2020-03-24 stsp if (zb->csum) {
257 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
258 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
259 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
260 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
262 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
263 d5c81d44 2021-07-08 stsp if (zb->csum) {
264 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
265 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
266 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
267 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
269 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
270 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
272 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
273 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
275 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
276 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
277 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
280 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
281 63581804 2018-07-09 stsp return NULL;
285 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
287 63581804 2018-07-09 stsp free(zb->inbuf);
288 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
289 63581804 2018-07-09 stsp free(zb->outbuf);
290 63581804 2018-07-09 stsp inflateEnd(&zb->z);
293 63581804 2018-07-09 stsp const struct got_error *
294 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
295 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
297 63581804 2018-07-09 stsp const struct got_error *err;
298 6fb3a497 2020-03-18 stsp size_t avail, consumed;
299 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
300 63581804 2018-07-09 stsp void *newbuf;
301 17d745b8 2018-07-23 stsp int nbuf = 1;
303 2decf4c6 2020-03-18 stsp if (outbuf) {
304 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
305 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
306 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
307 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
309 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
311 63581804 2018-07-09 stsp return err;
313 63581804 2018-07-09 stsp *outlen = 0;
314 6fb3a497 2020-03-18 stsp if (consumed_total)
315 6fb3a497 2020-03-18 stsp *consumed_total = 0;
318 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
321 63581804 2018-07-09 stsp *outlen += avail;
322 6fb3a497 2020-03-18 stsp if (consumed_total)
323 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
324 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
325 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
327 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
328 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
329 63581804 2018-07-09 stsp if (newbuf == NULL) {
330 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
331 63581804 2018-07-09 stsp free(*outbuf);
332 63581804 2018-07-09 stsp *outbuf = NULL;
333 63581804 2018-07-09 stsp *outlen = 0;
336 63581804 2018-07-09 stsp *outbuf = newbuf;
337 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
338 9c8bf189 2022-02-12 thomas zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
340 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
343 63581804 2018-07-09 stsp got_inflate_end(&zb);
344 63581804 2018-07-09 stsp return err;
347 63581804 2018-07-09 stsp const struct got_error *
348 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
349 b6b86fd1 2022-08-30 thomas size_t *consumed_total, struct got_inflate_checksum *csum,
350 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
352 63581804 2018-07-09 stsp const struct got_error *err;
353 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
354 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
355 63581804 2018-07-09 stsp void *newbuf;
356 17d745b8 2018-07-23 stsp int nbuf = 1;
357 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
359 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
360 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
361 55fdd257 2020-03-18 stsp bufsize = expected_size;
363 2decf4c6 2020-03-18 stsp if (outbuf) {
364 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
365 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
366 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
367 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
369 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
373 63581804 2018-07-09 stsp *outlen = 0;
374 3ab5e33c 2020-03-18 stsp if (consumed_total)
375 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
378 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
381 63581804 2018-07-09 stsp *outlen += avail;
382 3ab5e33c 2020-03-18 stsp if (consumed_total)
383 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
384 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
385 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
387 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
388 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
389 63581804 2018-07-09 stsp if (newbuf == NULL) {
390 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
391 63581804 2018-07-09 stsp free(*outbuf);
392 63581804 2018-07-09 stsp *outbuf = NULL;
393 63581804 2018-07-09 stsp *outlen = 0;
396 63581804 2018-07-09 stsp *outbuf = newbuf;
397 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
398 9c8bf189 2022-02-12 thomas zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
400 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
403 63581804 2018-07-09 stsp got_inflate_end(&zb);
404 63581804 2018-07-09 stsp return err;
407 63581804 2018-07-09 stsp const struct got_error *
408 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
409 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
410 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
412 63581804 2018-07-09 stsp const struct got_error *err;
413 37bd7602 2018-07-23 stsp size_t avail, consumed;
414 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
415 63581804 2018-07-09 stsp void *newbuf;
416 37bd7602 2018-07-23 stsp int nbuf = 1;
418 2e5a6fad 2020-03-18 stsp if (outbuf) {
419 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
420 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
421 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
422 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
424 2e5a6fad 2020-03-18 stsp free(*outbuf);
425 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
426 2e5a6fad 2020-03-18 stsp return err;
429 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
431 d94c35b0 2022-01-23 thomas return err;
434 63581804 2018-07-09 stsp *outlen = 0;
435 2e5a6fad 2020-03-18 stsp if (consumed_total)
436 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
438 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
439 63581804 2018-07-09 stsp &consumed);
442 63581804 2018-07-09 stsp offset += consumed;
443 2e5a6fad 2020-03-18 stsp if (consumed_total)
444 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
445 63581804 2018-07-09 stsp len -= consumed;
446 63581804 2018-07-09 stsp *outlen += avail;
447 63581804 2018-07-09 stsp if (len == 0)
449 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
450 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
452 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
453 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
454 63581804 2018-07-09 stsp if (newbuf == NULL) {
455 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
456 63581804 2018-07-09 stsp free(*outbuf);
457 63581804 2018-07-09 stsp *outbuf = NULL;
458 63581804 2018-07-09 stsp *outlen = 0;
461 63581804 2018-07-09 stsp *outbuf = newbuf;
462 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
463 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
465 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
467 63581804 2018-07-09 stsp got_inflate_end(&zb);
468 63581804 2018-07-09 stsp return err;
471 63581804 2018-07-09 stsp const struct got_error *
472 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
473 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
475 63581804 2018-07-09 stsp const struct got_error *err = NULL;
476 63581804 2018-07-09 stsp size_t avail;
477 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
479 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
483 63581804 2018-07-09 stsp *outlen = 0;
486 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
489 63581804 2018-07-09 stsp if (avail > 0) {
491 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
492 63581804 2018-07-09 stsp if (n != avail) {
493 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
496 63581804 2018-07-09 stsp *outlen += avail;
498 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
501 63581804 2018-07-09 stsp if (err == NULL) {
502 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
503 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
505 63581804 2018-07-09 stsp got_inflate_end(&zb);
506 63581804 2018-07-09 stsp return err;
509 63581804 2018-07-09 stsp const struct got_error *
510 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
511 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
513 63581804 2018-07-09 stsp const struct got_error *err;
514 63581804 2018-07-09 stsp size_t avail;
515 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
517 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
521 63581804 2018-07-09 stsp *outlen = 0;
524 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
527 63581804 2018-07-09 stsp if (avail > 0) {
529 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
530 63581804 2018-07-09 stsp if (n != 1) {
531 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
534 63581804 2018-07-09 stsp *outlen += avail;
536 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
539 63581804 2018-07-09 stsp if (err == NULL)
540 63581804 2018-07-09 stsp rewind(outfile);
541 63581804 2018-07-09 stsp got_inflate_end(&zb);
542 63581804 2018-07-09 stsp return err;
545 63581804 2018-07-09 stsp const struct got_error *
546 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
547 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
549 63581804 2018-07-09 stsp const struct got_error *err;
550 4788f1ce 2020-03-18 stsp size_t avail, consumed;
551 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
553 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
557 63581804 2018-07-09 stsp *outlen = 0;
558 4788f1ce 2020-03-18 stsp if (consumed_total)
559 4788f1ce 2020-03-18 stsp *consumed_total = 0;
561 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
564 63581804 2018-07-09 stsp if (avail > 0) {
566 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
567 63581804 2018-07-09 stsp if (n != 1) {
568 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
571 63581804 2018-07-09 stsp *outlen += avail;
572 4788f1ce 2020-03-18 stsp if (consumed_total)
573 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
575 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
578 63581804 2018-07-09 stsp if (err == NULL)
579 63581804 2018-07-09 stsp rewind(outfile);
580 63581804 2018-07-09 stsp got_inflate_end(&zb);
581 63581804 2018-07-09 stsp return err;
584 63581804 2018-07-09 stsp const struct got_error *
585 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
586 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
587 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
589 63581804 2018-07-09 stsp const struct got_error *err;
590 4788f1ce 2020-03-18 stsp size_t avail, consumed;
591 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
593 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
597 63581804 2018-07-09 stsp *outlen = 0;
598 4788f1ce 2020-03-18 stsp if (consumed_total)
599 4788f1ce 2020-03-18 stsp *consumed_total = 0;
601 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
602 63581804 2018-07-09 stsp &consumed);
605 63581804 2018-07-09 stsp offset += consumed;
606 4788f1ce 2020-03-18 stsp if (consumed_total)
607 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
608 63581804 2018-07-09 stsp len -= consumed;
609 63581804 2018-07-09 stsp if (avail > 0) {
611 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
612 63581804 2018-07-09 stsp if (n != 1) {
613 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
616 63581804 2018-07-09 stsp *outlen += avail;
618 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
621 63581804 2018-07-09 stsp if (err == NULL)
622 63581804 2018-07-09 stsp rewind(outfile);
623 63581804 2018-07-09 stsp got_inflate_end(&zb);
624 63581804 2018-07-09 stsp return err;