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_zbuf.h"
32 const struct got_error *
33 got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
35 const struct got_error *err = NULL;
37 memset(zb, 0, sizeof(*zb));
39 zb->z.zalloc = Z_NULL;
41 if (inflateInit(&zb->z) != Z_OK) {
42 err = got_error(GOT_ERR_IO);
46 zb->inlen = zb->outlen = bufsize;
48 zb->inbuf = calloc(1, zb->inlen);
49 if (zb->inbuf == NULL) {
50 err = got_error_from_errno();
55 zb->outbuf = calloc(1, zb->outlen);
56 if (zb->outbuf == NULL) {
57 err = got_error_from_errno();
60 zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
70 const struct got_error *
71 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
73 size_t last_total_out = zb->z.total_out;
77 z->next_out = zb->outbuf;
78 z->avail_out = zb->outlen;
82 if (z->avail_in == 0) {
83 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
86 return got_ferror(f, GOT_ERR_IO);
91 z->next_in = zb->inbuf;
94 ret = inflate(z, Z_SYNC_FLUSH);
95 } while (ret == Z_OK && z->avail_out > 0);
98 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
100 if (ret != Z_STREAM_END)
101 return got_error(GOT_ERR_DECOMPRESSION);
102 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
105 *outlenp = z->total_out - last_total_out;
109 const struct got_error *
110 got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
112 size_t last_total_out = zb->z.total_out;
113 z_stream *z = &zb->z;
116 z->next_out = zb->outbuf;
117 z->avail_out = zb->outlen;
121 if (z->avail_in == 0) {
122 ssize_t n = read(fd, zb->inbuf, zb->inlen);
124 return got_error_from_errno();
130 z->next_in = zb->inbuf;
133 ret = inflate(z, Z_SYNC_FLUSH);
134 } while (ret == Z_OK && z->avail_out > 0);
137 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
139 if (ret != Z_STREAM_END)
140 return got_error(GOT_ERR_DECOMPRESSION);
141 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
144 *outlenp = z->total_out - last_total_out;
149 got_inflate_end(struct got_zstream_buf *zb)
152 if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
157 const struct got_error *
158 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
160 const struct got_error *err;
162 struct got_zstream_buf zb;
165 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
167 return got_error_from_errno();
168 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
175 err = got_inflate_read(&zb, f, &avail);
179 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
180 newbuf = reallocarray(*outbuf, 1,
181 *outlen + GOT_ZSTREAM_BUFSIZE);
182 if (newbuf == NULL) {
183 err = got_error_from_errno();
190 zb.outbuf = newbuf + *outlen;
191 zb.outlen = GOT_ZSTREAM_BUFSIZE;
193 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
196 got_inflate_end(&zb);
200 const struct got_error *
201 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
203 const struct got_error *err;
205 struct got_zstream_buf zb;
208 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
210 return got_error_from_errno();
211 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
218 err = got_inflate_read_fd(&zb, infd, &avail);
222 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
223 newbuf = reallocarray(*outbuf, 1,
224 *outlen + GOT_ZSTREAM_BUFSIZE);
225 if (newbuf == NULL) {
226 err = got_error_from_errno();
233 zb.outbuf = newbuf + *outlen;
234 zb.outlen = GOT_ZSTREAM_BUFSIZE;
236 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
239 got_inflate_end(&zb);
243 const struct got_error *
244 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
246 const struct got_error *err = NULL;
248 struct got_zstream_buf zb;
250 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
257 err = got_inflate_read(&zb, infile, &avail);
262 n = write(outfd, zb.outbuf, avail);
264 err = got_error_from_errno();
269 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
273 if (lseek(outfd, SEEK_SET, 0) == -1)
274 err = got_error_from_errno();
276 got_inflate_end(&zb);
280 const struct got_error *
281 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
283 const struct got_error *err;
285 struct got_zstream_buf zb;
287 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
294 err = got_inflate_read(&zb, infile, &avail);
299 n = fwrite(zb.outbuf, avail, 1, outfile);
301 err = got_ferror(outfile, GOT_ERR_IO);
306 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
311 got_inflate_end(&zb);
315 const struct got_error *
316 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
318 const struct got_error *err;
320 struct got_zstream_buf zb;
322 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
329 err = got_inflate_read_fd(&zb, infd, &avail);
334 n = fwrite(zb.outbuf, avail, 1, outfile);
336 err = got_ferror(outfile, GOT_ERR_IO);
341 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
346 got_inflate_end(&zb);