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 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 23bc48a9 2019-03-19 stsp got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize)
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 63581804 2018-07-09 stsp done:
78 63581804 2018-07-09 stsp if (err)
79 63581804 2018-07-09 stsp got_inflate_end(zb);
80 63581804 2018-07-09 stsp return err;
81 63581804 2018-07-09 stsp }
82 63581804 2018-07-09 stsp
83 63581804 2018-07-09 stsp const struct got_error *
84 6fb3a497 2020-03-18 stsp got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
85 6fb3a497 2020-03-18 stsp size_t *consumed)
86 63581804 2018-07-09 stsp {
87 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
88 6fb3a497 2020-03-18 stsp size_t last_total_in = zb->z.total_in;
89 63581804 2018-07-09 stsp z_stream *z = &zb->z;
90 63581804 2018-07-09 stsp int ret = Z_ERRNO;
91 63581804 2018-07-09 stsp
92 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
93 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
94 63581804 2018-07-09 stsp
95 63581804 2018-07-09 stsp *outlenp = 0;
96 6fb3a497 2020-03-18 stsp if (consumed)
97 6fb3a497 2020-03-18 stsp *consumed = 0;
98 63581804 2018-07-09 stsp do {
99 63581804 2018-07-09 stsp if (z->avail_in == 0) {
100 63581804 2018-07-09 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
101 63581804 2018-07-09 stsp if (n == 0) {
102 63581804 2018-07-09 stsp if (ferror(f))
103 63581804 2018-07-09 stsp return got_ferror(f, GOT_ERR_IO);
104 63581804 2018-07-09 stsp /* EOF */
105 63581804 2018-07-09 stsp ret = Z_STREAM_END;
106 63581804 2018-07-09 stsp break;
107 63581804 2018-07-09 stsp }
108 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
109 63581804 2018-07-09 stsp z->avail_in = n;
110 63581804 2018-07-09 stsp }
111 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
112 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
113 63581804 2018-07-09 stsp
114 8baa7d26 2020-03-17 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
115 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
116 63581804 2018-07-09 stsp } else {
117 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
118 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
119 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
120 63581804 2018-07-09 stsp }
121 63581804 2018-07-09 stsp
122 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
123 6fb3a497 2020-03-18 stsp if (consumed)
124 6fb3a497 2020-03-18 stsp *consumed += z->total_in - last_total_in;
125 63581804 2018-07-09 stsp return NULL;
126 63581804 2018-07-09 stsp }
127 63581804 2018-07-09 stsp
128 63581804 2018-07-09 stsp const struct got_error *
129 23bc48a9 2019-03-19 stsp got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp)
130 63581804 2018-07-09 stsp {
131 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
132 63581804 2018-07-09 stsp z_stream *z = &zb->z;
133 63581804 2018-07-09 stsp int ret = Z_ERRNO;
134 63581804 2018-07-09 stsp
135 63581804 2018-07-09 stsp z->next_out = zb->outbuf;
136 63581804 2018-07-09 stsp z->avail_out = zb->outlen;
137 63581804 2018-07-09 stsp
138 63581804 2018-07-09 stsp *outlenp = 0;
139 63581804 2018-07-09 stsp do {
140 63581804 2018-07-09 stsp if (z->avail_in == 0) {
141 63581804 2018-07-09 stsp ssize_t n = read(fd, zb->inbuf, zb->inlen);
142 63581804 2018-07-09 stsp if (n < 0)
143 638f9024 2019-05-13 stsp return got_error_from_errno("read");
144 63581804 2018-07-09 stsp else if (n == 0) {
145 63581804 2018-07-09 stsp /* EOF */
146 63581804 2018-07-09 stsp ret = Z_STREAM_END;
147 63581804 2018-07-09 stsp break;
148 63581804 2018-07-09 stsp }
149 63581804 2018-07-09 stsp z->next_in = zb->inbuf;
150 63581804 2018-07-09 stsp z->avail_in = n;
151 63581804 2018-07-09 stsp }
152 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
153 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
154 63581804 2018-07-09 stsp
155 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
156 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
157 63581804 2018-07-09 stsp } else {
158 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
159 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
160 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
161 63581804 2018-07-09 stsp }
162 63581804 2018-07-09 stsp
163 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
164 63581804 2018-07-09 stsp return NULL;
165 63581804 2018-07-09 stsp }
166 63581804 2018-07-09 stsp
167 63581804 2018-07-09 stsp const struct got_error *
168 23bc48a9 2019-03-19 stsp got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
169 63581804 2018-07-09 stsp size_t len, size_t *outlenp, size_t *consumed)
170 63581804 2018-07-09 stsp {
171 63581804 2018-07-09 stsp size_t last_total_out = zb->z.total_out;
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 63581804 2018-07-09 stsp *consumed = 0;
180 63581804 2018-07-09 stsp
181 63581804 2018-07-09 stsp do {
182 37bd7602 2018-07-23 stsp size_t last_total_in = zb->z.total_in;
183 63581804 2018-07-09 stsp if (z->avail_in == 0) {
184 63581804 2018-07-09 stsp if (len == 0) {
185 63581804 2018-07-09 stsp /* EOF */
186 63581804 2018-07-09 stsp ret = Z_STREAM_END;
187 63581804 2018-07-09 stsp break;
188 63581804 2018-07-09 stsp }
189 37bd7602 2018-07-23 stsp z->next_in = map + offset + *consumed;
190 ee0cb6f2 2020-03-17 stsp z->avail_in = len - *consumed;
191 63581804 2018-07-09 stsp }
192 63581804 2018-07-09 stsp ret = inflate(z, Z_SYNC_FLUSH);
193 37bd7602 2018-07-23 stsp *consumed += z->total_in - last_total_in;
194 63581804 2018-07-09 stsp } while (ret == Z_OK && z->avail_out > 0);
195 63581804 2018-07-09 stsp
196 686d24ff 2020-03-15 stsp if (ret == Z_OK || ret == Z_BUF_ERROR) {
197 23bc48a9 2019-03-19 stsp zb->flags |= GOT_INFLATE_F_HAVE_MORE;
198 63581804 2018-07-09 stsp } else {
199 63581804 2018-07-09 stsp if (ret != Z_STREAM_END)
200 63581804 2018-07-09 stsp return got_error(GOT_ERR_DECOMPRESSION);
201 23bc48a9 2019-03-19 stsp zb->flags &= ~GOT_INFLATE_F_HAVE_MORE;
202 63581804 2018-07-09 stsp }
203 63581804 2018-07-09 stsp
204 63581804 2018-07-09 stsp *outlenp = z->total_out - last_total_out;
205 63581804 2018-07-09 stsp return NULL;
206 63581804 2018-07-09 stsp }
207 63581804 2018-07-09 stsp
208 63581804 2018-07-09 stsp void
209 23bc48a9 2019-03-19 stsp got_inflate_end(struct got_inflate_buf *zb)
210 63581804 2018-07-09 stsp {
211 63581804 2018-07-09 stsp free(zb->inbuf);
212 23bc48a9 2019-03-19 stsp if (zb->flags & GOT_INFLATE_F_OWN_OUTBUF)
213 63581804 2018-07-09 stsp free(zb->outbuf);
214 63581804 2018-07-09 stsp inflateEnd(&zb->z);
215 63581804 2018-07-09 stsp }
216 63581804 2018-07-09 stsp
217 63581804 2018-07-09 stsp const struct got_error *
218 6fb3a497 2020-03-18 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen,
219 6fb3a497 2020-03-18 stsp size_t *consumed_total, FILE *f)
220 63581804 2018-07-09 stsp {
221 63581804 2018-07-09 stsp const struct got_error *err;
222 6fb3a497 2020-03-18 stsp size_t avail, consumed;
223 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
224 63581804 2018-07-09 stsp void *newbuf;
225 17d745b8 2018-07-23 stsp int nbuf = 1;
226 63581804 2018-07-09 stsp
227 6dc3b75a 2019-05-22 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
228 63581804 2018-07-09 stsp if (*outbuf == NULL)
229 6331840f 2019-05-22 stsp return got_error_from_errno("malloc");
230 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
231 63581804 2018-07-09 stsp if (err)
232 63581804 2018-07-09 stsp return err;
233 63581804 2018-07-09 stsp
234 63581804 2018-07-09 stsp *outlen = 0;
235 6fb3a497 2020-03-18 stsp if (consumed_total)
236 6fb3a497 2020-03-18 stsp *consumed_total = 0;
237 63581804 2018-07-09 stsp
238 63581804 2018-07-09 stsp do {
239 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, f, &avail, &consumed);
240 63581804 2018-07-09 stsp if (err)
241 5aef3967 2018-07-22 stsp goto done;
242 63581804 2018-07-09 stsp *outlen += avail;
243 6fb3a497 2020-03-18 stsp if (consumed_total)
244 6fb3a497 2020-03-18 stsp *consumed_total += consumed;
245 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
246 6dc3b75a 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
247 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
248 63581804 2018-07-09 stsp if (newbuf == NULL) {
249 6dc3b75a 2019-05-22 stsp err = got_error_from_errno("reallocarray");
250 63581804 2018-07-09 stsp free(*outbuf);
251 63581804 2018-07-09 stsp *outbuf = NULL;
252 63581804 2018-07-09 stsp *outlen = 0;
253 63581804 2018-07-09 stsp goto done;
254 63581804 2018-07-09 stsp }
255 63581804 2018-07-09 stsp *outbuf = newbuf;
256 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
257 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
258 63581804 2018-07-09 stsp }
259 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
260 63581804 2018-07-09 stsp
261 63581804 2018-07-09 stsp done:
262 63581804 2018-07-09 stsp got_inflate_end(&zb);
263 63581804 2018-07-09 stsp return err;
264 63581804 2018-07-09 stsp }
265 63581804 2018-07-09 stsp
266 63581804 2018-07-09 stsp const struct got_error *
267 63581804 2018-07-09 stsp got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
268 63581804 2018-07-09 stsp {
269 63581804 2018-07-09 stsp const struct got_error *err;
270 63581804 2018-07-09 stsp size_t avail;
271 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
272 63581804 2018-07-09 stsp void *newbuf;
273 17d745b8 2018-07-23 stsp int nbuf = 1;
274 63581804 2018-07-09 stsp
275 f2c5fe0e 2019-05-22 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
276 63581804 2018-07-09 stsp if (*outbuf == NULL)
277 6331840f 2019-05-22 stsp return got_error_from_errno("malloc");
278 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
279 63581804 2018-07-09 stsp if (err)
280 5aef3967 2018-07-22 stsp goto done;
281 63581804 2018-07-09 stsp
282 63581804 2018-07-09 stsp *outlen = 0;
283 63581804 2018-07-09 stsp
284 63581804 2018-07-09 stsp do {
285 63581804 2018-07-09 stsp err = got_inflate_read_fd(&zb, infd, &avail);
286 63581804 2018-07-09 stsp if (err)
287 5aef3967 2018-07-22 stsp goto done;
288 63581804 2018-07-09 stsp *outlen += avail;
289 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
290 f2c5fe0e 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
291 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
292 63581804 2018-07-09 stsp if (newbuf == NULL) {
293 f2c5fe0e 2019-05-22 stsp err = got_error_from_errno("reallocarray");
294 63581804 2018-07-09 stsp free(*outbuf);
295 63581804 2018-07-09 stsp *outbuf = NULL;
296 63581804 2018-07-09 stsp *outlen = 0;
297 63581804 2018-07-09 stsp goto done;
298 63581804 2018-07-09 stsp }
299 63581804 2018-07-09 stsp *outbuf = newbuf;
300 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
301 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
302 63581804 2018-07-09 stsp }
303 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
304 63581804 2018-07-09 stsp
305 63581804 2018-07-09 stsp done:
306 63581804 2018-07-09 stsp got_inflate_end(&zb);
307 63581804 2018-07-09 stsp return err;
308 63581804 2018-07-09 stsp }
309 63581804 2018-07-09 stsp
310 63581804 2018-07-09 stsp const struct got_error *
311 63581804 2018-07-09 stsp got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
312 63581804 2018-07-09 stsp size_t offset, size_t len)
313 63581804 2018-07-09 stsp {
314 63581804 2018-07-09 stsp const struct got_error *err;
315 37bd7602 2018-07-23 stsp size_t avail, consumed;
316 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
317 63581804 2018-07-09 stsp void *newbuf;
318 37bd7602 2018-07-23 stsp int nbuf = 1;
319 63581804 2018-07-09 stsp
320 b3a605ce 2019-05-22 stsp *outbuf = malloc(GOT_INFLATE_BUFSIZE);
321 63581804 2018-07-09 stsp if (*outbuf == NULL)
322 6331840f 2019-05-22 stsp return got_error_from_errno("malloc");
323 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE);
324 60507209 2018-07-13 stsp if (err) {
325 60507209 2018-07-13 stsp free(*outbuf);
326 60507209 2018-07-13 stsp *outbuf = NULL;
327 63581804 2018-07-09 stsp return err;
328 60507209 2018-07-13 stsp }
329 63581804 2018-07-09 stsp
330 63581804 2018-07-09 stsp *outlen = 0;
331 63581804 2018-07-09 stsp
332 63581804 2018-07-09 stsp do {
333 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
334 63581804 2018-07-09 stsp &consumed);
335 63581804 2018-07-09 stsp if (err)
336 3efa19e7 2018-07-13 stsp goto done;
337 63581804 2018-07-09 stsp offset += consumed;
338 63581804 2018-07-09 stsp len -= consumed;
339 63581804 2018-07-09 stsp *outlen += avail;
340 63581804 2018-07-09 stsp if (len == 0)
341 63581804 2018-07-09 stsp break;
342 23bc48a9 2019-03-19 stsp if (zb.flags & GOT_INFLATE_F_HAVE_MORE) {
343 b3a605ce 2019-05-22 stsp newbuf = reallocarray(*outbuf, ++nbuf,
344 23bc48a9 2019-03-19 stsp GOT_INFLATE_BUFSIZE);
345 63581804 2018-07-09 stsp if (newbuf == NULL) {
346 b3a605ce 2019-05-22 stsp err = got_error_from_errno("reallocarray");
347 63581804 2018-07-09 stsp free(*outbuf);
348 63581804 2018-07-09 stsp *outbuf = NULL;
349 63581804 2018-07-09 stsp *outlen = 0;
350 63581804 2018-07-09 stsp goto done;
351 63581804 2018-07-09 stsp }
352 63581804 2018-07-09 stsp *outbuf = newbuf;
353 63581804 2018-07-09 stsp zb.outbuf = newbuf + *outlen;
354 23bc48a9 2019-03-19 stsp zb.outlen = (nbuf * GOT_INFLATE_BUFSIZE) - *outlen;
355 63581804 2018-07-09 stsp }
356 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
357 63581804 2018-07-09 stsp done:
358 63581804 2018-07-09 stsp got_inflate_end(&zb);
359 63581804 2018-07-09 stsp return err;
360 63581804 2018-07-09 stsp }
361 63581804 2018-07-09 stsp
362 63581804 2018-07-09 stsp const struct got_error *
363 63581804 2018-07-09 stsp got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
364 63581804 2018-07-09 stsp {
365 63581804 2018-07-09 stsp const struct got_error *err = NULL;
366 63581804 2018-07-09 stsp size_t avail;
367 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
368 63581804 2018-07-09 stsp
369 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
370 63581804 2018-07-09 stsp if (err)
371 63581804 2018-07-09 stsp goto done;
372 63581804 2018-07-09 stsp
373 63581804 2018-07-09 stsp *outlen = 0;
374 63581804 2018-07-09 stsp
375 63581804 2018-07-09 stsp do {
376 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
377 63581804 2018-07-09 stsp if (err)
378 5aef3967 2018-07-22 stsp goto done;
379 63581804 2018-07-09 stsp if (avail > 0) {
380 63581804 2018-07-09 stsp ssize_t n;
381 63581804 2018-07-09 stsp n = write(outfd, zb.outbuf, avail);
382 63581804 2018-07-09 stsp if (n != avail) {
383 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
384 63581804 2018-07-09 stsp goto done;
385 63581804 2018-07-09 stsp }
386 63581804 2018-07-09 stsp *outlen += avail;
387 63581804 2018-07-09 stsp }
388 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
389 63581804 2018-07-09 stsp
390 63581804 2018-07-09 stsp done:
391 63581804 2018-07-09 stsp if (err == NULL) {
392 63581804 2018-07-09 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
393 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
394 63581804 2018-07-09 stsp }
395 63581804 2018-07-09 stsp got_inflate_end(&zb);
396 63581804 2018-07-09 stsp return err;
397 63581804 2018-07-09 stsp }
398 63581804 2018-07-09 stsp
399 63581804 2018-07-09 stsp const struct got_error *
400 63581804 2018-07-09 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
401 63581804 2018-07-09 stsp {
402 63581804 2018-07-09 stsp const struct got_error *err;
403 63581804 2018-07-09 stsp size_t avail;
404 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
405 63581804 2018-07-09 stsp
406 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
407 63581804 2018-07-09 stsp if (err)
408 63581804 2018-07-09 stsp goto done;
409 63581804 2018-07-09 stsp
410 63581804 2018-07-09 stsp *outlen = 0;
411 63581804 2018-07-09 stsp
412 63581804 2018-07-09 stsp do {
413 6fb3a497 2020-03-18 stsp err = got_inflate_read(&zb, infile, &avail, NULL);
414 63581804 2018-07-09 stsp if (err)
415 5aef3967 2018-07-22 stsp goto done;
416 63581804 2018-07-09 stsp if (avail > 0) {
417 63581804 2018-07-09 stsp size_t n;
418 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
419 63581804 2018-07-09 stsp if (n != 1) {
420 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
421 63581804 2018-07-09 stsp goto done;
422 63581804 2018-07-09 stsp }
423 63581804 2018-07-09 stsp *outlen += avail;
424 63581804 2018-07-09 stsp }
425 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
426 63581804 2018-07-09 stsp
427 63581804 2018-07-09 stsp done:
428 63581804 2018-07-09 stsp if (err == NULL)
429 63581804 2018-07-09 stsp rewind(outfile);
430 63581804 2018-07-09 stsp got_inflate_end(&zb);
431 63581804 2018-07-09 stsp return err;
432 63581804 2018-07-09 stsp }
433 63581804 2018-07-09 stsp
434 63581804 2018-07-09 stsp const struct got_error *
435 63581804 2018-07-09 stsp got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
436 63581804 2018-07-09 stsp {
437 63581804 2018-07-09 stsp const struct got_error *err;
438 63581804 2018-07-09 stsp size_t avail;
439 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
440 63581804 2018-07-09 stsp
441 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
442 63581804 2018-07-09 stsp if (err)
443 63581804 2018-07-09 stsp goto done;
444 63581804 2018-07-09 stsp
445 63581804 2018-07-09 stsp *outlen = 0;
446 63581804 2018-07-09 stsp
447 63581804 2018-07-09 stsp do {
448 63581804 2018-07-09 stsp err = got_inflate_read_fd(&zb, infd, &avail);
449 63581804 2018-07-09 stsp if (err)
450 5aef3967 2018-07-22 stsp goto done;
451 63581804 2018-07-09 stsp if (avail > 0) {
452 63581804 2018-07-09 stsp size_t n;
453 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
454 63581804 2018-07-09 stsp if (n != 1) {
455 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
456 63581804 2018-07-09 stsp goto done;
457 63581804 2018-07-09 stsp }
458 63581804 2018-07-09 stsp *outlen += avail;
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
462 63581804 2018-07-09 stsp done:
463 63581804 2018-07-09 stsp if (err == NULL)
464 63581804 2018-07-09 stsp rewind(outfile);
465 63581804 2018-07-09 stsp got_inflate_end(&zb);
466 63581804 2018-07-09 stsp return err;
467 63581804 2018-07-09 stsp }
468 63581804 2018-07-09 stsp
469 63581804 2018-07-09 stsp const struct got_error *
470 63581804 2018-07-09 stsp got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
471 63581804 2018-07-09 stsp size_t len, FILE *outfile)
472 63581804 2018-07-09 stsp {
473 63581804 2018-07-09 stsp const struct got_error *err;
474 63581804 2018-07-09 stsp size_t avail;
475 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
476 63581804 2018-07-09 stsp size_t consumed;
477 63581804 2018-07-09 stsp
478 23bc48a9 2019-03-19 stsp err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE);
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 63581804 2018-07-09 stsp err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
486 63581804 2018-07-09 stsp &consumed);
487 63581804 2018-07-09 stsp if (err)
488 5aef3967 2018-07-22 stsp goto done;
489 63581804 2018-07-09 stsp offset += consumed;
490 63581804 2018-07-09 stsp len -= consumed;
491 63581804 2018-07-09 stsp if (avail > 0) {
492 63581804 2018-07-09 stsp size_t n;
493 63581804 2018-07-09 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
494 63581804 2018-07-09 stsp if (n != 1) {
495 63581804 2018-07-09 stsp err = got_ferror(outfile, GOT_ERR_IO);
496 63581804 2018-07-09 stsp goto done;
497 63581804 2018-07-09 stsp }
498 63581804 2018-07-09 stsp *outlen += avail;
499 63581804 2018-07-09 stsp }
500 23bc48a9 2019-03-19 stsp } while (zb.flags & GOT_INFLATE_F_HAVE_MORE);
501 63581804 2018-07-09 stsp
502 63581804 2018-07-09 stsp done:
503 63581804 2018-07-09 stsp if (err == NULL)
504 63581804 2018-07-09 stsp rewind(outfile);
505 63581804 2018-07-09 stsp got_inflate_end(&zb);
506 63581804 2018-07-09 stsp return err;
507 63581804 2018-07-09 stsp }