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 63581804 2018-07-09 stsp #include <sha1.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 6ad68bce 2020-03-24 stsp csum_input(struct got_inflate_checksum *csum, const char *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 d5c81d44 2021-07-08 stsp csum_output(struct got_inflate_checksum *csum, const char *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 d5c81d44 2021-07-08 stsp char *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 d5c81d44 2021-07-08 stsp char *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 d5c81d44 2021-07-08 stsp char *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 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
252 63581804 2018-07-09 stsp }
253 6ad68bce 2020-03-24 stsp if (zb->csum) {
254 6ad68bce 2020-03-24 stsp csum_in = z->next_in;
255 d5c81d44 2021-07-08 stsp csum_avail_in = z->avail_in;
256 d5c81d44 2021-07-08 stsp csum_out = z->next_out;
257 d5c81d44 2021-07-08 stsp csum_avail_out = z->avail_out;
258 1e87a3c3 2020-03-18 stsp }
259 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
260 d5c81d44 2021-07-08 stsp if (zb->csum) {
261 d5c81d44 2021-07-08 stsp csum_input(zb->csum, csum_in,
262 d5c81d44 2021-07-08 stsp csum_avail_in - z->avail_in);
263 d5c81d44 2021-07-08 stsp csum_output(zb->csum, csum_out,
264 d5c81d44 2021-07-08 stsp csum_avail_out - z->avail_out);
265 d5c81d44 2021-07-08 stsp }
266 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
267 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
268 63581804 2018-07-09 stsp
269 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
270 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
271 63581804 2018-07-09 stsp } else {
272 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
273 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
274 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
275 63581804 2018-07-09 stsp }
276 63581804 2018-07-09 stsp
277 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
278 63581804 2018-07-09 stsp return NULL;
279 63581804 2018-07-09 stsp }
280 63581804 2018-07-09 stsp
281 63581804 2018-07-09 stsp void
282 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
283 63581804 2018-07-09 stsp {
284 63581804 2018-07-09 stsp free(zb->inbuf);
285 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
286 63581804 2018-07-09 stsp free(zb->outbuf);
287 63581804 2018-07-09 stsp inflateEnd(&zb->z);
288 63581804 2018-07-09 stsp }
289 63581804 2018-07-09 stsp
290 63581804 2018-07-09 stsp const struct got_error *
291 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
292 12f2167a 2021-07-04 stsp size_t *consumed_total, struct got_inflate_checksum *csum, FILE *f)
293 63581804 2018-07-09 stsp {
294 63581804 2018-07-09 stsp const struct got_error *err;
295 6fb3a497 2020-03-18 stsp size_t avail, consumed;
296 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
297 63581804 2018-07-09 stsp void *newbuf;
298 17d745b8 2018-07-23 stsp int nbuf = 1;
299 63581804 2018-07-09 stsp
300 2decf4c6 2020-03-18 stsp if (outbuf) {
301 2decf4c6 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
302 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
303 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
304 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
305 2decf4c6 2020-03-18 stsp } else
306 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
307 63581804 2018-07-09 stsp if (err)
308 63581804 2018-07-09 stsp return err;
309 63581804 2018-07-09 stsp
310 63581804 2018-07-09 stsp *outlen = 0;
311 6fb3a497 2020-03-18 stsp if (consumed_total)
312 6fb3a497 2020-03-18 stsp *consumed_total = 0;
313 63581804 2018-07-09 stsp
314 63581804 2018-07-09 stsp do {
315 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
316 63581804 2018-07-09 stsp if (err)
317 5aef3967 2018-07-22 stsp goto done;
318 63581804 2018-07-09 stsp *outlen += avail;
319 6fb3a497 2020-03-18 stsp if (consumed_total)
320 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
321 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
322 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
323 2decf4c6 2020-03-18 stsp continue;
324 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
325 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
326 62d463ca 2020-10-20 naddy GOT_INFLATE_BUFSIZE);
327 63581804 2018-07-09 stsp if (newbuf == NULL) {
328 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
329 63581804 2018-07-09 stsp free(*outbuf);
330 63581804 2018-07-09 stsp *outbuf = NULL;
331 63581804 2018-07-09 stsp *outlen = 0;
332 63581804 2018-07-09 stsp goto done;
333 63581804 2018-07-09 stsp }
334 63581804 2018-07-09 stsp *outbuf = newbuf;
335 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
336 63581804 2018-07-09 stsp }
337 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
338 63581804 2018-07-09 stsp
339 63581804 2018-07-09 stsp done:
340 63581804 2018-07-09 stsp got_inflate_end(&zb);
341 63581804 2018-07-09 stsp return err;
342 63581804 2018-07-09 stsp }
343 63581804 2018-07-09 stsp
344 63581804 2018-07-09 stsp const struct got_error *
345 3ab5e33c 2020-03-18 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
346 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum,
347 6ad68bce 2020-03-24 stsp size_t expected_size, int infd)
348 63581804 2018-07-09 stsp {
349 63581804 2018-07-09 stsp const struct got_error *err;
350 3ab5e33c 2020-03-18 stsp size_t avail, consumed;
351 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
352 63581804 2018-07-09 stsp void *newbuf;
353 17d745b8 2018-07-23 stsp int nbuf = 1;
354 55fdd257 2020-03-18 stsp size_t bufsize = GOT_INFLATE_BUFSIZE;
355 63581804 2018-07-09 stsp
356 55fdd257 2020-03-18 stsp /* Optimize buffer size in case short reads should suffice. */
357 55fdd257 2020-03-18 stsp if (expected_size > 0 && expected_size < bufsize)
358 55fdd257 2020-03-18 stsp bufsize = expected_size;
359 3168e5da 2020-09-10 stsp
360 2decf4c6 2020-03-18 stsp if (outbuf) {
361 55fdd257 2020-03-18 stsp *outbuf = malloc(bufsize);
362 2decf4c6 2020-03-18 stsp if (*outbuf == NULL)
363 2decf4c6 2020-03-18 stsp return got_error_from_errno("malloc");
364 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
365 2decf4c6 2020-03-18 stsp } else
366 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, bufsize, csum);
367 63581804 2018-07-09 stsp if (err)
368 5aef3967 2018-07-22 stsp goto done;
369 63581804 2018-07-09 stsp
370 63581804 2018-07-09 stsp *outlen = 0;
371 3ab5e33c 2020-03-18 stsp if (consumed_total)
372 3ab5e33c 2020-03-18 stsp *consumed_total = 0;
373 63581804 2018-07-09 stsp
374 63581804 2018-07-09 stsp do {
375 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
376 63581804 2018-07-09 stsp if (err)
377 5aef3967 2018-07-22 stsp goto done;
378 63581804 2018-07-09 stsp *outlen += avail;
379 3ab5e33c 2020-03-18 stsp if (consumed_total)
380 3ab5e33c 2020-03-18 stsp *consumed_total += consumed;
381 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
382 2decf4c6 2020-03-18 stsp if (outbuf == NULL)
383 2decf4c6 2020-03-18 stsp continue;
384 5eddcd60 2020-03-18 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
385 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
386 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
387 63581804 2018-07-09 stsp if (newbuf == NULL) {
388 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
389 63581804 2018-07-09 stsp free(*outbuf);
390 63581804 2018-07-09 stsp *outbuf = NULL;
391 63581804 2018-07-09 stsp *outlen = 0;
392 63581804 2018-07-09 stsp goto done;
393 63581804 2018-07-09 stsp }
394 63581804 2018-07-09 stsp *outbuf = newbuf;
395 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
396 63581804 2018-07-09 stsp }
397 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
398 63581804 2018-07-09 stsp
399 63581804 2018-07-09 stsp done:
400 63581804 2018-07-09 stsp got_inflate_end(&zb);
401 63581804 2018-07-09 stsp return err;
402 63581804 2018-07-09 stsp }
403 63581804 2018-07-09 stsp
404 63581804 2018-07-09 stsp const struct got_error *
405 2e5a6fad 2020-03-18 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
406 6ad68bce 2020-03-24 stsp size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
407 6ad68bce 2020-03-24 stsp size_t offset, size_t len)
408 63581804 2018-07-09 stsp {
409 63581804 2018-07-09 stsp const struct got_error *err;
410 37bd7602 2018-07-23 stsp size_t avail, consumed;
411 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
412 63581804 2018-07-09 stsp void *newbuf;
413 37bd7602 2018-07-23 stsp int nbuf = 1;
414 63581804 2018-07-09 stsp
415 2e5a6fad 2020-03-18 stsp if (outbuf) {
416 2e5a6fad 2020-03-18 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
417 2e5a6fad 2020-03-18 stsp if (*outbuf == NULL)
418 2e5a6fad 2020-03-18 stsp return got_error_from_errno("malloc");
419 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
420 2e5a6fad 2020-03-18 stsp if (err) {
421 2e5a6fad 2020-03-18 stsp free(*outbuf);
422 2e5a6fad 2020-03-18 stsp *outbuf = NULL;
423 2e5a6fad 2020-03-18 stsp return err;
424 2e5a6fad 2020-03-18 stsp }
425 2e5a6fad 2020-03-18 stsp } else {
426 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
427 60507209 2018-07-13 stsp }
428 63581804 2018-07-09 stsp
429 63581804 2018-07-09 stsp *outlen = 0;
430 2e5a6fad 2020-03-18 stsp if (consumed_total)
431 2e5a6fad 2020-03-18 stsp *consumed_total = 0;
432 63581804 2018-07-09 stsp do {
433 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
434 63581804 2018-07-09 stsp &consumed);
435 63581804 2018-07-09 stsp if (err)
436 3efa19e7 2018-07-13 stsp goto done;
437 63581804 2018-07-09 stsp offset += consumed;
438 2e5a6fad 2020-03-18 stsp if (consumed_total)
439 2e5a6fad 2020-03-18 stsp *consumed_total += consumed;
440 63581804 2018-07-09 stsp len -= consumed;
441 63581804 2018-07-09 stsp *outlen += avail;
442 63581804 2018-07-09 stsp if (len == 0)
443 63581804 2018-07-09 stsp break;
444 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
445 2e5a6fad 2020-03-18 stsp if (outbuf == NULL)
446 2e5a6fad 2020-03-18 stsp continue;
447 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
448 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
449 63581804 2018-07-09 stsp if (newbuf == NULL) {
450 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
451 63581804 2018-07-09 stsp free(*outbuf);
452 63581804 2018-07-09 stsp *outbuf = NULL;
453 63581804 2018-07-09 stsp *outlen = 0;
454 63581804 2018-07-09 stsp goto done;
455 63581804 2018-07-09 stsp }
456 63581804 2018-07-09 stsp *outbuf = newbuf;
457 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
458 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
459 63581804 2018-07-09 stsp }
460 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
461 63581804 2018-07-09 stsp done:
462 63581804 2018-07-09 stsp got_inflate_end(&zb);
463 63581804 2018-07-09 stsp return err;
464 63581804 2018-07-09 stsp }
465 63581804 2018-07-09 stsp
466 63581804 2018-07-09 stsp const struct got_error *
467 12f2167a 2021-07-04 stsp got_inflate_to_fd(size_t *outlen, FILE *infile,
468 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, int outfd)
469 63581804 2018-07-09 stsp {
470 63581804 2018-07-09 stsp const struct got_error *err = NULL;
471 63581804 2018-07-09 stsp size_t avail;
472 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
473 63581804 2018-07-09 stsp
474 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
475 63581804 2018-07-09 stsp if (err)
476 63581804 2018-07-09 stsp goto done;
477 63581804 2018-07-09 stsp
478 63581804 2018-07-09 stsp *outlen = 0;
479 63581804 2018-07-09 stsp
480 63581804 2018-07-09 stsp do {
481 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
482 63581804 2018-07-09 stsp if (err)
483 5aef3967 2018-07-22 stsp goto done;
484 63581804 2018-07-09 stsp if (avail > 0) {
485 63581804 2018-07-09 stsp ssize_t n;
486 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
487 63581804 2018-07-09 stsp if (n != avail) {
488 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
489 63581804 2018-07-09 stsp goto done;
490 63581804 2018-07-09 stsp }
491 63581804 2018-07-09 stsp *outlen += avail;
492 63581804 2018-07-09 stsp }
493 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
494 63581804 2018-07-09 stsp
495 63581804 2018-07-09 stsp done:
496 63581804 2018-07-09 stsp if (err == NULL) {
497 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
498 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
499 63581804 2018-07-09 stsp }
500 63581804 2018-07-09 stsp got_inflate_end(&zb);
501 63581804 2018-07-09 stsp return err;
502 63581804 2018-07-09 stsp }
503 63581804 2018-07-09 stsp
504 63581804 2018-07-09 stsp const struct got_error *
505 12f2167a 2021-07-04 stsp got_inflate_to_file(size_t *outlen, FILE *infile,
506 12f2167a 2021-07-04 stsp struct got_inflate_checksum *csum, FILE *outfile)
507 63581804 2018-07-09 stsp {
508 63581804 2018-07-09 stsp const struct got_error *err;
509 63581804 2018-07-09 stsp size_t avail;
510 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
511 63581804 2018-07-09 stsp
512 12f2167a 2021-07-04 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
513 63581804 2018-07-09 stsp if (err)
514 63581804 2018-07-09 stsp goto done;
515 63581804 2018-07-09 stsp
516 63581804 2018-07-09 stsp *outlen = 0;
517 63581804 2018-07-09 stsp
518 63581804 2018-07-09 stsp do {
519 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
520 63581804 2018-07-09 stsp if (err)
521 5aef3967 2018-07-22 stsp goto done;
522 63581804 2018-07-09 stsp if (avail > 0) {
523 63581804 2018-07-09 stsp size_t n;
524 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
525 63581804 2018-07-09 stsp if (n != 1) {
526 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
527 63581804 2018-07-09 stsp goto done;
528 63581804 2018-07-09 stsp }
529 63581804 2018-07-09 stsp *outlen += avail;
530 63581804 2018-07-09 stsp }
531 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
532 63581804 2018-07-09 stsp
533 63581804 2018-07-09 stsp done:
534 63581804 2018-07-09 stsp if (err == NULL)
535 63581804 2018-07-09 stsp rewind(outfile);
536 63581804 2018-07-09 stsp got_inflate_end(&zb);
537 63581804 2018-07-09 stsp return err;
538 63581804 2018-07-09 stsp }
539 63581804 2018-07-09 stsp
540 63581804 2018-07-09 stsp const struct got_error *
541 4788f1ce 2020-03-18 stsp got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
542 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, int infd, FILE *outfile)
543 63581804 2018-07-09 stsp {
544 63581804 2018-07-09 stsp const struct got_error *err;
545 4788f1ce 2020-03-18 stsp size_t avail, consumed;
546 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
547 63581804 2018-07-09 stsp
548 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
549 63581804 2018-07-09 stsp if (err)
550 63581804 2018-07-09 stsp goto done;
551 63581804 2018-07-09 stsp
552 63581804 2018-07-09 stsp *outlen = 0;
553 4788f1ce 2020-03-18 stsp if (consumed_total)
554 4788f1ce 2020-03-18 stsp *consumed_total = 0;
555 63581804 2018-07-09 stsp do {
556 4788f1ce 2020-03-18 stsp err = got_inflate_read_fd(&zb, infd, &avail, &consumed);
557 63581804 2018-07-09 stsp if (err)
558 5aef3967 2018-07-22 stsp goto done;
559 63581804 2018-07-09 stsp if (avail > 0) {
560 63581804 2018-07-09 stsp size_t n;
561 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
562 63581804 2018-07-09 stsp if (n != 1) {
563 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
564 63581804 2018-07-09 stsp goto done;
565 63581804 2018-07-09 stsp }
566 63581804 2018-07-09 stsp *outlen += avail;
567 4788f1ce 2020-03-18 stsp if (consumed_total)
568 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
569 63581804 2018-07-09 stsp }
570 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
571 63581804 2018-07-09 stsp
572 63581804 2018-07-09 stsp done:
573 63581804 2018-07-09 stsp if (err == NULL)
574 63581804 2018-07-09 stsp rewind(outfile);
575 63581804 2018-07-09 stsp got_inflate_end(&zb);
576 63581804 2018-07-09 stsp return err;
577 63581804 2018-07-09 stsp }
578 63581804 2018-07-09 stsp
579 63581804 2018-07-09 stsp const struct got_error *
580 4788f1ce 2020-03-18 stsp got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
581 6ad68bce 2020-03-24 stsp struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
582 6ad68bce 2020-03-24 stsp size_t len, FILE *outfile)
583 63581804 2018-07-09 stsp {
584 63581804 2018-07-09 stsp const struct got_error *err;
585 4788f1ce 2020-03-18 stsp size_t avail, consumed;
586 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
587 63581804 2018-07-09 stsp
588 6ad68bce 2020-03-24 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
589 63581804 2018-07-09 stsp if (err)
590 63581804 2018-07-09 stsp goto done;
591 63581804 2018-07-09 stsp
592 63581804 2018-07-09 stsp *outlen = 0;
593 4788f1ce 2020-03-18 stsp if (consumed_total)
594 4788f1ce 2020-03-18 stsp *consumed_total = 0;
595 63581804 2018-07-09 stsp do {
596 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
597 63581804 2018-07-09 stsp &consumed);
598 63581804 2018-07-09 stsp if (err)
599 5aef3967 2018-07-22 stsp goto done;
600 63581804 2018-07-09 stsp offset += consumed;
601 4788f1ce 2020-03-18 stsp if (consumed_total)
602 4788f1ce 2020-03-18 stsp *consumed_total += consumed;
603 63581804 2018-07-09 stsp len -= consumed;
604 63581804 2018-07-09 stsp if (avail > 0) {
605 63581804 2018-07-09 stsp size_t n;
606 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
607 63581804 2018-07-09 stsp if (n != 1) {
608 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
609 63581804 2018-07-09 stsp goto done;
610 63581804 2018-07-09 stsp }
611 63581804 2018-07-09 stsp *outlen += avail;
612 63581804 2018-07-09 stsp }
613 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
614 63581804 2018-07-09 stsp
615 63581804 2018-07-09 stsp done:
616 63581804 2018-07-09 stsp if (err == NULL)
617 63581804 2018-07-09 stsp rewind(outfile);
618 63581804 2018-07-09 stsp got_inflate_end(&zb);
619 63581804 2018-07-09 stsp return err;
620 63581804 2018-07-09 stsp }