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