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,
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;
78 zb->input_crc = input_crc;
85 const struct got_error *
86 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
89 size_t last_total_out = zb->z.total_out;
90 size_t last_total_in = zb->z.total_in;
94 z->next_out = zb->outbuf;
95 z->avail_out = zb->outlen;
102 size_t crc_avail = 0;
104 if (z->avail_in == 0) {
105 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
108 return got_ferror(f, GOT_ERR_IO);
113 z->next_in = zb->inbuf;
118 crc_avail = z->avail_in;
120 ret = inflate(z, Z_SYNC_FLUSH);
122 *zb->input_crc = crc32(*zb->input_crc,
123 crc_in, crc_avail - z->avail_in);
125 } while (ret == Z_OK && z->avail_out > 0);
127 if (ret == Z_OK || ret == Z_BUF_ERROR) {
128 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
130 if (ret != Z_STREAM_END)
131 return got_error(GOT_ERR_DECOMPRESSION);
132 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
135 *outlenp = z->total_out - last_total_out;
137 *consumed += z->total_in - last_total_in;
141 const struct got_error *
142 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
145 size_t last_total_out = zb->z.total_out;
146 size_t last_total_in = zb->z.total_in;
147 z_stream *z = &zb->z;
150 z->next_out = zb->outbuf;
151 z->avail_out = zb->outlen;
158 size_t crc_avail = 0;
160 if (z->avail_in == 0) {
161 ssize_t n = read(fd, zb->inbuf, zb->inlen);
163 return got_error_from_errno("read");
169 z->next_in = zb->inbuf;
174 crc_avail = z->avail_in;
176 ret = inflate(z, Z_SYNC_FLUSH);
178 *zb->input_crc = crc32(*zb->input_crc,
179 crc_in, crc_avail - z->avail_in);
181 } while (ret == Z_OK && z->avail_out > 0);
183 if (ret == Z_OK || ret == Z_BUF_ERROR) {
184 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
186 if (ret != Z_STREAM_END)
187 return got_error(GOT_ERR_DECOMPRESSION);
188 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
191 *outlenp = z->total_out - last_total_out;
193 *consumed += z->total_in - last_total_in;
197 const struct got_error *
198 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
199 size_t len, size_t *outlenp, size_t *consumed)
201 size_t last_total_out = zb->z.total_out;
202 z_stream *z = &zb->z;
205 z->next_out = zb->outbuf;
206 z->avail_out = zb->outlen;
213 size_t crc_avail = 0;
214 size_t last_total_in = zb->z.total_in;
216 if (z->avail_in == 0) {
222 z->next_in = map + offset + *consumed;
223 z->avail_in = len - *consumed;
227 crc_avail = z->avail_in;
229 ret = inflate(z, Z_SYNC_FLUSH);
231 *zb->input_crc = crc32(*zb->input_crc,
232 crc_in, crc_avail - z->avail_in);
234 *consumed += z->total_in - last_total_in;
235 } while (ret == Z_OK && z->avail_out > 0);
237 if (ret == Z_OK || ret == Z_BUF_ERROR) {
238 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
240 if (ret != Z_STREAM_END)
241 return got_error(GOT_ERR_DECOMPRESSION);
242 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
245 *outlenp = z->total_out - last_total_out;
250 got_inflate_end(struct got_inflate_buf *zb)
253 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
258 const struct got_error *
259 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
260 size_t *consumed_total, FILE *f)
262 const struct got_error *err;
263 size_t avail, consumed;
264 struct got_inflate_buf zb;
269 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
271 return got_error_from_errno("malloc");
272 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
274 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
283 err = got_inflate_read(&zb, f, &avail, &consumed);
288 *consumed_total += consumed;
289 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
292 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
293 newbuf = reallocarray(*outbuf, ++nbuf,
294 GOT_INFLATE_BUFSIZE);
295 if (newbuf == NULL) {
296 err = got_error_from_errno("reallocarray");
303 zb.outbuf = newbuf + *outlen;
305 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
308 got_inflate_end(&zb);
312 const struct got_error *
313 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
314 size_t *consumed_total, uint32_t *input_crc, size_t expected_size, int infd)
316 const struct got_error *err;
317 size_t avail, consumed;
318 struct got_inflate_buf zb;
321 size_t bufsize = GOT_INFLATE_BUFSIZE;
323 /* Optimize buffer size in case short reads should suffice. */
324 if (expected_size > 0 && expected_size < bufsize)
325 bufsize = expected_size;
328 *outbuf = malloc(bufsize);
330 return got_error_from_errno("malloc");
331 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
334 err = got_inflate_init(&zb, NULL, bufsize, input_crc);
343 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
348 *consumed_total += consumed;
349 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
352 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
353 newbuf = reallocarray(*outbuf, ++nbuf,
354 GOT_INFLATE_BUFSIZE);
355 if (newbuf == NULL) {
356 err = got_error_from_errno("reallocarray");
363 zb.outbuf = newbuf + *outlen;
365 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
368 got_inflate_end(&zb);
372 const struct got_error *
373 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
374 size_t *consumed_total, uint32_t *input_crc, uint8_t *map, size_t offset,
377 const struct got_error *err;
378 size_t avail, consumed;
379 struct got_inflate_buf zb;
384 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
386 return got_error_from_errno("malloc");
387 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
395 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE,
403 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
409 *consumed_total += consumed;
414 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
417 newbuf = reallocarray(*outbuf, ++nbuf,
418 GOT_INFLATE_BUFSIZE);
419 if (newbuf == NULL) {
420 err = got_error_from_errno("reallocarray");
427 zb.outbuf = newbuf + *outlen;
428 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
430 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
432 got_inflate_end(&zb);
436 const struct got_error *
437 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
439 const struct got_error *err = NULL;
441 struct got_inflate_buf zb;
443 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
450 err = got_inflate_read(&zb, infile, &avail, NULL);
455 n = write(outfd, zb.outbuf, avail);
457 err = got_error_from_errno("write");
462 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
466 if (lseek(outfd, SEEK_SET, 0) == -1)
467 err = got_error_from_errno("lseek");
469 got_inflate_end(&zb);
473 const struct got_error *
474 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
476 const struct got_error *err;
478 struct got_inflate_buf zb;
480 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
487 err = got_inflate_read(&zb, infile, &avail, NULL);
492 n = fwrite(zb.outbuf, avail, 1, outfile);
494 err = got_ferror(outfile, GOT_ERR_IO);
499 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
504 got_inflate_end(&zb);
508 const struct got_error *
509 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
510 uint32_t *input_crc, int infd, FILE *outfile)
512 const struct got_error *err;
513 size_t avail, consumed;
514 struct got_inflate_buf zb;
516 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, input_crc);
524 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
529 n = fwrite(zb.outbuf, avail, 1, outfile);
531 err = got_ferror(outfile, GOT_ERR_IO);
536 *consumed_total += consumed;
538 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
543 got_inflate_end(&zb);
547 const struct got_error *
548 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
549 uint32_t *input_crc, uint8_t *map, size_t offset, size_t len,
552 const struct got_error *err;
553 size_t avail, consumed;
554 struct got_inflate_buf zb;
556 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, input_crc);
564 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
570 *consumed_total += consumed;
574 n = fwrite(zb.outbuf, avail, 1, outfile);
576 err = got_ferror(outfile, GOT_ERR_IO);
581 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
586 got_inflate_end(&zb);