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"
35 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 const struct got_error *
39 got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
40 struct got_inflate_checksum *csum)
42 const struct got_error *err = NULL;
45 memset(&zb->z, 0, sizeof(zb->z));
47 zb->z.zalloc = Z_NULL;
49 zerr = inflateInit(&zb->z);
52 return got_error_from_errno("inflateInit");
53 if (zerr == Z_MEM_ERROR) {
55 return got_error_from_errno("inflateInit");
57 return got_error(GOT_ERR_DECOMPRESSION);
60 zb->inlen = zb->outlen = bufsize;
62 zb->inbuf = calloc(1, zb->inlen);
63 if (zb->inbuf == NULL) {
64 err = got_error_from_errno("calloc");
70 zb->outbuf = calloc(1, zb->outlen);
71 if (zb->outbuf == NULL) {
72 err = got_error_from_errno("calloc");
75 zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
87 csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
90 *csum->input_crc = crc32(*csum->input_crc, buf, len);
93 SHA1Update(csum->input_sha1, buf, len);
97 csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
100 *csum->output_crc = crc32(*csum->output_crc, buf, len);
102 if (csum->output_sha1)
103 SHA1Update(csum->output_sha1, buf, len);
106 const struct got_error *
107 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
110 size_t last_total_out = zb->z.total_out;
111 size_t last_total_in = zb->z.total_in;
112 z_stream *z = &zb->z;
115 z->next_out = zb->outbuf;
116 z->avail_out = zb->outlen;
122 uint8_t *csum_in = NULL, *csum_out = NULL;
123 size_t csum_avail_in = 0, csum_avail_out = 0;
125 if (z->avail_in == 0) {
126 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
129 return got_ferror(f, GOT_ERR_IO);
134 z->next_in = zb->inbuf;
138 csum_in = z->next_in;
139 csum_avail_in = z->avail_in;
140 csum_out = z->next_out;
141 csum_avail_out = z->avail_out;
143 ret = inflate(z, Z_SYNC_FLUSH);
145 csum_input(zb->csum, csum_in,
146 csum_avail_in - z->avail_in);
147 csum_output(zb->csum, csum_out,
148 csum_avail_out - z->avail_out);
150 } while (ret == Z_OK && z->avail_out > 0);
152 if (ret == Z_OK || ret == Z_BUF_ERROR) {
153 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
155 if (ret != Z_STREAM_END)
156 return got_error(GOT_ERR_DECOMPRESSION);
157 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
160 *outlenp = z->total_out - last_total_out;
162 *consumed += z->total_in - last_total_in;
166 const struct got_error *
167 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
170 size_t last_total_out = zb->z.total_out;
171 size_t last_total_in = zb->z.total_in;
172 z_stream *z = &zb->z;
175 z->next_out = zb->outbuf;
176 z->avail_out = zb->outlen;
182 uint8_t *csum_in = NULL, *csum_out = NULL;
183 size_t csum_avail_in = 0, csum_avail_out = 0;
185 if (z->avail_in == 0) {
186 ssize_t n = read(fd, zb->inbuf, zb->inlen);
188 return got_error_from_errno("read");
194 z->next_in = zb->inbuf;
198 csum_in = z->next_in;
199 csum_avail_in = z->avail_in;
200 csum_out = z->next_out;
201 csum_avail_out = z->avail_out;
203 ret = inflate(z, Z_SYNC_FLUSH);
205 csum_input(zb->csum, csum_in,
206 csum_avail_in - z->avail_in);
207 csum_output(zb->csum, csum_out,
208 csum_avail_out - z->avail_out);
210 } while (ret == Z_OK && z->avail_out > 0);
212 if (ret == Z_OK || ret == Z_BUF_ERROR) {
213 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
215 if (ret != Z_STREAM_END)
216 return got_error(GOT_ERR_DECOMPRESSION);
217 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
220 *outlenp = z->total_out - last_total_out;
222 *consumed += z->total_in - last_total_in;
226 const struct got_error *
227 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
228 size_t len, size_t *outlenp, size_t *consumed)
230 size_t last_total_out = zb->z.total_out;
231 z_stream *z = &zb->z;
234 z->next_out = zb->outbuf;
235 z->avail_out = zb->outlen;
241 uint8_t *csum_in = NULL, *csum_out = NULL;
242 size_t csum_avail_in = 0, csum_avail_out = 0;
243 size_t last_total_in = zb->z.total_in;
245 if (z->avail_in == 0) {
251 z->next_in = map + offset + *consumed;
252 z->avail_in = len - *consumed;
255 csum_in = z->next_in;
256 csum_avail_in = z->avail_in;
257 csum_out = z->next_out;
258 csum_avail_out = z->avail_out;
260 ret = inflate(z, Z_SYNC_FLUSH);
262 csum_input(zb->csum, csum_in,
263 csum_avail_in - z->avail_in);
264 csum_output(zb->csum, csum_out,
265 csum_avail_out - z->avail_out);
267 *consumed += z->total_in - last_total_in;
268 } while (ret == Z_OK && z->avail_out > 0);
270 if (ret == Z_OK || ret == Z_BUF_ERROR) {
271 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
273 if (ret != Z_STREAM_END)
274 return got_error(GOT_ERR_DECOMPRESSION);
275 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
278 *outlenp = z->total_out - last_total_out;
283 got_inflate_end(struct got_inflate_buf *zb)
286 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
291 const struct got_error *
292 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
293 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
295 const struct got_error *err;
296 size_t avail, consumed;
297 struct got_inflate_buf zb;
302 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
304 return got_error_from_errno("malloc");
305 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
307 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
316 err = got_inflate_read(&zb, f, &avail, &consumed);
321 *consumed_total += consumed;
322 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
325 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
326 newbuf = reallocarray(*outbuf, ++nbuf,
327 GOT_INFLATE_BUFSIZE);
328 if (newbuf == NULL) {
329 err = got_error_from_errno("reallocarray");
336 zb.outbuf = newbuf + *outlen;
338 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
341 got_inflate_end(&zb);
345 const struct got_error *
346 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
347 size_t *consumed_total, struct got_inflate_checksum *csum,
348 size_t expected_size, int infd)
350 const struct got_error *err;
351 size_t avail, consumed;
352 struct got_inflate_buf zb;
355 size_t bufsize = GOT_INFLATE_BUFSIZE;
357 /* Optimize buffer size in case short reads should suffice. */
358 if (expected_size > 0 && expected_size < bufsize)
359 bufsize = expected_size;
362 *outbuf = malloc(bufsize);
364 return got_error_from_errno("malloc");
365 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
367 err = got_inflate_init(&zb, NULL, bufsize, csum);
376 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
381 *consumed_total += consumed;
382 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
385 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
386 newbuf = reallocarray(*outbuf, ++nbuf,
387 GOT_INFLATE_BUFSIZE);
388 if (newbuf == NULL) {
389 err = got_error_from_errno("reallocarray");
396 zb.outbuf = newbuf + *outlen;
398 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
401 got_inflate_end(&zb);
405 const struct got_error *
406 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
407 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
408 size_t offset, size_t len)
410 const struct got_error *err;
411 size_t avail, consumed;
412 struct got_inflate_buf zb;
417 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
419 return got_error_from_errno("malloc");
420 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
427 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
434 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
440 *consumed_total += consumed;
445 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
448 newbuf = reallocarray(*outbuf, ++nbuf,
449 GOT_INFLATE_BUFSIZE);
450 if (newbuf == NULL) {
451 err = got_error_from_errno("reallocarray");
458 zb.outbuf = newbuf + *outlen;
459 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
461 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
463 got_inflate_end(&zb);
467 const struct got_error *
468 got_inflate_to_fd(size_t *outlen, FILE *infile,
469 struct got_inflate_checksum *csum, int outfd)
471 const struct got_error *err = NULL;
473 struct got_inflate_buf zb;
475 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
482 err = got_inflate_read(&zb, infile, &avail, NULL);
487 n = write(outfd, zb.outbuf, avail);
489 err = got_error_from_errno("write");
494 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
498 if (lseek(outfd, SEEK_SET, 0) == -1)
499 err = got_error_from_errno("lseek");
501 got_inflate_end(&zb);
505 const struct got_error *
506 got_inflate_to_file(size_t *outlen, FILE *infile,
507 struct got_inflate_checksum *csum, FILE *outfile)
509 const struct got_error *err;
511 struct got_inflate_buf zb;
513 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
520 err = got_inflate_read(&zb, infile, &avail, NULL);
525 n = fwrite(zb.outbuf, avail, 1, outfile);
527 err = got_ferror(outfile, GOT_ERR_IO);
532 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
537 got_inflate_end(&zb);
541 const struct got_error *
542 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
543 struct got_inflate_checksum *csum, int infd, FILE *outfile)
545 const struct got_error *err;
546 size_t avail, consumed;
547 struct got_inflate_buf zb;
549 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
557 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
562 n = fwrite(zb.outbuf, avail, 1, outfile);
564 err = got_ferror(outfile, GOT_ERR_IO);
569 *consumed_total += consumed;
571 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
576 got_inflate_end(&zb);
580 const struct got_error *
581 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
582 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
583 size_t len, FILE *outfile)
585 const struct got_error *err;
586 size_t avail, consumed;
587 struct got_inflate_buf zb;
589 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
597 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
603 *consumed_total += consumed;
607 n = fwrite(zb.outbuf, avail, 1, outfile);
609 err = got_ferror(outfile, GOT_ERR_IO);
614 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
619 got_inflate_end(&zb);