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 uint8_t *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 static void
97 csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
98 {
99 if (csum->output_crc)
100 *csum->output_crc = crc32(*csum->output_crc, buf, len);
102 if (csum->output_sha1)
103 SHA1Update(csum->output_sha1, buf, len);
106 const struct got_error *
107 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
108 size_t *consumed)
110 size_t last_total_out = zb->z.total_out;
111 size_t last_total_in = zb->z.total_in;
112 z_stream *z = &zb->z;
113 int ret = Z_ERRNO;
115 z->next_out = zb->outbuf;
116 z->avail_out = zb->outlen;
118 *outlenp = 0;
119 if (consumed)
120 *consumed = 0;
121 do {
122 uint8_t *csum_in = NULL, *csum_out = NULL;
123 size_t csum_avail_in = 0, csum_avail_out = 0;
125 if (z->avail_in == 0) {
126 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
127 if (n == 0) {
128 if (ferror(f))
129 return got_ferror(f, GOT_ERR_IO);
130 /* EOF */
131 ret = Z_STREAM_END;
132 break;
134 z->next_in = zb->inbuf;
135 z->avail_in = n;
137 if (zb->csum) {
138 csum_in = z->next_in;
139 csum_avail_in = z->avail_in;
140 csum_out = z->next_out;
141 csum_avail_out = z->avail_out;
143 ret = inflate(z, Z_SYNC_FLUSH);
144 if (zb->csum) {
145 csum_input(zb->csum, csum_in,
146 csum_avail_in - z->avail_in);
147 csum_output(zb->csum, csum_out,
148 csum_avail_out - z->avail_out);
150 } while (ret == Z_OK && z->avail_out > 0);
152 if (ret == Z_OK || ret == Z_BUF_ERROR) {
153 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
154 } else {
155 if (ret != Z_STREAM_END)
156 return got_error(GOT_ERR_DECOMPRESSION);
157 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
160 *outlenp = z->total_out - last_total_out;
161 if (consumed)
162 *consumed += z->total_in - last_total_in;
163 return NULL;
166 const struct got_error *
167 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
168 size_t *consumed)
170 size_t last_total_out = zb->z.total_out;
171 size_t last_total_in = zb->z.total_in;
172 z_stream *z = &zb->z;
173 int ret = Z_ERRNO;
175 z->next_out = zb->outbuf;
176 z->avail_out = zb->outlen;
178 *outlenp = 0;
179 if (consumed)
180 *consumed = 0;
181 do {
182 uint8_t *csum_in = NULL, *csum_out = NULL;
183 size_t csum_avail_in = 0, csum_avail_out = 0;
185 if (z->avail_in == 0) {
186 ssize_t n = read(fd, zb->inbuf, zb->inlen);
187 if (n < 0)
188 return got_error_from_errno("read");
189 else if (n == 0) {
190 /* EOF */
191 ret = Z_STREAM_END;
192 break;
194 z->next_in = zb->inbuf;
195 z->avail_in = n;
197 if (zb->csum) {
198 csum_in = z->next_in;
199 csum_avail_in = z->avail_in;
200 csum_out = z->next_out;
201 csum_avail_out = z->avail_out;
203 ret = inflate(z, Z_SYNC_FLUSH);
204 if (zb->csum) {
205 csum_input(zb->csum, csum_in,
206 csum_avail_in - z->avail_in);
207 csum_output(zb->csum, csum_out,
208 csum_avail_out - z->avail_out);
210 } while (ret == Z_OK && z->avail_out > 0);
212 if (ret == Z_OK || ret == Z_BUF_ERROR) {
213 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
214 } else {
215 if (ret != Z_STREAM_END)
216 return got_error(GOT_ERR_DECOMPRESSION);
217 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
220 *outlenp = z->total_out - last_total_out;
221 if (consumed)
222 *consumed += z->total_in - last_total_in;
223 return NULL;
226 const struct got_error *
227 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
228 size_t len, size_t *outlenp, size_t *consumed)
230 size_t last_total_out = zb->z.total_out;
231 z_stream *z = &zb->z;
232 int ret = Z_ERRNO;
234 z->next_out = zb->outbuf;
235 z->avail_out = zb->outlen;
237 *outlenp = 0;
238 *consumed = 0;
240 do {
241 uint8_t *csum_in = NULL, *csum_out = NULL;
242 size_t csum_avail_in = 0, csum_avail_out = 0;
243 size_t last_total_in = zb->z.total_in;
245 if (z->avail_in == 0) {
246 if (len == 0) {
247 /* EOF */
248 ret = Z_STREAM_END;
249 break;
251 z->next_in = map + offset + *consumed;
252 z->avail_in = len - *consumed;
254 if (zb->csum) {
255 csum_in = z->next_in;
256 csum_avail_in = z->avail_in;
257 csum_out = z->next_out;
258 csum_avail_out = z->avail_out;
260 ret = inflate(z, Z_SYNC_FLUSH);
261 if (zb->csum) {
262 csum_input(zb->csum, csum_in,
263 csum_avail_in - z->avail_in);
264 csum_output(zb->csum, csum_out,
265 csum_avail_out - z->avail_out);
267 *consumed += z->total_in - last_total_in;
268 } while (ret == Z_OK && z->avail_out > 0);
270 if (ret == Z_OK || ret == Z_BUF_ERROR) {
271 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
272 } else {
273 if (ret != Z_STREAM_END)
274 return got_error(GOT_ERR_DECOMPRESSION);
275 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
278 *outlenp = z->total_out - last_total_out;
279 return NULL;
282 void
283 got_inflate_end(struct got_inflate_buf *zb)
285 free(zb->inbuf);
286 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
287 free(zb->outbuf);
288 inflateEnd(&zb->z);
291 const struct got_error *
292 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
293 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
295 const struct got_error *err;
296 size_t avail, consumed;
297 struct got_inflate_buf zb;
298 void *newbuf;
299 int nbuf = 1;
301 if (outbuf) {
302 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
303 if (*outbuf == NULL)
304 return got_error_from_errno("malloc");
305 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
306 } else
307 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
308 if (err)
309 return err;
311 *outlen = 0;
312 if (consumed_total)
313 *consumed_total = 0;
315 do {
316 err = got_inflate_read(&zb, f, &avail, &consumed);
317 if (err)
318 goto done;
319 *outlen += avail;
320 if (consumed_total)
321 *consumed_total += consumed;
322 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
323 if (outbuf == NULL)
324 continue;
325 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
326 newbuf = reallocarray(*outbuf, ++nbuf,
327 GOT_INFLATE_BUFSIZE);
328 if (newbuf == NULL) {
329 err = got_error_from_errno("reallocarray");
330 free(*outbuf);
331 *outbuf = NULL;
332 *outlen = 0;
333 goto done;
335 *outbuf = newbuf;
336 zb.outbuf = newbuf + *outlen;
338 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
340 done:
341 got_inflate_end(&zb);
342 return err;
345 const struct got_error *
346 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
347 size_t *consumed_total, struct got_inflate_checksum *csum,
348 size_t expected_size, int infd)
350 const struct got_error *err;
351 size_t avail, consumed;
352 struct got_inflate_buf zb;
353 void *newbuf;
354 int nbuf = 1;
355 size_t bufsize = GOT_INFLATE_BUFSIZE;
357 /* Optimize buffer size in case short reads should suffice. */
358 if (expected_size > 0 && expected_size < bufsize)
359 bufsize = expected_size;
361 if (outbuf) {
362 *outbuf = malloc(bufsize);
363 if (*outbuf == NULL)
364 return got_error_from_errno("malloc");
365 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
366 } else
367 err = got_inflate_init(&zb, NULL, bufsize, csum);
368 if (err)
369 goto done;
371 *outlen = 0;
372 if (consumed_total)
373 *consumed_total = 0;
375 do {
376 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
377 if (err)
378 goto done;
379 *outlen += avail;
380 if (consumed_total)
381 *consumed_total += consumed;
382 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
383 if (outbuf == NULL)
384 continue;
385 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
386 newbuf = reallocarray(*outbuf, ++nbuf,
387 GOT_INFLATE_BUFSIZE);
388 if (newbuf == NULL) {
389 err = got_error_from_errno("reallocarray");
390 free(*outbuf);
391 *outbuf = NULL;
392 *outlen = 0;
393 goto done;
395 *outbuf = newbuf;
396 zb.outbuf = newbuf + *outlen;
398 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
400 done:
401 got_inflate_end(&zb);
402 return err;
405 const struct got_error *
406 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
407 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
408 size_t offset, size_t len)
410 const struct got_error *err;
411 size_t avail, consumed;
412 struct got_inflate_buf zb;
413 void *newbuf;
414 int nbuf = 1;
416 if (outbuf) {
417 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
418 if (*outbuf == NULL)
419 return got_error_from_errno("malloc");
420 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
421 if (err) {
422 free(*outbuf);
423 *outbuf = NULL;
424 return err;
426 } else {
427 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
430 *outlen = 0;
431 if (consumed_total)
432 *consumed_total = 0;
433 do {
434 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
435 &consumed);
436 if (err)
437 goto done;
438 offset += consumed;
439 if (consumed_total)
440 *consumed_total += consumed;
441 len -= consumed;
442 *outlen += avail;
443 if (len == 0)
444 break;
445 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
446 if (outbuf == NULL)
447 continue;
448 newbuf = reallocarray(*outbuf, ++nbuf,
449 GOT_INFLATE_BUFSIZE);
450 if (newbuf == NULL) {
451 err = got_error_from_errno("reallocarray");
452 free(*outbuf);
453 *outbuf = NULL;
454 *outlen = 0;
455 goto done;
457 *outbuf = newbuf;
458 zb.outbuf = newbuf + *outlen;
459 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
461 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
462 done:
463 got_inflate_end(&zb);
464 return err;
467 const struct got_error *
468 got_inflate_to_fd(size_t *outlen, FILE *infile,
469 struct got_inflate_checksum *csum, int outfd)
471 const struct got_error *err = NULL;
472 size_t avail;
473 struct got_inflate_buf zb;
475 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
476 if (err)
477 goto done;
479 *outlen = 0;
481 do {
482 err = got_inflate_read(&zb, infile, &avail, NULL);
483 if (err)
484 goto done;
485 if (avail > 0) {
486 ssize_t n;
487 n = write(outfd, zb.outbuf, avail);
488 if (n != avail) {
489 err = got_error_from_errno("write");
490 goto done;
492 *outlen += avail;
494 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
496 done:
497 if (err == NULL) {
498 if (lseek(outfd, SEEK_SET, 0) == -1)
499 err = got_error_from_errno("lseek");
501 got_inflate_end(&zb);
502 return err;
505 const struct got_error *
506 got_inflate_to_file(size_t *outlen, FILE *infile,
507 struct got_inflate_checksum *csum, FILE *outfile)
509 const struct got_error *err;
510 size_t avail;
511 struct got_inflate_buf zb;
513 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
514 if (err)
515 goto done;
517 *outlen = 0;
519 do {
520 err = got_inflate_read(&zb, infile, &avail, NULL);
521 if (err)
522 goto done;
523 if (avail > 0) {
524 size_t n;
525 n = fwrite(zb.outbuf, avail, 1, outfile);
526 if (n != 1) {
527 err = got_ferror(outfile, GOT_ERR_IO);
528 goto done;
530 *outlen += avail;
532 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
534 done:
535 if (err == NULL)
536 rewind(outfile);
537 got_inflate_end(&zb);
538 return err;
541 const struct got_error *
542 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
543 struct got_inflate_checksum *csum, int infd, FILE *outfile)
545 const struct got_error *err;
546 size_t avail, consumed;
547 struct got_inflate_buf zb;
549 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
550 if (err)
551 goto done;
553 *outlen = 0;
554 if (consumed_total)
555 *consumed_total = 0;
556 do {
557 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
558 if (err)
559 goto done;
560 if (avail > 0) {
561 size_t n;
562 n = fwrite(zb.outbuf, avail, 1, outfile);
563 if (n != 1) {
564 err = got_ferror(outfile, GOT_ERR_IO);
565 goto done;
567 *outlen += avail;
568 if (consumed_total)
569 *consumed_total += consumed;
571 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
573 done:
574 if (err == NULL)
575 rewind(outfile);
576 got_inflate_end(&zb);
577 return err;
580 const struct got_error *
581 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
582 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
583 size_t len, FILE *outfile)
585 const struct got_error *err;
586 size_t avail, consumed;
587 struct got_inflate_buf zb;
589 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
590 if (err)
591 goto done;
593 *outlen = 0;
594 if (consumed_total)
595 *consumed_total = 0;
596 do {
597 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
598 &consumed);
599 if (err)
600 goto done;
601 offset += consumed;
602 if (consumed_total)
603 *consumed_total += consumed;
604 len -= consumed;
605 if (avail > 0) {
606 size_t n;
607 n = fwrite(zb.outbuf, avail, 1, outfile);
608 if (n != 1) {
609 err = got_ferror(outfile, GOT_ERR_IO);
610 goto done;
612 *outlen += avail;
614 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
616 done:
617 if (err == NULL)
618 rewind(outfile);
619 got_inflate_end(&zb);
620 return err;