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