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 23bc48a9 2019-03-19 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize)
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;
79 63581804 2018-07-09 stsp got_inflate_end(zb);
83 63581804 2018-07-09 stsp const struct got_error *
84 23bc48a9 2019-03-19 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp)
86 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
87 63581804 2018-07-09 stsp z_stream *z = &zb->z;
88 63581804 2018-07-09 stsp int ret = Z_ERRNO;
89 93658fb9 2020-03-18 stsp off_t off, consumed;
91 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
92 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
94 63581804 2018-07-09 stsp *outlenp = 0;
95 93658fb9 2020-03-18 stsp off = ftello(f);
96 93658fb9 2020-03-18 stsp consumed = 0;
98 93658fb9 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
99 63581804 2018-07-09 stsp if (z->avail_in == 0) {
100 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
101 63581804 2018-07-09 stsp if (n == 0) {
102 63581804 2018-07-09 stsp if (ferror(f))
103 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
105 63581804 2018-07-09 stsp ret = Z_STREAM_END;
108 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
109 63581804 2018-07-09 stsp z->avail_in = n;
111 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
112 93658fb9 2020-03-18 stsp consumed += z->total_in - last_total_in;
113 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
115 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
116 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
118 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
119 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
120 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
123 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
124 93658fb9 2020-03-18 stsp fseek(f, off + consumed, SEEK_SET);
125 63581804 2018-07-09 stsp return NULL;
128 63581804 2018-07-09 stsp const struct got_error *
129 23bc48a9 2019-03-19 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp)
131 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
132 63581804 2018-07-09 stsp z_stream *z = &zb->z;
133 63581804 2018-07-09 stsp int ret = Z_ERRNO;
135 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
136 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
138 63581804 2018-07-09 stsp *outlenp = 0;
140 63581804 2018-07-09 stsp if (z->avail_in == 0) {
141 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
143 638f9024 2019-05-13 stsp return got_error_from_errno("read");
144 63581804 2018-07-09 stsp else if (n == 0) {
146 63581804 2018-07-09 stsp ret = Z_STREAM_END;
149 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
150 63581804 2018-07-09 stsp z->avail_in = n;
152 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
153 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
155 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
156 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
158 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
159 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
160 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
163 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
164 63581804 2018-07-09 stsp return NULL;
167 63581804 2018-07-09 stsp const struct got_error *
168 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
169 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
171 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
172 63581804 2018-07-09 stsp z_stream *z = &zb->z;
173 63581804 2018-07-09 stsp int ret = Z_ERRNO;
175 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
176 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
178 63581804 2018-07-09 stsp *outlenp = 0;
179 63581804 2018-07-09 stsp *consumed = 0;
182 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
183 63581804 2018-07-09 stsp if (z->avail_in == 0) {
184 63581804 2018-07-09 stsp if (len == 0) {
186 63581804 2018-07-09 stsp ret = Z_STREAM_END;
189 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
190 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
192 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
193 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
194 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
196 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
197 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
199 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
200 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
201 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
204 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
205 63581804 2018-07-09 stsp return NULL;
209 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
211 63581804 2018-07-09 stsp free(zb->inbuf);
212 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
213 63581804 2018-07-09 stsp free(zb->outbuf);
214 63581804 2018-07-09 stsp inflateEnd(&zb->z);
217 63581804 2018-07-09 stsp const struct got_error *
218 63581804 2018-07-09 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
220 63581804 2018-07-09 stsp const struct got_error *err;
221 63581804 2018-07-09 stsp size_t avail;
222 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
223 63581804 2018-07-09 stsp void *newbuf;
224 17d745b8 2018-07-23 stsp int nbuf = 1;
226 6dc3b75a 2019-05-22 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
227 63581804 2018-07-09 stsp if (*outbuf == NULL)
228 6331840f 2019-05-22 stsp return got_error_from_errno("malloc");
229 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
231 63581804 2018-07-09 stsp return err;
233 63581804 2018-07-09 stsp *outlen = 0;
236 63581804 2018-07-09 stsp err = got_inflate_read(&zb, f, &avail);
239 63581804 2018-07-09 stsp *outlen += avail;
240 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
241 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
242 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
243 63581804 2018-07-09 stsp if (newbuf == NULL) {
244 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
245 63581804 2018-07-09 stsp free(*outbuf);
246 63581804 2018-07-09 stsp *outbuf = NULL;
247 63581804 2018-07-09 stsp *outlen = 0;
250 63581804 2018-07-09 stsp *outbuf = newbuf;
251 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
252 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
254 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
257 63581804 2018-07-09 stsp got_inflate_end(&zb);
258 63581804 2018-07-09 stsp return err;
261 63581804 2018-07-09 stsp const struct got_error *
262 63581804 2018-07-09 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
264 63581804 2018-07-09 stsp const struct got_error *err;
265 63581804 2018-07-09 stsp size_t avail;
266 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
267 63581804 2018-07-09 stsp void *newbuf;
268 17d745b8 2018-07-23 stsp int nbuf = 1;
270 f2c5fe0e 2019-05-22 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
271 63581804 2018-07-09 stsp if (*outbuf == NULL)
272 6331840f 2019-05-22 stsp return got_error_from_errno("malloc");
273 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
277 63581804 2018-07-09 stsp *outlen = 0;
280 63581804 2018-07-09 stsp err = got_inflate_read_fd(&zb, infd, &avail);
283 63581804 2018-07-09 stsp *outlen += avail;
284 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
285 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
286 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
287 63581804 2018-07-09 stsp if (newbuf == NULL) {
288 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
289 63581804 2018-07-09 stsp free(*outbuf);
290 63581804 2018-07-09 stsp *outbuf = NULL;
291 63581804 2018-07-09 stsp *outlen = 0;
294 63581804 2018-07-09 stsp *outbuf = newbuf;
295 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
296 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
298 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
301 63581804 2018-07-09 stsp got_inflate_end(&zb);
302 63581804 2018-07-09 stsp return err;
305 63581804 2018-07-09 stsp const struct got_error *
306 63581804 2018-07-09 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
307 63581804 2018-07-09 stsp size_t offset, size_t len)
309 63581804 2018-07-09 stsp const struct got_error *err;
310 37bd7602 2018-07-23 stsp size_t avail, consumed;
311 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
312 63581804 2018-07-09 stsp void *newbuf;
313 37bd7602 2018-07-23 stsp int nbuf = 1;
315 b3a605ce 2019-05-22 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
316 63581804 2018-07-09 stsp if (*outbuf == NULL)
317 6331840f 2019-05-22 stsp return got_error_from_errno("malloc");
318 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
320 60507209 2018-07-13 stsp free(*outbuf);
321 60507209 2018-07-13 stsp *outbuf = NULL;
322 63581804 2018-07-09 stsp return err;
325 63581804 2018-07-09 stsp *outlen = 0;
328 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
329 63581804 2018-07-09 stsp &consumed);
332 63581804 2018-07-09 stsp offset += consumed;
333 63581804 2018-07-09 stsp len -= consumed;
334 63581804 2018-07-09 stsp *outlen += avail;
335 63581804 2018-07-09 stsp if (len == 0)
337 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
338 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
339 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
340 63581804 2018-07-09 stsp if (newbuf == NULL) {
341 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
342 63581804 2018-07-09 stsp free(*outbuf);
343 63581804 2018-07-09 stsp *outbuf = NULL;
344 63581804 2018-07-09 stsp *outlen = 0;
347 63581804 2018-07-09 stsp *outbuf = newbuf;
348 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
349 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
351 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
353 63581804 2018-07-09 stsp got_inflate_end(&zb);
354 63581804 2018-07-09 stsp return err;
357 63581804 2018-07-09 stsp const struct got_error *
358 63581804 2018-07-09 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
360 63581804 2018-07-09 stsp const struct got_error *err = NULL;
361 63581804 2018-07-09 stsp size_t avail;
362 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
364 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
368 63581804 2018-07-09 stsp *outlen = 0;
371 63581804 2018-07-09 stsp err = got_inflate_read(&zb, infile, &avail);
374 63581804 2018-07-09 stsp if (avail > 0) {
376 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
377 63581804 2018-07-09 stsp if (n != avail) {
378 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
381 63581804 2018-07-09 stsp *outlen += avail;
383 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
386 63581804 2018-07-09 stsp if (err == NULL) {
387 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
388 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
390 63581804 2018-07-09 stsp got_inflate_end(&zb);
391 63581804 2018-07-09 stsp return err;
394 63581804 2018-07-09 stsp const struct got_error *
395 63581804 2018-07-09 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
397 63581804 2018-07-09 stsp const struct got_error *err;
398 63581804 2018-07-09 stsp size_t avail;
399 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
401 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
405 63581804 2018-07-09 stsp *outlen = 0;
408 63581804 2018-07-09 stsp err = got_inflate_read(&zb, infile, &avail);
411 63581804 2018-07-09 stsp if (avail > 0) {
413 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
414 63581804 2018-07-09 stsp if (n != 1) {
415 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
418 63581804 2018-07-09 stsp *outlen += avail;
420 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
423 63581804 2018-07-09 stsp if (err == NULL)
424 63581804 2018-07-09 stsp rewind(outfile);
425 63581804 2018-07-09 stsp got_inflate_end(&zb);
426 63581804 2018-07-09 stsp return err;
429 63581804 2018-07-09 stsp const struct got_error *
430 63581804 2018-07-09 stsp got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
432 63581804 2018-07-09 stsp const struct got_error *err;
433 63581804 2018-07-09 stsp size_t avail;
434 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
436 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
440 63581804 2018-07-09 stsp *outlen = 0;
443 63581804 2018-07-09 stsp err = got_inflate_read_fd(&zb, infd, &avail);
446 63581804 2018-07-09 stsp if (avail > 0) {
448 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
449 63581804 2018-07-09 stsp if (n != 1) {
450 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
453 63581804 2018-07-09 stsp *outlen += avail;
455 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
458 63581804 2018-07-09 stsp if (err == NULL)
459 63581804 2018-07-09 stsp rewind(outfile);
460 63581804 2018-07-09 stsp got_inflate_end(&zb);
461 63581804 2018-07-09 stsp return err;
464 63581804 2018-07-09 stsp const struct got_error *
465 63581804 2018-07-09 stsp got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
466 63581804 2018-07-09 stsp size_t len, FILE *outfile)
468 63581804 2018-07-09 stsp const struct got_error *err;
469 63581804 2018-07-09 stsp size_t avail;
470 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
471 63581804 2018-07-09 stsp size_t consumed;
473 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
477 63581804 2018-07-09 stsp *outlen = 0;
480 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
481 63581804 2018-07-09 stsp &consumed);
484 63581804 2018-07-09 stsp offset += consumed;
485 63581804 2018-07-09 stsp len -= consumed;
486 63581804 2018-07-09 stsp if (avail > 0) {
488 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
489 63581804 2018-07-09 stsp if (n != 1) {
490 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
493 63581804 2018-07-09 stsp *outlen += avail;
495 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
498 63581804 2018-07-09 stsp if (err == NULL)
499 63581804 2018-07-09 stsp rewind(outfile);
500 63581804 2018-07-09 stsp got_inflate_end(&zb);
501 63581804 2018-07-09 stsp return err;