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.
18 2181e0c8 2019-03-19 stsp #include <errno.h>
19 2181e0c8 2019-03-19 stsp #include <stdio.h>
20 2181e0c8 2019-03-19 stsp #include <stdlib.h>
21 2181e0c8 2019-03-19 stsp #include <string.h>
22 2181e0c8 2019-03-19 stsp #include <zlib.h>
23 2181e0c8 2019-03-19 stsp #include <time.h>
25 2181e0c8 2019-03-19 stsp #include "got_error.h"
26 2181e0c8 2019-03-19 stsp #include "got_object.h"
27 324d37e7 2019-05-11 stsp #include "got_path.h"
29 2181e0c8 2019-03-19 stsp #include "got_lib_deflate.h"
32 2181e0c8 2019-03-19 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 2181e0c8 2019-03-19 stsp const struct got_error *
36 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 const struct got_error *err = NULL;
41 2181e0c8 2019-03-19 stsp memset(&zb->z, 0, sizeof(zb->z));
43 2181e0c8 2019-03-19 stsp zb->z.zalloc = Z_NULL;
44 2181e0c8 2019-03-19 stsp zb->z.zfree = Z_NULL;
45 2181e0c8 2019-03-19 stsp zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
46 2181e0c8 2019-03-19 stsp if (zerr != Z_OK) {
47 2181e0c8 2019-03-19 stsp if (zerr == Z_ERRNO)
48 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
49 2181e0c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
50 2181e0c8 2019-03-19 stsp errno = ENOMEM;
51 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
53 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
56 2181e0c8 2019-03-19 stsp zb->inlen = zb->outlen = bufsize;
58 2181e0c8 2019-03-19 stsp zb->inbuf = calloc(1, zb->inlen);
59 2181e0c8 2019-03-19 stsp if (zb->inbuf == NULL) {
60 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
64 2181e0c8 2019-03-19 stsp zb->flags = 0;
65 2181e0c8 2019-03-19 stsp if (outbuf == NULL) {
66 2181e0c8 2019-03-19 stsp zb->outbuf = calloc(1, zb->outlen);
67 2181e0c8 2019-03-19 stsp if (zb->outbuf == NULL) {
68 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
71 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
73 2181e0c8 2019-03-19 stsp zb->outbuf = outbuf;
76 2181e0c8 2019-03-19 stsp got_deflate_end(zb);
81 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 if (csum->output_crc)
84 91b40e30 2021-05-21 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
86 91b40e30 2021-05-21 stsp if (csum->output_sha1)
87 91b40e30 2021-05-21 stsp SHA1Update(csum->output_sha1, buf, len);
90 2181e0c8 2019-03-19 stsp const struct got_error *
91 2181e0c8 2019-03-19 stsp got_deflate_read(struct got_deflate_buf *zb, FILE *f, size_t *outlenp)
93 2181e0c8 2019-03-19 stsp size_t last_total_out = zb->z.total_out;
94 2181e0c8 2019-03-19 stsp z_stream *z = &zb->z;
95 2181e0c8 2019-03-19 stsp int ret = Z_ERRNO;
97 2181e0c8 2019-03-19 stsp z->next_out = zb->outbuf;
98 2181e0c8 2019-03-19 stsp z->avail_out = zb->outlen;
100 2181e0c8 2019-03-19 stsp *outlenp = 0;
102 2181e0c8 2019-03-19 stsp if (z->avail_in == 0) {
103 2181e0c8 2019-03-19 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
104 2181e0c8 2019-03-19 stsp if (n == 0) {
105 2181e0c8 2019-03-19 stsp if (ferror(f))
106 2181e0c8 2019-03-19 stsp return got_ferror(f, GOT_ERR_IO);
108 4e4a7005 2019-04-13 stsp ret = deflate(z, Z_FINISH);
111 2181e0c8 2019-03-19 stsp z->next_in = zb->inbuf;
112 2181e0c8 2019-03-19 stsp z->avail_in = n;
114 2181e0c8 2019-03-19 stsp ret = deflate(z, Z_NO_FLUSH);
115 2181e0c8 2019-03-19 stsp } while (ret == Z_OK && z->avail_out > 0);
117 2181e0c8 2019-03-19 stsp if (ret == Z_OK) {
118 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
120 2181e0c8 2019-03-19 stsp if (ret != Z_STREAM_END)
121 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
122 2181e0c8 2019-03-19 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
125 2181e0c8 2019-03-19 stsp *outlenp = z->total_out - last_total_out;
126 2181e0c8 2019-03-19 stsp return NULL;
129 2b0ae357 2022-01-10 thomas const struct got_error *
130 2b0ae357 2022-01-10 thomas got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
131 2b0ae357 2022-01-10 thomas size_t len, size_t *outlenp, size_t *consumed)
133 2b0ae357 2022-01-10 thomas z_stream *z = &zb->z;
134 2b0ae357 2022-01-10 thomas size_t last_total_out = z->total_out;
135 2b0ae357 2022-01-10 thomas int ret = Z_ERRNO;
137 2b0ae357 2022-01-10 thomas z->next_out = zb->outbuf;
138 2b0ae357 2022-01-10 thomas z->avail_out = zb->outlen;
140 2b0ae357 2022-01-10 thomas *outlenp = 0;
141 2b0ae357 2022-01-10 thomas *consumed = 0;
143 2b0ae357 2022-01-10 thomas size_t last_total_in = z->total_in;
144 2b0ae357 2022-01-10 thomas if (z->avail_in == 0) {
145 2b0ae357 2022-01-10 thomas z->next_in = map + offset + *consumed;
146 2b0ae357 2022-01-10 thomas z->avail_in = len - *consumed;
147 2b0ae357 2022-01-10 thomas if (z->avail_in == 0) {
148 2b0ae357 2022-01-10 thomas /* EOF */
149 2b0ae357 2022-01-10 thomas ret = deflate(z, Z_FINISH);
153 2b0ae357 2022-01-10 thomas ret = deflate(z, Z_NO_FLUSH);
154 2b0ae357 2022-01-10 thomas *consumed += z->total_in - last_total_in;
155 2b0ae357 2022-01-10 thomas } while (ret == Z_OK && z->avail_out > 0);
157 2b0ae357 2022-01-10 thomas if (ret == Z_OK) {
158 2b0ae357 2022-01-10 thomas zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
160 2b0ae357 2022-01-10 thomas if (ret != Z_STREAM_END)
161 2b0ae357 2022-01-10 thomas return got_error(GOT_ERR_COMPRESSION);
162 2b0ae357 2022-01-10 thomas zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
165 2b0ae357 2022-01-10 thomas *outlenp = z->total_out - last_total_out;
166 2b0ae357 2022-01-10 thomas return NULL;
170 2181e0c8 2019-03-19 stsp got_deflate_end(struct got_deflate_buf *zb)
172 2181e0c8 2019-03-19 stsp free(zb->inbuf);
173 2181e0c8 2019-03-19 stsp if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
174 2181e0c8 2019-03-19 stsp free(zb->outbuf);
175 2181e0c8 2019-03-19 stsp deflateEnd(&zb->z);
178 2181e0c8 2019-03-19 stsp const struct got_error *
179 91b40e30 2021-05-21 stsp got_deflate_to_file(size_t *outlen, FILE *infile, FILE *outfile,
180 91b40e30 2021-05-21 stsp struct got_deflate_checksum *csum)
182 2181e0c8 2019-03-19 stsp const struct got_error *err;
183 2181e0c8 2019-03-19 stsp size_t avail;
184 2181e0c8 2019-03-19 stsp struct got_deflate_buf zb;
186 3b9e6fcf 2021-06-05 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
190 2181e0c8 2019-03-19 stsp *outlen = 0;
193 2181e0c8 2019-03-19 stsp err = got_deflate_read(&zb, infile, &avail);
196 2181e0c8 2019-03-19 stsp if (avail > 0) {
198 2181e0c8 2019-03-19 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
199 2181e0c8 2019-03-19 stsp if (n != 1) {
200 2181e0c8 2019-03-19 stsp err = got_ferror(outfile, GOT_ERR_IO);
204 3b9e6fcf 2021-06-05 stsp csum_output(csum, zb.outbuf, avail);
205 2181e0c8 2019-03-19 stsp *outlen += avail;
207 2181e0c8 2019-03-19 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
210 2181e0c8 2019-03-19 stsp got_deflate_end(&zb);
211 2181e0c8 2019-03-19 stsp return err;
214 2b0ae357 2022-01-10 thomas const struct got_error *
215 2b0ae357 2022-01-10 thomas got_deflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
216 2b0ae357 2022-01-10 thomas size_t len, FILE *outfile, struct got_deflate_checksum *csum)
218 2b0ae357 2022-01-10 thomas const struct got_error *err;
219 2b0ae357 2022-01-10 thomas size_t avail, consumed;
220 2b0ae357 2022-01-10 thomas struct got_deflate_buf zb;
222 2b0ae357 2022-01-10 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
224 2b0ae357 2022-01-10 thomas goto done;
226 2b0ae357 2022-01-10 thomas *outlen = 0;
228 2b0ae357 2022-01-10 thomas err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
229 2b0ae357 2022-01-10 thomas &consumed);
231 2b0ae357 2022-01-10 thomas goto done;
232 2b0ae357 2022-01-10 thomas offset += consumed;
233 2b0ae357 2022-01-10 thomas len -= consumed;
234 2b0ae357 2022-01-10 thomas if (avail > 0) {
235 2b0ae357 2022-01-10 thomas size_t n;
236 2b0ae357 2022-01-10 thomas n = fwrite(zb.outbuf, avail, 1, outfile);
237 2b0ae357 2022-01-10 thomas if (n != 1) {
238 2b0ae357 2022-01-10 thomas err = got_ferror(outfile, GOT_ERR_IO);
239 2b0ae357 2022-01-10 thomas goto done;
241 2b0ae357 2022-01-10 thomas if (csum)
242 2b0ae357 2022-01-10 thomas csum_output(csum, zb.outbuf, avail);
243 2b0ae357 2022-01-10 thomas *outlen += avail;
245 2b0ae357 2022-01-10 thomas } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
248 2b0ae357 2022-01-10 thomas got_deflate_end(&zb);
249 2b0ae357 2022-01-10 thomas return err;