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 char *buf, size_t len)
90 *csum->input_crc = crc32(*csum->input_crc, buf, len);
93 SHA1Update(csum->input_sha1, buf, len);
96 const struct got_error *
97 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
100 size_t last_total_out = zb->z.total_out;
101 size_t last_total_in = zb->z.total_in;
102 z_stream *z = &zb->z;
105 z->next_out = zb->outbuf;
106 z->avail_out = zb->outlen;
112 char *csum_in = NULL;
113 size_t csum_avail = 0;
115 if (z->avail_in == 0) {
116 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
119 return got_ferror(f, GOT_ERR_IO);
124 z->next_in = zb->inbuf;
128 csum_in = z->next_in;
129 csum_avail = z->avail_in;
131 ret = inflate(z, Z_SYNC_FLUSH);
133 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
134 } while (ret == Z_OK && z->avail_out > 0);
136 if (ret == Z_OK || ret == Z_BUF_ERROR) {
137 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
139 if (ret != Z_STREAM_END)
140 return got_error(GOT_ERR_DECOMPRESSION);
141 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
144 *outlenp = z->total_out - last_total_out;
146 *consumed += z->total_in - last_total_in;
150 const struct got_error *
151 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
154 size_t last_total_out = zb->z.total_out;
155 size_t last_total_in = zb->z.total_in;
156 z_stream *z = &zb->z;
159 z->next_out = zb->outbuf;
160 z->avail_out = zb->outlen;
166 char *csum_in = NULL;
167 size_t csum_avail = 0;
169 if (z->avail_in == 0) {
170 ssize_t n = read(fd, zb->inbuf, zb->inlen);
172 return got_error_from_errno("read");
178 z->next_in = zb->inbuf;
182 csum_in = z->next_in;
183 csum_avail = z->avail_in;
185 ret = inflate(z, Z_SYNC_FLUSH);
187 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
188 } while (ret == Z_OK && z->avail_out > 0);
190 if (ret == Z_OK || ret == Z_BUF_ERROR) {
191 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
193 if (ret != Z_STREAM_END)
194 return got_error(GOT_ERR_DECOMPRESSION);
195 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
198 *outlenp = z->total_out - last_total_out;
200 *consumed += z->total_in - last_total_in;
204 const struct got_error *
205 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
206 size_t len, size_t *outlenp, size_t *consumed)
208 size_t last_total_out = zb->z.total_out;
209 z_stream *z = &zb->z;
212 z->next_out = zb->outbuf;
213 z->avail_out = zb->outlen;
219 char *csum_in = NULL;
220 size_t csum_avail = 0;
221 size_t last_total_in = zb->z.total_in;
223 if (z->avail_in == 0) {
229 z->next_in = map + offset + *consumed;
230 z->avail_in = len - *consumed;
233 csum_in = z->next_in;
234 csum_avail = z->avail_in;
236 ret = inflate(z, Z_SYNC_FLUSH);
238 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
239 *consumed += z->total_in - last_total_in;
240 } while (ret == Z_OK && z->avail_out > 0);
242 if (ret == Z_OK || ret == Z_BUF_ERROR) {
243 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
245 if (ret != Z_STREAM_END)
246 return got_error(GOT_ERR_DECOMPRESSION);
247 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
250 *outlenp = z->total_out - last_total_out;
255 got_inflate_end(struct got_inflate_buf *zb)
258 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
263 const struct got_error *
264 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
265 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
267 const struct got_error *err;
268 size_t avail, consumed;
269 struct got_inflate_buf zb;
274 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
276 return got_error_from_errno("malloc");
277 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
279 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
288 err = got_inflate_read(&zb, f, &avail, &consumed);
293 *consumed_total += consumed;
294 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
297 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
298 newbuf = reallocarray(*outbuf, ++nbuf,
299 GOT_INFLATE_BUFSIZE);
300 if (newbuf == NULL) {
301 err = got_error_from_errno("reallocarray");
308 zb.outbuf = newbuf + *outlen;
310 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
313 got_inflate_end(&zb);
317 const struct got_error *
318 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
319 size_t *consumed_total, struct got_inflate_checksum *csum,
320 size_t expected_size, int infd)
322 const struct got_error *err;
323 size_t avail, consumed;
324 struct got_inflate_buf zb;
327 size_t bufsize = GOT_INFLATE_BUFSIZE;
329 /* Optimize buffer size in case short reads should suffice. */
330 if (expected_size > 0 && expected_size < bufsize)
331 bufsize = expected_size;
334 *outbuf = malloc(bufsize);
336 return got_error_from_errno("malloc");
337 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
339 err = got_inflate_init(&zb, NULL, bufsize, csum);
348 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
353 *consumed_total += consumed;
354 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
357 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
358 newbuf = reallocarray(*outbuf, ++nbuf,
359 GOT_INFLATE_BUFSIZE);
360 if (newbuf == NULL) {
361 err = got_error_from_errno("reallocarray");
368 zb.outbuf = newbuf + *outlen;
370 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
373 got_inflate_end(&zb);
377 const struct got_error *
378 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
379 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
380 size_t offset, size_t len)
382 const struct got_error *err;
383 size_t avail, consumed;
384 struct got_inflate_buf zb;
389 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
391 return got_error_from_errno("malloc");
392 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
399 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
406 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
412 *consumed_total += consumed;
417 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
420 newbuf = reallocarray(*outbuf, ++nbuf,
421 GOT_INFLATE_BUFSIZE);
422 if (newbuf == NULL) {
423 err = got_error_from_errno("reallocarray");
430 zb.outbuf = newbuf + *outlen;
431 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
433 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
435 got_inflate_end(&zb);
439 const struct got_error *
440 got_inflate_to_fd(size_t *outlen, FILE *infile,
441 struct got_inflate_checksum *csum, int outfd)
443 const struct got_error *err = NULL;
445 struct got_inflate_buf zb;
447 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
454 err = got_inflate_read(&zb, infile, &avail, NULL);
459 n = write(outfd, zb.outbuf, avail);
461 err = got_error_from_errno("write");
466 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
470 if (lseek(outfd, SEEK_SET, 0) == -1)
471 err = got_error_from_errno("lseek");
473 got_inflate_end(&zb);
477 const struct got_error *
478 got_inflate_to_file(size_t *outlen, FILE *infile,
479 struct got_inflate_checksum *csum, FILE *outfile)
481 const struct got_error *err;
483 struct got_inflate_buf zb;
485 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
492 err = got_inflate_read(&zb, infile, &avail, NULL);
497 n = fwrite(zb.outbuf, avail, 1, outfile);
499 err = got_ferror(outfile, GOT_ERR_IO);
504 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
509 got_inflate_end(&zb);
513 const struct got_error *
514 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
515 struct got_inflate_checksum *csum, int infd, FILE *outfile)
517 const struct got_error *err;
518 size_t avail, consumed;
519 struct got_inflate_buf zb;
521 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
529 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
534 n = fwrite(zb.outbuf, avail, 1, outfile);
536 err = got_ferror(outfile, GOT_ERR_IO);
541 *consumed_total += consumed;
543 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
548 got_inflate_end(&zb);
552 const struct got_error *
553 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
554 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
555 size_t len, FILE *outfile)
557 const struct got_error *err;
558 size_t avail, consumed;
559 struct got_inflate_buf zb;
561 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
569 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
575 *consumed_total += consumed;
579 n = fwrite(zb.outbuf, avail, 1, outfile);
581 err = got_ferror(outfile, GOT_ERR_IO);
586 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
591 got_inflate_end(&zb);