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 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 1e87a3c3 2020-03-18 stsp uint32_t *input_crc)
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 1e87a3c3 2020-03-18 stsp zb->input_crc = input_crc;
81 63581804 2018-07-09 stsp got_inflate_end(zb);
85 63581804 2018-07-09 stsp const struct got_error *
86 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
87 6fb3a497 2020-03-18 stsp size_t *consumed)
89 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
90 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
91 63581804 2018-07-09 stsp z_stream *z = &zb->z;
92 63581804 2018-07-09 stsp int ret = Z_ERRNO;
94 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
95 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
97 63581804 2018-07-09 stsp *outlenp = 0;
98 6fb3a497 2020-03-18 stsp if (consumed)
99 6fb3a497 2020-03-18 stsp *consumed = 0;
101 1e87a3c3 2020-03-18 stsp char *crc_in = NULL;
102 1e87a3c3 2020-03-18 stsp size_t crc_avail = 0;
104 63581804 2018-07-09 stsp if (z->avail_in == 0) {
105 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
106 63581804 2018-07-09 stsp if (n == 0) {
107 63581804 2018-07-09 stsp if (ferror(f))
108 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
110 63581804 2018-07-09 stsp ret = Z_STREAM_END;
113 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
114 63581804 2018-07-09 stsp z->avail_in = n;
116 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
117 1e87a3c3 2020-03-18 stsp crc_in = z->next_in;
118 1e87a3c3 2020-03-18 stsp crc_avail = z->avail_in;
120 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
121 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
122 1e87a3c3 2020-03-18 stsp *zb->input_crc = crc32(*zb->input_crc,
123 1e87a3c3 2020-03-18 stsp crc_in, crc_avail - z->avail_in);
125 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
127 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
128 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
130 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
131 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
132 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
135 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
136 6fb3a497 2020-03-18 stsp if (consumed)
137 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
138 63581804 2018-07-09 stsp return NULL;
141 63581804 2018-07-09 stsp const struct got_error *
142 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
143 3ab5e33c 2020-03-18 stsp size_t *consumed)
145 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
146 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
147 63581804 2018-07-09 stsp z_stream *z = &zb->z;
148 63581804 2018-07-09 stsp int ret = Z_ERRNO;
150 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
151 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
153 63581804 2018-07-09 stsp *outlenp = 0;
154 3ab5e33c 2020-03-18 stsp if (consumed)
155 3ab5e33c 2020-03-18 stsp *consumed = 0;
157 1e87a3c3 2020-03-18 stsp char *crc_in = NULL;
158 1e87a3c3 2020-03-18 stsp size_t crc_avail = 0;
160 63581804 2018-07-09 stsp if (z->avail_in == 0) {
161 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
163 638f9024 2019-05-13 stsp return got_error_from_errno("read");
164 63581804 2018-07-09 stsp else if (n == 0) {
166 63581804 2018-07-09 stsp ret = Z_STREAM_END;
169 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
170 63581804 2018-07-09 stsp z->avail_in = n;
172 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
173 1e87a3c3 2020-03-18 stsp crc_in = z->next_in;
174 1e87a3c3 2020-03-18 stsp crc_avail = z->avail_in;
176 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
177 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
178 1e87a3c3 2020-03-18 stsp *zb->input_crc = crc32(*zb->input_crc,
179 1e87a3c3 2020-03-18 stsp crc_in, crc_avail - z->avail_in);
181 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
183 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
184 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
186 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
187 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
188 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
191 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
192 3ab5e33c 2020-03-18 stsp if (consumed)
193 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
194 63581804 2018-07-09 stsp return NULL;
197 63581804 2018-07-09 stsp const struct got_error *
198 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
199 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
201 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
202 63581804 2018-07-09 stsp z_stream *z = &zb->z;
203 63581804 2018-07-09 stsp int ret = Z_ERRNO;
205 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
206 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
208 63581804 2018-07-09 stsp *outlenp = 0;
209 63581804 2018-07-09 stsp *consumed = 0;
212 1e87a3c3 2020-03-18 stsp char *crc_in = NULL;
213 1e87a3c3 2020-03-18 stsp size_t crc_avail = 0;
214 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
216 63581804 2018-07-09 stsp if (z->avail_in == 0) {
217 63581804 2018-07-09 stsp if (len == 0) {
219 63581804 2018-07-09 stsp ret = Z_STREAM_END;
222 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
223 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
225 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
226 1e87a3c3 2020-03-18 stsp crc_in = z->next_in;
227 1e87a3c3 2020-03-18 stsp crc_avail = z->avail_in;
229 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
230 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
231 1e87a3c3 2020-03-18 stsp *zb->input_crc = crc32(*zb->input_crc,
232 1e87a3c3 2020-03-18 stsp crc_in, crc_avail - z->avail_in);
234 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
235 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
237 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
238 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
240 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
241 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
242 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
245 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
246 63581804 2018-07-09 stsp return NULL;
250 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
252 63581804 2018-07-09 stsp free(zb->inbuf);
253 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
254 63581804 2018-07-09 stsp free(zb->outbuf);
255 63581804 2018-07-09 stsp inflateEnd(&zb->z);
258 63581804 2018-07-09 stsp const struct got_error *
259 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
260 6fb3a497 2020-03-18 stsp size_t *consumed_total, FILE *f)
262 63581804 2018-07-09 stsp const struct got_error *err;
263 6fb3a497 2020-03-18 stsp size_t avail, consumed;
264 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
265 63581804 2018-07-09 stsp void *newbuf;
266 17d745b8 2018-07-23 stsp int nbuf = 1;
268 2decf4c6 2020-03-18 stsp if (outbuf) {
269 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
270 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
271 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
272 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
274 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
276 63581804 2018-07-09 stsp return err;
278 63581804 2018-07-09 stsp *outlen = 0;
279 6fb3a497 2020-03-18 stsp if (consumed_total)
280 6fb3a497 2020-03-18 stsp *consumed_total = 0;
283 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
286 63581804 2018-07-09 stsp *outlen += avail;
287 6fb3a497 2020-03-18 stsp if (consumed_total)
288 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
289 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
290 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
292 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
293 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
294 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
295 63581804 2018-07-09 stsp if (newbuf == NULL) {
296 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
297 63581804 2018-07-09 stsp free(*outbuf);
298 63581804 2018-07-09 stsp *outbuf = NULL;
299 63581804 2018-07-09 stsp *outlen = 0;
302 63581804 2018-07-09 stsp *outbuf = newbuf;
303 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
305 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
308 63581804 2018-07-09 stsp got_inflate_end(&zb);
309 63581804 2018-07-09 stsp return err;
312 63581804 2018-07-09 stsp const struct got_error *
313 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
314 1e87a3c3 2020-03-18 stsp size_t *consumed_total, uint32_t *input_crc, int infd)
316 63581804 2018-07-09 stsp const struct got_error *err;
317 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
318 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
319 63581804 2018-07-09 stsp void *newbuf;
320 17d745b8 2018-07-23 stsp int nbuf = 1;
322 2decf4c6 2020-03-18 stsp if (outbuf) {
323 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
324 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
325 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
326 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
327 1e87a3c3 2020-03-18 stsp input_crc);
329 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE,
330 1e87a3c3 2020-03-18 stsp input_crc);
334 63581804 2018-07-09 stsp *outlen = 0;
335 3ab5e33c 2020-03-18 stsp if (consumed_total)
336 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
339 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
342 63581804 2018-07-09 stsp *outlen += avail;
343 3ab5e33c 2020-03-18 stsp if (consumed_total)
344 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
345 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
346 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
348 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
349 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
350 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
351 63581804 2018-07-09 stsp if (newbuf == NULL) {
352 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
353 63581804 2018-07-09 stsp free(*outbuf);
354 63581804 2018-07-09 stsp *outbuf = NULL;
355 63581804 2018-07-09 stsp *outlen = 0;
358 63581804 2018-07-09 stsp *outbuf = newbuf;
359 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
361 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
364 63581804 2018-07-09 stsp got_inflate_end(&zb);
365 63581804 2018-07-09 stsp return err;
368 63581804 2018-07-09 stsp const struct got_error *
369 63581804 2018-07-09 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
370 63581804 2018-07-09 stsp size_t offset, size_t len)
372 63581804 2018-07-09 stsp const struct got_error *err;
373 37bd7602 2018-07-23 stsp size_t avail, consumed;
374 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
375 63581804 2018-07-09 stsp void *newbuf;
376 37bd7602 2018-07-23 stsp int nbuf = 1;
378 b3a605ce 2019-05-22 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
379 63581804 2018-07-09 stsp if (*outbuf == NULL)
380 6331840f 2019-05-22 stsp return got_error_from_errno("malloc");
381 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
383 60507209 2018-07-13 stsp free(*outbuf);
384 60507209 2018-07-13 stsp *outbuf = NULL;
385 63581804 2018-07-09 stsp return err;
388 63581804 2018-07-09 stsp *outlen = 0;
391 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
392 63581804 2018-07-09 stsp &consumed);
395 63581804 2018-07-09 stsp offset += consumed;
396 63581804 2018-07-09 stsp len -= consumed;
397 63581804 2018-07-09 stsp *outlen += avail;
398 63581804 2018-07-09 stsp if (len == 0)
400 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
401 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
402 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
403 63581804 2018-07-09 stsp if (newbuf == NULL) {
404 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
405 63581804 2018-07-09 stsp free(*outbuf);
406 63581804 2018-07-09 stsp *outbuf = NULL;
407 63581804 2018-07-09 stsp *outlen = 0;
410 63581804 2018-07-09 stsp *outbuf = newbuf;
411 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
412 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
414 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
416 63581804 2018-07-09 stsp got_inflate_end(&zb);
417 63581804 2018-07-09 stsp return err;
420 63581804 2018-07-09 stsp const struct got_error *
421 63581804 2018-07-09 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
423 63581804 2018-07-09 stsp const struct got_error *err = NULL;
424 63581804 2018-07-09 stsp size_t avail;
425 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
427 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
431 63581804 2018-07-09 stsp *outlen = 0;
434 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
437 63581804 2018-07-09 stsp if (avail > 0) {
439 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
440 63581804 2018-07-09 stsp if (n != avail) {
441 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
444 63581804 2018-07-09 stsp *outlen += avail;
446 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
449 63581804 2018-07-09 stsp if (err == NULL) {
450 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
451 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
453 63581804 2018-07-09 stsp got_inflate_end(&zb);
454 63581804 2018-07-09 stsp return err;
457 63581804 2018-07-09 stsp const struct got_error *
458 63581804 2018-07-09 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
460 63581804 2018-07-09 stsp const struct got_error *err;
461 63581804 2018-07-09 stsp size_t avail;
462 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
464 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
468 63581804 2018-07-09 stsp *outlen = 0;
471 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
474 63581804 2018-07-09 stsp if (avail > 0) {
476 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
477 63581804 2018-07-09 stsp if (n != 1) {
478 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
481 63581804 2018-07-09 stsp *outlen += avail;
483 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
486 63581804 2018-07-09 stsp if (err == NULL)
487 63581804 2018-07-09 stsp rewind(outfile);
488 63581804 2018-07-09 stsp got_inflate_end(&zb);
489 63581804 2018-07-09 stsp return err;
492 63581804 2018-07-09 stsp const struct got_error *
493 63581804 2018-07-09 stsp got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
495 63581804 2018-07-09 stsp const struct got_error *err;
496 63581804 2018-07-09 stsp size_t avail;
497 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
499 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
503 63581804 2018-07-09 stsp *outlen = 0;
506 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, NULL);
509 63581804 2018-07-09 stsp if (avail > 0) {
511 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
512 63581804 2018-07-09 stsp if (n != 1) {
513 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
516 63581804 2018-07-09 stsp *outlen += avail;
518 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
521 63581804 2018-07-09 stsp if (err == NULL)
522 63581804 2018-07-09 stsp rewind(outfile);
523 63581804 2018-07-09 stsp got_inflate_end(&zb);
524 63581804 2018-07-09 stsp return err;
527 63581804 2018-07-09 stsp const struct got_error *
528 63581804 2018-07-09 stsp got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
529 63581804 2018-07-09 stsp size_t len, FILE *outfile)
531 63581804 2018-07-09 stsp const struct got_error *err;
532 63581804 2018-07-09 stsp size_t avail;
533 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
534 63581804 2018-07-09 stsp size_t consumed;
536 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
540 63581804 2018-07-09 stsp *outlen = 0;
543 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
544 63581804 2018-07-09 stsp &consumed);
547 63581804 2018-07-09 stsp offset += consumed;
548 63581804 2018-07-09 stsp len -= consumed;
549 63581804 2018-07-09 stsp if (avail > 0) {
551 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
552 63581804 2018-07-09 stsp if (n != 1) {
553 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
556 63581804 2018-07-09 stsp *outlen += avail;
558 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
561 63581804 2018-07-09 stsp if (err == NULL)
562 63581804 2018-07-09 stsp rewind(outfile);
563 63581804 2018-07-09 stsp got_inflate_end(&zb);
564 63581804 2018-07-09 stsp return err;