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 63581804 2018-07-09 stsp #include <sha1.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 6ad68bce 2020-03-24 stsp csum_input(struct got_inflate_checksum *csum, const char *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 d5c81d44 2021-07-08 stsp csum_output(struct got_inflate_checksum *csum, const char *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 d5c81d44 2021-07-08 stsp char *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 d5c81d44 2021-07-08 stsp char *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 d5c81d44 2021-07-08 stsp char *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 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
253 6ad68bce 2020-03-24 stsp if (zb->csum) {
254 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
255 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
256 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
257 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
259 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
260 d5c81d44 2021-07-08 stsp if (zb->csum) {
261 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
262 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
263 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
264 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
266 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
267 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
269 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
270 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
272 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
273 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
274 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
277 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
278 63581804 2018-07-09 stsp return NULL;
282 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
284 63581804 2018-07-09 stsp free(zb->inbuf);
285 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
286 63581804 2018-07-09 stsp free(zb->outbuf);
287 63581804 2018-07-09 stsp inflateEnd(&zb->z);
290 63581804 2018-07-09 stsp const struct got_error *
291 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
292 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
294 63581804 2018-07-09 stsp const struct got_error *err;
295 6fb3a497 2020-03-18 stsp size_t avail, consumed;
296 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
297 63581804 2018-07-09 stsp void *newbuf;
298 17d745b8 2018-07-23 stsp int nbuf = 1;
300 2decf4c6 2020-03-18 stsp if (outbuf) {
301 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
302 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
303 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
304 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
306 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
308 63581804 2018-07-09 stsp return err;
310 63581804 2018-07-09 stsp *outlen = 0;
311 6fb3a497 2020-03-18 stsp if (consumed_total)
312 6fb3a497 2020-03-18 stsp *consumed_total = 0;
315 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
318 63581804 2018-07-09 stsp *outlen += avail;
319 6fb3a497 2020-03-18 stsp if (consumed_total)
320 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
321 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
322 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
324 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
325 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
326 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
327 63581804 2018-07-09 stsp if (newbuf == NULL) {
328 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
329 63581804 2018-07-09 stsp free(*outbuf);
330 63581804 2018-07-09 stsp *outbuf = NULL;
331 63581804 2018-07-09 stsp *outlen = 0;
334 63581804 2018-07-09 stsp *outbuf = newbuf;
335 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
337 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
340 63581804 2018-07-09 stsp got_inflate_end(&zb);
341 63581804 2018-07-09 stsp return err;
344 63581804 2018-07-09 stsp const struct got_error *
345 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
346 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
347 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
349 63581804 2018-07-09 stsp const struct got_error *err;
350 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
351 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
352 63581804 2018-07-09 stsp void *newbuf;
353 17d745b8 2018-07-23 stsp int nbuf = 1;
354 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
356 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
357 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
358 55fdd257 2020-03-18 stsp bufsize = expected_size;
360 2decf4c6 2020-03-18 stsp if (outbuf) {
361 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
362 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
363 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
364 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
366 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
370 63581804 2018-07-09 stsp *outlen = 0;
371 3ab5e33c 2020-03-18 stsp if (consumed_total)
372 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
375 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
378 63581804 2018-07-09 stsp *outlen += avail;
379 3ab5e33c 2020-03-18 stsp if (consumed_total)
380 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
381 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
382 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
384 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
385 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
386 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
387 63581804 2018-07-09 stsp if (newbuf == NULL) {
388 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
389 63581804 2018-07-09 stsp free(*outbuf);
390 63581804 2018-07-09 stsp *outbuf = NULL;
391 63581804 2018-07-09 stsp *outlen = 0;
394 63581804 2018-07-09 stsp *outbuf = newbuf;
395 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
397 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
400 63581804 2018-07-09 stsp got_inflate_end(&zb);
401 63581804 2018-07-09 stsp return err;
404 63581804 2018-07-09 stsp const struct got_error *
405 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
406 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
407 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
409 63581804 2018-07-09 stsp const struct got_error *err;
410 37bd7602 2018-07-23 stsp size_t avail, consumed;
411 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
412 63581804 2018-07-09 stsp void *newbuf;
413 37bd7602 2018-07-23 stsp int nbuf = 1;
415 2e5a6fad 2020-03-18 stsp if (outbuf) {
416 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
417 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
418 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
419 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
421 2e5a6fad 2020-03-18 stsp free(*outbuf);
422 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
423 2e5a6fad 2020-03-18 stsp return err;
426 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
429 63581804 2018-07-09 stsp *outlen = 0;
430 2e5a6fad 2020-03-18 stsp if (consumed_total)
431 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
433 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
434 63581804 2018-07-09 stsp &consumed);
437 63581804 2018-07-09 stsp offset += consumed;
438 2e5a6fad 2020-03-18 stsp if (consumed_total)
439 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
440 63581804 2018-07-09 stsp len -= consumed;
441 63581804 2018-07-09 stsp *outlen += avail;
442 63581804 2018-07-09 stsp if (len == 0)
444 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
445 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
447 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
448 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
449 63581804 2018-07-09 stsp if (newbuf == NULL) {
450 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
451 63581804 2018-07-09 stsp free(*outbuf);
452 63581804 2018-07-09 stsp *outbuf = NULL;
453 63581804 2018-07-09 stsp *outlen = 0;
456 63581804 2018-07-09 stsp *outbuf = newbuf;
457 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
458 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
460 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
462 63581804 2018-07-09 stsp got_inflate_end(&zb);
463 63581804 2018-07-09 stsp return err;
466 63581804 2018-07-09 stsp const struct got_error *
467 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
468 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
470 63581804 2018-07-09 stsp const struct got_error *err = NULL;
471 63581804 2018-07-09 stsp size_t avail;
472 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
474 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
478 63581804 2018-07-09 stsp *outlen = 0;
481 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
484 63581804 2018-07-09 stsp if (avail > 0) {
486 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
487 63581804 2018-07-09 stsp if (n != avail) {
488 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
491 63581804 2018-07-09 stsp *outlen += avail;
493 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
496 63581804 2018-07-09 stsp if (err == NULL) {
497 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
498 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
500 63581804 2018-07-09 stsp got_inflate_end(&zb);
501 63581804 2018-07-09 stsp return err;
504 63581804 2018-07-09 stsp const struct got_error *
505 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
506 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
508 63581804 2018-07-09 stsp const struct got_error *err;
509 63581804 2018-07-09 stsp size_t avail;
510 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
512 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
516 63581804 2018-07-09 stsp *outlen = 0;
519 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
522 63581804 2018-07-09 stsp if (avail > 0) {
524 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
525 63581804 2018-07-09 stsp if (n != 1) {
526 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
529 63581804 2018-07-09 stsp *outlen += avail;
531 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
534 63581804 2018-07-09 stsp if (err == NULL)
535 63581804 2018-07-09 stsp rewind(outfile);
536 63581804 2018-07-09 stsp got_inflate_end(&zb);
537 63581804 2018-07-09 stsp return err;
540 63581804 2018-07-09 stsp const struct got_error *
541 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
542 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
544 63581804 2018-07-09 stsp const struct got_error *err;
545 4788f1ce 2020-03-18 stsp size_t avail, consumed;
546 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
548 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
552 63581804 2018-07-09 stsp *outlen = 0;
553 4788f1ce 2020-03-18 stsp if (consumed_total)
554 4788f1ce 2020-03-18 stsp *consumed_total = 0;
556 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
559 63581804 2018-07-09 stsp if (avail > 0) {
561 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
562 63581804 2018-07-09 stsp if (n != 1) {
563 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
566 63581804 2018-07-09 stsp *outlen += avail;
567 4788f1ce 2020-03-18 stsp if (consumed_total)
568 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
570 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
573 63581804 2018-07-09 stsp if (err == NULL)
574 63581804 2018-07-09 stsp rewind(outfile);
575 63581804 2018-07-09 stsp got_inflate_end(&zb);
576 63581804 2018-07-09 stsp return err;
579 63581804 2018-07-09 stsp const struct got_error *
580 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
581 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
582 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
584 63581804 2018-07-09 stsp const struct got_error *err;
585 4788f1ce 2020-03-18 stsp size_t avail, consumed;
586 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
588 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
592 63581804 2018-07-09 stsp *outlen = 0;
593 4788f1ce 2020-03-18 stsp if (consumed_total)
594 4788f1ce 2020-03-18 stsp *consumed_total = 0;
596 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
597 63581804 2018-07-09 stsp &consumed);
600 63581804 2018-07-09 stsp offset += consumed;
601 4788f1ce 2020-03-18 stsp if (consumed_total)
602 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
603 63581804 2018-07-09 stsp len -= consumed;
604 63581804 2018-07-09 stsp if (avail > 0) {
606 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
607 63581804 2018-07-09 stsp if (n != 1) {
608 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
611 63581804 2018-07-09 stsp *outlen += avail;
613 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
616 63581804 2018-07-09 stsp if (err == NULL)
617 63581804 2018-07-09 stsp rewind(outfile);
618 63581804 2018-07-09 stsp got_inflate_end(&zb);
619 63581804 2018-07-09 stsp return err;