2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
28 #include "got_error.h"
29 #include "got_object.h"
32 #include "got_lib_inflate.h"
33 #include "got_lib_poll.h"
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
39 const struct got_error *
40 got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
41 struct got_inflate_checksum *csum)
43 const struct got_error *err = NULL;
46 memset(zb, 0, sizeof(*zb));
48 zb->z.zalloc = Z_NULL;
50 zerr = inflateInit(&zb->z);
53 return got_error_from_errno("inflateInit");
54 if (zerr == Z_MEM_ERROR) {
56 return got_error_from_errno("inflateInit");
58 return got_error(GOT_ERR_DECOMPRESSION);
61 zb->inlen = zb->outlen = bufsize;
63 zb->inbuf = calloc(1, zb->inlen);
64 if (zb->inbuf == NULL) {
65 err = got_error_from_errno("calloc");
71 zb->outbuf = calloc(1, zb->outlen);
72 if (zb->outbuf == NULL) {
73 err = got_error_from_errno("calloc");
76 zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
88 csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
91 *csum->input_crc = crc32(*csum->input_crc, buf, len);
94 SHA1Update(csum->input_sha1, buf, len);
98 csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
100 if (csum->output_crc)
101 *csum->output_crc = crc32(*csum->output_crc, buf, len);
103 if (csum->output_sha1)
104 SHA1Update(csum->output_sha1, buf, len);
107 const struct got_error *
108 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
111 size_t last_total_out = zb->z.total_out;
112 size_t last_total_in = zb->z.total_in;
113 z_stream *z = &zb->z;
116 z->next_out = zb->outbuf;
117 z->avail_out = zb->outlen;
123 uint8_t *csum_in = NULL, *csum_out = NULL;
124 size_t csum_avail_in = 0, csum_avail_out = 0;
126 if (z->avail_in == 0) {
127 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
130 return got_ferror(f, GOT_ERR_IO);
135 z->next_in = zb->inbuf;
139 csum_in = z->next_in;
140 csum_avail_in = z->avail_in;
141 csum_out = z->next_out;
142 csum_avail_out = z->avail_out;
144 ret = inflate(z, Z_SYNC_FLUSH);
146 csum_input(zb->csum, csum_in,
147 csum_avail_in - z->avail_in);
148 csum_output(zb->csum, csum_out,
149 csum_avail_out - z->avail_out);
151 } while (ret == Z_OK && z->avail_out > 0);
153 if (ret == Z_OK || ret == Z_BUF_ERROR) {
154 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
156 if (ret != Z_STREAM_END)
157 return got_error(GOT_ERR_DECOMPRESSION);
158 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
161 *outlenp = z->total_out - last_total_out;
163 *consumed += z->total_in - last_total_in;
167 const struct got_error *
168 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
171 const struct got_error *err = NULL;
172 size_t last_total_out = zb->z.total_out;
173 size_t last_total_in = zb->z.total_in;
174 z_stream *z = &zb->z;
177 z->next_out = zb->outbuf;
178 z->avail_out = zb->outlen;
184 uint8_t *csum_in = NULL, *csum_out = NULL;
185 size_t csum_avail_in = 0, csum_avail_out = 0;
187 if (z->avail_in == 0) {
189 err = got_poll_fd(fd, POLLIN, INFTIM);
191 if (err->code == GOT_ERR_EOF) {
197 n = read(fd, zb->inbuf, zb->inlen);
199 return got_error_from_errno("read");
205 z->next_in = zb->inbuf;
209 csum_in = z->next_in;
210 csum_avail_in = z->avail_in;
211 csum_out = z->next_out;
212 csum_avail_out = z->avail_out;
214 ret = inflate(z, Z_SYNC_FLUSH);
216 csum_input(zb->csum, csum_in,
217 csum_avail_in - z->avail_in);
218 csum_output(zb->csum, csum_out,
219 csum_avail_out - z->avail_out);
221 } while (ret == Z_OK && z->avail_out > 0);
223 if (ret == Z_OK || ret == Z_BUF_ERROR) {
224 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
226 if (ret != Z_STREAM_END)
227 return got_error(GOT_ERR_DECOMPRESSION);
228 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
231 *outlenp = z->total_out - last_total_out;
233 *consumed += z->total_in - last_total_in;
237 const struct got_error *
238 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
239 size_t len, size_t *outlenp, size_t *consumed)
241 size_t last_total_out = zb->z.total_out;
242 z_stream *z = &zb->z;
245 z->next_out = zb->outbuf;
246 z->avail_out = zb->outlen;
252 uint8_t *csum_in = NULL, *csum_out = NULL;
253 size_t csum_avail_in = 0, csum_avail_out = 0;
254 size_t last_total_in = zb->z.total_in;
256 if (z->avail_in == 0) {
262 z->next_in = map + offset + *consumed;
263 if (len - *consumed > UINT_MAX)
264 z->avail_in = UINT_MAX;
266 z->avail_in = len - *consumed;
269 csum_in = z->next_in;
270 csum_avail_in = z->avail_in;
271 csum_out = z->next_out;
272 csum_avail_out = z->avail_out;
274 ret = inflate(z, Z_SYNC_FLUSH);
276 csum_input(zb->csum, csum_in,
277 csum_avail_in - z->avail_in);
278 csum_output(zb->csum, csum_out,
279 csum_avail_out - z->avail_out);
281 *consumed += z->total_in - last_total_in;
282 } while (ret == Z_OK && z->avail_out > 0);
284 if (ret == Z_OK || ret == Z_BUF_ERROR) {
285 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
287 if (ret != Z_STREAM_END)
288 return got_error(GOT_ERR_DECOMPRESSION);
289 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
292 *outlenp = z->total_out - last_total_out;
297 got_inflate_end(struct got_inflate_buf *zb)
300 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
305 const struct got_error *
306 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
307 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
309 const struct got_error *err;
310 size_t avail, consumed;
311 struct got_inflate_buf zb;
316 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
318 return got_error_from_errno("malloc");
319 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
321 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
330 err = got_inflate_read(&zb, f, &avail, &consumed);
335 *consumed_total += consumed;
336 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
339 newbuf = reallocarray(*outbuf, ++nbuf,
340 GOT_INFLATE_BUFSIZE);
341 if (newbuf == NULL) {
342 err = got_error_from_errno("reallocarray");
349 zb.outbuf = newbuf + *outlen;
350 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
352 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
355 got_inflate_end(&zb);
359 const struct got_error *
360 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
361 size_t *consumed_total, struct got_inflate_checksum *csum,
362 size_t expected_size, int infd)
364 const struct got_error *err;
365 size_t avail, consumed;
366 struct got_inflate_buf zb;
369 size_t bufsize = GOT_INFLATE_BUFSIZE;
371 /* Optimize buffer size in case short reads should suffice. */
372 if (expected_size > 0 && expected_size < bufsize)
373 bufsize = expected_size;
376 *outbuf = malloc(bufsize);
378 return got_error_from_errno("malloc");
379 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
381 err = got_inflate_init(&zb, NULL, bufsize, csum);
390 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
395 *consumed_total += consumed;
396 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
399 newbuf = reallocarray(*outbuf, ++nbuf,
400 GOT_INFLATE_BUFSIZE);
401 if (newbuf == NULL) {
402 err = got_error_from_errno("reallocarray");
409 zb.outbuf = newbuf + *outlen;
410 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
412 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
415 got_inflate_end(&zb);
419 const struct got_error *
420 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
421 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
422 size_t offset, size_t len)
424 const struct got_error *err;
425 size_t avail, consumed;
426 struct got_inflate_buf zb;
431 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
433 return got_error_from_errno("malloc");
434 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
441 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
450 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
456 *consumed_total += consumed;
461 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
464 newbuf = reallocarray(*outbuf, ++nbuf,
465 GOT_INFLATE_BUFSIZE);
466 if (newbuf == NULL) {
467 err = got_error_from_errno("reallocarray");
474 zb.outbuf = newbuf + *outlen;
475 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
477 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
479 got_inflate_end(&zb);
483 const struct got_error *
484 got_inflate_to_fd(size_t *outlen, FILE *infile,
485 struct got_inflate_checksum *csum, int outfd)
487 const struct got_error *err = NULL;
489 struct got_inflate_buf zb;
491 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
498 err = got_inflate_read(&zb, infile, &avail, NULL);
503 n = write(outfd, zb.outbuf, avail);
505 err = got_error_from_errno("write");
510 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
514 if (lseek(outfd, SEEK_SET, 0) == -1)
515 err = got_error_from_errno("lseek");
517 got_inflate_end(&zb);
521 const struct got_error *
522 got_inflate_to_file(size_t *outlen, FILE *infile,
523 struct got_inflate_checksum *csum, FILE *outfile)
525 const struct got_error *err;
527 struct got_inflate_buf zb;
529 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
536 err = got_inflate_read(&zb, infile, &avail, NULL);
541 n = fwrite(zb.outbuf, avail, 1, outfile);
543 err = got_ferror(outfile, GOT_ERR_IO);
548 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
553 got_inflate_end(&zb);
557 const struct got_error *
558 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
559 struct got_inflate_checksum *csum, int infd, FILE *outfile)
561 const struct got_error *err;
562 size_t avail, consumed;
563 struct got_inflate_buf zb;
565 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
573 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
578 n = fwrite(zb.outbuf, avail, 1, outfile);
580 err = got_ferror(outfile, GOT_ERR_IO);
585 *consumed_total += consumed;
587 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
592 got_inflate_end(&zb);
596 const struct got_error *
597 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
598 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
599 size_t len, FILE *outfile)
601 const struct got_error *err;
602 size_t avail, consumed;
603 struct got_inflate_buf zb;
605 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
613 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
619 *consumed_total += consumed;
623 n = fwrite(zb.outbuf, avail, 1, outfile);
625 err = got_ferror(outfile, GOT_ERR_IO);
630 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
635 got_inflate_end(&zb);