Blame


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