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.
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);
96 csum_output(struct got_inflate_checksum *csum, const char *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 char *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 char *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 char *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 z->avail_in = len - *consumed;
254 csum_in = z->next_in;
255 csum_avail_in = z->avail_in;
256 csum_out = z->next_out;
257 csum_avail_out = z->avail_out;
259 ret = inflate(z, Z_SYNC_FLUSH);
261 csum_input(zb->csum, csum_in,
262 csum_avail_in - z->avail_in);
263 csum_output(zb->csum, csum_out,
264 csum_avail_out - z->avail_out);
266 *consumed += z->total_in - last_total_in;
267 } while (ret == Z_OK && z->avail_out > 0);
269 if (ret == Z_OK || ret == Z_BUF_ERROR) {
270 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
272 if (ret != Z_STREAM_END)
273 return got_error(GOT_ERR_DECOMPRESSION);
274 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
277 *outlenp = z->total_out - last_total_out;
282 got_inflate_end(struct got_inflate_buf *zb)
285 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
290 const struct got_error *
291 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
292 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
294 const struct got_error *err;
295 size_t avail, consumed;
296 struct got_inflate_buf zb;
301 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
303 return got_error_from_errno("malloc");
304 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
306 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
315 err = got_inflate_read(&zb, f, &avail, &consumed);
320 *consumed_total += consumed;
321 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
324 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
325 newbuf = reallocarray(*outbuf, ++nbuf,
326 GOT_INFLATE_BUFSIZE);
327 if (newbuf == NULL) {
328 err = got_error_from_errno("reallocarray");
335 zb.outbuf = newbuf + *outlen;
337 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
340 got_inflate_end(&zb);
344 const struct got_error *
345 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
346 size_t *consumed_total, struct got_inflate_checksum *csum,
347 size_t expected_size, int infd)
349 const struct got_error *err;
350 size_t avail, consumed;
351 struct got_inflate_buf zb;
354 size_t bufsize = GOT_INFLATE_BUFSIZE;
356 /* Optimize buffer size in case short reads should suffice. */
357 if (expected_size > 0 && expected_size < bufsize)
358 bufsize = expected_size;
361 *outbuf = malloc(bufsize);
363 return got_error_from_errno("malloc");
364 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
366 err = got_inflate_init(&zb, NULL, bufsize, csum);
375 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
380 *consumed_total += consumed;
381 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
384 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
385 newbuf = reallocarray(*outbuf, ++nbuf,
386 GOT_INFLATE_BUFSIZE);
387 if (newbuf == NULL) {
388 err = got_error_from_errno("reallocarray");
395 zb.outbuf = newbuf + *outlen;
397 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
400 got_inflate_end(&zb);
404 const struct got_error *
405 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
406 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
407 size_t offset, size_t len)
409 const struct got_error *err;
410 size_t avail, consumed;
411 struct got_inflate_buf zb;
416 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
418 return got_error_from_errno("malloc");
419 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
426 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
433 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
439 *consumed_total += consumed;
444 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
447 newbuf = reallocarray(*outbuf, ++nbuf,
448 GOT_INFLATE_BUFSIZE);
449 if (newbuf == NULL) {
450 err = got_error_from_errno("reallocarray");
457 zb.outbuf = newbuf + *outlen;
458 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
460 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
462 got_inflate_end(&zb);
466 const struct got_error *
467 got_inflate_to_fd(size_t *outlen, FILE *infile,
468 struct got_inflate_checksum *csum, int outfd)
470 const struct got_error *err = NULL;
472 struct got_inflate_buf zb;
474 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
481 err = got_inflate_read(&zb, infile, &avail, NULL);
486 n = write(outfd, zb.outbuf, avail);
488 err = got_error_from_errno("write");
493 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
497 if (lseek(outfd, SEEK_SET, 0) == -1)
498 err = got_error_from_errno("lseek");
500 got_inflate_end(&zb);
504 const struct got_error *
505 got_inflate_to_file(size_t *outlen, FILE *infile,
506 struct got_inflate_checksum *csum, FILE *outfile)
508 const struct got_error *err;
510 struct got_inflate_buf zb;
512 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
519 err = got_inflate_read(&zb, infile, &avail, NULL);
524 n = fwrite(zb.outbuf, avail, 1, outfile);
526 err = got_ferror(outfile, GOT_ERR_IO);
531 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
536 got_inflate_end(&zb);
540 const struct got_error *
541 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
542 struct got_inflate_checksum *csum, int infd, FILE *outfile)
544 const struct got_error *err;
545 size_t avail, consumed;
546 struct got_inflate_buf zb;
548 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
556 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
561 n = fwrite(zb.outbuf, avail, 1, outfile);
563 err = got_ferror(outfile, GOT_ERR_IO);
568 *consumed_total += consumed;
570 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
575 got_inflate_end(&zb);
579 const struct got_error *
580 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
581 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
582 size_t len, FILE *outfile)
584 const struct got_error *err;
585 size_t avail, consumed;
586 struct got_inflate_buf zb;
588 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
596 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
602 *consumed_total += consumed;
606 n = fwrite(zb.outbuf, avail, 1, outfile);
608 err = got_ferror(outfile, GOT_ERR_IO);
613 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
618 got_inflate_end(&zb);