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>
26 #include "got_error.h"
27 #include "got_object.h"
29 #include "got_lib_path.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_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
39 const struct got_error *err = NULL;
41 memset(&zb->z, 0, sizeof(zb->z));
43 zb->z.zalloc = Z_NULL;
45 if (inflateInit(&zb->z) != Z_OK) {
46 err = got_error(GOT_ERR_IO);
50 zb->inlen = zb->outlen = bufsize;
52 zb->inbuf = calloc(1, zb->inlen);
53 if (zb->inbuf == NULL) {
54 err = got_error_from_errno();
60 zb->outbuf = calloc(1, zb->outlen);
61 if (zb->outbuf == NULL) {
62 err = got_error_from_errno();
65 zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
75 const struct got_error *
76 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
78 size_t last_total_out = zb->z.total_out;
82 z->next_out = zb->outbuf;
83 z->avail_out = zb->outlen;
87 if (z->avail_in == 0) {
88 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
91 return got_ferror(f, GOT_ERR_IO);
96 z->next_in = zb->inbuf;
99 ret = inflate(z, Z_SYNC_FLUSH);
100 } while (ret == Z_OK && z->avail_out > 0);
103 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
105 if (ret != Z_STREAM_END)
106 return got_error(GOT_ERR_DECOMPRESSION);
107 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
110 *outlenp = z->total_out - last_total_out;
114 const struct got_error *
115 got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
117 size_t last_total_out = zb->z.total_out;
118 z_stream *z = &zb->z;
121 z->next_out = zb->outbuf;
122 z->avail_out = zb->outlen;
126 if (z->avail_in == 0) {
127 ssize_t n = read(fd, zb->inbuf, zb->inlen);
129 return got_error_from_errno();
135 z->next_in = zb->inbuf;
138 ret = inflate(z, Z_SYNC_FLUSH);
139 } while (ret == Z_OK && z->avail_out > 0);
142 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
144 if (ret != Z_STREAM_END)
145 return got_error(GOT_ERR_DECOMPRESSION);
146 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
149 *outlenp = z->total_out - last_total_out;
153 const struct got_error *
154 got_inflate_read_mmap(struct got_zstream_buf *zb, uint8_t *map, size_t offset,
155 size_t len, size_t *outlenp, size_t *consumed)
157 size_t last_total_out = zb->z.total_out;
158 z_stream *z = &zb->z;
161 z->next_out = zb->outbuf;
162 z->avail_out = zb->outlen;
168 size_t last_total_in = zb->z.total_in;
169 if (z->avail_in == 0) {
175 z->next_in = map + offset + *consumed;
176 z->avail_in = MIN(zb->inlen, len);
179 ret = inflate(z, Z_SYNC_FLUSH);
180 *consumed += z->total_in - last_total_in;
181 } while (ret == Z_OK && z->avail_out > 0);
184 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
186 if (ret != Z_STREAM_END)
187 return got_error(GOT_ERR_DECOMPRESSION);
188 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
191 *outlenp = z->total_out - last_total_out;
196 got_inflate_end(struct got_zstream_buf *zb)
199 if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
204 const struct got_error *
205 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
207 const struct got_error *err;
209 struct got_zstream_buf zb;
213 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
215 return got_error_from_errno();
216 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
223 err = got_inflate_read(&zb, f, &avail);
227 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
229 newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
230 GOT_ZSTREAM_BUFSIZE);
231 if (newbuf == NULL) {
232 err = got_error_from_errno();
239 zb.outbuf = newbuf + *outlen;
240 zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
242 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
245 got_inflate_end(&zb);
249 const struct got_error *
250 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
252 const struct got_error *err;
254 struct got_zstream_buf zb;
258 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
260 return got_error_from_errno();
261 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
268 err = got_inflate_read_fd(&zb, infd, &avail);
272 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
274 newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
275 GOT_ZSTREAM_BUFSIZE);
276 if (newbuf == NULL) {
277 err = got_error_from_errno();
284 zb.outbuf = newbuf + *outlen;
285 zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
287 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
290 got_inflate_end(&zb);
294 const struct got_error *
295 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
296 size_t offset, size_t len)
298 const struct got_error *err;
299 size_t avail, consumed;
300 struct got_zstream_buf zb;
304 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
306 return got_error_from_errno();
307 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
317 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
326 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
328 newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
329 GOT_ZSTREAM_BUFSIZE);
330 if (newbuf == NULL) {
331 err = got_error_from_errno();
338 zb.outbuf = newbuf + *outlen;
339 zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
341 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
343 got_inflate_end(&zb);
347 const struct got_error *
348 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
350 const struct got_error *err = NULL;
352 struct got_zstream_buf zb;
354 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
361 err = got_inflate_read(&zb, infile, &avail);
366 n = write(outfd, zb.outbuf, avail);
368 err = got_error_from_errno();
373 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
377 if (lseek(outfd, SEEK_SET, 0) == -1)
378 err = got_error_from_errno();
380 got_inflate_end(&zb);
384 const struct got_error *
385 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
387 const struct got_error *err;
389 struct got_zstream_buf zb;
391 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
398 err = got_inflate_read(&zb, infile, &avail);
403 n = fwrite(zb.outbuf, avail, 1, outfile);
405 err = got_ferror(outfile, GOT_ERR_IO);
410 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
415 got_inflate_end(&zb);
419 const struct got_error *
420 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
422 const struct got_error *err;
424 struct got_zstream_buf zb;
426 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
433 err = got_inflate_read_fd(&zb, infd, &avail);
438 n = fwrite(zb.outbuf, avail, 1, outfile);
440 err = got_ferror(outfile, GOT_ERR_IO);
445 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
450 got_inflate_end(&zb);
454 const struct got_error *
455 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
456 size_t len, FILE *outfile)
458 const struct got_error *err;
460 struct got_zstream_buf zb;
463 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
470 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
478 n = fwrite(zb.outbuf, avail, 1, outfile);
480 err = got_ferror(outfile, GOT_ERR_IO);
485 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
490 got_inflate_end(&zb);