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);
428 *outlen = 0;
429 if (consumed_total)
430 *consumed_total = 0;
431 do {
432 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
433 &consumed);
434 if (err)
435 goto done;
436 offset += consumed;
437 if (consumed_total)
438 *consumed_total += consumed;
439 len -= consumed;
440 *outlen += avail;
441 if (len == 0)
442 break;
443 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
444 if (outbuf == NULL)
445 continue;
446 newbuf = reallocarray(*outbuf, ++nbuf,
447 GOT_INFLATE_BUFSIZE);
448 if (newbuf == NULL) {
449 err = got_error_from_errno("reallocarray");
450 free(*outbuf);
451 *outbuf = NULL;
452 *outlen = 0;
453 goto done;
455 *outbuf = newbuf;
456 zb.outbuf = newbuf + *outlen;
457 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
459 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
460 done:
461 got_inflate_end(&zb);
462 return err;
465 const struct got_error *
466 got_inflate_to_fd(size_t *outlen, FILE *infile,
467 struct got_inflate_checksum *csum, int outfd)
469 const struct got_error *err = NULL;
470 size_t avail;
471 struct got_inflate_buf zb;
473 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
474 if (err)
475 goto done;
477 *outlen = 0;
479 do {
480 err = got_inflate_read(&zb, infile, &avail, NULL);
481 if (err)
482 goto done;
483 if (avail > 0) {
484 ssize_t n;
485 n = write(outfd, zb.outbuf, avail);
486 if (n != avail) {
487 err = got_error_from_errno("write");
488 goto done;
490 *outlen += avail;
492 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
494 done:
495 if (err == NULL) {
496 if (lseek(outfd, SEEK_SET, 0) == -1)
497 err = got_error_from_errno("lseek");
499 got_inflate_end(&zb);
500 return err;
503 const struct got_error *
504 got_inflate_to_file(size_t *outlen, FILE *infile,
505 struct got_inflate_checksum *csum, FILE *outfile)
507 const struct got_error *err;
508 size_t avail;
509 struct got_inflate_buf zb;
511 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
512 if (err)
513 goto done;
515 *outlen = 0;
517 do {
518 err = got_inflate_read(&zb, infile, &avail, NULL);
519 if (err)
520 goto done;
521 if (avail > 0) {
522 size_t n;
523 n = fwrite(zb.outbuf, avail, 1, outfile);
524 if (n != 1) {
525 err = got_ferror(outfile, GOT_ERR_IO);
526 goto done;
528 *outlen += avail;
530 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
532 done:
533 if (err == NULL)
534 rewind(outfile);
535 got_inflate_end(&zb);
536 return err;
539 const struct got_error *
540 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
541 struct got_inflate_checksum *csum, int infd, FILE *outfile)
543 const struct got_error *err;
544 size_t avail, consumed;
545 struct got_inflate_buf zb;
547 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
548 if (err)
549 goto done;
551 *outlen = 0;
552 if (consumed_total)
553 *consumed_total = 0;
554 do {
555 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
556 if (err)
557 goto done;
558 if (avail > 0) {
559 size_t n;
560 n = fwrite(zb.outbuf, avail, 1, outfile);
561 if (n != 1) {
562 err = got_ferror(outfile, GOT_ERR_IO);
563 goto done;
565 *outlen += avail;
566 if (consumed_total)
567 *consumed_total += consumed;
569 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
571 done:
572 if (err == NULL)
573 rewind(outfile);
574 got_inflate_end(&zb);
575 return err;
578 const struct got_error *
579 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
580 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
581 size_t len, FILE *outfile)
583 const struct got_error *err;
584 size_t avail, consumed;
585 struct got_inflate_buf zb;
587 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
588 if (err)
589 goto done;
591 *outlen = 0;
592 if (consumed_total)
593 *consumed_total = 0;
594 do {
595 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
596 &consumed);
597 if (err)
598 goto done;
599 offset += consumed;
600 if (consumed_total)
601 *consumed_total += consumed;
602 len -= consumed;
603 if (avail > 0) {
604 size_t n;
605 n = fwrite(zb.outbuf, avail, 1, outfile);
606 if (n != 1) {
607 err = got_ferror(outfile, GOT_ERR_IO);
608 goto done;
610 *outlen += avail;
612 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
614 done:
615 if (err == NULL)
616 rewind(outfile);
617 got_inflate_end(&zb);
618 return err;