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.
26 #include "got_error.h"
27 #include "got_object.h"
30 #include "got_lib_inflate.h"
33 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
36 const struct got_error *
37 got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
38 struct got_inflate_checksum *csum)
40 const struct got_error *err = NULL;
43 memset(&zb->z, 0, sizeof(zb->z));
45 zb->z.zalloc = Z_NULL;
47 zerr = inflateInit(&zb->z);
50 return got_error_from_errno("inflateInit");
51 if (zerr == Z_MEM_ERROR) {
53 return got_error_from_errno("inflateInit");
55 return got_error(GOT_ERR_DECOMPRESSION);
58 zb->inlen = zb->outlen = bufsize;
60 zb->inbuf = calloc(1, zb->inlen);
61 if (zb->inbuf == NULL) {
62 err = got_error_from_errno("calloc");
68 zb->outbuf = calloc(1, zb->outlen);
69 if (zb->outbuf == NULL) {
70 err = got_error_from_errno("calloc");
73 zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
85 csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
88 *csum->input_crc = crc32(*csum->input_crc, buf, len);
91 SHA1Update(csum->input_sha1, buf, len);
95 csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
98 *csum->output_crc = crc32(*csum->output_crc, buf, len);
100 if (csum->output_sha1)
101 SHA1Update(csum->output_sha1, buf, len);
104 const struct got_error *
105 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
108 size_t last_total_out = zb->z.total_out;
109 size_t last_total_in = zb->z.total_in;
110 z_stream *z = &zb->z;
113 z->next_out = zb->outbuf;
114 z->avail_out = zb->outlen;
120 uint8_t *csum_in = NULL, *csum_out = NULL;
121 size_t csum_avail_in = 0, csum_avail_out = 0;
123 if (z->avail_in == 0) {
124 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
127 return got_ferror(f, GOT_ERR_IO);
132 z->next_in = zb->inbuf;
136 csum_in = z->next_in;
137 csum_avail_in = z->avail_in;
138 csum_out = z->next_out;
139 csum_avail_out = z->avail_out;
141 ret = inflate(z, Z_SYNC_FLUSH);
143 csum_input(zb->csum, csum_in,
144 csum_avail_in - z->avail_in);
145 csum_output(zb->csum, csum_out,
146 csum_avail_out - z->avail_out);
148 } while (ret == Z_OK && z->avail_out > 0);
150 if (ret == Z_OK || ret == Z_BUF_ERROR) {
151 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
153 if (ret != Z_STREAM_END)
154 return got_error(GOT_ERR_DECOMPRESSION);
155 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
158 *outlenp = z->total_out - last_total_out;
160 *consumed += z->total_in - last_total_in;
164 const struct got_error *
165 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
168 size_t last_total_out = zb->z.total_out;
169 size_t last_total_in = zb->z.total_in;
170 z_stream *z = &zb->z;
173 z->next_out = zb->outbuf;
174 z->avail_out = zb->outlen;
180 uint8_t *csum_in = NULL, *csum_out = NULL;
181 size_t csum_avail_in = 0, csum_avail_out = 0;
183 if (z->avail_in == 0) {
184 ssize_t n = read(fd, zb->inbuf, zb->inlen);
186 return got_error_from_errno("read");
192 z->next_in = zb->inbuf;
196 csum_in = z->next_in;
197 csum_avail_in = z->avail_in;
198 csum_out = z->next_out;
199 csum_avail_out = z->avail_out;
201 ret = inflate(z, Z_SYNC_FLUSH);
203 csum_input(zb->csum, csum_in,
204 csum_avail_in - z->avail_in);
205 csum_output(zb->csum, csum_out,
206 csum_avail_out - z->avail_out);
208 } while (ret == Z_OK && z->avail_out > 0);
210 if (ret == Z_OK || ret == Z_BUF_ERROR) {
211 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
213 if (ret != Z_STREAM_END)
214 return got_error(GOT_ERR_DECOMPRESSION);
215 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
218 *outlenp = z->total_out - last_total_out;
220 *consumed += z->total_in - last_total_in;
224 const struct got_error *
225 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
226 size_t len, size_t *outlenp, size_t *consumed)
228 size_t last_total_out = zb->z.total_out;
229 z_stream *z = &zb->z;
232 z->next_out = zb->outbuf;
233 z->avail_out = zb->outlen;
239 uint8_t *csum_in = NULL, *csum_out = NULL;
240 size_t csum_avail_in = 0, csum_avail_out = 0;
241 size_t last_total_in = zb->z.total_in;
243 if (z->avail_in == 0) {
249 z->next_in = map + offset + *consumed;
250 if (len - *consumed > UINT_MAX)
251 z->avail_in = UINT_MAX;
253 z->avail_in = len - *consumed;
256 csum_in = z->next_in;
257 csum_avail_in = z->avail_in;
258 csum_out = z->next_out;
259 csum_avail_out = z->avail_out;
261 ret = inflate(z, Z_SYNC_FLUSH);
263 csum_input(zb->csum, csum_in,
264 csum_avail_in - z->avail_in);
265 csum_output(zb->csum, csum_out,
266 csum_avail_out - z->avail_out);
268 *consumed += z->total_in - last_total_in;
269 } while (ret == Z_OK && z->avail_out > 0);
271 if (ret == Z_OK || ret == Z_BUF_ERROR) {
272 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
274 if (ret != Z_STREAM_END)
275 return got_error(GOT_ERR_DECOMPRESSION);
276 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
279 *outlenp = z->total_out - last_total_out;
284 got_inflate_end(struct got_inflate_buf *zb)
287 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
292 const struct got_error *
293 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
294 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
296 const struct got_error *err;
297 size_t avail, consumed;
298 struct got_inflate_buf zb;
303 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
305 return got_error_from_errno("malloc");
306 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
308 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
317 err = got_inflate_read(&zb, f, &avail, &consumed);
322 *consumed_total += consumed;
323 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
326 newbuf = reallocarray(*outbuf, ++nbuf,
327 GOT_INFLATE_BUFSIZE);
328 if (newbuf == NULL) {
329 err = got_error_from_errno("reallocarray");
336 zb.outbuf = newbuf + *outlen;
337 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
339 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
342 got_inflate_end(&zb);
346 const struct got_error *
347 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
348 size_t *consumed_total, struct got_inflate_checksum *csum,
349 size_t expected_size, int infd)
351 const struct got_error *err;
352 size_t avail, consumed;
353 struct got_inflate_buf zb;
356 size_t bufsize = GOT_INFLATE_BUFSIZE;
358 /* Optimize buffer size in case short reads should suffice. */
359 if (expected_size > 0 && expected_size < bufsize)
360 bufsize = expected_size;
363 *outbuf = malloc(bufsize);
365 return got_error_from_errno("malloc");
366 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
368 err = got_inflate_init(&zb, NULL, bufsize, csum);
377 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
382 *consumed_total += consumed;
383 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
386 newbuf = reallocarray(*outbuf, ++nbuf,
387 GOT_INFLATE_BUFSIZE);
388 if (newbuf == NULL) {
389 err = got_error_from_errno("reallocarray");
396 zb.outbuf = newbuf + *outlen;
397 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
399 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
402 got_inflate_end(&zb);
406 const struct got_error *
407 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
408 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
409 size_t offset, size_t len)
411 const struct got_error *err;
412 size_t avail, consumed;
413 struct got_inflate_buf zb;
418 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
420 return got_error_from_errno("malloc");
421 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
428 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
437 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
443 *consumed_total += consumed;
448 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
451 newbuf = reallocarray(*outbuf, ++nbuf,
452 GOT_INFLATE_BUFSIZE);
453 if (newbuf == NULL) {
454 err = got_error_from_errno("reallocarray");
461 zb.outbuf = newbuf + *outlen;
462 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
464 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
466 got_inflate_end(&zb);
470 const struct got_error *
471 got_inflate_to_fd(size_t *outlen, FILE *infile,
472 struct got_inflate_checksum *csum, int outfd)
474 const struct got_error *err = NULL;
476 struct got_inflate_buf zb;
478 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
485 err = got_inflate_read(&zb, infile, &avail, NULL);
490 n = write(outfd, zb.outbuf, avail);
492 err = got_error_from_errno("write");
497 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
501 if (lseek(outfd, SEEK_SET, 0) == -1)
502 err = got_error_from_errno("lseek");
504 got_inflate_end(&zb);
508 const struct got_error *
509 got_inflate_to_file(size_t *outlen, FILE *infile,
510 struct got_inflate_checksum *csum, FILE *outfile)
512 const struct got_error *err;
514 struct got_inflate_buf zb;
516 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
523 err = got_inflate_read(&zb, infile, &avail, NULL);
528 n = fwrite(zb.outbuf, avail, 1, outfile);
530 err = got_ferror(outfile, GOT_ERR_IO);
535 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
540 got_inflate_end(&zb);
544 const struct got_error *
545 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
546 struct got_inflate_checksum *csum, int infd, FILE *outfile)
548 const struct got_error *err;
549 size_t avail, consumed;
550 struct got_inflate_buf zb;
552 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
560 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
565 n = fwrite(zb.outbuf, avail, 1, outfile);
567 err = got_ferror(outfile, GOT_ERR_IO);
572 *consumed_total += consumed;
574 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
579 got_inflate_end(&zb);
583 const struct got_error *
584 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
585 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
586 size_t len, FILE *outfile)
588 const struct got_error *err;
589 size_t avail, consumed;
590 struct got_inflate_buf zb;
592 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
600 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
606 *consumed_total += consumed;
610 n = fwrite(zb.outbuf, avail, 1, outfile);
612 err = got_ferror(outfile, GOT_ERR_IO);
617 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
622 got_inflate_end(&zb);