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 char *buf, size_t len)
89 *csum->input_crc = crc32(*csum->input_crc, buf, len);
92 SHA1Update(csum->input_sha1, buf, len);
95 const struct got_error *
96 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
99 size_t last_total_out = zb->z.total_out;
100 size_t last_total_in = zb->z.total_in;
101 z_stream *z = &zb->z;
104 z->next_out = zb->outbuf;
105 z->avail_out = zb->outlen;
111 char *csum_in = NULL;
112 size_t csum_avail = 0;
114 if (z->avail_in == 0) {
115 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
118 return got_ferror(f, GOT_ERR_IO);
123 z->next_in = zb->inbuf;
127 csum_in = z->next_in;
128 csum_avail = z->avail_in;
130 ret = inflate(z, Z_SYNC_FLUSH);
132 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
133 } while (ret == Z_OK && z->avail_out > 0);
135 if (ret == Z_OK || ret == Z_BUF_ERROR) {
136 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
138 if (ret != Z_STREAM_END)
139 return got_error(GOT_ERR_DECOMPRESSION);
140 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
143 *outlenp = z->total_out - last_total_out;
145 *consumed += z->total_in - last_total_in;
149 const struct got_error *
150 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
153 size_t last_total_out = zb->z.total_out;
154 size_t last_total_in = zb->z.total_in;
155 z_stream *z = &zb->z;
158 z->next_out = zb->outbuf;
159 z->avail_out = zb->outlen;
165 char *csum_in = NULL;
166 size_t csum_avail = 0;
168 if (z->avail_in == 0) {
169 ssize_t n = read(fd, zb->inbuf, zb->inlen);
171 return got_error_from_errno("read");
177 z->next_in = zb->inbuf;
181 csum_in = z->next_in;
182 csum_avail = z->avail_in;
184 ret = inflate(z, Z_SYNC_FLUSH);
186 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
187 } while (ret == Z_OK && z->avail_out > 0);
189 if (ret == Z_OK || ret == Z_BUF_ERROR) {
190 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
192 if (ret != Z_STREAM_END)
193 return got_error(GOT_ERR_DECOMPRESSION);
194 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
197 *outlenp = z->total_out - last_total_out;
199 *consumed += z->total_in - last_total_in;
203 const struct got_error *
204 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
205 size_t len, size_t *outlenp, size_t *consumed)
207 size_t last_total_out = zb->z.total_out;
208 z_stream *z = &zb->z;
211 z->next_out = zb->outbuf;
212 z->avail_out = zb->outlen;
218 char *csum_in = NULL;
219 size_t csum_avail = 0;
220 size_t last_total_in = zb->z.total_in;
222 if (z->avail_in == 0) {
228 z->next_in = map + offset + *consumed;
229 z->avail_in = len - *consumed;
232 csum_in = z->next_in;
233 csum_avail = z->avail_in;
235 ret = inflate(z, Z_SYNC_FLUSH);
237 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
238 *consumed += z->total_in - last_total_in;
239 } while (ret == Z_OK && z->avail_out > 0);
241 if (ret == Z_OK || ret == Z_BUF_ERROR) {
242 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
244 if (ret != Z_STREAM_END)
245 return got_error(GOT_ERR_DECOMPRESSION);
246 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
249 *outlenp = z->total_out - last_total_out;
254 got_inflate_end(struct got_inflate_buf *zb)
257 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
262 const struct got_error *
263 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
264 size_t *consumed_total, FILE *f)
266 const struct got_error *err;
267 size_t avail, consumed;
268 struct got_inflate_buf zb;
273 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
275 return got_error_from_errno("malloc");
276 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
278 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
287 err = got_inflate_read(&zb, f, &avail, &consumed);
292 *consumed_total += consumed;
293 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
296 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
297 newbuf = reallocarray(*outbuf, ++nbuf,
298 GOT_INFLATE_BUFSIZE);
299 if (newbuf == NULL) {
300 err = got_error_from_errno("reallocarray");
307 zb.outbuf = newbuf + *outlen;
309 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
312 got_inflate_end(&zb);
316 const struct got_error *
317 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
318 size_t *consumed_total, struct got_inflate_checksum *csum,
319 size_t expected_size, int infd)
321 const struct got_error *err;
322 size_t avail, consumed;
323 struct got_inflate_buf zb;
326 size_t bufsize = GOT_INFLATE_BUFSIZE;
328 /* Optimize buffer size in case short reads should suffice. */
329 if (expected_size > 0 && expected_size < bufsize)
330 bufsize = expected_size;
333 *outbuf = malloc(bufsize);
335 return got_error_from_errno("malloc");
336 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
338 err = got_inflate_init(&zb, NULL, bufsize, csum);
347 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
352 *consumed_total += consumed;
353 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
356 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
357 newbuf = reallocarray(*outbuf, ++nbuf,
358 GOT_INFLATE_BUFSIZE);
359 if (newbuf == NULL) {
360 err = got_error_from_errno("reallocarray");
367 zb.outbuf = newbuf + *outlen;
369 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
372 got_inflate_end(&zb);
376 const struct got_error *
377 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
378 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
379 size_t offset, size_t len)
381 const struct got_error *err;
382 size_t avail, consumed;
383 struct got_inflate_buf zb;
388 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
390 return got_error_from_errno("malloc");
391 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
398 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
405 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
411 *consumed_total += consumed;
416 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
419 newbuf = reallocarray(*outbuf, ++nbuf,
420 GOT_INFLATE_BUFSIZE);
421 if (newbuf == NULL) {
422 err = got_error_from_errno("reallocarray");
429 zb.outbuf = newbuf + *outlen;
430 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
432 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
434 got_inflate_end(&zb);
438 const struct got_error *
439 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
441 const struct got_error *err = NULL;
443 struct got_inflate_buf zb;
445 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
452 err = got_inflate_read(&zb, infile, &avail, NULL);
457 n = write(outfd, zb.outbuf, avail);
459 err = got_error_from_errno("write");
464 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
468 if (lseek(outfd, SEEK_SET, 0) == -1)
469 err = got_error_from_errno("lseek");
471 got_inflate_end(&zb);
475 const struct got_error *
476 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
478 const struct got_error *err;
480 struct got_inflate_buf zb;
482 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
489 err = got_inflate_read(&zb, infile, &avail, NULL);
494 n = fwrite(zb.outbuf, avail, 1, outfile);
496 err = got_ferror(outfile, GOT_ERR_IO);
501 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
506 got_inflate_end(&zb);
510 const struct got_error *
511 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
512 struct got_inflate_checksum *csum, int infd, FILE *outfile)
514 const struct got_error *err;
515 size_t avail, consumed;
516 struct got_inflate_buf zb;
518 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
526 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
531 n = fwrite(zb.outbuf, avail, 1, outfile);
533 err = got_ferror(outfile, GOT_ERR_IO);
538 *consumed_total += consumed;
540 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
545 got_inflate_end(&zb);
549 const struct got_error *
550 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
551 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
552 size_t len, FILE *outfile)
554 const struct got_error *err;
555 size_t avail, consumed;
556 struct got_inflate_buf zb;
558 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
566 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
572 *consumed_total += consumed;
576 n = fwrite(zb.outbuf, avail, 1, outfile);
578 err = got_ferror(outfile, GOT_ERR_IO);
583 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
588 got_inflate_end(&zb);