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)
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;
83 const struct got_error *
84 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
87 size_t last_total_out = zb->z.total_out;
88 size_t last_total_in = zb->z.total_in;
92 z->next_out = zb->outbuf;
93 z->avail_out = zb->outlen;
99 if (z->avail_in == 0) {
100 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
103 return got_ferror(f, GOT_ERR_IO);
108 z->next_in = zb->inbuf;
111 ret = inflate(z, Z_SYNC_FLUSH);
112 } while (ret == Z_OK && z->avail_out > 0);
114 if (ret == Z_OK || ret == Z_BUF_ERROR) {
115 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
117 if (ret != Z_STREAM_END)
118 return got_error(GOT_ERR_DECOMPRESSION);
119 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
122 *outlenp = z->total_out - last_total_out;
124 *consumed += z->total_in - last_total_in;
128 const struct got_error *
129 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp)
131 size_t last_total_out = zb->z.total_out;
132 z_stream *z = &zb->z;
135 z->next_out = zb->outbuf;
136 z->avail_out = zb->outlen;
140 if (z->avail_in == 0) {
141 ssize_t n = read(fd, zb->inbuf, zb->inlen);
143 return got_error_from_errno("read");
149 z->next_in = zb->inbuf;
152 ret = inflate(z, Z_SYNC_FLUSH);
153 } while (ret == Z_OK && z->avail_out > 0);
155 if (ret == Z_OK || ret == Z_BUF_ERROR) {
156 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
158 if (ret != Z_STREAM_END)
159 return got_error(GOT_ERR_DECOMPRESSION);
160 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
163 *outlenp = z->total_out - last_total_out;
167 const struct got_error *
168 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
169 size_t len, size_t *outlenp, size_t *consumed)
171 size_t last_total_out = zb->z.total_out;
172 z_stream *z = &zb->z;
175 z->next_out = zb->outbuf;
176 z->avail_out = zb->outlen;
182 size_t last_total_in = zb->z.total_in;
183 if (z->avail_in == 0) {
189 z->next_in = map + offset + *consumed;
190 z->avail_in = len - *consumed;
192 ret = inflate(z, Z_SYNC_FLUSH);
193 *consumed += z->total_in - last_total_in;
194 } while (ret == Z_OK && z->avail_out > 0);
196 if (ret == Z_OK || ret == Z_BUF_ERROR) {
197 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
199 if (ret != Z_STREAM_END)
200 return got_error(GOT_ERR_DECOMPRESSION);
201 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
204 *outlenp = z->total_out - last_total_out;
209 got_inflate_end(struct got_inflate_buf *zb)
212 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
217 const struct got_error *
218 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
219 size_t *consumed_total, FILE *f)
221 const struct got_error *err;
222 size_t avail, consumed;
223 struct got_inflate_buf zb;
227 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
229 return got_error_from_errno("malloc");
230 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
239 err = got_inflate_read(&zb, f, &avail, &consumed);
244 *consumed_total += consumed;
245 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
246 newbuf = reallocarray(*outbuf, ++nbuf,
247 GOT_INFLATE_BUFSIZE);
248 if (newbuf == NULL) {
249 err = got_error_from_errno("reallocarray");
256 zb.outbuf = newbuf + *outlen;
257 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
259 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
262 got_inflate_end(&zb);
266 const struct got_error *
267 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
269 const struct got_error *err;
271 struct got_inflate_buf zb;
275 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
277 return got_error_from_errno("malloc");
278 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
285 err = got_inflate_read_fd(&zb, infd, &avail);
289 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
290 newbuf = reallocarray(*outbuf, ++nbuf,
291 GOT_INFLATE_BUFSIZE);
292 if (newbuf == NULL) {
293 err = got_error_from_errno("reallocarray");
300 zb.outbuf = newbuf + *outlen;
301 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
303 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
306 got_inflate_end(&zb);
310 const struct got_error *
311 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
312 size_t offset, size_t len)
314 const struct got_error *err;
315 size_t avail, consumed;
316 struct got_inflate_buf zb;
320 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
322 return got_error_from_errno("malloc");
323 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
333 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
342 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
343 newbuf = reallocarray(*outbuf, ++nbuf,
344 GOT_INFLATE_BUFSIZE);
345 if (newbuf == NULL) {
346 err = got_error_from_errno("reallocarray");
353 zb.outbuf = newbuf + *outlen;
354 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
356 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
358 got_inflate_end(&zb);
362 const struct got_error *
363 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
365 const struct got_error *err = NULL;
367 struct got_inflate_buf zb;
369 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
376 err = got_inflate_read(&zb, infile, &avail, NULL);
381 n = write(outfd, zb.outbuf, avail);
383 err = got_error_from_errno("write");
388 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
392 if (lseek(outfd, SEEK_SET, 0) == -1)
393 err = got_error_from_errno("lseek");
395 got_inflate_end(&zb);
399 const struct got_error *
400 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
402 const struct got_error *err;
404 struct got_inflate_buf zb;
406 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
413 err = got_inflate_read(&zb, infile, &avail, NULL);
418 n = fwrite(zb.outbuf, avail, 1, outfile);
420 err = got_ferror(outfile, GOT_ERR_IO);
425 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
430 got_inflate_end(&zb);
434 const struct got_error *
435 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
437 const struct got_error *err;
439 struct got_inflate_buf zb;
441 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
448 err = got_inflate_read_fd(&zb, infd, &avail);
453 n = fwrite(zb.outbuf, avail, 1, outfile);
455 err = got_ferror(outfile, GOT_ERR_IO);
460 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
465 got_inflate_end(&zb);
469 const struct got_error *
470 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
471 size_t len, FILE *outfile)
473 const struct got_error *err;
475 struct got_inflate_buf zb;
478 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
485 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
493 n = fwrite(zb.outbuf, avail, 1, outfile);
495 err = got_ferror(outfile, GOT_ERR_IO);
500 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
505 got_inflate_end(&zb);