2 63581804 2018-07-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 63581804 2018-07-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 63581804 2018-07-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 63581804 2018-07-09 stsp * copyright notice and this permission notice appear in all copies.
8 63581804 2018-07-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 63581804 2018-07-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 63581804 2018-07-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 63581804 2018-07-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 63581804 2018-07-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 63581804 2018-07-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 63581804 2018-07-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 63581804 2018-07-09 stsp #include <sys/queue.h>
19 63581804 2018-07-09 stsp #include <stdio.h>
20 63581804 2018-07-09 stsp #include <stdlib.h>
21 63581804 2018-07-09 stsp #include <string.h>
22 63581804 2018-07-09 stsp #include <sha1.h>
23 63581804 2018-07-09 stsp #include <zlib.h>
24 63581804 2018-07-09 stsp #include <time.h>
26 63581804 2018-07-09 stsp #include "got_error.h"
27 63581804 2018-07-09 stsp #include "got_object.h"
29 63581804 2018-07-09 stsp #include "got_lib_path.h"
30 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
33 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
36 63581804 2018-07-09 stsp const struct got_error *
37 63581804 2018-07-09 stsp got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
39 63581804 2018-07-09 stsp const struct got_error *err = NULL;
41 63581804 2018-07-09 stsp memset(&zb->z, 0, sizeof(zb->z));
43 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
44 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
45 63581804 2018-07-09 stsp if (inflateInit(&zb->z) != Z_OK) {
46 63581804 2018-07-09 stsp err = got_error(GOT_ERR_IO);
50 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
52 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
53 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
54 63581804 2018-07-09 stsp err = got_error_from_errno();
58 63581804 2018-07-09 stsp zb->flags = 0;
59 63581804 2018-07-09 stsp if (outbuf == NULL) {
60 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
61 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
62 63581804 2018-07-09 stsp err = got_error_from_errno();
65 63581804 2018-07-09 stsp zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
67 63581804 2018-07-09 stsp zb->outbuf = outbuf;
71 63581804 2018-07-09 stsp got_inflate_end(zb);
75 63581804 2018-07-09 stsp const struct got_error *
76 63581804 2018-07-09 stsp got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
78 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
79 63581804 2018-07-09 stsp z_stream *z = &zb->z;
80 63581804 2018-07-09 stsp int ret = Z_ERRNO;
82 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
83 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
85 63581804 2018-07-09 stsp *outlenp = 0;
87 63581804 2018-07-09 stsp if (z->avail_in == 0) {
88 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
89 63581804 2018-07-09 stsp if (n == 0) {
90 63581804 2018-07-09 stsp if (ferror(f))
91 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
93 63581804 2018-07-09 stsp ret = Z_STREAM_END;
96 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
97 63581804 2018-07-09 stsp z->avail_in = n;
99 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
100 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
102 63581804 2018-07-09 stsp if (ret == Z_OK) {
103 63581804 2018-07-09 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
105 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
106 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
107 63581804 2018-07-09 stsp zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
110 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
111 63581804 2018-07-09 stsp return NULL;
114 63581804 2018-07-09 stsp const struct got_error *
115 63581804 2018-07-09 stsp got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
117 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
118 63581804 2018-07-09 stsp z_stream *z = &zb->z;
119 63581804 2018-07-09 stsp int ret = Z_ERRNO;
121 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
122 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
124 63581804 2018-07-09 stsp *outlenp = 0;
126 63581804 2018-07-09 stsp if (z->avail_in == 0) {
127 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
129 63581804 2018-07-09 stsp return got_error_from_errno();
130 63581804 2018-07-09 stsp else if (n == 0) {
132 63581804 2018-07-09 stsp ret = Z_STREAM_END;
135 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
136 63581804 2018-07-09 stsp z->avail_in = n;
138 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
139 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
141 63581804 2018-07-09 stsp if (ret == Z_OK) {
142 63581804 2018-07-09 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
144 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
145 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
146 63581804 2018-07-09 stsp zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
149 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
150 63581804 2018-07-09 stsp return NULL;
153 63581804 2018-07-09 stsp const struct got_error *
154 63581804 2018-07-09 stsp got_inflate_read_mmap(struct got_zstream_buf *zb, uint8_t *map, size_t offset,
155 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
157 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
158 63581804 2018-07-09 stsp z_stream *z = &zb->z;
159 63581804 2018-07-09 stsp int ret = Z_ERRNO;
161 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
162 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
164 63581804 2018-07-09 stsp *outlenp = 0;
165 63581804 2018-07-09 stsp *consumed = 0;
168 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
169 63581804 2018-07-09 stsp if (z->avail_in == 0) {
170 63581804 2018-07-09 stsp if (len == 0) {
172 63581804 2018-07-09 stsp ret = Z_STREAM_END;
175 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
176 63581804 2018-07-09 stsp z->avail_in = MIN(zb->inlen, len);
177 63581804 2018-07-09 stsp len -= z->avail_in;
179 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
180 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
181 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
183 63581804 2018-07-09 stsp if (ret == Z_OK) {
184 63581804 2018-07-09 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
186 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
187 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
188 63581804 2018-07-09 stsp zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
191 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
192 63581804 2018-07-09 stsp return NULL;
196 63581804 2018-07-09 stsp got_inflate_end(struct got_zstream_buf *zb)
198 63581804 2018-07-09 stsp free(zb->inbuf);
199 63581804 2018-07-09 stsp if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
200 63581804 2018-07-09 stsp free(zb->outbuf);
201 63581804 2018-07-09 stsp inflateEnd(&zb->z);
204 63581804 2018-07-09 stsp const struct got_error *
205 63581804 2018-07-09 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
207 63581804 2018-07-09 stsp const struct got_error *err;
208 63581804 2018-07-09 stsp size_t avail;
209 63581804 2018-07-09 stsp struct got_zstream_buf zb;
210 63581804 2018-07-09 stsp void *newbuf;
211 17d745b8 2018-07-23 stsp int nbuf = 1;
213 63581804 2018-07-09 stsp *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
214 63581804 2018-07-09 stsp if (*outbuf == NULL)
215 63581804 2018-07-09 stsp return got_error_from_errno();
216 63581804 2018-07-09 stsp err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
218 63581804 2018-07-09 stsp return err;
220 63581804 2018-07-09 stsp *outlen = 0;
223 63581804 2018-07-09 stsp err = got_inflate_read(&zb, f, &avail);
226 63581804 2018-07-09 stsp *outlen += avail;
227 63581804 2018-07-09 stsp if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
229 17d745b8 2018-07-23 stsp newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
230 17d745b8 2018-07-23 stsp GOT_ZSTREAM_BUFSIZE);
231 63581804 2018-07-09 stsp if (newbuf == NULL) {
232 63581804 2018-07-09 stsp err = got_error_from_errno();
233 63581804 2018-07-09 stsp free(*outbuf);
234 63581804 2018-07-09 stsp *outbuf = NULL;
235 63581804 2018-07-09 stsp *outlen = 0;
238 63581804 2018-07-09 stsp *outbuf = newbuf;
239 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
240 17d745b8 2018-07-23 stsp zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
242 63581804 2018-07-09 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
245 63581804 2018-07-09 stsp got_inflate_end(&zb);
246 63581804 2018-07-09 stsp return err;
249 63581804 2018-07-09 stsp const struct got_error *
250 63581804 2018-07-09 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
252 63581804 2018-07-09 stsp const struct got_error *err;
253 63581804 2018-07-09 stsp size_t avail;
254 63581804 2018-07-09 stsp struct got_zstream_buf zb;
255 63581804 2018-07-09 stsp void *newbuf;
256 17d745b8 2018-07-23 stsp int nbuf = 1;
258 63581804 2018-07-09 stsp *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
259 63581804 2018-07-09 stsp if (*outbuf == NULL)
260 63581804 2018-07-09 stsp return got_error_from_errno();
261 63581804 2018-07-09 stsp err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
265 63581804 2018-07-09 stsp *outlen = 0;
268 63581804 2018-07-09 stsp err = got_inflate_read_fd(&zb, infd, &avail);
271 63581804 2018-07-09 stsp *outlen += avail;
272 63581804 2018-07-09 stsp if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
274 17d745b8 2018-07-23 stsp newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
275 17d745b8 2018-07-23 stsp GOT_ZSTREAM_BUFSIZE);
276 63581804 2018-07-09 stsp if (newbuf == NULL) {
277 63581804 2018-07-09 stsp err = got_error_from_errno();
278 63581804 2018-07-09 stsp free(*outbuf);
279 63581804 2018-07-09 stsp *outbuf = NULL;
280 63581804 2018-07-09 stsp *outlen = 0;
283 63581804 2018-07-09 stsp *outbuf = newbuf;
284 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
285 17d745b8 2018-07-23 stsp zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
287 63581804 2018-07-09 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
290 63581804 2018-07-09 stsp got_inflate_end(&zb);
291 63581804 2018-07-09 stsp return err;
294 63581804 2018-07-09 stsp const struct got_error *
295 63581804 2018-07-09 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
296 63581804 2018-07-09 stsp size_t offset, size_t len)
298 63581804 2018-07-09 stsp const struct got_error *err;
299 37bd7602 2018-07-23 stsp size_t avail, consumed;
300 63581804 2018-07-09 stsp struct got_zstream_buf zb;
301 63581804 2018-07-09 stsp void *newbuf;
302 37bd7602 2018-07-23 stsp int nbuf = 1;
304 63581804 2018-07-09 stsp *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
305 63581804 2018-07-09 stsp if (*outbuf == NULL)
306 63581804 2018-07-09 stsp return got_error_from_errno();
307 63581804 2018-07-09 stsp err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
309 60507209 2018-07-13 stsp free(*outbuf);
310 60507209 2018-07-13 stsp *outbuf = NULL;
311 63581804 2018-07-09 stsp return err;
314 63581804 2018-07-09 stsp *outlen = 0;
317 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
318 63581804 2018-07-09 stsp &consumed);
321 63581804 2018-07-09 stsp offset += consumed;
322 63581804 2018-07-09 stsp len -= consumed;
323 63581804 2018-07-09 stsp *outlen += avail;
324 63581804 2018-07-09 stsp if (len == 0)
326 63581804 2018-07-09 stsp if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
328 666b4ca8 2018-07-23 stsp newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
329 666b4ca8 2018-07-23 stsp GOT_ZSTREAM_BUFSIZE);
330 63581804 2018-07-09 stsp if (newbuf == NULL) {
331 63581804 2018-07-09 stsp err = got_error_from_errno();
332 63581804 2018-07-09 stsp free(*outbuf);
333 63581804 2018-07-09 stsp *outbuf = NULL;
334 63581804 2018-07-09 stsp *outlen = 0;
337 63581804 2018-07-09 stsp *outbuf = newbuf;
338 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
339 37bd7602 2018-07-23 stsp zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
341 63581804 2018-07-09 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
343 63581804 2018-07-09 stsp got_inflate_end(&zb);
344 63581804 2018-07-09 stsp return err;
347 63581804 2018-07-09 stsp const struct got_error *
348 63581804 2018-07-09 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
350 63581804 2018-07-09 stsp const struct got_error *err = NULL;
351 63581804 2018-07-09 stsp size_t avail;
352 63581804 2018-07-09 stsp struct got_zstream_buf zb;
354 63581804 2018-07-09 stsp err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
358 63581804 2018-07-09 stsp *outlen = 0;
361 63581804 2018-07-09 stsp err = got_inflate_read(&zb, infile, &avail);
364 63581804 2018-07-09 stsp if (avail > 0) {
366 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
367 63581804 2018-07-09 stsp if (n != avail) {
368 63581804 2018-07-09 stsp err = got_error_from_errno();
371 63581804 2018-07-09 stsp *outlen += avail;
373 63581804 2018-07-09 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
376 63581804 2018-07-09 stsp if (err == NULL) {
377 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
378 63581804 2018-07-09 stsp err = got_error_from_errno();
380 63581804 2018-07-09 stsp got_inflate_end(&zb);
381 63581804 2018-07-09 stsp return err;
384 63581804 2018-07-09 stsp const struct got_error *
385 63581804 2018-07-09 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
387 63581804 2018-07-09 stsp const struct got_error *err;
388 63581804 2018-07-09 stsp size_t avail;
389 63581804 2018-07-09 stsp struct got_zstream_buf zb;
391 63581804 2018-07-09 stsp err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
395 63581804 2018-07-09 stsp *outlen = 0;
398 63581804 2018-07-09 stsp err = got_inflate_read(&zb, infile, &avail);
401 63581804 2018-07-09 stsp if (avail > 0) {
403 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
404 63581804 2018-07-09 stsp if (n != 1) {
405 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
408 63581804 2018-07-09 stsp *outlen += avail;
410 63581804 2018-07-09 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
413 63581804 2018-07-09 stsp if (err == NULL)
414 63581804 2018-07-09 stsp rewind(outfile);
415 63581804 2018-07-09 stsp got_inflate_end(&zb);
416 63581804 2018-07-09 stsp return err;
419 63581804 2018-07-09 stsp const struct got_error *
420 63581804 2018-07-09 stsp got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
422 63581804 2018-07-09 stsp const struct got_error *err;
423 63581804 2018-07-09 stsp size_t avail;
424 63581804 2018-07-09 stsp struct got_zstream_buf zb;
426 63581804 2018-07-09 stsp err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
430 63581804 2018-07-09 stsp *outlen = 0;
433 63581804 2018-07-09 stsp err = got_inflate_read_fd(&zb, infd, &avail);
436 63581804 2018-07-09 stsp if (avail > 0) {
438 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
439 63581804 2018-07-09 stsp if (n != 1) {
440 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
443 63581804 2018-07-09 stsp *outlen += avail;
445 63581804 2018-07-09 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
448 63581804 2018-07-09 stsp if (err == NULL)
449 63581804 2018-07-09 stsp rewind(outfile);
450 63581804 2018-07-09 stsp got_inflate_end(&zb);
451 63581804 2018-07-09 stsp return err;
454 63581804 2018-07-09 stsp const struct got_error *
455 63581804 2018-07-09 stsp got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
456 63581804 2018-07-09 stsp size_t len, FILE *outfile)
458 63581804 2018-07-09 stsp const struct got_error *err;
459 63581804 2018-07-09 stsp size_t avail;
460 63581804 2018-07-09 stsp struct got_zstream_buf zb;
461 63581804 2018-07-09 stsp size_t consumed;
463 63581804 2018-07-09 stsp err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
467 63581804 2018-07-09 stsp *outlen = 0;
470 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
471 63581804 2018-07-09 stsp &consumed);
474 63581804 2018-07-09 stsp offset += consumed;
475 63581804 2018-07-09 stsp len -= consumed;
476 63581804 2018-07-09 stsp if (avail > 0) {
478 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
479 63581804 2018-07-09 stsp if (n != 1) {
480 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
483 63581804 2018-07-09 stsp *outlen += avail;
485 63581804 2018-07-09 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
488 63581804 2018-07-09 stsp if (err == NULL)
489 63581804 2018-07-09 stsp rewind(outfile);
490 63581804 2018-07-09 stsp got_inflate_end(&zb);
491 63581804 2018-07-09 stsp return err;