Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <unistd.h>
25 #include <zlib.h>
26 #include <time.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_path.h"
32 #include "got_lib_inflate.h"
34 #ifndef MIN
35 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
36 #endif
38 const struct got_error *
39 got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
40 struct got_inflate_checksum *csum)
41 {
42 const struct got_error *err = NULL;
43 int zerr;
45 memset(&zb->z, 0, sizeof(zb->z));
47 zb->z.zalloc = Z_NULL;
48 zb->z.zfree = Z_NULL;
49 zerr = inflateInit(&zb->z);
50 if (zerr != Z_OK) {
51 if (zerr == Z_ERRNO)
52 return got_error_from_errno("inflateInit");
53 if (zerr == Z_MEM_ERROR) {
54 errno = ENOMEM;
55 return got_error_from_errno("inflateInit");
56 }
57 return got_error(GOT_ERR_DECOMPRESSION);
58 }
60 zb->inlen = zb->outlen = bufsize;
62 zb->inbuf = calloc(1, zb->inlen);
63 if (zb->inbuf == NULL) {
64 err = got_error_from_errno("calloc");
65 goto done;
66 }
68 zb->flags = 0;
69 if (outbuf == NULL) {
70 zb->outbuf = calloc(1, zb->outlen);
71 if (zb->outbuf == NULL) {
72 err = got_error_from_errno("calloc");
73 goto done;
74 }
75 zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
76 } else
77 zb->outbuf = outbuf;
79 zb->csum = csum;
80 done:
81 if (err)
82 got_inflate_end(zb);
83 return err;
84 }
86 static void
87 csum_input(struct got_inflate_checksum *csum, const char *buf, size_t len)
88 {
89 if (csum->input_crc)
90 *csum->input_crc = crc32(*csum->input_crc, buf, len);
92 if (csum->input_sha1)
93 SHA1Update(csum->input_sha1, buf, len);
94 }
96 const struct got_error *
97 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
98 size_t *consumed)
99 {
100 size_t last_total_out = zb->z.total_out;
101 size_t last_total_in = zb->z.total_in;
102 z_stream *z = &zb->z;
103 int ret = Z_ERRNO;
105 z->next_out = zb->outbuf;
106 z->avail_out = zb->outlen;
108 *outlenp = 0;
109 if (consumed)
110 *consumed = 0;
111 do {
112 char *csum_in = NULL;
113 size_t csum_avail = 0;
115 if (z->avail_in == 0) {
116 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
117 if (n == 0) {
118 if (ferror(f))
119 return got_ferror(f, GOT_ERR_IO);
120 /* EOF */
121 ret = Z_STREAM_END;
122 break;
124 z->next_in = zb->inbuf;
125 z->avail_in = n;
127 if (zb->csum) {
128 csum_in = z->next_in;
129 csum_avail = z->avail_in;
131 ret = inflate(z, Z_SYNC_FLUSH);
132 if (zb->csum)
133 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
134 } while (ret == Z_OK && z->avail_out > 0);
136 if (ret == Z_OK || ret == Z_BUF_ERROR) {
137 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
138 } else {
139 if (ret != Z_STREAM_END)
140 return got_error(GOT_ERR_DECOMPRESSION);
141 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
144 *outlenp = z->total_out - last_total_out;
145 if (consumed)
146 *consumed += z->total_in - last_total_in;
147 return NULL;
150 const struct got_error *
151 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
152 size_t *consumed)
154 size_t last_total_out = zb->z.total_out;
155 size_t last_total_in = zb->z.total_in;
156 z_stream *z = &zb->z;
157 int ret = Z_ERRNO;
159 z->next_out = zb->outbuf;
160 z->avail_out = zb->outlen;
162 *outlenp = 0;
163 if (consumed)
164 *consumed = 0;
165 do {
166 char *csum_in = NULL;
167 size_t csum_avail = 0;
169 if (z->avail_in == 0) {
170 ssize_t n = read(fd, zb->inbuf, zb->inlen);
171 if (n < 0)
172 return got_error_from_errno("read");
173 else if (n == 0) {
174 /* EOF */
175 ret = Z_STREAM_END;
176 break;
178 z->next_in = zb->inbuf;
179 z->avail_in = n;
181 if (zb->csum) {
182 csum_in = z->next_in;
183 csum_avail = z->avail_in;
185 ret = inflate(z, Z_SYNC_FLUSH);
186 if (zb->csum)
187 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
188 } while (ret == Z_OK && z->avail_out > 0);
190 if (ret == Z_OK || ret == Z_BUF_ERROR) {
191 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
192 } else {
193 if (ret != Z_STREAM_END)
194 return got_error(GOT_ERR_DECOMPRESSION);
195 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
198 *outlenp = z->total_out - last_total_out;
199 if (consumed)
200 *consumed += z->total_in - last_total_in;
201 return NULL;
204 const struct got_error *
205 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
206 size_t len, size_t *outlenp, size_t *consumed)
208 size_t last_total_out = zb->z.total_out;
209 z_stream *z = &zb->z;
210 int ret = Z_ERRNO;
212 z->next_out = zb->outbuf;
213 z->avail_out = zb->outlen;
215 *outlenp = 0;
216 *consumed = 0;
218 do {
219 char *csum_in = NULL;
220 size_t csum_avail = 0;
221 size_t last_total_in = zb->z.total_in;
223 if (z->avail_in == 0) {
224 if (len == 0) {
225 /* EOF */
226 ret = Z_STREAM_END;
227 break;
229 z->next_in = map + offset + *consumed;
230 z->avail_in = len - *consumed;
232 if (zb->csum) {
233 csum_in = z->next_in;
234 csum_avail = z->avail_in;
236 ret = inflate(z, Z_SYNC_FLUSH);
237 if (zb->csum)
238 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
239 *consumed += z->total_in - last_total_in;
240 } while (ret == Z_OK && z->avail_out > 0);
242 if (ret == Z_OK || ret == Z_BUF_ERROR) {
243 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
244 } else {
245 if (ret != Z_STREAM_END)
246 return got_error(GOT_ERR_DECOMPRESSION);
247 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
250 *outlenp = z->total_out - last_total_out;
251 return NULL;
254 void
255 got_inflate_end(struct got_inflate_buf *zb)
257 free(zb->inbuf);
258 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
259 free(zb->outbuf);
260 inflateEnd(&zb->z);
263 const struct got_error *
264 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
265 size_t *consumed_total, FILE *f)
267 const struct got_error *err;
268 size_t avail, consumed;
269 struct got_inflate_buf zb;
270 void *newbuf;
271 int nbuf = 1;
273 if (outbuf) {
274 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
275 if (*outbuf == NULL)
276 return got_error_from_errno("malloc");
277 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
278 } else
279 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
280 if (err)
281 return err;
283 *outlen = 0;
284 if (consumed_total)
285 *consumed_total = 0;
287 do {
288 err = got_inflate_read(&zb, f, &avail, &consumed);
289 if (err)
290 goto done;
291 *outlen += avail;
292 if (consumed_total)
293 *consumed_total += consumed;
294 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
295 if (outbuf == NULL)
296 continue;
297 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
298 newbuf = reallocarray(*outbuf, ++nbuf,
299 GOT_INFLATE_BUFSIZE);
300 if (newbuf == NULL) {
301 err = got_error_from_errno("reallocarray");
302 free(*outbuf);
303 *outbuf = NULL;
304 *outlen = 0;
305 goto done;
307 *outbuf = newbuf;
308 zb.outbuf = newbuf + *outlen;
310 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
312 done:
313 got_inflate_end(&zb);
314 return err;
317 const struct got_error *
318 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
319 size_t *consumed_total, struct got_inflate_checksum *csum,
320 size_t expected_size, int infd)
322 const struct got_error *err;
323 size_t avail, consumed;
324 struct got_inflate_buf zb;
325 void *newbuf;
326 int nbuf = 1;
327 size_t bufsize = GOT_INFLATE_BUFSIZE;
329 /* Optimize buffer size in case short reads should suffice. */
330 if (expected_size > 0 && expected_size < bufsize)
331 bufsize = expected_size;
333 if (outbuf) {
334 *outbuf = malloc(bufsize);
335 if (*outbuf == NULL)
336 return got_error_from_errno("malloc");
337 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
338 } else
339 err = got_inflate_init(&zb, NULL, bufsize, csum);
340 if (err)
341 goto done;
343 *outlen = 0;
344 if (consumed_total)
345 *consumed_total = 0;
347 do {
348 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
349 if (err)
350 goto done;
351 *outlen += avail;
352 if (consumed_total)
353 *consumed_total += consumed;
354 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
355 if (outbuf == NULL)
356 continue;
357 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
358 newbuf = reallocarray(*outbuf, ++nbuf,
359 GOT_INFLATE_BUFSIZE);
360 if (newbuf == NULL) {
361 err = got_error_from_errno("reallocarray");
362 free(*outbuf);
363 *outbuf = NULL;
364 *outlen = 0;
365 goto done;
367 *outbuf = newbuf;
368 zb.outbuf = newbuf + *outlen;
370 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
372 done:
373 got_inflate_end(&zb);
374 return err;
377 const struct got_error *
378 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
379 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
380 size_t offset, size_t len)
382 const struct got_error *err;
383 size_t avail, consumed;
384 struct got_inflate_buf zb;
385 void *newbuf;
386 int nbuf = 1;
388 if (outbuf) {
389 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
390 if (*outbuf == NULL)
391 return got_error_from_errno("malloc");
392 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
393 if (err) {
394 free(*outbuf);
395 *outbuf = NULL;
396 return err;
398 } else {
399 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
402 *outlen = 0;
403 if (consumed_total)
404 *consumed_total = 0;
405 do {
406 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
407 &consumed);
408 if (err)
409 goto done;
410 offset += consumed;
411 if (consumed_total)
412 *consumed_total += consumed;
413 len -= consumed;
414 *outlen += avail;
415 if (len == 0)
416 break;
417 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
418 if (outbuf == NULL)
419 continue;
420 newbuf = reallocarray(*outbuf, ++nbuf,
421 GOT_INFLATE_BUFSIZE);
422 if (newbuf == NULL) {
423 err = got_error_from_errno("reallocarray");
424 free(*outbuf);
425 *outbuf = NULL;
426 *outlen = 0;
427 goto done;
429 *outbuf = newbuf;
430 zb.outbuf = newbuf + *outlen;
431 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
433 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
434 done:
435 got_inflate_end(&zb);
436 return err;
439 const struct got_error *
440 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
442 const struct got_error *err = NULL;
443 size_t avail;
444 struct got_inflate_buf zb;
446 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
447 if (err)
448 goto done;
450 *outlen = 0;
452 do {
453 err = got_inflate_read(&zb, infile, &avail, NULL);
454 if (err)
455 goto done;
456 if (avail > 0) {
457 ssize_t n;
458 n = write(outfd, zb.outbuf, avail);
459 if (n != avail) {
460 err = got_error_from_errno("write");
461 goto done;
463 *outlen += avail;
465 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
467 done:
468 if (err == NULL) {
469 if (lseek(outfd, SEEK_SET, 0) == -1)
470 err = got_error_from_errno("lseek");
472 got_inflate_end(&zb);
473 return err;
476 const struct got_error *
477 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
479 const struct got_error *err;
480 size_t avail;
481 struct got_inflate_buf zb;
483 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
484 if (err)
485 goto done;
487 *outlen = 0;
489 do {
490 err = got_inflate_read(&zb, infile, &avail, NULL);
491 if (err)
492 goto done;
493 if (avail > 0) {
494 size_t n;
495 n = fwrite(zb.outbuf, avail, 1, outfile);
496 if (n != 1) {
497 err = got_ferror(outfile, GOT_ERR_IO);
498 goto done;
500 *outlen += avail;
502 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
504 done:
505 if (err == NULL)
506 rewind(outfile);
507 got_inflate_end(&zb);
508 return err;
511 const struct got_error *
512 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
513 struct got_inflate_checksum *csum, int infd, FILE *outfile)
515 const struct got_error *err;
516 size_t avail, consumed;
517 struct got_inflate_buf zb;
519 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
520 if (err)
521 goto done;
523 *outlen = 0;
524 if (consumed_total)
525 *consumed_total = 0;
526 do {
527 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
528 if (err)
529 goto done;
530 if (avail > 0) {
531 size_t n;
532 n = fwrite(zb.outbuf, avail, 1, outfile);
533 if (n != 1) {
534 err = got_ferror(outfile, GOT_ERR_IO);
535 goto done;
537 *outlen += avail;
538 if (consumed_total)
539 *consumed_total += consumed;
541 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
543 done:
544 if (err == NULL)
545 rewind(outfile);
546 got_inflate_end(&zb);
547 return err;
550 const struct got_error *
551 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
552 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
553 size_t len, FILE *outfile)
555 const struct got_error *err;
556 size_t avail, consumed;
557 struct got_inflate_buf zb;
559 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
560 if (err)
561 goto done;
563 *outlen = 0;
564 if (consumed_total)
565 *consumed_total = 0;
566 do {
567 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
568 &consumed);
569 if (err)
570 goto done;
571 offset += consumed;
572 if (consumed_total)
573 *consumed_total += consumed;
574 len -= consumed;
575 if (avail > 0) {
576 size_t n;
577 n = fwrite(zb.outbuf, avail, 1, outfile);
578 if (n != 1) {
579 err = got_ferror(outfile, GOT_ERR_IO);
580 goto done;
582 *outlen += avail;
584 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
586 done:
587 if (err == NULL)
588 rewind(outfile);
589 got_inflate_end(&zb);
590 return err;