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 2181e0c8 2019-03-19 stsp #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 <sha1.h>
24 e83f12a6 2023-02-12 op #include <sha2.h>
25 2181e0c8 2019-03-19 stsp #include <zlib.h>
26 2181e0c8 2019-03-19 stsp #include <time.h>
28 2181e0c8 2019-03-19 stsp #include "got_error.h"
29 2181e0c8 2019-03-19 stsp #include "got_object.h"
30 324d37e7 2019-05-11 stsp #include "got_path.h"
32 2181e0c8 2019-03-19 stsp #include "got_lib_deflate.h"
33 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
36 2181e0c8 2019-03-19 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
39 2181e0c8 2019-03-19 stsp const struct got_error *
40 3b9e6fcf 2021-06-05 stsp got_deflate_init(struct got_deflate_buf *zb, uint8_t *outbuf, size_t bufsize)
42 2181e0c8 2019-03-19 stsp const struct got_error *err = NULL;
45 0fc5e229 2022-11-08 stsp memset(zb, 0, sizeof(*zb));
47 2181e0c8 2019-03-19 stsp zb->z.zalloc = Z_NULL;
48 2181e0c8 2019-03-19 stsp zb->z.zfree = Z_NULL;
49 2181e0c8 2019-03-19 stsp zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
50 2181e0c8 2019-03-19 stsp if (zerr != Z_OK) {
51 2181e0c8 2019-03-19 stsp if (zerr == Z_ERRNO)
52 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
53 2181e0c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
54 2181e0c8 2019-03-19 stsp errno = ENOMEM;
55 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
57 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
60 2181e0c8 2019-03-19 stsp zb->inlen = zb->outlen = bufsize;
62 2181e0c8 2019-03-19 stsp zb->inbuf = calloc(1, zb->inlen);
63 2181e0c8 2019-03-19 stsp if (zb->inbuf == NULL) {
64 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
68 2181e0c8 2019-03-19 stsp zb->flags = 0;
69 2181e0c8 2019-03-19 stsp if (outbuf == NULL) {
70 2181e0c8 2019-03-19 stsp zb->outbuf = calloc(1, zb->outlen);
71 2181e0c8 2019-03-19 stsp if (zb->outbuf == NULL) {
72 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
75 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
77 2181e0c8 2019-03-19 stsp zb->outbuf = outbuf;
80 2181e0c8 2019-03-19 stsp got_deflate_end(zb);
85 31e61ec1 2021-09-28 naddy csum_output(struct got_deflate_checksum *csum, const uint8_t *buf, size_t len)
87 91b40e30 2021-05-21 stsp if (csum->output_crc)
88 91b40e30 2021-05-21 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
90 91b40e30 2021-05-21 stsp if (csum->output_sha1)
91 91b40e30 2021-05-21 stsp SHA1Update(csum->output_sha1, buf, len);
94 2181e0c8 2019-03-19 stsp const struct got_error *
95 72840534 2022-01-19 stsp got_deflate_read(struct got_deflate_buf *zb, FILE *f, off_t len,
96 72840534 2022-01-19 stsp size_t *outlenp, off_t *consumed)
98 2181e0c8 2019-03-19 stsp size_t last_total_out = zb->z.total_out;
99 2181e0c8 2019-03-19 stsp z_stream *z = &zb->z;
100 2181e0c8 2019-03-19 stsp int ret = Z_ERRNO;
102 2181e0c8 2019-03-19 stsp z->next_out = zb->outbuf;
103 2181e0c8 2019-03-19 stsp z->avail_out = zb->outlen;
105 2181e0c8 2019-03-19 stsp *outlenp = 0;
106 72840534 2022-01-19 stsp *consumed = 0;
108 72840534 2022-01-19 stsp size_t last_total_in = z->total_in;
109 2181e0c8 2019-03-19 stsp if (z->avail_in == 0) {
110 72840534 2022-01-19 stsp size_t n = 0;
111 72840534 2022-01-19 stsp if (*consumed < len) {
112 72840534 2022-01-19 stsp n = fread(zb->inbuf, 1,
113 72840534 2022-01-19 stsp MIN(zb->inlen, len - *consumed), f);
115 2181e0c8 2019-03-19 stsp if (n == 0) {
116 2181e0c8 2019-03-19 stsp if (ferror(f))
117 2181e0c8 2019-03-19 stsp return got_ferror(f, GOT_ERR_IO);
119 4e4a7005 2019-04-13 stsp ret = deflate(z, Z_FINISH);
122 2181e0c8 2019-03-19 stsp z->next_in = zb->inbuf;
123 2181e0c8 2019-03-19 stsp z->avail_in = n;
125 2181e0c8 2019-03-19 stsp ret = deflate(z, Z_NO_FLUSH);
126 72840534 2022-01-19 stsp *consumed += z->total_in - last_total_in;
127 2181e0c8 2019-03-19 stsp } while (ret == Z_OK && z->avail_out > 0);
129 2181e0c8 2019-03-19 stsp if (ret == Z_OK) {
130 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
132 2181e0c8 2019-03-19 stsp if (ret != Z_STREAM_END)
133 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
134 2181e0c8 2019-03-19 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
137 2181e0c8 2019-03-19 stsp *outlenp = z->total_out - last_total_out;
138 2181e0c8 2019-03-19 stsp return NULL;
141 2d9e6abf 2022-05-04 stsp static const struct got_error *
142 2d9e6abf 2022-05-04 stsp deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
143 2d9e6abf 2022-05-04 stsp size_t len, size_t *outlenp, size_t *consumed, int flush_on_eof)
145 64a8571e 2022-01-07 stsp z_stream *z = &zb->z;
146 64a8571e 2022-01-07 stsp size_t last_total_out = z->total_out;
147 64a8571e 2022-01-07 stsp int ret = Z_ERRNO;
149 64a8571e 2022-01-07 stsp z->next_out = zb->outbuf;
150 64a8571e 2022-01-07 stsp z->avail_out = zb->outlen;
152 64a8571e 2022-01-07 stsp *outlenp = 0;
153 64a8571e 2022-01-07 stsp *consumed = 0;
155 64a8571e 2022-01-07 stsp size_t last_total_in = z->total_in;
156 64a8571e 2022-01-07 stsp if (z->avail_in == 0) {
157 64a8571e 2022-01-07 stsp z->next_in = map + offset + *consumed;
158 a9bd296d 2022-02-08 stsp if (len - *consumed > UINT_MAX)
159 a9bd296d 2022-02-08 stsp z->avail_in = UINT_MAX;
161 a9bd296d 2022-02-08 stsp z->avail_in = len - *consumed;
162 64a8571e 2022-01-07 stsp if (z->avail_in == 0) {
164 2d9e6abf 2022-05-04 stsp if (flush_on_eof)
165 2d9e6abf 2022-05-04 stsp ret = deflate(z, Z_FINISH);
169 64a8571e 2022-01-07 stsp ret = deflate(z, Z_NO_FLUSH);
170 64a8571e 2022-01-07 stsp *consumed += z->total_in - last_total_in;
171 64a8571e 2022-01-07 stsp } while (ret == Z_OK && z->avail_out > 0);
173 64a8571e 2022-01-07 stsp if (ret == Z_OK) {
174 64a8571e 2022-01-07 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
176 64a8571e 2022-01-07 stsp if (ret != Z_STREAM_END)
177 64a8571e 2022-01-07 stsp return got_error(GOT_ERR_COMPRESSION);
178 64a8571e 2022-01-07 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
181 64a8571e 2022-01-07 stsp *outlenp = z->total_out - last_total_out;
182 2d9e6abf 2022-05-04 stsp return NULL;
185 2d9e6abf 2022-05-04 stsp const struct got_error *
186 2d9e6abf 2022-05-04 stsp got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
187 2d9e6abf 2022-05-04 stsp size_t len, size_t *outlenp, size_t *consumed)
189 2d9e6abf 2022-05-04 stsp return deflate_read_mmap(zb, map, offset, len, outlenp, consumed, 1);
192 2d9e6abf 2022-05-04 stsp const struct got_error *
193 2d9e6abf 2022-05-04 stsp got_deflate_flush(struct got_deflate_buf *zb, FILE *outfile,
194 2d9e6abf 2022-05-04 stsp struct got_deflate_checksum *csum, off_t *outlenp)
198 2d9e6abf 2022-05-04 stsp z_stream *z = &zb->z;
200 2d9e6abf 2022-05-04 stsp if (z->avail_in != 0)
201 2d9e6abf 2022-05-04 stsp return got_error_msg(GOT_ERR_COMPRESSION,
202 2d9e6abf 2022-05-04 stsp "cannot flush zb with pending input data");
205 2d9e6abf 2022-05-04 stsp size_t avail, last_total_out = zb->z.total_out;
207 2d9e6abf 2022-05-04 stsp z->next_out = zb->outbuf;
208 2d9e6abf 2022-05-04 stsp z->avail_out = zb->outlen;
210 2d9e6abf 2022-05-04 stsp ret = deflate(z, Z_FINISH);
211 2d9e6abf 2022-05-04 stsp if (ret != Z_STREAM_END && ret != Z_OK)
212 2d9e6abf 2022-05-04 stsp return got_error(GOT_ERR_COMPRESSION);
214 2d9e6abf 2022-05-04 stsp avail = z->total_out - last_total_out;
215 2d9e6abf 2022-05-04 stsp if (avail > 0) {
216 2d9e6abf 2022-05-04 stsp n = fwrite(zb->outbuf, avail, 1, outfile);
217 2d9e6abf 2022-05-04 stsp if (n != 1)
218 2d9e6abf 2022-05-04 stsp return got_ferror(outfile, GOT_ERR_IO);
220 2d9e6abf 2022-05-04 stsp csum_output(csum, zb->outbuf, avail);
221 2d9e6abf 2022-05-04 stsp if (outlenp)
222 2d9e6abf 2022-05-04 stsp *outlenp += avail;
224 2d9e6abf 2022-05-04 stsp } while (ret != Z_STREAM_END);
226 2d9e6abf 2022-05-04 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
227 64a8571e 2022-01-07 stsp return NULL;
231 2181e0c8 2019-03-19 stsp got_deflate_end(struct got_deflate_buf *zb)
233 2181e0c8 2019-03-19 stsp free(zb->inbuf);
234 2181e0c8 2019-03-19 stsp if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
235 2181e0c8 2019-03-19 stsp free(zb->outbuf);
236 2181e0c8 2019-03-19 stsp deflateEnd(&zb->z);
239 894e4711 2022-10-15 stsp const struct got_error *
240 894e4711 2022-10-15 stsp got_deflate_to_fd(off_t *outlen, FILE *infile, off_t len, int outfd,
241 894e4711 2022-10-15 stsp struct got_deflate_checksum *csum)
243 894e4711 2022-10-15 stsp const struct got_error *err;
244 894e4711 2022-10-15 stsp size_t avail;
245 894e4711 2022-10-15 stsp off_t consumed;
246 894e4711 2022-10-15 stsp struct got_deflate_buf zb;
248 894e4711 2022-10-15 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
252 894e4711 2022-10-15 stsp *outlen = 0;
255 894e4711 2022-10-15 stsp err = got_deflate_read(&zb, infile, len, &avail, &consumed);
258 894e4711 2022-10-15 stsp len -= consumed;
259 894e4711 2022-10-15 stsp if (avail > 0) {
260 13b2bc37 2022-10-23 stsp err = got_poll_write_full(outfd, zb.outbuf, avail);
264 894e4711 2022-10-15 stsp csum_output(csum, zb.outbuf, avail);
265 894e4711 2022-10-15 stsp *outlen += avail;
267 894e4711 2022-10-15 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
270 894e4711 2022-10-15 stsp got_deflate_end(&zb);
271 894e4711 2022-10-15 stsp return err;
274 894e4711 2022-10-15 stsp const struct got_error *
275 894e4711 2022-10-15 stsp got_deflate_to_fd_mmap(off_t *outlen, uint8_t *map, size_t offset,
276 894e4711 2022-10-15 stsp size_t len, int outfd, struct got_deflate_checksum *csum)
278 894e4711 2022-10-15 stsp const struct got_error *err;
279 894e4711 2022-10-15 stsp size_t avail, consumed;
280 894e4711 2022-10-15 stsp struct got_deflate_buf zb;
282 894e4711 2022-10-15 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
286 894e4711 2022-10-15 stsp *outlen = 0;
288 894e4711 2022-10-15 stsp err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
289 894e4711 2022-10-15 stsp &consumed);
292 894e4711 2022-10-15 stsp offset += consumed;
293 894e4711 2022-10-15 stsp len -= consumed;
294 894e4711 2022-10-15 stsp if (avail > 0) {
295 13b2bc37 2022-10-23 stsp err = got_poll_write_full(outfd, zb.outbuf, avail);
299 894e4711 2022-10-15 stsp csum_output(csum, zb.outbuf, avail);
300 894e4711 2022-10-15 stsp *outlen += avail;
302 894e4711 2022-10-15 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
305 894e4711 2022-10-15 stsp got_deflate_end(&zb);
306 894e4711 2022-10-15 stsp return err;
309 2181e0c8 2019-03-19 stsp const struct got_error *
310 72840534 2022-01-19 stsp got_deflate_to_file(off_t *outlen, FILE *infile, off_t len,
311 72840534 2022-01-19 stsp FILE *outfile, struct got_deflate_checksum *csum)
313 2181e0c8 2019-03-19 stsp const struct got_error *err;
314 2181e0c8 2019-03-19 stsp size_t avail;
315 72840534 2022-01-19 stsp off_t consumed;
316 2181e0c8 2019-03-19 stsp struct got_deflate_buf zb;
318 3b9e6fcf 2021-06-05 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
322 2181e0c8 2019-03-19 stsp *outlen = 0;
325 72840534 2022-01-19 stsp err = got_deflate_read(&zb, infile, len, &avail, &consumed);
328 72840534 2022-01-19 stsp len -= consumed;
329 2181e0c8 2019-03-19 stsp if (avail > 0) {
331 2181e0c8 2019-03-19 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
332 2181e0c8 2019-03-19 stsp if (n != 1) {
333 2181e0c8 2019-03-19 stsp err = got_ferror(outfile, GOT_ERR_IO);
337 3b9e6fcf 2021-06-05 stsp csum_output(csum, zb.outbuf, avail);
338 2181e0c8 2019-03-19 stsp *outlen += avail;
340 2181e0c8 2019-03-19 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
343 2181e0c8 2019-03-19 stsp got_deflate_end(&zb);
344 2181e0c8 2019-03-19 stsp return err;
347 64a8571e 2022-01-07 stsp const struct got_error *
348 72840534 2022-01-19 stsp got_deflate_to_file_mmap(off_t *outlen, uint8_t *map, size_t offset,
349 64a8571e 2022-01-07 stsp size_t len, FILE *outfile, struct got_deflate_checksum *csum)
351 64a8571e 2022-01-07 stsp const struct got_error *err;
352 64a8571e 2022-01-07 stsp size_t avail, consumed;
353 64a8571e 2022-01-07 stsp struct got_deflate_buf zb;
355 64a8571e 2022-01-07 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
359 64a8571e 2022-01-07 stsp *outlen = 0;
361 64a8571e 2022-01-07 stsp err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
362 64a8571e 2022-01-07 stsp &consumed);
365 64a8571e 2022-01-07 stsp offset += consumed;
366 64a8571e 2022-01-07 stsp len -= consumed;
367 64a8571e 2022-01-07 stsp if (avail > 0) {
369 64a8571e 2022-01-07 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
370 64a8571e 2022-01-07 stsp if (n != 1) {
371 64a8571e 2022-01-07 stsp err = got_ferror(outfile, GOT_ERR_IO);
375 64a8571e 2022-01-07 stsp csum_output(csum, zb.outbuf, avail);
376 64a8571e 2022-01-07 stsp *outlen += avail;
378 64a8571e 2022-01-07 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
381 2d9e6abf 2022-05-04 stsp got_deflate_end(&zb);
382 2d9e6abf 2022-05-04 stsp return err;
385 2d9e6abf 2022-05-04 stsp const struct got_error *
386 2d9e6abf 2022-05-04 stsp got_deflate_append_to_file_mmap(struct got_deflate_buf *zb, off_t *outlen,
387 2d9e6abf 2022-05-04 stsp uint8_t *map, size_t offset, size_t len, FILE *outfile,
388 2d9e6abf 2022-05-04 stsp struct got_deflate_checksum *csum)
390 2d9e6abf 2022-05-04 stsp const struct got_error *err;
391 2d9e6abf 2022-05-04 stsp size_t avail, consumed;
394 2d9e6abf 2022-05-04 stsp err = deflate_read_mmap(zb, map, offset, len, &avail,
395 2d9e6abf 2022-05-04 stsp &consumed, 0);
398 2d9e6abf 2022-05-04 stsp offset += consumed;
399 2d9e6abf 2022-05-04 stsp len -= consumed;
400 2d9e6abf 2022-05-04 stsp if (avail > 0) {
402 2d9e6abf 2022-05-04 stsp n = fwrite(zb->outbuf, avail, 1, outfile);
403 2d9e6abf 2022-05-04 stsp if (n != 1) {
404 2d9e6abf 2022-05-04 stsp err = got_ferror(outfile, GOT_ERR_IO);
408 2d9e6abf 2022-05-04 stsp csum_output(csum, zb->outbuf, avail);
409 2d9e6abf 2022-05-04 stsp if (outlen)
410 2d9e6abf 2022-05-04 stsp *outlen += avail;
412 2d9e6abf 2022-05-04 stsp } while ((zb->flags & GOT_DEFLATE_F_HAVE_MORE) && len > 0);
414 2d9e6abf 2022-05-04 stsp return err;
417 2d9e6abf 2022-05-04 stsp const struct got_error *
418 2d9e6abf 2022-05-04 stsp got_deflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
419 2d9e6abf 2022-05-04 stsp size_t *consumed_total, struct got_deflate_checksum *csum, uint8_t *map,
420 2d9e6abf 2022-05-04 stsp size_t offset, size_t len)
422 2d9e6abf 2022-05-04 stsp const struct got_error *err;
423 2d9e6abf 2022-05-04 stsp size_t avail, consumed;
424 2d9e6abf 2022-05-04 stsp struct got_deflate_buf zb;
425 2d9e6abf 2022-05-04 stsp void *newbuf;
426 2d9e6abf 2022-05-04 stsp size_t nbuf = 1;
428 2d9e6abf 2022-05-04 stsp if (outbuf) {
429 2d9e6abf 2022-05-04 stsp *outbuf = malloc(GOT_DEFLATE_BUFSIZE);
430 2d9e6abf 2022-05-04 stsp if (*outbuf == NULL)
431 2d9e6abf 2022-05-04 stsp return got_error_from_errno("malloc");
432 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, *outbuf, GOT_DEFLATE_BUFSIZE);
434 2d9e6abf 2022-05-04 stsp free(*outbuf);
435 2d9e6abf 2022-05-04 stsp *outbuf = NULL;
436 2d9e6abf 2022-05-04 stsp return err;
439 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
441 2d9e6abf 2022-05-04 stsp return err;
444 2d9e6abf 2022-05-04 stsp *outlen = 0;
445 2d9e6abf 2022-05-04 stsp if (consumed_total)
446 2d9e6abf 2022-05-04 stsp *consumed_total = 0;
448 2d9e6abf 2022-05-04 stsp err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
449 2d9e6abf 2022-05-04 stsp &consumed);
452 2d9e6abf 2022-05-04 stsp offset += consumed;
453 2d9e6abf 2022-05-04 stsp if (consumed_total)
454 2d9e6abf 2022-05-04 stsp *consumed_total += consumed;
455 2d9e6abf 2022-05-04 stsp len -= consumed;
456 2d9e6abf 2022-05-04 stsp if (avail > 0 && csum)
457 2d9e6abf 2022-05-04 stsp csum_output(csum, zb.outbuf, avail);
458 2d9e6abf 2022-05-04 stsp *outlen += avail;
459 2d9e6abf 2022-05-04 stsp if ((zb.flags & GOT_DEFLATE_F_HAVE_MORE) && outbuf != NULL) {
460 2d9e6abf 2022-05-04 stsp newbuf = reallocarray(*outbuf, ++nbuf,
461 2d9e6abf 2022-05-04 stsp GOT_DEFLATE_BUFSIZE);
462 2d9e6abf 2022-05-04 stsp if (newbuf == NULL) {
463 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("reallocarray");
464 2d9e6abf 2022-05-04 stsp free(*outbuf);
465 2d9e6abf 2022-05-04 stsp *outbuf = NULL;
466 2d9e6abf 2022-05-04 stsp *outlen = 0;
469 2d9e6abf 2022-05-04 stsp *outbuf = newbuf;
470 2d9e6abf 2022-05-04 stsp zb.outbuf = newbuf + *outlen;
471 2d9e6abf 2022-05-04 stsp zb.outlen = (nbuf * GOT_DEFLATE_BUFSIZE) - *outlen;
473 2d9e6abf 2022-05-04 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
475 64a8571e 2022-01-07 stsp got_deflate_end(&zb);
476 64a8571e 2022-01-07 stsp return err;