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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sha1.h>
23 #include <zlib.h>
24 #include <time.h>
26 #include "got_error.h"
27 #include "got_object.h"
29 #include "got_lib_path.h"
30 #include "got_lib_inflate.h"
32 #ifndef MIN
33 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
34 #endif
36 const struct got_error *
37 got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
38 {
39 const struct got_error *err = NULL;
41 memset(&zb->z, 0, sizeof(zb->z));
43 zb->z.zalloc = Z_NULL;
44 zb->z.zfree = Z_NULL;
45 if (inflateInit(&zb->z) != Z_OK) {
46 err = got_error(GOT_ERR_IO);
47 goto done;
48 }
50 zb->inlen = zb->outlen = bufsize;
52 zb->inbuf = calloc(1, zb->inlen);
53 if (zb->inbuf == NULL) {
54 err = got_error_from_errno();
55 goto done;
56 }
58 zb->flags = 0;
59 if (outbuf == NULL) {
60 zb->outbuf = calloc(1, zb->outlen);
61 if (zb->outbuf == NULL) {
62 err = got_error_from_errno();
63 goto done;
64 }
65 zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
66 } else
67 zb->outbuf = outbuf;
69 done:
70 if (err)
71 got_inflate_end(zb);
72 return err;
73 }
75 const struct got_error *
76 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
77 {
78 size_t last_total_out = zb->z.total_out;
79 z_stream *z = &zb->z;
80 int ret = Z_ERRNO;
82 z->next_out = zb->outbuf;
83 z->avail_out = zb->outlen;
85 *outlenp = 0;
86 do {
87 if (z->avail_in == 0) {
88 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
89 if (n == 0) {
90 if (ferror(f))
91 return got_ferror(f, GOT_ERR_IO);
92 /* EOF */
93 ret = Z_STREAM_END;
94 break;
95 }
96 z->next_in = zb->inbuf;
97 z->avail_in = n;
98 }
99 ret = inflate(z, Z_SYNC_FLUSH);
100 } while (ret == Z_OK && z->avail_out > 0);
102 if (ret == Z_OK) {
103 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
104 } else {
105 if (ret != Z_STREAM_END)
106 return got_error(GOT_ERR_DECOMPRESSION);
107 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
110 *outlenp = z->total_out - last_total_out;
111 return NULL;
114 const struct got_error *
115 got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
117 size_t last_total_out = zb->z.total_out;
118 z_stream *z = &zb->z;
119 int ret = Z_ERRNO;
121 z->next_out = zb->outbuf;
122 z->avail_out = zb->outlen;
124 *outlenp = 0;
125 do {
126 if (z->avail_in == 0) {
127 ssize_t n = read(fd, zb->inbuf, zb->inlen);
128 if (n < 0)
129 return got_error_from_errno();
130 else if (n == 0) {
131 /* EOF */
132 ret = Z_STREAM_END;
133 break;
135 z->next_in = zb->inbuf;
136 z->avail_in = n;
138 ret = inflate(z, Z_SYNC_FLUSH);
139 } while (ret == Z_OK && z->avail_out > 0);
141 if (ret == Z_OK) {
142 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
143 } else {
144 if (ret != Z_STREAM_END)
145 return got_error(GOT_ERR_DECOMPRESSION);
146 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
149 *outlenp = z->total_out - last_total_out;
150 return NULL;
153 const struct got_error *
154 got_inflate_read_mmap(struct got_zstream_buf *zb, uint8_t *map, size_t offset,
155 size_t len, size_t *outlenp, size_t *consumed)
157 size_t last_total_out = zb->z.total_out;
158 z_stream *z = &zb->z;
159 int ret = Z_ERRNO;
161 z->next_out = zb->outbuf;
162 z->avail_out = zb->outlen;
164 *outlenp = 0;
165 *consumed = 0;
167 do {
168 size_t last_total_in = zb->z.total_in;
169 if (z->avail_in == 0) {
170 if (len == 0) {
171 /* EOF */
172 ret = Z_STREAM_END;
173 break;
175 z->next_in = map + offset + *consumed;
176 z->avail_in = MIN(zb->inlen, len);
177 len -= z->avail_in;
179 ret = inflate(z, Z_SYNC_FLUSH);
180 *consumed += z->total_in - last_total_in;
181 } while (ret == Z_OK && z->avail_out > 0);
183 if (ret == Z_OK) {
184 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
185 } else {
186 if (ret != Z_STREAM_END)
187 return got_error(GOT_ERR_DECOMPRESSION);
188 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
191 *outlenp = z->total_out - last_total_out;
192 return NULL;
195 void
196 got_inflate_end(struct got_zstream_buf *zb)
198 free(zb->inbuf);
199 if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
200 free(zb->outbuf);
201 inflateEnd(&zb->z);
204 const struct got_error *
205 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
207 const struct got_error *err;
208 size_t avail;
209 struct got_zstream_buf zb;
210 void *newbuf;
211 int nbuf = 1;
213 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
214 if (*outbuf == NULL)
215 return got_error_from_errno();
216 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
217 if (err)
218 return err;
220 *outlen = 0;
222 do {
223 err = got_inflate_read(&zb, f, &avail);
224 if (err)
225 goto done;
226 *outlen += avail;
227 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
228 nbuf++;
229 newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
230 GOT_ZSTREAM_BUFSIZE);
231 if (newbuf == NULL) {
232 err = got_error_from_errno();
233 free(*outbuf);
234 *outbuf = NULL;
235 *outlen = 0;
236 goto done;
238 *outbuf = newbuf;
239 zb.outbuf = newbuf + *outlen;
240 zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
242 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
244 done:
245 got_inflate_end(&zb);
246 return err;
249 const struct got_error *
250 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
252 const struct got_error *err;
253 size_t avail;
254 struct got_zstream_buf zb;
255 void *newbuf;
256 int nbuf = 1;
258 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
259 if (*outbuf == NULL)
260 return got_error_from_errno();
261 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
262 if (err)
263 goto done;
265 *outlen = 0;
267 do {
268 err = got_inflate_read_fd(&zb, infd, &avail);
269 if (err)
270 goto done;
271 *outlen += avail;
272 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
273 nbuf++;
274 newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
275 GOT_ZSTREAM_BUFSIZE);
276 if (newbuf == NULL) {
277 err = got_error_from_errno();
278 free(*outbuf);
279 *outbuf = NULL;
280 *outlen = 0;
281 goto done;
283 *outbuf = newbuf;
284 zb.outbuf = newbuf + *outlen;
285 zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
287 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
289 done:
290 got_inflate_end(&zb);
291 return err;
294 const struct got_error *
295 got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
296 size_t offset, size_t len)
298 const struct got_error *err;
299 size_t avail, consumed;
300 struct got_zstream_buf zb;
301 void *newbuf;
302 int nbuf = 1;
304 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
305 if (*outbuf == NULL)
306 return got_error_from_errno();
307 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
308 if (err) {
309 free(*outbuf);
310 *outbuf = NULL;
311 return err;
314 *outlen = 0;
316 do {
317 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
318 &consumed);
319 if (err)
320 goto done;
321 offset += consumed;
322 len -= consumed;
323 *outlen += avail;
324 if (len == 0)
325 break;
326 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
327 nbuf++;
328 newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
329 GOT_ZSTREAM_BUFSIZE);
330 if (newbuf == NULL) {
331 err = got_error_from_errno();
332 free(*outbuf);
333 *outbuf = NULL;
334 *outlen = 0;
335 goto done;
337 *outbuf = newbuf;
338 zb.outbuf = newbuf + *outlen;
339 zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
341 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
342 done:
343 got_inflate_end(&zb);
344 return err;
347 const struct got_error *
348 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
350 const struct got_error *err = NULL;
351 size_t avail;
352 struct got_zstream_buf zb;
354 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
355 if (err)
356 goto done;
358 *outlen = 0;
360 do {
361 err = got_inflate_read(&zb, infile, &avail);
362 if (err)
363 goto done;
364 if (avail > 0) {
365 ssize_t n;
366 n = write(outfd, zb.outbuf, avail);
367 if (n != avail) {
368 err = got_error_from_errno();
369 goto done;
371 *outlen += avail;
373 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
375 done:
376 if (err == NULL) {
377 if (lseek(outfd, SEEK_SET, 0) == -1)
378 err = got_error_from_errno();
380 got_inflate_end(&zb);
381 return err;
384 const struct got_error *
385 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
387 const struct got_error *err;
388 size_t avail;
389 struct got_zstream_buf zb;
391 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
392 if (err)
393 goto done;
395 *outlen = 0;
397 do {
398 err = got_inflate_read(&zb, infile, &avail);
399 if (err)
400 goto done;
401 if (avail > 0) {
402 size_t n;
403 n = fwrite(zb.outbuf, avail, 1, outfile);
404 if (n != 1) {
405 err = got_ferror(outfile, GOT_ERR_IO);
406 goto done;
408 *outlen += avail;
410 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
412 done:
413 if (err == NULL)
414 rewind(outfile);
415 got_inflate_end(&zb);
416 return err;
419 const struct got_error *
420 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
422 const struct got_error *err;
423 size_t avail;
424 struct got_zstream_buf zb;
426 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
427 if (err)
428 goto done;
430 *outlen = 0;
432 do {
433 err = got_inflate_read_fd(&zb, infd, &avail);
434 if (err)
435 goto done;
436 if (avail > 0) {
437 size_t n;
438 n = fwrite(zb.outbuf, avail, 1, outfile);
439 if (n != 1) {
440 err = got_ferror(outfile, GOT_ERR_IO);
441 goto done;
443 *outlen += avail;
445 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
447 done:
448 if (err == NULL)
449 rewind(outfile);
450 got_inflate_end(&zb);
451 return err;
454 const struct got_error *
455 got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
456 size_t len, FILE *outfile)
458 const struct got_error *err;
459 size_t avail;
460 struct got_zstream_buf zb;
461 size_t consumed;
463 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
464 if (err)
465 goto done;
467 *outlen = 0;
469 do {
470 err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
471 &consumed);
472 if (err)
473 goto done;
474 offset += consumed;
475 len -= consumed;
476 if (avail > 0) {
477 size_t n;
478 n = fwrite(zb.outbuf, avail, 1, outfile);
479 if (n != 1) {
480 err = got_ferror(outfile, GOT_ERR_IO);
481 goto done;
483 *outlen += avail;
485 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
487 done:
488 if (err == NULL)
489 rewind(outfile);
490 got_inflate_end(&zb);
491 return err;