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) {
290 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, int infd)
316 const struct got_error *err;
317 size_t avail, consumed;
318 struct got_inflate_buf zb;
323 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
325 return got_error_from_errno("malloc");
326 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
329 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE,
339 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
344 *consumed_total += consumed;
345 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
346 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
349 newbuf = reallocarray(*outbuf, ++nbuf,
350 GOT_INFLATE_BUFSIZE);
351 if (newbuf == NULL) {
352 err = got_error_from_errno("reallocarray");
359 zb.outbuf = newbuf + *outlen;
361 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
364 got_inflate_end(&zb);
368 const struct got_error *
369 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
370 size_t offset, size_t len)
372 const struct got_error *err;
373 size_t avail, consumed;
374 struct got_inflate_buf zb;
378 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
380 return got_error_from_errno("malloc");
381 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
391 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
400 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
401 newbuf = reallocarray(*outbuf, ++nbuf,
402 GOT_INFLATE_BUFSIZE);
403 if (newbuf == NULL) {
404 err = got_error_from_errno("reallocarray");
411 zb.outbuf = newbuf + *outlen;
412 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
414 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
416 got_inflate_end(&zb);
420 const struct got_error *
421 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
423 const struct got_error *err = NULL;
425 struct got_inflate_buf zb;
427 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
434 err = got_inflate_read(&zb, infile, &avail, NULL);
439 n = write(outfd, zb.outbuf, avail);
441 err = got_error_from_errno("write");
446 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
450 if (lseek(outfd, SEEK_SET, 0) == -1)
451 err = got_error_from_errno("lseek");
453 got_inflate_end(&zb);
457 const struct got_error *
458 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
460 const struct got_error *err;
462 struct got_inflate_buf zb;
464 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
471 err = got_inflate_read(&zb, infile, &avail, NULL);
476 n = fwrite(zb.outbuf, avail, 1, outfile);
478 err = got_ferror(outfile, GOT_ERR_IO);
483 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
488 got_inflate_end(&zb);
492 const struct got_error *
493 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
495 const struct got_error *err;
497 struct got_inflate_buf zb;
499 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
506 err = got_inflate_read_fd(&zb, infd, &avail, NULL);
511 n = fwrite(zb.outbuf, avail, 1, outfile);
513 err = got_ferror(outfile, GOT_ERR_IO);
518 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
523 got_inflate_end(&zb);
527 const struct got_error *
528 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
529 size_t len, FILE *outfile)
531 const struct got_error *err;
533 struct got_inflate_buf zb;
536 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
543 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
551 n = fwrite(zb.outbuf, avail, 1, outfile);
553 err = got_ferror(outfile, GOT_ERR_IO);
558 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
563 got_inflate_end(&zb);