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