Blame


1 4ca7b755 2018-01-26 stsp /*
2 4ca7b755 2018-01-26 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 4ca7b755 2018-01-26 stsp *
4 4ca7b755 2018-01-26 stsp * Permission to use, copy, modify, and distribute this software for any
5 4ca7b755 2018-01-26 stsp * purpose with or without fee is hereby granted, provided that the above
6 4ca7b755 2018-01-26 stsp * copyright notice and this permission notice appear in all copies.
7 4ca7b755 2018-01-26 stsp *
8 4ca7b755 2018-01-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 4ca7b755 2018-01-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 4ca7b755 2018-01-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 4ca7b755 2018-01-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 4ca7b755 2018-01-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 4ca7b755 2018-01-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 4ca7b755 2018-01-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 4ca7b755 2018-01-26 stsp */
16 4ca7b755 2018-01-26 stsp
17 4ca7b755 2018-01-26 stsp #include <sys/queue.h>
18 4ca7b755 2018-01-26 stsp
19 4ca7b755 2018-01-26 stsp #include <stdio.h>
20 4ca7b755 2018-01-26 stsp #include <stdlib.h>
21 4ca7b755 2018-01-26 stsp #include <string.h>
22 4ca7b755 2018-01-26 stsp #include <sha1.h>
23 4ca7b755 2018-01-26 stsp #include <zlib.h>
24 4ca7b755 2018-01-26 stsp
25 4ca7b755 2018-01-26 stsp #include "got_error.h"
26 4ca7b755 2018-01-26 stsp #include "got_object.h"
27 4ca7b755 2018-01-26 stsp
28 885d3e02 2018-01-27 stsp #include "path.h"
29 4ca7b755 2018-01-26 stsp #include "zb.h"
30 4ca7b755 2018-01-26 stsp
31 4ca7b755 2018-01-26 stsp const struct got_error *
32 4ca7b755 2018-01-26 stsp got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
33 4ca7b755 2018-01-26 stsp {
34 4ca7b755 2018-01-26 stsp const struct got_error *err = NULL;
35 4ca7b755 2018-01-26 stsp
36 4ca7b755 2018-01-26 stsp memset(zb, 0, sizeof(*zb));
37 4ca7b755 2018-01-26 stsp
38 4ca7b755 2018-01-26 stsp zb->z.zalloc = Z_NULL;
39 4ca7b755 2018-01-26 stsp zb->z.zfree = Z_NULL;
40 4ca7b755 2018-01-26 stsp if (inflateInit(&zb->z) != Z_OK) {
41 4ca7b755 2018-01-26 stsp err = got_error(GOT_ERR_IO);
42 4ca7b755 2018-01-26 stsp goto done;
43 4ca7b755 2018-01-26 stsp }
44 4ca7b755 2018-01-26 stsp
45 4ca7b755 2018-01-26 stsp zb->inlen = zb->outlen = bufsize;
46 4ca7b755 2018-01-26 stsp
47 4ca7b755 2018-01-26 stsp zb->inbuf = calloc(1, zb->inlen);
48 4ca7b755 2018-01-26 stsp if (zb->inbuf == NULL) {
49 4ca7b755 2018-01-26 stsp err = got_error(GOT_ERR_NO_MEM);
50 4ca7b755 2018-01-26 stsp goto done;
51 4ca7b755 2018-01-26 stsp }
52 4ca7b755 2018-01-26 stsp
53 4ca7b755 2018-01-26 stsp zb->outbuf = calloc(1, zb->outlen);
54 4ca7b755 2018-01-26 stsp if (zb->outbuf == NULL) {
55 4ca7b755 2018-01-26 stsp err = got_error(GOT_ERR_NO_MEM);
56 4ca7b755 2018-01-26 stsp goto done;
57 4ca7b755 2018-01-26 stsp }
58 4ca7b755 2018-01-26 stsp
59 4ca7b755 2018-01-26 stsp done:
60 4ca7b755 2018-01-26 stsp if (err)
61 4ca7b755 2018-01-26 stsp got_inflate_end(zb);
62 4ca7b755 2018-01-26 stsp return err;
63 4ca7b755 2018-01-26 stsp }
64 4ca7b755 2018-01-26 stsp
65 4ca7b755 2018-01-26 stsp const struct got_error *
66 885d3e02 2018-01-27 stsp got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *inlenp,
67 885d3e02 2018-01-27 stsp size_t *outlenp)
68 4ca7b755 2018-01-26 stsp {
69 4ca7b755 2018-01-26 stsp size_t last_total_out = zb->z.total_out;
70 4ca7b755 2018-01-26 stsp z_stream *z = &zb->z;
71 885d3e02 2018-01-27 stsp int ret;
72 4ca7b755 2018-01-26 stsp
73 4ca7b755 2018-01-26 stsp z->next_out = zb->outbuf;
74 4ca7b755 2018-01-26 stsp z->avail_out = zb->outlen;
75 4ca7b755 2018-01-26 stsp
76 6336b73f 2018-01-27 stsp *outlenp = 0;
77 885d3e02 2018-01-27 stsp if (inlenp)
78 885d3e02 2018-01-27 stsp *inlenp = 0;
79 4ca7b755 2018-01-26 stsp do {
80 4ca7b755 2018-01-26 stsp if (z->avail_in == 0) {
81 885d3e02 2018-01-27 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
82 4ca7b755 2018-01-26 stsp if (n == 0) {
83 4ca7b755 2018-01-26 stsp if (ferror(f))
84 4ca7b755 2018-01-26 stsp return got_ferror(f, GOT_ERR_IO);
85 6336b73f 2018-01-27 stsp break; /* EOF */
86 4ca7b755 2018-01-26 stsp }
87 4ca7b755 2018-01-26 stsp z->next_in = zb->inbuf;
88 4ca7b755 2018-01-26 stsp z->avail_in = n;
89 885d3e02 2018-01-27 stsp if (inlenp)
90 885d3e02 2018-01-27 stsp *inlenp += n;
91 4ca7b755 2018-01-26 stsp }
92 4ca7b755 2018-01-26 stsp ret = inflate(z, Z_SYNC_FLUSH);
93 4ca7b755 2018-01-26 stsp } while (ret == Z_OK && z->avail_out > 0);
94 4ca7b755 2018-01-26 stsp
95 4ca7b755 2018-01-26 stsp if (ret != Z_OK) {
96 4ca7b755 2018-01-26 stsp if (ret != Z_STREAM_END)
97 4ca7b755 2018-01-26 stsp return got_error(GOT_ERR_DECOMPRESSION);
98 4ca7b755 2018-01-26 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
99 4ca7b755 2018-01-26 stsp }
100 4ca7b755 2018-01-26 stsp
101 4ca7b755 2018-01-26 stsp *outlenp = z->total_out - last_total_out;
102 4ca7b755 2018-01-26 stsp return NULL;
103 4ca7b755 2018-01-26 stsp }
104 4ca7b755 2018-01-26 stsp
105 4ca7b755 2018-01-26 stsp void
106 4ca7b755 2018-01-26 stsp got_inflate_end(struct got_zstream_buf *zb)
107 4ca7b755 2018-01-26 stsp {
108 4ca7b755 2018-01-26 stsp free(zb->inbuf);
109 4ca7b755 2018-01-26 stsp free(zb->outbuf);
110 4ca7b755 2018-01-26 stsp inflateEnd(&zb->z);
111 4ca7b755 2018-01-26 stsp }
112 885d3e02 2018-01-27 stsp
113 885d3e02 2018-01-27 stsp const struct got_error *
114 885d3e02 2018-01-27 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f, size_t insize)
115 885d3e02 2018-01-27 stsp {
116 885d3e02 2018-01-27 stsp const struct got_error *err;
117 885d3e02 2018-01-27 stsp size_t inbytes, consumed, avail;
118 885d3e02 2018-01-27 stsp struct got_zstream_buf zb;
119 885d3e02 2018-01-27 stsp void *newbuf;
120 885d3e02 2018-01-27 stsp
121 885d3e02 2018-01-27 stsp err = got_inflate_init(&zb, 8192);
122 885d3e02 2018-01-27 stsp if (err)
123 885d3e02 2018-01-27 stsp return err;
124 885d3e02 2018-01-27 stsp
125 885d3e02 2018-01-27 stsp *outbuf = NULL;
126 885d3e02 2018-01-27 stsp *outlen = 0;
127 885d3e02 2018-01-27 stsp inbytes = 0;
128 885d3e02 2018-01-27 stsp
129 885d3e02 2018-01-27 stsp do {
130 885d3e02 2018-01-27 stsp err = got_inflate_read(&zb, f, &consumed, &avail);
131 885d3e02 2018-01-27 stsp if (err)
132 885d3e02 2018-01-27 stsp return err;
133 885d3e02 2018-01-27 stsp inbytes += consumed;
134 885d3e02 2018-01-27 stsp if (avail == 0) {
135 885d3e02 2018-01-27 stsp if (inbytes < insize)
136 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
137 885d3e02 2018-01-27 stsp break;
138 885d3e02 2018-01-27 stsp }
139 885d3e02 2018-01-27 stsp newbuf = reallocarray(*outbuf, 1, *outlen + avail);
140 885d3e02 2018-01-27 stsp if (newbuf == NULL) {
141 885d3e02 2018-01-27 stsp free(*outbuf);
142 885d3e02 2018-01-27 stsp *outbuf = NULL;
143 885d3e02 2018-01-27 stsp *outlen = 0;
144 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_NO_MEM);
145 885d3e02 2018-01-27 stsp goto done;
146 885d3e02 2018-01-27 stsp }
147 885d3e02 2018-01-27 stsp memcpy(newbuf + *outlen, zb.outbuf, avail);
148 885d3e02 2018-01-27 stsp *outbuf = newbuf;
149 885d3e02 2018-01-27 stsp *outlen += avail;
150 885d3e02 2018-01-27 stsp } while (inbytes < insize);
151 885d3e02 2018-01-27 stsp
152 885d3e02 2018-01-27 stsp done:
153 885d3e02 2018-01-27 stsp got_inflate_end(&zb);
154 885d3e02 2018-01-27 stsp return err;
155 885d3e02 2018-01-27 stsp }
156 885d3e02 2018-01-27 stsp
157 885d3e02 2018-01-27 stsp const struct got_error *
158 885d3e02 2018-01-27 stsp got_inflate_to_tempfile(FILE **outfile, size_t *outlen, FILE *f)
159 885d3e02 2018-01-27 stsp {
160 885d3e02 2018-01-27 stsp const struct got_error *err;
161 885d3e02 2018-01-27 stsp size_t avail;
162 885d3e02 2018-01-27 stsp struct got_zstream_buf zb;
163 885d3e02 2018-01-27 stsp void *newbuf;
164 885d3e02 2018-01-27 stsp
165 885d3e02 2018-01-27 stsp *outfile = got_opentemp();
166 885d3e02 2018-01-27 stsp if (*outfile == NULL)
167 885d3e02 2018-01-27 stsp return got_error_from_errno();
168 885d3e02 2018-01-27 stsp
169 885d3e02 2018-01-27 stsp err = got_inflate_init(&zb, 8192);
170 885d3e02 2018-01-27 stsp if (err)
171 885d3e02 2018-01-27 stsp goto done;
172 885d3e02 2018-01-27 stsp
173 885d3e02 2018-01-27 stsp *outlen = 0;
174 885d3e02 2018-01-27 stsp
175 885d3e02 2018-01-27 stsp do {
176 885d3e02 2018-01-27 stsp err = got_inflate_read(&zb, f, NULL, &avail);
177 885d3e02 2018-01-27 stsp if (err)
178 885d3e02 2018-01-27 stsp return err;
179 885d3e02 2018-01-27 stsp if (avail > 0) {
180 885d3e02 2018-01-27 stsp size_t n;
181 885d3e02 2018-01-27 stsp n = fwrite(zb.outbuf, avail, 1, *outfile);
182 885d3e02 2018-01-27 stsp if (n != 1) {
183 885d3e02 2018-01-27 stsp err = got_ferror(*outfile, GOT_ERR_IO);
184 885d3e02 2018-01-27 stsp goto done;
185 885d3e02 2018-01-27 stsp }
186 885d3e02 2018-01-27 stsp *outlen += avail;
187 885d3e02 2018-01-27 stsp }
188 885d3e02 2018-01-27 stsp } while (avail > 0);
189 885d3e02 2018-01-27 stsp
190 885d3e02 2018-01-27 stsp done:
191 885d3e02 2018-01-27 stsp if (err) {
192 885d3e02 2018-01-27 stsp fclose(*outfile);
193 885d3e02 2018-01-27 stsp *outfile = NULL;
194 885d3e02 2018-01-27 stsp } else
195 885d3e02 2018-01-27 stsp rewind(*outfile);
196 885d3e02 2018-01-27 stsp got_inflate_end(&zb);
197 885d3e02 2018-01-27 stsp return err;
198 885d3e02 2018-01-27 stsp }