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 <sha1.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 char *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 char *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 char *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 char *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 char *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 z->avail_in = len - *consumed;
253 if (zb->csum) {
254 csum_in = z->next_in;
255 csum_avail_in = z->avail_in;
256 csum_out = z->next_out;
257 csum_avail_out = z->avail_out;
259 ret = inflate(z, Z_SYNC_FLUSH);
260 if (zb->csum) {
261 csum_input(zb->csum, csum_in,
262 csum_avail_in - z->avail_in);
263 csum_output(zb->csum, csum_out,
264 csum_avail_out - z->avail_out);
266 *consumed += z->total_in - last_total_in;
267 } while (ret == Z_OK && z->avail_out > 0);
269 if (ret == Z_OK || ret == Z_BUF_ERROR) {
270 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
271 } else {
272 if (ret != Z_STREAM_END)
273 return got_error(GOT_ERR_DECOMPRESSION);
274 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
277 *outlenp = z->total_out - last_total_out;
278 return NULL;
281 void
282 got_inflate_end(struct got_inflate_buf *zb)
284 free(zb->inbuf);
285 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
286 free(zb->outbuf);
287 inflateEnd(&zb->z);
290 const struct got_error *
291 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
292 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
294 const struct got_error *err;
295 size_t avail, consumed;
296 struct got_inflate_buf zb;
297 void *newbuf;
298 int nbuf = 1;
300 if (outbuf) {
301 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
302 if (*outbuf == NULL)
303 return got_error_from_errno("malloc");
304 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
305 } else
306 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
307 if (err)
308 return err;
310 *outlen = 0;
311 if (consumed_total)
312 *consumed_total = 0;
314 do {
315 err = got_inflate_read(&zb, f, &avail, &consumed);
316 if (err)
317 goto done;
318 *outlen += avail;
319 if (consumed_total)
320 *consumed_total += consumed;
321 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
322 if (outbuf == NULL)
323 continue;
324 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
325 newbuf = reallocarray(*outbuf, ++nbuf,
326 GOT_INFLATE_BUFSIZE);
327 if (newbuf == NULL) {
328 err = got_error_from_errno("reallocarray");
329 free(*outbuf);
330 *outbuf = NULL;
331 *outlen = 0;
332 goto done;
334 *outbuf = newbuf;
335 zb.outbuf = newbuf + *outlen;
337 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
339 done:
340 got_inflate_end(&zb);
341 return err;
344 const struct got_error *
345 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
346 size_t *consumed_total, struct got_inflate_checksum *csum,
347 size_t expected_size, int infd)
349 const struct got_error *err;
350 size_t avail, consumed;
351 struct got_inflate_buf zb;
352 void *newbuf;
353 int nbuf = 1;
354 size_t bufsize = GOT_INFLATE_BUFSIZE;
356 /* Optimize buffer size in case short reads should suffice. */
357 if (expected_size > 0 && expected_size < bufsize)
358 bufsize = expected_size;
360 if (outbuf) {
361 *outbuf = malloc(bufsize);
362 if (*outbuf == NULL)
363 return got_error_from_errno("malloc");
364 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
365 } else
366 err = got_inflate_init(&zb, NULL, bufsize, csum);
367 if (err)
368 goto done;
370 *outlen = 0;
371 if (consumed_total)
372 *consumed_total = 0;
374 do {
375 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
376 if (err)
377 goto done;
378 *outlen += avail;
379 if (consumed_total)
380 *consumed_total += consumed;
381 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
382 if (outbuf == NULL)
383 continue;
384 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
385 newbuf = reallocarray(*outbuf, ++nbuf,
386 GOT_INFLATE_BUFSIZE);
387 if (newbuf == NULL) {
388 err = got_error_from_errno("reallocarray");
389 free(*outbuf);
390 *outbuf = NULL;
391 *outlen = 0;
392 goto done;
394 *outbuf = newbuf;
395 zb.outbuf = newbuf + *outlen;
397 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
399 done:
400 got_inflate_end(&zb);
401 return err;
404 const struct got_error *
405 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
406 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
407 size_t offset, size_t len)
409 const struct got_error *err;
410 size_t avail, consumed;
411 struct got_inflate_buf zb;
412 void *newbuf;
413 int nbuf = 1;
415 if (outbuf) {
416 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
417 if (*outbuf == NULL)
418 return got_error_from_errno("malloc");
419 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
420 if (err) {
421 free(*outbuf);
422 *outbuf = NULL;
423 return err;
425 } else {
426 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
429 *outlen = 0;
430 if (consumed_total)
431 *consumed_total = 0;
432 do {
433 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
434 &consumed);
435 if (err)
436 goto done;
437 offset += consumed;
438 if (consumed_total)
439 *consumed_total += consumed;
440 len -= consumed;
441 *outlen += avail;
442 if (len == 0)
443 break;
444 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
445 if (outbuf == NULL)
446 continue;
447 newbuf = reallocarray(*outbuf, ++nbuf,
448 GOT_INFLATE_BUFSIZE);
449 if (newbuf == NULL) {
450 err = got_error_from_errno("reallocarray");
451 free(*outbuf);
452 *outbuf = NULL;
453 *outlen = 0;
454 goto done;
456 *outbuf = newbuf;
457 zb.outbuf = newbuf + *outlen;
458 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
460 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
461 done:
462 got_inflate_end(&zb);
463 return err;
466 const struct got_error *
467 got_inflate_to_fd(size_t *outlen, FILE *infile,
468 struct got_inflate_checksum *csum, int outfd)
470 const struct got_error *err = NULL;
471 size_t avail;
472 struct got_inflate_buf zb;
474 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
475 if (err)
476 goto done;
478 *outlen = 0;
480 do {
481 err = got_inflate_read(&zb, infile, &avail, NULL);
482 if (err)
483 goto done;
484 if (avail > 0) {
485 ssize_t n;
486 n = write(outfd, zb.outbuf, avail);
487 if (n != avail) {
488 err = got_error_from_errno("write");
489 goto done;
491 *outlen += avail;
493 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
495 done:
496 if (err == NULL) {
497 if (lseek(outfd, SEEK_SET, 0) == -1)
498 err = got_error_from_errno("lseek");
500 got_inflate_end(&zb);
501 return err;
504 const struct got_error *
505 got_inflate_to_file(size_t *outlen, FILE *infile,
506 struct got_inflate_checksum *csum, FILE *outfile)
508 const struct got_error *err;
509 size_t avail;
510 struct got_inflate_buf zb;
512 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
513 if (err)
514 goto done;
516 *outlen = 0;
518 do {
519 err = got_inflate_read(&zb, infile, &avail, NULL);
520 if (err)
521 goto done;
522 if (avail > 0) {
523 size_t n;
524 n = fwrite(zb.outbuf, avail, 1, outfile);
525 if (n != 1) {
526 err = got_ferror(outfile, GOT_ERR_IO);
527 goto done;
529 *outlen += avail;
531 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
533 done:
534 if (err == NULL)
535 rewind(outfile);
536 got_inflate_end(&zb);
537 return err;
540 const struct got_error *
541 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
542 struct got_inflate_checksum *csum, int infd, FILE *outfile)
544 const struct got_error *err;
545 size_t avail, consumed;
546 struct got_inflate_buf zb;
548 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
549 if (err)
550 goto done;
552 *outlen = 0;
553 if (consumed_total)
554 *consumed_total = 0;
555 do {
556 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
557 if (err)
558 goto done;
559 if (avail > 0) {
560 size_t n;
561 n = fwrite(zb.outbuf, avail, 1, outfile);
562 if (n != 1) {
563 err = got_ferror(outfile, GOT_ERR_IO);
564 goto done;
566 *outlen += avail;
567 if (consumed_total)
568 *consumed_total += consumed;
570 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
572 done:
573 if (err == NULL)
574 rewind(outfile);
575 got_inflate_end(&zb);
576 return err;
579 const struct got_error *
580 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
581 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
582 size_t len, FILE *outfile)
584 const struct got_error *err;
585 size_t avail, consumed;
586 struct got_inflate_buf zb;
588 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
589 if (err)
590 goto done;
592 *outlen = 0;
593 if (consumed_total)
594 *consumed_total = 0;
595 do {
596 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
597 &consumed);
598 if (err)
599 goto done;
600 offset += consumed;
601 if (consumed_total)
602 *consumed_total += consumed;
603 len -= consumed;
604 if (avail > 0) {
605 size_t n;
606 n = fwrite(zb.outbuf, avail, 1, outfile);
607 if (n != 1) {
608 err = got_ferror(outfile, GOT_ERR_IO);
609 goto done;
611 *outlen += avail;
613 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
615 done:
616 if (err == NULL)
617 rewind(outfile);
618 got_inflate_end(&zb);
619 return err;