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 if (len - *consumed > UINT_MAX)
253 z->avail_in = UINT_MAX;
254 else
255 z->avail_in = len - *consumed;
257 if (zb->csum) {
258 csum_in = z->next_in;
259 csum_avail_in = z->avail_in;
260 csum_out = z->next_out;
261 csum_avail_out = z->avail_out;
263 ret = inflate(z, Z_SYNC_FLUSH);
264 if (zb->csum) {
265 csum_input(zb->csum, csum_in,
266 csum_avail_in - z->avail_in);
267 csum_output(zb->csum, csum_out,
268 csum_avail_out - z->avail_out);
270 *consumed += z->total_in - last_total_in;
271 } while (ret == Z_OK && z->avail_out > 0);
273 if (ret == Z_OK || ret == Z_BUF_ERROR) {
274 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
275 } else {
276 if (ret != Z_STREAM_END)
277 return got_error(GOT_ERR_DECOMPRESSION);
278 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
281 *outlenp = z->total_out - last_total_out;
282 return NULL;
285 void
286 got_inflate_end(struct got_inflate_buf *zb)
288 free(zb->inbuf);
289 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
290 free(zb->outbuf);
291 inflateEnd(&zb->z);
294 const struct got_error *
295 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
296 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
298 const struct got_error *err;
299 size_t avail, consumed;
300 struct got_inflate_buf zb;
301 void *newbuf;
302 int nbuf = 1;
304 if (outbuf) {
305 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
306 if (*outbuf == NULL)
307 return got_error_from_errno("malloc");
308 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
309 } else
310 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
311 if (err)
312 return err;
314 *outlen = 0;
315 if (consumed_total)
316 *consumed_total = 0;
318 do {
319 err = got_inflate_read(&zb, f, &avail, &consumed);
320 if (err)
321 goto done;
322 *outlen += avail;
323 if (consumed_total)
324 *consumed_total += consumed;
325 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
326 if (outbuf == NULL)
327 continue;
328 newbuf = reallocarray(*outbuf, ++nbuf,
329 GOT_INFLATE_BUFSIZE);
330 if (newbuf == NULL) {
331 err = got_error_from_errno("reallocarray");
332 free(*outbuf);
333 *outbuf = NULL;
334 *outlen = 0;
335 goto done;
337 *outbuf = newbuf;
338 zb.outbuf = newbuf + *outlen;
339 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
341 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
343 done:
344 got_inflate_end(&zb);
345 return err;
348 const struct got_error *
349 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
350 size_t *consumed_total, struct got_inflate_checksum *csum,
351 size_t expected_size, int infd)
353 const struct got_error *err;
354 size_t avail, consumed;
355 struct got_inflate_buf zb;
356 void *newbuf;
357 int nbuf = 1;
358 size_t bufsize = GOT_INFLATE_BUFSIZE;
360 /* Optimize buffer size in case short reads should suffice. */
361 if (expected_size > 0 && expected_size < bufsize)
362 bufsize = expected_size;
364 if (outbuf) {
365 *outbuf = malloc(bufsize);
366 if (*outbuf == NULL)
367 return got_error_from_errno("malloc");
368 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
369 } else
370 err = got_inflate_init(&zb, NULL, bufsize, csum);
371 if (err)
372 goto done;
374 *outlen = 0;
375 if (consumed_total)
376 *consumed_total = 0;
378 do {
379 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
380 if (err)
381 goto done;
382 *outlen += avail;
383 if (consumed_total)
384 *consumed_total += consumed;
385 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
386 if (outbuf == NULL)
387 continue;
388 newbuf = reallocarray(*outbuf, ++nbuf,
389 GOT_INFLATE_BUFSIZE);
390 if (newbuf == NULL) {
391 err = got_error_from_errno("reallocarray");
392 free(*outbuf);
393 *outbuf = NULL;
394 *outlen = 0;
395 goto done;
397 *outbuf = newbuf;
398 zb.outbuf = newbuf + *outlen;
399 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
401 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
403 done:
404 got_inflate_end(&zb);
405 return err;
408 const struct got_error *
409 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
410 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
411 size_t offset, size_t len)
413 const struct got_error *err;
414 size_t avail, consumed;
415 struct got_inflate_buf zb;
416 void *newbuf;
417 int nbuf = 1;
419 if (outbuf) {
420 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
421 if (*outbuf == NULL)
422 return got_error_from_errno("malloc");
423 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
424 if (err) {
425 free(*outbuf);
426 *outbuf = NULL;
427 return err;
429 } else {
430 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
431 if (err)
432 return err;
435 *outlen = 0;
436 if (consumed_total)
437 *consumed_total = 0;
438 do {
439 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
440 &consumed);
441 if (err)
442 goto done;
443 offset += consumed;
444 if (consumed_total)
445 *consumed_total += consumed;
446 len -= consumed;
447 *outlen += avail;
448 if (len == 0)
449 break;
450 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
451 if (outbuf == NULL)
452 continue;
453 newbuf = reallocarray(*outbuf, ++nbuf,
454 GOT_INFLATE_BUFSIZE);
455 if (newbuf == NULL) {
456 err = got_error_from_errno("reallocarray");
457 free(*outbuf);
458 *outbuf = NULL;
459 *outlen = 0;
460 goto done;
462 *outbuf = newbuf;
463 zb.outbuf = newbuf + *outlen;
464 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
466 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
467 done:
468 got_inflate_end(&zb);
469 return err;
472 const struct got_error *
473 got_inflate_to_fd(size_t *outlen, FILE *infile,
474 struct got_inflate_checksum *csum, int outfd)
476 const struct got_error *err = NULL;
477 size_t avail;
478 struct got_inflate_buf zb;
480 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
481 if (err)
482 goto done;
484 *outlen = 0;
486 do {
487 err = got_inflate_read(&zb, infile, &avail, NULL);
488 if (err)
489 goto done;
490 if (avail > 0) {
491 ssize_t n;
492 n = write(outfd, zb.outbuf, avail);
493 if (n != avail) {
494 err = got_error_from_errno("write");
495 goto done;
497 *outlen += avail;
499 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
501 done:
502 if (err == NULL) {
503 if (lseek(outfd, SEEK_SET, 0) == -1)
504 err = got_error_from_errno("lseek");
506 got_inflate_end(&zb);
507 return err;
510 const struct got_error *
511 got_inflate_to_file(size_t *outlen, FILE *infile,
512 struct got_inflate_checksum *csum, FILE *outfile)
514 const struct got_error *err;
515 size_t avail;
516 struct got_inflate_buf zb;
518 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
519 if (err)
520 goto done;
522 *outlen = 0;
524 do {
525 err = got_inflate_read(&zb, infile, &avail, NULL);
526 if (err)
527 goto done;
528 if (avail > 0) {
529 size_t n;
530 n = fwrite(zb.outbuf, avail, 1, outfile);
531 if (n != 1) {
532 err = got_ferror(outfile, GOT_ERR_IO);
533 goto done;
535 *outlen += avail;
537 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
539 done:
540 if (err == NULL)
541 rewind(outfile);
542 got_inflate_end(&zb);
543 return err;
546 const struct got_error *
547 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
548 struct got_inflate_checksum *csum, int infd, FILE *outfile)
550 const struct got_error *err;
551 size_t avail, consumed;
552 struct got_inflate_buf zb;
554 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
555 if (err)
556 goto done;
558 *outlen = 0;
559 if (consumed_total)
560 *consumed_total = 0;
561 do {
562 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
563 if (err)
564 goto done;
565 if (avail > 0) {
566 size_t n;
567 n = fwrite(zb.outbuf, avail, 1, outfile);
568 if (n != 1) {
569 err = got_ferror(outfile, GOT_ERR_IO);
570 goto done;
572 *outlen += avail;
573 if (consumed_total)
574 *consumed_total += consumed;
576 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
578 done:
579 if (err == NULL)
580 rewind(outfile);
581 got_inflate_end(&zb);
582 return err;
585 const struct got_error *
586 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
587 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
588 size_t len, FILE *outfile)
590 const struct got_error *err;
591 size_t avail, consumed;
592 struct got_inflate_buf zb;
594 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
595 if (err)
596 goto done;
598 *outlen = 0;
599 if (consumed_total)
600 *consumed_total = 0;
601 do {
602 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
603 &consumed);
604 if (err)
605 goto done;
606 offset += consumed;
607 if (consumed_total)
608 *consumed_total += consumed;
609 len -= consumed;
610 if (avail > 0) {
611 size_t n;
612 n = fwrite(zb.outbuf, avail, 1, outfile);
613 if (n != 1) {
614 err = got_ferror(outfile, GOT_ERR_IO);
615 goto done;
617 *outlen += avail;
619 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
621 done:
622 if (err == NULL)
623 rewind(outfile);
624 got_inflate_end(&zb);
625 return err;