2 2181e0c8 2019-03-19 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 2181e0c8 2019-03-19 stsp #include <sys/queue.h>
19 2181e0c8 2019-03-19 stsp #include <errno.h>
20 2181e0c8 2019-03-19 stsp #include <stdio.h>
21 2181e0c8 2019-03-19 stsp #include <stdlib.h>
22 2181e0c8 2019-03-19 stsp #include <string.h>
23 2181e0c8 2019-03-19 stsp #include <sha1.h>
24 2181e0c8 2019-03-19 stsp #include <zlib.h>
25 2181e0c8 2019-03-19 stsp #include <time.h>
27 2181e0c8 2019-03-19 stsp #include "got_error.h"
28 2181e0c8 2019-03-19 stsp #include "got_object.h"
29 324d37e7 2019-05-11 stsp #include "got_path.h"
31 2181e0c8 2019-03-19 stsp #include "got_lib_deflate.h"
34 2181e0c8 2019-03-19 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 2181e0c8 2019-03-19 stsp const struct got_error *
38 3b9e6fcf 2021-06-05 stsp got_deflate_init(struct got_deflate_buf *zb, uint8_t *outbuf, size_t bufsize)
40 2181e0c8 2019-03-19 stsp const struct got_error *err = NULL;
43 2181e0c8 2019-03-19 stsp memset(&zb->z, 0, sizeof(zb->z));
45 2181e0c8 2019-03-19 stsp zb->z.zalloc = Z_NULL;
46 2181e0c8 2019-03-19 stsp zb->z.zfree = Z_NULL;
47 2181e0c8 2019-03-19 stsp zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
48 2181e0c8 2019-03-19 stsp if (zerr != Z_OK) {
49 2181e0c8 2019-03-19 stsp if (zerr == Z_ERRNO)
50 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
51 2181e0c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
52 2181e0c8 2019-03-19 stsp errno = ENOMEM;
53 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
55 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
58 2181e0c8 2019-03-19 stsp zb->inlen = zb->outlen = bufsize;
60 2181e0c8 2019-03-19 stsp zb->inbuf = calloc(1, zb->inlen);
61 2181e0c8 2019-03-19 stsp if (zb->inbuf == NULL) {
62 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
66 2181e0c8 2019-03-19 stsp zb->flags = 0;
67 2181e0c8 2019-03-19 stsp if (outbuf == NULL) {
68 2181e0c8 2019-03-19 stsp zb->outbuf = calloc(1, zb->outlen);
69 2181e0c8 2019-03-19 stsp if (zb->outbuf == NULL) {
70 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
73 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
75 2181e0c8 2019-03-19 stsp zb->outbuf = outbuf;
78 2181e0c8 2019-03-19 stsp got_deflate_end(zb);
83 31e61ec1 2021-09-28 naddy csum_output(struct got_deflate_checksum *csum, const uint8_t *buf, size_t len)
85 91b40e30 2021-05-21 stsp if (csum->output_crc)
86 91b40e30 2021-05-21 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
88 91b40e30 2021-05-21 stsp if (csum->output_sha1)
89 91b40e30 2021-05-21 stsp SHA1Update(csum->output_sha1, buf, len);
92 2181e0c8 2019-03-19 stsp const struct got_error *
93 72840534 2022-01-19 stsp got_deflate_read(struct got_deflate_buf *zb, FILE *f, off_t len,
94 72840534 2022-01-19 stsp size_t *outlenp, off_t *consumed)
96 2181e0c8 2019-03-19 stsp size_t last_total_out = zb->z.total_out;
97 2181e0c8 2019-03-19 stsp z_stream *z = &zb->z;
98 2181e0c8 2019-03-19 stsp int ret = Z_ERRNO;
100 2181e0c8 2019-03-19 stsp z->next_out = zb->outbuf;
101 2181e0c8 2019-03-19 stsp z->avail_out = zb->outlen;
103 2181e0c8 2019-03-19 stsp *outlenp = 0;
104 72840534 2022-01-19 stsp *consumed = 0;
106 72840534 2022-01-19 stsp size_t last_total_in = z->total_in;
107 2181e0c8 2019-03-19 stsp if (z->avail_in == 0) {
108 72840534 2022-01-19 stsp size_t n = 0;
109 72840534 2022-01-19 stsp if (*consumed < len) {
110 72840534 2022-01-19 stsp n = fread(zb->inbuf, 1,
111 72840534 2022-01-19 stsp MIN(zb->inlen, len - *consumed), f);
113 2181e0c8 2019-03-19 stsp if (n == 0) {
114 2181e0c8 2019-03-19 stsp if (ferror(f))
115 2181e0c8 2019-03-19 stsp return got_ferror(f, GOT_ERR_IO);
117 4e4a7005 2019-04-13 stsp ret = deflate(z, Z_FINISH);
120 2181e0c8 2019-03-19 stsp z->next_in = zb->inbuf;
121 2181e0c8 2019-03-19 stsp z->avail_in = n;
123 2181e0c8 2019-03-19 stsp ret = deflate(z, Z_NO_FLUSH);
124 72840534 2022-01-19 stsp *consumed += z->total_in - last_total_in;
125 2181e0c8 2019-03-19 stsp } while (ret == Z_OK && z->avail_out > 0);
127 2181e0c8 2019-03-19 stsp if (ret == Z_OK) {
128 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
130 2181e0c8 2019-03-19 stsp if (ret != Z_STREAM_END)
131 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
132 2181e0c8 2019-03-19 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
135 2181e0c8 2019-03-19 stsp *outlenp = z->total_out - last_total_out;
136 2181e0c8 2019-03-19 stsp return NULL;
139 64a8571e 2022-01-07 stsp const struct got_error *
140 64a8571e 2022-01-07 stsp got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
141 64a8571e 2022-01-07 stsp size_t len, size_t *outlenp, size_t *consumed)
143 64a8571e 2022-01-07 stsp z_stream *z = &zb->z;
144 64a8571e 2022-01-07 stsp size_t last_total_out = z->total_out;
145 64a8571e 2022-01-07 stsp int ret = Z_ERRNO;
147 64a8571e 2022-01-07 stsp z->next_out = zb->outbuf;
148 64a8571e 2022-01-07 stsp z->avail_out = zb->outlen;
150 64a8571e 2022-01-07 stsp *outlenp = 0;
151 64a8571e 2022-01-07 stsp *consumed = 0;
153 64a8571e 2022-01-07 stsp size_t last_total_in = z->total_in;
154 64a8571e 2022-01-07 stsp if (z->avail_in == 0) {
155 64a8571e 2022-01-07 stsp z->next_in = map + offset + *consumed;
156 a9bd296d 2022-02-08 stsp if (len - *consumed > UINT_MAX)
157 a9bd296d 2022-02-08 stsp z->avail_in = UINT_MAX;
159 a9bd296d 2022-02-08 stsp z->avail_in = len - *consumed;
160 64a8571e 2022-01-07 stsp if (z->avail_in == 0) {
162 64a8571e 2022-01-07 stsp ret = deflate(z, Z_FINISH);
166 64a8571e 2022-01-07 stsp ret = deflate(z, Z_NO_FLUSH);
167 64a8571e 2022-01-07 stsp *consumed += z->total_in - last_total_in;
168 64a8571e 2022-01-07 stsp } while (ret == Z_OK && z->avail_out > 0);
170 64a8571e 2022-01-07 stsp if (ret == Z_OK) {
171 64a8571e 2022-01-07 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
173 64a8571e 2022-01-07 stsp if (ret != Z_STREAM_END)
174 64a8571e 2022-01-07 stsp return got_error(GOT_ERR_COMPRESSION);
175 64a8571e 2022-01-07 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
178 64a8571e 2022-01-07 stsp *outlenp = z->total_out - last_total_out;
179 64a8571e 2022-01-07 stsp return NULL;
183 2181e0c8 2019-03-19 stsp got_deflate_end(struct got_deflate_buf *zb)
185 2181e0c8 2019-03-19 stsp free(zb->inbuf);
186 2181e0c8 2019-03-19 stsp if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
187 2181e0c8 2019-03-19 stsp free(zb->outbuf);
188 2181e0c8 2019-03-19 stsp deflateEnd(&zb->z);
191 2181e0c8 2019-03-19 stsp const struct got_error *
192 72840534 2022-01-19 stsp got_deflate_to_file(off_t *outlen, FILE *infile, off_t len,
193 72840534 2022-01-19 stsp FILE *outfile, struct got_deflate_checksum *csum)
195 2181e0c8 2019-03-19 stsp const struct got_error *err;
196 2181e0c8 2019-03-19 stsp size_t avail;
197 72840534 2022-01-19 stsp off_t consumed;
198 2181e0c8 2019-03-19 stsp struct got_deflate_buf zb;
200 3b9e6fcf 2021-06-05 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
204 2181e0c8 2019-03-19 stsp *outlen = 0;
207 72840534 2022-01-19 stsp err = got_deflate_read(&zb, infile, len, &avail, &consumed);
210 72840534 2022-01-19 stsp len -= consumed;
211 2181e0c8 2019-03-19 stsp if (avail > 0) {
213 2181e0c8 2019-03-19 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
214 2181e0c8 2019-03-19 stsp if (n != 1) {
215 2181e0c8 2019-03-19 stsp err = got_ferror(outfile, GOT_ERR_IO);
219 3b9e6fcf 2021-06-05 stsp csum_output(csum, zb.outbuf, avail);
220 2181e0c8 2019-03-19 stsp *outlen += avail;
222 2181e0c8 2019-03-19 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
225 2181e0c8 2019-03-19 stsp got_deflate_end(&zb);
226 2181e0c8 2019-03-19 stsp return err;
229 64a8571e 2022-01-07 stsp const struct got_error *
230 72840534 2022-01-19 stsp got_deflate_to_file_mmap(off_t *outlen, uint8_t *map, size_t offset,
231 64a8571e 2022-01-07 stsp size_t len, FILE *outfile, struct got_deflate_checksum *csum)
233 64a8571e 2022-01-07 stsp const struct got_error *err;
234 64a8571e 2022-01-07 stsp size_t avail, consumed;
235 64a8571e 2022-01-07 stsp struct got_deflate_buf zb;
237 64a8571e 2022-01-07 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
241 64a8571e 2022-01-07 stsp *outlen = 0;
243 64a8571e 2022-01-07 stsp err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
244 64a8571e 2022-01-07 stsp &consumed);
247 64a8571e 2022-01-07 stsp offset += consumed;
248 64a8571e 2022-01-07 stsp len -= consumed;
249 64a8571e 2022-01-07 stsp if (avail > 0) {
251 64a8571e 2022-01-07 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
252 64a8571e 2022-01-07 stsp if (n != 1) {
253 64a8571e 2022-01-07 stsp err = got_ferror(outfile, GOT_ERR_IO);
257 64a8571e 2022-01-07 stsp csum_output(csum, zb.outbuf, avail);
258 64a8571e 2022-01-07 stsp *outlen += avail;
260 64a8571e 2022-01-07 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
263 64a8571e 2022-01-07 stsp got_deflate_end(&zb);
264 64a8571e 2022-01-07 stsp return err;