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 55fdd257 2020-03-18 stsp size_t *consumed_total, uint32_t *input_crc, size_t expected_size, 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;
321 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
323 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
324 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
325 55fdd257 2020-03-18 stsp bufsize = expected_size;
327 2decf4c6 2020-03-18 stsp if (outbuf) {
328 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
329 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
330 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
331 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
332 1e87a3c3 2020-03-18 stsp input_crc);
334 55fdd257 2020-03-18 stsp err = got_inflate_init(&zb, NULL, bufsize, input_crc);
338 63581804 2018-07-09 stsp *outlen = 0;
339 3ab5e33c 2020-03-18 stsp if (consumed_total)
340 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
343 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
346 63581804 2018-07-09 stsp *outlen += avail;
347 3ab5e33c 2020-03-18 stsp if (consumed_total)
348 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
349 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
350 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
352 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
353 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
354 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
355 63581804 2018-07-09 stsp if (newbuf == NULL) {
356 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
357 63581804 2018-07-09 stsp free(*outbuf);
358 63581804 2018-07-09 stsp *outbuf = NULL;
359 63581804 2018-07-09 stsp *outlen = 0;
362 63581804 2018-07-09 stsp *outbuf = newbuf;
363 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
365 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
368 63581804 2018-07-09 stsp got_inflate_end(&zb);
369 63581804 2018-07-09 stsp return err;
372 63581804 2018-07-09 stsp const struct got_error *
373 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
374 2e5a6fad 2020-03-18 stsp size_t *consumed_total, uint32_t *input_crc, uint8_t *map, size_t offset,
375 2e5a6fad 2020-03-18 stsp size_t len)
377 63581804 2018-07-09 stsp const struct got_error *err;
378 37bd7602 2018-07-23 stsp size_t avail, consumed;
379 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
380 63581804 2018-07-09 stsp void *newbuf;
381 37bd7602 2018-07-23 stsp int nbuf = 1;
383 2e5a6fad 2020-03-18 stsp if (outbuf) {
384 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
385 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
386 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
387 2e5a6fad 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
388 2e5a6fad 2020-03-18 stsp input_crc);
390 2e5a6fad 2020-03-18 stsp free(*outbuf);
391 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
392 2e5a6fad 2020-03-18 stsp return err;
395 2e5a6fad 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE,
396 2e5a6fad 2020-03-18 stsp input_crc);
399 63581804 2018-07-09 stsp *outlen = 0;
400 2e5a6fad 2020-03-18 stsp if (consumed_total)
401 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
403 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
404 63581804 2018-07-09 stsp &consumed);
407 63581804 2018-07-09 stsp offset += consumed;
408 2e5a6fad 2020-03-18 stsp if (consumed_total)
409 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
410 63581804 2018-07-09 stsp len -= consumed;
411 63581804 2018-07-09 stsp *outlen += avail;
412 63581804 2018-07-09 stsp if (len == 0)
414 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
415 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
417 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
418 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
419 63581804 2018-07-09 stsp if (newbuf == NULL) {
420 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
421 63581804 2018-07-09 stsp free(*outbuf);
422 63581804 2018-07-09 stsp *outbuf = NULL;
423 63581804 2018-07-09 stsp *outlen = 0;
426 63581804 2018-07-09 stsp *outbuf = newbuf;
427 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
428 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
430 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
432 63581804 2018-07-09 stsp got_inflate_end(&zb);
433 63581804 2018-07-09 stsp return err;
436 63581804 2018-07-09 stsp const struct got_error *
437 63581804 2018-07-09 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
439 63581804 2018-07-09 stsp const struct got_error *err = NULL;
440 63581804 2018-07-09 stsp size_t avail;
441 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
443 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
447 63581804 2018-07-09 stsp *outlen = 0;
450 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
453 63581804 2018-07-09 stsp if (avail > 0) {
455 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
456 63581804 2018-07-09 stsp if (n != avail) {
457 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
460 63581804 2018-07-09 stsp *outlen += avail;
462 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
465 63581804 2018-07-09 stsp if (err == NULL) {
466 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
467 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
469 63581804 2018-07-09 stsp got_inflate_end(&zb);
470 63581804 2018-07-09 stsp return err;
473 63581804 2018-07-09 stsp const struct got_error *
474 63581804 2018-07-09 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
476 63581804 2018-07-09 stsp const struct got_error *err;
477 63581804 2018-07-09 stsp size_t avail;
478 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
480 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
484 63581804 2018-07-09 stsp *outlen = 0;
487 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
490 63581804 2018-07-09 stsp if (avail > 0) {
492 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
493 63581804 2018-07-09 stsp if (n != 1) {
494 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
497 63581804 2018-07-09 stsp *outlen += avail;
499 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
502 63581804 2018-07-09 stsp if (err == NULL)
503 63581804 2018-07-09 stsp rewind(outfile);
504 63581804 2018-07-09 stsp got_inflate_end(&zb);
505 63581804 2018-07-09 stsp return err;
508 63581804 2018-07-09 stsp const struct got_error *
509 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
510 4788f1ce 2020-03-18 stsp uint32_t *input_crc, int infd, FILE *outfile)
512 63581804 2018-07-09 stsp const struct got_error *err;
513 4788f1ce 2020-03-18 stsp size_t avail, consumed;
514 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
516 4788f1ce 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, input_crc);
520 63581804 2018-07-09 stsp *outlen = 0;
521 4788f1ce 2020-03-18 stsp if (consumed_total)
522 4788f1ce 2020-03-18 stsp *consumed_total = 0;
524 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
527 63581804 2018-07-09 stsp if (avail > 0) {
529 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
530 63581804 2018-07-09 stsp if (n != 1) {
531 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
534 63581804 2018-07-09 stsp *outlen += avail;
535 4788f1ce 2020-03-18 stsp if (consumed_total)
536 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
538 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
541 63581804 2018-07-09 stsp if (err == NULL)
542 63581804 2018-07-09 stsp rewind(outfile);
543 63581804 2018-07-09 stsp got_inflate_end(&zb);
544 63581804 2018-07-09 stsp return err;
547 63581804 2018-07-09 stsp const struct got_error *
548 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
549 4788f1ce 2020-03-18 stsp uint32_t *input_crc, uint8_t *map, size_t offset, size_t len,
550 4788f1ce 2020-03-18 stsp FILE *outfile)
552 63581804 2018-07-09 stsp const struct got_error *err;
553 4788f1ce 2020-03-18 stsp size_t avail, consumed;
554 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
556 4788f1ce 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, input_crc);
560 63581804 2018-07-09 stsp *outlen = 0;
561 4788f1ce 2020-03-18 stsp if (consumed_total)
562 4788f1ce 2020-03-18 stsp *consumed_total = 0;
564 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
565 63581804 2018-07-09 stsp &consumed);
568 63581804 2018-07-09 stsp offset += consumed;
569 4788f1ce 2020-03-18 stsp if (consumed_total)
570 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
571 63581804 2018-07-09 stsp len -= consumed;
572 63581804 2018-07-09 stsp if (avail > 0) {
574 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
575 63581804 2018-07-09 stsp if (n != 1) {
576 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
579 63581804 2018-07-09 stsp *outlen += avail;
581 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
584 63581804 2018-07-09 stsp if (err == NULL)
585 63581804 2018-07-09 stsp rewind(outfile);
586 63581804 2018-07-09 stsp got_inflate_end(&zb);
587 63581804 2018-07-09 stsp return err;