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 {
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 done:
78 if (err)
79 got_inflate_end(zb);
80 return err;
81 }
83 const struct got_error *
84 got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
85 size_t *consumed)
86 {
87 size_t last_total_out = zb->z.total_out;
88 size_t last_total_in = zb->z.total_in;
89 z_stream *z = &zb->z;
90 int ret = Z_ERRNO;
92 z->next_out = zb->outbuf;
93 z->avail_out = zb->outlen;
95 *outlenp = 0;
96 if (consumed)
97 *consumed = 0;
98 do {
99 if (z->avail_in == 0) {
100 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
101 if (n == 0) {
102 if (ferror(f))
103 return got_ferror(f, GOT_ERR_IO);
104 /* EOF */
105 ret = Z_STREAM_END;
106 break;
108 z->next_in = zb->inbuf;
109 z->avail_in = n;
111 ret = inflate(z, Z_SYNC_FLUSH);
112 } while (ret == Z_OK && z->avail_out > 0);
114 if (ret == Z_OK || ret == Z_BUF_ERROR) {
115 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
116 } else {
117 if (ret != Z_STREAM_END)
118 return got_error(GOT_ERR_DECOMPRESSION);
119 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
122 *outlenp = z->total_out - last_total_out;
123 if (consumed)
124 *consumed += z->total_in - last_total_in;
125 return NULL;
128 const struct got_error *
129 got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
130 size_t *consumed)
132 size_t last_total_out = zb->z.total_out;
133 size_t last_total_in = zb->z.total_in;
134 z_stream *z = &zb->z;
135 int ret = Z_ERRNO;
137 z->next_out = zb->outbuf;
138 z->avail_out = zb->outlen;
140 *outlenp = 0;
141 if (consumed)
142 *consumed = 0;
143 do {
144 if (z->avail_in == 0) {
145 ssize_t n = read(fd, zb->inbuf, zb->inlen);
146 if (n < 0)
147 return got_error_from_errno("read");
148 else if (n == 0) {
149 /* EOF */
150 ret = Z_STREAM_END;
151 break;
153 z->next_in = zb->inbuf;
154 z->avail_in = n;
156 ret = inflate(z, Z_SYNC_FLUSH);
157 } while (ret == Z_OK && z->avail_out > 0);
159 if (ret == Z_OK || ret == Z_BUF_ERROR) {
160 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
161 } else {
162 if (ret != Z_STREAM_END)
163 return got_error(GOT_ERR_DECOMPRESSION);
164 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
167 *outlenp = z->total_out - last_total_out;
168 if (consumed)
169 *consumed += z->total_in - last_total_in;
170 return NULL;
173 const struct got_error *
174 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
175 size_t len, size_t *outlenp, size_t *consumed)
177 size_t last_total_out = zb->z.total_out;
178 z_stream *z = &zb->z;
179 int ret = Z_ERRNO;
181 z->next_out = zb->outbuf;
182 z->avail_out = zb->outlen;
184 *outlenp = 0;
185 *consumed = 0;
187 do {
188 size_t last_total_in = zb->z.total_in;
189 if (z->avail_in == 0) {
190 if (len == 0) {
191 /* EOF */
192 ret = Z_STREAM_END;
193 break;
195 z->next_in = map + offset + *consumed;
196 z->avail_in = len - *consumed;
198 ret = inflate(z, Z_SYNC_FLUSH);
199 *consumed += z->total_in - last_total_in;
200 } while (ret == Z_OK && z->avail_out > 0);
202 if (ret == Z_OK || ret == Z_BUF_ERROR) {
203 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
204 } else {
205 if (ret != Z_STREAM_END)
206 return got_error(GOT_ERR_DECOMPRESSION);
207 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
210 *outlenp = z->total_out - last_total_out;
211 return NULL;
214 void
215 got_inflate_end(struct got_inflate_buf *zb)
217 free(zb->inbuf);
218 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
219 free(zb->outbuf);
220 inflateEnd(&zb->z);
223 const struct got_error *
224 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
225 size_t *consumed_total, FILE *f)
227 const struct got_error *err;
228 size_t avail, consumed;
229 struct got_inflate_buf zb;
230 void *newbuf;
231 int nbuf = 1;
233 if (outbuf) {
234 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
235 if (*outbuf == NULL)
236 return got_error_from_errno("malloc");
237 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
238 } else
239 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
240 if (err)
241 return err;
243 *outlen = 0;
244 if (consumed_total)
245 *consumed_total = 0;
247 do {
248 err = got_inflate_read(&zb, f, &avail, &consumed);
249 if (err)
250 goto done;
251 *outlen += avail;
252 if (consumed_total)
253 *consumed_total += consumed;
254 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
255 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
256 if (outbuf == NULL)
257 continue;
258 newbuf = reallocarray(*outbuf, ++nbuf,
259 GOT_INFLATE_BUFSIZE);
260 if (newbuf == NULL) {
261 err = got_error_from_errno("reallocarray");
262 free(*outbuf);
263 *outbuf = NULL;
264 *outlen = 0;
265 goto done;
267 *outbuf = newbuf;
268 zb.outbuf = newbuf + *outlen;
270 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
272 done:
273 got_inflate_end(&zb);
274 return err;
277 const struct got_error *
278 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
279 size_t *consumed_total, int infd)
281 const struct got_error *err;
282 size_t avail, consumed;
283 struct got_inflate_buf zb;
284 void *newbuf;
285 int nbuf = 1;
287 if (outbuf) {
288 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
289 if (*outbuf == NULL)
290 return got_error_from_errno("malloc");
291 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
292 } else
293 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
294 if (err)
295 goto done;
297 *outlen = 0;
298 if (consumed_total)
299 *consumed_total = 0;
301 do {
302 err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
303 if (err)
304 goto done;
305 *outlen += avail;
306 if (consumed_total)
307 *consumed_total += consumed;
308 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
309 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
310 if (outbuf == NULL)
311 continue;
312 newbuf = reallocarray(*outbuf, ++nbuf,
313 GOT_INFLATE_BUFSIZE);
314 if (newbuf == NULL) {
315 err = got_error_from_errno("reallocarray");
316 free(*outbuf);
317 *outbuf = NULL;
318 *outlen = 0;
319 goto done;
321 *outbuf = newbuf;
322 zb.outbuf = newbuf + *outlen;
324 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
326 done:
327 got_inflate_end(&zb);
328 return err;
331 const struct got_error *
332 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
333 size_t offset, size_t len)
335 const struct got_error *err;
336 size_t avail, consumed;
337 struct got_inflate_buf zb;
338 void *newbuf;
339 int nbuf = 1;
341 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
342 if (*outbuf == NULL)
343 return got_error_from_errno("malloc");
344 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
345 if (err) {
346 free(*outbuf);
347 *outbuf = NULL;
348 return err;
351 *outlen = 0;
353 do {
354 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
355 &consumed);
356 if (err)
357 goto done;
358 offset += consumed;
359 len -= consumed;
360 *outlen += avail;
361 if (len == 0)
362 break;
363 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
364 newbuf = reallocarray(*outbuf, ++nbuf,
365 GOT_INFLATE_BUFSIZE);
366 if (newbuf == NULL) {
367 err = got_error_from_errno("reallocarray");
368 free(*outbuf);
369 *outbuf = NULL;
370 *outlen = 0;
371 goto done;
373 *outbuf = newbuf;
374 zb.outbuf = newbuf + *outlen;
375 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
377 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
378 done:
379 got_inflate_end(&zb);
380 return err;
383 const struct got_error *
384 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
386 const struct got_error *err = NULL;
387 size_t avail;
388 struct got_inflate_buf zb;
390 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
391 if (err)
392 goto done;
394 *outlen = 0;
396 do {
397 err = got_inflate_read(&zb, infile, &avail, NULL);
398 if (err)
399 goto done;
400 if (avail > 0) {
401 ssize_t n;
402 n = write(outfd, zb.outbuf, avail);
403 if (n != avail) {
404 err = got_error_from_errno("write");
405 goto done;
407 *outlen += avail;
409 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
411 done:
412 if (err == NULL) {
413 if (lseek(outfd, SEEK_SET, 0) == -1)
414 err = got_error_from_errno("lseek");
416 got_inflate_end(&zb);
417 return err;
420 const struct got_error *
421 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
423 const struct got_error *err;
424 size_t avail;
425 struct got_inflate_buf zb;
427 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
428 if (err)
429 goto done;
431 *outlen = 0;
433 do {
434 err = got_inflate_read(&zb, infile, &avail, NULL);
435 if (err)
436 goto done;
437 if (avail > 0) {
438 size_t n;
439 n = fwrite(zb.outbuf, avail, 1, outfile);
440 if (n != 1) {
441 err = got_ferror(outfile, GOT_ERR_IO);
442 goto done;
444 *outlen += avail;
446 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
448 done:
449 if (err == NULL)
450 rewind(outfile);
451 got_inflate_end(&zb);
452 return err;
455 const struct got_error *
456 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
458 const struct got_error *err;
459 size_t avail;
460 struct got_inflate_buf zb;
462 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
463 if (err)
464 goto done;
466 *outlen = 0;
468 do {
469 err = got_inflate_read_fd(&zb, infd, &avail, NULL);
470 if (err)
471 goto done;
472 if (avail > 0) {
473 size_t n;
474 n = fwrite(zb.outbuf, avail, 1, outfile);
475 if (n != 1) {
476 err = got_ferror(outfile, GOT_ERR_IO);
477 goto done;
479 *outlen += avail;
481 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
483 done:
484 if (err == NULL)
485 rewind(outfile);
486 got_inflate_end(&zb);
487 return err;
490 const struct got_error *
491 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
492 size_t len, FILE *outfile)
494 const struct got_error *err;
495 size_t avail;
496 struct got_inflate_buf zb;
497 size_t consumed;
499 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
500 if (err)
501 goto done;
503 *outlen = 0;
505 do {
506 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
507 &consumed);
508 if (err)
509 goto done;
510 offset += consumed;
511 len -= consumed;
512 if (avail > 0) {
513 size_t n;
514 n = fwrite(zb.outbuf, avail, 1, outfile);
515 if (n != 1) {
516 err = got_ferror(outfile, GOT_ERR_IO);
517 goto done;
519 *outlen += avail;
521 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
523 done:
524 if (err == NULL)
525 rewind(outfile);
526 got_inflate_end(&zb);
527 return err;