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);
436 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
442 *consumed_total += consumed;
447 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
450 newbuf = reallocarray(*outbuf, ++nbuf,
451 GOT_INFLATE_BUFSIZE);
452 if (newbuf == NULL) {
453 err = got_error_from_errno("reallocarray");
460 zb.outbuf = newbuf + *outlen;
461 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
463 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
465 got_inflate_end(&zb);
469 const struct got_error *
470 got_inflate_to_fd(size_t *outlen, FILE *infile,
471 struct got_inflate_checksum *csum, int outfd)
473 const struct got_error *err = NULL;
475 struct got_inflate_buf zb;
477 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
484 err = got_inflate_read(&zb, infile, &avail, NULL);
489 n = write(outfd, zb.outbuf, avail);
491 err = got_error_from_errno("write");
496 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
500 if (lseek(outfd, SEEK_SET, 0) == -1)
501 err = got_error_from_errno("lseek");
503 got_inflate_end(&zb);
507 const struct got_error *
508 got_inflate_to_file(size_t *outlen, FILE *infile,
509 struct got_inflate_checksum *csum, FILE *outfile)
511 const struct got_error *err;
513 struct got_inflate_buf zb;
515 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
522 err = got_inflate_read(&zb, infile, &avail, NULL);
527 n = fwrite(zb.outbuf, avail, 1, outfile);
529 err = got_ferror(outfile, GOT_ERR_IO);
534 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
539 got_inflate_end(&zb);
543 const struct got_error *
544 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
545 struct got_inflate_checksum *csum, int infd, FILE *outfile)
547 const struct got_error *err;
548 size_t avail, consumed;
549 struct got_inflate_buf zb;
551 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
559 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
564 n = fwrite(zb.outbuf, avail, 1, outfile);
566 err = got_ferror(outfile, GOT_ERR_IO);
571 *consumed_total += consumed;
573 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
578 got_inflate_end(&zb);
582 const struct got_error *
583 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
584 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
585 size_t len, FILE *outfile)
587 const struct got_error *err;
588 size_t avail, consumed;
589 struct got_inflate_buf zb;
591 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
599 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
605 *consumed_total += consumed;
609 n = fwrite(zb.outbuf, avail, 1, outfile);
611 err = got_ferror(outfile, GOT_ERR_IO);
616 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
621 got_inflate_end(&zb);