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 b16893ba 2023-02-24 thomas #include "got_lib_hash.h"
32 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
35 2181e0c8 2019-03-19 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 2181e0c8 2019-03-19 stsp const struct got_error *
39 3b9e6fcf 2021-06-05 stsp got_deflate_init(struct got_deflate_buf *zb, uint8_t *outbuf, size_t bufsize)
41 2181e0c8 2019-03-19 stsp const struct got_error *err = NULL;
44 0699dbc2 2022-11-08 thomas memset(zb, 0, sizeof(*zb));
46 2181e0c8 2019-03-19 stsp zb->z.zalloc = Z_NULL;
47 2181e0c8 2019-03-19 stsp zb->z.zfree = Z_NULL;
48 2181e0c8 2019-03-19 stsp zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
49 2181e0c8 2019-03-19 stsp if (zerr != Z_OK) {
50 2181e0c8 2019-03-19 stsp if (zerr == Z_ERRNO)
51 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
52 2181e0c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
53 2181e0c8 2019-03-19 stsp errno = ENOMEM;
54 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
56 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
59 2181e0c8 2019-03-19 stsp zb->inlen = zb->outlen = bufsize;
61 2181e0c8 2019-03-19 stsp zb->inbuf = calloc(1, zb->inlen);
62 2181e0c8 2019-03-19 stsp if (zb->inbuf == NULL) {
63 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
67 2181e0c8 2019-03-19 stsp zb->flags = 0;
68 2181e0c8 2019-03-19 stsp if (outbuf == NULL) {
69 2181e0c8 2019-03-19 stsp zb->outbuf = calloc(1, zb->outlen);
70 2181e0c8 2019-03-19 stsp if (zb->outbuf == NULL) {
71 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
74 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
76 2181e0c8 2019-03-19 stsp zb->outbuf = outbuf;
79 2181e0c8 2019-03-19 stsp got_deflate_end(zb);
84 dbaa2362 2021-09-28 thomas csum_output(struct got_deflate_checksum *csum, const uint8_t *buf, size_t len)
86 91b40e30 2021-05-21 stsp if (csum->output_crc)
87 91b40e30 2021-05-21 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
89 b16893ba 2023-02-24 thomas if (csum->output_ctx)
90 b16893ba 2023-02-24 thomas got_hash_update(csum->output_ctx, buf, len);
93 2181e0c8 2019-03-19 stsp const struct got_error *
94 e8f02263 2022-01-23 thomas got_deflate_read(struct got_deflate_buf *zb, FILE *f, off_t len,
95 e8f02263 2022-01-23 thomas size_t *outlenp, off_t *consumed)
97 2181e0c8 2019-03-19 stsp size_t last_total_out = zb->z.total_out;
98 2181e0c8 2019-03-19 stsp z_stream *z = &zb->z;
99 2181e0c8 2019-03-19 stsp int ret = Z_ERRNO;
101 2181e0c8 2019-03-19 stsp z->next_out = zb->outbuf;
102 2181e0c8 2019-03-19 stsp z->avail_out = zb->outlen;
104 2181e0c8 2019-03-19 stsp *outlenp = 0;
105 e8f02263 2022-01-23 thomas *consumed = 0;
107 e8f02263 2022-01-23 thomas size_t last_total_in = z->total_in;
108 2181e0c8 2019-03-19 stsp if (z->avail_in == 0) {
109 e8f02263 2022-01-23 thomas size_t n = 0;
110 e8f02263 2022-01-23 thomas if (*consumed < len) {
111 e8f02263 2022-01-23 thomas n = fread(zb->inbuf, 1,
112 e8f02263 2022-01-23 thomas MIN(zb->inlen, len - *consumed), f);
114 2181e0c8 2019-03-19 stsp if (n == 0) {
115 2181e0c8 2019-03-19 stsp if (ferror(f))
116 2181e0c8 2019-03-19 stsp return got_ferror(f, GOT_ERR_IO);
118 4e4a7005 2019-04-13 stsp ret = deflate(z, Z_FINISH);
121 2181e0c8 2019-03-19 stsp z->next_in = zb->inbuf;
122 2181e0c8 2019-03-19 stsp z->avail_in = n;
124 2181e0c8 2019-03-19 stsp ret = deflate(z, Z_NO_FLUSH);
125 e8f02263 2022-01-23 thomas *consumed += z->total_in - last_total_in;
126 2181e0c8 2019-03-19 stsp } while (ret == Z_OK && z->avail_out > 0);
128 2181e0c8 2019-03-19 stsp if (ret == Z_OK) {
129 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
131 2181e0c8 2019-03-19 stsp if (ret != Z_STREAM_END)
132 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
133 2181e0c8 2019-03-19 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
136 2181e0c8 2019-03-19 stsp *outlenp = z->total_out - last_total_out;
137 2181e0c8 2019-03-19 stsp return NULL;
140 9249e7e3 2022-05-12 thomas static const struct got_error *
141 9249e7e3 2022-05-12 thomas deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
142 9249e7e3 2022-05-12 thomas size_t len, size_t *outlenp, size_t *consumed, int flush_on_eof)
144 2b0ae357 2022-01-10 thomas z_stream *z = &zb->z;
145 2b0ae357 2022-01-10 thomas size_t last_total_out = z->total_out;
146 2b0ae357 2022-01-10 thomas int ret = Z_ERRNO;
148 2b0ae357 2022-01-10 thomas z->next_out = zb->outbuf;
149 2b0ae357 2022-01-10 thomas z->avail_out = zb->outlen;
151 2b0ae357 2022-01-10 thomas *outlenp = 0;
152 2b0ae357 2022-01-10 thomas *consumed = 0;
154 2b0ae357 2022-01-10 thomas size_t last_total_in = z->total_in;
155 2b0ae357 2022-01-10 thomas if (z->avail_in == 0) {
156 2b0ae357 2022-01-10 thomas z->next_in = map + offset + *consumed;
157 f6a55b40 2022-02-12 thomas if (len - *consumed > UINT_MAX)
158 f6a55b40 2022-02-12 thomas z->avail_in = UINT_MAX;
160 f6a55b40 2022-02-12 thomas z->avail_in = len - *consumed;
161 2b0ae357 2022-01-10 thomas if (z->avail_in == 0) {
162 2b0ae357 2022-01-10 thomas /* EOF */
163 9249e7e3 2022-05-12 thomas if (flush_on_eof)
164 9249e7e3 2022-05-12 thomas ret = deflate(z, Z_FINISH);
168 2b0ae357 2022-01-10 thomas ret = deflate(z, Z_NO_FLUSH);
169 2b0ae357 2022-01-10 thomas *consumed += z->total_in - last_total_in;
170 2b0ae357 2022-01-10 thomas } while (ret == Z_OK && z->avail_out > 0);
172 2b0ae357 2022-01-10 thomas if (ret == Z_OK) {
173 2b0ae357 2022-01-10 thomas zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
175 2b0ae357 2022-01-10 thomas if (ret != Z_STREAM_END)
176 2b0ae357 2022-01-10 thomas return got_error(GOT_ERR_COMPRESSION);
177 2b0ae357 2022-01-10 thomas zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
180 2b0ae357 2022-01-10 thomas *outlenp = z->total_out - last_total_out;
181 9249e7e3 2022-05-12 thomas return NULL;
184 9249e7e3 2022-05-12 thomas const struct got_error *
185 9249e7e3 2022-05-12 thomas got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
186 9249e7e3 2022-05-12 thomas size_t len, size_t *outlenp, size_t *consumed)
188 9249e7e3 2022-05-12 thomas return deflate_read_mmap(zb, map, offset, len, outlenp, consumed, 1);
191 9249e7e3 2022-05-12 thomas const struct got_error *
192 9249e7e3 2022-05-12 thomas got_deflate_flush(struct got_deflate_buf *zb, FILE *outfile,
193 9249e7e3 2022-05-12 thomas struct got_deflate_checksum *csum, off_t *outlenp)
196 9249e7e3 2022-05-12 thomas size_t n;
197 9249e7e3 2022-05-12 thomas z_stream *z = &zb->z;
199 9249e7e3 2022-05-12 thomas if (z->avail_in != 0)
200 9249e7e3 2022-05-12 thomas return got_error_msg(GOT_ERR_COMPRESSION,
201 9249e7e3 2022-05-12 thomas "cannot flush zb with pending input data");
204 9249e7e3 2022-05-12 thomas size_t avail, last_total_out = zb->z.total_out;
206 9249e7e3 2022-05-12 thomas z->next_out = zb->outbuf;
207 9249e7e3 2022-05-12 thomas z->avail_out = zb->outlen;
209 9249e7e3 2022-05-12 thomas ret = deflate(z, Z_FINISH);
210 9249e7e3 2022-05-12 thomas if (ret != Z_STREAM_END && ret != Z_OK)
211 9249e7e3 2022-05-12 thomas return got_error(GOT_ERR_COMPRESSION);
213 9249e7e3 2022-05-12 thomas avail = z->total_out - last_total_out;
214 9249e7e3 2022-05-12 thomas if (avail > 0) {
215 9249e7e3 2022-05-12 thomas n = fwrite(zb->outbuf, avail, 1, outfile);
216 9249e7e3 2022-05-12 thomas if (n != 1)
217 9249e7e3 2022-05-12 thomas return got_ferror(outfile, GOT_ERR_IO);
218 9249e7e3 2022-05-12 thomas if (csum)
219 9249e7e3 2022-05-12 thomas csum_output(csum, zb->outbuf, avail);
220 9249e7e3 2022-05-12 thomas if (outlenp)
221 9249e7e3 2022-05-12 thomas *outlenp += avail;
223 9249e7e3 2022-05-12 thomas } while (ret != Z_STREAM_END);
225 9249e7e3 2022-05-12 thomas zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
226 2b0ae357 2022-01-10 thomas return NULL;
230 2181e0c8 2019-03-19 stsp got_deflate_end(struct got_deflate_buf *zb)
232 2181e0c8 2019-03-19 stsp free(zb->inbuf);
233 2181e0c8 2019-03-19 stsp if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
234 2181e0c8 2019-03-19 stsp free(zb->outbuf);
235 2181e0c8 2019-03-19 stsp deflateEnd(&zb->z);
238 20a7d452 2022-10-16 thomas const struct got_error *
239 20a7d452 2022-10-16 thomas got_deflate_to_fd(off_t *outlen, FILE *infile, off_t len, int outfd,
240 20a7d452 2022-10-16 thomas struct got_deflate_checksum *csum)
242 20a7d452 2022-10-16 thomas const struct got_error *err;
243 20a7d452 2022-10-16 thomas size_t avail;
244 20a7d452 2022-10-16 thomas off_t consumed;
245 20a7d452 2022-10-16 thomas struct got_deflate_buf zb;
247 20a7d452 2022-10-16 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
249 20a7d452 2022-10-16 thomas goto done;
251 20a7d452 2022-10-16 thomas *outlen = 0;
254 20a7d452 2022-10-16 thomas err = got_deflate_read(&zb, infile, len, &avail, &consumed);
256 20a7d452 2022-10-16 thomas goto done;
257 20a7d452 2022-10-16 thomas len -= consumed;
258 20a7d452 2022-10-16 thomas if (avail > 0) {
259 3efd8e31 2022-10-23 thomas err = got_poll_write_full(outfd, zb.outbuf, avail);
261 20a7d452 2022-10-16 thomas goto done;
262 20a7d452 2022-10-16 thomas if (csum)
263 20a7d452 2022-10-16 thomas csum_output(csum, zb.outbuf, avail);
264 20a7d452 2022-10-16 thomas *outlen += avail;
266 20a7d452 2022-10-16 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
269 20a7d452 2022-10-16 thomas got_deflate_end(&zb);
270 20a7d452 2022-10-16 thomas return err;
273 20a7d452 2022-10-16 thomas const struct got_error *
274 20a7d452 2022-10-16 thomas got_deflate_to_fd_mmap(off_t *outlen, uint8_t *map, size_t offset,
275 20a7d452 2022-10-16 thomas size_t len, int outfd, struct got_deflate_checksum *csum)
277 20a7d452 2022-10-16 thomas const struct got_error *err;
278 20a7d452 2022-10-16 thomas size_t avail, consumed;
279 20a7d452 2022-10-16 thomas struct got_deflate_buf zb;
281 20a7d452 2022-10-16 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
283 20a7d452 2022-10-16 thomas goto done;
285 20a7d452 2022-10-16 thomas *outlen = 0;
287 20a7d452 2022-10-16 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
288 20a7d452 2022-10-16 thomas &consumed);
290 20a7d452 2022-10-16 thomas goto done;
291 20a7d452 2022-10-16 thomas offset += consumed;
292 20a7d452 2022-10-16 thomas len -= consumed;
293 20a7d452 2022-10-16 thomas if (avail > 0) {
294 3efd8e31 2022-10-23 thomas err = got_poll_write_full(outfd, zb.outbuf, avail);
296 20a7d452 2022-10-16 thomas goto done;
297 20a7d452 2022-10-16 thomas if (csum)
298 20a7d452 2022-10-16 thomas csum_output(csum, zb.outbuf, avail);
299 20a7d452 2022-10-16 thomas *outlen += avail;
301 20a7d452 2022-10-16 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
304 20a7d452 2022-10-16 thomas got_deflate_end(&zb);
305 20a7d452 2022-10-16 thomas return err;
308 2181e0c8 2019-03-19 stsp const struct got_error *
309 e8f02263 2022-01-23 thomas got_deflate_to_file(off_t *outlen, FILE *infile, off_t len,
310 e8f02263 2022-01-23 thomas FILE *outfile, struct got_deflate_checksum *csum)
312 2181e0c8 2019-03-19 stsp const struct got_error *err;
313 2181e0c8 2019-03-19 stsp size_t avail;
314 e8f02263 2022-01-23 thomas off_t consumed;
315 2181e0c8 2019-03-19 stsp struct got_deflate_buf zb;
317 3b9e6fcf 2021-06-05 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
321 2181e0c8 2019-03-19 stsp *outlen = 0;
324 e8f02263 2022-01-23 thomas err = got_deflate_read(&zb, infile, len, &avail, &consumed);
327 e8f02263 2022-01-23 thomas len -= consumed;
328 2181e0c8 2019-03-19 stsp if (avail > 0) {
330 2181e0c8 2019-03-19 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
331 2181e0c8 2019-03-19 stsp if (n != 1) {
332 2181e0c8 2019-03-19 stsp err = got_ferror(outfile, GOT_ERR_IO);
336 3b9e6fcf 2021-06-05 stsp csum_output(csum, zb.outbuf, avail);
337 2181e0c8 2019-03-19 stsp *outlen += avail;
339 2181e0c8 2019-03-19 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
342 2181e0c8 2019-03-19 stsp got_deflate_end(&zb);
343 2181e0c8 2019-03-19 stsp return err;
346 2b0ae357 2022-01-10 thomas const struct got_error *
347 e8f02263 2022-01-23 thomas got_deflate_to_file_mmap(off_t *outlen, uint8_t *map, size_t offset,
348 2b0ae357 2022-01-10 thomas size_t len, FILE *outfile, struct got_deflate_checksum *csum)
350 2b0ae357 2022-01-10 thomas const struct got_error *err;
351 2b0ae357 2022-01-10 thomas size_t avail, consumed;
352 2b0ae357 2022-01-10 thomas struct got_deflate_buf zb;
354 2b0ae357 2022-01-10 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
356 2b0ae357 2022-01-10 thomas goto done;
358 2b0ae357 2022-01-10 thomas *outlen = 0;
360 2b0ae357 2022-01-10 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
361 2b0ae357 2022-01-10 thomas &consumed);
363 2b0ae357 2022-01-10 thomas goto done;
364 2b0ae357 2022-01-10 thomas offset += consumed;
365 2b0ae357 2022-01-10 thomas len -= consumed;
366 2b0ae357 2022-01-10 thomas if (avail > 0) {
367 2b0ae357 2022-01-10 thomas size_t n;
368 2b0ae357 2022-01-10 thomas n = fwrite(zb.outbuf, avail, 1, outfile);
369 2b0ae357 2022-01-10 thomas if (n != 1) {
370 2b0ae357 2022-01-10 thomas err = got_ferror(outfile, GOT_ERR_IO);
371 2b0ae357 2022-01-10 thomas goto done;
373 2b0ae357 2022-01-10 thomas if (csum)
374 2b0ae357 2022-01-10 thomas csum_output(csum, zb.outbuf, avail);
375 2b0ae357 2022-01-10 thomas *outlen += avail;
377 2b0ae357 2022-01-10 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
380 9249e7e3 2022-05-12 thomas got_deflate_end(&zb);
381 9249e7e3 2022-05-12 thomas return err;
384 9249e7e3 2022-05-12 thomas const struct got_error *
385 9249e7e3 2022-05-12 thomas got_deflate_append_to_file_mmap(struct got_deflate_buf *zb, off_t *outlen,
386 9249e7e3 2022-05-12 thomas uint8_t *map, size_t offset, size_t len, FILE *outfile,
387 9249e7e3 2022-05-12 thomas struct got_deflate_checksum *csum)
389 9249e7e3 2022-05-12 thomas const struct got_error *err;
390 9249e7e3 2022-05-12 thomas size_t avail, consumed;
393 9249e7e3 2022-05-12 thomas err = deflate_read_mmap(zb, map, offset, len, &avail,
394 9249e7e3 2022-05-12 thomas &consumed, 0);
397 9249e7e3 2022-05-12 thomas offset += consumed;
398 9249e7e3 2022-05-12 thomas len -= consumed;
399 9249e7e3 2022-05-12 thomas if (avail > 0) {
400 9249e7e3 2022-05-12 thomas size_t n;
401 9249e7e3 2022-05-12 thomas n = fwrite(zb->outbuf, avail, 1, outfile);
402 9249e7e3 2022-05-12 thomas if (n != 1) {
403 9249e7e3 2022-05-12 thomas err = got_ferror(outfile, GOT_ERR_IO);
406 9249e7e3 2022-05-12 thomas if (csum)
407 9249e7e3 2022-05-12 thomas csum_output(csum, zb->outbuf, avail);
408 9249e7e3 2022-05-12 thomas if (outlen)
409 9249e7e3 2022-05-12 thomas *outlen += avail;
411 9249e7e3 2022-05-12 thomas } while ((zb->flags & GOT_DEFLATE_F_HAVE_MORE) && len > 0);
413 9249e7e3 2022-05-12 thomas return err;
416 9249e7e3 2022-05-12 thomas const struct got_error *
417 9249e7e3 2022-05-12 thomas got_deflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
418 9249e7e3 2022-05-12 thomas size_t *consumed_total, struct got_deflate_checksum *csum, uint8_t *map,
419 9249e7e3 2022-05-12 thomas size_t offset, size_t len)
421 9249e7e3 2022-05-12 thomas const struct got_error *err;
422 9249e7e3 2022-05-12 thomas size_t avail, consumed;
423 9249e7e3 2022-05-12 thomas struct got_deflate_buf zb;
424 9249e7e3 2022-05-12 thomas void *newbuf;
425 9249e7e3 2022-05-12 thomas size_t nbuf = 1;
427 9249e7e3 2022-05-12 thomas if (outbuf) {
428 9249e7e3 2022-05-12 thomas *outbuf = malloc(GOT_DEFLATE_BUFSIZE);
429 9249e7e3 2022-05-12 thomas if (*outbuf == NULL)
430 9249e7e3 2022-05-12 thomas return got_error_from_errno("malloc");
431 9249e7e3 2022-05-12 thomas err = got_deflate_init(&zb, *outbuf, GOT_DEFLATE_BUFSIZE);
432 9249e7e3 2022-05-12 thomas if (err) {
433 9249e7e3 2022-05-12 thomas free(*outbuf);
434 9249e7e3 2022-05-12 thomas *outbuf = NULL;
435 9249e7e3 2022-05-12 thomas return err;
438 9249e7e3 2022-05-12 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
440 9249e7e3 2022-05-12 thomas return err;
443 9249e7e3 2022-05-12 thomas *outlen = 0;
444 9249e7e3 2022-05-12 thomas if (consumed_total)
445 9249e7e3 2022-05-12 thomas *consumed_total = 0;
447 9249e7e3 2022-05-12 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
448 9249e7e3 2022-05-12 thomas &consumed);
450 9249e7e3 2022-05-12 thomas goto done;
451 9249e7e3 2022-05-12 thomas offset += consumed;
452 9249e7e3 2022-05-12 thomas if (consumed_total)
453 9249e7e3 2022-05-12 thomas *consumed_total += consumed;
454 9249e7e3 2022-05-12 thomas len -= consumed;
455 9249e7e3 2022-05-12 thomas if (avail > 0 && csum)
456 9249e7e3 2022-05-12 thomas csum_output(csum, zb.outbuf, avail);
457 9249e7e3 2022-05-12 thomas *outlen += avail;
458 9249e7e3 2022-05-12 thomas if ((zb.flags & GOT_DEFLATE_F_HAVE_MORE) && outbuf != NULL) {
459 9249e7e3 2022-05-12 thomas newbuf = reallocarray(*outbuf, ++nbuf,
460 9249e7e3 2022-05-12 thomas GOT_DEFLATE_BUFSIZE);
461 9249e7e3 2022-05-12 thomas if (newbuf == NULL) {
462 9249e7e3 2022-05-12 thomas err = got_error_from_errno("reallocarray");
463 9249e7e3 2022-05-12 thomas free(*outbuf);
464 9249e7e3 2022-05-12 thomas *outbuf = NULL;
465 9249e7e3 2022-05-12 thomas *outlen = 0;
466 9249e7e3 2022-05-12 thomas goto done;
468 9249e7e3 2022-05-12 thomas *outbuf = newbuf;
469 9249e7e3 2022-05-12 thomas zb.outbuf = newbuf + *outlen;
470 9249e7e3 2022-05-12 thomas zb.outlen = (nbuf * GOT_DEFLATE_BUFSIZE) - *outlen;
472 9249e7e3 2022-05-12 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
474 2b0ae357 2022-01-10 thomas got_deflate_end(&zb);
475 2b0ae357 2022-01-10 thomas return err;