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