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