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 2181e0c8 2019-03-19 stsp
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 <sha1.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 2181e0c8 2019-03-19 stsp
32 2181e0c8 2019-03-19 stsp #ifndef MIN
33 2181e0c8 2019-03-19 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
34 2181e0c8 2019-03-19 stsp #endif
35 2181e0c8 2019-03-19 stsp
36 2181e0c8 2019-03-19 stsp const struct got_error *
37 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 {
39 2181e0c8 2019-03-19 stsp const struct got_error *err = NULL;
40 2181e0c8 2019-03-19 stsp int zerr;
41 2181e0c8 2019-03-19 stsp
42 2181e0c8 2019-03-19 stsp memset(&zb->z, 0, sizeof(zb->z));
43 2181e0c8 2019-03-19 stsp
44 2181e0c8 2019-03-19 stsp zb->z.zalloc = Z_NULL;
45 2181e0c8 2019-03-19 stsp zb->z.zfree = Z_NULL;
46 2181e0c8 2019-03-19 stsp zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
47 2181e0c8 2019-03-19 stsp if (zerr != Z_OK) {
48 2181e0c8 2019-03-19 stsp if (zerr == Z_ERRNO)
49 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
50 2181e0c8 2019-03-19 stsp if (zerr == Z_MEM_ERROR) {
51 2181e0c8 2019-03-19 stsp errno = ENOMEM;
52 638f9024 2019-05-13 stsp return got_error_from_errno("deflateInit");
53 2181e0c8 2019-03-19 stsp }
54 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
55 2181e0c8 2019-03-19 stsp }
56 2181e0c8 2019-03-19 stsp
57 2181e0c8 2019-03-19 stsp zb->inlen = zb->outlen = bufsize;
58 2181e0c8 2019-03-19 stsp
59 2181e0c8 2019-03-19 stsp zb->inbuf = calloc(1, zb->inlen);
60 2181e0c8 2019-03-19 stsp if (zb->inbuf == NULL) {
61 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
62 2181e0c8 2019-03-19 stsp goto done;
63 2181e0c8 2019-03-19 stsp }
64 2181e0c8 2019-03-19 stsp
65 2181e0c8 2019-03-19 stsp zb->flags = 0;
66 2181e0c8 2019-03-19 stsp if (outbuf == NULL) {
67 2181e0c8 2019-03-19 stsp zb->outbuf = calloc(1, zb->outlen);
68 2181e0c8 2019-03-19 stsp if (zb->outbuf == 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 zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
73 2181e0c8 2019-03-19 stsp } else
74 2181e0c8 2019-03-19 stsp zb->outbuf = outbuf;
75 2181e0c8 2019-03-19 stsp done:
76 2181e0c8 2019-03-19 stsp if (err)
77 2181e0c8 2019-03-19 stsp got_deflate_end(zb);
78 2181e0c8 2019-03-19 stsp return err;
79 2181e0c8 2019-03-19 stsp }
80 2181e0c8 2019-03-19 stsp
81 91b40e30 2021-05-21 stsp static void
82 91b40e30 2021-05-21 stsp csum_output(struct got_deflate_checksum *csum, const char *buf, size_t len)
83 91b40e30 2021-05-21 stsp {
84 91b40e30 2021-05-21 stsp if (csum->output_crc)
85 91b40e30 2021-05-21 stsp *csum->output_crc = crc32(*csum->output_crc, buf, len);
86 91b40e30 2021-05-21 stsp
87 91b40e30 2021-05-21 stsp if (csum->output_sha1)
88 91b40e30 2021-05-21 stsp SHA1Update(csum->output_sha1, buf, len);
89 91b40e30 2021-05-21 stsp }
90 91b40e30 2021-05-21 stsp
91 2181e0c8 2019-03-19 stsp const struct got_error *
92 2181e0c8 2019-03-19 stsp got_deflate_read(struct got_deflate_buf *zb, FILE *f, size_t *outlenp)
93 2181e0c8 2019-03-19 stsp {
94 2181e0c8 2019-03-19 stsp size_t last_total_out = zb->z.total_out;
95 2181e0c8 2019-03-19 stsp z_stream *z = &zb->z;
96 2181e0c8 2019-03-19 stsp int ret = Z_ERRNO;
97 2181e0c8 2019-03-19 stsp
98 2181e0c8 2019-03-19 stsp z->next_out = zb->outbuf;
99 2181e0c8 2019-03-19 stsp z->avail_out = zb->outlen;
100 2181e0c8 2019-03-19 stsp
101 2181e0c8 2019-03-19 stsp *outlenp = 0;
102 2181e0c8 2019-03-19 stsp do {
103 2181e0c8 2019-03-19 stsp if (z->avail_in == 0) {
104 2181e0c8 2019-03-19 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
105 2181e0c8 2019-03-19 stsp if (n == 0) {
106 2181e0c8 2019-03-19 stsp if (ferror(f))
107 2181e0c8 2019-03-19 stsp return got_ferror(f, GOT_ERR_IO);
108 2181e0c8 2019-03-19 stsp /* EOF */
109 4e4a7005 2019-04-13 stsp ret = deflate(z, Z_FINISH);
110 2181e0c8 2019-03-19 stsp break;
111 2181e0c8 2019-03-19 stsp }
112 2181e0c8 2019-03-19 stsp z->next_in = zb->inbuf;
113 2181e0c8 2019-03-19 stsp z->avail_in = n;
114 2181e0c8 2019-03-19 stsp }
115 2181e0c8 2019-03-19 stsp ret = deflate(z, Z_NO_FLUSH);
116 2181e0c8 2019-03-19 stsp } while (ret == Z_OK && z->avail_out > 0);
117 2181e0c8 2019-03-19 stsp
118 2181e0c8 2019-03-19 stsp if (ret == Z_OK) {
119 2181e0c8 2019-03-19 stsp zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
120 2181e0c8 2019-03-19 stsp } else {
121 2181e0c8 2019-03-19 stsp if (ret != Z_STREAM_END)
122 2181e0c8 2019-03-19 stsp return got_error(GOT_ERR_COMPRESSION);
123 2181e0c8 2019-03-19 stsp zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
124 2181e0c8 2019-03-19 stsp }
125 2181e0c8 2019-03-19 stsp
126 2181e0c8 2019-03-19 stsp *outlenp = z->total_out - last_total_out;
127 2181e0c8 2019-03-19 stsp return NULL;
128 2181e0c8 2019-03-19 stsp }
129 2181e0c8 2019-03-19 stsp
130 2181e0c8 2019-03-19 stsp void
131 2181e0c8 2019-03-19 stsp got_deflate_end(struct got_deflate_buf *zb)
132 2181e0c8 2019-03-19 stsp {
133 2181e0c8 2019-03-19 stsp free(zb->inbuf);
134 2181e0c8 2019-03-19 stsp if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
135 2181e0c8 2019-03-19 stsp free(zb->outbuf);
136 2181e0c8 2019-03-19 stsp deflateEnd(&zb->z);
137 2181e0c8 2019-03-19 stsp }
138 2181e0c8 2019-03-19 stsp
139 2181e0c8 2019-03-19 stsp const struct got_error *
140 91b40e30 2021-05-21 stsp got_deflate_to_file(size_t *outlen, FILE *infile, FILE *outfile,
141 91b40e30 2021-05-21 stsp struct got_deflate_checksum *csum)
142 2181e0c8 2019-03-19 stsp {
143 2181e0c8 2019-03-19 stsp const struct got_error *err;
144 2181e0c8 2019-03-19 stsp size_t avail;
145 2181e0c8 2019-03-19 stsp struct got_deflate_buf zb;
146 2181e0c8 2019-03-19 stsp
147 3b9e6fcf 2021-06-05 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
148 2181e0c8 2019-03-19 stsp if (err)
149 2181e0c8 2019-03-19 stsp goto done;
150 2181e0c8 2019-03-19 stsp
151 2181e0c8 2019-03-19 stsp *outlen = 0;
152 2181e0c8 2019-03-19 stsp
153 2181e0c8 2019-03-19 stsp do {
154 2181e0c8 2019-03-19 stsp err = got_deflate_read(&zb, infile, &avail);
155 2181e0c8 2019-03-19 stsp if (err)
156 2181e0c8 2019-03-19 stsp goto done;
157 2181e0c8 2019-03-19 stsp if (avail > 0) {
158 2181e0c8 2019-03-19 stsp size_t n;
159 2181e0c8 2019-03-19 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
160 2181e0c8 2019-03-19 stsp if (n != 1) {
161 2181e0c8 2019-03-19 stsp err = got_ferror(outfile, GOT_ERR_IO);
162 2181e0c8 2019-03-19 stsp goto done;
163 2181e0c8 2019-03-19 stsp }
164 3b9e6fcf 2021-06-05 stsp if (csum)
165 3b9e6fcf 2021-06-05 stsp csum_output(csum, zb.outbuf, avail);
166 2181e0c8 2019-03-19 stsp *outlen += avail;
167 2181e0c8 2019-03-19 stsp }
168 2181e0c8 2019-03-19 stsp } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
169 2181e0c8 2019-03-19 stsp
170 2181e0c8 2019-03-19 stsp done:
171 2181e0c8 2019-03-19 stsp got_deflate_end(&zb);
172 2181e0c8 2019-03-19 stsp return err;
173 2181e0c8 2019-03-19 stsp }