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 void
109 got_inflate_end(struct got_zstream_buf *zb)
111 free(zb->inbuf);
112 if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
113 free(zb->outbuf);
114 inflateEnd(&zb->z);
117 const struct got_error *
118 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
120 const struct got_error *err;
121 size_t avail;
122 struct got_zstream_buf zb;
123 void *newbuf;
125 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
126 if (*outbuf == NULL)
127 return got_error_from_errno();
128 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
129 if (err)
130 return err;
132 *outlen = 0;
134 do {
135 err = got_inflate_read(&zb, f, &avail);
136 if (err)
137 return err;
138 *outlen += avail;
139 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
140 newbuf = reallocarray(*outbuf, 1,
141 *outlen + GOT_ZSTREAM_BUFSIZE);
142 if (newbuf == NULL) {
143 err = got_error_from_errno();
144 free(*outbuf);
145 *outbuf = NULL;
146 *outlen = 0;
147 goto done;
149 *outbuf = newbuf;
150 zb.outbuf = newbuf + *outlen;
151 zb.outlen = GOT_ZSTREAM_BUFSIZE;
153 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
155 done:
156 got_inflate_end(&zb);
157 return err;
160 const struct got_error *
161 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
163 const struct got_error *err;
164 size_t avail;
165 struct got_zstream_buf zb;
167 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
168 if (err)
169 goto done;
171 *outlen = 0;
173 do {
174 err = got_inflate_read(&zb, infile, &avail);
175 if (err)
176 return err;
177 if (avail > 0) {
178 size_t n;
179 n = fwrite(zb.outbuf, avail, 1, outfile);
180 if (n != 1) {
181 err = got_ferror(outfile, GOT_ERR_IO);
182 goto done;
184 *outlen += avail;
186 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
188 done:
189 if (err == NULL)
190 rewind(outfile);
191 got_inflate_end(&zb);
192 return err;