Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sha1.h>
23 #include <zlib.h>
25 #include "got_error.h"
26 #include "got_object.h"
28 #include "got_lib_path.h"
29 #include "got_lib_zbuf.h"
31 const struct got_error *
32 got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
33 {
34 const struct got_error *err = NULL;
36 memset(zb, 0, sizeof(*zb));
38 zb->z.zalloc = Z_NULL;
39 zb->z.zfree = Z_NULL;
40 if (inflateInit(&zb->z) != Z_OK) {
41 err = got_error(GOT_ERR_IO);
42 goto done;
43 }
45 zb->inlen = zb->outlen = bufsize;
47 zb->inbuf = calloc(1, zb->inlen);
48 if (zb->inbuf == NULL) {
49 err = got_error_from_errno();
50 goto done;
51 }
53 if (outbuf == NULL) {
54 zb->outbuf = calloc(1, zb->outlen);
55 if (zb->outbuf == NULL) {
56 err = got_error_from_errno();
57 goto done;
58 }
59 zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
60 } else
61 zb->outbuf = outbuf;
63 done:
64 if (err)
65 got_inflate_end(zb);
66 return err;
67 }
69 const struct got_error *
70 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
71 {
72 size_t last_total_out = zb->z.total_out;
73 z_stream *z = &zb->z;
74 int ret = Z_ERRNO;
76 z->next_out = zb->outbuf;
77 z->avail_out = zb->outlen;
79 *outlenp = 0;
80 do {
81 if (z->avail_in == 0) {
82 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
83 if (n == 0) {
84 if (ferror(f))
85 return got_ferror(f, GOT_ERR_IO);
86 /* EOF */
87 ret = Z_STREAM_END;
88 break;
89 }
90 z->next_in = zb->inbuf;
91 z->avail_in = n;
92 }
93 ret = inflate(z, Z_SYNC_FLUSH);
94 } while (ret == Z_OK && z->avail_out > 0);
96 if (ret == Z_OK) {
97 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
98 } else {
99 if (ret != Z_STREAM_END)
100 return got_error(GOT_ERR_DECOMPRESSION);
101 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
104 *outlenp = z->total_out - last_total_out;
105 return NULL;
108 const struct got_error *
109 got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
111 size_t last_total_out = zb->z.total_out;
112 z_stream *z = &zb->z;
113 int ret = Z_ERRNO;
115 z->next_out = zb->outbuf;
116 z->avail_out = zb->outlen;
118 *outlenp = 0;
119 do {
120 if (z->avail_in == 0) {
121 ssize_t n = read(fd, zb->inbuf, zb->inlen);
122 if (n < 0)
123 return got_error_from_errno();
124 else if (n == 0) {
125 /* EOF */
126 ret = Z_STREAM_END;
127 break;
129 z->next_in = zb->inbuf;
130 z->avail_in = n;
132 ret = inflate(z, Z_SYNC_FLUSH);
133 } while (ret == Z_OK && z->avail_out > 0);
135 if (ret == Z_OK) {
136 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
137 } else {
138 if (ret != Z_STREAM_END)
139 return got_error(GOT_ERR_DECOMPRESSION);
140 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
143 *outlenp = z->total_out - last_total_out;
144 return NULL;
147 void
148 got_inflate_end(struct got_zstream_buf *zb)
150 free(zb->inbuf);
151 if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
152 free(zb->outbuf);
153 inflateEnd(&zb->z);
156 const struct got_error *
157 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
159 const struct got_error *err;
160 size_t avail;
161 struct got_zstream_buf zb;
162 void *newbuf;
164 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
165 if (*outbuf == NULL)
166 return got_error_from_errno();
167 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
168 if (err)
169 return err;
171 *outlen = 0;
173 do {
174 err = got_inflate_read(&zb, f, &avail);
175 if (err)
176 return err;
177 *outlen += avail;
178 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
179 newbuf = reallocarray(*outbuf, 1,
180 *outlen + GOT_ZSTREAM_BUFSIZE);
181 if (newbuf == NULL) {
182 err = got_error_from_errno();
183 free(*outbuf);
184 *outbuf = NULL;
185 *outlen = 0;
186 goto done;
188 *outbuf = newbuf;
189 zb.outbuf = newbuf + *outlen;
190 zb.outlen = GOT_ZSTREAM_BUFSIZE;
192 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
194 done:
195 got_inflate_end(&zb);
196 return err;
199 const struct got_error *
200 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
202 const struct got_error *err;
203 size_t avail;
204 struct got_zstream_buf zb;
205 void *newbuf;
207 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
208 if (*outbuf == NULL)
209 return got_error_from_errno();
210 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
211 if (err)
212 return err;
214 *outlen = 0;
216 do {
217 err = got_inflate_read_fd(&zb, infd, &avail);
218 if (err)
219 return err;
220 *outlen += avail;
221 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
222 newbuf = reallocarray(*outbuf, 1,
223 *outlen + GOT_ZSTREAM_BUFSIZE);
224 if (newbuf == NULL) {
225 err = got_error_from_errno();
226 free(*outbuf);
227 *outbuf = NULL;
228 *outlen = 0;
229 goto done;
231 *outbuf = newbuf;
232 zb.outbuf = newbuf + *outlen;
233 zb.outlen = GOT_ZSTREAM_BUFSIZE;
235 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
237 done:
238 got_inflate_end(&zb);
239 return err;
242 const struct got_error *
243 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
245 const struct got_error *err = NULL;
246 size_t avail;
247 struct got_zstream_buf zb;
249 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
250 if (err)
251 goto done;
253 *outlen = 0;
255 do {
256 err = got_inflate_read(&zb, infile, &avail);
257 if (err)
258 return err;
259 if (avail > 0) {
260 ssize_t n;
261 n = write(outfd, zb.outbuf, avail);
262 if (n != avail) {
263 err = got_error_from_errno();
264 goto done;
266 *outlen += avail;
268 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
270 done:
271 if (err == NULL) {
272 if (lseek(outfd, SEEK_SET, 0) == -1)
273 err = got_error_from_errno();
275 got_inflate_end(&zb);
276 return err;
279 const struct got_error *
280 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
282 const struct got_error *err;
283 size_t avail;
284 struct got_zstream_buf zb;
286 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
287 if (err)
288 goto done;
290 *outlen = 0;
292 do {
293 err = got_inflate_read(&zb, infile, &avail);
294 if (err)
295 return err;
296 if (avail > 0) {
297 size_t n;
298 n = fwrite(zb.outbuf, avail, 1, outfile);
299 if (n != 1) {
300 err = got_ferror(outfile, GOT_ERR_IO);
301 goto done;
303 *outlen += avail;
305 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
307 done:
308 if (err == NULL)
309 rewind(outfile);
310 got_inflate_end(&zb);
311 return err;
314 const struct got_error *
315 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
317 const struct got_error *err;
318 size_t avail;
319 struct got_zstream_buf zb;
321 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
322 if (err)
323 goto done;
325 *outlen = 0;
327 do {
328 err = got_inflate_read_fd(&zb, infd, &avail);
329 if (err)
330 return err;
331 if (avail > 0) {
332 size_t n;
333 n = fwrite(zb.outbuf, avail, 1, outfile);
334 if (n != 1) {
335 err = got_ferror(outfile, GOT_ERR_IO);
336 goto done;
338 *outlen += avail;
340 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
342 done:
343 if (err == NULL)
344 rewind(outfile);
345 got_inflate_end(&zb);
346 return err;