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 if (len - *consumed > UINT_MAX)
251 z->avail_in = UINT_MAX;
252 else
253 z->avail_in = len - *consumed;
255 if (zb->csum) {
256 csum_in = z->next_in;
257 csum_avail_in = z->avail_in;
258 csum_out = z->next_out;
259 csum_avail_out = z->avail_out;
261 ret = inflate(z, Z_SYNC_FLUSH);
262 if (zb->csum) {
263 csum_input(zb->csum, csum_in,
264 csum_avail_in - z->avail_in);
265 csum_output(zb->csum, csum_out,
266 csum_avail_out - z->avail_out);
268 *consumed += z->total_in - last_total_in;
269 } while (ret == Z_OK && z->avail_out > 0);
271 if (ret == Z_OK || ret == Z_BUF_ERROR) {
272 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
273 } else {
274 if (ret != Z_STREAM_END)
275 return got_error(GOT_ERR_DECOMPRESSION);
276 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
279 *outlenp = z->total_out - last_total_out;
280 return NULL;
283 void
284 got_inflate_end(struct got_inflate_buf *zb)
286 free(zb->inbuf);
287 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
288 free(zb->outbuf);
289 inflateEnd(&zb->z);
292 const struct got_error *
293 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
294 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
296 const struct got_error *err;
297 size_t avail, consumed;
298 struct got_inflate_buf zb;
299 void *newbuf;
300 int nbuf = 1;
302 if (outbuf) {
303 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
304 if (*outbuf == NULL)
305 return got_error_from_errno("malloc");
306 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
307 } else
308 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
309 if (err)
310 return err;
312 *outlen = 0;
313 if (consumed_total)
314 *consumed_total = 0;
316 do {
317 err = got_inflate_read(&zb, f, &avail, &consumed);
318 if (err)
319 goto done;
320 *outlen += avail;
321 if (consumed_total)
322 *consumed_total += consumed;
323 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
324 if (outbuf == NULL)
325 continue;
326 newbuf = reallocarray(*outbuf, ++nbuf,
327 GOT_INFLATE_BUFSIZE);
328 if (newbuf == NULL) {
329 err = got_error_from_errno("reallocarray");
330 free(*outbuf);
331 *outbuf = NULL;
332 *outlen = 0;
333 goto done;
335 *outbuf = newbuf;
336 zb.outbuf = newbuf + *outlen;
337 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
339 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
341 done:
342 got_inflate_end(&zb);
343 return err;
346 const struct got_error *
347 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
348 size_t *consumed_total, struct got_inflate_checksum *csum,
349 size_t expected_size, int infd)
351 const struct got_error *err;
352 size_t avail, consumed;
353 struct got_inflate_buf zb;
354 void *newbuf;
355 int nbuf = 1;
356 size_t bufsize = GOT_INFLATE_BUFSIZE;
358 /* Optimize buffer size in case short reads should suffice. */
359 if (expected_size > 0 && expected_size < bufsize)
360 bufsize = expected_size;
362 if (outbuf) {
363 *outbuf = malloc(bufsize);
364 if (*outbuf == NULL)
365 return got_error_from_errno("malloc");
366 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
367 } else
368 err = got_inflate_init(&zb, NULL, bufsize, csum);
369 if (err)
370 goto done;
372 *outlen = 0;
373 if (consumed_total)
374 *consumed_total = 0;
376 do {
377 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
378 if (err)
379 goto done;
380 *outlen += avail;
381 if (consumed_total)
382 *consumed_total += consumed;
383 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
384 if (outbuf == NULL)
385 continue;
386 newbuf = reallocarray(*outbuf, ++nbuf,
387 GOT_INFLATE_BUFSIZE);
388 if (newbuf == NULL) {
389 err = got_error_from_errno("reallocarray");
390 free(*outbuf);
391 *outbuf = NULL;
392 *outlen = 0;
393 goto done;
395 *outbuf = newbuf;
396 zb.outbuf = newbuf + *outlen;
397 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
399 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
401 done:
402 got_inflate_end(&zb);
403 return err;
406 const struct got_error *
407 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
408 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
409 size_t offset, size_t len)
411 const struct got_error *err;
412 size_t avail, consumed;
413 struct got_inflate_buf zb;
414 void *newbuf;
415 int nbuf = 1;
417 if (outbuf) {
418 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
419 if (*outbuf == NULL)
420 return got_error_from_errno("malloc");
421 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
422 if (err) {
423 free(*outbuf);
424 *outbuf = NULL;
425 return err;
427 } else {
428 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
429 if (err)
430 return err;
433 *outlen = 0;
434 if (consumed_total)
435 *consumed_total = 0;
436 do {
437 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
438 &consumed);
439 if (err)
440 goto done;
441 offset += consumed;
442 if (consumed_total)
443 *consumed_total += consumed;
444 len -= consumed;
445 *outlen += avail;
446 if (len == 0)
447 break;
448 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
449 if (outbuf == NULL)
450 continue;
451 newbuf = reallocarray(*outbuf, ++nbuf,
452 GOT_INFLATE_BUFSIZE);
453 if (newbuf == NULL) {
454 err = got_error_from_errno("reallocarray");
455 free(*outbuf);
456 *outbuf = NULL;
457 *outlen = 0;
458 goto done;
460 *outbuf = newbuf;
461 zb.outbuf = newbuf + *outlen;
462 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
464 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
465 done:
466 got_inflate_end(&zb);
467 return err;
470 const struct got_error *
471 got_inflate_to_fd(size_t *outlen, FILE *infile,
472 struct got_inflate_checksum *csum, int outfd)
474 const struct got_error *err = NULL;
475 size_t avail;
476 struct got_inflate_buf zb;
478 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
479 if (err)
480 goto done;
482 *outlen = 0;
484 do {
485 err = got_inflate_read(&zb, infile, &avail, NULL);
486 if (err)
487 goto done;
488 if (avail > 0) {
489 ssize_t n;
490 n = write(outfd, zb.outbuf, avail);
491 if (n != avail) {
492 err = got_error_from_errno("write");
493 goto done;
495 *outlen += avail;
497 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
499 done:
500 if (err == NULL) {
501 if (lseek(outfd, SEEK_SET, 0) == -1)
502 err = got_error_from_errno("lseek");
504 got_inflate_end(&zb);
505 return err;
508 const struct got_error *
509 got_inflate_to_file(size_t *outlen, FILE *infile,
510 struct got_inflate_checksum *csum, FILE *outfile)
512 const struct got_error *err;
513 size_t avail;
514 struct got_inflate_buf zb;
516 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
517 if (err)
518 goto done;
520 *outlen = 0;
522 do {
523 err = got_inflate_read(&zb, infile, &avail, NULL);
524 if (err)
525 goto done;
526 if (avail > 0) {
527 size_t n;
528 n = fwrite(zb.outbuf, avail, 1, outfile);
529 if (n != 1) {
530 err = got_ferror(outfile, GOT_ERR_IO);
531 goto done;
533 *outlen += avail;
535 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
537 done:
538 if (err == NULL)
539 rewind(outfile);
540 got_inflate_end(&zb);
541 return err;
544 const struct got_error *
545 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
546 struct got_inflate_checksum *csum, int infd, FILE *outfile)
548 const struct got_error *err;
549 size_t avail, consumed;
550 struct got_inflate_buf zb;
552 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
553 if (err)
554 goto done;
556 *outlen = 0;
557 if (consumed_total)
558 *consumed_total = 0;
559 do {
560 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
561 if (err)
562 goto done;
563 if (avail > 0) {
564 size_t n;
565 n = fwrite(zb.outbuf, avail, 1, outfile);
566 if (n != 1) {
567 err = got_ferror(outfile, GOT_ERR_IO);
568 goto done;
570 *outlen += avail;
571 if (consumed_total)
572 *consumed_total += consumed;
574 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
576 done:
577 if (err == NULL)
578 rewind(outfile);
579 got_inflate_end(&zb);
580 return err;
583 const struct got_error *
584 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
585 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
586 size_t len, FILE *outfile)
588 const struct got_error *err;
589 size_t avail, consumed;
590 struct got_inflate_buf zb;
592 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
593 if (err)
594 goto done;
596 *outlen = 0;
597 if (consumed_total)
598 *consumed_total = 0;
599 do {
600 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
601 &consumed);
602 if (err)
603 goto done;
604 offset += consumed;
605 if (consumed_total)
606 *consumed_total += consumed;
607 len -= consumed;
608 if (avail > 0) {
609 size_t n;
610 n = fwrite(zb.outbuf, avail, 1, outfile);
611 if (n != 1) {
612 err = got_ferror(outfile, GOT_ERR_IO);
613 goto done;
615 *outlen += avail;
617 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
619 done:
620 if (err == NULL)
621 rewind(outfile);
622 got_inflate_end(&zb);
623 return err;