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