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;
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 break; /* EOF */
87 }
88 z->next_in = zb->inbuf;
89 z->avail_in = n;
90 }
91 ret = inflate(z, Z_SYNC_FLUSH);
92 } while (ret == Z_OK && z->avail_out > 0);
94 if (ret == Z_OK) {
95 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
96 } else {
97 if (ret != Z_STREAM_END)
98 return got_error(GOT_ERR_DECOMPRESSION);
99 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
102 *outlenp = z->total_out - last_total_out;
103 return NULL;
106 void
107 got_inflate_end(struct got_zstream_buf *zb)
109 free(zb->inbuf);
110 if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
111 free(zb->outbuf);
112 inflateEnd(&zb->z);
115 const struct got_error *
116 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
118 const struct got_error *err;
119 size_t avail;
120 struct got_zstream_buf zb;
121 void *newbuf;
123 *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
124 if (*outbuf == NULL)
125 return got_error_from_errno();
126 err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
127 if (err)
128 return err;
130 *outlen = 0;
132 do {
133 err = got_inflate_read(&zb, f, &avail);
134 if (err)
135 return err;
136 *outlen += avail;
137 if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
138 newbuf = reallocarray(*outbuf, 1,
139 *outlen + GOT_ZSTREAM_BUFSIZE);
140 if (newbuf == NULL) {
141 err = got_error_from_errno();
142 free(*outbuf);
143 *outbuf = NULL;
144 *outlen = 0;
145 goto done;
147 *outbuf = newbuf;
148 zb.outbuf = newbuf + *outlen;
149 zb.outlen = GOT_ZSTREAM_BUFSIZE;
151 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
153 done:
154 got_inflate_end(&zb);
155 return err;
158 const struct got_error *
159 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
161 const struct got_error *err;
162 size_t avail;
163 struct got_zstream_buf zb;
165 err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
166 if (err)
167 goto done;
169 *outlen = 0;
171 do {
172 err = got_inflate_read(&zb, infile, &avail);
173 if (err)
174 return err;
175 if (avail > 0) {
176 size_t n;
177 n = fwrite(zb.outbuf, avail, 1, outfile);
178 if (n != 1) {
179 err = got_ferror(outfile, GOT_ERR_IO);
180 goto done;
182 *outlen += avail;
184 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
186 done:
187 if (err == NULL)
188 rewind(outfile);
189 got_inflate_end(&zb);
190 return err;