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.
18 5211b8c8 2019-03-19 stsp #include <errno.h>
19 63581804 2018-07-09 stsp #include <stdio.h>
20 63581804 2018-07-09 stsp #include <stdlib.h>
21 63581804 2018-07-09 stsp #include <string.h>
22 81a12da5 2020-09-09 naddy #include <unistd.h>
23 63581804 2018-07-09 stsp #include <zlib.h>
24 63581804 2018-07-09 stsp #include <time.h>
26 63581804 2018-07-09 stsp #include "got_error.h"
27 63581804 2018-07-09 stsp #include "got_object.h"
28 324d37e7 2019-05-11 stsp #include "got_path.h"
30 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
33 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
36 63581804 2018-07-09 stsp const struct got_error *
37 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
38 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
40 63581804 2018-07-09 stsp const struct got_error *err = NULL;
43 63581804 2018-07-09 stsp memset(&zb->z, 0, sizeof(zb->z));
45 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
46 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
47 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
48 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
49 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
50 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
51 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
52 5211b8c8 2019-03-19 stsp errno = ENOMEM;
53 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
55 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
58 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
60 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
61 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
62 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
66 63581804 2018-07-09 stsp zb->flags = 0;
67 63581804 2018-07-09 stsp if (outbuf == NULL) {
68 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
69 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
70 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
73 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
75 63581804 2018-07-09 stsp zb->outbuf = outbuf;
77 6ad68bce 2020-03-24 stsp zb->csum = csum;
80 63581804 2018-07-09 stsp got_inflate_end(zb);
85 dbaa2362 2021-09-28 thomas csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
87 6ad68bce 2020-03-24 stsp if (csum->input_crc)
88 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
90 6ad68bce 2020-03-24 stsp if (csum->input_sha1)
91 6ad68bce 2020-03-24 stsp SHA1Update(csum->input_sha1, buf, len);
95 dbaa2362 2021-09-28 thomas csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
97 d5c81d44 2021-07-08 stsp if (csum->output_crc)
98 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
100 d5c81d44 2021-07-08 stsp if (csum->output_sha1)
101 d5c81d44 2021-07-08 stsp SHA1Update(csum->output_sha1, buf, len);
104 63581804 2018-07-09 stsp const struct got_error *
105 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
106 abc59930 2021-09-05 naddy size_t *consumed)
108 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
109 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
110 63581804 2018-07-09 stsp z_stream *z = &zb->z;
111 63581804 2018-07-09 stsp int ret = Z_ERRNO;
113 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
114 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
116 63581804 2018-07-09 stsp *outlenp = 0;
117 6fb3a497 2020-03-18 stsp if (consumed)
118 6fb3a497 2020-03-18 stsp *consumed = 0;
120 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
121 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
123 63581804 2018-07-09 stsp if (z->avail_in == 0) {
124 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
125 63581804 2018-07-09 stsp if (n == 0) {
126 63581804 2018-07-09 stsp if (ferror(f))
127 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
129 63581804 2018-07-09 stsp ret = Z_STREAM_END;
132 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
133 63581804 2018-07-09 stsp z->avail_in = n;
135 6ad68bce 2020-03-24 stsp if (zb->csum) {
136 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
137 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
138 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
139 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
141 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
142 d5c81d44 2021-07-08 stsp if (zb->csum) {
143 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
144 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
145 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
146 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
148 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
150 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
151 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
153 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
154 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
155 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
158 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
159 6fb3a497 2020-03-18 stsp if (consumed)
160 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
161 63581804 2018-07-09 stsp return NULL;
164 63581804 2018-07-09 stsp const struct got_error *
165 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
166 3ab5e33c 2020-03-18 stsp size_t *consumed)
168 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
169 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
170 63581804 2018-07-09 stsp z_stream *z = &zb->z;
171 63581804 2018-07-09 stsp int ret = Z_ERRNO;
173 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
174 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
176 63581804 2018-07-09 stsp *outlenp = 0;
177 3ab5e33c 2020-03-18 stsp if (consumed)
178 3ab5e33c 2020-03-18 stsp *consumed = 0;
180 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
181 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
183 63581804 2018-07-09 stsp if (z->avail_in == 0) {
184 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
186 638f9024 2019-05-13 stsp return got_error_from_errno("read");
187 63581804 2018-07-09 stsp else if (n == 0) {
189 63581804 2018-07-09 stsp ret = Z_STREAM_END;
192 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
193 63581804 2018-07-09 stsp z->avail_in = n;
195 6ad68bce 2020-03-24 stsp if (zb->csum) {
196 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
197 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
198 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
199 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
201 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
202 d5c81d44 2021-07-08 stsp if (zb->csum) {
203 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
204 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
205 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
206 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
208 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
210 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
211 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
213 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
214 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
215 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
218 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
219 3ab5e33c 2020-03-18 stsp if (consumed)
220 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
221 63581804 2018-07-09 stsp return NULL;
224 63581804 2018-07-09 stsp const struct got_error *
225 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
226 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
228 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
229 63581804 2018-07-09 stsp z_stream *z = &zb->z;
230 63581804 2018-07-09 stsp int ret = Z_ERRNO;
232 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
233 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
235 63581804 2018-07-09 stsp *outlenp = 0;
236 63581804 2018-07-09 stsp *consumed = 0;
239 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
240 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
241 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
243 63581804 2018-07-09 stsp if (z->avail_in == 0) {
244 63581804 2018-07-09 stsp if (len == 0) {
246 63581804 2018-07-09 stsp ret = Z_STREAM_END;
249 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
250 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
252 6ad68bce 2020-03-24 stsp if (zb->csum) {
253 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
254 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
255 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
256 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
258 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
259 d5c81d44 2021-07-08 stsp if (zb->csum) {
260 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
261 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
262 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
263 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
265 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
266 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
268 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
269 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
271 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
272 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
273 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
276 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
277 63581804 2018-07-09 stsp return NULL;
281 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
283 63581804 2018-07-09 stsp free(zb->inbuf);
284 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
285 63581804 2018-07-09 stsp free(zb->outbuf);
286 63581804 2018-07-09 stsp inflateEnd(&zb->z);
289 63581804 2018-07-09 stsp const struct got_error *
290 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
291 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
293 63581804 2018-07-09 stsp const struct got_error *err;
294 6fb3a497 2020-03-18 stsp size_t avail, consumed;
295 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
296 63581804 2018-07-09 stsp void *newbuf;
297 17d745b8 2018-07-23 stsp int nbuf = 1;
299 2decf4c6 2020-03-18 stsp if (outbuf) {
300 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
301 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
302 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
303 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
305 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
307 63581804 2018-07-09 stsp return err;
309 63581804 2018-07-09 stsp *outlen = 0;
310 6fb3a497 2020-03-18 stsp if (consumed_total)
311 6fb3a497 2020-03-18 stsp *consumed_total = 0;
314 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
317 63581804 2018-07-09 stsp *outlen += avail;
318 6fb3a497 2020-03-18 stsp if (consumed_total)
319 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
320 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
321 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
323 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
324 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
325 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
326 63581804 2018-07-09 stsp if (newbuf == NULL) {
327 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
328 63581804 2018-07-09 stsp free(*outbuf);
329 63581804 2018-07-09 stsp *outbuf = NULL;
330 63581804 2018-07-09 stsp *outlen = 0;
333 63581804 2018-07-09 stsp *outbuf = newbuf;
334 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
336 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
339 63581804 2018-07-09 stsp got_inflate_end(&zb);
340 63581804 2018-07-09 stsp return err;
343 63581804 2018-07-09 stsp const struct got_error *
344 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
345 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
346 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
348 63581804 2018-07-09 stsp const struct got_error *err;
349 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
350 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
351 63581804 2018-07-09 stsp void *newbuf;
352 17d745b8 2018-07-23 stsp int nbuf = 1;
353 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
355 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
356 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
357 55fdd257 2020-03-18 stsp bufsize = expected_size;
359 2decf4c6 2020-03-18 stsp if (outbuf) {
360 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
361 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
362 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
363 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
365 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
369 63581804 2018-07-09 stsp *outlen = 0;
370 3ab5e33c 2020-03-18 stsp if (consumed_total)
371 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
374 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
377 63581804 2018-07-09 stsp *outlen += avail;
378 3ab5e33c 2020-03-18 stsp if (consumed_total)
379 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
380 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
381 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
383 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
384 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
385 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
386 63581804 2018-07-09 stsp if (newbuf == NULL) {
387 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
388 63581804 2018-07-09 stsp free(*outbuf);
389 63581804 2018-07-09 stsp *outbuf = NULL;
390 63581804 2018-07-09 stsp *outlen = 0;
393 63581804 2018-07-09 stsp *outbuf = newbuf;
394 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
396 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
399 63581804 2018-07-09 stsp got_inflate_end(&zb);
400 63581804 2018-07-09 stsp return err;
403 63581804 2018-07-09 stsp const struct got_error *
404 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
405 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
406 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
408 63581804 2018-07-09 stsp const struct got_error *err;
409 37bd7602 2018-07-23 stsp size_t avail, consumed;
410 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
411 63581804 2018-07-09 stsp void *newbuf;
412 37bd7602 2018-07-23 stsp int nbuf = 1;
414 2e5a6fad 2020-03-18 stsp if (outbuf) {
415 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
416 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
417 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
418 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
420 2e5a6fad 2020-03-18 stsp free(*outbuf);
421 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
422 2e5a6fad 2020-03-18 stsp return err;
425 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
428 63581804 2018-07-09 stsp *outlen = 0;
429 2e5a6fad 2020-03-18 stsp if (consumed_total)
430 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
432 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
433 63581804 2018-07-09 stsp &consumed);
436 63581804 2018-07-09 stsp offset += consumed;
437 2e5a6fad 2020-03-18 stsp if (consumed_total)
438 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
439 63581804 2018-07-09 stsp len -= consumed;
440 63581804 2018-07-09 stsp *outlen += avail;
441 63581804 2018-07-09 stsp if (len == 0)
443 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
444 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
446 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
447 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
448 63581804 2018-07-09 stsp if (newbuf == NULL) {
449 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
450 63581804 2018-07-09 stsp free(*outbuf);
451 63581804 2018-07-09 stsp *outbuf = NULL;
452 63581804 2018-07-09 stsp *outlen = 0;
455 63581804 2018-07-09 stsp *outbuf = newbuf;
456 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
457 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
459 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
461 63581804 2018-07-09 stsp got_inflate_end(&zb);
462 63581804 2018-07-09 stsp return err;
465 63581804 2018-07-09 stsp const struct got_error *
466 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
467 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
469 63581804 2018-07-09 stsp const struct got_error *err = NULL;
470 63581804 2018-07-09 stsp size_t avail;
471 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
473 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
477 63581804 2018-07-09 stsp *outlen = 0;
480 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
483 63581804 2018-07-09 stsp if (avail > 0) {
485 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
486 63581804 2018-07-09 stsp if (n != avail) {
487 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
490 63581804 2018-07-09 stsp *outlen += avail;
492 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
495 63581804 2018-07-09 stsp if (err == NULL) {
496 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
497 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
499 63581804 2018-07-09 stsp got_inflate_end(&zb);
500 63581804 2018-07-09 stsp return err;
503 63581804 2018-07-09 stsp const struct got_error *
504 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
505 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
507 63581804 2018-07-09 stsp const struct got_error *err;
508 63581804 2018-07-09 stsp size_t avail;
509 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
511 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
515 63581804 2018-07-09 stsp *outlen = 0;
518 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
521 63581804 2018-07-09 stsp if (avail > 0) {
523 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
524 63581804 2018-07-09 stsp if (n != 1) {
525 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
528 63581804 2018-07-09 stsp *outlen += avail;
530 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
533 63581804 2018-07-09 stsp if (err == NULL)
534 63581804 2018-07-09 stsp rewind(outfile);
535 63581804 2018-07-09 stsp got_inflate_end(&zb);
536 63581804 2018-07-09 stsp return err;
539 63581804 2018-07-09 stsp const struct got_error *
540 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
541 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
543 63581804 2018-07-09 stsp const struct got_error *err;
544 4788f1ce 2020-03-18 stsp size_t avail, consumed;
545 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
547 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
551 63581804 2018-07-09 stsp *outlen = 0;
552 4788f1ce 2020-03-18 stsp if (consumed_total)
553 4788f1ce 2020-03-18 stsp *consumed_total = 0;
555 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
558 63581804 2018-07-09 stsp if (avail > 0) {
560 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
561 63581804 2018-07-09 stsp if (n != 1) {
562 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
565 63581804 2018-07-09 stsp *outlen += avail;
566 4788f1ce 2020-03-18 stsp if (consumed_total)
567 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
569 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
572 63581804 2018-07-09 stsp if (err == NULL)
573 63581804 2018-07-09 stsp rewind(outfile);
574 63581804 2018-07-09 stsp got_inflate_end(&zb);
575 63581804 2018-07-09 stsp return err;
578 63581804 2018-07-09 stsp const struct got_error *
579 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
580 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
581 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
583 63581804 2018-07-09 stsp const struct got_error *err;
584 4788f1ce 2020-03-18 stsp size_t avail, consumed;
585 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
587 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
591 63581804 2018-07-09 stsp *outlen = 0;
592 4788f1ce 2020-03-18 stsp if (consumed_total)
593 4788f1ce 2020-03-18 stsp *consumed_total = 0;
595 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
596 63581804 2018-07-09 stsp &consumed);
599 63581804 2018-07-09 stsp offset += consumed;
600 4788f1ce 2020-03-18 stsp if (consumed_total)
601 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
602 63581804 2018-07-09 stsp len -= consumed;
603 63581804 2018-07-09 stsp if (avail > 0) {
605 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
606 63581804 2018-07-09 stsp if (n != 1) {
607 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
610 63581804 2018-07-09 stsp *outlen += avail;
612 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
615 63581804 2018-07-09 stsp if (err == NULL)
616 63581804 2018-07-09 stsp rewind(outfile);
617 63581804 2018-07-09 stsp got_inflate_end(&zb);
618 63581804 2018-07-09 stsp return err;