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