Blame


1 63581804 2018-07-09 stsp /*
2 63581804 2018-07-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 63581804 2018-07-09 stsp *
4 63581804 2018-07-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 63581804 2018-07-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 63581804 2018-07-09 stsp * copyright notice and this permission notice appear in all copies.
7 63581804 2018-07-09 stsp *
8 63581804 2018-07-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 63581804 2018-07-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 63581804 2018-07-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 63581804 2018-07-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 63581804 2018-07-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 63581804 2018-07-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 63581804 2018-07-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 63581804 2018-07-09 stsp */
16 63581804 2018-07-09 stsp
17 63581804 2018-07-09 stsp #include <sys/queue.h>
18 63581804 2018-07-09 stsp
19 5211b8c8 2019-03-19 stsp #include <errno.h>
20 63581804 2018-07-09 stsp #include <stdio.h>
21 63581804 2018-07-09 stsp #include <stdlib.h>
22 63581804 2018-07-09 stsp #include <string.h>
23 63581804 2018-07-09 stsp #include <sha1.h>
24 63581804 2018-07-09 stsp #include <zlib.h>
25 63581804 2018-07-09 stsp #include <time.h>
26 63581804 2018-07-09 stsp
27 63581804 2018-07-09 stsp #include "got_error.h"
28 63581804 2018-07-09 stsp #include "got_object.h"
29 324d37e7 2019-05-11 stsp #include "got_path.h"
30 63581804 2018-07-09 stsp
31 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
32 63581804 2018-07-09 stsp
33 63581804 2018-07-09 stsp #ifndef MIN
34 63581804 2018-07-09 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 63581804 2018-07-09 stsp #endif
36 63581804 2018-07-09 stsp
37 63581804 2018-07-09 stsp const struct got_error *
38 1e87a3c3 2020-03-18 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
39 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum)
40 63581804 2018-07-09 stsp {
41 63581804 2018-07-09 stsp const struct got_error *err = NULL;
42 5211b8c8 2019-03-19 stsp int zerr;
43 63581804 2018-07-09 stsp
44 63581804 2018-07-09 stsp memset(&zb->z, 0, sizeof(zb->z));
45 63581804 2018-07-09 stsp
46 63581804 2018-07-09 stsp zb->z.zalloc = Z_NULL;
47 63581804 2018-07-09 stsp zb->z.zfree = Z_NULL;
48 5211b8c8 2019-03-19 stsp zerr = inflateInit(&zb->z);
49 5211b8c8 2019-03-19 stsp if (zerr != Z_OK) {
50 5211b8c8 2019-03-19 stsp if (zerr == Z_ERRNO)
51 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
52 5211b8c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
53 5211b8c8 2019-03-19 stsp errno = ENOMEM;
54 638f9024 2019-05-13 stsp return got_error_from_errno("inflateInit");
55 5211b8c8 2019-03-19 stsp }
56 5211b8c8 2019-03-19 stsp return got_error(GOT_ERR_DECOMPRESSION);
57 63581804 2018-07-09 stsp }
58 63581804 2018-07-09 stsp
59 63581804 2018-07-09 stsp zb->inlen = zb->outlen = bufsize;
60 63581804 2018-07-09 stsp
61 63581804 2018-07-09 stsp zb->inbuf = calloc(1, zb->inlen);
62 63581804 2018-07-09 stsp if (zb->inbuf == NULL) {
63 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
64 63581804 2018-07-09 stsp goto done;
65 63581804 2018-07-09 stsp }
66 63581804 2018-07-09 stsp
67 63581804 2018-07-09 stsp zb->flags = 0;
68 63581804 2018-07-09 stsp if (outbuf == NULL) {
69 63581804 2018-07-09 stsp zb->outbuf = calloc(1, zb->outlen);
70 63581804 2018-07-09 stsp if (zb->outbuf == NULL) {
71 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
72 63581804 2018-07-09 stsp goto done;
73 63581804 2018-07-09 stsp }
74 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_OWN_OUTBUF;
75 63581804 2018-07-09 stsp } else
76 63581804 2018-07-09 stsp zb->outbuf = outbuf;
77 63581804 2018-07-09 stsp
78 6ad68bce 2020-03-24 stsp zb->csum = csum;
79 63581804 2018-07-09 stsp done:
80 63581804 2018-07-09 stsp if (err)
81 63581804 2018-07-09 stsp got_inflate_end(zb);
82 63581804 2018-07-09 stsp return err;
83 63581804 2018-07-09 stsp }
84 63581804 2018-07-09 stsp
85 6ad68bce 2020-03-24 stsp static void
86 6ad68bce 2020-03-24 stsp csum_input(struct got_inflate_checksum *csum, const char *buf, size_t len)
87 6ad68bce 2020-03-24 stsp {
88 6ad68bce 2020-03-24 stsp if (csum->input_crc)
89 6ad68bce 2020-03-24 stsp *csum->input_crc = crc32(*csum->input_crc, buf, len);
90 6ad68bce 2020-03-24 stsp
91 6ad68bce 2020-03-24 stsp if (csum->input_sha1)
92 6ad68bce 2020-03-24 stsp SHA1Update(csum->input_sha1, buf, len);
93 6ad68bce 2020-03-24 stsp }
94 6ad68bce 2020-03-24 stsp
95 63581804 2018-07-09 stsp const struct got_error *
96 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
97 6fb3a497 2020-03-18 stsp size_t *consumed)
98 63581804 2018-07-09 stsp {
99 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
100 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
101 63581804 2018-07-09 stsp z_stream *z = &zb->z;
102 63581804 2018-07-09 stsp int ret = Z_ERRNO;
103 63581804 2018-07-09 stsp
104 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
105 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
106 63581804 2018-07-09 stsp
107 63581804 2018-07-09 stsp *outlenp = 0;
108 6fb3a497 2020-03-18 stsp if (consumed)
109 6fb3a497 2020-03-18 stsp *consumed = 0;
110 63581804 2018-07-09 stsp do {
111 6ad68bce 2020-03-24 stsp char *csum_in = NULL;
112 6ad68bce 2020-03-24 stsp size_t csum_avail = 0;
113 1e87a3c3 2020-03-18 stsp
114 63581804 2018-07-09 stsp if (z->avail_in == 0) {
115 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
116 63581804 2018-07-09 stsp if (n == 0) {
117 63581804 2018-07-09 stsp if (ferror(f))
118 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
119 63581804 2018-07-09 stsp /* EOF */
120 63581804 2018-07-09 stsp ret = Z_STREAM_END;
121 63581804 2018-07-09 stsp break;
122 63581804 2018-07-09 stsp }
123 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
124 63581804 2018-07-09 stsp z->avail_in = n;
125 63581804 2018-07-09 stsp }
126 6ad68bce 2020-03-24 stsp if (zb->csum) {
127 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
128 6ad68bce 2020-03-24 stsp csum_avail = z->avail_in;
129 1e87a3c3 2020-03-18 stsp }
130 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
131 6ad68bce 2020-03-24 stsp if (zb->csum)
132 6ad68bce 2020-03-24 stsp csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
133 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
134 63581804 2018-07-09 stsp
135 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
136 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
137 63581804 2018-07-09 stsp } else {
138 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
139 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
140 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
141 63581804 2018-07-09 stsp }
142 63581804 2018-07-09 stsp
143 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
144 6fb3a497 2020-03-18 stsp if (consumed)
145 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
146 63581804 2018-07-09 stsp return NULL;
147 63581804 2018-07-09 stsp }
148 63581804 2018-07-09 stsp
149 63581804 2018-07-09 stsp const struct got_error *
150 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
151 3ab5e33c 2020-03-18 stsp size_t *consumed)
152 63581804 2018-07-09 stsp {
153 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
154 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
155 63581804 2018-07-09 stsp z_stream *z = &zb->z;
156 63581804 2018-07-09 stsp int ret = Z_ERRNO;
157 63581804 2018-07-09 stsp
158 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
159 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
160 63581804 2018-07-09 stsp
161 63581804 2018-07-09 stsp *outlenp = 0;
162 3ab5e33c 2020-03-18 stsp if (consumed)
163 3ab5e33c 2020-03-18 stsp *consumed = 0;
164 63581804 2018-07-09 stsp do {
165 6ad68bce 2020-03-24 stsp char *csum_in = NULL;
166 6ad68bce 2020-03-24 stsp size_t csum_avail = 0;
167 1e87a3c3 2020-03-18 stsp
168 63581804 2018-07-09 stsp if (z->avail_in == 0) {
169 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
170 63581804 2018-07-09 stsp if (n < 0)
171 638f9024 2019-05-13 stsp return got_error_from_errno("read");
172 63581804 2018-07-09 stsp else if (n == 0) {
173 63581804 2018-07-09 stsp /* EOF */
174 63581804 2018-07-09 stsp ret = Z_STREAM_END;
175 63581804 2018-07-09 stsp break;
176 63581804 2018-07-09 stsp }
177 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
178 63581804 2018-07-09 stsp z->avail_in = n;
179 63581804 2018-07-09 stsp }
180 6ad68bce 2020-03-24 stsp if (zb->csum) {
181 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
182 6ad68bce 2020-03-24 stsp csum_avail = z->avail_in;
183 1e87a3c3 2020-03-18 stsp }
184 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
185 6ad68bce 2020-03-24 stsp if (zb->csum)
186 6ad68bce 2020-03-24 stsp csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
187 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
188 63581804 2018-07-09 stsp
189 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
190 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
191 63581804 2018-07-09 stsp } else {
192 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
193 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
194 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
195 63581804 2018-07-09 stsp }
196 63581804 2018-07-09 stsp
197 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
198 3ab5e33c 2020-03-18 stsp if (consumed)
199 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
200 63581804 2018-07-09 stsp return NULL;
201 63581804 2018-07-09 stsp }
202 63581804 2018-07-09 stsp
203 63581804 2018-07-09 stsp const struct got_error *
204 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
205 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
206 63581804 2018-07-09 stsp {
207 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
208 63581804 2018-07-09 stsp z_stream *z = &zb->z;
209 63581804 2018-07-09 stsp int ret = Z_ERRNO;
210 63581804 2018-07-09 stsp
211 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
212 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
213 63581804 2018-07-09 stsp
214 63581804 2018-07-09 stsp *outlenp = 0;
215 63581804 2018-07-09 stsp *consumed = 0;
216 63581804 2018-07-09 stsp
217 63581804 2018-07-09 stsp do {
218 6ad68bce 2020-03-24 stsp char *csum_in = NULL;
219 6ad68bce 2020-03-24 stsp size_t csum_avail = 0;
220 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
221 1e87a3c3 2020-03-18 stsp
222 63581804 2018-07-09 stsp if (z->avail_in == 0) {
223 63581804 2018-07-09 stsp if (len == 0) {
224 63581804 2018-07-09 stsp /* EOF */
225 63581804 2018-07-09 stsp ret = Z_STREAM_END;
226 63581804 2018-07-09 stsp break;
227 63581804 2018-07-09 stsp }
228 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
229 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
230 63581804 2018-07-09 stsp }
231 6ad68bce 2020-03-24 stsp if (zb->csum) {
232 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
233 6ad68bce 2020-03-24 stsp csum_avail = z->avail_in;
234 1e87a3c3 2020-03-18 stsp }
235 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
236 6ad68bce 2020-03-24 stsp if (zb->csum)
237 6ad68bce 2020-03-24 stsp csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
238 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
239 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
240 63581804 2018-07-09 stsp
241 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
242 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
243 63581804 2018-07-09 stsp } else {
244 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
245 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
246 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
247 63581804 2018-07-09 stsp }
248 63581804 2018-07-09 stsp
249 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
250 63581804 2018-07-09 stsp return NULL;
251 63581804 2018-07-09 stsp }
252 63581804 2018-07-09 stsp
253 63581804 2018-07-09 stsp void
254 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
255 63581804 2018-07-09 stsp {
256 63581804 2018-07-09 stsp free(zb->inbuf);
257 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
258 63581804 2018-07-09 stsp free(zb->outbuf);
259 63581804 2018-07-09 stsp inflateEnd(&zb->z);
260 63581804 2018-07-09 stsp }
261 63581804 2018-07-09 stsp
262 63581804 2018-07-09 stsp const struct got_error *
263 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
264 6fb3a497 2020-03-18 stsp size_t *consumed_total, FILE *f)
265 63581804 2018-07-09 stsp {
266 63581804 2018-07-09 stsp const struct got_error *err;
267 6fb3a497 2020-03-18 stsp size_t avail, consumed;
268 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
269 63581804 2018-07-09 stsp void *newbuf;
270 17d745b8 2018-07-23 stsp int nbuf = 1;
271 63581804 2018-07-09 stsp
272 2decf4c6 2020-03-18 stsp if (outbuf) {
273 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
274 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
275 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
276 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
277 2decf4c6 2020-03-18 stsp } else
278 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
279 63581804 2018-07-09 stsp if (err)
280 63581804 2018-07-09 stsp return err;
281 63581804 2018-07-09 stsp
282 63581804 2018-07-09 stsp *outlen = 0;
283 6fb3a497 2020-03-18 stsp if (consumed_total)
284 6fb3a497 2020-03-18 stsp *consumed_total = 0;
285 63581804 2018-07-09 stsp
286 63581804 2018-07-09 stsp do {
287 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
288 63581804 2018-07-09 stsp if (err)
289 5aef3967 2018-07-22 stsp goto done;
290 63581804 2018-07-09 stsp *outlen += avail;
291 6fb3a497 2020-03-18 stsp if (consumed_total)
292 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
293 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
294 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
295 2decf4c6 2020-03-18 stsp continue;
296 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
297 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
298 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
299 63581804 2018-07-09 stsp if (newbuf == NULL) {
300 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
301 63581804 2018-07-09 stsp free(*outbuf);
302 63581804 2018-07-09 stsp *outbuf = NULL;
303 63581804 2018-07-09 stsp *outlen = 0;
304 63581804 2018-07-09 stsp goto done;
305 63581804 2018-07-09 stsp }
306 63581804 2018-07-09 stsp *outbuf = newbuf;
307 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
308 63581804 2018-07-09 stsp }
309 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
310 63581804 2018-07-09 stsp
311 63581804 2018-07-09 stsp done:
312 63581804 2018-07-09 stsp got_inflate_end(&zb);
313 63581804 2018-07-09 stsp return err;
314 63581804 2018-07-09 stsp }
315 63581804 2018-07-09 stsp
316 63581804 2018-07-09 stsp const struct got_error *
317 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
318 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
319 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
320 63581804 2018-07-09 stsp {
321 63581804 2018-07-09 stsp const struct got_error *err;
322 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
323 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
324 63581804 2018-07-09 stsp void *newbuf;
325 17d745b8 2018-07-23 stsp int nbuf = 1;
326 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
327 63581804 2018-07-09 stsp
328 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
329 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
330 55fdd257 2020-03-18 stsp bufsize = expected_size;
331 55fdd257 2020-03-18 stsp
332 2decf4c6 2020-03-18 stsp if (outbuf) {
333 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
334 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
335 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
336 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
337 2decf4c6 2020-03-18 stsp } else
338 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
339 63581804 2018-07-09 stsp if (err)
340 5aef3967 2018-07-22 stsp goto done;
341 63581804 2018-07-09 stsp
342 63581804 2018-07-09 stsp *outlen = 0;
343 3ab5e33c 2020-03-18 stsp if (consumed_total)
344 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
345 63581804 2018-07-09 stsp
346 63581804 2018-07-09 stsp do {
347 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
348 63581804 2018-07-09 stsp if (err)
349 5aef3967 2018-07-22 stsp goto done;
350 63581804 2018-07-09 stsp *outlen += avail;
351 3ab5e33c 2020-03-18 stsp if (consumed_total)
352 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
353 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
354 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
355 2decf4c6 2020-03-18 stsp continue;
356 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
357 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
358 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
359 63581804 2018-07-09 stsp if (newbuf == NULL) {
360 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
361 63581804 2018-07-09 stsp free(*outbuf);
362 63581804 2018-07-09 stsp *outbuf = NULL;
363 63581804 2018-07-09 stsp *outlen = 0;
364 63581804 2018-07-09 stsp goto done;
365 63581804 2018-07-09 stsp }
366 63581804 2018-07-09 stsp *outbuf = newbuf;
367 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
368 63581804 2018-07-09 stsp }
369 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
370 63581804 2018-07-09 stsp
371 63581804 2018-07-09 stsp done:
372 63581804 2018-07-09 stsp got_inflate_end(&zb);
373 63581804 2018-07-09 stsp return err;
374 63581804 2018-07-09 stsp }
375 63581804 2018-07-09 stsp
376 63581804 2018-07-09 stsp const struct got_error *
377 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
378 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
379 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
380 63581804 2018-07-09 stsp {
381 63581804 2018-07-09 stsp const struct got_error *err;
382 37bd7602 2018-07-23 stsp size_t avail, consumed;
383 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
384 63581804 2018-07-09 stsp void *newbuf;
385 37bd7602 2018-07-23 stsp int nbuf = 1;
386 63581804 2018-07-09 stsp
387 2e5a6fad 2020-03-18 stsp if (outbuf) {
388 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
389 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
390 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
391 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
392 2e5a6fad 2020-03-18 stsp if (err) {
393 2e5a6fad 2020-03-18 stsp free(*outbuf);
394 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
395 2e5a6fad 2020-03-18 stsp return err;
396 2e5a6fad 2020-03-18 stsp }
397 2e5a6fad 2020-03-18 stsp } else {
398 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
399 60507209 2018-07-13 stsp }
400 63581804 2018-07-09 stsp
401 63581804 2018-07-09 stsp *outlen = 0;
402 2e5a6fad 2020-03-18 stsp if (consumed_total)
403 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
404 63581804 2018-07-09 stsp do {
405 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
406 63581804 2018-07-09 stsp &consumed);
407 63581804 2018-07-09 stsp if (err)
408 3efa19e7 2018-07-13 stsp goto done;
409 63581804 2018-07-09 stsp offset += consumed;
410 2e5a6fad 2020-03-18 stsp if (consumed_total)
411 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
412 63581804 2018-07-09 stsp len -= consumed;
413 63581804 2018-07-09 stsp *outlen += avail;
414 63581804 2018-07-09 stsp if (len == 0)
415 63581804 2018-07-09 stsp break;
416 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
417 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
418 2e5a6fad 2020-03-18 stsp continue;
419 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
420 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
421 63581804 2018-07-09 stsp if (newbuf == NULL) {
422 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
423 63581804 2018-07-09 stsp free(*outbuf);
424 63581804 2018-07-09 stsp *outbuf = NULL;
425 63581804 2018-07-09 stsp *outlen = 0;
426 63581804 2018-07-09 stsp goto done;
427 63581804 2018-07-09 stsp }
428 63581804 2018-07-09 stsp *outbuf = newbuf;
429 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
430 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
431 63581804 2018-07-09 stsp }
432 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
433 63581804 2018-07-09 stsp done:
434 63581804 2018-07-09 stsp got_inflate_end(&zb);
435 63581804 2018-07-09 stsp return err;
436 63581804 2018-07-09 stsp }
437 63581804 2018-07-09 stsp
438 63581804 2018-07-09 stsp const struct got_error *
439 63581804 2018-07-09 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
440 63581804 2018-07-09 stsp {
441 63581804 2018-07-09 stsp const struct got_error *err = NULL;
442 63581804 2018-07-09 stsp size_t avail;
443 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
444 63581804 2018-07-09 stsp
445 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
446 63581804 2018-07-09 stsp if (err)
447 63581804 2018-07-09 stsp goto done;
448 63581804 2018-07-09 stsp
449 63581804 2018-07-09 stsp *outlen = 0;
450 63581804 2018-07-09 stsp
451 63581804 2018-07-09 stsp do {
452 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
453 63581804 2018-07-09 stsp if (err)
454 5aef3967 2018-07-22 stsp goto done;
455 63581804 2018-07-09 stsp if (avail > 0) {
456 63581804 2018-07-09 stsp ssize_t n;
457 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
458 63581804 2018-07-09 stsp if (n != avail) {
459 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
460 63581804 2018-07-09 stsp goto done;
461 63581804 2018-07-09 stsp }
462 63581804 2018-07-09 stsp *outlen += avail;
463 63581804 2018-07-09 stsp }
464 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
465 63581804 2018-07-09 stsp
466 63581804 2018-07-09 stsp done:
467 63581804 2018-07-09 stsp if (err == NULL) {
468 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
469 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
470 63581804 2018-07-09 stsp }
471 63581804 2018-07-09 stsp got_inflate_end(&zb);
472 63581804 2018-07-09 stsp return err;
473 63581804 2018-07-09 stsp }
474 63581804 2018-07-09 stsp
475 63581804 2018-07-09 stsp const struct got_error *
476 63581804 2018-07-09 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
477 63581804 2018-07-09 stsp {
478 63581804 2018-07-09 stsp const struct got_error *err;
479 63581804 2018-07-09 stsp size_t avail;
480 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
481 63581804 2018-07-09 stsp
482 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
483 63581804 2018-07-09 stsp if (err)
484 63581804 2018-07-09 stsp goto done;
485 63581804 2018-07-09 stsp
486 63581804 2018-07-09 stsp *outlen = 0;
487 63581804 2018-07-09 stsp
488 63581804 2018-07-09 stsp do {
489 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
490 63581804 2018-07-09 stsp if (err)
491 5aef3967 2018-07-22 stsp goto done;
492 63581804 2018-07-09 stsp if (avail > 0) {
493 63581804 2018-07-09 stsp size_t n;
494 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
495 63581804 2018-07-09 stsp if (n != 1) {
496 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
497 63581804 2018-07-09 stsp goto done;
498 63581804 2018-07-09 stsp }
499 63581804 2018-07-09 stsp *outlen += avail;
500 63581804 2018-07-09 stsp }
501 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
502 63581804 2018-07-09 stsp
503 63581804 2018-07-09 stsp done:
504 63581804 2018-07-09 stsp if (err == NULL)
505 63581804 2018-07-09 stsp rewind(outfile);
506 63581804 2018-07-09 stsp got_inflate_end(&zb);
507 63581804 2018-07-09 stsp return err;
508 63581804 2018-07-09 stsp }
509 63581804 2018-07-09 stsp
510 63581804 2018-07-09 stsp const struct got_error *
511 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
512 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
513 63581804 2018-07-09 stsp {
514 63581804 2018-07-09 stsp const struct got_error *err;
515 4788f1ce 2020-03-18 stsp size_t avail, consumed;
516 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
517 63581804 2018-07-09 stsp
518 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
519 63581804 2018-07-09 stsp if (err)
520 63581804 2018-07-09 stsp goto done;
521 63581804 2018-07-09 stsp
522 63581804 2018-07-09 stsp *outlen = 0;
523 4788f1ce 2020-03-18 stsp if (consumed_total)
524 4788f1ce 2020-03-18 stsp *consumed_total = 0;
525 63581804 2018-07-09 stsp do {
526 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
527 63581804 2018-07-09 stsp if (err)
528 5aef3967 2018-07-22 stsp goto done;
529 63581804 2018-07-09 stsp if (avail > 0) {
530 63581804 2018-07-09 stsp size_t n;
531 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
532 63581804 2018-07-09 stsp if (n != 1) {
533 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
534 63581804 2018-07-09 stsp goto done;
535 63581804 2018-07-09 stsp }
536 63581804 2018-07-09 stsp *outlen += avail;
537 4788f1ce 2020-03-18 stsp if (consumed_total)
538 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
539 63581804 2018-07-09 stsp }
540 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
541 63581804 2018-07-09 stsp
542 63581804 2018-07-09 stsp done:
543 63581804 2018-07-09 stsp if (err == NULL)
544 63581804 2018-07-09 stsp rewind(outfile);
545 63581804 2018-07-09 stsp got_inflate_end(&zb);
546 63581804 2018-07-09 stsp return err;
547 63581804 2018-07-09 stsp }
548 63581804 2018-07-09 stsp
549 63581804 2018-07-09 stsp const struct got_error *
550 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
551 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
552 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
553 63581804 2018-07-09 stsp {
554 63581804 2018-07-09 stsp const struct got_error *err;
555 4788f1ce 2020-03-18 stsp size_t avail, consumed;
556 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
557 63581804 2018-07-09 stsp
558 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
559 63581804 2018-07-09 stsp if (err)
560 63581804 2018-07-09 stsp goto done;
561 63581804 2018-07-09 stsp
562 63581804 2018-07-09 stsp *outlen = 0;
563 4788f1ce 2020-03-18 stsp if (consumed_total)
564 4788f1ce 2020-03-18 stsp *consumed_total = 0;
565 63581804 2018-07-09 stsp do {
566 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
567 63581804 2018-07-09 stsp &consumed);
568 63581804 2018-07-09 stsp if (err)
569 5aef3967 2018-07-22 stsp goto done;
570 63581804 2018-07-09 stsp offset += consumed;
571 4788f1ce 2020-03-18 stsp if (consumed_total)
572 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
573 63581804 2018-07-09 stsp len -= consumed;
574 63581804 2018-07-09 stsp if (avail > 0) {
575 63581804 2018-07-09 stsp size_t n;
576 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
577 63581804 2018-07-09 stsp if (n != 1) {
578 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
579 63581804 2018-07-09 stsp goto done;
580 63581804 2018-07-09 stsp }
581 63581804 2018-07-09 stsp *outlen += avail;
582 63581804 2018-07-09 stsp }
583 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
584 63581804 2018-07-09 stsp
585 63581804 2018-07-09 stsp done:
586 63581804 2018-07-09 stsp if (err == NULL)
587 63581804 2018-07-09 stsp rewind(outfile);
588 63581804 2018-07-09 stsp got_inflate_end(&zb);
589 63581804 2018-07-09 stsp return err;
590 63581804 2018-07-09 stsp }