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