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