2 2181e0c8 2019-03-19 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 2181e0c8 2019-03-19 stsp * Permission to use, copy, modify, and distribute this software for any
5 2181e0c8 2019-03-19 stsp * purpose with or without fee is hereby granted, provided that the above
6 2181e0c8 2019-03-19 stsp * copyright notice and this permission notice appear in all copies.
8 2181e0c8 2019-03-19 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2181e0c8 2019-03-19 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2181e0c8 2019-03-19 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2181e0c8 2019-03-19 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2181e0c8 2019-03-19 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2181e0c8 2019-03-19 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2181e0c8 2019-03-19 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 2181e0c8 2019-03-19 stsp #include <errno.h>
20 2181e0c8 2019-03-19 stsp #include <stdio.h>
21 2181e0c8 2019-03-19 stsp #include <stdlib.h>
22 2181e0c8 2019-03-19 stsp #include <string.h>
23 2181e0c8 2019-03-19 stsp #include <zlib.h>
24 2181e0c8 2019-03-19 stsp #include <time.h>
26 2181e0c8 2019-03-19 stsp #include "got_error.h"
27 2181e0c8 2019-03-19 stsp #include "got_object.h"
28 324d37e7 2019-05-11 stsp #include "got_path.h"
30 2181e0c8 2019-03-19 stsp #include "got_lib_deflate.h"
31 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
34 2181e0c8 2019-03-19 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 2181e0c8 2019-03-19 stsp const struct got_error *
38 3b9e6fcf 2021-06-05 stsp got_deflate_init(struct got_deflate_buf *zb, uint8_t *outbuf, size_t bufsize)
40 2181e0c8 2019-03-19 stsp const struct got_error *err = NULL;
43 0699dbc2 2022-11-08 thomas memset(zb, 0, sizeof(*zb));
45 2181e0c8 2019-03-19 stsp zb->z.zalloc = Z_NULL;
46 2181e0c8 2019-03-19 stsp zb->z.zfree = Z_NULL;
47 2181e0c8 2019-03-19 stsp zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
48 2181e0c8 2019-03-19 stsp if (zerr != Z_OK) {
49 2181e0c8 2019-03-19 stsp if (zerr == Z_ERRNO)
50 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
51 2181e0c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
52 2181e0c8 2019-03-19 stsp errno = ENOMEM;
53 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
55 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
58 2181e0c8 2019-03-19 stsp zb->inlen = zb->outlen = bufsize;
60 2181e0c8 2019-03-19 stsp zb->inbuf = calloc(1, zb->inlen);
61 2181e0c8 2019-03-19 stsp if (zb->inbuf == NULL) {
62 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
66 2181e0c8 2019-03-19 stsp zb->flags = 0;
67 2181e0c8 2019-03-19 stsp if (outbuf == NULL) {
68 2181e0c8 2019-03-19 stsp zb->outbuf = calloc(1, zb->outlen);
69 2181e0c8 2019-03-19 stsp if (zb->outbuf == NULL) {
70 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
73 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
75 2181e0c8 2019-03-19 stsp zb->outbuf = outbuf;
78 2181e0c8 2019-03-19 stsp got_deflate_end(zb);
83 dbaa2362 2021-09-28 thomas csum_output(struct got_deflate_checksum *csum, const uint8_t *buf, size_t len)
85 91b40e30 2021-05-21 stsp if (csum->output_crc)
86 91b40e30 2021-05-21 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
88 91b40e30 2021-05-21 stsp if (csum->output_sha1)
89 91b40e30 2021-05-21 stsp SHA1Update(csum->output_sha1, buf, len);
92 2181e0c8 2019-03-19 stsp const struct got_error *
93 e8f02263 2022-01-23 thomas got_deflate_read(struct got_deflate_buf *zb, FILE *f, off_t len,
94 e8f02263 2022-01-23 thomas size_t *outlenp, off_t *consumed)
96 2181e0c8 2019-03-19 stsp size_t last_total_out = zb->z.total_out;
97 2181e0c8 2019-03-19 stsp z_stream *z = &zb->z;
98 2181e0c8 2019-03-19 stsp int ret = Z_ERRNO;
100 2181e0c8 2019-03-19 stsp z->next_out = zb->outbuf;
101 2181e0c8 2019-03-19 stsp z->avail_out = zb->outlen;
103 2181e0c8 2019-03-19 stsp *outlenp = 0;
104 e8f02263 2022-01-23 thomas *consumed = 0;
106 e8f02263 2022-01-23 thomas size_t last_total_in = z->total_in;
107 2181e0c8 2019-03-19 stsp if (z->avail_in == 0) {
108 e8f02263 2022-01-23 thomas size_t n = 0;
109 e8f02263 2022-01-23 thomas if (*consumed < len) {
110 e8f02263 2022-01-23 thomas n = fread(zb->inbuf, 1,
111 e8f02263 2022-01-23 thomas MIN(zb->inlen, len - *consumed), f);
113 2181e0c8 2019-03-19 stsp if (n == 0) {
114 2181e0c8 2019-03-19 stsp if (ferror(f))
115 2181e0c8 2019-03-19 stsp return got_ferror(f, GOT_ERR_IO);
117 4e4a7005 2019-04-13 stsp ret = deflate(z, Z_FINISH);
120 2181e0c8 2019-03-19 stsp z->next_in = zb->inbuf;
121 2181e0c8 2019-03-19 stsp z->avail_in = n;
123 2181e0c8 2019-03-19 stsp ret = deflate(z, Z_NO_FLUSH);
124 e8f02263 2022-01-23 thomas *consumed += z->total_in - last_total_in;
125 2181e0c8 2019-03-19 stsp } while (ret == Z_OK && z->avail_out > 0);
127 2181e0c8 2019-03-19 stsp if (ret == Z_OK) {
128 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
130 2181e0c8 2019-03-19 stsp if (ret != Z_STREAM_END)
131 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
132 2181e0c8 2019-03-19 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
135 2181e0c8 2019-03-19 stsp *outlenp = z->total_out - last_total_out;
136 2181e0c8 2019-03-19 stsp return NULL;
139 9249e7e3 2022-05-12 thomas static const struct got_error *
140 9249e7e3 2022-05-12 thomas deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
141 9249e7e3 2022-05-12 thomas size_t len, size_t *outlenp, size_t *consumed, int flush_on_eof)
143 2b0ae357 2022-01-10 thomas z_stream *z = &zb->z;
144 2b0ae357 2022-01-10 thomas size_t last_total_out = z->total_out;
145 2b0ae357 2022-01-10 thomas int ret = Z_ERRNO;
147 2b0ae357 2022-01-10 thomas z->next_out = zb->outbuf;
148 2b0ae357 2022-01-10 thomas z->avail_out = zb->outlen;
150 2b0ae357 2022-01-10 thomas *outlenp = 0;
151 2b0ae357 2022-01-10 thomas *consumed = 0;
153 2b0ae357 2022-01-10 thomas size_t last_total_in = z->total_in;
154 2b0ae357 2022-01-10 thomas if (z->avail_in == 0) {
155 2b0ae357 2022-01-10 thomas z->next_in = map + offset + *consumed;
156 f6a55b40 2022-02-12 thomas if (len - *consumed > UINT_MAX)
157 f6a55b40 2022-02-12 thomas z->avail_in = UINT_MAX;
159 f6a55b40 2022-02-12 thomas z->avail_in = len - *consumed;
160 2b0ae357 2022-01-10 thomas if (z->avail_in == 0) {
161 2b0ae357 2022-01-10 thomas /* EOF */
162 9249e7e3 2022-05-12 thomas if (flush_on_eof)
163 9249e7e3 2022-05-12 thomas ret = deflate(z, Z_FINISH);
167 2b0ae357 2022-01-10 thomas ret = deflate(z, Z_NO_FLUSH);
168 2b0ae357 2022-01-10 thomas *consumed += z->total_in - last_total_in;
169 2b0ae357 2022-01-10 thomas } while (ret == Z_OK && z->avail_out > 0);
171 2b0ae357 2022-01-10 thomas if (ret == Z_OK) {
172 2b0ae357 2022-01-10 thomas zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
174 2b0ae357 2022-01-10 thomas if (ret != Z_STREAM_END)
175 2b0ae357 2022-01-10 thomas return got_error(GOT_ERR_COMPRESSION);
176 2b0ae357 2022-01-10 thomas zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
179 2b0ae357 2022-01-10 thomas *outlenp = z->total_out - last_total_out;
180 9249e7e3 2022-05-12 thomas return NULL;
183 9249e7e3 2022-05-12 thomas const struct got_error *
184 9249e7e3 2022-05-12 thomas got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
185 9249e7e3 2022-05-12 thomas size_t len, size_t *outlenp, size_t *consumed)
187 9249e7e3 2022-05-12 thomas return deflate_read_mmap(zb, map, offset, len, outlenp, consumed, 1);
190 9249e7e3 2022-05-12 thomas const struct got_error *
191 9249e7e3 2022-05-12 thomas got_deflate_flush(struct got_deflate_buf *zb, FILE *outfile,
192 9249e7e3 2022-05-12 thomas struct got_deflate_checksum *csum, off_t *outlenp)
195 9249e7e3 2022-05-12 thomas size_t n;
196 9249e7e3 2022-05-12 thomas z_stream *z = &zb->z;
198 9249e7e3 2022-05-12 thomas if (z->avail_in != 0)
199 9249e7e3 2022-05-12 thomas return got_error_msg(GOT_ERR_COMPRESSION,
200 9249e7e3 2022-05-12 thomas "cannot flush zb with pending input data");
203 9249e7e3 2022-05-12 thomas size_t avail, last_total_out = zb->z.total_out;
205 9249e7e3 2022-05-12 thomas z->next_out = zb->outbuf;
206 9249e7e3 2022-05-12 thomas z->avail_out = zb->outlen;
208 9249e7e3 2022-05-12 thomas ret = deflate(z, Z_FINISH);
209 9249e7e3 2022-05-12 thomas if (ret != Z_STREAM_END && ret != Z_OK)
210 9249e7e3 2022-05-12 thomas return got_error(GOT_ERR_COMPRESSION);
212 9249e7e3 2022-05-12 thomas avail = z->total_out - last_total_out;
213 9249e7e3 2022-05-12 thomas if (avail > 0) {
214 9249e7e3 2022-05-12 thomas n = fwrite(zb->outbuf, avail, 1, outfile);
215 9249e7e3 2022-05-12 thomas if (n != 1)
216 9249e7e3 2022-05-12 thomas return got_ferror(outfile, GOT_ERR_IO);
217 9249e7e3 2022-05-12 thomas if (csum)
218 9249e7e3 2022-05-12 thomas csum_output(csum, zb->outbuf, avail);
219 9249e7e3 2022-05-12 thomas if (outlenp)
220 9249e7e3 2022-05-12 thomas *outlenp += avail;
222 9249e7e3 2022-05-12 thomas } while (ret != Z_STREAM_END);
224 9249e7e3 2022-05-12 thomas zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
225 2b0ae357 2022-01-10 thomas return NULL;
229 2181e0c8 2019-03-19 stsp got_deflate_end(struct got_deflate_buf *zb)
231 2181e0c8 2019-03-19 stsp free(zb->inbuf);
232 2181e0c8 2019-03-19 stsp if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
233 2181e0c8 2019-03-19 stsp free(zb->outbuf);
234 2181e0c8 2019-03-19 stsp deflateEnd(&zb->z);
237 20a7d452 2022-10-16 thomas const struct got_error *
238 20a7d452 2022-10-16 thomas got_deflate_to_fd(off_t *outlen, FILE *infile, off_t len, int outfd,
239 20a7d452 2022-10-16 thomas struct got_deflate_checksum *csum)
241 20a7d452 2022-10-16 thomas const struct got_error *err;
242 20a7d452 2022-10-16 thomas size_t avail;
243 20a7d452 2022-10-16 thomas off_t consumed;
244 20a7d452 2022-10-16 thomas struct got_deflate_buf zb;
246 20a7d452 2022-10-16 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
248 20a7d452 2022-10-16 thomas goto done;
250 20a7d452 2022-10-16 thomas *outlen = 0;
253 20a7d452 2022-10-16 thomas err = got_deflate_read(&zb, infile, len, &avail, &consumed);
255 20a7d452 2022-10-16 thomas goto done;
256 20a7d452 2022-10-16 thomas len -= consumed;
257 20a7d452 2022-10-16 thomas if (avail > 0) {
258 3efd8e31 2022-10-23 thomas err = got_poll_write_full(outfd, zb.outbuf, avail);
260 20a7d452 2022-10-16 thomas goto done;
261 20a7d452 2022-10-16 thomas if (csum)
262 20a7d452 2022-10-16 thomas csum_output(csum, zb.outbuf, avail);
263 20a7d452 2022-10-16 thomas *outlen += avail;
265 20a7d452 2022-10-16 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
268 20a7d452 2022-10-16 thomas got_deflate_end(&zb);
269 20a7d452 2022-10-16 thomas return err;
272 20a7d452 2022-10-16 thomas const struct got_error *
273 20a7d452 2022-10-16 thomas got_deflate_to_fd_mmap(off_t *outlen, uint8_t *map, size_t offset,
274 20a7d452 2022-10-16 thomas size_t len, int outfd, struct got_deflate_checksum *csum)
276 20a7d452 2022-10-16 thomas const struct got_error *err;
277 20a7d452 2022-10-16 thomas size_t avail, consumed;
278 20a7d452 2022-10-16 thomas struct got_deflate_buf zb;
280 20a7d452 2022-10-16 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
282 20a7d452 2022-10-16 thomas goto done;
284 20a7d452 2022-10-16 thomas *outlen = 0;
286 20a7d452 2022-10-16 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
287 20a7d452 2022-10-16 thomas &consumed);
289 20a7d452 2022-10-16 thomas goto done;
290 20a7d452 2022-10-16 thomas offset += consumed;
291 20a7d452 2022-10-16 thomas len -= consumed;
292 20a7d452 2022-10-16 thomas if (avail > 0) {
293 3efd8e31 2022-10-23 thomas err = got_poll_write_full(outfd, zb.outbuf, avail);
295 20a7d452 2022-10-16 thomas goto done;
296 20a7d452 2022-10-16 thomas if (csum)
297 20a7d452 2022-10-16 thomas csum_output(csum, zb.outbuf, avail);
298 20a7d452 2022-10-16 thomas *outlen += avail;
300 20a7d452 2022-10-16 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
303 20a7d452 2022-10-16 thomas got_deflate_end(&zb);
304 20a7d452 2022-10-16 thomas return err;
307 2181e0c8 2019-03-19 stsp const struct got_error *
308 e8f02263 2022-01-23 thomas got_deflate_to_file(off_t *outlen, FILE *infile, off_t len,
309 e8f02263 2022-01-23 thomas FILE *outfile, struct got_deflate_checksum *csum)
311 2181e0c8 2019-03-19 stsp const struct got_error *err;
312 2181e0c8 2019-03-19 stsp size_t avail;
313 e8f02263 2022-01-23 thomas off_t consumed;
314 2181e0c8 2019-03-19 stsp struct got_deflate_buf zb;
316 3b9e6fcf 2021-06-05 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
320 2181e0c8 2019-03-19 stsp *outlen = 0;
323 e8f02263 2022-01-23 thomas err = got_deflate_read(&zb, infile, len, &avail, &consumed);
326 e8f02263 2022-01-23 thomas len -= consumed;
327 2181e0c8 2019-03-19 stsp if (avail > 0) {
329 2181e0c8 2019-03-19 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
330 2181e0c8 2019-03-19 stsp if (n != 1) {
331 2181e0c8 2019-03-19 stsp err = got_ferror(outfile, GOT_ERR_IO);
335 3b9e6fcf 2021-06-05 stsp csum_output(csum, zb.outbuf, avail);
336 2181e0c8 2019-03-19 stsp *outlen += avail;
338 2181e0c8 2019-03-19 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
341 2181e0c8 2019-03-19 stsp got_deflate_end(&zb);
342 2181e0c8 2019-03-19 stsp return err;
345 2b0ae357 2022-01-10 thomas const struct got_error *
346 e8f02263 2022-01-23 thomas got_deflate_to_file_mmap(off_t *outlen, uint8_t *map, size_t offset,
347 2b0ae357 2022-01-10 thomas size_t len, FILE *outfile, struct got_deflate_checksum *csum)
349 2b0ae357 2022-01-10 thomas const struct got_error *err;
350 2b0ae357 2022-01-10 thomas size_t avail, consumed;
351 2b0ae357 2022-01-10 thomas struct got_deflate_buf zb;
353 2b0ae357 2022-01-10 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
355 2b0ae357 2022-01-10 thomas goto done;
357 2b0ae357 2022-01-10 thomas *outlen = 0;
359 2b0ae357 2022-01-10 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
360 2b0ae357 2022-01-10 thomas &consumed);
362 2b0ae357 2022-01-10 thomas goto done;
363 2b0ae357 2022-01-10 thomas offset += consumed;
364 2b0ae357 2022-01-10 thomas len -= consumed;
365 2b0ae357 2022-01-10 thomas if (avail > 0) {
366 2b0ae357 2022-01-10 thomas size_t n;
367 2b0ae357 2022-01-10 thomas n = fwrite(zb.outbuf, avail, 1, outfile);
368 2b0ae357 2022-01-10 thomas if (n != 1) {
369 2b0ae357 2022-01-10 thomas err = got_ferror(outfile, GOT_ERR_IO);
370 2b0ae357 2022-01-10 thomas goto done;
372 2b0ae357 2022-01-10 thomas if (csum)
373 2b0ae357 2022-01-10 thomas csum_output(csum, zb.outbuf, avail);
374 2b0ae357 2022-01-10 thomas *outlen += avail;
376 2b0ae357 2022-01-10 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
379 9249e7e3 2022-05-12 thomas got_deflate_end(&zb);
380 9249e7e3 2022-05-12 thomas return err;
383 9249e7e3 2022-05-12 thomas const struct got_error *
384 9249e7e3 2022-05-12 thomas got_deflate_append_to_file_mmap(struct got_deflate_buf *zb, off_t *outlen,
385 9249e7e3 2022-05-12 thomas uint8_t *map, size_t offset, size_t len, FILE *outfile,
386 9249e7e3 2022-05-12 thomas struct got_deflate_checksum *csum)
388 9249e7e3 2022-05-12 thomas const struct got_error *err;
389 9249e7e3 2022-05-12 thomas size_t avail, consumed;
392 9249e7e3 2022-05-12 thomas err = deflate_read_mmap(zb, map, offset, len, &avail,
393 9249e7e3 2022-05-12 thomas &consumed, 0);
396 9249e7e3 2022-05-12 thomas offset += consumed;
397 9249e7e3 2022-05-12 thomas len -= consumed;
398 9249e7e3 2022-05-12 thomas if (avail > 0) {
399 9249e7e3 2022-05-12 thomas size_t n;
400 9249e7e3 2022-05-12 thomas n = fwrite(zb->outbuf, avail, 1, outfile);
401 9249e7e3 2022-05-12 thomas if (n != 1) {
402 9249e7e3 2022-05-12 thomas err = got_ferror(outfile, GOT_ERR_IO);
405 9249e7e3 2022-05-12 thomas if (csum)
406 9249e7e3 2022-05-12 thomas csum_output(csum, zb->outbuf, avail);
407 9249e7e3 2022-05-12 thomas if (outlen)
408 9249e7e3 2022-05-12 thomas *outlen += avail;
410 9249e7e3 2022-05-12 thomas } while ((zb->flags & GOT_DEFLATE_F_HAVE_MORE) && len > 0);
412 9249e7e3 2022-05-12 thomas return err;
415 9249e7e3 2022-05-12 thomas const struct got_error *
416 9249e7e3 2022-05-12 thomas got_deflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
417 9249e7e3 2022-05-12 thomas size_t *consumed_total, struct got_deflate_checksum *csum, uint8_t *map,
418 9249e7e3 2022-05-12 thomas size_t offset, size_t len)
420 9249e7e3 2022-05-12 thomas const struct got_error *err;
421 9249e7e3 2022-05-12 thomas size_t avail, consumed;
422 9249e7e3 2022-05-12 thomas struct got_deflate_buf zb;
423 9249e7e3 2022-05-12 thomas void *newbuf;
424 9249e7e3 2022-05-12 thomas size_t nbuf = 1;
426 9249e7e3 2022-05-12 thomas if (outbuf) {
427 9249e7e3 2022-05-12 thomas *outbuf = malloc(GOT_DEFLATE_BUFSIZE);
428 9249e7e3 2022-05-12 thomas if (*outbuf == NULL)
429 9249e7e3 2022-05-12 thomas return got_error_from_errno("malloc");
430 9249e7e3 2022-05-12 thomas err = got_deflate_init(&zb, *outbuf, GOT_DEFLATE_BUFSIZE);
431 9249e7e3 2022-05-12 thomas if (err) {
432 9249e7e3 2022-05-12 thomas free(*outbuf);
433 9249e7e3 2022-05-12 thomas *outbuf = NULL;
434 9249e7e3 2022-05-12 thomas return err;
437 9249e7e3 2022-05-12 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
439 9249e7e3 2022-05-12 thomas return err;
442 9249e7e3 2022-05-12 thomas *outlen = 0;
443 9249e7e3 2022-05-12 thomas if (consumed_total)
444 9249e7e3 2022-05-12 thomas *consumed_total = 0;
446 9249e7e3 2022-05-12 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
447 9249e7e3 2022-05-12 thomas &consumed);
449 9249e7e3 2022-05-12 thomas goto done;
450 9249e7e3 2022-05-12 thomas offset += consumed;
451 9249e7e3 2022-05-12 thomas if (consumed_total)
452 9249e7e3 2022-05-12 thomas *consumed_total += consumed;
453 9249e7e3 2022-05-12 thomas len -= consumed;
454 9249e7e3 2022-05-12 thomas if (avail > 0 && csum)
455 9249e7e3 2022-05-12 thomas csum_output(csum, zb.outbuf, avail);
456 9249e7e3 2022-05-12 thomas *outlen += avail;
457 9249e7e3 2022-05-12 thomas if ((zb.flags & GOT_DEFLATE_F_HAVE_MORE) && outbuf != NULL) {
458 9249e7e3 2022-05-12 thomas newbuf = reallocarray(*outbuf, ++nbuf,
459 9249e7e3 2022-05-12 thomas GOT_DEFLATE_BUFSIZE);
460 9249e7e3 2022-05-12 thomas if (newbuf == NULL) {
461 9249e7e3 2022-05-12 thomas err = got_error_from_errno("reallocarray");
462 9249e7e3 2022-05-12 thomas free(*outbuf);
463 9249e7e3 2022-05-12 thomas *outbuf = NULL;
464 9249e7e3 2022-05-12 thomas *outlen = 0;
465 9249e7e3 2022-05-12 thomas goto done;
467 9249e7e3 2022-05-12 thomas *outbuf = newbuf;
468 9249e7e3 2022-05-12 thomas zb.outbuf = newbuf + *outlen;
469 9249e7e3 2022-05-12 thomas zb.outlen = (nbuf * GOT_DEFLATE_BUFSIZE) - *outlen;
471 9249e7e3 2022-05-12 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
473 2b0ae357 2022-01-10 thomas got_deflate_end(&zb);
474 2b0ae357 2022-01-10 thomas return err;