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 <unistd.h>
24 #include <zlib.h>
25 #include <time.h>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_path.h"
31 #include "got_lib_inflate.h"
33 #ifndef MIN
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 #endif
37 const struct got_error *
38 got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
39 struct got_inflate_checksum *csum)
40 {
41 const struct got_error *err = NULL;
42 int zerr;
44 memset(&zb->z, 0, sizeof(zb->z));
46 zb->z.zalloc = Z_NULL;
47 zb->z.zfree = Z_NULL;
48 zerr = inflateInit(&zb->z);
49 if (zerr != Z_OK) {
50 if (zerr == Z_ERRNO)
51 return got_error_from_errno("inflateInit");
52 if (zerr == Z_MEM_ERROR) {
53 errno = ENOMEM;
54 return got_error_from_errno("inflateInit");
55 }
56 return got_error(GOT_ERR_DECOMPRESSION);
57 }
59 zb->inlen = zb->outlen = bufsize;
61 zb->inbuf = calloc(1, zb->inlen);
62 if (zb->inbuf == NULL) {
63 err = got_error_from_errno("calloc");
64 goto done;
65 }
67 zb->flags = 0;
68 if (outbuf == NULL) {
69 zb->outbuf = calloc(1, zb->outlen);
70 if (zb->outbuf == NULL) {
71 err = got_error_from_errno("calloc");
72 goto done;
73 }
74 zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
75 } else
76 zb->outbuf = outbuf;
78 zb->csum = csum;
79 done:
80 if (err)
81 got_inflate_end(zb);
82 return err;
83 }
85 static void
86 csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
87 {
88 if (csum->input_crc)
89 *csum->input_crc = crc32(*csum->input_crc, buf, len);
91 if (csum->input_sha1)
92 SHA1Update(csum->input_sha1, buf, len);
93 }
95 static void
96 csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
97 {
98 if (csum->output_crc)
99 *csum->output_crc = crc32(*csum->output_crc, buf, len);
101 if (csum->output_sha1)
102 SHA1Update(csum->output_sha1, buf, len);
105 const struct got_error *
106 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
107 size_t *consumed)
109 size_t last_total_out = zb->z.total_out;
110 size_t last_total_in = zb->z.total_in;
111 z_stream *z = &zb->z;
112 int ret = Z_ERRNO;
114 z->next_out = zb->outbuf;
115 z->avail_out = zb->outlen;
117 *outlenp = 0;
118 if (consumed)
119 *consumed = 0;
120 do {
121 uint8_t *csum_in = NULL, *csum_out = NULL;
122 size_t csum_avail_in = 0, csum_avail_out = 0;
124 if (z->avail_in == 0) {
125 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
126 if (n == 0) {
127 if (ferror(f))
128 return got_ferror(f, GOT_ERR_IO);
129 /* EOF */
130 ret = Z_STREAM_END;
131 break;
133 z->next_in = zb->inbuf;
134 z->avail_in = n;
136 if (zb->csum) {
137 csum_in = z->next_in;
138 csum_avail_in = z->avail_in;
139 csum_out = z->next_out;
140 csum_avail_out = z->avail_out;
142 ret = inflate(z, Z_SYNC_FLUSH);
143 if (zb->csum) {
144 csum_input(zb->csum, csum_in,
145 csum_avail_in - z->avail_in);
146 csum_output(zb->csum, csum_out,
147 csum_avail_out - z->avail_out);
149 } while (ret == Z_OK && z->avail_out > 0);
151 if (ret == Z_OK || ret == Z_BUF_ERROR) {
152 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
153 } else {
154 if (ret != Z_STREAM_END)
155 return got_error(GOT_ERR_DECOMPRESSION);
156 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
159 *outlenp = z->total_out - last_total_out;
160 if (consumed)
161 *consumed += z->total_in - last_total_in;
162 return NULL;
165 const struct got_error *
166 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
167 size_t *consumed)
169 size_t last_total_out = zb->z.total_out;
170 size_t last_total_in = zb->z.total_in;
171 z_stream *z = &zb->z;
172 int ret = Z_ERRNO;
174 z->next_out = zb->outbuf;
175 z->avail_out = zb->outlen;
177 *outlenp = 0;
178 if (consumed)
179 *consumed = 0;
180 do {
181 uint8_t *csum_in = NULL, *csum_out = NULL;
182 size_t csum_avail_in = 0, csum_avail_out = 0;
184 if (z->avail_in == 0) {
185 ssize_t n = read(fd, zb->inbuf, zb->inlen);
186 if (n < 0)
187 return got_error_from_errno("read");
188 else if (n == 0) {
189 /* EOF */
190 ret = Z_STREAM_END;
191 break;
193 z->next_in = zb->inbuf;
194 z->avail_in = n;
196 if (zb->csum) {
197 csum_in = z->next_in;
198 csum_avail_in = z->avail_in;
199 csum_out = z->next_out;
200 csum_avail_out = z->avail_out;
202 ret = inflate(z, Z_SYNC_FLUSH);
203 if (zb->csum) {
204 csum_input(zb->csum, csum_in,
205 csum_avail_in - z->avail_in);
206 csum_output(zb->csum, csum_out,
207 csum_avail_out - z->avail_out);
209 } while (ret == Z_OK && z->avail_out > 0);
211 if (ret == Z_OK || ret == Z_BUF_ERROR) {
212 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
213 } else {
214 if (ret != Z_STREAM_END)
215 return got_error(GOT_ERR_DECOMPRESSION);
216 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
219 *outlenp = z->total_out - last_total_out;
220 if (consumed)
221 *consumed += z->total_in - last_total_in;
222 return NULL;
225 const struct got_error *
226 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
227 size_t len, size_t *outlenp, size_t *consumed)
229 size_t last_total_out = zb->z.total_out;
230 z_stream *z = &zb->z;
231 int ret = Z_ERRNO;
233 z->next_out = zb->outbuf;
234 z->avail_out = zb->outlen;
236 *outlenp = 0;
237 *consumed = 0;
239 do {
240 uint8_t *csum_in = NULL, *csum_out = NULL;
241 size_t csum_avail_in = 0, csum_avail_out = 0;
242 size_t last_total_in = zb->z.total_in;
244 if (z->avail_in == 0) {
245 if (len == 0) {
246 /* EOF */
247 ret = Z_STREAM_END;
248 break;
250 z->next_in = map + offset + *consumed;
251 if (len - *consumed > UINT_MAX)
252 z->avail_in = UINT_MAX;
253 else
254 z->avail_in = len - *consumed;
256 if (zb->csum) {
257 csum_in = z->next_in;
258 csum_avail_in = z->avail_in;
259 csum_out = z->next_out;
260 csum_avail_out = z->avail_out;
262 ret = inflate(z, Z_SYNC_FLUSH);
263 if (zb->csum) {
264 csum_input(zb->csum, csum_in,
265 csum_avail_in - z->avail_in);
266 csum_output(zb->csum, csum_out,
267 csum_avail_out - z->avail_out);
269 *consumed += z->total_in - last_total_in;
270 } while (ret == Z_OK && z->avail_out > 0);
272 if (ret == Z_OK || ret == Z_BUF_ERROR) {
273 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
274 } else {
275 if (ret != Z_STREAM_END)
276 return got_error(GOT_ERR_DECOMPRESSION);
277 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
280 *outlenp = z->total_out - last_total_out;
281 return NULL;
284 void
285 got_inflate_end(struct got_inflate_buf *zb)
287 free(zb->inbuf);
288 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
289 free(zb->outbuf);
290 inflateEnd(&zb->z);
293 const struct got_error *
294 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
295 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
297 const struct got_error *err;
298 size_t avail, consumed;
299 struct got_inflate_buf zb;
300 void *newbuf;
301 int nbuf = 1;
303 if (outbuf) {
304 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
305 if (*outbuf == NULL)
306 return got_error_from_errno("malloc");
307 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
308 } else
309 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
310 if (err)
311 return err;
313 *outlen = 0;
314 if (consumed_total)
315 *consumed_total = 0;
317 do {
318 err = got_inflate_read(&zb, f, &avail, &consumed);
319 if (err)
320 goto done;
321 *outlen += avail;
322 if (consumed_total)
323 *consumed_total += consumed;
324 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
325 if (outbuf == NULL)
326 continue;
327 newbuf = reallocarray(*outbuf, ++nbuf,
328 GOT_INFLATE_BUFSIZE);
329 if (newbuf == NULL) {
330 err = got_error_from_errno("reallocarray");
331 free(*outbuf);
332 *outbuf = NULL;
333 *outlen = 0;
334 goto done;
336 *outbuf = newbuf;
337 zb.outbuf = newbuf + *outlen;
338 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
340 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
342 done:
343 got_inflate_end(&zb);
344 return err;
347 const struct got_error *
348 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
349 size_t *consumed_total, struct got_inflate_checksum *csum,
350 size_t expected_size, int infd)
352 const struct got_error *err;
353 size_t avail, consumed;
354 struct got_inflate_buf zb;
355 void *newbuf;
356 int nbuf = 1;
357 size_t bufsize = GOT_INFLATE_BUFSIZE;
359 /* Optimize buffer size in case short reads should suffice. */
360 if (expected_size > 0 && expected_size < bufsize)
361 bufsize = expected_size;
363 if (outbuf) {
364 *outbuf = malloc(bufsize);
365 if (*outbuf == NULL)
366 return got_error_from_errno("malloc");
367 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
368 } else
369 err = got_inflate_init(&zb, NULL, bufsize, csum);
370 if (err)
371 goto done;
373 *outlen = 0;
374 if (consumed_total)
375 *consumed_total = 0;
377 do {
378 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
379 if (err)
380 goto done;
381 *outlen += avail;
382 if (consumed_total)
383 *consumed_total += consumed;
384 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
385 if (outbuf == NULL)
386 continue;
387 newbuf = reallocarray(*outbuf, ++nbuf,
388 GOT_INFLATE_BUFSIZE);
389 if (newbuf == NULL) {
390 err = got_error_from_errno("reallocarray");
391 free(*outbuf);
392 *outbuf = NULL;
393 *outlen = 0;
394 goto done;
396 *outbuf = newbuf;
397 zb.outbuf = newbuf + *outlen;
398 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
400 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
402 done:
403 got_inflate_end(&zb);
404 return err;
407 const struct got_error *
408 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
409 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
410 size_t offset, size_t len)
412 const struct got_error *err;
413 size_t avail, consumed;
414 struct got_inflate_buf zb;
415 void *newbuf;
416 int nbuf = 1;
418 if (outbuf) {
419 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
420 if (*outbuf == NULL)
421 return got_error_from_errno("malloc");
422 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
423 if (err) {
424 free(*outbuf);
425 *outbuf = NULL;
426 return err;
428 } else {
429 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
430 if (err)
431 return err;
434 *outlen = 0;
435 if (consumed_total)
436 *consumed_total = 0;
437 do {
438 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
439 &consumed);
440 if (err)
441 goto done;
442 offset += consumed;
443 if (consumed_total)
444 *consumed_total += consumed;
445 len -= consumed;
446 *outlen += avail;
447 if (len == 0)
448 break;
449 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
450 if (outbuf == NULL)
451 continue;
452 newbuf = reallocarray(*outbuf, ++nbuf,
453 GOT_INFLATE_BUFSIZE);
454 if (newbuf == NULL) {
455 err = got_error_from_errno("reallocarray");
456 free(*outbuf);
457 *outbuf = NULL;
458 *outlen = 0;
459 goto done;
461 *outbuf = newbuf;
462 zb.outbuf = newbuf + *outlen;
463 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
465 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
466 done:
467 got_inflate_end(&zb);
468 return err;
471 const struct got_error *
472 got_inflate_to_fd(size_t *outlen, FILE *infile,
473 struct got_inflate_checksum *csum, int outfd)
475 const struct got_error *err = NULL;
476 size_t avail;
477 struct got_inflate_buf zb;
479 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
480 if (err)
481 goto done;
483 *outlen = 0;
485 do {
486 err = got_inflate_read(&zb, infile, &avail, NULL);
487 if (err)
488 goto done;
489 if (avail > 0) {
490 ssize_t n;
491 n = write(outfd, zb.outbuf, avail);
492 if (n != avail) {
493 err = got_error_from_errno("write");
494 goto done;
496 *outlen += avail;
498 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
500 done:
501 if (err == NULL) {
502 if (lseek(outfd, SEEK_SET, 0) == -1)
503 err = got_error_from_errno("lseek");
505 got_inflate_end(&zb);
506 return err;
509 const struct got_error *
510 got_inflate_to_file(size_t *outlen, FILE *infile,
511 struct got_inflate_checksum *csum, FILE *outfile)
513 const struct got_error *err;
514 size_t avail;
515 struct got_inflate_buf zb;
517 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
518 if (err)
519 goto done;
521 *outlen = 0;
523 do {
524 err = got_inflate_read(&zb, infile, &avail, NULL);
525 if (err)
526 goto done;
527 if (avail > 0) {
528 size_t n;
529 n = fwrite(zb.outbuf, avail, 1, outfile);
530 if (n != 1) {
531 err = got_ferror(outfile, GOT_ERR_IO);
532 goto done;
534 *outlen += avail;
536 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
538 done:
539 if (err == NULL)
540 rewind(outfile);
541 got_inflate_end(&zb);
542 return err;
545 const struct got_error *
546 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
547 struct got_inflate_checksum *csum, int infd, FILE *outfile)
549 const struct got_error *err;
550 size_t avail, consumed;
551 struct got_inflate_buf zb;
553 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
554 if (err)
555 goto done;
557 *outlen = 0;
558 if (consumed_total)
559 *consumed_total = 0;
560 do {
561 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
562 if (err)
563 goto done;
564 if (avail > 0) {
565 size_t n;
566 n = fwrite(zb.outbuf, avail, 1, outfile);
567 if (n != 1) {
568 err = got_ferror(outfile, GOT_ERR_IO);
569 goto done;
571 *outlen += avail;
572 if (consumed_total)
573 *consumed_total += consumed;
575 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
577 done:
578 if (err == NULL)
579 rewind(outfile);
580 got_inflate_end(&zb);
581 return err;
584 const struct got_error *
585 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
586 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
587 size_t len, FILE *outfile)
589 const struct got_error *err;
590 size_t avail, consumed;
591 struct got_inflate_buf zb;
593 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
594 if (err)
595 goto done;
597 *outlen = 0;
598 if (consumed_total)
599 *consumed_total = 0;
600 do {
601 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
602 &consumed);
603 if (err)
604 goto done;
605 offset += consumed;
606 if (consumed_total)
607 *consumed_total += consumed;
608 len -= consumed;
609 if (avail > 0) {
610 size_t n;
611 n = fwrite(zb.outbuf, avail, 1, outfile);
612 if (n != 1) {
613 err = got_ferror(outfile, GOT_ERR_IO);
614 goto done;
616 *outlen += avail;
618 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
620 done:
621 if (err == NULL)
622 rewind(outfile);
623 got_inflate_end(&zb);
624 return err;