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 63581804 2018-07-09 stsp #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 63581804 2018-07-09 stsp #include <sha1.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 63581804 2018-07-09 stsp #include "got_error.h"
29 63581804 2018-07-09 stsp #include "got_object.h"
30 324d37e7 2019-05-11 stsp #include "got_path.h"
32 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 63581804 2018-07-09 stsp const struct got_error *
39 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
40 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
42 63581804 2018-07-09 stsp const struct got_error *err = NULL;
45 63581804 2018-07-09 stsp memset(&zb->z, 0, sizeof(zb->z));
47 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
48 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
49 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
50 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
51 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
52 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
53 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
54 5211b8c8 2019-03-19 stsp errno = ENOMEM;
55 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
57 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
60 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
62 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
63 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
64 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
68 63581804 2018-07-09 stsp zb->flags = 0;
69 63581804 2018-07-09 stsp if (outbuf == NULL) {
70 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
71 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
72 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
75 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
77 63581804 2018-07-09 stsp zb->outbuf = outbuf;
79 6ad68bce 2020-03-24 stsp zb->csum = csum;
82 63581804 2018-07-09 stsp got_inflate_end(zb);
87 6ad68bce 2020-03-24 stsp csum_input(struct got_inflate_checksum *csum, const char *buf, size_t len)
89 6ad68bce 2020-03-24 stsp if (csum->input_crc)
90 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
92 6ad68bce 2020-03-24 stsp if (csum->input_sha1)
93 6ad68bce 2020-03-24 stsp SHA1Update(csum->input_sha1, buf, len);
96 63581804 2018-07-09 stsp const struct got_error *
97 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
98 6fb3a497 2020-03-18 stsp size_t *consumed)
100 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
101 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
102 63581804 2018-07-09 stsp z_stream *z = &zb->z;
103 63581804 2018-07-09 stsp int ret = Z_ERRNO;
105 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
106 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
108 63581804 2018-07-09 stsp *outlenp = 0;
109 6fb3a497 2020-03-18 stsp if (consumed)
110 6fb3a497 2020-03-18 stsp *consumed = 0;
112 6ad68bce 2020-03-24 stsp char *csum_in = NULL;
113 6ad68bce 2020-03-24 stsp size_t csum_avail = 0;
115 63581804 2018-07-09 stsp if (z->avail_in == 0) {
116 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
117 63581804 2018-07-09 stsp if (n == 0) {
118 63581804 2018-07-09 stsp if (ferror(f))
119 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
121 63581804 2018-07-09 stsp ret = Z_STREAM_END;
124 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
125 63581804 2018-07-09 stsp z->avail_in = n;
127 6ad68bce 2020-03-24 stsp if (zb->csum) {
128 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
129 6ad68bce 2020-03-24 stsp csum_avail = z->avail_in;
131 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
132 6ad68bce 2020-03-24 stsp if (zb->csum)
133 6ad68bce 2020-03-24 stsp csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
134 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
136 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
137 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
139 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
140 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
141 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
144 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
145 6fb3a497 2020-03-18 stsp if (consumed)
146 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
147 63581804 2018-07-09 stsp return NULL;
150 63581804 2018-07-09 stsp const struct got_error *
151 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
152 3ab5e33c 2020-03-18 stsp size_t *consumed)
154 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
155 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
156 63581804 2018-07-09 stsp z_stream *z = &zb->z;
157 63581804 2018-07-09 stsp int ret = Z_ERRNO;
159 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
160 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
162 63581804 2018-07-09 stsp *outlenp = 0;
163 3ab5e33c 2020-03-18 stsp if (consumed)
164 3ab5e33c 2020-03-18 stsp *consumed = 0;
166 6ad68bce 2020-03-24 stsp char *csum_in = NULL;
167 6ad68bce 2020-03-24 stsp size_t csum_avail = 0;
169 63581804 2018-07-09 stsp if (z->avail_in == 0) {
170 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
172 638f9024 2019-05-13 stsp return got_error_from_errno("read");
173 63581804 2018-07-09 stsp else if (n == 0) {
175 63581804 2018-07-09 stsp ret = Z_STREAM_END;
178 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
179 63581804 2018-07-09 stsp z->avail_in = n;
181 6ad68bce 2020-03-24 stsp if (zb->csum) {
182 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
183 6ad68bce 2020-03-24 stsp csum_avail = z->avail_in;
185 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
186 6ad68bce 2020-03-24 stsp if (zb->csum)
187 6ad68bce 2020-03-24 stsp csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
188 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
190 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
191 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
193 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
194 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
195 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
198 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
199 3ab5e33c 2020-03-18 stsp if (consumed)
200 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
201 63581804 2018-07-09 stsp return NULL;
204 63581804 2018-07-09 stsp const struct got_error *
205 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
206 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
208 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
209 63581804 2018-07-09 stsp z_stream *z = &zb->z;
210 63581804 2018-07-09 stsp int ret = Z_ERRNO;
212 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
213 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
215 63581804 2018-07-09 stsp *outlenp = 0;
216 63581804 2018-07-09 stsp *consumed = 0;
219 6ad68bce 2020-03-24 stsp char *csum_in = NULL;
220 6ad68bce 2020-03-24 stsp size_t csum_avail = 0;
221 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
223 63581804 2018-07-09 stsp if (z->avail_in == 0) {
224 63581804 2018-07-09 stsp if (len == 0) {
226 63581804 2018-07-09 stsp ret = Z_STREAM_END;
229 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
230 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
232 6ad68bce 2020-03-24 stsp if (zb->csum) {
233 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
234 6ad68bce 2020-03-24 stsp csum_avail = z->avail_in;
236 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
237 6ad68bce 2020-03-24 stsp if (zb->csum)
238 6ad68bce 2020-03-24 stsp csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
239 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
240 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
242 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
243 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
245 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
246 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
247 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
250 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
251 63581804 2018-07-09 stsp return NULL;
255 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
257 63581804 2018-07-09 stsp free(zb->inbuf);
258 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
259 63581804 2018-07-09 stsp free(zb->outbuf);
260 63581804 2018-07-09 stsp inflateEnd(&zb->z);
263 63581804 2018-07-09 stsp const struct got_error *
264 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
265 6fb3a497 2020-03-18 stsp size_t *consumed_total, FILE *f)
267 63581804 2018-07-09 stsp const struct got_error *err;
268 6fb3a497 2020-03-18 stsp size_t avail, consumed;
269 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
270 63581804 2018-07-09 stsp void *newbuf;
271 17d745b8 2018-07-23 stsp int nbuf = 1;
273 2decf4c6 2020-03-18 stsp if (outbuf) {
274 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
275 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
276 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
277 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
279 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
281 63581804 2018-07-09 stsp return err;
283 63581804 2018-07-09 stsp *outlen = 0;
284 6fb3a497 2020-03-18 stsp if (consumed_total)
285 6fb3a497 2020-03-18 stsp *consumed_total = 0;
288 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
291 63581804 2018-07-09 stsp *outlen += avail;
292 6fb3a497 2020-03-18 stsp if (consumed_total)
293 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
294 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
295 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
297 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
298 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
299 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
300 63581804 2018-07-09 stsp if (newbuf == NULL) {
301 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
302 63581804 2018-07-09 stsp free(*outbuf);
303 63581804 2018-07-09 stsp *outbuf = NULL;
304 63581804 2018-07-09 stsp *outlen = 0;
307 63581804 2018-07-09 stsp *outbuf = newbuf;
308 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
310 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
313 63581804 2018-07-09 stsp got_inflate_end(&zb);
314 63581804 2018-07-09 stsp return err;
317 63581804 2018-07-09 stsp const struct got_error *
318 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
319 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
320 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
322 63581804 2018-07-09 stsp const struct got_error *err;
323 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
324 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
325 63581804 2018-07-09 stsp void *newbuf;
326 17d745b8 2018-07-23 stsp int nbuf = 1;
327 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
329 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
330 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
331 55fdd257 2020-03-18 stsp bufsize = expected_size;
333 2decf4c6 2020-03-18 stsp if (outbuf) {
334 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
335 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
336 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
337 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
339 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
343 63581804 2018-07-09 stsp *outlen = 0;
344 3ab5e33c 2020-03-18 stsp if (consumed_total)
345 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
348 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
351 63581804 2018-07-09 stsp *outlen += avail;
352 3ab5e33c 2020-03-18 stsp if (consumed_total)
353 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
354 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
355 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
357 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
358 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
359 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
360 63581804 2018-07-09 stsp if (newbuf == NULL) {
361 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
362 63581804 2018-07-09 stsp free(*outbuf);
363 63581804 2018-07-09 stsp *outbuf = NULL;
364 63581804 2018-07-09 stsp *outlen = 0;
367 63581804 2018-07-09 stsp *outbuf = newbuf;
368 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
370 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
373 63581804 2018-07-09 stsp got_inflate_end(&zb);
374 63581804 2018-07-09 stsp return err;
377 63581804 2018-07-09 stsp const struct got_error *
378 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
379 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
380 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
382 63581804 2018-07-09 stsp const struct got_error *err;
383 37bd7602 2018-07-23 stsp size_t avail, consumed;
384 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
385 63581804 2018-07-09 stsp void *newbuf;
386 37bd7602 2018-07-23 stsp int nbuf = 1;
388 2e5a6fad 2020-03-18 stsp if (outbuf) {
389 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
390 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
391 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
392 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
394 2e5a6fad 2020-03-18 stsp free(*outbuf);
395 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
396 2e5a6fad 2020-03-18 stsp return err;
399 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
402 63581804 2018-07-09 stsp *outlen = 0;
403 2e5a6fad 2020-03-18 stsp if (consumed_total)
404 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
406 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
407 63581804 2018-07-09 stsp &consumed);
410 63581804 2018-07-09 stsp offset += consumed;
411 2e5a6fad 2020-03-18 stsp if (consumed_total)
412 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
413 63581804 2018-07-09 stsp len -= consumed;
414 63581804 2018-07-09 stsp *outlen += avail;
415 63581804 2018-07-09 stsp if (len == 0)
417 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
418 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
420 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
421 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
422 63581804 2018-07-09 stsp if (newbuf == NULL) {
423 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
424 63581804 2018-07-09 stsp free(*outbuf);
425 63581804 2018-07-09 stsp *outbuf = NULL;
426 63581804 2018-07-09 stsp *outlen = 0;
429 63581804 2018-07-09 stsp *outbuf = newbuf;
430 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
431 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
433 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
435 63581804 2018-07-09 stsp got_inflate_end(&zb);
436 63581804 2018-07-09 stsp return err;
439 63581804 2018-07-09 stsp const struct got_error *
440 63581804 2018-07-09 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
442 63581804 2018-07-09 stsp const struct got_error *err = NULL;
443 63581804 2018-07-09 stsp size_t avail;
444 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
446 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
450 63581804 2018-07-09 stsp *outlen = 0;
453 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
456 63581804 2018-07-09 stsp if (avail > 0) {
458 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
459 63581804 2018-07-09 stsp if (n != avail) {
460 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
463 63581804 2018-07-09 stsp *outlen += avail;
465 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
468 63581804 2018-07-09 stsp if (err == NULL) {
469 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
470 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
472 63581804 2018-07-09 stsp got_inflate_end(&zb);
473 63581804 2018-07-09 stsp return err;
476 63581804 2018-07-09 stsp const struct got_error *
477 63581804 2018-07-09 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
479 63581804 2018-07-09 stsp const struct got_error *err;
480 63581804 2018-07-09 stsp size_t avail;
481 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
483 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
487 63581804 2018-07-09 stsp *outlen = 0;
490 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
493 63581804 2018-07-09 stsp if (avail > 0) {
495 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
496 63581804 2018-07-09 stsp if (n != 1) {
497 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
500 63581804 2018-07-09 stsp *outlen += avail;
502 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
505 63581804 2018-07-09 stsp if (err == NULL)
506 63581804 2018-07-09 stsp rewind(outfile);
507 63581804 2018-07-09 stsp got_inflate_end(&zb);
508 63581804 2018-07-09 stsp return err;
511 63581804 2018-07-09 stsp const struct got_error *
512 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
513 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
515 63581804 2018-07-09 stsp const struct got_error *err;
516 4788f1ce 2020-03-18 stsp size_t avail, consumed;
517 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
519 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
523 63581804 2018-07-09 stsp *outlen = 0;
524 4788f1ce 2020-03-18 stsp if (consumed_total)
525 4788f1ce 2020-03-18 stsp *consumed_total = 0;
527 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
530 63581804 2018-07-09 stsp if (avail > 0) {
532 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
533 63581804 2018-07-09 stsp if (n != 1) {
534 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
537 63581804 2018-07-09 stsp *outlen += avail;
538 4788f1ce 2020-03-18 stsp if (consumed_total)
539 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
541 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
544 63581804 2018-07-09 stsp if (err == NULL)
545 63581804 2018-07-09 stsp rewind(outfile);
546 63581804 2018-07-09 stsp got_inflate_end(&zb);
547 63581804 2018-07-09 stsp return err;
550 63581804 2018-07-09 stsp const struct got_error *
551 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
552 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
553 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
555 63581804 2018-07-09 stsp const struct got_error *err;
556 4788f1ce 2020-03-18 stsp size_t avail, consumed;
557 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
559 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
563 63581804 2018-07-09 stsp *outlen = 0;
564 4788f1ce 2020-03-18 stsp if (consumed_total)
565 4788f1ce 2020-03-18 stsp *consumed_total = 0;
567 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
568 63581804 2018-07-09 stsp &consumed);
571 63581804 2018-07-09 stsp offset += consumed;
572 4788f1ce 2020-03-18 stsp if (consumed_total)
573 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
574 63581804 2018-07-09 stsp len -= consumed;
575 63581804 2018-07-09 stsp if (avail > 0) {
577 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
578 63581804 2018-07-09 stsp if (n != 1) {
579 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
582 63581804 2018-07-09 stsp *outlen += avail;
584 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
587 63581804 2018-07-09 stsp if (err == NULL)
588 63581804 2018-07-09 stsp rewind(outfile);
589 63581804 2018-07-09 stsp got_inflate_end(&zb);
590 63581804 2018-07-09 stsp return err;