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 8b925c6c 2022-07-16 thomas #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 3efd8e31 2022-10-23 thomas #include <poll.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"
33 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
36 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
39 63581804 2018-07-09 stsp const struct got_error *
40 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
41 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
43 63581804 2018-07-09 stsp const struct got_error *err = NULL;
46 fedfac2c 2022-11-08 thomas memset(zb, 0, sizeof(*zb));
48 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
49 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
50 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
51 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
52 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
53 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
54 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
55 5211b8c8 2019-03-19 stsp errno = ENOMEM;
56 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
58 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
61 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
63 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
64 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
65 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
69 63581804 2018-07-09 stsp zb->flags = 0;
70 63581804 2018-07-09 stsp if (outbuf == NULL) {
71 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
72 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
73 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
76 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
78 63581804 2018-07-09 stsp zb->outbuf = outbuf;
80 6ad68bce 2020-03-24 stsp zb->csum = csum;
83 63581804 2018-07-09 stsp got_inflate_end(zb);
88 dbaa2362 2021-09-28 thomas csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
90 6ad68bce 2020-03-24 stsp if (csum->input_crc)
91 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
93 6ad68bce 2020-03-24 stsp if (csum->input_sha1)
94 6ad68bce 2020-03-24 stsp SHA1Update(csum->input_sha1, buf, len);
98 dbaa2362 2021-09-28 thomas csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
100 d5c81d44 2021-07-08 stsp if (csum->output_crc)
101 d5c81d44 2021-07-08 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
103 d5c81d44 2021-07-08 stsp if (csum->output_sha1)
104 d5c81d44 2021-07-08 stsp SHA1Update(csum->output_sha1, buf, len);
107 63581804 2018-07-09 stsp const struct got_error *
108 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
109 abc59930 2021-09-05 naddy size_t *consumed)
111 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
112 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
113 63581804 2018-07-09 stsp z_stream *z = &zb->z;
114 63581804 2018-07-09 stsp int ret = Z_ERRNO;
116 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
117 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
119 63581804 2018-07-09 stsp *outlenp = 0;
120 6fb3a497 2020-03-18 stsp if (consumed)
121 6fb3a497 2020-03-18 stsp *consumed = 0;
123 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
124 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
126 63581804 2018-07-09 stsp if (z->avail_in == 0) {
127 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
128 63581804 2018-07-09 stsp if (n == 0) {
129 63581804 2018-07-09 stsp if (ferror(f))
130 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
132 63581804 2018-07-09 stsp ret = Z_STREAM_END;
135 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
136 63581804 2018-07-09 stsp z->avail_in = n;
138 6ad68bce 2020-03-24 stsp if (zb->csum) {
139 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
140 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
141 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
142 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
144 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
145 d5c81d44 2021-07-08 stsp if (zb->csum) {
146 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
147 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
148 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
149 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
151 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
153 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
154 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
156 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
157 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
158 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
161 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
162 6fb3a497 2020-03-18 stsp if (consumed)
163 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
164 63581804 2018-07-09 stsp return NULL;
167 63581804 2018-07-09 stsp const struct got_error *
168 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
169 3ab5e33c 2020-03-18 stsp size_t *consumed)
171 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
172 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
173 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
174 63581804 2018-07-09 stsp z_stream *z = &zb->z;
175 63581804 2018-07-09 stsp int ret = Z_ERRNO;
177 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
178 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
180 63581804 2018-07-09 stsp *outlenp = 0;
181 3ab5e33c 2020-03-18 stsp if (consumed)
182 3ab5e33c 2020-03-18 stsp *consumed = 0;
184 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
185 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
187 63581804 2018-07-09 stsp if (z->avail_in == 0) {
188 3efd8e31 2022-10-23 thomas ssize_t n;
189 3efd8e31 2022-10-23 thomas err = got_poll_fd(fd, POLLIN, INFTIM);
190 3efd8e31 2022-10-23 thomas if (err) {
191 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_EOF) {
192 3efd8e31 2022-10-23 thomas ret = Z_STREAM_END;
195 3efd8e31 2022-10-23 thomas return err;
197 3efd8e31 2022-10-23 thomas n = read(fd, zb->inbuf, zb->inlen);
199 638f9024 2019-05-13 stsp return got_error_from_errno("read");
200 63581804 2018-07-09 stsp else if (n == 0) {
202 63581804 2018-07-09 stsp ret = Z_STREAM_END;
205 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
206 63581804 2018-07-09 stsp z->avail_in = n;
208 6ad68bce 2020-03-24 stsp if (zb->csum) {
209 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
210 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
211 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
212 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
214 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
215 d5c81d44 2021-07-08 stsp if (zb->csum) {
216 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
217 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
218 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
219 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
221 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
223 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
224 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
226 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
227 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
228 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
231 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
232 3ab5e33c 2020-03-18 stsp if (consumed)
233 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
234 63581804 2018-07-09 stsp return NULL;
237 63581804 2018-07-09 stsp const struct got_error *
238 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
239 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
241 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
242 63581804 2018-07-09 stsp z_stream *z = &zb->z;
243 63581804 2018-07-09 stsp int ret = Z_ERRNO;
245 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
246 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
248 63581804 2018-07-09 stsp *outlenp = 0;
249 63581804 2018-07-09 stsp *consumed = 0;
252 dbaa2362 2021-09-28 thomas uint8_t *csum_in = NULL, *csum_out = NULL;
253 d5c81d44 2021-07-08 stsp size_t csum_avail_in = 0, csum_avail_out = 0;
254 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
256 63581804 2018-07-09 stsp if (z->avail_in == 0) {
257 63581804 2018-07-09 stsp if (len == 0) {
259 63581804 2018-07-09 stsp ret = Z_STREAM_END;
262 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
263 f6a55b40 2022-02-12 thomas if (len - *consumed > UINT_MAX)
264 f6a55b40 2022-02-12 thomas z->avail_in = UINT_MAX;
266 f6a55b40 2022-02-12 thomas z->avail_in = len - *consumed;
268 6ad68bce 2020-03-24 stsp if (zb->csum) {
269 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
270 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
271 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
272 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
274 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
275 d5c81d44 2021-07-08 stsp if (zb->csum) {
276 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
277 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
278 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
279 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
281 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
282 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
284 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
285 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
287 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
288 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
289 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
292 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
293 63581804 2018-07-09 stsp return NULL;
297 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
299 63581804 2018-07-09 stsp free(zb->inbuf);
300 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
301 63581804 2018-07-09 stsp free(zb->outbuf);
302 63581804 2018-07-09 stsp inflateEnd(&zb->z);
305 63581804 2018-07-09 stsp const struct got_error *
306 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
307 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
309 63581804 2018-07-09 stsp const struct got_error *err;
310 6fb3a497 2020-03-18 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 17d745b8 2018-07-23 stsp int nbuf = 1;
315 2decf4c6 2020-03-18 stsp if (outbuf) {
316 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
317 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
318 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
319 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
321 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
323 63581804 2018-07-09 stsp return err;
325 63581804 2018-07-09 stsp *outlen = 0;
326 6fb3a497 2020-03-18 stsp if (consumed_total)
327 6fb3a497 2020-03-18 stsp *consumed_total = 0;
330 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
333 63581804 2018-07-09 stsp *outlen += avail;
334 6fb3a497 2020-03-18 stsp if (consumed_total)
335 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
336 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
337 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
339 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
340 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
341 63581804 2018-07-09 stsp if (newbuf == NULL) {
342 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
343 63581804 2018-07-09 stsp free(*outbuf);
344 63581804 2018-07-09 stsp *outbuf = NULL;
345 63581804 2018-07-09 stsp *outlen = 0;
348 63581804 2018-07-09 stsp *outbuf = newbuf;
349 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
350 9c8bf189 2022-02-12 thomas zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
352 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
355 63581804 2018-07-09 stsp got_inflate_end(&zb);
356 63581804 2018-07-09 stsp return err;
359 63581804 2018-07-09 stsp const struct got_error *
360 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
361 b6b86fd1 2022-08-30 thomas size_t *consumed_total, struct got_inflate_checksum *csum,
362 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
364 63581804 2018-07-09 stsp const struct got_error *err;
365 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
366 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
367 63581804 2018-07-09 stsp void *newbuf;
368 17d745b8 2018-07-23 stsp int nbuf = 1;
369 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
371 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
372 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
373 55fdd257 2020-03-18 stsp bufsize = expected_size;
375 2decf4c6 2020-03-18 stsp if (outbuf) {
376 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
377 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
378 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
379 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
381 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
385 63581804 2018-07-09 stsp *outlen = 0;
386 3ab5e33c 2020-03-18 stsp if (consumed_total)
387 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
390 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
393 63581804 2018-07-09 stsp *outlen += avail;
394 3ab5e33c 2020-03-18 stsp if (consumed_total)
395 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
396 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
397 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
399 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
400 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
401 63581804 2018-07-09 stsp if (newbuf == NULL) {
402 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
403 63581804 2018-07-09 stsp free(*outbuf);
404 63581804 2018-07-09 stsp *outbuf = NULL;
405 63581804 2018-07-09 stsp *outlen = 0;
408 63581804 2018-07-09 stsp *outbuf = newbuf;
409 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
410 9c8bf189 2022-02-12 thomas zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
412 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
415 63581804 2018-07-09 stsp got_inflate_end(&zb);
416 63581804 2018-07-09 stsp return err;
419 63581804 2018-07-09 stsp const struct got_error *
420 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
421 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
422 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
424 63581804 2018-07-09 stsp const struct got_error *err;
425 37bd7602 2018-07-23 stsp size_t avail, consumed;
426 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
427 63581804 2018-07-09 stsp void *newbuf;
428 37bd7602 2018-07-23 stsp int nbuf = 1;
430 2e5a6fad 2020-03-18 stsp if (outbuf) {
431 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
432 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
433 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
434 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
436 2e5a6fad 2020-03-18 stsp free(*outbuf);
437 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
438 2e5a6fad 2020-03-18 stsp return err;
441 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
443 d94c35b0 2022-01-23 thomas return err;
446 63581804 2018-07-09 stsp *outlen = 0;
447 2e5a6fad 2020-03-18 stsp if (consumed_total)
448 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
450 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
451 63581804 2018-07-09 stsp &consumed);
454 63581804 2018-07-09 stsp offset += consumed;
455 2e5a6fad 2020-03-18 stsp if (consumed_total)
456 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
457 63581804 2018-07-09 stsp len -= consumed;
458 63581804 2018-07-09 stsp *outlen += avail;
459 63581804 2018-07-09 stsp if (len == 0)
461 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
462 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
464 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
465 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
466 63581804 2018-07-09 stsp if (newbuf == NULL) {
467 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
468 63581804 2018-07-09 stsp free(*outbuf);
469 63581804 2018-07-09 stsp *outbuf = NULL;
470 63581804 2018-07-09 stsp *outlen = 0;
473 63581804 2018-07-09 stsp *outbuf = newbuf;
474 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
475 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
477 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
479 63581804 2018-07-09 stsp got_inflate_end(&zb);
480 63581804 2018-07-09 stsp return err;
483 63581804 2018-07-09 stsp const struct got_error *
484 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
485 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
487 63581804 2018-07-09 stsp const struct got_error *err = NULL;
488 63581804 2018-07-09 stsp size_t avail;
489 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
491 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
495 63581804 2018-07-09 stsp *outlen = 0;
498 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
501 63581804 2018-07-09 stsp if (avail > 0) {
503 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
504 63581804 2018-07-09 stsp if (n != avail) {
505 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
508 63581804 2018-07-09 stsp *outlen += avail;
510 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
513 63581804 2018-07-09 stsp if (err == NULL) {
514 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
515 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
517 63581804 2018-07-09 stsp got_inflate_end(&zb);
518 63581804 2018-07-09 stsp return err;
521 63581804 2018-07-09 stsp const struct got_error *
522 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
523 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
525 63581804 2018-07-09 stsp const struct got_error *err;
526 63581804 2018-07-09 stsp size_t avail;
527 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
529 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
533 63581804 2018-07-09 stsp *outlen = 0;
536 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
539 63581804 2018-07-09 stsp if (avail > 0) {
541 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
542 63581804 2018-07-09 stsp if (n != 1) {
543 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
546 63581804 2018-07-09 stsp *outlen += avail;
548 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
551 63581804 2018-07-09 stsp if (err == NULL)
552 63581804 2018-07-09 stsp rewind(outfile);
553 63581804 2018-07-09 stsp got_inflate_end(&zb);
554 63581804 2018-07-09 stsp return err;
557 63581804 2018-07-09 stsp const struct got_error *
558 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
559 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
561 63581804 2018-07-09 stsp const struct got_error *err;
562 4788f1ce 2020-03-18 stsp size_t avail, consumed;
563 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
565 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
569 63581804 2018-07-09 stsp *outlen = 0;
570 4788f1ce 2020-03-18 stsp if (consumed_total)
571 4788f1ce 2020-03-18 stsp *consumed_total = 0;
573 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
576 63581804 2018-07-09 stsp if (avail > 0) {
578 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
579 63581804 2018-07-09 stsp if (n != 1) {
580 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
583 63581804 2018-07-09 stsp *outlen += avail;
584 4788f1ce 2020-03-18 stsp if (consumed_total)
585 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
587 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
590 63581804 2018-07-09 stsp if (err == NULL)
591 63581804 2018-07-09 stsp rewind(outfile);
592 63581804 2018-07-09 stsp got_inflate_end(&zb);
593 63581804 2018-07-09 stsp return err;
596 63581804 2018-07-09 stsp const struct got_error *
597 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
598 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
599 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
601 63581804 2018-07-09 stsp const struct got_error *err;
602 4788f1ce 2020-03-18 stsp size_t avail, consumed;
603 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
605 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
609 63581804 2018-07-09 stsp *outlen = 0;
610 4788f1ce 2020-03-18 stsp if (consumed_total)
611 4788f1ce 2020-03-18 stsp *consumed_total = 0;
613 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
614 63581804 2018-07-09 stsp &consumed);
617 63581804 2018-07-09 stsp offset += consumed;
618 4788f1ce 2020-03-18 stsp if (consumed_total)
619 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
620 63581804 2018-07-09 stsp len -= consumed;
621 63581804 2018-07-09 stsp if (avail > 0) {
623 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
624 63581804 2018-07-09 stsp if (n != 1) {
625 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
628 63581804 2018-07-09 stsp *outlen += avail;
630 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
633 63581804 2018-07-09 stsp if (err == NULL)
634 63581804 2018-07-09 stsp rewind(outfile);
635 63581804 2018-07-09 stsp got_inflate_end(&zb);
636 63581804 2018-07-09 stsp return err;