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