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>
27 #include "got_error.h"
28 #include "got_object.h"
31 #include "got_lib_inflate.h"
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 const struct got_error *
38 got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
39 struct got_inflate_checksum *csum)
41 const struct got_error *err = NULL;
44 memset(&zb->z, 0, sizeof(zb->z));
46 zb->z.zalloc = Z_NULL;
48 zerr = inflateInit(&zb->z);
51 return got_error_from_errno("inflateInit");
52 if (zerr == Z_MEM_ERROR) {
54 return got_error_from_errno("inflateInit");
56 return got_error(GOT_ERR_DECOMPRESSION);
59 zb->inlen = zb->outlen = bufsize;
61 zb->inbuf = calloc(1, zb->inlen);
62 if (zb->inbuf == NULL) {
63 err = got_error_from_errno("calloc");
69 zb->outbuf = calloc(1, zb->outlen);
70 if (zb->outbuf == NULL) {
71 err = got_error_from_errno("calloc");
74 zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
86 csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
89 *csum->input_crc = crc32(*csum->input_crc, buf, len);
92 SHA1Update(csum->input_sha1, buf, len);
96 csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
99 *csum->output_crc = crc32(*csum->output_crc, buf, len);
101 if (csum->output_sha1)
102 SHA1Update(csum->output_sha1, buf, len);
105 const struct got_error *
106 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
109 size_t last_total_out = zb->z.total_out;
110 size_t last_total_in = zb->z.total_in;
111 z_stream *z = &zb->z;
114 z->next_out = zb->outbuf;
115 z->avail_out = zb->outlen;
121 uint8_t *csum_in = NULL, *csum_out = NULL;
122 size_t csum_avail_in = 0, csum_avail_out = 0;
124 if (z->avail_in == 0) {
125 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
128 return got_ferror(f, GOT_ERR_IO);
133 z->next_in = zb->inbuf;
137 csum_in = z->next_in;
138 csum_avail_in = z->avail_in;
139 csum_out = z->next_out;
140 csum_avail_out = z->avail_out;
142 ret = inflate(z, Z_SYNC_FLUSH);
144 csum_input(zb->csum, csum_in,
145 csum_avail_in - z->avail_in);
146 csum_output(zb->csum, csum_out,
147 csum_avail_out - z->avail_out);
149 } while (ret == Z_OK && z->avail_out > 0);
151 if (ret == Z_OK || ret == Z_BUF_ERROR) {
152 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
154 if (ret != Z_STREAM_END)
155 return got_error(GOT_ERR_DECOMPRESSION);
156 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
159 *outlenp = z->total_out - last_total_out;
161 *consumed += z->total_in - last_total_in;
165 const struct got_error *
166 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
169 size_t last_total_out = zb->z.total_out;
170 size_t last_total_in = zb->z.total_in;
171 z_stream *z = &zb->z;
174 z->next_out = zb->outbuf;
175 z->avail_out = zb->outlen;
181 uint8_t *csum_in = NULL, *csum_out = NULL;
182 size_t csum_avail_in = 0, csum_avail_out = 0;
184 if (z->avail_in == 0) {
185 ssize_t n = read(fd, zb->inbuf, zb->inlen);
187 return got_error_from_errno("read");
193 z->next_in = zb->inbuf;
197 csum_in = z->next_in;
198 csum_avail_in = z->avail_in;
199 csum_out = z->next_out;
200 csum_avail_out = z->avail_out;
202 ret = inflate(z, Z_SYNC_FLUSH);
204 csum_input(zb->csum, csum_in,
205 csum_avail_in - z->avail_in);
206 csum_output(zb->csum, csum_out,
207 csum_avail_out - z->avail_out);
209 } while (ret == Z_OK && z->avail_out > 0);
211 if (ret == Z_OK || ret == Z_BUF_ERROR) {
212 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
214 if (ret != Z_STREAM_END)
215 return got_error(GOT_ERR_DECOMPRESSION);
216 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
219 *outlenp = z->total_out - last_total_out;
221 *consumed += z->total_in - last_total_in;
225 const struct got_error *
226 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
227 size_t len, size_t *outlenp, size_t *consumed)
229 size_t last_total_out = zb->z.total_out;
230 z_stream *z = &zb->z;
233 z->next_out = zb->outbuf;
234 z->avail_out = zb->outlen;
240 uint8_t *csum_in = NULL, *csum_out = NULL;
241 size_t csum_avail_in = 0, csum_avail_out = 0;
242 size_t last_total_in = zb->z.total_in;
244 if (z->avail_in == 0) {
250 z->next_in = map + offset + *consumed;
251 if (len - *consumed > UINT_MAX)
252 z->avail_in = UINT_MAX;
254 z->avail_in = len - *consumed;
257 csum_in = z->next_in;
258 csum_avail_in = z->avail_in;
259 csum_out = z->next_out;
260 csum_avail_out = z->avail_out;
262 ret = inflate(z, Z_SYNC_FLUSH);
264 csum_input(zb->csum, csum_in,
265 csum_avail_in - z->avail_in);
266 csum_output(zb->csum, csum_out,
267 csum_avail_out - z->avail_out);
269 *consumed += z->total_in - last_total_in;
270 } while (ret == Z_OK && z->avail_out > 0);
272 if (ret == Z_OK || ret == Z_BUF_ERROR) {
273 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
275 if (ret != Z_STREAM_END)
276 return got_error(GOT_ERR_DECOMPRESSION);
277 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
280 *outlenp = z->total_out - last_total_out;
285 got_inflate_end(struct got_inflate_buf *zb)
288 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
293 const struct got_error *
294 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
295 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
297 const struct got_error *err;
298 size_t avail, consumed;
299 struct got_inflate_buf zb;
304 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
306 return got_error_from_errno("malloc");
307 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
309 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
318 err = got_inflate_read(&zb, f, &avail, &consumed);
323 *consumed_total += consumed;
324 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
327 newbuf = reallocarray(*outbuf, ++nbuf,
328 GOT_INFLATE_BUFSIZE);
329 if (newbuf == NULL) {
330 err = got_error_from_errno("reallocarray");
337 zb.outbuf = newbuf + *outlen;
338 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
340 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
343 got_inflate_end(&zb);
347 const struct got_error *
348 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
349 size_t *consumed_total, struct got_inflate_checksum *csum,
350 size_t expected_size, int infd)
352 const struct got_error *err;
353 size_t avail, consumed;
354 struct got_inflate_buf zb;
357 size_t bufsize = GOT_INFLATE_BUFSIZE;
359 /* Optimize buffer size in case short reads should suffice. */
360 if (expected_size > 0 && expected_size < bufsize)
361 bufsize = expected_size;
364 *outbuf = malloc(bufsize);
366 return got_error_from_errno("malloc");
367 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
369 err = got_inflate_init(&zb, NULL, bufsize, csum);
378 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
383 *consumed_total += consumed;
384 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
387 newbuf = reallocarray(*outbuf, ++nbuf,
388 GOT_INFLATE_BUFSIZE);
389 if (newbuf == NULL) {
390 err = got_error_from_errno("reallocarray");
397 zb.outbuf = newbuf + *outlen;
398 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
400 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
403 got_inflate_end(&zb);
407 const struct got_error *
408 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
409 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
410 size_t offset, size_t len)
412 const struct got_error *err;
413 size_t avail, consumed;
414 struct got_inflate_buf zb;
419 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
421 return got_error_from_errno("malloc");
422 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
429 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
438 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
444 *consumed_total += consumed;
449 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
452 newbuf = reallocarray(*outbuf, ++nbuf,
453 GOT_INFLATE_BUFSIZE);
454 if (newbuf == NULL) {
455 err = got_error_from_errno("reallocarray");
462 zb.outbuf = newbuf + *outlen;
463 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
465 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
467 got_inflate_end(&zb);
471 const struct got_error *
472 got_inflate_to_fd(size_t *outlen, FILE *infile,
473 struct got_inflate_checksum *csum, int outfd)
475 const struct got_error *err = NULL;
477 struct got_inflate_buf zb;
479 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
486 err = got_inflate_read(&zb, infile, &avail, NULL);
491 n = write(outfd, zb.outbuf, avail);
493 err = got_error_from_errno("write");
498 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
502 if (lseek(outfd, SEEK_SET, 0) == -1)
503 err = got_error_from_errno("lseek");
505 got_inflate_end(&zb);
509 const struct got_error *
510 got_inflate_to_file(size_t *outlen, FILE *infile,
511 struct got_inflate_checksum *csum, FILE *outfile)
513 const struct got_error *err;
515 struct got_inflate_buf zb;
517 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
524 err = got_inflate_read(&zb, infile, &avail, NULL);
529 n = fwrite(zb.outbuf, avail, 1, outfile);
531 err = got_ferror(outfile, GOT_ERR_IO);
536 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
541 got_inflate_end(&zb);
545 const struct got_error *
546 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
547 struct got_inflate_checksum *csum, int infd, FILE *outfile)
549 const struct got_error *err;
550 size_t avail, consumed;
551 struct got_inflate_buf zb;
553 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
561 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
566 n = fwrite(zb.outbuf, avail, 1, outfile);
568 err = got_ferror(outfile, GOT_ERR_IO);
573 *consumed_total += consumed;
575 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
580 got_inflate_end(&zb);
584 const struct got_error *
585 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
586 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
587 size_t len, FILE *outfile)
589 const struct got_error *err;
590 size_t avail, consumed;
591 struct got_inflate_buf zb;
593 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
601 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
607 *consumed_total += consumed;
611 n = fwrite(zb.outbuf, avail, 1, outfile);
613 err = got_ferror(outfile, GOT_ERR_IO);
618 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
623 got_inflate_end(&zb);