Blame


1 2181e0c8 2019-03-19 stsp /*
2 2181e0c8 2019-03-19 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 2181e0c8 2019-03-19 stsp *
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.
7 2181e0c8 2019-03-19 stsp *
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.
15 2181e0c8 2019-03-19 stsp */
16 2181e0c8 2019-03-19 stsp
17 2181e0c8 2019-03-19 stsp
18 2181e0c8 2019-03-19 stsp #include <errno.h>
19 2181e0c8 2019-03-19 stsp #include <stdio.h>
20 2181e0c8 2019-03-19 stsp #include <stdlib.h>
21 2181e0c8 2019-03-19 stsp #include <string.h>
22 2181e0c8 2019-03-19 stsp #include <zlib.h>
23 2181e0c8 2019-03-19 stsp #include <time.h>
24 2181e0c8 2019-03-19 stsp
25 2181e0c8 2019-03-19 stsp #include "got_error.h"
26 2181e0c8 2019-03-19 stsp #include "got_object.h"
27 324d37e7 2019-05-11 stsp #include "got_path.h"
28 2181e0c8 2019-03-19 stsp
29 2181e0c8 2019-03-19 stsp #include "got_lib_deflate.h"
30 2181e0c8 2019-03-19 stsp
31 2181e0c8 2019-03-19 stsp #ifndef MIN
32 2181e0c8 2019-03-19 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
33 2181e0c8 2019-03-19 stsp #endif
34 2181e0c8 2019-03-19 stsp
35 2181e0c8 2019-03-19 stsp const struct got_error *
36 3b9e6fcf 2021-06-05 stsp got_deflate_init(struct got_deflate_buf *zb, uint8_t *outbuf, size_t bufsize)
37 2181e0c8 2019-03-19 stsp {
38 2181e0c8 2019-03-19 stsp const struct got_error *err = NULL;
39 2181e0c8 2019-03-19 stsp int zerr;
40 2181e0c8 2019-03-19 stsp
41 2181e0c8 2019-03-19 stsp memset(&zb->z, 0, sizeof(zb->z));
42 2181e0c8 2019-03-19 stsp
43 2181e0c8 2019-03-19 stsp zb->z.zalloc = Z_NULL;
44 2181e0c8 2019-03-19 stsp zb->z.zfree = Z_NULL;
45 2181e0c8 2019-03-19 stsp zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
46 2181e0c8 2019-03-19 stsp if (zerr != Z_OK) {
47 2181e0c8 2019-03-19 stsp if (zerr == Z_ERRNO)
48 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
49 2181e0c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
50 2181e0c8 2019-03-19 stsp errno = ENOMEM;
51 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
52 2181e0c8 2019-03-19 stsp }
53 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
54 2181e0c8 2019-03-19 stsp }
55 2181e0c8 2019-03-19 stsp
56 2181e0c8 2019-03-19 stsp zb->inlen = zb->outlen = bufsize;
57 2181e0c8 2019-03-19 stsp
58 2181e0c8 2019-03-19 stsp zb->inbuf = calloc(1, zb->inlen);
59 2181e0c8 2019-03-19 stsp if (zb->inbuf == NULL) {
60 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
61 2181e0c8 2019-03-19 stsp goto done;
62 2181e0c8 2019-03-19 stsp }
63 2181e0c8 2019-03-19 stsp
64 2181e0c8 2019-03-19 stsp zb->flags = 0;
65 2181e0c8 2019-03-19 stsp if (outbuf == NULL) {
66 2181e0c8 2019-03-19 stsp zb->outbuf = calloc(1, zb->outlen);
67 2181e0c8 2019-03-19 stsp if (zb->outbuf == NULL) {
68 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
69 2181e0c8 2019-03-19 stsp goto done;
70 2181e0c8 2019-03-19 stsp }
71 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
72 2181e0c8 2019-03-19 stsp } else
73 2181e0c8 2019-03-19 stsp zb->outbuf = outbuf;
74 2181e0c8 2019-03-19 stsp done:
75 2181e0c8 2019-03-19 stsp if (err)
76 2181e0c8 2019-03-19 stsp got_deflate_end(zb);
77 2181e0c8 2019-03-19 stsp return err;
78 2181e0c8 2019-03-19 stsp }
79 2181e0c8 2019-03-19 stsp
80 91b40e30 2021-05-21 stsp static void
81 dbaa2362 2021-09-28 thomas csum_output(struct got_deflate_checksum *csum, const uint8_t *buf, size_t len)
82 91b40e30 2021-05-21 stsp {
83 91b40e30 2021-05-21 stsp if (csum->output_crc)
84 91b40e30 2021-05-21 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
85 91b40e30 2021-05-21 stsp
86 91b40e30 2021-05-21 stsp if (csum->output_sha1)
87 91b40e30 2021-05-21 stsp SHA1Update(csum->output_sha1, buf, len);
88 91b40e30 2021-05-21 stsp }
89 91b40e30 2021-05-21 stsp
90 2181e0c8 2019-03-19 stsp const struct got_error *
91 e8f02263 2022-01-23 thomas got_deflate_read(struct got_deflate_buf *zb, FILE *f, off_t len,
92 e8f02263 2022-01-23 thomas size_t *outlenp, off_t *consumed)
93 2181e0c8 2019-03-19 stsp {
94 2181e0c8 2019-03-19 stsp size_t last_total_out = zb->z.total_out;
95 2181e0c8 2019-03-19 stsp z_stream *z = &zb->z;
96 2181e0c8 2019-03-19 stsp int ret = Z_ERRNO;
97 2181e0c8 2019-03-19 stsp
98 2181e0c8 2019-03-19 stsp z->next_out = zb->outbuf;
99 2181e0c8 2019-03-19 stsp z->avail_out = zb->outlen;
100 2181e0c8 2019-03-19 stsp
101 2181e0c8 2019-03-19 stsp *outlenp = 0;
102 e8f02263 2022-01-23 thomas *consumed = 0;
103 2181e0c8 2019-03-19 stsp do {
104 e8f02263 2022-01-23 thomas size_t last_total_in = z->total_in;
105 2181e0c8 2019-03-19 stsp if (z->avail_in == 0) {
106 e8f02263 2022-01-23 thomas size_t n = 0;
107 e8f02263 2022-01-23 thomas if (*consumed < len) {
108 e8f02263 2022-01-23 thomas n = fread(zb->inbuf, 1,
109 e8f02263 2022-01-23 thomas MIN(zb->inlen, len - *consumed), f);
110 e8f02263 2022-01-23 thomas }
111 2181e0c8 2019-03-19 stsp if (n == 0) {
112 2181e0c8 2019-03-19 stsp if (ferror(f))
113 2181e0c8 2019-03-19 stsp return got_ferror(f, GOT_ERR_IO);
114 2181e0c8 2019-03-19 stsp /* EOF */
115 4e4a7005 2019-04-13 stsp ret = deflate(z, Z_FINISH);
116 2181e0c8 2019-03-19 stsp break;
117 2181e0c8 2019-03-19 stsp }
118 2181e0c8 2019-03-19 stsp z->next_in = zb->inbuf;
119 2181e0c8 2019-03-19 stsp z->avail_in = n;
120 2181e0c8 2019-03-19 stsp }
121 2181e0c8 2019-03-19 stsp ret = deflate(z, Z_NO_FLUSH);
122 e8f02263 2022-01-23 thomas *consumed += z->total_in - last_total_in;
123 2181e0c8 2019-03-19 stsp } while (ret == Z_OK && z->avail_out > 0);
124 2181e0c8 2019-03-19 stsp
125 2181e0c8 2019-03-19 stsp if (ret == Z_OK) {
126 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
127 2181e0c8 2019-03-19 stsp } else {
128 2181e0c8 2019-03-19 stsp if (ret != Z_STREAM_END)
129 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
130 2181e0c8 2019-03-19 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
131 2181e0c8 2019-03-19 stsp }
132 2181e0c8 2019-03-19 stsp
133 2181e0c8 2019-03-19 stsp *outlenp = z->total_out - last_total_out;
134 2181e0c8 2019-03-19 stsp return NULL;
135 2181e0c8 2019-03-19 stsp }
136 2181e0c8 2019-03-19 stsp
137 9249e7e3 2022-05-12 thomas static const struct got_error *
138 9249e7e3 2022-05-12 thomas deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
139 9249e7e3 2022-05-12 thomas size_t len, size_t *outlenp, size_t *consumed, int flush_on_eof)
140 2b0ae357 2022-01-10 thomas {
141 2b0ae357 2022-01-10 thomas z_stream *z = &zb->z;
142 2b0ae357 2022-01-10 thomas size_t last_total_out = z->total_out;
143 2b0ae357 2022-01-10 thomas int ret = Z_ERRNO;
144 2b0ae357 2022-01-10 thomas
145 2b0ae357 2022-01-10 thomas z->next_out = zb->outbuf;
146 2b0ae357 2022-01-10 thomas z->avail_out = zb->outlen;
147 2b0ae357 2022-01-10 thomas
148 2b0ae357 2022-01-10 thomas *outlenp = 0;
149 2b0ae357 2022-01-10 thomas *consumed = 0;
150 2b0ae357 2022-01-10 thomas do {
151 2b0ae357 2022-01-10 thomas size_t last_total_in = z->total_in;
152 2b0ae357 2022-01-10 thomas if (z->avail_in == 0) {
153 2b0ae357 2022-01-10 thomas z->next_in = map + offset + *consumed;
154 f6a55b40 2022-02-12 thomas if (len - *consumed > UINT_MAX)
155 f6a55b40 2022-02-12 thomas z->avail_in = UINT_MAX;
156 f6a55b40 2022-02-12 thomas else
157 f6a55b40 2022-02-12 thomas z->avail_in = len - *consumed;
158 2b0ae357 2022-01-10 thomas if (z->avail_in == 0) {
159 2b0ae357 2022-01-10 thomas /* EOF */
160 9249e7e3 2022-05-12 thomas if (flush_on_eof)
161 9249e7e3 2022-05-12 thomas ret = deflate(z, Z_FINISH);
162 2b0ae357 2022-01-10 thomas break;
163 2b0ae357 2022-01-10 thomas }
164 2b0ae357 2022-01-10 thomas }
165 2b0ae357 2022-01-10 thomas ret = deflate(z, Z_NO_FLUSH);
166 2b0ae357 2022-01-10 thomas *consumed += z->total_in - last_total_in;
167 2b0ae357 2022-01-10 thomas } while (ret == Z_OK && z->avail_out > 0);
168 2b0ae357 2022-01-10 thomas
169 2b0ae357 2022-01-10 thomas if (ret == Z_OK) {
170 2b0ae357 2022-01-10 thomas zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
171 2b0ae357 2022-01-10 thomas } else {
172 2b0ae357 2022-01-10 thomas if (ret != Z_STREAM_END)
173 2b0ae357 2022-01-10 thomas return got_error(GOT_ERR_COMPRESSION);
174 2b0ae357 2022-01-10 thomas zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
175 2b0ae357 2022-01-10 thomas }
176 2b0ae357 2022-01-10 thomas
177 2b0ae357 2022-01-10 thomas *outlenp = z->total_out - last_total_out;
178 9249e7e3 2022-05-12 thomas return NULL;
179 9249e7e3 2022-05-12 thomas }
180 9249e7e3 2022-05-12 thomas
181 9249e7e3 2022-05-12 thomas const struct got_error *
182 9249e7e3 2022-05-12 thomas got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
183 9249e7e3 2022-05-12 thomas size_t len, size_t *outlenp, size_t *consumed)
184 9249e7e3 2022-05-12 thomas {
185 9249e7e3 2022-05-12 thomas return deflate_read_mmap(zb, map, offset, len, outlenp, consumed, 1);
186 9249e7e3 2022-05-12 thomas }
187 9249e7e3 2022-05-12 thomas
188 9249e7e3 2022-05-12 thomas const struct got_error *
189 9249e7e3 2022-05-12 thomas got_deflate_flush(struct got_deflate_buf *zb, FILE *outfile,
190 9249e7e3 2022-05-12 thomas struct got_deflate_checksum *csum, off_t *outlenp)
191 9249e7e3 2022-05-12 thomas {
192 9249e7e3 2022-05-12 thomas int ret;
193 9249e7e3 2022-05-12 thomas size_t n;
194 9249e7e3 2022-05-12 thomas z_stream *z = &zb->z;
195 9249e7e3 2022-05-12 thomas
196 9249e7e3 2022-05-12 thomas if (z->avail_in != 0)
197 9249e7e3 2022-05-12 thomas return got_error_msg(GOT_ERR_COMPRESSION,
198 9249e7e3 2022-05-12 thomas "cannot flush zb with pending input data");
199 9249e7e3 2022-05-12 thomas
200 9249e7e3 2022-05-12 thomas do {
201 9249e7e3 2022-05-12 thomas size_t avail, last_total_out = zb->z.total_out;
202 9249e7e3 2022-05-12 thomas
203 9249e7e3 2022-05-12 thomas z->next_out = zb->outbuf;
204 9249e7e3 2022-05-12 thomas z->avail_out = zb->outlen;
205 9249e7e3 2022-05-12 thomas
206 9249e7e3 2022-05-12 thomas ret = deflate(z, Z_FINISH);
207 9249e7e3 2022-05-12 thomas if (ret != Z_STREAM_END && ret != Z_OK)
208 9249e7e3 2022-05-12 thomas return got_error(GOT_ERR_COMPRESSION);
209 9249e7e3 2022-05-12 thomas
210 9249e7e3 2022-05-12 thomas avail = z->total_out - last_total_out;
211 9249e7e3 2022-05-12 thomas if (avail > 0) {
212 9249e7e3 2022-05-12 thomas n = fwrite(zb->outbuf, avail, 1, outfile);
213 9249e7e3 2022-05-12 thomas if (n != 1)
214 9249e7e3 2022-05-12 thomas return got_ferror(outfile, GOT_ERR_IO);
215 9249e7e3 2022-05-12 thomas if (csum)
216 9249e7e3 2022-05-12 thomas csum_output(csum, zb->outbuf, avail);
217 9249e7e3 2022-05-12 thomas if (outlenp)
218 9249e7e3 2022-05-12 thomas *outlenp += avail;
219 9249e7e3 2022-05-12 thomas }
220 9249e7e3 2022-05-12 thomas } while (ret != Z_STREAM_END);
221 9249e7e3 2022-05-12 thomas
222 9249e7e3 2022-05-12 thomas zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
223 2b0ae357 2022-01-10 thomas return NULL;
224 2b0ae357 2022-01-10 thomas }
225 2b0ae357 2022-01-10 thomas
226 2181e0c8 2019-03-19 stsp void
227 2181e0c8 2019-03-19 stsp got_deflate_end(struct got_deflate_buf *zb)
228 2181e0c8 2019-03-19 stsp {
229 2181e0c8 2019-03-19 stsp free(zb->inbuf);
230 2181e0c8 2019-03-19 stsp if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
231 2181e0c8 2019-03-19 stsp free(zb->outbuf);
232 2181e0c8 2019-03-19 stsp deflateEnd(&zb->z);
233 2181e0c8 2019-03-19 stsp }
234 2181e0c8 2019-03-19 stsp
235 2181e0c8 2019-03-19 stsp const struct got_error *
236 e8f02263 2022-01-23 thomas got_deflate_to_file(off_t *outlen, FILE *infile, off_t len,
237 e8f02263 2022-01-23 thomas FILE *outfile, struct got_deflate_checksum *csum)
238 2181e0c8 2019-03-19 stsp {
239 2181e0c8 2019-03-19 stsp const struct got_error *err;
240 2181e0c8 2019-03-19 stsp size_t avail;
241 e8f02263 2022-01-23 thomas off_t consumed;
242 2181e0c8 2019-03-19 stsp struct got_deflate_buf zb;
243 2181e0c8 2019-03-19 stsp
244 3b9e6fcf 2021-06-05 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
245 2181e0c8 2019-03-19 stsp if (err)
246 2181e0c8 2019-03-19 stsp goto done;
247 2181e0c8 2019-03-19 stsp
248 2181e0c8 2019-03-19 stsp *outlen = 0;
249 2181e0c8 2019-03-19 stsp
250 2181e0c8 2019-03-19 stsp do {
251 e8f02263 2022-01-23 thomas err = got_deflate_read(&zb, infile, len, &avail, &consumed);
252 2181e0c8 2019-03-19 stsp if (err)
253 2181e0c8 2019-03-19 stsp goto done;
254 e8f02263 2022-01-23 thomas len -= consumed;
255 2181e0c8 2019-03-19 stsp if (avail > 0) {
256 2181e0c8 2019-03-19 stsp size_t n;
257 2181e0c8 2019-03-19 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
258 2181e0c8 2019-03-19 stsp if (n != 1) {
259 2181e0c8 2019-03-19 stsp err = got_ferror(outfile, GOT_ERR_IO);
260 2181e0c8 2019-03-19 stsp goto done;
261 2181e0c8 2019-03-19 stsp }
262 3b9e6fcf 2021-06-05 stsp if (csum)
263 3b9e6fcf 2021-06-05 stsp csum_output(csum, zb.outbuf, avail);
264 2181e0c8 2019-03-19 stsp *outlen += avail;
265 2181e0c8 2019-03-19 stsp }
266 2181e0c8 2019-03-19 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
267 2181e0c8 2019-03-19 stsp
268 2181e0c8 2019-03-19 stsp done:
269 2181e0c8 2019-03-19 stsp got_deflate_end(&zb);
270 2181e0c8 2019-03-19 stsp return err;
271 2181e0c8 2019-03-19 stsp }
272 2b0ae357 2022-01-10 thomas
273 2b0ae357 2022-01-10 thomas const struct got_error *
274 e8f02263 2022-01-23 thomas got_deflate_to_file_mmap(off_t *outlen, uint8_t *map, size_t offset,
275 2b0ae357 2022-01-10 thomas size_t len, FILE *outfile, struct got_deflate_checksum *csum)
276 2b0ae357 2022-01-10 thomas {
277 2b0ae357 2022-01-10 thomas const struct got_error *err;
278 2b0ae357 2022-01-10 thomas size_t avail, consumed;
279 2b0ae357 2022-01-10 thomas struct got_deflate_buf zb;
280 2b0ae357 2022-01-10 thomas
281 2b0ae357 2022-01-10 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
282 2b0ae357 2022-01-10 thomas if (err)
283 2b0ae357 2022-01-10 thomas goto done;
284 2b0ae357 2022-01-10 thomas
285 2b0ae357 2022-01-10 thomas *outlen = 0;
286 2b0ae357 2022-01-10 thomas do {
287 2b0ae357 2022-01-10 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
288 2b0ae357 2022-01-10 thomas &consumed);
289 2b0ae357 2022-01-10 thomas if (err)
290 2b0ae357 2022-01-10 thomas goto done;
291 2b0ae357 2022-01-10 thomas offset += consumed;
292 2b0ae357 2022-01-10 thomas len -= consumed;
293 2b0ae357 2022-01-10 thomas if (avail > 0) {
294 2b0ae357 2022-01-10 thomas size_t n;
295 2b0ae357 2022-01-10 thomas n = fwrite(zb.outbuf, avail, 1, outfile);
296 2b0ae357 2022-01-10 thomas if (n != 1) {
297 2b0ae357 2022-01-10 thomas err = got_ferror(outfile, GOT_ERR_IO);
298 2b0ae357 2022-01-10 thomas goto done;
299 2b0ae357 2022-01-10 thomas }
300 2b0ae357 2022-01-10 thomas if (csum)
301 2b0ae357 2022-01-10 thomas csum_output(csum, zb.outbuf, avail);
302 2b0ae357 2022-01-10 thomas *outlen += avail;
303 2b0ae357 2022-01-10 thomas }
304 2b0ae357 2022-01-10 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
305 9249e7e3 2022-05-12 thomas
306 9249e7e3 2022-05-12 thomas done:
307 9249e7e3 2022-05-12 thomas got_deflate_end(&zb);
308 9249e7e3 2022-05-12 thomas return err;
309 9249e7e3 2022-05-12 thomas }
310 9249e7e3 2022-05-12 thomas
311 9249e7e3 2022-05-12 thomas const struct got_error *
312 9249e7e3 2022-05-12 thomas got_deflate_append_to_file_mmap(struct got_deflate_buf *zb, off_t *outlen,
313 9249e7e3 2022-05-12 thomas uint8_t *map, size_t offset, size_t len, FILE *outfile,
314 9249e7e3 2022-05-12 thomas struct got_deflate_checksum *csum)
315 9249e7e3 2022-05-12 thomas {
316 9249e7e3 2022-05-12 thomas const struct got_error *err;
317 9249e7e3 2022-05-12 thomas size_t avail, consumed;
318 9249e7e3 2022-05-12 thomas
319 9249e7e3 2022-05-12 thomas do {
320 9249e7e3 2022-05-12 thomas err = deflate_read_mmap(zb, map, offset, len, &avail,
321 9249e7e3 2022-05-12 thomas &consumed, 0);
322 9249e7e3 2022-05-12 thomas if (err)
323 9249e7e3 2022-05-12 thomas break;
324 9249e7e3 2022-05-12 thomas offset += consumed;
325 9249e7e3 2022-05-12 thomas len -= consumed;
326 9249e7e3 2022-05-12 thomas if (avail > 0) {
327 9249e7e3 2022-05-12 thomas size_t n;
328 9249e7e3 2022-05-12 thomas n = fwrite(zb->outbuf, avail, 1, outfile);
329 9249e7e3 2022-05-12 thomas if (n != 1) {
330 9249e7e3 2022-05-12 thomas err = got_ferror(outfile, GOT_ERR_IO);
331 9249e7e3 2022-05-12 thomas break;
332 9249e7e3 2022-05-12 thomas }
333 9249e7e3 2022-05-12 thomas if (csum)
334 9249e7e3 2022-05-12 thomas csum_output(csum, zb->outbuf, avail);
335 9249e7e3 2022-05-12 thomas if (outlen)
336 9249e7e3 2022-05-12 thomas *outlen += avail;
337 9249e7e3 2022-05-12 thomas }
338 9249e7e3 2022-05-12 thomas } while ((zb->flags & GOT_DEFLATE_F_HAVE_MORE) && len > 0);
339 2b0ae357 2022-01-10 thomas
340 9249e7e3 2022-05-12 thomas return err;
341 9249e7e3 2022-05-12 thomas }
342 9249e7e3 2022-05-12 thomas
343 9249e7e3 2022-05-12 thomas const struct got_error *
344 9249e7e3 2022-05-12 thomas got_deflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
345 9249e7e3 2022-05-12 thomas size_t *consumed_total, struct got_deflate_checksum *csum, uint8_t *map,
346 9249e7e3 2022-05-12 thomas size_t offset, size_t len)
347 9249e7e3 2022-05-12 thomas {
348 9249e7e3 2022-05-12 thomas const struct got_error *err;
349 9249e7e3 2022-05-12 thomas size_t avail, consumed;
350 9249e7e3 2022-05-12 thomas struct got_deflate_buf zb;
351 9249e7e3 2022-05-12 thomas void *newbuf;
352 9249e7e3 2022-05-12 thomas size_t nbuf = 1;
353 9249e7e3 2022-05-12 thomas
354 9249e7e3 2022-05-12 thomas if (outbuf) {
355 9249e7e3 2022-05-12 thomas *outbuf = malloc(GOT_DEFLATE_BUFSIZE);
356 9249e7e3 2022-05-12 thomas if (*outbuf == NULL)
357 9249e7e3 2022-05-12 thomas return got_error_from_errno("malloc");
358 9249e7e3 2022-05-12 thomas err = got_deflate_init(&zb, *outbuf, GOT_DEFLATE_BUFSIZE);
359 9249e7e3 2022-05-12 thomas if (err) {
360 9249e7e3 2022-05-12 thomas free(*outbuf);
361 9249e7e3 2022-05-12 thomas *outbuf = NULL;
362 9249e7e3 2022-05-12 thomas return err;
363 9249e7e3 2022-05-12 thomas }
364 9249e7e3 2022-05-12 thomas } else {
365 9249e7e3 2022-05-12 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
366 9249e7e3 2022-05-12 thomas if (err)
367 9249e7e3 2022-05-12 thomas return err;
368 9249e7e3 2022-05-12 thomas }
369 9249e7e3 2022-05-12 thomas
370 9249e7e3 2022-05-12 thomas *outlen = 0;
371 9249e7e3 2022-05-12 thomas if (consumed_total)
372 9249e7e3 2022-05-12 thomas *consumed_total = 0;
373 9249e7e3 2022-05-12 thomas do {
374 9249e7e3 2022-05-12 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
375 9249e7e3 2022-05-12 thomas &consumed);
376 9249e7e3 2022-05-12 thomas if (err)
377 9249e7e3 2022-05-12 thomas goto done;
378 9249e7e3 2022-05-12 thomas offset += consumed;
379 9249e7e3 2022-05-12 thomas if (consumed_total)
380 9249e7e3 2022-05-12 thomas *consumed_total += consumed;
381 9249e7e3 2022-05-12 thomas len -= consumed;
382 9249e7e3 2022-05-12 thomas if (avail > 0 && csum)
383 9249e7e3 2022-05-12 thomas csum_output(csum, zb.outbuf, avail);
384 9249e7e3 2022-05-12 thomas *outlen += avail;
385 9249e7e3 2022-05-12 thomas if ((zb.flags & GOT_DEFLATE_F_HAVE_MORE) && outbuf != NULL) {
386 9249e7e3 2022-05-12 thomas newbuf = reallocarray(*outbuf, ++nbuf,
387 9249e7e3 2022-05-12 thomas GOT_DEFLATE_BUFSIZE);
388 9249e7e3 2022-05-12 thomas if (newbuf == NULL) {
389 9249e7e3 2022-05-12 thomas err = got_error_from_errno("reallocarray");
390 9249e7e3 2022-05-12 thomas free(*outbuf);
391 9249e7e3 2022-05-12 thomas *outbuf = NULL;
392 9249e7e3 2022-05-12 thomas *outlen = 0;
393 9249e7e3 2022-05-12 thomas goto done;
394 9249e7e3 2022-05-12 thomas }
395 9249e7e3 2022-05-12 thomas *outbuf = newbuf;
396 9249e7e3 2022-05-12 thomas zb.outbuf = newbuf + *outlen;
397 9249e7e3 2022-05-12 thomas zb.outlen = (nbuf * GOT_DEFLATE_BUFSIZE) - *outlen;
398 9249e7e3 2022-05-12 thomas }
399 9249e7e3 2022-05-12 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
400 2b0ae357 2022-01-10 thomas done:
401 2b0ae357 2022-01-10 thomas got_deflate_end(&zb);
402 2b0ae357 2022-01-10 thomas return err;
403 2b0ae357 2022-01-10 thomas }