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>
25 #include "got_error.h"
26 #include "got_object.h"
28 #include "got_lib_path.h"
29 #include "got_lib_zbuf.h"
31 const struct got_error *
32 got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
34 const struct got_error *err = NULL;
36 memset(zb, 0, sizeof(*zb));
38 zb->z.zalloc = Z_NULL;
40 if (inflateInit(&zb->z) != Z_OK) {
41 err = got_error(GOT_ERR_IO);
45 zb->inlen = zb->outlen = bufsize;
47 zb->inbuf = calloc(1, zb->inlen);
48 if (zb->inbuf == NULL) {
49 err = got_error_from_errno();
54 zb->outbuf = calloc(1, zb->outlen);
55 if (zb->outbuf == NULL) {
56 err = got_error_from_errno();
59 zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
69 const struct got_error *
70 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
72 size_t last_total_out = zb->z.total_out;
76 z->next_out = zb->outbuf;
77 z->avail_out = zb->outlen;
81 if (z->avail_in == 0) {
82 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
85 return got_ferror(f, GOT_ERR_IO);
90 z->next_in = zb->inbuf;
93 ret = inflate(z, Z_SYNC_FLUSH);
94 } while (ret == Z_OK && z->avail_out > 0);
97 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
99 if (ret != Z_STREAM_END)
100 return got_error(GOT_ERR_DECOMPRESSION);
101 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
104 *outlenp = z->total_out - last_total_out;
108 const struct got_error *
109 got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
111 size_t last_total_out = zb->z.total_out;
112 z_stream *z = &zb->z;
115 z->next_out = zb->outbuf;
116 z->avail_out = zb->outlen;
120 if (z->avail_in == 0) {
121 ssize_t n = read(fd, zb->inbuf, zb->inlen);
123 return got_error_from_errno();
129 z->next_in = zb->inbuf;
132 ret = inflate(z, Z_SYNC_FLUSH);
133 } while (ret == Z_OK && z->avail_out > 0);
136 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
138 if (ret != Z_STREAM_END)
139 return got_error(GOT_ERR_DECOMPRESSION);
140 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
143 *outlenp = z->total_out - last_total_out;
148 got_inflate_end(struct got_zstream_buf *zb)
151 if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
156 const struct got_error *
157 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
159 const struct got_error *err;
161 struct got_zstream_buf zb;
164 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
166 return got_error_from_errno();
167 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
174 err = got_inflate_read(&zb, f, &avail);
178 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
179 newbuf = reallocarray(*outbuf, 1,
180 *outlen + GOT_ZSTREAM_BUFSIZE);
181 if (newbuf == NULL) {
182 err = got_error_from_errno();
189 zb.outbuf = newbuf + *outlen;
190 zb.outlen = GOT_ZSTREAM_BUFSIZE;
192 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
195 got_inflate_end(&zb);
199 const struct got_error *
200 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
202 const struct got_error *err;
204 struct got_zstream_buf zb;
207 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
209 return got_error_from_errno();
210 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
217 err = got_inflate_read_fd(&zb, infd, &avail);
221 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
222 newbuf = reallocarray(*outbuf, 1,
223 *outlen + GOT_ZSTREAM_BUFSIZE);
224 if (newbuf == NULL) {
225 err = got_error_from_errno();
232 zb.outbuf = newbuf + *outlen;
233 zb.outlen = GOT_ZSTREAM_BUFSIZE;
235 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
238 got_inflate_end(&zb);
242 const struct got_error *
243 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
245 const struct got_error *err = NULL;
247 struct got_zstream_buf zb;
249 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
256 err = got_inflate_read(&zb, infile, &avail);
261 n = write(outfd, zb.outbuf, avail);
263 err = got_error_from_errno();
268 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
272 if (lseek(outfd, SEEK_SET, 0) == -1)
273 err = got_error_from_errno();
275 got_inflate_end(&zb);
279 const struct got_error *
280 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
282 const struct got_error *err;
284 struct got_zstream_buf zb;
286 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
293 err = got_inflate_read(&zb, infile, &avail);
298 n = fwrite(zb.outbuf, avail, 1, outfile);
300 err = got_ferror(outfile, GOT_ERR_IO);
305 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
310 got_inflate_end(&zb);
314 const struct got_error *
315 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
317 const struct got_error *err;
319 struct got_zstream_buf zb;
321 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
328 err = got_inflate_read_fd(&zb, infd, &avail);
333 n = fwrite(zb.outbuf, avail, 1, outfile);
335 err = got_ferror(outfile, GOT_ERR_IO);
340 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
345 got_inflate_end(&zb);