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)
86 size_t last_total_out = zb->z.total_out;
91 z->next_out = zb->outbuf;
92 z->avail_out = zb->outlen;
98 size_t last_total_in = zb->z.total_in;
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 consumed += z->total_in - last_total_in;
113 } while (ret == Z_OK && z->avail_out > 0);
115 if (ret == Z_OK || ret == Z_BUF_ERROR) {
116 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
118 if (ret != Z_STREAM_END)
119 return got_error(GOT_ERR_DECOMPRESSION);
120 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
123 *outlenp = z->total_out - last_total_out;
124 fseek(f, off + consumed, SEEK_SET);
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, FILE *f)
220 const struct got_error *err;
222 struct got_inflate_buf zb;
226 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
228 return got_error_from_errno("malloc");
229 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
236 err = got_inflate_read(&zb, f, &avail);
240 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
241 newbuf = reallocarray(*outbuf, ++nbuf,
242 GOT_INFLATE_BUFSIZE);
243 if (newbuf == NULL) {
244 err = got_error_from_errno("reallocarray");
251 zb.outbuf = newbuf + *outlen;
252 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
254 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
257 got_inflate_end(&zb);
261 const struct got_error *
262 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
264 const struct got_error *err;
266 struct got_inflate_buf zb;
270 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
272 return got_error_from_errno("malloc");
273 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
280 err = got_inflate_read_fd(&zb, infd, &avail);
284 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
285 newbuf = reallocarray(*outbuf, ++nbuf,
286 GOT_INFLATE_BUFSIZE);
287 if (newbuf == NULL) {
288 err = got_error_from_errno("reallocarray");
295 zb.outbuf = newbuf + *outlen;
296 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
298 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
301 got_inflate_end(&zb);
305 const struct got_error *
306 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
307 size_t offset, size_t len)
309 const struct got_error *err;
310 size_t avail, consumed;
311 struct got_inflate_buf zb;
315 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
317 return got_error_from_errno("malloc");
318 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
328 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
337 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
338 newbuf = reallocarray(*outbuf, ++nbuf,
339 GOT_INFLATE_BUFSIZE);
340 if (newbuf == NULL) {
341 err = got_error_from_errno("reallocarray");
348 zb.outbuf = newbuf + *outlen;
349 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
351 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
353 got_inflate_end(&zb);
357 const struct got_error *
358 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
360 const struct got_error *err = NULL;
362 struct got_inflate_buf zb;
364 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
371 err = got_inflate_read(&zb, infile, &avail);
376 n = write(outfd, zb.outbuf, avail);
378 err = got_error_from_errno("write");
383 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
387 if (lseek(outfd, SEEK_SET, 0) == -1)
388 err = got_error_from_errno("lseek");
390 got_inflate_end(&zb);
394 const struct got_error *
395 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
397 const struct got_error *err;
399 struct got_inflate_buf zb;
401 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
408 err = got_inflate_read(&zb, infile, &avail);
413 n = fwrite(zb.outbuf, avail, 1, outfile);
415 err = got_ferror(outfile, GOT_ERR_IO);
420 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
425 got_inflate_end(&zb);
429 const struct got_error *
430 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
432 const struct got_error *err;
434 struct got_inflate_buf zb;
436 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
443 err = got_inflate_read_fd(&zb, infd, &avail);
448 n = fwrite(zb.outbuf, avail, 1, outfile);
450 err = got_ferror(outfile, GOT_ERR_IO);
455 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
460 got_inflate_end(&zb);
464 const struct got_error *
465 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
466 size_t len, FILE *outfile)
468 const struct got_error *err;
470 struct got_inflate_buf zb;
473 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
480 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
488 n = fwrite(zb.outbuf, avail, 1, outfile);
490 err = got_ferror(outfile, GOT_ERR_IO);
495 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
500 got_inflate_end(&zb);