/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-2023 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 zip_int64_t crc_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t); |
52 | | |
53 | | |
54 | | zip_source_t * |
55 | 43.1k | zip_source_crc_create(zip_source_t *src, int validate, zip_error_t *error) { |
56 | 43.1k | struct crc_context *ctx; |
57 | 43.1k | zip_source_t *new_src; |
58 | | |
59 | 43.1k | if (src == NULL) { |
60 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
61 | 0 | return NULL; |
62 | 0 | } |
63 | | |
64 | 43.1k | if ((ctx = (struct crc_context *)malloc(sizeof(*ctx))) == NULL) { |
65 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
66 | 0 | return NULL; |
67 | 0 | } |
68 | | |
69 | 43.1k | zip_error_init(&ctx->error); |
70 | 43.1k | ctx->validate = validate; |
71 | 43.1k | ctx->crc_complete = 0; |
72 | 43.1k | ctx->crc_position = 0; |
73 | 43.1k | ctx->crc = (zip_uint32_t)crc32(0, NULL, 0); |
74 | 43.1k | ctx->size = 0; |
75 | | |
76 | 43.1k | new_src = zip_source_layered_create(src, crc_read, ctx, error); |
77 | 43.1k | if (new_src == NULL) { |
78 | 0 | free(ctx); |
79 | 0 | return NULL; |
80 | 0 | } |
81 | 43.1k | return new_src; |
82 | 43.1k | } |
83 | | |
84 | | |
85 | | static zip_int64_t |
86 | 265k | crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { |
87 | 265k | struct crc_context *ctx; |
88 | 265k | zip_int64_t n; |
89 | | |
90 | 265k | ctx = (struct crc_context *)_ctx; |
91 | | |
92 | 265k | switch (cmd) { |
93 | 34.2k | case ZIP_SOURCE_OPEN: |
94 | 34.2k | ctx->position = 0; |
95 | 34.2k | return 0; |
96 | | |
97 | 76.3k | case ZIP_SOURCE_READ: |
98 | 76.3k | if ((n = zip_source_read(src, data, len)) < 0) { |
99 | 22.8k | zip_error_set_from_source(&ctx->error, src); |
100 | 22.8k | return -1; |
101 | 22.8k | } |
102 | | |
103 | 53.4k | if (n == 0) { |
104 | 11.4k | if (ctx->crc_position == ctx->position) { |
105 | 11.4k | ctx->crc_complete = 1; |
106 | 11.4k | ctx->size = ctx->position; |
107 | | |
108 | 11.4k | if (ctx->validate) { |
109 | 10.4k | struct zip_stat st; |
110 | | |
111 | 10.4k | if (zip_source_stat(src, &st) < 0) { |
112 | 0 | zip_error_set_from_source(&ctx->error, src); |
113 | 0 | return -1; |
114 | 0 | } |
115 | | |
116 | 10.4k | if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) { |
117 | 8.40k | zip_error_set(&ctx->error, ZIP_ER_CRC, 0); |
118 | 8.40k | return -1; |
119 | 8.40k | } |
120 | 2.07k | if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) { |
121 | | /* We don't have the index here, but the caller should know which file they are reading from. */ |
122 | 830 | zip_error_set(&ctx->error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_INVALID_FILE_LENGTH, MAX_DETAIL_INDEX)); |
123 | 830 | return -1; |
124 | 830 | } |
125 | 2.07k | } |
126 | 11.4k | } |
127 | 11.4k | } |
128 | 42.0k | else if (!ctx->crc_complete && ctx->position <= ctx->crc_position) { |
129 | 42.0k | zip_uint64_t i, nn; |
130 | | |
131 | 84.0k | for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) { |
132 | 42.0k | nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n - i); |
133 | | |
134 | 42.0k | ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data + i, (uInt)nn); |
135 | 42.0k | ctx->crc_position += nn; |
136 | 42.0k | } |
137 | 42.0k | } |
138 | 44.1k | ctx->position += (zip_uint64_t)n; |
139 | 44.1k | return n; |
140 | | |
141 | 34.2k | case ZIP_SOURCE_CLOSE: |
142 | 34.2k | return 0; |
143 | | |
144 | 2.32k | case ZIP_SOURCE_STAT: { |
145 | 2.32k | zip_stat_t *st; |
146 | | |
147 | 2.32k | st = (zip_stat_t *)data; |
148 | | |
149 | 2.32k | if (ctx->crc_complete) { |
150 | 935 | if ((st->valid & ZIP_STAT_SIZE) && st->size != ctx->size) { |
151 | 0 | zip_error_set(&ctx->error, ZIP_ER_DATA_LENGTH, 0); |
152 | 0 | return -1; |
153 | 0 | } |
154 | | /* TODO: Set comp_size, comp_method, encryption_method? |
155 | | After all, this only works for uncompressed data. */ |
156 | 935 | st->size = ctx->size; |
157 | 935 | st->crc = ctx->crc; |
158 | 935 | st->comp_size = ctx->size; |
159 | 935 | st->comp_method = ZIP_CM_STORE; |
160 | 935 | st->encryption_method = ZIP_EM_NONE; |
161 | 935 | st->valid |= ZIP_STAT_SIZE | ZIP_STAT_CRC | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; |
162 | 935 | } |
163 | 2.32k | return 0; |
164 | 2.32k | } |
165 | | |
166 | 32.1k | case ZIP_SOURCE_ERROR: |
167 | 32.1k | return zip_error_to_data(&ctx->error, data, len); |
168 | | |
169 | 43.1k | case ZIP_SOURCE_FREE: |
170 | 43.1k | free(ctx); |
171 | 43.1k | return 0; |
172 | | |
173 | 43.1k | case ZIP_SOURCE_SUPPORTS: { |
174 | 43.1k | zip_int64_t mask = zip_source_supports(src); |
175 | | |
176 | 43.1k | if (mask < 0) { |
177 | 0 | zip_error_set_from_source(&ctx->error, src); |
178 | 0 | return -1; |
179 | 0 | } |
180 | | |
181 | 43.1k | 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); |
182 | 43.1k | mask |= zip_source_make_command_bitmap(ZIP_SOURCE_FREE, -1); |
183 | 43.1k | return mask; |
184 | 43.1k | } |
185 | | |
186 | 0 | case ZIP_SOURCE_SEEK: { |
187 | 0 | zip_int64_t new_position; |
188 | 0 | zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); |
189 | |
|
190 | 0 | if (args == NULL) { |
191 | 0 | return -1; |
192 | 0 | } |
193 | 0 | if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) { |
194 | 0 | zip_error_set_from_source(&ctx->error, src); |
195 | 0 | return -1; |
196 | 0 | } |
197 | | |
198 | 0 | ctx->position = (zip_uint64_t)new_position; |
199 | |
|
200 | 0 | return 0; |
201 | 0 | } |
202 | | |
203 | 0 | case ZIP_SOURCE_TELL: |
204 | 0 | return (zip_int64_t)ctx->position; |
205 | | |
206 | 0 | default: |
207 | 0 | return zip_source_pass_to_lower_layer(src, data, len, cmd); |
208 | 265k | } |
209 | 265k | } |