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