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 {
86 size_t last_total_out = zb->z.total_out;
87 z_stream *z = &zb->z;
88 int ret = Z_ERRNO;
89 off_t off, consumed;
91 z->next_out = zb->outbuf;
92 z->avail_out = zb->outlen;
94 *outlenp = 0;
95 off = ftello(f);
96 consumed = 0;
97 do {
98 size_t last_total_in = zb->z.total_in;
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 consumed += z->total_in - last_total_in;
113 } while (ret == Z_OK && z->avail_out > 0);
115 if (ret == Z_OK || ret == Z_BUF_ERROR) {
116 zb->flags |= GOT_INFLATE_F_HAVE_MORE;
117 } else {
118 if (ret != Z_STREAM_END)
119 return got_error(GOT_ERR_DECOMPRESSION);
120 zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
123 *outlenp = z->total_out - last_total_out;
124 fseek(f, off + consumed, SEEK_SET);
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, FILE *f)
220 const struct got_error *err;
221 size_t avail;
222 struct got_inflate_buf zb;
223 void *newbuf;
224 int nbuf = 1;
226 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
227 if (*outbuf == NULL)
228 return got_error_from_errno("malloc");
229 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
230 if (err)
231 return err;
233 *outlen = 0;
235 do {
236 err = got_inflate_read(&zb, f, &avail);
237 if (err)
238 goto done;
239 *outlen += avail;
240 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
241 newbuf = reallocarray(*outbuf, ++nbuf,
242 GOT_INFLATE_BUFSIZE);
243 if (newbuf == NULL) {
244 err = got_error_from_errno("reallocarray");
245 free(*outbuf);
246 *outbuf = NULL;
247 *outlen = 0;
248 goto done;
250 *outbuf = newbuf;
251 zb.outbuf = newbuf + *outlen;
252 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
254 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
256 done:
257 got_inflate_end(&zb);
258 return err;
261 const struct got_error *
262 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
264 const struct got_error *err;
265 size_t avail;
266 struct got_inflate_buf zb;
267 void *newbuf;
268 int nbuf = 1;
270 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
271 if (*outbuf == NULL)
272 return got_error_from_errno("malloc");
273 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
274 if (err)
275 goto done;
277 *outlen = 0;
279 do {
280 err = got_inflate_read_fd(&zb, infd, &avail);
281 if (err)
282 goto done;
283 *outlen += avail;
284 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
285 newbuf = reallocarray(*outbuf, ++nbuf,
286 GOT_INFLATE_BUFSIZE);
287 if (newbuf == NULL) {
288 err = got_error_from_errno("reallocarray");
289 free(*outbuf);
290 *outbuf = NULL;
291 *outlen = 0;
292 goto done;
294 *outbuf = newbuf;
295 zb.outbuf = newbuf + *outlen;
296 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
298 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
300 done:
301 got_inflate_end(&zb);
302 return err;
305 const struct got_error *
306 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
307 size_t offset, size_t len)
309 const struct got_error *err;
310 size_t avail, consumed;
311 struct got_inflate_buf zb;
312 void *newbuf;
313 int nbuf = 1;
315 *outbuf = malloc(GOT_INFLATE_BUFSIZE);
316 if (*outbuf == NULL)
317 return got_error_from_errno("malloc");
318 err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
319 if (err) {
320 free(*outbuf);
321 *outbuf = NULL;
322 return err;
325 *outlen = 0;
327 do {
328 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
329 &consumed);
330 if (err)
331 goto done;
332 offset += consumed;
333 len -= consumed;
334 *outlen += avail;
335 if (len == 0)
336 break;
337 if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
338 newbuf = reallocarray(*outbuf, ++nbuf,
339 GOT_INFLATE_BUFSIZE);
340 if (newbuf == NULL) {
341 err = got_error_from_errno("reallocarray");
342 free(*outbuf);
343 *outbuf = NULL;
344 *outlen = 0;
345 goto done;
347 *outbuf = newbuf;
348 zb.outbuf = newbuf + *outlen;
349 zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
351 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
352 done:
353 got_inflate_end(&zb);
354 return err;
357 const struct got_error *
358 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
360 const struct got_error *err = NULL;
361 size_t avail;
362 struct got_inflate_buf zb;
364 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
365 if (err)
366 goto done;
368 *outlen = 0;
370 do {
371 err = got_inflate_read(&zb, infile, &avail);
372 if (err)
373 goto done;
374 if (avail > 0) {
375 ssize_t n;
376 n = write(outfd, zb.outbuf, avail);
377 if (n != avail) {
378 err = got_error_from_errno("write");
379 goto done;
381 *outlen += avail;
383 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
385 done:
386 if (err == NULL) {
387 if (lseek(outfd, SEEK_SET, 0) == -1)
388 err = got_error_from_errno("lseek");
390 got_inflate_end(&zb);
391 return err;
394 const struct got_error *
395 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
397 const struct got_error *err;
398 size_t avail;
399 struct got_inflate_buf zb;
401 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
402 if (err)
403 goto done;
405 *outlen = 0;
407 do {
408 err = got_inflate_read(&zb, infile, &avail);
409 if (err)
410 goto done;
411 if (avail > 0) {
412 size_t n;
413 n = fwrite(zb.outbuf, avail, 1, outfile);
414 if (n != 1) {
415 err = got_ferror(outfile, GOT_ERR_IO);
416 goto done;
418 *outlen += avail;
420 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
422 done:
423 if (err == NULL)
424 rewind(outfile);
425 got_inflate_end(&zb);
426 return err;
429 const struct got_error *
430 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
432 const struct got_error *err;
433 size_t avail;
434 struct got_inflate_buf zb;
436 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
437 if (err)
438 goto done;
440 *outlen = 0;
442 do {
443 err = got_inflate_read_fd(&zb, infd, &avail);
444 if (err)
445 goto done;
446 if (avail > 0) {
447 size_t n;
448 n = fwrite(zb.outbuf, avail, 1, outfile);
449 if (n != 1) {
450 err = got_ferror(outfile, GOT_ERR_IO);
451 goto done;
453 *outlen += avail;
455 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
457 done:
458 if (err == NULL)
459 rewind(outfile);
460 got_inflate_end(&zb);
461 return err;
464 const struct got_error *
465 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
466 size_t len, FILE *outfile)
468 const struct got_error *err;
469 size_t avail;
470 struct got_inflate_buf zb;
471 size_t consumed;
473 err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
474 if (err)
475 goto done;
477 *outlen = 0;
479 do {
480 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
481 &consumed);
482 if (err)
483 goto done;
484 offset += consumed;
485 len -= consumed;
486 if (avail > 0) {
487 size_t n;
488 n = fwrite(zb.outbuf, avail, 1, outfile);
489 if (n != 1) {
490 err = got_ferror(outfile, GOT_ERR_IO);
491 goto done;
493 *outlen += avail;
495 } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
497 done:
498 if (err == NULL)
499 rewind(outfile);
500 got_inflate_end(&zb);
501 return err;