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