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 "path.h"
29 #include "zb.h"
31 const struct got_error *
32 got_inflate_init(struct got_zstream_buf *zb, 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(GOT_ERR_NO_MEM);
50 goto done;
51 }
53 zb->outbuf = calloc(1, zb->outlen);
54 if (zb->outbuf == NULL) {
55 err = got_error(GOT_ERR_NO_MEM);
56 goto done;
57 }
59 done:
60 if (err)
61 got_inflate_end(zb);
62 return err;
63 }
65 const struct got_error *
66 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *inlenp,
67 size_t *outlenp)
68 {
69 size_t last_total_out = zb->z.total_out;
70 z_stream *z = &zb->z;
71 int ret;
73 z->next_out = zb->outbuf;
74 z->avail_out = zb->outlen;
76 *outlenp = 0;
77 if (inlenp)
78 *inlenp = 0;
79 do {
80 if (z->avail_in == 0) {
81 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
82 if (n == 0) {
83 if (ferror(f))
84 return got_ferror(f, GOT_ERR_IO);
85 break; /* EOF */
86 }
87 z->next_in = zb->inbuf;
88 z->avail_in = n;
89 if (inlenp)
90 *inlenp += n;
91 }
92 ret = inflate(z, Z_SYNC_FLUSH);
93 } while (ret == Z_OK && z->avail_out > 0);
95 if (ret != Z_OK) {
96 if (ret != Z_STREAM_END)
97 return got_error(GOT_ERR_DECOMPRESSION);
98 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
99 }
101 *outlenp = z->total_out - last_total_out;
102 return NULL;
105 void
106 got_inflate_end(struct got_zstream_buf *zb)
108 free(zb->inbuf);
109 free(zb->outbuf);
110 inflateEnd(&zb->z);
113 const struct got_error *
114 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f, size_t insize)
116 const struct got_error *err;
117 size_t inbytes, consumed, avail;
118 struct got_zstream_buf zb;
119 void *newbuf;
121 err = got_inflate_init(&zb, 8192);
122 if (err)
123 return err;
125 *outbuf = NULL;
126 *outlen = 0;
127 inbytes = 0;
129 do {
130 err = got_inflate_read(&zb, f, &consumed, &avail);
131 if (err)
132 return err;
133 inbytes += consumed;
134 if (avail == 0) {
135 if (inbytes < insize)
136 err = got_error(GOT_ERR_BAD_DELTA);
137 break;
139 newbuf = reallocarray(*outbuf, 1, *outlen + avail);
140 if (newbuf == NULL) {
141 free(*outbuf);
142 *outbuf = NULL;
143 *outlen = 0;
144 err = got_error(GOT_ERR_NO_MEM);
145 goto done;
147 memcpy(newbuf + *outlen, zb.outbuf, avail);
148 *outbuf = newbuf;
149 *outlen += avail;
150 } while (inbytes < insize);
152 done:
153 got_inflate_end(&zb);
154 return err;
157 const struct got_error *
158 got_inflate_to_tempfile(FILE **outfile, 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 *outfile = got_opentemp();
166 if (*outfile == NULL)
167 return got_error_from_errno();
169 err = got_inflate_init(&zb, 8192);
170 if (err)
171 goto done;
173 *outlen = 0;
175 do {
176 err = got_inflate_read(&zb, f, NULL, &avail);
177 if (err)
178 return err;
179 if (avail > 0) {
180 size_t n;
181 n = fwrite(zb.outbuf, avail, 1, *outfile);
182 if (n != 1) {
183 err = got_ferror(*outfile, GOT_ERR_IO);
184 goto done;
186 *outlen += avail;
188 } while (avail > 0);
190 done:
191 if (err) {
192 fclose(*outfile);
193 *outfile = NULL;
194 } else
195 rewind(*outfile);
196 got_inflate_end(&zb);
197 return err;