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 <sha1.h>
24 #include <unistd.h>
25 #include <zlib.h>
26 #include <time.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_path.h"
32 #include "got_lib_inflate.h"
34 #ifndef MIN
35 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
36 #endif
38 const struct got_error *
39 got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
40 struct got_inflate_checksum *csum)
41 {
42 const struct got_error *err = NULL;
43 int zerr;
45 memset(&zb->z, 0, sizeof(zb->z));
47 zb->z.zalloc = Z_NULL;
48 zb->z.zfree = Z_NULL;
49 zerr = inflateInit(&zb->z);
50 if (zerr != Z_OK) {
51 if (zerr == Z_ERRNO)
52 return got_error_from_errno("inflateInit");
53 if (zerr == Z_MEM_ERROR) {
54 errno = ENOMEM;
55 return got_error_from_errno("inflateInit");
56 }
57 return got_error(GOT_ERR_DECOMPRESSION);
58 }
60 zb->inlen = zb->outlen = bufsize;
62 zb->inbuf = calloc(1, zb->inlen);
63 if (zb->inbuf == NULL) {
64 err = got_error_from_errno("calloc");
65 goto done;
66 }
68 zb->flags = 0;
69 if (outbuf == NULL) {
70 zb->outbuf = calloc(1, zb->outlen);
71 if (zb->outbuf == NULL) {
72 err = got_error_from_errno("calloc");
73 goto done;
74 }
75 zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
76 } else
77 zb->outbuf = outbuf;
79 zb->csum = csum;
80 done:
81 if (err)
82 got_inflate_end(zb);
83 return err;
84 }
86 static void
87 csum_input(struct got_inflate_checksum *csum, const char *buf, size_t len)
88 {
89 if (csum->input_crc)
90 *csum->input_crc = crc32(*csum->input_crc, buf, len);
92 if (csum->input_sha1)
93 SHA1Update(csum->input_sha1, buf, len);
94 }
96 const struct got_error *
97 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
98 size_t *consumed)
99 {
100 size_t last_total_out = zb->z.total_out;
101 size_t last_total_in = zb->z.total_in;
102 z_stream *z = &zb->z;
103 int ret = Z_ERRNO;
105 z->next_out = zb->outbuf;
106 z->avail_out = zb->outlen;
108 *outlenp = 0;
109 if (consumed)
110 *consumed = 0;
111 do {
112 char *csum_in = NULL;
113 size_t csum_avail = 0;
115 if (z->avail_in == 0) {
116 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
117 if (n == 0) {
118 if (ferror(f))
119 return got_ferror(f, GOT_ERR_IO);
120 /* EOF */
121 ret = Z_STREAM_END;
122 break;
124 z->next_in = zb->inbuf;
125 z->avail_in = n;
127 if (zb->csum) {
128 csum_in = z->next_in;
129 csum_avail = z->avail_in;
131 ret = inflate(z, Z_SYNC_FLUSH);
132 if (zb->csum)
133 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
134 } while (ret == Z_OK && z->avail_out > 0);
136 if (ret == Z_OK || ret == Z_BUF_ERROR) {
137 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
138 } else {
139 if (ret != Z_STREAM_END)
140 return got_error(GOT_ERR_DECOMPRESSION);
141 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
144 *outlenp = z->total_out - last_total_out;
145 if (consumed)
146 *consumed += z->total_in - last_total_in;
147 return NULL;
150 const struct got_error *
151 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
152 size_t *consumed)
154 size_t last_total_out = zb->z.total_out;
155 size_t last_total_in = zb->z.total_in;
156 z_stream *z = &zb->z;
157 int ret = Z_ERRNO;
159 z->next_out = zb->outbuf;
160 z->avail_out = zb->outlen;
162 *outlenp = 0;
163 if (consumed)
164 *consumed = 0;
165 do {
166 char *csum_in = NULL;
167 size_t csum_avail = 0;
169 if (z->avail_in == 0) {
170 ssize_t n = read(fd, zb->inbuf, zb->inlen);
171 if (n < 0)
172 return got_error_from_errno("read");
173 else if (n == 0) {
174 /* EOF */
175 ret = Z_STREAM_END;
176 break;
178 z->next_in = zb->inbuf;
179 z->avail_in = n;
181 if (zb->csum) {
182 csum_in = z->next_in;
183 csum_avail = z->avail_in;
185 ret = inflate(z, Z_SYNC_FLUSH);
186 if (zb->csum)
187 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
188 } while (ret == Z_OK && z->avail_out > 0);
190 if (ret == Z_OK || ret == Z_BUF_ERROR) {
191 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
192 } else {
193 if (ret != Z_STREAM_END)
194 return got_error(GOT_ERR_DECOMPRESSION);
195 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
198 *outlenp = z->total_out - last_total_out;
199 if (consumed)
200 *consumed += z->total_in - last_total_in;
201 return NULL;
204 const struct got_error *
205 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
206 size_t len, size_t *outlenp, size_t *consumed)
208 size_t last_total_out = zb->z.total_out;
209 z_stream *z = &zb->z;
210 int ret = Z_ERRNO;
212 z->next_out = zb->outbuf;
213 z->avail_out = zb->outlen;
215 *outlenp = 0;
216 *consumed = 0;
218 do {
219 char *csum_in = NULL;
220 size_t csum_avail = 0;
221 size_t last_total_in = zb->z.total_in;
223 if (z->avail_in == 0) {
224 if (len == 0) {
225 /* EOF */
226 ret = Z_STREAM_END;
227 break;
229 z->next_in = map + offset + *consumed;
230 z->avail_in = len - *consumed;
232 if (zb->csum) {
233 csum_in = z->next_in;
234 csum_avail = z->avail_in;
236 ret = inflate(z, Z_SYNC_FLUSH);
237 if (zb->csum)
238 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
239 *consumed += z->total_in - last_total_in;
240 } while (ret == Z_OK && z->avail_out > 0);
242 if (ret == Z_OK || ret == Z_BUF_ERROR) {
243 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
244 } else {
245 if (ret != Z_STREAM_END)
246 return got_error(GOT_ERR_DECOMPRESSION);
247 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
250 *outlenp = z->total_out - last_total_out;
251 return NULL;
254 void
255 got_inflate_end(struct got_inflate_buf *zb)
257 free(zb->inbuf);
258 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
259 free(zb->outbuf);
260 inflateEnd(&zb->z);
263 const struct got_error *
264 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
265 size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
267 const struct got_error *err;
268 size_t avail, consumed;
269 struct got_inflate_buf zb;
270 void *newbuf;
271 int nbuf = 1;
273 if (outbuf) {
274 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
275 if (*outbuf == NULL)
276 return got_error_from_errno("malloc");
277 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
278 } else
279 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
280 if (err)
281 return err;
283 *outlen = 0;
284 if (consumed_total)
285 *consumed_total = 0;
287 do {
288 err = got_inflate_read(&zb, f, &avail, &consumed);
289 if (err)
290 goto done;
291 *outlen += avail;
292 if (consumed_total)
293 *consumed_total += consumed;
294 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
295 if (outbuf == NULL)
296 continue;
297 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
298 newbuf = reallocarray(*outbuf, ++nbuf,
299 GOT_INFLATE_BUFSIZE);
300 if (newbuf == NULL) {
301 err = got_error_from_errno("reallocarray");
302 free(*outbuf);
303 *outbuf = NULL;
304 *outlen = 0;
305 goto done;
307 *outbuf = newbuf;
308 zb.outbuf = newbuf + *outlen;
310 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
312 done:
313 got_inflate_end(&zb);
314 return err;
317 const struct got_error *
318 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
319 size_t *consumed_total, struct got_inflate_checksum *csum,
320 size_t expected_size, int infd)
322 const struct got_error *err;
323 size_t avail, consumed;
324 struct got_inflate_buf zb;
325 void *newbuf;
326 int nbuf = 1;
327 size_t bufsize = GOT_INFLATE_BUFSIZE;
329 /* Optimize buffer size in case short reads should suffice. */
330 if (expected_size > 0 && expected_size < bufsize)
331 bufsize = expected_size;
333 if (outbuf) {
334 *outbuf = malloc(bufsize);
335 if (*outbuf == NULL)
336 return got_error_from_errno("malloc");
337 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
338 } else
339 err = got_inflate_init(&zb, NULL, bufsize, csum);
340 if (err)
341 goto done;
343 *outlen = 0;
344 if (consumed_total)
345 *consumed_total = 0;
347 do {
348 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
349 if (err)
350 goto done;
351 *outlen += avail;
352 if (consumed_total)
353 *consumed_total += consumed;
354 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
355 if (outbuf == NULL)
356 continue;
357 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
358 newbuf = reallocarray(*outbuf, ++nbuf,
359 GOT_INFLATE_BUFSIZE);
360 if (newbuf == NULL) {
361 err = got_error_from_errno("reallocarray");
362 free(*outbuf);
363 *outbuf = NULL;
364 *outlen = 0;
365 goto done;
367 *outbuf = newbuf;
368 zb.outbuf = newbuf + *outlen;
370 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
372 done:
373 got_inflate_end(&zb);
374 return err;
377 const struct got_error *
378 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
379 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
380 size_t offset, size_t len)
382 const struct got_error *err;
383 size_t avail, consumed;
384 struct got_inflate_buf zb;
385 void *newbuf;
386 int nbuf = 1;
388 if (outbuf) {
389 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
390 if (*outbuf == NULL)
391 return got_error_from_errno("malloc");
392 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
393 if (err) {
394 free(*outbuf);
395 *outbuf = NULL;
396 return err;
398 } else {
399 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
402 *outlen = 0;
403 if (consumed_total)
404 *consumed_total = 0;
405 do {
406 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
407 &consumed);
408 if (err)
409 goto done;
410 offset += consumed;
411 if (consumed_total)
412 *consumed_total += consumed;
413 len -= consumed;
414 *outlen += avail;
415 if (len == 0)
416 break;
417 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
418 if (outbuf == NULL)
419 continue;
420 newbuf = reallocarray(*outbuf, ++nbuf,
421 GOT_INFLATE_BUFSIZE);
422 if (newbuf == NULL) {
423 err = got_error_from_errno("reallocarray");
424 free(*outbuf);
425 *outbuf = NULL;
426 *outlen = 0;
427 goto done;
429 *outbuf = newbuf;
430 zb.outbuf = newbuf + *outlen;
431 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
433 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
434 done:
435 got_inflate_end(&zb);
436 return err;
439 const struct got_error *
440 got_inflate_to_fd(size_t *outlen, FILE *infile,
441 struct got_inflate_checksum *csum, int outfd)
443 const struct got_error *err = NULL;
444 size_t avail;
445 struct got_inflate_buf zb;
447 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
448 if (err)
449 goto done;
451 *outlen = 0;
453 do {
454 err = got_inflate_read(&zb, infile, &avail, NULL);
455 if (err)
456 goto done;
457 if (avail > 0) {
458 ssize_t n;
459 n = write(outfd, zb.outbuf, avail);
460 if (n != avail) {
461 err = got_error_from_errno("write");
462 goto done;
464 *outlen += avail;
466 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
468 done:
469 if (err == NULL) {
470 if (lseek(outfd, SEEK_SET, 0) == -1)
471 err = got_error_from_errno("lseek");
473 got_inflate_end(&zb);
474 return err;
477 const struct got_error *
478 got_inflate_to_file(size_t *outlen, FILE *infile,
479 struct got_inflate_checksum *csum, FILE *outfile)
481 const struct got_error *err;
482 size_t avail;
483 struct got_inflate_buf zb;
485 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
486 if (err)
487 goto done;
489 *outlen = 0;
491 do {
492 err = got_inflate_read(&zb, infile, &avail, NULL);
493 if (err)
494 goto done;
495 if (avail > 0) {
496 size_t n;
497 n = fwrite(zb.outbuf, avail, 1, outfile);
498 if (n != 1) {
499 err = got_ferror(outfile, GOT_ERR_IO);
500 goto done;
502 *outlen += avail;
504 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
506 done:
507 if (err == NULL)
508 rewind(outfile);
509 got_inflate_end(&zb);
510 return err;
513 const struct got_error *
514 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
515 struct got_inflate_checksum *csum, int infd, FILE *outfile)
517 const struct got_error *err;
518 size_t avail, consumed;
519 struct got_inflate_buf zb;
521 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
522 if (err)
523 goto done;
525 *outlen = 0;
526 if (consumed_total)
527 *consumed_total = 0;
528 do {
529 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
530 if (err)
531 goto done;
532 if (avail > 0) {
533 size_t n;
534 n = fwrite(zb.outbuf, avail, 1, outfile);
535 if (n != 1) {
536 err = got_ferror(outfile, GOT_ERR_IO);
537 goto done;
539 *outlen += avail;
540 if (consumed_total)
541 *consumed_total += consumed;
543 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
545 done:
546 if (err == NULL)
547 rewind(outfile);
548 got_inflate_end(&zb);
549 return err;
552 const struct got_error *
553 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
554 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
555 size_t len, FILE *outfile)
557 const struct got_error *err;
558 size_t avail, consumed;
559 struct got_inflate_buf zb;
561 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
562 if (err)
563 goto done;
565 *outlen = 0;
566 if (consumed_total)
567 *consumed_total = 0;
568 do {
569 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
570 &consumed);
571 if (err)
572 goto done;
573 offset += consumed;
574 if (consumed_total)
575 *consumed_total += consumed;
576 len -= consumed;
577 if (avail > 0) {
578 size_t n;
579 n = fwrite(zb.outbuf, avail, 1, outfile);
580 if (n != 1) {
581 err = got_ferror(outfile, GOT_ERR_IO);
582 goto done;
584 *outlen += avail;
586 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
588 done:
589 if (err == NULL)
590 rewind(outfile);
591 got_inflate_end(&zb);
592 return err;