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>
24 #include <time.h>
26 #include "got_error.h"
27 #include "got_object.h"
29 #include "got_lib_path.h"
30 #include "got_lib_zbuf.h"
32 const struct got_error *
33 got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
34 {
35 const struct got_error *err = NULL;
37 memset(zb, 0, sizeof(*zb));
39 zb->z.zalloc = Z_NULL;
40 zb->z.zfree = Z_NULL;
41 if (inflateInit(&zb->z) != Z_OK) {
42 err = got_error(GOT_ERR_IO);
43 goto done;
44 }
46 zb->inlen = zb->outlen = bufsize;
48 zb->inbuf = calloc(1, zb->inlen);
49 if (zb->inbuf == NULL) {
50 err = got_error_from_errno();
51 goto done;
52 }
54 if (outbuf == NULL) {
55 zb->outbuf = calloc(1, zb->outlen);
56 if (zb->outbuf == NULL) {
57 err = got_error_from_errno();
58 goto done;
59 }
60 zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
61 } else
62 zb->outbuf = outbuf;
64 done:
65 if (err)
66 got_inflate_end(zb);
67 return err;
68 }
70 const struct got_error *
71 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
72 {
73 size_t last_total_out = zb->z.total_out;
74 z_stream *z = &zb->z;
75 int ret = Z_ERRNO;
77 z->next_out = zb->outbuf;
78 z->avail_out = zb->outlen;
80 *outlenp = 0;
81 do {
82 if (z->avail_in == 0) {
83 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
84 if (n == 0) {
85 if (ferror(f))
86 return got_ferror(f, GOT_ERR_IO);
87 /* EOF */
88 ret = Z_STREAM_END;
89 break;
90 }
91 z->next_in = zb->inbuf;
92 z->avail_in = n;
93 }
94 ret = inflate(z, Z_SYNC_FLUSH);
95 } while (ret == Z_OK && z->avail_out > 0);
97 if (ret == Z_OK) {
98 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
99 } else {
100 if (ret != Z_STREAM_END)
101 return got_error(GOT_ERR_DECOMPRESSION);
102 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
105 *outlenp = z->total_out - last_total_out;
106 return NULL;
109 const struct got_error *
110 got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
112 size_t last_total_out = zb->z.total_out;
113 z_stream *z = &zb->z;
114 int ret = Z_ERRNO;
116 z->next_out = zb->outbuf;
117 z->avail_out = zb->outlen;
119 *outlenp = 0;
120 do {
121 if (z->avail_in == 0) {
122 ssize_t n = read(fd, zb->inbuf, zb->inlen);
123 if (n < 0)
124 return got_error_from_errno();
125 else if (n == 0) {
126 /* EOF */
127 ret = Z_STREAM_END;
128 break;
130 z->next_in = zb->inbuf;
131 z->avail_in = n;
133 ret = inflate(z, Z_SYNC_FLUSH);
134 } while (ret == Z_OK && z->avail_out > 0);
136 if (ret == Z_OK) {
137 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
138 } else {
139 if (ret != Z_STREAM_END)
140 return got_error(GOT_ERR_DECOMPRESSION);
141 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
144 *outlenp = z->total_out - last_total_out;
145 return NULL;
148 void
149 got_inflate_end(struct got_zstream_buf *zb)
151 free(zb->inbuf);
152 if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
153 free(zb->outbuf);
154 inflateEnd(&zb->z);
157 const struct got_error *
158 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
160 const struct got_error *err;
161 size_t avail;
162 struct got_zstream_buf zb;
163 void *newbuf;
165 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
166 if (*outbuf == NULL)
167 return got_error_from_errno();
168 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
169 if (err)
170 return err;
172 *outlen = 0;
174 do {
175 err = got_inflate_read(&zb, f, &avail);
176 if (err)
177 return err;
178 *outlen += avail;
179 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
180 newbuf = reallocarray(*outbuf, 1,
181 *outlen + GOT_ZSTREAM_BUFSIZE);
182 if (newbuf == NULL) {
183 err = got_error_from_errno();
184 free(*outbuf);
185 *outbuf = NULL;
186 *outlen = 0;
187 goto done;
189 *outbuf = newbuf;
190 zb.outbuf = newbuf + *outlen;
191 zb.outlen = GOT_ZSTREAM_BUFSIZE;
193 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
195 done:
196 got_inflate_end(&zb);
197 return err;
200 const struct got_error *
201 got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd)
203 const struct got_error *err;
204 size_t avail;
205 struct got_zstream_buf zb;
206 void *newbuf;
208 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
209 if (*outbuf == NULL)
210 return got_error_from_errno();
211 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
212 if (err)
213 return err;
215 *outlen = 0;
217 do {
218 err = got_inflate_read_fd(&zb, infd, &avail);
219 if (err)
220 return err;
221 *outlen += avail;
222 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
223 newbuf = reallocarray(*outbuf, 1,
224 *outlen + GOT_ZSTREAM_BUFSIZE);
225 if (newbuf == NULL) {
226 err = got_error_from_errno();
227 free(*outbuf);
228 *outbuf = NULL;
229 *outlen = 0;
230 goto done;
232 *outbuf = newbuf;
233 zb.outbuf = newbuf + *outlen;
234 zb.outlen = GOT_ZSTREAM_BUFSIZE;
236 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
238 done:
239 got_inflate_end(&zb);
240 return err;
243 const struct got_error *
244 got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
246 const struct got_error *err = NULL;
247 size_t avail;
248 struct got_zstream_buf zb;
250 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
251 if (err)
252 goto done;
254 *outlen = 0;
256 do {
257 err = got_inflate_read(&zb, infile, &avail);
258 if (err)
259 return err;
260 if (avail > 0) {
261 ssize_t n;
262 n = write(outfd, zb.outbuf, avail);
263 if (n != avail) {
264 err = got_error_from_errno();
265 goto done;
267 *outlen += avail;
269 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
271 done:
272 if (err == NULL) {
273 if (lseek(outfd, SEEK_SET, 0) == -1)
274 err = got_error_from_errno();
276 got_inflate_end(&zb);
277 return err;
280 const struct got_error *
281 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
283 const struct got_error *err;
284 size_t avail;
285 struct got_zstream_buf zb;
287 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
288 if (err)
289 goto done;
291 *outlen = 0;
293 do {
294 err = got_inflate_read(&zb, infile, &avail);
295 if (err)
296 return err;
297 if (avail > 0) {
298 size_t n;
299 n = fwrite(zb.outbuf, avail, 1, outfile);
300 if (n != 1) {
301 err = got_ferror(outfile, GOT_ERR_IO);
302 goto done;
304 *outlen += avail;
306 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
308 done:
309 if (err == NULL)
310 rewind(outfile);
311 got_inflate_end(&zb);
312 return err;
315 const struct got_error *
316 got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile)
318 const struct got_error *err;
319 size_t avail;
320 struct got_zstream_buf zb;
322 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
323 if (err)
324 goto done;
326 *outlen = 0;
328 do {
329 err = got_inflate_read_fd(&zb, infd, &avail);
330 if (err)
331 return err;
332 if (avail > 0) {
333 size_t n;
334 n = fwrite(zb.outbuf, avail, 1, outfile);
335 if (n != 1) {
336 err = got_ferror(outfile, GOT_ERR_IO);
337 goto done;
339 *outlen += avail;
341 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
343 done:
344 if (err == NULL)
345 rewind(outfile);
346 got_inflate_end(&zb);
347 return err;