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"
31 const struct got_error *
32 got_inflate_init(struct got_zstream_buf *zb, 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(GOT_ERR_NO_MEM);
53 zb->outbuf = calloc(1, zb->outlen);
54 if (zb->outbuf == NULL) {
55 err = got_error(GOT_ERR_NO_MEM);
65 const struct got_error *
66 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *inlenp,
69 size_t last_total_out = zb->z.total_out;
73 z->next_out = zb->outbuf;
74 z->avail_out = zb->outlen;
79 if (z->avail_in == 0) {
80 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
83 return got_ferror(f, GOT_ERR_IO);
87 z->next_in = zb->inbuf;
92 ret = inflate(z, Z_SYNC_FLUSH);
93 } while (ret == Z_OK && z->avail_out > 0);
96 if (ret != Z_STREAM_END)
97 return got_error(GOT_ERR_DECOMPRESSION);
98 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
101 *outlenp = z->total_out - last_total_out;
106 got_inflate_end(struct got_zstream_buf *zb)
113 const struct got_error *
114 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f, size_t insize)
116 const struct got_error *err;
117 size_t inbytes, consumed, avail;
118 struct got_zstream_buf zb;
121 err = got_inflate_init(&zb, 8192);
130 err = got_inflate_read(&zb, f, &consumed, &avail);
135 if (inbytes < insize)
136 err = got_error(GOT_ERR_BAD_DELTA);
139 newbuf = reallocarray(*outbuf, 1, *outlen + avail);
140 if (newbuf == NULL) {
144 err = got_error(GOT_ERR_NO_MEM);
147 memcpy(newbuf + *outlen, zb.outbuf, avail);
150 } while (inbytes < insize);
153 got_inflate_end(&zb);
157 const struct got_error *
158 got_inflate_to_tempfile(FILE **outfile, size_t *outlen, FILE *f)
160 const struct got_error *err;
162 struct got_zstream_buf zb;
165 *outfile = got_opentemp();
166 if (*outfile == NULL)
167 return got_error_from_errno();
169 err = got_inflate_init(&zb, 8192);
176 err = got_inflate_read(&zb, f, NULL, &avail);
181 n = fwrite(zb.outbuf, avail, 1, *outfile);
183 err = got_ferror(*outfile, GOT_ERR_IO);
196 got_inflate_end(&zb);