/src/libzip/lib/zip_source_crc.c
Line | Count | Source |
1 | | /* |
2 | | zip_source_crc.c -- pass-through source that calculates CRC32 and size |
3 | | Copyright (C) 2009-2025 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | |
35 | | #include <limits.h> |
36 | | #include <stdlib.h> |
37 | | #include <zlib.h> |
38 | | |
39 | | #include "zipint.h" |
40 | | |
41 | | struct crc_context { |
42 | | int validate; /* whether to check CRC on EOF and return error on mismatch */ |
43 | | int crc_complete; /* whether CRC was computed for complete file */ |
44 | | zip_error_t error; |
45 | | zip_uint64_t size; |
46 | | zip_uint64_t position; /* current reading position */ |
47 | | zip_uint64_t crc_position; /* how far we've computed the CRC */ |
48 | | zip_uint32_t crc; |
49 | | }; |
50 | | |
51 | | static void crc_context_free(struct crc_context *ctx); |
52 | | static struct crc_context *crc_context_new(void); |
53 | | static void crc_context_read_init(struct crc_context *ctx); |
54 | | static zip_int64_t crc_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t); |
55 | | static zip_int64_t validate_crc(struct crc_context *ctx, zip_source_t *src); |
56 | | |
57 | 87.2k | zip_source_t *zip_source_crc_create(zip_source_t *src, int validate, zip_error_t *error) { |
58 | 87.2k | struct crc_context *ctx; |
59 | 87.2k | zip_source_t *new_src; |
60 | | |
61 | 87.2k | if (src == NULL) { |
62 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
63 | 0 | return NULL; |
64 | 0 | } |
65 | | |
66 | 87.2k | if ((ctx = crc_context_new()) == NULL) { |
67 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
68 | 0 | return NULL; |
69 | 0 | } |
70 | 87.2k | ctx->validate = validate; |
71 | | |
72 | 87.2k | new_src = zip_source_layered_create(src, crc_read, ctx, error); |
73 | 87.2k | if (new_src == NULL) { |
74 | 0 | crc_context_free(ctx); |
75 | 0 | return NULL; |
76 | 0 | } |
77 | 87.2k | return new_src; |
78 | 87.2k | } |
79 | | |
80 | | |
81 | 458k | static zip_int64_t crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { |
82 | 458k | struct crc_context *ctx; |
83 | 458k | zip_int64_t n; |
84 | | |
85 | 458k | ctx = (struct crc_context *)_ctx; |
86 | | |
87 | 458k | switch (cmd) { |
88 | 59.1k | case ZIP_SOURCE_OPEN: |
89 | 59.1k | crc_context_read_init(ctx); |
90 | 59.1k | return 0; |
91 | | |
92 | 94.8k | case ZIP_SOURCE_READ: |
93 | 94.8k | if ((n = zip_source_read(src, data, len)) < 0) { |
94 | 7.32k | zip_error_set_from_source(&ctx->error, src); |
95 | 7.32k | return -1; |
96 | 7.32k | } |
97 | | |
98 | 87.5k | if (n == 0) { |
99 | 51.8k | return validate_crc(ctx, src); |
100 | 51.8k | } |
101 | 35.6k | else { |
102 | | /* |
103 | | We can only compute the CRC data in the correct order. So we update the CRC for data read this time that starts at ctx->crc_position. |
104 | | |
105 | | If we already computed the CRC for the whole file, or if there is a gap between ctx->crc_position and the start of new data, or if we already computed the CRC for all new data read, we don't update the CRC. |
106 | | |
107 | | ctx->position is the position of the first byte in data. |
108 | | |
109 | | ctx->crc_position is the position of the first byte in data that has not been included in the CRC yet. |
110 | | |
111 | | Therefore, we want to compute the CRC for data[ctx->crc_position - ctx->position .. n-1]. |
112 | | |
113 | | */ |
114 | 35.6k | if (!ctx->crc_complete && ctx->position <= ctx->crc_position && ctx->crc_position < ctx->position + (zip_uint64_t)n) { |
115 | 35.6k | zip_uint64_t i, nn; |
116 | | |
117 | 71.3k | for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) { |
118 | 35.6k | nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n - i); |
119 | | |
120 | 35.6k | ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data + i, (uInt)nn); |
121 | 35.6k | ctx->crc_position += nn; |
122 | 35.6k | } |
123 | 35.6k | } |
124 | 35.6k | } |
125 | 35.6k | ctx->position += (zip_uint64_t)n; |
126 | 35.6k | return n; |
127 | | |
128 | 59.1k | case ZIP_SOURCE_CLOSE: { |
129 | 59.1k | zip_int64_t ret = zip_source_at_eof(src); |
130 | 59.1k | if (ret < 0) { |
131 | 6.82k | zip_error_set_from_source(&ctx->error, src); |
132 | 6.82k | return -1; |
133 | 6.82k | } |
134 | 52.3k | else if (ret == 1) { |
135 | 51.9k | return validate_crc(ctx, src); |
136 | 51.9k | } |
137 | 412 | else { |
138 | 412 | return 0; |
139 | 412 | } |
140 | 59.1k | } |
141 | | |
142 | 36.4k | case ZIP_SOURCE_STAT: { |
143 | 36.4k | zip_stat_t *st; |
144 | | |
145 | 36.4k | st = (zip_stat_t *)data; |
146 | | |
147 | 36.4k | if (ctx->crc_complete) { |
148 | 22.7k | if ((st->valid & ZIP_STAT_SIZE) && st->size != ctx->size) { |
149 | 0 | zip_error_set(&ctx->error, ZIP_ER_DATA_LENGTH, 0); |
150 | 0 | return -1; |
151 | 0 | } |
152 | | /* TODO: Set comp_size, comp_method, encryption_method? |
153 | | After all, this only works for uncompressed data. */ |
154 | 22.7k | st->size = ctx->size; |
155 | 22.7k | st->crc = ctx->crc; |
156 | 22.7k | st->comp_size = ctx->size; |
157 | 22.7k | st->comp_method = ZIP_CM_STORE; |
158 | 22.7k | st->encryption_method = ZIP_EM_NONE; |
159 | 22.7k | st->valid |= ZIP_STAT_SIZE | ZIP_STAT_CRC | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; |
160 | 22.7k | } |
161 | 36.4k | return 0; |
162 | 36.4k | } |
163 | | |
164 | 34.0k | case ZIP_SOURCE_ERROR: |
165 | 34.0k | return zip_error_to_data(&ctx->error, data, len); |
166 | | |
167 | 87.2k | case ZIP_SOURCE_FREE: |
168 | 87.2k | crc_context_free(ctx); |
169 | 87.2k | return 0; |
170 | | |
171 | 87.2k | case ZIP_SOURCE_SUPPORTS: { |
172 | 87.2k | zip_int64_t mask = zip_source_supports(src); |
173 | | |
174 | 87.2k | if (mask < 0) { |
175 | 0 | zip_error_set_from_source(&ctx->error, src); |
176 | 0 | return -1; |
177 | 0 | } |
178 | | |
179 | 87.2k | mask &= ~zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, ZIP_SOURCE_GET_FILE_ATTRIBUTES, -1); |
180 | 87.2k | mask |= zip_source_make_command_bitmap(ZIP_SOURCE_FREE, -1); |
181 | 87.2k | return mask; |
182 | 87.2k | } |
183 | | |
184 | 0 | case ZIP_SOURCE_SEEK: { |
185 | 0 | zip_int64_t new_position; |
186 | 0 | zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); |
187 | |
|
188 | 0 | if (args == NULL) { |
189 | 0 | return -1; |
190 | 0 | } |
191 | 0 | if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) { |
192 | 0 | zip_error_set_from_source(&ctx->error, src); |
193 | 0 | return -1; |
194 | 0 | } |
195 | | |
196 | 0 | ctx->position = (zip_uint64_t)new_position; |
197 | |
|
198 | 0 | return 0; |
199 | 0 | } |
200 | | |
201 | 0 | case ZIP_SOURCE_TELL: |
202 | 0 | return (zip_int64_t)ctx->position; |
203 | | |
204 | 0 | default: |
205 | 0 | return zip_source_pass_to_lower_layer(src, data, len, cmd); |
206 | 458k | } |
207 | 458k | } |
208 | | |
209 | 103k | zip_int64_t validate_crc(struct crc_context *ctx, zip_source_t *src) { |
210 | 103k | struct zip_stat st; |
211 | | |
212 | 103k | ctx->size = ctx->position; |
213 | | |
214 | 103k | if (ctx->validate) { |
215 | 58.3k | if (zip_source_stat(src, &st) < 0) { |
216 | 0 | zip_error_set_from_source(&ctx->error, src); |
217 | 0 | return -1; |
218 | 0 | } |
219 | 58.3k | } |
220 | | |
221 | 103k | if (ctx->crc_position == ctx->position) { |
222 | 103k | ctx->crc_complete = 1; |
223 | | |
224 | 103k | if (ctx->validate) { |
225 | 58.3k | if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) { |
226 | 16.6k | zip_error_set(&ctx->error, ZIP_ER_CRC, 0); |
227 | 16.6k | return -1; |
228 | 16.6k | } |
229 | 58.3k | } |
230 | 103k | } |
231 | | |
232 | 87.1k | if (ctx->validate) { |
233 | 41.7k | if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) { |
234 | | /* We don't have the index here, but the caller should know which file they are reading from. */ |
235 | 3.27k | zip_error_set(&ctx->error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_INVALID_FILE_LENGTH, MAX_DETAIL_INDEX)); |
236 | 3.27k | return -1; |
237 | 3.27k | } |
238 | 41.7k | } |
239 | | |
240 | 83.8k | return 0; |
241 | 87.1k | } |
242 | | |
243 | 87.2k | static struct crc_context *crc_context_new(void) { |
244 | 87.2k | struct crc_context *ctx; |
245 | | |
246 | 87.2k | if ((ctx = (struct crc_context *)malloc(sizeof(*ctx))) == NULL) { |
247 | 0 | return NULL; |
248 | 0 | } |
249 | | |
250 | 87.2k | zip_error_init(&ctx->error); |
251 | 87.2k | ctx->validate = 0; |
252 | 87.2k | crc_context_read_init(ctx); |
253 | | |
254 | 87.2k | return ctx; |
255 | 87.2k | } |
256 | | |
257 | 87.2k | static void crc_context_free(struct crc_context *ctx) { |
258 | 87.2k | if (ctx == NULL) { |
259 | 0 | return; |
260 | 0 | } |
261 | | |
262 | 87.2k | zip_error_fini(&ctx->error); |
263 | 87.2k | free(ctx); |
264 | 87.2k | } |
265 | | |
266 | 146k | static void crc_context_read_init(struct crc_context *ctx) { |
267 | 146k | ctx->position = 0; |
268 | 146k | ctx->crc_complete = 0; |
269 | 146k | ctx->crc_position = 0; |
270 | | ctx->crc = (zip_uint32_t)crc32(0, NULL, 0); |
271 | 146k | ctx->size = 0; |
272 | 146k | } |