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;
90 z->next_out = zb->outbuf;
91 z->avail_out = zb->outlen;
95 if (z->avail_in == 0) {
96 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
99 return got_ferror(f, GOT_ERR_IO);
104 z->next_in = zb->inbuf;
107 ret = inflate(z, Z_SYNC_FLUSH);
108 } while (ret == Z_OK && z->avail_out > 0);
111 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
113 if (ret != Z_STREAM_END)
114 return got_error(GOT_ERR_DECOMPRESSION);
115 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
118 *outlenp = z->total_out - last_total_out;
122 const struct got_error *
123 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp)
125 size_t last_total_out = zb->z.total_out;
126 z_stream *z = &zb->z;
129 z->next_out = zb->outbuf;
130 z->avail_out = zb->outlen;
134 if (z->avail_in == 0) {
135 ssize_t n = read(fd, zb->inbuf, zb->inlen);
137 return got_error_from_errno("read");
143 z->next_in = zb->inbuf;
146 ret = inflate(z, Z_SYNC_FLUSH);
147 } while (ret == Z_OK && z->avail_out > 0);
150 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
152 if (ret != Z_STREAM_END)
153 return got_error(GOT_ERR_DECOMPRESSION);
154 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
157 *outlenp = z->total_out - last_total_out;
161 const struct got_error *
162 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
163 size_t len, size_t *outlenp, size_t *consumed)
165 size_t last_total_out = zb->z.total_out;
166 z_stream *z = &zb->z;
169 z->next_out = zb->outbuf;
170 z->avail_out = zb->outlen;
176 size_t last_total_in = zb->z.total_in;
177 if (z->avail_in == 0) {
183 z->next_in = map + offset + *consumed;
184 z->avail_in = MIN(zb->inlen, len);
187 ret = inflate(z, Z_SYNC_FLUSH);
188 *consumed += z->total_in - last_total_in;
189 } while (ret == Z_OK && z->avail_out > 0);
192 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
194 if (ret != Z_STREAM_END)
195 return got_error(GOT_ERR_DECOMPRESSION);
196 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
199 *outlenp = z->total_out - last_total_out;
204 got_inflate_end(struct got_inflate_buf *zb)
207 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
212 const struct got_error *
213 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
215 const struct got_error *err;
217 struct got_inflate_buf zb;
221 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
223 return got_error_from_errno("calloc");
224 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
231 err = got_inflate_read(&zb, f, &avail);
235 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
236 newbuf = reallocarray(*outbuf, ++nbuf,
237 GOT_INFLATE_BUFSIZE);
238 if (newbuf == NULL) {
239 err = got_error_from_errno("reallocarray");
246 zb.outbuf = newbuf + *outlen;
247 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
249 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
252 got_inflate_end(&zb);
256 const struct got_error *
257 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
259 const struct got_error *err;
261 struct got_inflate_buf zb;
265 *outbuf = calloc(1, GOT_INFLATE_BUFSIZE);
267 return got_error_from_errno("calloc");
268 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
275 err = got_inflate_read_fd(&zb, infd, &avail);
279 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
281 newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
282 GOT_INFLATE_BUFSIZE);
283 if (newbuf == NULL) {
284 err = got_error_from_errno("recallocarray");
291 zb.outbuf = newbuf + *outlen;
292 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
294 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
297 got_inflate_end(&zb);
301 const struct got_error *
302 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
303 size_t offset, size_t len)
305 const struct got_error *err;
306 size_t avail, consumed;
307 struct got_inflate_buf zb;
311 *outbuf = calloc(1, GOT_INFLATE_BUFSIZE);
313 return got_error_from_errno("calloc");
314 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
324 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
333 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
335 newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
336 GOT_INFLATE_BUFSIZE);
337 if (newbuf == NULL) {
338 err = got_error_from_errno("recallocarray");
345 zb.outbuf = newbuf + *outlen;
346 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
348 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
350 got_inflate_end(&zb);
354 const struct got_error *
355 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
357 const struct got_error *err = NULL;
359 struct got_inflate_buf zb;
361 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
368 err = got_inflate_read(&zb, infile, &avail);
373 n = write(outfd, zb.outbuf, avail);
375 err = got_error_from_errno("write");
380 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
384 if (lseek(outfd, SEEK_SET, 0) == -1)
385 err = got_error_from_errno("lseek");
387 got_inflate_end(&zb);
391 const struct got_error *
392 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
394 const struct got_error *err;
396 struct got_inflate_buf zb;
398 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
405 err = got_inflate_read(&zb, infile, &avail);
410 n = fwrite(zb.outbuf, avail, 1, outfile);
412 err = got_ferror(outfile, GOT_ERR_IO);
417 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
422 got_inflate_end(&zb);
426 const struct got_error *
427 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
429 const struct got_error *err;
431 struct got_inflate_buf zb;
433 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
440 err = got_inflate_read_fd(&zb, infd, &avail);
445 n = fwrite(zb.outbuf, avail, 1, outfile);
447 err = got_ferror(outfile, GOT_ERR_IO);
452 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
457 got_inflate_end(&zb);
461 const struct got_error *
462 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
463 size_t len, FILE *outfile)
465 const struct got_error *err;
467 struct got_inflate_buf zb;
470 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
477 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
485 n = fwrite(zb.outbuf, avail, 1, outfile);
487 err = got_ferror(outfile, GOT_ERR_IO);
492 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
497 got_inflate_end(&zb);