2 a76a38d9 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 a76a38d9 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 a76a38d9 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 a76a38d9 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
8 a76a38d9 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a76a38d9 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a76a38d9 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a76a38d9 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a76a38d9 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a76a38d9 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a76a38d9 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a76a38d9 2018-03-11 stsp #include <sys/queue.h>
19 a76a38d9 2018-03-11 stsp #include <stdio.h>
20 a76a38d9 2018-03-11 stsp #include <stdlib.h>
21 a76a38d9 2018-03-11 stsp #include <string.h>
22 a76a38d9 2018-03-11 stsp #include <sha1.h>
23 a76a38d9 2018-03-11 stsp #include <zlib.h>
24 788c352e 2018-06-16 stsp #include <time.h>
26 a76a38d9 2018-03-11 stsp #include "got_error.h"
27 a76a38d9 2018-03-11 stsp #include "got_object.h"
29 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
30 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
32 a76a38d9 2018-03-11 stsp const struct got_error *
33 19d747f7 2018-03-16 stsp got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
35 a76a38d9 2018-03-11 stsp const struct got_error *err = NULL;
37 a76a38d9 2018-03-11 stsp memset(zb, 0, sizeof(*zb));
39 a76a38d9 2018-03-11 stsp zb->z.zalloc = Z_NULL;
40 a76a38d9 2018-03-11 stsp zb->z.zfree = Z_NULL;
41 a76a38d9 2018-03-11 stsp if (inflateInit(&zb->z) != Z_OK) {
42 a76a38d9 2018-03-11 stsp err = got_error(GOT_ERR_IO);
46 a76a38d9 2018-03-11 stsp zb->inlen = zb->outlen = bufsize;
48 a76a38d9 2018-03-11 stsp zb->inbuf = calloc(1, zb->inlen);
49 a76a38d9 2018-03-11 stsp if (zb->inbuf == NULL) {
50 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
54 19d747f7 2018-03-16 stsp if (outbuf == NULL) {
55 19d747f7 2018-03-16 stsp zb->outbuf = calloc(1, zb->outlen);
56 19d747f7 2018-03-16 stsp if (zb->outbuf == NULL) {
57 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
60 19d747f7 2018-03-16 stsp zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
62 19d747f7 2018-03-16 stsp zb->outbuf = outbuf;
66 a76a38d9 2018-03-11 stsp got_inflate_end(zb);
70 a76a38d9 2018-03-11 stsp const struct got_error *
71 a76a38d9 2018-03-11 stsp got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
73 a76a38d9 2018-03-11 stsp size_t last_total_out = zb->z.total_out;
74 a76a38d9 2018-03-11 stsp z_stream *z = &zb->z;
75 92088384 2018-04-01 stsp int ret = Z_ERRNO;
77 a76a38d9 2018-03-11 stsp z->next_out = zb->outbuf;
78 a76a38d9 2018-03-11 stsp z->avail_out = zb->outlen;
80 a76a38d9 2018-03-11 stsp *outlenp = 0;
82 a76a38d9 2018-03-11 stsp if (z->avail_in == 0) {
83 a76a38d9 2018-03-11 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
84 a76a38d9 2018-03-11 stsp if (n == 0) {
85 a76a38d9 2018-03-11 stsp if (ferror(f))
86 a76a38d9 2018-03-11 stsp return got_ferror(f, GOT_ERR_IO);
88 92088384 2018-04-01 stsp ret = Z_STREAM_END;
91 a76a38d9 2018-03-11 stsp z->next_in = zb->inbuf;
92 a76a38d9 2018-03-11 stsp z->avail_in = n;
94 a76a38d9 2018-03-11 stsp ret = inflate(z, Z_SYNC_FLUSH);
95 a76a38d9 2018-03-11 stsp } while (ret == Z_OK && z->avail_out > 0);
97 a76a38d9 2018-03-11 stsp if (ret == Z_OK) {
98 a76a38d9 2018-03-11 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
100 a76a38d9 2018-03-11 stsp if (ret != Z_STREAM_END)
101 a76a38d9 2018-03-11 stsp return got_error(GOT_ERR_DECOMPRESSION);
102 a76a38d9 2018-03-11 stsp zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
105 a76a38d9 2018-03-11 stsp *outlenp = z->total_out - last_total_out;
106 a76a38d9 2018-03-11 stsp return NULL;
109 962916a2 2018-04-24 stsp const struct got_error *
110 962916a2 2018-04-24 stsp got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
112 962916a2 2018-04-24 stsp size_t last_total_out = zb->z.total_out;
113 962916a2 2018-04-24 stsp z_stream *z = &zb->z;
114 962916a2 2018-04-24 stsp int ret = Z_ERRNO;
116 962916a2 2018-04-24 stsp z->next_out = zb->outbuf;
117 962916a2 2018-04-24 stsp z->avail_out = zb->outlen;
119 962916a2 2018-04-24 stsp *outlenp = 0;
121 962916a2 2018-04-24 stsp if (z->avail_in == 0) {
122 962916a2 2018-04-24 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
124 962916a2 2018-04-24 stsp return got_error_from_errno();
125 962916a2 2018-04-24 stsp else if (n == 0) {
127 962916a2 2018-04-24 stsp ret = Z_STREAM_END;
130 962916a2 2018-04-24 stsp z->next_in = zb->inbuf;
131 962916a2 2018-04-24 stsp z->avail_in = n;
133 962916a2 2018-04-24 stsp ret = inflate(z, Z_SYNC_FLUSH);
134 962916a2 2018-04-24 stsp } while (ret == Z_OK && z->avail_out > 0);
136 962916a2 2018-04-24 stsp if (ret == Z_OK) {
137 962916a2 2018-04-24 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
139 962916a2 2018-04-24 stsp if (ret != Z_STREAM_END)
140 962916a2 2018-04-24 stsp return got_error(GOT_ERR_DECOMPRESSION);
141 962916a2 2018-04-24 stsp zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
144 962916a2 2018-04-24 stsp *outlenp = z->total_out - last_total_out;
145 962916a2 2018-04-24 stsp return NULL;
149 a76a38d9 2018-03-11 stsp got_inflate_end(struct got_zstream_buf *zb)
151 a76a38d9 2018-03-11 stsp free(zb->inbuf);
152 19d747f7 2018-03-16 stsp if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
153 19d747f7 2018-03-16 stsp free(zb->outbuf);
154 a76a38d9 2018-03-11 stsp inflateEnd(&zb->z);
157 a76a38d9 2018-03-11 stsp const struct got_error *
158 a76a38d9 2018-03-11 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
160 a76a38d9 2018-03-11 stsp const struct got_error *err;
161 a76a38d9 2018-03-11 stsp size_t avail;
162 a76a38d9 2018-03-11 stsp struct got_zstream_buf zb;
163 a76a38d9 2018-03-11 stsp void *newbuf;
165 19d747f7 2018-03-16 stsp *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
166 19d747f7 2018-03-16 stsp if (*outbuf == NULL)
167 0a585a0d 2018-03-17 stsp return got_error_from_errno();
168 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
170 a76a38d9 2018-03-11 stsp return err;
172 a76a38d9 2018-03-11 stsp *outlen = 0;
175 a76a38d9 2018-03-11 stsp err = got_inflate_read(&zb, f, &avail);
177 a76a38d9 2018-03-11 stsp return err;
178 19d747f7 2018-03-16 stsp *outlen += avail;
179 19d747f7 2018-03-16 stsp if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
180 19d747f7 2018-03-16 stsp newbuf = reallocarray(*outbuf, 1,
181 19d747f7 2018-03-16 stsp *outlen + GOT_ZSTREAM_BUFSIZE);
182 a76a38d9 2018-03-11 stsp if (newbuf == NULL) {
183 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
184 a76a38d9 2018-03-11 stsp free(*outbuf);
185 a76a38d9 2018-03-11 stsp *outbuf = NULL;
186 a76a38d9 2018-03-11 stsp *outlen = 0;
189 a76a38d9 2018-03-11 stsp *outbuf = newbuf;
190 19d747f7 2018-03-16 stsp zb.outbuf = newbuf + *outlen;
191 19d747f7 2018-03-16 stsp zb.outlen = GOT_ZSTREAM_BUFSIZE;
193 a76a38d9 2018-03-11 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
196 a76a38d9 2018-03-11 stsp got_inflate_end(&zb);
197 a76a38d9 2018-03-11 stsp return err;
200 a76a38d9 2018-03-11 stsp const struct got_error *
201 8b2180d4 2018-04-26 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
203 8b2180d4 2018-04-26 stsp const struct got_error *err;
204 8b2180d4 2018-04-26 stsp size_t avail;
205 8b2180d4 2018-04-26 stsp struct got_zstream_buf zb;
206 8b2180d4 2018-04-26 stsp void *newbuf;
208 8b2180d4 2018-04-26 stsp *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
209 8b2180d4 2018-04-26 stsp if (*outbuf == NULL)
210 8b2180d4 2018-04-26 stsp return got_error_from_errno();
211 8b2180d4 2018-04-26 stsp err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
213 8b2180d4 2018-04-26 stsp return err;
215 8b2180d4 2018-04-26 stsp *outlen = 0;
218 8b2180d4 2018-04-26 stsp err = got_inflate_read_fd(&zb, infd, &avail);
220 8b2180d4 2018-04-26 stsp return err;
221 8b2180d4 2018-04-26 stsp *outlen += avail;
222 8b2180d4 2018-04-26 stsp if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
223 8b2180d4 2018-04-26 stsp newbuf = reallocarray(*outbuf, 1,
224 8b2180d4 2018-04-26 stsp *outlen + GOT_ZSTREAM_BUFSIZE);
225 8b2180d4 2018-04-26 stsp if (newbuf == NULL) {
226 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
227 8b2180d4 2018-04-26 stsp free(*outbuf);
228 8b2180d4 2018-04-26 stsp *outbuf = NULL;
229 8b2180d4 2018-04-26 stsp *outlen = 0;
232 8b2180d4 2018-04-26 stsp *outbuf = newbuf;
233 8b2180d4 2018-04-26 stsp zb.outbuf = newbuf + *outlen;
234 8b2180d4 2018-04-26 stsp zb.outlen = GOT_ZSTREAM_BUFSIZE;
236 8b2180d4 2018-04-26 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
239 8b2180d4 2018-04-26 stsp got_inflate_end(&zb);
240 8b2180d4 2018-04-26 stsp return err;
243 8b2180d4 2018-04-26 stsp const struct got_error *
244 8b2180d4 2018-04-26 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
246 962916a2 2018-04-24 stsp const struct got_error *err = NULL;
247 962916a2 2018-04-24 stsp size_t avail;
248 962916a2 2018-04-24 stsp struct got_zstream_buf zb;
250 962916a2 2018-04-24 stsp err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
254 962916a2 2018-04-24 stsp *outlen = 0;
257 8b2180d4 2018-04-26 stsp err = got_inflate_read(&zb, infile, &avail);
259 962916a2 2018-04-24 stsp return err;
260 962916a2 2018-04-24 stsp if (avail > 0) {
262 962916a2 2018-04-24 stsp n = write(outfd, zb.outbuf, avail);
263 962916a2 2018-04-24 stsp if (n != avail) {
264 962916a2 2018-04-24 stsp err = got_error_from_errno();
267 962916a2 2018-04-24 stsp *outlen += avail;
269 962916a2 2018-04-24 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
272 962916a2 2018-04-24 stsp if (err == NULL) {
273 962916a2 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
274 962916a2 2018-04-24 stsp err = got_error_from_errno();
276 962916a2 2018-04-24 stsp got_inflate_end(&zb);
277 962916a2 2018-04-24 stsp return err;
280 962916a2 2018-04-24 stsp const struct got_error *
281 a76a38d9 2018-03-11 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
283 a76a38d9 2018-03-11 stsp const struct got_error *err;
284 a76a38d9 2018-03-11 stsp size_t avail;
285 a76a38d9 2018-03-11 stsp struct got_zstream_buf zb;
287 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
291 a76a38d9 2018-03-11 stsp *outlen = 0;
294 a76a38d9 2018-03-11 stsp err = got_inflate_read(&zb, infile, &avail);
296 a76a38d9 2018-03-11 stsp return err;
297 a76a38d9 2018-03-11 stsp if (avail > 0) {
299 a76a38d9 2018-03-11 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
300 a76a38d9 2018-03-11 stsp if (n != 1) {
301 a76a38d9 2018-03-11 stsp err = got_ferror(outfile, GOT_ERR_IO);
304 a76a38d9 2018-03-11 stsp *outlen += avail;
306 a76a38d9 2018-03-11 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
309 a76a38d9 2018-03-11 stsp if (err == NULL)
310 a76a38d9 2018-03-11 stsp rewind(outfile);
311 a76a38d9 2018-03-11 stsp got_inflate_end(&zb);
312 a76a38d9 2018-03-11 stsp return err;
315 8b2180d4 2018-04-26 stsp const struct got_error *
316 8b2180d4 2018-04-26 stsp got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
318 8b2180d4 2018-04-26 stsp const struct got_error *err;
319 8b2180d4 2018-04-26 stsp size_t avail;
320 8b2180d4 2018-04-26 stsp struct got_zstream_buf zb;
322 8b2180d4 2018-04-26 stsp err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
326 8b2180d4 2018-04-26 stsp *outlen = 0;
329 8b2180d4 2018-04-26 stsp err = got_inflate_read_fd(&zb, infd, &avail);
331 8b2180d4 2018-04-26 stsp return err;
332 8b2180d4 2018-04-26 stsp if (avail > 0) {
334 8b2180d4 2018-04-26 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
335 8b2180d4 2018-04-26 stsp if (n != 1) {
336 8b2180d4 2018-04-26 stsp err = got_ferror(outfile, GOT_ERR_IO);
339 8b2180d4 2018-04-26 stsp *outlen += avail;
341 8b2180d4 2018-04-26 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
344 8b2180d4 2018-04-26 stsp if (err == NULL)
345 8b2180d4 2018-04-26 stsp rewind(outfile);
346 8b2180d4 2018-04-26 stsp got_inflate_end(&zb);
347 8b2180d4 2018-04-26 stsp return err;