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 <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 const struct got_error *
96 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
97 size_t *consumed)
98 {
99 size_t last_total_out = zb->z.total_out;
100 size_t last_total_in = zb->z.total_in;
101 z_stream *z = &zb->z;
102 int ret = Z_ERRNO;
104 z->next_out = zb->outbuf;
105 z->avail_out = zb->outlen;
107 *outlenp = 0;
108 if (consumed)
109 *consumed = 0;
110 do {
111 char *csum_in = NULL;
112 size_t csum_avail = 0;
114 if (z->avail_in == 0) {
115 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
116 if (n == 0) {
117 if (ferror(f))
118 return got_ferror(f, GOT_ERR_IO);
119 /* EOF */
120 ret = Z_STREAM_END;
121 break;
123 z->next_in = zb->inbuf;
124 z->avail_in = n;
126 if (zb->csum) {
127 csum_in = z->next_in;
128 csum_avail = z->avail_in;
130 ret = inflate(z, Z_SYNC_FLUSH);
131 if (zb->csum)
132 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
133 } while (ret == Z_OK && z->avail_out > 0);
135 if (ret == Z_OK || ret == Z_BUF_ERROR) {
136 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
137 } else {
138 if (ret != Z_STREAM_END)
139 return got_error(GOT_ERR_DECOMPRESSION);
140 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
143 *outlenp = z->total_out - last_total_out;
144 if (consumed)
145 *consumed += z->total_in - last_total_in;
146 return NULL;
149 const struct got_error *
150 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
151 size_t *consumed)
153 size_t last_total_out = zb->z.total_out;
154 size_t last_total_in = zb->z.total_in;
155 z_stream *z = &zb->z;
156 int ret = Z_ERRNO;
158 z->next_out = zb->outbuf;
159 z->avail_out = zb->outlen;
161 *outlenp = 0;
162 if (consumed)
163 *consumed = 0;
164 do {
165 char *csum_in = NULL;
166 size_t csum_avail = 0;
168 if (z->avail_in == 0) {
169 ssize_t n = read(fd, zb->inbuf, zb->inlen);
170 if (n < 0)
171 return got_error_from_errno("read");
172 else if (n == 0) {
173 /* EOF */
174 ret = Z_STREAM_END;
175 break;
177 z->next_in = zb->inbuf;
178 z->avail_in = n;
180 if (zb->csum) {
181 csum_in = z->next_in;
182 csum_avail = z->avail_in;
184 ret = inflate(z, Z_SYNC_FLUSH);
185 if (zb->csum)
186 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
187 } while (ret == Z_OK && z->avail_out > 0);
189 if (ret == Z_OK || ret == Z_BUF_ERROR) {
190 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
191 } else {
192 if (ret != Z_STREAM_END)
193 return got_error(GOT_ERR_DECOMPRESSION);
194 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
197 *outlenp = z->total_out - last_total_out;
198 if (consumed)
199 *consumed += z->total_in - last_total_in;
200 return NULL;
203 const struct got_error *
204 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
205 size_t len, size_t *outlenp, size_t *consumed)
207 size_t last_total_out = zb->z.total_out;
208 z_stream *z = &zb->z;
209 int ret = Z_ERRNO;
211 z->next_out = zb->outbuf;
212 z->avail_out = zb->outlen;
214 *outlenp = 0;
215 *consumed = 0;
217 do {
218 char *csum_in = NULL;
219 size_t csum_avail = 0;
220 size_t last_total_in = zb->z.total_in;
222 if (z->avail_in == 0) {
223 if (len == 0) {
224 /* EOF */
225 ret = Z_STREAM_END;
226 break;
228 z->next_in = map + offset + *consumed;
229 z->avail_in = len - *consumed;
231 if (zb->csum) {
232 csum_in = z->next_in;
233 csum_avail = z->avail_in;
235 ret = inflate(z, Z_SYNC_FLUSH);
236 if (zb->csum)
237 csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
238 *consumed += z->total_in - last_total_in;
239 } while (ret == Z_OK && z->avail_out > 0);
241 if (ret == Z_OK || ret == Z_BUF_ERROR) {
242 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
243 } else {
244 if (ret != Z_STREAM_END)
245 return got_error(GOT_ERR_DECOMPRESSION);
246 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
249 *outlenp = z->total_out - last_total_out;
250 return NULL;
253 void
254 got_inflate_end(struct got_inflate_buf *zb)
256 free(zb->inbuf);
257 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
258 free(zb->outbuf);
259 inflateEnd(&zb->z);
262 const struct got_error *
263 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
264 size_t *consumed_total, FILE *f)
266 const struct got_error *err;
267 size_t avail, consumed;
268 struct got_inflate_buf zb;
269 void *newbuf;
270 int nbuf = 1;
272 if (outbuf) {
273 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
274 if (*outbuf == NULL)
275 return got_error_from_errno("malloc");
276 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
277 } else
278 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
279 if (err)
280 return err;
282 *outlen = 0;
283 if (consumed_total)
284 *consumed_total = 0;
286 do {
287 err = got_inflate_read(&zb, f, &avail, &consumed);
288 if (err)
289 goto done;
290 *outlen += avail;
291 if (consumed_total)
292 *consumed_total += consumed;
293 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
294 if (outbuf == NULL)
295 continue;
296 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
297 newbuf = reallocarray(*outbuf, ++nbuf,
298 GOT_INFLATE_BUFSIZE);
299 if (newbuf == NULL) {
300 err = got_error_from_errno("reallocarray");
301 free(*outbuf);
302 *outbuf = NULL;
303 *outlen = 0;
304 goto done;
306 *outbuf = newbuf;
307 zb.outbuf = newbuf + *outlen;
309 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
311 done:
312 got_inflate_end(&zb);
313 return err;
316 const struct got_error *
317 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
318 size_t *consumed_total, struct got_inflate_checksum *csum,
319 size_t expected_size, int infd)
321 const struct got_error *err;
322 size_t avail, consumed;
323 struct got_inflate_buf zb;
324 void *newbuf;
325 int nbuf = 1;
326 size_t bufsize = GOT_INFLATE_BUFSIZE;
328 /* Optimize buffer size in case short reads should suffice. */
329 if (expected_size > 0 && expected_size < bufsize)
330 bufsize = expected_size;
332 if (outbuf) {
333 *outbuf = malloc(bufsize);
334 if (*outbuf == NULL)
335 return got_error_from_errno("malloc");
336 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
337 } else
338 err = got_inflate_init(&zb, NULL, bufsize, csum);
339 if (err)
340 goto done;
342 *outlen = 0;
343 if (consumed_total)
344 *consumed_total = 0;
346 do {
347 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
348 if (err)
349 goto done;
350 *outlen += avail;
351 if (consumed_total)
352 *consumed_total += consumed;
353 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
354 if (outbuf == NULL)
355 continue;
356 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
357 newbuf = reallocarray(*outbuf, ++nbuf,
358 GOT_INFLATE_BUFSIZE);
359 if (newbuf == NULL) {
360 err = got_error_from_errno("reallocarray");
361 free(*outbuf);
362 *outbuf = NULL;
363 *outlen = 0;
364 goto done;
366 *outbuf = newbuf;
367 zb.outbuf = newbuf + *outlen;
369 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
371 done:
372 got_inflate_end(&zb);
373 return err;
376 const struct got_error *
377 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
378 size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
379 size_t offset, size_t len)
381 const struct got_error *err;
382 size_t avail, consumed;
383 struct got_inflate_buf zb;
384 void *newbuf;
385 int nbuf = 1;
387 if (outbuf) {
388 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
389 if (*outbuf == NULL)
390 return got_error_from_errno("malloc");
391 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
392 if (err) {
393 free(*outbuf);
394 *outbuf = NULL;
395 return err;
397 } else {
398 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
401 *outlen = 0;
402 if (consumed_total)
403 *consumed_total = 0;
404 do {
405 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
406 &consumed);
407 if (err)
408 goto done;
409 offset += consumed;
410 if (consumed_total)
411 *consumed_total += consumed;
412 len -= consumed;
413 *outlen += avail;
414 if (len == 0)
415 break;
416 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
417 if (outbuf == NULL)
418 continue;
419 newbuf = reallocarray(*outbuf, ++nbuf,
420 GOT_INFLATE_BUFSIZE);
421 if (newbuf == NULL) {
422 err = got_error_from_errno("reallocarray");
423 free(*outbuf);
424 *outbuf = NULL;
425 *outlen = 0;
426 goto done;
428 *outbuf = newbuf;
429 zb.outbuf = newbuf + *outlen;
430 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
432 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
433 done:
434 got_inflate_end(&zb);
435 return err;
438 const struct got_error *
439 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
441 const struct got_error *err = NULL;
442 size_t avail;
443 struct got_inflate_buf zb;
445 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
446 if (err)
447 goto done;
449 *outlen = 0;
451 do {
452 err = got_inflate_read(&zb, infile, &avail, NULL);
453 if (err)
454 goto done;
455 if (avail > 0) {
456 ssize_t n;
457 n = write(outfd, zb.outbuf, avail);
458 if (n != avail) {
459 err = got_error_from_errno("write");
460 goto done;
462 *outlen += avail;
464 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
466 done:
467 if (err == NULL) {
468 if (lseek(outfd, SEEK_SET, 0) == -1)
469 err = got_error_from_errno("lseek");
471 got_inflate_end(&zb);
472 return err;
475 const struct got_error *
476 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
478 const struct got_error *err;
479 size_t avail;
480 struct got_inflate_buf zb;
482 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
483 if (err)
484 goto done;
486 *outlen = 0;
488 do {
489 err = got_inflate_read(&zb, infile, &avail, NULL);
490 if (err)
491 goto done;
492 if (avail > 0) {
493 size_t n;
494 n = fwrite(zb.outbuf, avail, 1, outfile);
495 if (n != 1) {
496 err = got_ferror(outfile, GOT_ERR_IO);
497 goto done;
499 *outlen += avail;
501 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
503 done:
504 if (err == NULL)
505 rewind(outfile);
506 got_inflate_end(&zb);
507 return err;
510 const struct got_error *
511 got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
512 struct got_inflate_checksum *csum, int infd, FILE *outfile)
514 const struct got_error *err;
515 size_t avail, consumed;
516 struct got_inflate_buf zb;
518 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
519 if (err)
520 goto done;
522 *outlen = 0;
523 if (consumed_total)
524 *consumed_total = 0;
525 do {
526 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
527 if (err)
528 goto done;
529 if (avail > 0) {
530 size_t n;
531 n = fwrite(zb.outbuf, avail, 1, outfile);
532 if (n != 1) {
533 err = got_ferror(outfile, GOT_ERR_IO);
534 goto done;
536 *outlen += avail;
537 if (consumed_total)
538 *consumed_total += consumed;
540 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
542 done:
543 if (err == NULL)
544 rewind(outfile);
545 got_inflate_end(&zb);
546 return err;
549 const struct got_error *
550 got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
551 struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
552 size_t len, FILE *outfile)
554 const struct got_error *err;
555 size_t avail, consumed;
556 struct got_inflate_buf zb;
558 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
559 if (err)
560 goto done;
562 *outlen = 0;
563 if (consumed_total)
564 *consumed_total = 0;
565 do {
566 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
567 &consumed);
568 if (err)
569 goto done;
570 offset += consumed;
571 if (consumed_total)
572 *consumed_total += consumed;
573 len -= consumed;
574 if (avail > 0) {
575 size_t n;
576 n = fwrite(zb.outbuf, avail, 1, outfile);
577 if (n != 1) {
578 err = got_ferror(outfile, GOT_ERR_IO);
579 goto done;
581 *outlen += avail;
583 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
585 done:
586 if (err == NULL)
587 rewind(outfile);
588 got_inflate_end(&zb);
589 return err;