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