/src/libzip/lib/zip_source_zip_new.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | zip_source_zip_new.c -- prepare data structures for zip_fopen/zip_source_zip |
3 | | Copyright (C) 2012-2024 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 <stdlib.h> |
36 | | |
37 | | #include "zipint.h" |
38 | | |
39 | | static void _zip_file_attributes_from_dirent(zip_file_attributes_t *attributes, zip_dirent_t *de); |
40 | | |
41 | 0 | ZIP_EXTERN zip_source_t *zip_source_zip_file(zip_t* za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_int64_t len, const char *password) { |
42 | 0 | return zip_source_zip_file_create(srcza, srcidx, flags, start, len, password, &za->error); |
43 | 0 | } |
44 | | |
45 | | |
46 | 40.4k | ZIP_EXTERN zip_source_t *zip_source_zip_file_create(zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_int64_t len, const char *password, zip_error_t *error) { |
47 | | /* TODO: We need to make sure that the returned source is invalidated when srcza is closed. */ |
48 | 40.4k | zip_source_t *src, *s2; |
49 | 40.4k | zip_stat_t st; |
50 | 40.4k | zip_file_attributes_t attributes; |
51 | 40.4k | zip_dirent_t *de; |
52 | 40.4k | bool partial_data, needs_crc, encrypted, needs_decrypt, compressed, needs_decompress, changed_data, have_size, have_comp_size; |
53 | 40.4k | zip_flags_t stat_flags; |
54 | 40.4k | zip_int64_t data_len; |
55 | 40.4k | bool take_ownership = false; |
56 | 40.4k | bool empty_data = false; |
57 | | |
58 | 40.4k | if (srcza == NULL || srcidx >= srcza->nentry || len < -1) { |
59 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
60 | 0 | return NULL; |
61 | 0 | } |
62 | | |
63 | 40.4k | if (flags & ZIP_FL_ENCRYPTED) { |
64 | 0 | flags |= ZIP_FL_COMPRESSED; |
65 | 0 | } |
66 | | |
67 | 40.4k | changed_data = false; |
68 | 40.4k | if ((flags & ZIP_FL_UNCHANGED) == 0) { |
69 | 40.4k | zip_entry_t *entry = srcza->entry + srcidx; |
70 | 40.4k | if (ZIP_ENTRY_DATA_CHANGED(entry)) { |
71 | 0 | if ((flags & ZIP_FL_COMPRESSED) || !zip_source_supports_reopen(entry->source)) { |
72 | 0 | zip_error_set(error, ZIP_ER_CHANGED, 0); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | | |
76 | 0 | changed_data = true; |
77 | 0 | } |
78 | 40.4k | else if (entry->deleted) { |
79 | 0 | zip_error_set(error, ZIP_ER_CHANGED, 0); |
80 | 0 | return NULL; |
81 | 0 | } |
82 | 40.4k | } |
83 | | |
84 | 40.4k | stat_flags = flags; |
85 | 40.4k | if (!changed_data) { |
86 | 40.4k | stat_flags |= ZIP_FL_UNCHANGED; |
87 | 40.4k | } |
88 | | |
89 | 40.4k | if (zip_stat_index(srcza, srcidx, stat_flags, &st) < 0) { |
90 | 0 | zip_error_set(error, ZIP_ER_INTERNAL, 0); |
91 | 0 | return NULL; |
92 | 0 | } |
93 | | |
94 | 40.4k | if ((start > 0 || len >= 0) && (flags & ZIP_FL_COMPRESSED)) { |
95 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
96 | 0 | return NULL; |
97 | 0 | } |
98 | | |
99 | 40.4k | have_size = (st.valid & ZIP_STAT_SIZE) != 0; |
100 | | /* overflow or past end of file */ |
101 | 40.4k | if (len >= 0 && ((start > 0 && start + len < start) || (have_size && start + len > st.size))) { |
102 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
103 | 0 | return NULL; |
104 | 0 | } |
105 | | |
106 | 40.4k | if (len == -1) { |
107 | 40.4k | if (have_size) { |
108 | 40.4k | if (st.size - start > ZIP_INT64_MAX) { |
109 | 175 | zip_error_set(error, ZIP_ER_INVAL, 0); |
110 | 175 | return NULL; |
111 | 175 | } |
112 | 40.2k | data_len = (zip_int64_t)(st.size - start); |
113 | 40.2k | } |
114 | 0 | else { |
115 | 0 | data_len = -1; |
116 | 0 | } |
117 | 40.4k | } |
118 | 0 | else { |
119 | 0 | data_len = len; |
120 | 0 | } |
121 | | |
122 | 40.2k | if (have_size) { |
123 | 40.2k | partial_data = (zip_uint64_t)(data_len) < st.size; |
124 | 40.2k | } |
125 | 0 | else { |
126 | 0 | partial_data = true; |
127 | 0 | } |
128 | 40.2k | encrypted = (st.valid & ZIP_STAT_ENCRYPTION_METHOD) && (st.encryption_method != ZIP_EM_NONE); |
129 | 40.2k | needs_decrypt = ((flags & ZIP_FL_ENCRYPTED) == 0) && encrypted; |
130 | 40.2k | compressed = (st.valid & ZIP_STAT_COMP_METHOD) && (st.comp_method != ZIP_CM_STORE); |
131 | 40.2k | needs_decompress = ((flags & ZIP_FL_COMPRESSED) == 0) && compressed; |
132 | | /* when reading the whole file, check for CRC errors */ |
133 | 40.2k | needs_crc = ((flags & ZIP_FL_COMPRESSED) == 0 || !compressed) && !partial_data && (st.valid & ZIP_STAT_CRC) != 0; |
134 | | |
135 | 40.2k | if (needs_decrypt) { |
136 | 7.38k | if (password == NULL) { |
137 | 1.11k | password = srcza->default_password; |
138 | 1.11k | } |
139 | 7.38k | if (password == NULL) { |
140 | 1.11k | zip_error_set(error, ZIP_ER_NOPASSWD, 0); |
141 | 1.11k | return NULL; |
142 | 1.11k | } |
143 | 7.38k | } |
144 | | |
145 | 39.1k | if ((de = _zip_get_dirent(srcza, srcidx, flags, error)) == NULL) { |
146 | 0 | return NULL; |
147 | 0 | } |
148 | 39.1k | _zip_file_attributes_from_dirent(&attributes, de); |
149 | | |
150 | 39.1k | have_comp_size = (st.valid & ZIP_STAT_COMP_SIZE) != 0; |
151 | 39.1k | if (needs_decrypt || needs_decompress) { |
152 | 35.0k | empty_data = (have_comp_size && st.comp_size == 0); |
153 | 35.0k | } |
154 | 4.11k | else { |
155 | 4.11k | empty_data = (have_size && st.size == 0); |
156 | 4.11k | } |
157 | 39.1k | if (empty_data) { |
158 | 6.35k | src = zip_source_buffer_with_attributes_create(NULL, 0, 0, &attributes, error); |
159 | 6.35k | } |
160 | 32.8k | else { |
161 | 32.8k | src = NULL; |
162 | 32.8k | } |
163 | | |
164 | | |
165 | | /* If we created source buffer above, we want the window source to take ownership of it. */ |
166 | 39.1k | take_ownership = src != NULL; |
167 | | /* if we created a buffer source above, then treat it as if |
168 | | reading the changed data - that way we don't need add another |
169 | | special case to the code below that wraps it in the window |
170 | | source */ |
171 | 39.1k | changed_data = changed_data || (src != NULL); |
172 | | |
173 | 39.1k | if (partial_data && !needs_decrypt && !needs_decompress) { |
174 | 0 | struct zip_stat st2; |
175 | 0 | zip_t *source_archive; |
176 | 0 | zip_uint64_t source_index; |
177 | |
|
178 | 0 | if (changed_data) { |
179 | 0 | if (src == NULL) { |
180 | 0 | src = srcza->entry[srcidx].source; |
181 | 0 | } |
182 | 0 | source_archive = NULL; |
183 | 0 | source_index = 0; |
184 | 0 | } |
185 | 0 | else { |
186 | 0 | src = srcza->src; |
187 | 0 | source_archive = srcza; |
188 | 0 | source_index = srcidx; |
189 | 0 | } |
190 | |
|
191 | 0 | st2.comp_method = ZIP_CM_STORE; |
192 | 0 | st2.valid = ZIP_STAT_COMP_METHOD; |
193 | 0 | if (data_len >= 0) { |
194 | 0 | st2.size = (zip_uint64_t)data_len; |
195 | 0 | st2.comp_size = (zip_uint64_t)data_len; |
196 | 0 | st2.valid |= ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE; |
197 | 0 | } |
198 | 0 | if (st.valid & ZIP_STAT_MTIME) { |
199 | 0 | st2.mtime = st.mtime; |
200 | 0 | st2.valid |= ZIP_STAT_MTIME; |
201 | 0 | } |
202 | |
|
203 | 0 | if ((src = _zip_source_window_new(src, start, data_len, &st2, ZIP_STAT_NAME, &attributes, &de->last_mod, source_archive, source_index, take_ownership, error)) == NULL) { |
204 | 0 | return NULL; |
205 | 0 | } |
206 | 0 | } |
207 | | /* here we restrict src to file data, so no point in doing it for |
208 | | source that already represents only the file data */ |
209 | 39.1k | else if (!changed_data) { |
210 | | /* this branch is executed only for archive sources; we know |
211 | | that stat data come from the archive too, so it's safe to |
212 | | assume that st has a comp_size specified */ |
213 | 32.8k | if (st.comp_size > ZIP_INT64_MAX) { |
214 | 152 | zip_error_set(error, ZIP_ER_INVAL, 0); |
215 | 152 | return NULL; |
216 | 152 | } |
217 | | /* despite the fact that we want the whole data file, we still |
218 | | wrap the source into a window source to add st and |
219 | | attributes and to have a source that positions the read |
220 | | offset properly before each read for multiple zip_file_t |
221 | | referring to the same underlying source */ |
222 | 32.6k | if ((src = _zip_source_window_new(srcza->src, 0, (zip_int64_t)st.comp_size, &st, ZIP_STAT_NAME, &attributes, &de->last_mod, srcza, srcidx, take_ownership, error)) == NULL) { |
223 | 0 | return NULL; |
224 | 0 | } |
225 | 32.6k | } |
226 | 6.35k | else { |
227 | | /* this branch gets executed when reading the whole changed |
228 | | data file or when "reading" from a zero-sized source buffer |
229 | | that we created above */ |
230 | 6.35k | if (src == NULL) { |
231 | 0 | src = srcza->entry[srcidx].source; |
232 | 0 | } |
233 | | /* despite the fact that we want the whole data file, we still |
234 | | wrap the source into a window source to add st and |
235 | | attributes and to have a source that positions the read |
236 | | offset properly before each read for multiple zip_file_t |
237 | | referring to the same underlying source */ |
238 | 6.35k | if ((src = _zip_source_window_new(src, 0, data_len, &st, ZIP_STAT_NAME, &attributes, &de->last_mod, NULL, 0, take_ownership, error)) == NULL) { |
239 | 0 | return NULL; |
240 | 0 | } |
241 | 6.35k | } |
242 | | |
243 | | /* In all cases, src is a window source and therefore is owned by this function. */ |
244 | | |
245 | 39.0k | if (_zip_source_set_source_archive(src, srcza) < 0) { |
246 | 0 | zip_source_free(src); |
247 | 0 | return NULL; |
248 | 0 | } |
249 | | |
250 | | /* creating a layered source calls zip_keep() on the lower layer, so we free it */ |
251 | | |
252 | 39.0k | if (needs_decrypt) { |
253 | 6.23k | zip_encryption_implementation enc_impl; |
254 | | |
255 | 6.23k | if ((enc_impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) { |
256 | 909 | zip_source_free(src); |
257 | 909 | zip_error_set(error, ZIP_ER_ENCRNOTSUPP, 0); |
258 | 909 | return NULL; |
259 | 909 | } |
260 | | |
261 | 5.32k | s2 = enc_impl(srcza, src, st.encryption_method, 0, password); |
262 | 5.32k | if (s2 == NULL) { |
263 | 11 | zip_source_free(src); |
264 | 11 | return NULL; |
265 | 11 | } |
266 | | |
267 | 5.31k | src = s2; |
268 | 5.31k | } |
269 | 38.0k | if (needs_decompress) { |
270 | 32.4k | s2 = zip_source_decompress(srcza, src, st.comp_method); |
271 | 32.4k | if (s2 == NULL) { |
272 | 6.24k | zip_source_free(src); |
273 | 6.24k | return NULL; |
274 | 6.24k | } |
275 | 26.1k | src = s2; |
276 | 26.1k | } |
277 | 31.8k | if (needs_crc) { |
278 | 31.5k | s2 = zip_source_crc_create(src, 1, error); |
279 | 31.5k | if (s2 == NULL) { |
280 | 0 | zip_source_free(src); |
281 | 0 | return NULL; |
282 | 0 | } |
283 | 31.5k | src = s2; |
284 | 31.5k | } |
285 | | |
286 | 31.8k | if (partial_data && (needs_decrypt || needs_decompress)) { |
287 | 0 | zip_stat_t st2; |
288 | 0 | zip_stat_init(&st2); |
289 | 0 | if (data_len >= 0) { |
290 | 0 | st2.valid = ZIP_STAT_SIZE; |
291 | 0 | st2.size = (zip_uint64_t)data_len; |
292 | 0 | } |
293 | 0 | s2 = _zip_source_window_new(src, start, data_len, &st2, ZIP_STAT_NAME, NULL, NULL, NULL, 0, true, error); |
294 | 0 | if (s2 == NULL) { |
295 | 0 | zip_source_free(src); |
296 | 0 | return NULL; |
297 | 0 | } |
298 | 0 | src = s2; |
299 | 0 | } |
300 | | |
301 | 31.8k | return src; |
302 | 31.8k | } |
303 | | |
304 | | static void |
305 | 39.1k | _zip_file_attributes_from_dirent(zip_file_attributes_t *attributes, zip_dirent_t *de) { |
306 | 39.1k | zip_file_attributes_init(attributes); |
307 | 39.1k | attributes->valid = ZIP_FILE_ATTRIBUTES_ASCII | ZIP_FILE_ATTRIBUTES_HOST_SYSTEM | ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES | ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS; |
308 | 39.1k | attributes->ascii = de->int_attrib & 1; |
309 | 39.1k | attributes->host_system = de->version_madeby >> 8; |
310 | 39.1k | attributes->external_file_attributes = de->ext_attrib; |
311 | 39.1k | attributes->general_purpose_bit_flags = de->bitflags; |
312 | 39.1k | attributes->general_purpose_bit_mask = ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK; |
313 | 39.1k | } |