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)
131 size_t last_total_out = zb->z.total_out;
132 z_stream *z = &zb->z;
133 int ret = Z_ERRNO;
135 z->next_out = zb->outbuf;
136 z->avail_out = zb->outlen;
138 *outlenp = 0;
139 do {
140 if (z->avail_in == 0) {
141 ssize_t n = read(fd, zb->inbuf, zb->inlen);
142 if (n < 0)
143 return got_error_from_errno("read");
144 else if (n == 0) {
145 /* EOF */
146 ret = Z_STREAM_END;
147 break;
149 z->next_in = zb->inbuf;
150 z->avail_in = n;
152 ret = inflate(z, Z_SYNC_FLUSH);
153 } while (ret == Z_OK && z->avail_out > 0);
155 if (ret == Z_OK || ret == Z_BUF_ERROR) {
156 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
157 } else {
158 if (ret != Z_STREAM_END)
159 return got_error(GOT_ERR_DECOMPRESSION);
160 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
163 *outlenp = z->total_out - last_total_out;
164 return NULL;
167 const struct got_error *
168 got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
169 size_t len, size_t *outlenp, size_t *consumed)
171 size_t last_total_out = zb->z.total_out;
172 z_stream *z = &zb->z;
173 int ret = Z_ERRNO;
175 z->next_out = zb->outbuf;
176 z->avail_out = zb->outlen;
178 *outlenp = 0;
179 *consumed = 0;
181 do {
182 size_t last_total_in = zb->z.total_in;
183 if (z->avail_in == 0) {
184 if (len == 0) {
185 /* EOF */
186 ret = Z_STREAM_END;
187 break;
189 z->next_in = map + offset + *consumed;
190 z->avail_in = len - *consumed;
192 ret = inflate(z, Z_SYNC_FLUSH);
193 *consumed += z->total_in - last_total_in;
194 } while (ret == Z_OK && z->avail_out > 0);
196 if (ret == Z_OK || ret == Z_BUF_ERROR) {
197 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
198 } else {
199 if (ret != Z_STREAM_END)
200 return got_error(GOT_ERR_DECOMPRESSION);
201 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
204 *outlenp = z->total_out - last_total_out;
205 return NULL;
208 void
209 got_inflate_end(struct got_inflate_buf *zb)
211 free(zb->inbuf);
212 if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
213 free(zb->outbuf);
214 inflateEnd(&zb->z);
217 const struct got_error *
218 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
219 size_t *consumed_total, FILE *f)
221 const struct got_error *err;
222 size_t avail, consumed;
223 struct got_inflate_buf zb;
224 void *newbuf;
225 int nbuf = 1;
227 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
228 if (*outbuf == NULL)
229 return got_error_from_errno("malloc");
230 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
231 if (err)
232 return err;
234 *outlen = 0;
235 if (consumed_total)
236 *consumed_total = 0;
238 do {
239 err = got_inflate_read(&zb, f, &avail, &consumed);
240 if (err)
241 goto done;
242 *outlen += avail;
243 if (consumed_total)
244 *consumed_total += consumed;
245 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
246 newbuf = reallocarray(*outbuf, ++nbuf,
247 GOT_INFLATE_BUFSIZE);
248 if (newbuf == NULL) {
249 err = got_error_from_errno("reallocarray");
250 free(*outbuf);
251 *outbuf = NULL;
252 *outlen = 0;
253 goto done;
255 *outbuf = newbuf;
256 zb.outbuf = newbuf + *outlen;
257 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
259 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
261 done:
262 got_inflate_end(&zb);
263 return err;
266 const struct got_error *
267 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
269 const struct got_error *err;
270 size_t avail;
271 struct got_inflate_buf zb;
272 void *newbuf;
273 int nbuf = 1;
275 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
276 if (*outbuf == NULL)
277 return got_error_from_errno("malloc");
278 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
279 if (err)
280 goto done;
282 *outlen = 0;
284 do {
285 err = got_inflate_read_fd(&zb, infd, &avail);
286 if (err)
287 goto done;
288 *outlen += avail;
289 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
290 newbuf = reallocarray(*outbuf, ++nbuf,
291 GOT_INFLATE_BUFSIZE);
292 if (newbuf == NULL) {
293 err = got_error_from_errno("reallocarray");
294 free(*outbuf);
295 *outbuf = NULL;
296 *outlen = 0;
297 goto done;
299 *outbuf = newbuf;
300 zb.outbuf = newbuf + *outlen;
301 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
303 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
305 done:
306 got_inflate_end(&zb);
307 return err;
310 const struct got_error *
311 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
312 size_t offset, size_t len)
314 const struct got_error *err;
315 size_t avail, consumed;
316 struct got_inflate_buf zb;
317 void *newbuf;
318 int nbuf = 1;
320 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
321 if (*outbuf == NULL)
322 return got_error_from_errno("malloc");
323 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
324 if (err) {
325 free(*outbuf);
326 *outbuf = NULL;
327 return err;
330 *outlen = 0;
332 do {
333 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
334 &consumed);
335 if (err)
336 goto done;
337 offset += consumed;
338 len -= consumed;
339 *outlen += avail;
340 if (len == 0)
341 break;
342 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
343 newbuf = reallocarray(*outbuf, ++nbuf,
344 GOT_INFLATE_BUFSIZE);
345 if (newbuf == NULL) {
346 err = got_error_from_errno("reallocarray");
347 free(*outbuf);
348 *outbuf = NULL;
349 *outlen = 0;
350 goto done;
352 *outbuf = newbuf;
353 zb.outbuf = newbuf + *outlen;
354 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
356 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
357 done:
358 got_inflate_end(&zb);
359 return err;
362 const struct got_error *
363 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
365 const struct got_error *err = NULL;
366 size_t avail;
367 struct got_inflate_buf zb;
369 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
370 if (err)
371 goto done;
373 *outlen = 0;
375 do {
376 err = got_inflate_read(&zb, infile, &avail, NULL);
377 if (err)
378 goto done;
379 if (avail > 0) {
380 ssize_t n;
381 n = write(outfd, zb.outbuf, avail);
382 if (n != avail) {
383 err = got_error_from_errno("write");
384 goto done;
386 *outlen += avail;
388 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
390 done:
391 if (err == NULL) {
392 if (lseek(outfd, SEEK_SET, 0) == -1)
393 err = got_error_from_errno("lseek");
395 got_inflate_end(&zb);
396 return err;
399 const struct got_error *
400 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
402 const struct got_error *err;
403 size_t avail;
404 struct got_inflate_buf zb;
406 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
407 if (err)
408 goto done;
410 *outlen = 0;
412 do {
413 err = got_inflate_read(&zb, infile, &avail, NULL);
414 if (err)
415 goto done;
416 if (avail > 0) {
417 size_t n;
418 n = fwrite(zb.outbuf, avail, 1, outfile);
419 if (n != 1) {
420 err = got_ferror(outfile, GOT_ERR_IO);
421 goto done;
423 *outlen += avail;
425 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
427 done:
428 if (err == NULL)
429 rewind(outfile);
430 got_inflate_end(&zb);
431 return err;
434 const struct got_error *
435 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
437 const struct got_error *err;
438 size_t avail;
439 struct got_inflate_buf zb;
441 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
442 if (err)
443 goto done;
445 *outlen = 0;
447 do {
448 err = got_inflate_read_fd(&zb, infd, &avail);
449 if (err)
450 goto done;
451 if (avail > 0) {
452 size_t n;
453 n = fwrite(zb.outbuf, avail, 1, outfile);
454 if (n != 1) {
455 err = got_ferror(outfile, GOT_ERR_IO);
456 goto done;
458 *outlen += avail;
460 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
462 done:
463 if (err == NULL)
464 rewind(outfile);
465 got_inflate_end(&zb);
466 return err;
469 const struct got_error *
470 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
471 size_t len, FILE *outfile)
473 const struct got_error *err;
474 size_t avail;
475 struct got_inflate_buf zb;
476 size_t consumed;
478 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
479 if (err)
480 goto done;
482 *outlen = 0;
484 do {
485 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
486 &consumed);
487 if (err)
488 goto done;
489 offset += consumed;
490 len -= consumed;
491 if (avail > 0) {
492 size_t n;
493 n = fwrite(zb.outbuf, avail, 1, outfile);
494 if (n != 1) {
495 err = got_ferror(outfile, GOT_ERR_IO);
496 goto done;
498 *outlen += avail;
500 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
502 done:
503 if (err == NULL)
504 rewind(outfile);
505 got_inflate_end(&zb);
506 return err;