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