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