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