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 if (len - *consumed > UINT_MAX)
253 z->avail_in = UINT_MAX;
255 z->avail_in = len - *consumed;
258 csum_in = z->next_in;
259 csum_avail_in = z->avail_in;
260 csum_out = z->next_out;
261 csum_avail_out = z->avail_out;
263 ret = inflate(z, Z_SYNC_FLUSH);
265 csum_input(zb->csum, csum_in,
266 csum_avail_in - z->avail_in);
267 csum_output(zb->csum, csum_out,
268 csum_avail_out - z->avail_out);
270 *consumed += z->total_in - last_total_in;
271 } while (ret == Z_OK && z->avail_out > 0);
273 if (ret == Z_OK || ret == Z_BUF_ERROR) {
274 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
276 if (ret != Z_STREAM_END)
277 return got_error(GOT_ERR_DECOMPRESSION);
278 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
281 *outlenp = z->total_out - last_total_out;
286 got_inflate_end(struct got_inflate_buf *zb)
289 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
294 const struct got_error *
295 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
296 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
298 const struct got_error *err;
299 size_t avail, consumed;
300 struct got_inflate_buf zb;
305 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
307 return got_error_from_errno("malloc");
308 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
310 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
319 err = got_inflate_read(&zb, f, &avail, &consumed);
324 *consumed_total += consumed;
325 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
328 newbuf = reallocarray(*outbuf, ++nbuf,
329 GOT_INFLATE_BUFSIZE);
330 if (newbuf == NULL) {
331 err = got_error_from_errno("reallocarray");
338 zb.outbuf = newbuf + *outlen;
339 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
341 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
344 got_inflate_end(&zb);
348 const struct got_error *
349 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
350 size_t *consumed_total, struct got_inflate_checksum *csum,
351 size_t expected_size, int infd)
353 const struct got_error *err;
354 size_t avail, consumed;
355 struct got_inflate_buf zb;
358 size_t bufsize = GOT_INFLATE_BUFSIZE;
360 /* Optimize buffer size in case short reads should suffice. */
361 if (expected_size > 0 && expected_size < bufsize)
362 bufsize = expected_size;
365 *outbuf = malloc(bufsize);
367 return got_error_from_errno("malloc");
368 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
370 err = got_inflate_init(&zb, NULL, bufsize, csum);
379 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
384 *consumed_total += consumed;
385 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
388 newbuf = reallocarray(*outbuf, ++nbuf,
389 GOT_INFLATE_BUFSIZE);
390 if (newbuf == NULL) {
391 err = got_error_from_errno("reallocarray");
398 zb.outbuf = newbuf + *outlen;
399 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
401 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
404 got_inflate_end(&zb);
408 const struct got_error *
409 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
410 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
411 size_t offset, size_t len)
413 const struct got_error *err;
414 size_t avail, consumed;
415 struct got_inflate_buf zb;
420 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
422 return got_error_from_errno("malloc");
423 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
430 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
439 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
445 *consumed_total += consumed;
450 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
453 newbuf = reallocarray(*outbuf, ++nbuf,
454 GOT_INFLATE_BUFSIZE);
455 if (newbuf == NULL) {
456 err = got_error_from_errno("reallocarray");
463 zb.outbuf = newbuf + *outlen;
464 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
466 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
468 got_inflate_end(&zb);
472 const struct got_error *
473 got_inflate_to_fd(size_t *outlen, FILE *infile,
474 struct got_inflate_checksum *csum, int outfd)
476 const struct got_error *err = NULL;
478 struct got_inflate_buf zb;
480 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
487 err = got_inflate_read(&zb, infile, &avail, NULL);
492 n = write(outfd, zb.outbuf, avail);
494 err = got_error_from_errno("write");
499 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
503 if (lseek(outfd, SEEK_SET, 0) == -1)
504 err = got_error_from_errno("lseek");
506 got_inflate_end(&zb);
510 const struct got_error *
511 got_inflate_to_file(size_t *outlen, FILE *infile,
512 struct got_inflate_checksum *csum, FILE *outfile)
514 const struct got_error *err;
516 struct got_inflate_buf zb;
518 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
525 err = got_inflate_read(&zb, infile, &avail, NULL);
530 n = fwrite(zb.outbuf, avail, 1, outfile);
532 err = got_ferror(outfile, GOT_ERR_IO);
537 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
542 got_inflate_end(&zb);
546 const struct got_error *
547 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
548 struct got_inflate_checksum *csum, int infd, FILE *outfile)
550 const struct got_error *err;
551 size_t avail, consumed;
552 struct got_inflate_buf zb;
554 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
562 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
567 n = fwrite(zb.outbuf, avail, 1, outfile);
569 err = got_ferror(outfile, GOT_ERR_IO);
574 *consumed_total += consumed;
576 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
581 got_inflate_end(&zb);
585 const struct got_error *
586 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
587 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
588 size_t len, FILE *outfile)
590 const struct got_error *err;
591 size_t avail, consumed;
592 struct got_inflate_buf zb;
594 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
602 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
608 *consumed_total += consumed;
612 n = fwrite(zb.outbuf, avail, 1, outfile);
614 err = got_ferror(outfile, GOT_ERR_IO);
619 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
624 got_inflate_end(&zb);