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 1e87a3c3 2020-03-18 stsp uint32_t *input_crc)
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 1e87a3c3 2020-03-18 stsp zb->input_crc = input_crc;
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 63581804 2018-07-09 stsp const struct got_error *
86 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
87 6fb3a497 2020-03-18 stsp size_t *consumed)
88 63581804 2018-07-09 stsp {
89 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
90 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
91 63581804 2018-07-09 stsp z_stream *z = &zb->z;
92 63581804 2018-07-09 stsp int ret = Z_ERRNO;
93 63581804 2018-07-09 stsp
94 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
95 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
96 63581804 2018-07-09 stsp
97 63581804 2018-07-09 stsp *outlenp = 0;
98 6fb3a497 2020-03-18 stsp if (consumed)
99 6fb3a497 2020-03-18 stsp *consumed = 0;
100 63581804 2018-07-09 stsp do {
101 1e87a3c3 2020-03-18 stsp char *crc_in = NULL;
102 1e87a3c3 2020-03-18 stsp size_t crc_avail = 0;
103 1e87a3c3 2020-03-18 stsp
104 63581804 2018-07-09 stsp if (z->avail_in == 0) {
105 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
106 63581804 2018-07-09 stsp if (n == 0) {
107 63581804 2018-07-09 stsp if (ferror(f))
108 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
109 63581804 2018-07-09 stsp /* EOF */
110 63581804 2018-07-09 stsp ret = Z_STREAM_END;
111 63581804 2018-07-09 stsp break;
112 63581804 2018-07-09 stsp }
113 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
114 63581804 2018-07-09 stsp z->avail_in = n;
115 63581804 2018-07-09 stsp }
116 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
117 1e87a3c3 2020-03-18 stsp crc_in = z->next_in;
118 1e87a3c3 2020-03-18 stsp crc_avail = z->avail_in;
119 1e87a3c3 2020-03-18 stsp }
120 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
121 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
122 1e87a3c3 2020-03-18 stsp *zb->input_crc = crc32(*zb->input_crc,
123 1e87a3c3 2020-03-18 stsp crc_in, crc_avail - z->avail_in);
124 1e87a3c3 2020-03-18 stsp }
125 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
126 63581804 2018-07-09 stsp
127 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
128 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
129 63581804 2018-07-09 stsp } else {
130 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
131 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
132 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
133 63581804 2018-07-09 stsp }
134 63581804 2018-07-09 stsp
135 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
136 6fb3a497 2020-03-18 stsp if (consumed)
137 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
138 63581804 2018-07-09 stsp return NULL;
139 63581804 2018-07-09 stsp }
140 63581804 2018-07-09 stsp
141 63581804 2018-07-09 stsp const struct got_error *
142 3ab5e33c 2020-03-18 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
143 3ab5e33c 2020-03-18 stsp size_t *consumed)
144 63581804 2018-07-09 stsp {
145 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
146 3ab5e33c 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
147 63581804 2018-07-09 stsp z_stream *z = &zb->z;
148 63581804 2018-07-09 stsp int ret = Z_ERRNO;
149 63581804 2018-07-09 stsp
150 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
151 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
152 63581804 2018-07-09 stsp
153 63581804 2018-07-09 stsp *outlenp = 0;
154 3ab5e33c 2020-03-18 stsp if (consumed)
155 3ab5e33c 2020-03-18 stsp *consumed = 0;
156 63581804 2018-07-09 stsp do {
157 1e87a3c3 2020-03-18 stsp char *crc_in = NULL;
158 1e87a3c3 2020-03-18 stsp size_t crc_avail = 0;
159 1e87a3c3 2020-03-18 stsp
160 63581804 2018-07-09 stsp if (z->avail_in == 0) {
161 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
162 63581804 2018-07-09 stsp if (n < 0)
163 638f9024 2019-05-13 stsp return got_error_from_errno("read");
164 63581804 2018-07-09 stsp else if (n == 0) {
165 63581804 2018-07-09 stsp /* EOF */
166 63581804 2018-07-09 stsp ret = Z_STREAM_END;
167 63581804 2018-07-09 stsp break;
168 63581804 2018-07-09 stsp }
169 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
170 63581804 2018-07-09 stsp z->avail_in = n;
171 63581804 2018-07-09 stsp }
172 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
173 1e87a3c3 2020-03-18 stsp crc_in = z->next_in;
174 1e87a3c3 2020-03-18 stsp crc_avail = z->avail_in;
175 1e87a3c3 2020-03-18 stsp }
176 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
177 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
178 1e87a3c3 2020-03-18 stsp *zb->input_crc = crc32(*zb->input_crc,
179 1e87a3c3 2020-03-18 stsp crc_in, crc_avail - z->avail_in);
180 1e87a3c3 2020-03-18 stsp }
181 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
182 63581804 2018-07-09 stsp
183 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
184 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
185 63581804 2018-07-09 stsp } else {
186 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
187 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
188 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
189 63581804 2018-07-09 stsp }
190 63581804 2018-07-09 stsp
191 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
192 3ab5e33c 2020-03-18 stsp if (consumed)
193 3ab5e33c 2020-03-18 stsp *consumed += z->total_in - last_total_in;
194 63581804 2018-07-09 stsp return NULL;
195 63581804 2018-07-09 stsp }
196 63581804 2018-07-09 stsp
197 63581804 2018-07-09 stsp const struct got_error *
198 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
199 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
200 63581804 2018-07-09 stsp {
201 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
202 63581804 2018-07-09 stsp z_stream *z = &zb->z;
203 63581804 2018-07-09 stsp int ret = Z_ERRNO;
204 63581804 2018-07-09 stsp
205 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
206 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
207 63581804 2018-07-09 stsp
208 63581804 2018-07-09 stsp *outlenp = 0;
209 63581804 2018-07-09 stsp *consumed = 0;
210 63581804 2018-07-09 stsp
211 63581804 2018-07-09 stsp do {
212 1e87a3c3 2020-03-18 stsp char *crc_in = NULL;
213 1e87a3c3 2020-03-18 stsp size_t crc_avail = 0;
214 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
215 1e87a3c3 2020-03-18 stsp
216 63581804 2018-07-09 stsp if (z->avail_in == 0) {
217 63581804 2018-07-09 stsp if (len == 0) {
218 63581804 2018-07-09 stsp /* EOF */
219 63581804 2018-07-09 stsp ret = Z_STREAM_END;
220 63581804 2018-07-09 stsp break;
221 63581804 2018-07-09 stsp }
222 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
223 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
224 63581804 2018-07-09 stsp }
225 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
226 1e87a3c3 2020-03-18 stsp crc_in = z->next_in;
227 1e87a3c3 2020-03-18 stsp crc_avail = z->avail_in;
228 1e87a3c3 2020-03-18 stsp }
229 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
230 1e87a3c3 2020-03-18 stsp if (zb->input_crc) {
231 1e87a3c3 2020-03-18 stsp *zb->input_crc = crc32(*zb->input_crc,
232 1e87a3c3 2020-03-18 stsp crc_in, crc_avail - z->avail_in);
233 1e87a3c3 2020-03-18 stsp }
234 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
235 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
236 63581804 2018-07-09 stsp
237 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
238 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
239 63581804 2018-07-09 stsp } else {
240 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
241 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
242 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
243 63581804 2018-07-09 stsp }
244 63581804 2018-07-09 stsp
245 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
246 63581804 2018-07-09 stsp return NULL;
247 63581804 2018-07-09 stsp }
248 63581804 2018-07-09 stsp
249 63581804 2018-07-09 stsp void
250 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
251 63581804 2018-07-09 stsp {
252 63581804 2018-07-09 stsp free(zb->inbuf);
253 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
254 63581804 2018-07-09 stsp free(zb->outbuf);
255 63581804 2018-07-09 stsp inflateEnd(&zb->z);
256 63581804 2018-07-09 stsp }
257 63581804 2018-07-09 stsp
258 63581804 2018-07-09 stsp const struct got_error *
259 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
260 6fb3a497 2020-03-18 stsp size_t *consumed_total, FILE *f)
261 63581804 2018-07-09 stsp {
262 63581804 2018-07-09 stsp const struct got_error *err;
263 6fb3a497 2020-03-18 stsp size_t avail, consumed;
264 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
265 63581804 2018-07-09 stsp void *newbuf;
266 17d745b8 2018-07-23 stsp int nbuf = 1;
267 63581804 2018-07-09 stsp
268 2decf4c6 2020-03-18 stsp if (outbuf) {
269 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
270 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
271 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
272 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, NULL);
273 2decf4c6 2020-03-18 stsp } else
274 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
275 63581804 2018-07-09 stsp if (err)
276 63581804 2018-07-09 stsp return err;
277 63581804 2018-07-09 stsp
278 63581804 2018-07-09 stsp *outlen = 0;
279 6fb3a497 2020-03-18 stsp if (consumed_total)
280 6fb3a497 2020-03-18 stsp *consumed_total = 0;
281 63581804 2018-07-09 stsp
282 63581804 2018-07-09 stsp do {
283 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
284 63581804 2018-07-09 stsp if (err)
285 5aef3967 2018-07-22 stsp goto done;
286 63581804 2018-07-09 stsp *outlen += avail;
287 6fb3a497 2020-03-18 stsp if (consumed_total)
288 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
289 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
290 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
291 2decf4c6 2020-03-18 stsp continue;
292 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
293 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
294 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
295 63581804 2018-07-09 stsp if (newbuf == NULL) {
296 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
297 63581804 2018-07-09 stsp free(*outbuf);
298 63581804 2018-07-09 stsp *outbuf = NULL;
299 63581804 2018-07-09 stsp *outlen = 0;
300 63581804 2018-07-09 stsp goto done;
301 63581804 2018-07-09 stsp }
302 63581804 2018-07-09 stsp *outbuf = newbuf;
303 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
304 63581804 2018-07-09 stsp }
305 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
306 63581804 2018-07-09 stsp
307 63581804 2018-07-09 stsp done:
308 63581804 2018-07-09 stsp got_inflate_end(&zb);
309 63581804 2018-07-09 stsp return err;
310 63581804 2018-07-09 stsp }
311 63581804 2018-07-09 stsp
312 63581804 2018-07-09 stsp const struct got_error *
313 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
314 55fdd257 2020-03-18 stsp size_t *consumed_total, uint32_t *input_crc, size_t expected_size, int infd)
315 63581804 2018-07-09 stsp {
316 63581804 2018-07-09 stsp const struct got_error *err;
317 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
318 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
319 63581804 2018-07-09 stsp void *newbuf;
320 17d745b8 2018-07-23 stsp int nbuf = 1;
321 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
322 63581804 2018-07-09 stsp
323 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
324 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
325 55fdd257 2020-03-18 stsp bufsize = expected_size;
326 55fdd257 2020-03-18 stsp
327 2decf4c6 2020-03-18 stsp if (outbuf) {
328 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
329 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
330 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
331 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
332 1e87a3c3 2020-03-18 stsp input_crc);
333 2decf4c6 2020-03-18 stsp } else
334 55fdd257 2020-03-18 stsp err = got_inflate_init(&zb, NULL, bufsize, input_crc);
335 63581804 2018-07-09 stsp if (err)
336 5aef3967 2018-07-22 stsp goto done;
337 63581804 2018-07-09 stsp
338 63581804 2018-07-09 stsp *outlen = 0;
339 3ab5e33c 2020-03-18 stsp if (consumed_total)
340 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
341 63581804 2018-07-09 stsp
342 63581804 2018-07-09 stsp do {
343 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
344 63581804 2018-07-09 stsp if (err)
345 5aef3967 2018-07-22 stsp goto done;
346 63581804 2018-07-09 stsp *outlen += avail;
347 3ab5e33c 2020-03-18 stsp if (consumed_total)
348 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
349 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
350 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
351 2decf4c6 2020-03-18 stsp continue;
352 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
353 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
354 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
355 63581804 2018-07-09 stsp if (newbuf == NULL) {
356 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
357 63581804 2018-07-09 stsp free(*outbuf);
358 63581804 2018-07-09 stsp *outbuf = NULL;
359 63581804 2018-07-09 stsp *outlen = 0;
360 63581804 2018-07-09 stsp goto done;
361 63581804 2018-07-09 stsp }
362 63581804 2018-07-09 stsp *outbuf = newbuf;
363 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
364 63581804 2018-07-09 stsp }
365 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
366 63581804 2018-07-09 stsp
367 63581804 2018-07-09 stsp done:
368 63581804 2018-07-09 stsp got_inflate_end(&zb);
369 63581804 2018-07-09 stsp return err;
370 63581804 2018-07-09 stsp }
371 63581804 2018-07-09 stsp
372 63581804 2018-07-09 stsp const struct got_error *
373 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
374 2e5a6fad 2020-03-18 stsp size_t *consumed_total, uint32_t *input_crc, uint8_t *map, size_t offset,
375 2e5a6fad 2020-03-18 stsp size_t len)
376 63581804 2018-07-09 stsp {
377 63581804 2018-07-09 stsp const struct got_error *err;
378 37bd7602 2018-07-23 stsp size_t avail, consumed;
379 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
380 63581804 2018-07-09 stsp void *newbuf;
381 37bd7602 2018-07-23 stsp int nbuf = 1;
382 63581804 2018-07-09 stsp
383 2e5a6fad 2020-03-18 stsp if (outbuf) {
384 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
385 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
386 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
387 2e5a6fad 2020-03-18 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
388 2e5a6fad 2020-03-18 stsp input_crc);
389 2e5a6fad 2020-03-18 stsp if (err) {
390 2e5a6fad 2020-03-18 stsp free(*outbuf);
391 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
392 2e5a6fad 2020-03-18 stsp return err;
393 2e5a6fad 2020-03-18 stsp }
394 2e5a6fad 2020-03-18 stsp } else {
395 2e5a6fad 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE,
396 2e5a6fad 2020-03-18 stsp input_crc);
397 60507209 2018-07-13 stsp }
398 63581804 2018-07-09 stsp
399 63581804 2018-07-09 stsp *outlen = 0;
400 2e5a6fad 2020-03-18 stsp if (consumed_total)
401 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
402 63581804 2018-07-09 stsp do {
403 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
404 63581804 2018-07-09 stsp &consumed);
405 63581804 2018-07-09 stsp if (err)
406 3efa19e7 2018-07-13 stsp goto done;
407 63581804 2018-07-09 stsp offset += consumed;
408 2e5a6fad 2020-03-18 stsp if (consumed_total)
409 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
410 63581804 2018-07-09 stsp len -= consumed;
411 63581804 2018-07-09 stsp *outlen += avail;
412 63581804 2018-07-09 stsp if (len == 0)
413 63581804 2018-07-09 stsp break;
414 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
415 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
416 2e5a6fad 2020-03-18 stsp continue;
417 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
418 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
419 63581804 2018-07-09 stsp if (newbuf == NULL) {
420 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
421 63581804 2018-07-09 stsp free(*outbuf);
422 63581804 2018-07-09 stsp *outbuf = NULL;
423 63581804 2018-07-09 stsp *outlen = 0;
424 63581804 2018-07-09 stsp goto done;
425 63581804 2018-07-09 stsp }
426 63581804 2018-07-09 stsp *outbuf = newbuf;
427 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
428 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
429 63581804 2018-07-09 stsp }
430 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
431 63581804 2018-07-09 stsp done:
432 63581804 2018-07-09 stsp got_inflate_end(&zb);
433 63581804 2018-07-09 stsp return err;
434 63581804 2018-07-09 stsp }
435 63581804 2018-07-09 stsp
436 63581804 2018-07-09 stsp const struct got_error *
437 63581804 2018-07-09 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
438 63581804 2018-07-09 stsp {
439 63581804 2018-07-09 stsp const struct got_error *err = NULL;
440 63581804 2018-07-09 stsp size_t avail;
441 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
442 63581804 2018-07-09 stsp
443 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
444 63581804 2018-07-09 stsp if (err)
445 63581804 2018-07-09 stsp goto done;
446 63581804 2018-07-09 stsp
447 63581804 2018-07-09 stsp *outlen = 0;
448 63581804 2018-07-09 stsp
449 63581804 2018-07-09 stsp do {
450 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
451 63581804 2018-07-09 stsp if (err)
452 5aef3967 2018-07-22 stsp goto done;
453 63581804 2018-07-09 stsp if (avail > 0) {
454 63581804 2018-07-09 stsp ssize_t n;
455 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
456 63581804 2018-07-09 stsp if (n != avail) {
457 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
458 63581804 2018-07-09 stsp goto done;
459 63581804 2018-07-09 stsp }
460 63581804 2018-07-09 stsp *outlen += avail;
461 63581804 2018-07-09 stsp }
462 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
463 63581804 2018-07-09 stsp
464 63581804 2018-07-09 stsp done:
465 63581804 2018-07-09 stsp if (err == NULL) {
466 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
467 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
468 63581804 2018-07-09 stsp }
469 63581804 2018-07-09 stsp got_inflate_end(&zb);
470 63581804 2018-07-09 stsp return err;
471 63581804 2018-07-09 stsp }
472 63581804 2018-07-09 stsp
473 63581804 2018-07-09 stsp const struct got_error *
474 63581804 2018-07-09 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
475 63581804 2018-07-09 stsp {
476 63581804 2018-07-09 stsp const struct got_error *err;
477 63581804 2018-07-09 stsp size_t avail;
478 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
479 63581804 2018-07-09 stsp
480 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, NULL);
481 63581804 2018-07-09 stsp if (err)
482 63581804 2018-07-09 stsp goto done;
483 63581804 2018-07-09 stsp
484 63581804 2018-07-09 stsp *outlen = 0;
485 63581804 2018-07-09 stsp
486 63581804 2018-07-09 stsp do {
487 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
488 63581804 2018-07-09 stsp if (err)
489 5aef3967 2018-07-22 stsp goto done;
490 63581804 2018-07-09 stsp if (avail > 0) {
491 63581804 2018-07-09 stsp size_t n;
492 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
493 63581804 2018-07-09 stsp if (n != 1) {
494 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
495 63581804 2018-07-09 stsp goto done;
496 63581804 2018-07-09 stsp }
497 63581804 2018-07-09 stsp *outlen += avail;
498 63581804 2018-07-09 stsp }
499 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
500 63581804 2018-07-09 stsp
501 63581804 2018-07-09 stsp done:
502 63581804 2018-07-09 stsp if (err == NULL)
503 63581804 2018-07-09 stsp rewind(outfile);
504 63581804 2018-07-09 stsp got_inflate_end(&zb);
505 63581804 2018-07-09 stsp return err;
506 63581804 2018-07-09 stsp }
507 63581804 2018-07-09 stsp
508 63581804 2018-07-09 stsp const struct got_error *
509 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
510 4788f1ce 2020-03-18 stsp uint32_t *input_crc, int infd, FILE *outfile)
511 63581804 2018-07-09 stsp {
512 63581804 2018-07-09 stsp const struct got_error *err;
513 4788f1ce 2020-03-18 stsp size_t avail, consumed;
514 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
515 63581804 2018-07-09 stsp
516 4788f1ce 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, input_crc);
517 63581804 2018-07-09 stsp if (err)
518 63581804 2018-07-09 stsp goto done;
519 63581804 2018-07-09 stsp
520 63581804 2018-07-09 stsp *outlen = 0;
521 4788f1ce 2020-03-18 stsp if (consumed_total)
522 4788f1ce 2020-03-18 stsp *consumed_total = 0;
523 63581804 2018-07-09 stsp do {
524 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
525 63581804 2018-07-09 stsp if (err)
526 5aef3967 2018-07-22 stsp goto done;
527 63581804 2018-07-09 stsp if (avail > 0) {
528 63581804 2018-07-09 stsp size_t n;
529 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
530 63581804 2018-07-09 stsp if (n != 1) {
531 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
532 63581804 2018-07-09 stsp goto done;
533 63581804 2018-07-09 stsp }
534 63581804 2018-07-09 stsp *outlen += avail;
535 4788f1ce 2020-03-18 stsp if (consumed_total)
536 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
537 63581804 2018-07-09 stsp }
538 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
539 63581804 2018-07-09 stsp
540 63581804 2018-07-09 stsp done:
541 63581804 2018-07-09 stsp if (err == NULL)
542 63581804 2018-07-09 stsp rewind(outfile);
543 63581804 2018-07-09 stsp got_inflate_end(&zb);
544 63581804 2018-07-09 stsp return err;
545 63581804 2018-07-09 stsp }
546 63581804 2018-07-09 stsp
547 63581804 2018-07-09 stsp const struct got_error *
548 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
549 4788f1ce 2020-03-18 stsp uint32_t *input_crc, uint8_t *map, size_t offset, size_t len,
550 4788f1ce 2020-03-18 stsp FILE *outfile)
551 63581804 2018-07-09 stsp {
552 63581804 2018-07-09 stsp const struct got_error *err;
553 4788f1ce 2020-03-18 stsp size_t avail, consumed;
554 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
555 63581804 2018-07-09 stsp
556 4788f1ce 2020-03-18 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, input_crc);
557 63581804 2018-07-09 stsp if (err)
558 63581804 2018-07-09 stsp goto done;
559 63581804 2018-07-09 stsp
560 63581804 2018-07-09 stsp *outlen = 0;
561 4788f1ce 2020-03-18 stsp if (consumed_total)
562 4788f1ce 2020-03-18 stsp *consumed_total = 0;
563 63581804 2018-07-09 stsp do {
564 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
565 63581804 2018-07-09 stsp &consumed);
566 63581804 2018-07-09 stsp if (err)
567 5aef3967 2018-07-22 stsp goto done;
568 63581804 2018-07-09 stsp offset += consumed;
569 4788f1ce 2020-03-18 stsp if (consumed_total)
570 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
571 63581804 2018-07-09 stsp len -= consumed;
572 63581804 2018-07-09 stsp if (avail > 0) {
573 63581804 2018-07-09 stsp size_t n;
574 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
575 63581804 2018-07-09 stsp if (n != 1) {
576 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
577 63581804 2018-07-09 stsp goto done;
578 63581804 2018-07-09 stsp }
579 63581804 2018-07-09 stsp *outlen += avail;
580 63581804 2018-07-09 stsp }
581 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
582 63581804 2018-07-09 stsp
583 63581804 2018-07-09 stsp done:
584 63581804 2018-07-09 stsp if (err == NULL)
585 63581804 2018-07-09 stsp rewind(outfile);
586 63581804 2018-07-09 stsp got_inflate_end(&zb);
587 63581804 2018-07-09 stsp return err;
588 63581804 2018-07-09 stsp }