Blame


1 2181e0c8 2019-03-19 stsp /*
2 2181e0c8 2019-03-19 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 2181e0c8 2019-03-19 stsp *
4 2181e0c8 2019-03-19 stsp * Permission to use, copy, modify, and distribute this software for any
5 2181e0c8 2019-03-19 stsp * purpose with or without fee is hereby granted, provided that the above
6 2181e0c8 2019-03-19 stsp * copyright notice and this permission notice appear in all copies.
7 2181e0c8 2019-03-19 stsp *
8 2181e0c8 2019-03-19 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2181e0c8 2019-03-19 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2181e0c8 2019-03-19 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2181e0c8 2019-03-19 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2181e0c8 2019-03-19 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2181e0c8 2019-03-19 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2181e0c8 2019-03-19 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 2181e0c8 2019-03-19 stsp */
16 2181e0c8 2019-03-19 stsp
17 2181e0c8 2019-03-19 stsp #include <sys/queue.h>
18 2181e0c8 2019-03-19 stsp
19 2181e0c8 2019-03-19 stsp #include <errno.h>
20 2181e0c8 2019-03-19 stsp #include <stdio.h>
21 2181e0c8 2019-03-19 stsp #include <stdlib.h>
22 2181e0c8 2019-03-19 stsp #include <string.h>
23 2181e0c8 2019-03-19 stsp #include <sha1.h>
24 2181e0c8 2019-03-19 stsp #include <zlib.h>
25 2181e0c8 2019-03-19 stsp #include <time.h>
26 2181e0c8 2019-03-19 stsp
27 2181e0c8 2019-03-19 stsp #include "got_error.h"
28 2181e0c8 2019-03-19 stsp #include "got_object.h"
29 324d37e7 2019-05-11 stsp #include "got_path.h"
30 2181e0c8 2019-03-19 stsp
31 2181e0c8 2019-03-19 stsp #include "got_lib_deflate.h"
32 2181e0c8 2019-03-19 stsp
33 2181e0c8 2019-03-19 stsp #ifndef MIN
34 2181e0c8 2019-03-19 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 2181e0c8 2019-03-19 stsp #endif
36 2181e0c8 2019-03-19 stsp
37 2181e0c8 2019-03-19 stsp const struct got_error *
38 3b9e6fcf 2021-06-05 stsp got_deflate_init(struct got_deflate_buf *zb, uint8_t *outbuf, size_t bufsize)
39 2181e0c8 2019-03-19 stsp {
40 2181e0c8 2019-03-19 stsp const struct got_error *err = NULL;
41 2181e0c8 2019-03-19 stsp int zerr;
42 2181e0c8 2019-03-19 stsp
43 2181e0c8 2019-03-19 stsp memset(&zb->z, 0, sizeof(zb->z));
44 2181e0c8 2019-03-19 stsp
45 2181e0c8 2019-03-19 stsp zb->z.zalloc = Z_NULL;
46 2181e0c8 2019-03-19 stsp zb->z.zfree = Z_NULL;
47 2181e0c8 2019-03-19 stsp zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
48 2181e0c8 2019-03-19 stsp if (zerr != Z_OK) {
49 2181e0c8 2019-03-19 stsp if (zerr == Z_ERRNO)
50 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
51 2181e0c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
52 2181e0c8 2019-03-19 stsp errno = ENOMEM;
53 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
54 2181e0c8 2019-03-19 stsp }
55 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
56 2181e0c8 2019-03-19 stsp }
57 2181e0c8 2019-03-19 stsp
58 2181e0c8 2019-03-19 stsp zb->inlen = zb->outlen = bufsize;
59 2181e0c8 2019-03-19 stsp
60 2181e0c8 2019-03-19 stsp zb->inbuf = calloc(1, zb->inlen);
61 2181e0c8 2019-03-19 stsp if (zb->inbuf == NULL) {
62 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
63 2181e0c8 2019-03-19 stsp goto done;
64 2181e0c8 2019-03-19 stsp }
65 2181e0c8 2019-03-19 stsp
66 2181e0c8 2019-03-19 stsp zb->flags = 0;
67 2181e0c8 2019-03-19 stsp if (outbuf == NULL) {
68 2181e0c8 2019-03-19 stsp zb->outbuf = calloc(1, zb->outlen);
69 2181e0c8 2019-03-19 stsp if (zb->outbuf == NULL) {
70 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
71 2181e0c8 2019-03-19 stsp goto done;
72 2181e0c8 2019-03-19 stsp }
73 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
74 2181e0c8 2019-03-19 stsp } else
75 2181e0c8 2019-03-19 stsp zb->outbuf = outbuf;
76 2181e0c8 2019-03-19 stsp done:
77 2181e0c8 2019-03-19 stsp if (err)
78 2181e0c8 2019-03-19 stsp got_deflate_end(zb);
79 2181e0c8 2019-03-19 stsp return err;
80 2181e0c8 2019-03-19 stsp }
81 2181e0c8 2019-03-19 stsp
82 91b40e30 2021-05-21 stsp static void
83 31e61ec1 2021-09-28 naddy csum_output(struct got_deflate_checksum *csum, const uint8_t *buf, size_t len)
84 91b40e30 2021-05-21 stsp {
85 91b40e30 2021-05-21 stsp if (csum->output_crc)
86 91b40e30 2021-05-21 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
87 91b40e30 2021-05-21 stsp
88 91b40e30 2021-05-21 stsp if (csum->output_sha1)
89 91b40e30 2021-05-21 stsp SHA1Update(csum->output_sha1, buf, len);
90 91b40e30 2021-05-21 stsp }
91 91b40e30 2021-05-21 stsp
92 2181e0c8 2019-03-19 stsp const struct got_error *
93 72840534 2022-01-19 stsp got_deflate_read(struct got_deflate_buf *zb, FILE *f, off_t len,
94 72840534 2022-01-19 stsp size_t *outlenp, off_t *consumed)
95 2181e0c8 2019-03-19 stsp {
96 2181e0c8 2019-03-19 stsp size_t last_total_out = zb->z.total_out;
97 2181e0c8 2019-03-19 stsp z_stream *z = &zb->z;
98 2181e0c8 2019-03-19 stsp int ret = Z_ERRNO;
99 2181e0c8 2019-03-19 stsp
100 2181e0c8 2019-03-19 stsp z->next_out = zb->outbuf;
101 2181e0c8 2019-03-19 stsp z->avail_out = zb->outlen;
102 2181e0c8 2019-03-19 stsp
103 2181e0c8 2019-03-19 stsp *outlenp = 0;
104 72840534 2022-01-19 stsp *consumed = 0;
105 2181e0c8 2019-03-19 stsp do {
106 72840534 2022-01-19 stsp size_t last_total_in = z->total_in;
107 2181e0c8 2019-03-19 stsp if (z->avail_in == 0) {
108 72840534 2022-01-19 stsp size_t n = 0;
109 72840534 2022-01-19 stsp if (*consumed < len) {
110 72840534 2022-01-19 stsp n = fread(zb->inbuf, 1,
111 72840534 2022-01-19 stsp MIN(zb->inlen, len - *consumed), f);
112 72840534 2022-01-19 stsp }
113 2181e0c8 2019-03-19 stsp if (n == 0) {
114 2181e0c8 2019-03-19 stsp if (ferror(f))
115 2181e0c8 2019-03-19 stsp return got_ferror(f, GOT_ERR_IO);
116 2181e0c8 2019-03-19 stsp /* EOF */
117 4e4a7005 2019-04-13 stsp ret = deflate(z, Z_FINISH);
118 2181e0c8 2019-03-19 stsp break;
119 2181e0c8 2019-03-19 stsp }
120 2181e0c8 2019-03-19 stsp z->next_in = zb->inbuf;
121 2181e0c8 2019-03-19 stsp z->avail_in = n;
122 2181e0c8 2019-03-19 stsp }
123 2181e0c8 2019-03-19 stsp ret = deflate(z, Z_NO_FLUSH);
124 72840534 2022-01-19 stsp *consumed += z->total_in - last_total_in;
125 2181e0c8 2019-03-19 stsp } while (ret == Z_OK && z->avail_out > 0);
126 2181e0c8 2019-03-19 stsp
127 2181e0c8 2019-03-19 stsp if (ret == Z_OK) {
128 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
129 2181e0c8 2019-03-19 stsp } else {
130 2181e0c8 2019-03-19 stsp if (ret != Z_STREAM_END)
131 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
132 2181e0c8 2019-03-19 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
133 2181e0c8 2019-03-19 stsp }
134 2181e0c8 2019-03-19 stsp
135 2181e0c8 2019-03-19 stsp *outlenp = z->total_out - last_total_out;
136 2181e0c8 2019-03-19 stsp return NULL;
137 2181e0c8 2019-03-19 stsp }
138 2181e0c8 2019-03-19 stsp
139 2d9e6abf 2022-05-04 stsp static const struct got_error *
140 2d9e6abf 2022-05-04 stsp deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
141 2d9e6abf 2022-05-04 stsp size_t len, size_t *outlenp, size_t *consumed, int flush_on_eof)
142 64a8571e 2022-01-07 stsp {
143 64a8571e 2022-01-07 stsp z_stream *z = &zb->z;
144 64a8571e 2022-01-07 stsp size_t last_total_out = z->total_out;
145 64a8571e 2022-01-07 stsp int ret = Z_ERRNO;
146 64a8571e 2022-01-07 stsp
147 64a8571e 2022-01-07 stsp z->next_out = zb->outbuf;
148 64a8571e 2022-01-07 stsp z->avail_out = zb->outlen;
149 64a8571e 2022-01-07 stsp
150 64a8571e 2022-01-07 stsp *outlenp = 0;
151 64a8571e 2022-01-07 stsp *consumed = 0;
152 64a8571e 2022-01-07 stsp do {
153 64a8571e 2022-01-07 stsp size_t last_total_in = z->total_in;
154 64a8571e 2022-01-07 stsp if (z->avail_in == 0) {
155 64a8571e 2022-01-07 stsp z->next_in = map + offset + *consumed;
156 a9bd296d 2022-02-08 stsp if (len - *consumed > UINT_MAX)
157 a9bd296d 2022-02-08 stsp z->avail_in = UINT_MAX;
158 a9bd296d 2022-02-08 stsp else
159 a9bd296d 2022-02-08 stsp z->avail_in = len - *consumed;
160 64a8571e 2022-01-07 stsp if (z->avail_in == 0) {
161 64a8571e 2022-01-07 stsp /* EOF */
162 2d9e6abf 2022-05-04 stsp if (flush_on_eof)
163 2d9e6abf 2022-05-04 stsp ret = deflate(z, Z_FINISH);
164 64a8571e 2022-01-07 stsp break;
165 64a8571e 2022-01-07 stsp }
166 64a8571e 2022-01-07 stsp }
167 64a8571e 2022-01-07 stsp ret = deflate(z, Z_NO_FLUSH);
168 64a8571e 2022-01-07 stsp *consumed += z->total_in - last_total_in;
169 64a8571e 2022-01-07 stsp } while (ret == Z_OK && z->avail_out > 0);
170 64a8571e 2022-01-07 stsp
171 64a8571e 2022-01-07 stsp if (ret == Z_OK) {
172 64a8571e 2022-01-07 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
173 64a8571e 2022-01-07 stsp } else {
174 64a8571e 2022-01-07 stsp if (ret != Z_STREAM_END)
175 64a8571e 2022-01-07 stsp return got_error(GOT_ERR_COMPRESSION);
176 64a8571e 2022-01-07 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
177 64a8571e 2022-01-07 stsp }
178 64a8571e 2022-01-07 stsp
179 64a8571e 2022-01-07 stsp *outlenp = z->total_out - last_total_out;
180 2d9e6abf 2022-05-04 stsp return NULL;
181 2d9e6abf 2022-05-04 stsp }
182 2d9e6abf 2022-05-04 stsp
183 2d9e6abf 2022-05-04 stsp const struct got_error *
184 2d9e6abf 2022-05-04 stsp got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
185 2d9e6abf 2022-05-04 stsp size_t len, size_t *outlenp, size_t *consumed)
186 2d9e6abf 2022-05-04 stsp {
187 2d9e6abf 2022-05-04 stsp return deflate_read_mmap(zb, map, offset, len, outlenp, consumed, 1);
188 2d9e6abf 2022-05-04 stsp }
189 2d9e6abf 2022-05-04 stsp
190 2d9e6abf 2022-05-04 stsp const struct got_error *
191 2d9e6abf 2022-05-04 stsp got_deflate_flush(struct got_deflate_buf *zb, FILE *outfile,
192 2d9e6abf 2022-05-04 stsp struct got_deflate_checksum *csum, off_t *outlenp)
193 2d9e6abf 2022-05-04 stsp {
194 2d9e6abf 2022-05-04 stsp int ret;
195 2d9e6abf 2022-05-04 stsp size_t n;
196 2d9e6abf 2022-05-04 stsp z_stream *z = &zb->z;
197 2d9e6abf 2022-05-04 stsp
198 2d9e6abf 2022-05-04 stsp if (z->avail_in != 0)
199 2d9e6abf 2022-05-04 stsp return got_error_msg(GOT_ERR_COMPRESSION,
200 2d9e6abf 2022-05-04 stsp "cannot flush zb with pending input data");
201 2d9e6abf 2022-05-04 stsp
202 2d9e6abf 2022-05-04 stsp do {
203 2d9e6abf 2022-05-04 stsp size_t avail, last_total_out = zb->z.total_out;
204 2d9e6abf 2022-05-04 stsp
205 2d9e6abf 2022-05-04 stsp z->next_out = zb->outbuf;
206 2d9e6abf 2022-05-04 stsp z->avail_out = zb->outlen;
207 2d9e6abf 2022-05-04 stsp
208 2d9e6abf 2022-05-04 stsp ret = deflate(z, Z_FINISH);
209 2d9e6abf 2022-05-04 stsp if (ret != Z_STREAM_END && ret != Z_OK)
210 2d9e6abf 2022-05-04 stsp return got_error(GOT_ERR_COMPRESSION);
211 2d9e6abf 2022-05-04 stsp
212 2d9e6abf 2022-05-04 stsp avail = z->total_out - last_total_out;
213 2d9e6abf 2022-05-04 stsp if (avail > 0) {
214 2d9e6abf 2022-05-04 stsp n = fwrite(zb->outbuf, avail, 1, outfile);
215 2d9e6abf 2022-05-04 stsp if (n != 1)
216 2d9e6abf 2022-05-04 stsp return got_ferror(outfile, GOT_ERR_IO);
217 2d9e6abf 2022-05-04 stsp if (csum)
218 2d9e6abf 2022-05-04 stsp csum_output(csum, zb->outbuf, avail);
219 2d9e6abf 2022-05-04 stsp if (outlenp)
220 2d9e6abf 2022-05-04 stsp *outlenp += avail;
221 2d9e6abf 2022-05-04 stsp }
222 2d9e6abf 2022-05-04 stsp } while (ret != Z_STREAM_END);
223 2d9e6abf 2022-05-04 stsp
224 2d9e6abf 2022-05-04 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
225 64a8571e 2022-01-07 stsp return NULL;
226 64a8571e 2022-01-07 stsp }
227 64a8571e 2022-01-07 stsp
228 2181e0c8 2019-03-19 stsp void
229 2181e0c8 2019-03-19 stsp got_deflate_end(struct got_deflate_buf *zb)
230 2181e0c8 2019-03-19 stsp {
231 2181e0c8 2019-03-19 stsp free(zb->inbuf);
232 2181e0c8 2019-03-19 stsp if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
233 2181e0c8 2019-03-19 stsp free(zb->outbuf);
234 2181e0c8 2019-03-19 stsp deflateEnd(&zb->z);
235 894e4711 2022-10-15 stsp }
236 894e4711 2022-10-15 stsp
237 894e4711 2022-10-15 stsp const struct got_error *
238 894e4711 2022-10-15 stsp got_deflate_to_fd(off_t *outlen, FILE *infile, off_t len, int outfd,
239 894e4711 2022-10-15 stsp struct got_deflate_checksum *csum)
240 894e4711 2022-10-15 stsp {
241 894e4711 2022-10-15 stsp const struct got_error *err;
242 894e4711 2022-10-15 stsp size_t avail;
243 894e4711 2022-10-15 stsp off_t consumed;
244 894e4711 2022-10-15 stsp struct got_deflate_buf zb;
245 894e4711 2022-10-15 stsp
246 894e4711 2022-10-15 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
247 894e4711 2022-10-15 stsp if (err)
248 894e4711 2022-10-15 stsp goto done;
249 894e4711 2022-10-15 stsp
250 894e4711 2022-10-15 stsp *outlen = 0;
251 894e4711 2022-10-15 stsp
252 894e4711 2022-10-15 stsp do {
253 894e4711 2022-10-15 stsp err = got_deflate_read(&zb, infile, len, &avail, &consumed);
254 894e4711 2022-10-15 stsp if (err)
255 894e4711 2022-10-15 stsp goto done;
256 894e4711 2022-10-15 stsp len -= consumed;
257 894e4711 2022-10-15 stsp if (avail > 0) {
258 894e4711 2022-10-15 stsp ssize_t w;
259 894e4711 2022-10-15 stsp w = write(outfd, zb.outbuf, avail);
260 894e4711 2022-10-15 stsp if (w == -1) {
261 894e4711 2022-10-15 stsp err = got_error_from_errno("write");
262 894e4711 2022-10-15 stsp goto done;
263 894e4711 2022-10-15 stsp } else if (w != avail) {
264 894e4711 2022-10-15 stsp err = got_error(GOT_ERR_IO);
265 894e4711 2022-10-15 stsp goto done;
266 894e4711 2022-10-15 stsp }
267 894e4711 2022-10-15 stsp if (csum)
268 894e4711 2022-10-15 stsp csum_output(csum, zb.outbuf, avail);
269 894e4711 2022-10-15 stsp *outlen += avail;
270 894e4711 2022-10-15 stsp }
271 894e4711 2022-10-15 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
272 894e4711 2022-10-15 stsp
273 894e4711 2022-10-15 stsp done:
274 894e4711 2022-10-15 stsp got_deflate_end(&zb);
275 894e4711 2022-10-15 stsp return err;
276 894e4711 2022-10-15 stsp }
277 894e4711 2022-10-15 stsp
278 894e4711 2022-10-15 stsp const struct got_error *
279 894e4711 2022-10-15 stsp got_deflate_to_fd_mmap(off_t *outlen, uint8_t *map, size_t offset,
280 894e4711 2022-10-15 stsp size_t len, int outfd, struct got_deflate_checksum *csum)
281 894e4711 2022-10-15 stsp {
282 894e4711 2022-10-15 stsp const struct got_error *err;
283 894e4711 2022-10-15 stsp size_t avail, consumed;
284 894e4711 2022-10-15 stsp struct got_deflate_buf zb;
285 894e4711 2022-10-15 stsp
286 894e4711 2022-10-15 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
287 894e4711 2022-10-15 stsp if (err)
288 894e4711 2022-10-15 stsp goto done;
289 894e4711 2022-10-15 stsp
290 894e4711 2022-10-15 stsp *outlen = 0;
291 894e4711 2022-10-15 stsp do {
292 894e4711 2022-10-15 stsp err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
293 894e4711 2022-10-15 stsp &consumed);
294 894e4711 2022-10-15 stsp if (err)
295 894e4711 2022-10-15 stsp goto done;
296 894e4711 2022-10-15 stsp offset += consumed;
297 894e4711 2022-10-15 stsp len -= consumed;
298 894e4711 2022-10-15 stsp if (avail > 0) {
299 894e4711 2022-10-15 stsp ssize_t w;
300 894e4711 2022-10-15 stsp w = write(outfd, zb.outbuf, avail);
301 894e4711 2022-10-15 stsp if (w == -1) {
302 894e4711 2022-10-15 stsp err = got_error_from_errno("write");
303 894e4711 2022-10-15 stsp goto done;
304 894e4711 2022-10-15 stsp } else if (w != avail) {
305 894e4711 2022-10-15 stsp err = got_error(GOT_ERR_IO);
306 894e4711 2022-10-15 stsp goto done;
307 894e4711 2022-10-15 stsp }
308 894e4711 2022-10-15 stsp if (csum)
309 894e4711 2022-10-15 stsp csum_output(csum, zb.outbuf, avail);
310 894e4711 2022-10-15 stsp *outlen += avail;
311 894e4711 2022-10-15 stsp }
312 894e4711 2022-10-15 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
313 894e4711 2022-10-15 stsp
314 894e4711 2022-10-15 stsp done:
315 894e4711 2022-10-15 stsp got_deflate_end(&zb);
316 894e4711 2022-10-15 stsp return err;
317 2181e0c8 2019-03-19 stsp }
318 2181e0c8 2019-03-19 stsp
319 2181e0c8 2019-03-19 stsp const struct got_error *
320 72840534 2022-01-19 stsp got_deflate_to_file(off_t *outlen, FILE *infile, off_t len,
321 72840534 2022-01-19 stsp FILE *outfile, struct got_deflate_checksum *csum)
322 2181e0c8 2019-03-19 stsp {
323 2181e0c8 2019-03-19 stsp const struct got_error *err;
324 2181e0c8 2019-03-19 stsp size_t avail;
325 72840534 2022-01-19 stsp off_t consumed;
326 2181e0c8 2019-03-19 stsp struct got_deflate_buf zb;
327 2181e0c8 2019-03-19 stsp
328 3b9e6fcf 2021-06-05 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
329 2181e0c8 2019-03-19 stsp if (err)
330 2181e0c8 2019-03-19 stsp goto done;
331 2181e0c8 2019-03-19 stsp
332 2181e0c8 2019-03-19 stsp *outlen = 0;
333 2181e0c8 2019-03-19 stsp
334 2181e0c8 2019-03-19 stsp do {
335 72840534 2022-01-19 stsp err = got_deflate_read(&zb, infile, len, &avail, &consumed);
336 2181e0c8 2019-03-19 stsp if (err)
337 2181e0c8 2019-03-19 stsp goto done;
338 72840534 2022-01-19 stsp len -= consumed;
339 2181e0c8 2019-03-19 stsp if (avail > 0) {
340 2181e0c8 2019-03-19 stsp size_t n;
341 2181e0c8 2019-03-19 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
342 2181e0c8 2019-03-19 stsp if (n != 1) {
343 2181e0c8 2019-03-19 stsp err = got_ferror(outfile, GOT_ERR_IO);
344 2181e0c8 2019-03-19 stsp goto done;
345 2181e0c8 2019-03-19 stsp }
346 3b9e6fcf 2021-06-05 stsp if (csum)
347 3b9e6fcf 2021-06-05 stsp csum_output(csum, zb.outbuf, avail);
348 2181e0c8 2019-03-19 stsp *outlen += avail;
349 2181e0c8 2019-03-19 stsp }
350 2181e0c8 2019-03-19 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
351 2181e0c8 2019-03-19 stsp
352 2181e0c8 2019-03-19 stsp done:
353 2181e0c8 2019-03-19 stsp got_deflate_end(&zb);
354 2181e0c8 2019-03-19 stsp return err;
355 2181e0c8 2019-03-19 stsp }
356 64a8571e 2022-01-07 stsp
357 64a8571e 2022-01-07 stsp const struct got_error *
358 72840534 2022-01-19 stsp got_deflate_to_file_mmap(off_t *outlen, uint8_t *map, size_t offset,
359 64a8571e 2022-01-07 stsp size_t len, FILE *outfile, struct got_deflate_checksum *csum)
360 64a8571e 2022-01-07 stsp {
361 64a8571e 2022-01-07 stsp const struct got_error *err;
362 64a8571e 2022-01-07 stsp size_t avail, consumed;
363 64a8571e 2022-01-07 stsp struct got_deflate_buf zb;
364 64a8571e 2022-01-07 stsp
365 64a8571e 2022-01-07 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
366 64a8571e 2022-01-07 stsp if (err)
367 64a8571e 2022-01-07 stsp goto done;
368 64a8571e 2022-01-07 stsp
369 64a8571e 2022-01-07 stsp *outlen = 0;
370 64a8571e 2022-01-07 stsp do {
371 64a8571e 2022-01-07 stsp err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
372 64a8571e 2022-01-07 stsp &consumed);
373 64a8571e 2022-01-07 stsp if (err)
374 64a8571e 2022-01-07 stsp goto done;
375 64a8571e 2022-01-07 stsp offset += consumed;
376 64a8571e 2022-01-07 stsp len -= consumed;
377 64a8571e 2022-01-07 stsp if (avail > 0) {
378 64a8571e 2022-01-07 stsp size_t n;
379 64a8571e 2022-01-07 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
380 64a8571e 2022-01-07 stsp if (n != 1) {
381 64a8571e 2022-01-07 stsp err = got_ferror(outfile, GOT_ERR_IO);
382 64a8571e 2022-01-07 stsp goto done;
383 64a8571e 2022-01-07 stsp }
384 64a8571e 2022-01-07 stsp if (csum)
385 64a8571e 2022-01-07 stsp csum_output(csum, zb.outbuf, avail);
386 64a8571e 2022-01-07 stsp *outlen += avail;
387 64a8571e 2022-01-07 stsp }
388 64a8571e 2022-01-07 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
389 2d9e6abf 2022-05-04 stsp
390 2d9e6abf 2022-05-04 stsp done:
391 2d9e6abf 2022-05-04 stsp got_deflate_end(&zb);
392 2d9e6abf 2022-05-04 stsp return err;
393 2d9e6abf 2022-05-04 stsp }
394 2d9e6abf 2022-05-04 stsp
395 2d9e6abf 2022-05-04 stsp const struct got_error *
396 2d9e6abf 2022-05-04 stsp got_deflate_append_to_file_mmap(struct got_deflate_buf *zb, off_t *outlen,
397 2d9e6abf 2022-05-04 stsp uint8_t *map, size_t offset, size_t len, FILE *outfile,
398 2d9e6abf 2022-05-04 stsp struct got_deflate_checksum *csum)
399 2d9e6abf 2022-05-04 stsp {
400 2d9e6abf 2022-05-04 stsp const struct got_error *err;
401 2d9e6abf 2022-05-04 stsp size_t avail, consumed;
402 2d9e6abf 2022-05-04 stsp
403 2d9e6abf 2022-05-04 stsp do {
404 2d9e6abf 2022-05-04 stsp err = deflate_read_mmap(zb, map, offset, len, &avail,
405 2d9e6abf 2022-05-04 stsp &consumed, 0);
406 2d9e6abf 2022-05-04 stsp if (err)
407 2d9e6abf 2022-05-04 stsp break;
408 2d9e6abf 2022-05-04 stsp offset += consumed;
409 2d9e6abf 2022-05-04 stsp len -= consumed;
410 2d9e6abf 2022-05-04 stsp if (avail > 0) {
411 2d9e6abf 2022-05-04 stsp size_t n;
412 2d9e6abf 2022-05-04 stsp n = fwrite(zb->outbuf, avail, 1, outfile);
413 2d9e6abf 2022-05-04 stsp if (n != 1) {
414 2d9e6abf 2022-05-04 stsp err = got_ferror(outfile, GOT_ERR_IO);
415 2d9e6abf 2022-05-04 stsp break;
416 2d9e6abf 2022-05-04 stsp }
417 2d9e6abf 2022-05-04 stsp if (csum)
418 2d9e6abf 2022-05-04 stsp csum_output(csum, zb->outbuf, avail);
419 2d9e6abf 2022-05-04 stsp if (outlen)
420 2d9e6abf 2022-05-04 stsp *outlen += avail;
421 2d9e6abf 2022-05-04 stsp }
422 2d9e6abf 2022-05-04 stsp } while ((zb->flags & GOT_DEFLATE_F_HAVE_MORE) && len > 0);
423 64a8571e 2022-01-07 stsp
424 2d9e6abf 2022-05-04 stsp return err;
425 2d9e6abf 2022-05-04 stsp }
426 2d9e6abf 2022-05-04 stsp
427 2d9e6abf 2022-05-04 stsp const struct got_error *
428 2d9e6abf 2022-05-04 stsp got_deflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
429 2d9e6abf 2022-05-04 stsp size_t *consumed_total, struct got_deflate_checksum *csum, uint8_t *map,
430 2d9e6abf 2022-05-04 stsp size_t offset, size_t len)
431 2d9e6abf 2022-05-04 stsp {
432 2d9e6abf 2022-05-04 stsp const struct got_error *err;
433 2d9e6abf 2022-05-04 stsp size_t avail, consumed;
434 2d9e6abf 2022-05-04 stsp struct got_deflate_buf zb;
435 2d9e6abf 2022-05-04 stsp void *newbuf;
436 2d9e6abf 2022-05-04 stsp size_t nbuf = 1;
437 2d9e6abf 2022-05-04 stsp
438 2d9e6abf 2022-05-04 stsp if (outbuf) {
439 2d9e6abf 2022-05-04 stsp *outbuf = malloc(GOT_DEFLATE_BUFSIZE);
440 2d9e6abf 2022-05-04 stsp if (*outbuf == NULL)
441 2d9e6abf 2022-05-04 stsp return got_error_from_errno("malloc");
442 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, *outbuf, GOT_DEFLATE_BUFSIZE);
443 2d9e6abf 2022-05-04 stsp if (err) {
444 2d9e6abf 2022-05-04 stsp free(*outbuf);
445 2d9e6abf 2022-05-04 stsp *outbuf = NULL;
446 2d9e6abf 2022-05-04 stsp return err;
447 2d9e6abf 2022-05-04 stsp }
448 2d9e6abf 2022-05-04 stsp } else {
449 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
450 2d9e6abf 2022-05-04 stsp if (err)
451 2d9e6abf 2022-05-04 stsp return err;
452 2d9e6abf 2022-05-04 stsp }
453 2d9e6abf 2022-05-04 stsp
454 2d9e6abf 2022-05-04 stsp *outlen = 0;
455 2d9e6abf 2022-05-04 stsp if (consumed_total)
456 2d9e6abf 2022-05-04 stsp *consumed_total = 0;
457 2d9e6abf 2022-05-04 stsp do {
458 2d9e6abf 2022-05-04 stsp err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
459 2d9e6abf 2022-05-04 stsp &consumed);
460 2d9e6abf 2022-05-04 stsp if (err)
461 2d9e6abf 2022-05-04 stsp goto done;
462 2d9e6abf 2022-05-04 stsp offset += consumed;
463 2d9e6abf 2022-05-04 stsp if (consumed_total)
464 2d9e6abf 2022-05-04 stsp *consumed_total += consumed;
465 2d9e6abf 2022-05-04 stsp len -= consumed;
466 2d9e6abf 2022-05-04 stsp if (avail > 0 && csum)
467 2d9e6abf 2022-05-04 stsp csum_output(csum, zb.outbuf, avail);
468 2d9e6abf 2022-05-04 stsp *outlen += avail;
469 2d9e6abf 2022-05-04 stsp if ((zb.flags & GOT_DEFLATE_F_HAVE_MORE) && outbuf != NULL) {
470 2d9e6abf 2022-05-04 stsp newbuf = reallocarray(*outbuf, ++nbuf,
471 2d9e6abf 2022-05-04 stsp GOT_DEFLATE_BUFSIZE);
472 2d9e6abf 2022-05-04 stsp if (newbuf == NULL) {
473 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("reallocarray");
474 2d9e6abf 2022-05-04 stsp free(*outbuf);
475 2d9e6abf 2022-05-04 stsp *outbuf = NULL;
476 2d9e6abf 2022-05-04 stsp *outlen = 0;
477 2d9e6abf 2022-05-04 stsp goto done;
478 2d9e6abf 2022-05-04 stsp }
479 2d9e6abf 2022-05-04 stsp *outbuf = newbuf;
480 2d9e6abf 2022-05-04 stsp zb.outbuf = newbuf + *outlen;
481 2d9e6abf 2022-05-04 stsp zb.outlen = (nbuf * GOT_DEFLATE_BUFSIZE) - *outlen;
482 2d9e6abf 2022-05-04 stsp }
483 2d9e6abf 2022-05-04 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
484 64a8571e 2022-01-07 stsp done:
485 64a8571e 2022-01-07 stsp got_deflate_end(&zb);
486 64a8571e 2022-01-07 stsp return err;
487 64a8571e 2022-01-07 stsp }