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 */
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <zlib.h>
24 #include <time.h>
26 #include "got_error.h"
27 #include "got_object.h"
28 #include "got_path.h"
30 #include "got_lib_inflate.h"
32 #ifndef MIN
33 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
34 #endif
36 const struct got_error *
37 got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
38 struct got_inflate_checksum *csum)
39 {
40 const struct got_error *err = NULL;
41 int zerr;
43 memset(&zb->z, 0, sizeof(zb->z));
45 zb->z.zalloc = Z_NULL;
46 zb->z.zfree = Z_NULL;
47 zerr = inflateInit(&zb->z);
48 if (zerr != Z_OK) {
49 if (zerr == Z_ERRNO)
50 return got_error_from_errno("inflateInit");
51 if (zerr == Z_MEM_ERROR) {
52 errno = ENOMEM;
53 return got_error_from_errno("inflateInit");
54 }
55 return got_error(GOT_ERR_DECOMPRESSION);
56 }
58 zb->inlen = zb->outlen = bufsize;
60 zb->inbuf = calloc(1, zb->inlen);
61 if (zb->inbuf == NULL) {
62 err = got_error_from_errno("calloc");
63 goto done;
64 }
66 zb->flags = 0;
67 if (outbuf == NULL) {
68 zb->outbuf = calloc(1, zb->outlen);
69 if (zb->outbuf == NULL) {
70 err = got_error_from_errno("calloc");
71 goto done;
72 }
73 zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
74 } else
75 zb->outbuf = outbuf;
77 zb->csum = csum;
78 done:
79 if (err)
80 got_inflate_end(zb);
81 return err;
82 }
84 static void
85 csum_input(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
86 {
87 if (csum->input_crc)
88 *csum->input_crc = crc32(*csum->input_crc, buf, len);
90 if (csum->input_sha1)
91 SHA1Update(csum->input_sha1, buf, len);
92 }
94 static void
95 csum_output(struct got_inflate_checksum *csum, const uint8_t *buf, size_t len)
96 {
97 if (csum->output_crc)
98 *csum->output_crc = crc32(*csum->output_crc, buf, len);
100 if (csum->output_sha1)
101 SHA1Update(csum->output_sha1, buf, len);
104 const struct got_error *
105 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
106 size_t *consumed)
108 size_t last_total_out = zb->z.total_out;
109 size_t last_total_in = zb->z.total_in;
110 z_stream *z = &zb->z;
111 int ret = Z_ERRNO;
113 z->next_out = zb->outbuf;
114 z->avail_out = zb->outlen;
116 *outlenp = 0;
117 if (consumed)
118 *consumed = 0;
119 do {
120 uint8_t *csum_in = NULL, *csum_out = NULL;
121 size_t csum_avail_in = 0, csum_avail_out = 0;
123 if (z->avail_in == 0) {
124 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
125 if (n == 0) {
126 if (ferror(f))
127 return got_ferror(f, GOT_ERR_IO);
128 /* EOF */
129 ret = Z_STREAM_END;
130 break;
132 z->next_in = zb->inbuf;
133 z->avail_in = n;
135 if (zb->csum) {
136 csum_in = z->next_in;
137 csum_avail_in = z->avail_in;
138 csum_out = z->next_out;
139 csum_avail_out = z->avail_out;
141 ret = inflate(z, Z_SYNC_FLUSH);
142 if (zb->csum) {
143 csum_input(zb->csum, csum_in,
144 csum_avail_in - z->avail_in);
145 csum_output(zb->csum, csum_out,
146 csum_avail_out - z->avail_out);
148 } while (ret == Z_OK && z->avail_out > 0);
150 if (ret == Z_OK || ret == Z_BUF_ERROR) {
151 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
152 } else {
153 if (ret != Z_STREAM_END)
154 return got_error(GOT_ERR_DECOMPRESSION);
155 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
158 *outlenp = z->total_out - last_total_out;
159 if (consumed)
160 *consumed += z->total_in - last_total_in;
161 return NULL;
164 const struct got_error *
165 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
166 size_t *consumed)
168 size_t last_total_out = zb->z.total_out;
169 size_t last_total_in = zb->z.total_in;
170 z_stream *z = &zb->z;
171 int ret = Z_ERRNO;
173 z->next_out = zb->outbuf;
174 z->avail_out = zb->outlen;
176 *outlenp = 0;
177 if (consumed)
178 *consumed = 0;
179 do {
180 uint8_t *csum_in = NULL, *csum_out = NULL;
181 size_t csum_avail_in = 0, csum_avail_out = 0;
183 if (z->avail_in == 0) {
184 ssize_t n = read(fd, zb->inbuf, zb->inlen);
185 if (n < 0)
186 return got_error_from_errno("read");
187 else if (n == 0) {
188 /* EOF */
189 ret = Z_STREAM_END;
190 break;
192 z->next_in = zb->inbuf;
193 z->avail_in = n;
195 if (zb->csum) {
196 csum_in = z->next_in;
197 csum_avail_in = z->avail_in;
198 csum_out = z->next_out;
199 csum_avail_out = z->avail_out;
201 ret = inflate(z, Z_SYNC_FLUSH);
202 if (zb->csum) {
203 csum_input(zb->csum, csum_in,
204 csum_avail_in - z->avail_in);
205 csum_output(zb->csum, csum_out,
206 csum_avail_out - z->avail_out);
208 } while (ret == Z_OK && z->avail_out > 0);
210 if (ret == Z_OK || ret == Z_BUF_ERROR) {
211 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
212 } else {
213 if (ret != Z_STREAM_END)
214 return got_error(GOT_ERR_DECOMPRESSION);
215 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
218 *outlenp = z->total_out - last_total_out;
219 if (consumed)
220 *consumed += z->total_in - last_total_in;
221 return NULL;
224 const struct got_error *
225 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
226 size_t len, size_t *outlenp, size_t *consumed)
228 size_t last_total_out = zb->z.total_out;
229 z_stream *z = &zb->z;
230 int ret = Z_ERRNO;
232 z->next_out = zb->outbuf;
233 z->avail_out = zb->outlen;
235 *outlenp = 0;
236 *consumed = 0;
238 do {
239 uint8_t *csum_in = NULL, *csum_out = NULL;
240 size_t csum_avail_in = 0, csum_avail_out = 0;
241 size_t last_total_in = zb->z.total_in;
243 if (z->avail_in == 0) {
244 if (len == 0) {
245 /* EOF */
246 ret = Z_STREAM_END;
247 break;
249 z->next_in = map + offset + *consumed;
250 z->avail_in = len - *consumed;
252 if (zb->csum) {
253 csum_in = z->next_in;
254 csum_avail_in = z->avail_in;
255 csum_out = z->next_out;
256 csum_avail_out = z->avail_out;
258 ret = inflate(z, Z_SYNC_FLUSH);
259 if (zb->csum) {
260 csum_input(zb->csum, csum_in,
261 csum_avail_in - z->avail_in);
262 csum_output(zb->csum, csum_out,
263 csum_avail_out - z->avail_out);
265 *consumed += z->total_in - last_total_in;
266 } while (ret == Z_OK && z->avail_out > 0);
268 if (ret == Z_OK || ret == Z_BUF_ERROR) {
269 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
270 } else {
271 if (ret != Z_STREAM_END)
272 return got_error(GOT_ERR_DECOMPRESSION);
273 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
276 *outlenp = z->total_out - last_total_out;
277 return NULL;
280 void
281 got_inflate_end(struct got_inflate_buf *zb)
283 free(zb->inbuf);
284 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
285 free(zb->outbuf);
286 inflateEnd(&zb->z);
289 const struct got_error *
290 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
291 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
293 const struct got_error *err;
294 size_t avail, consumed;
295 struct got_inflate_buf zb;
296 void *newbuf;
297 int nbuf = 1;
299 if (outbuf) {
300 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
301 if (*outbuf == NULL)
302 return got_error_from_errno("malloc");
303 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
304 } else
305 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
306 if (err)
307 return err;
309 *outlen = 0;
310 if (consumed_total)
311 *consumed_total = 0;
313 do {
314 err = got_inflate_read(&zb, f, &avail, &consumed);
315 if (err)
316 goto done;
317 *outlen += avail;
318 if (consumed_total)
319 *consumed_total += consumed;
320 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
321 if (outbuf == NULL)
322 continue;
323 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
324 newbuf = reallocarray(*outbuf, ++nbuf,
325 GOT_INFLATE_BUFSIZE);
326 if (newbuf == NULL) {
327 err = got_error_from_errno("reallocarray");
328 free(*outbuf);
329 *outbuf = NULL;
330 *outlen = 0;
331 goto done;
333 *outbuf = newbuf;
334 zb.outbuf = newbuf + *outlen;
336 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
338 done:
339 got_inflate_end(&zb);
340 return err;
343 const struct got_error *
344 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
345 size_t *consumed_total, struct got_inflate_checksum *csum,
346 size_t expected_size, int infd)
348 const struct got_error *err;
349 size_t avail, consumed;
350 struct got_inflate_buf zb;
351 void *newbuf;
352 int nbuf = 1;
353 size_t bufsize = GOT_INFLATE_BUFSIZE;
355 /* Optimize buffer size in case short reads should suffice. */
356 if (expected_size > 0 && expected_size < bufsize)
357 bufsize = expected_size;
359 if (outbuf) {
360 *outbuf = malloc(bufsize);
361 if (*outbuf == NULL)
362 return got_error_from_errno("malloc");
363 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
364 } else
365 err = got_inflate_init(&zb, NULL, bufsize, csum);
366 if (err)
367 goto done;
369 *outlen = 0;
370 if (consumed_total)
371 *consumed_total = 0;
373 do {
374 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
375 if (err)
376 goto done;
377 *outlen += avail;
378 if (consumed_total)
379 *consumed_total += consumed;
380 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
381 if (outbuf == NULL)
382 continue;
383 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
384 newbuf = reallocarray(*outbuf, ++nbuf,
385 GOT_INFLATE_BUFSIZE);
386 if (newbuf == NULL) {
387 err = got_error_from_errno("reallocarray");
388 free(*outbuf);
389 *outbuf = NULL;
390 *outlen = 0;
391 goto done;
393 *outbuf = newbuf;
394 zb.outbuf = newbuf + *outlen;
396 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
398 done:
399 got_inflate_end(&zb);
400 return err;
403 const struct got_error *
404 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
405 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
406 size_t offset, size_t len)
408 const struct got_error *err;
409 size_t avail, consumed;
410 struct got_inflate_buf zb;
411 void *newbuf;
412 int nbuf = 1;
414 if (outbuf) {
415 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
416 if (*outbuf == NULL)
417 return got_error_from_errno("malloc");
418 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
419 if (err) {
420 free(*outbuf);
421 *outbuf = NULL;
422 return err;
424 } else {
425 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
426 if (err)
427 return err;
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;