/src/libzip/lib/zip_source_window.c
Line | Count | Source |
1 | | /* |
2 | | zip_source_window.c -- return part of lower source |
3 | | Copyright (C) 2012-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 | | #include "zipint.h" |
35 | | |
36 | | #include <stdlib.h> |
37 | | #include <string.h> |
38 | | |
39 | | struct window { |
40 | | zip_uint64_t start; /* where in file we start reading */ |
41 | | zip_uint64_t end; /* where in file we stop reading */ |
42 | | bool end_valid; /* whether end is set, otherwise read until EOF */ |
43 | | |
44 | | /* if not NULL, read file data for this file */ |
45 | | zip_t *source_archive; |
46 | | zip_uint64_t source_index; |
47 | | |
48 | | zip_uint64_t offset; /* offset in src for next read */ |
49 | | |
50 | | zip_stat_t stat; |
51 | | zip_uint64_t stat_invalid; |
52 | | zip_file_attributes_t attributes; |
53 | | zip_dostime_t dostime; |
54 | | bool dostime_valid; |
55 | | zip_error_t error; |
56 | | zip_int64_t supports; |
57 | | bool needs_seek; |
58 | | }; |
59 | | |
60 | | typedef struct window window_t; |
61 | | |
62 | | static window_t *window_new(void); |
63 | | static void window_free(window_t *ctx); |
64 | | |
65 | | static zip_int64_t window_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t); |
66 | | |
67 | | |
68 | 0 | ZIP_EXTERN zip_source_t *zip_source_window_create(zip_source_t *src, zip_uint64_t start, zip_int64_t len, zip_error_t *error) { |
69 | 0 | return _zip_source_window_new(src, start, len, NULL, 0, NULL, NULL, NULL, 0, false, error); |
70 | 0 | } |
71 | | |
72 | | |
73 | 10.4k | zip_source_t *_zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_int64_t length, zip_stat_t *st, zip_uint64_t st_invalid, zip_file_attributes_t *attributes, zip_dostime_t *dostime, zip_t *source_archive, zip_uint64_t source_index, bool take_ownership, zip_error_t *error) { |
74 | 10.4k | zip_source_t *window_source; |
75 | 10.4k | struct window *ctx; |
76 | 10.4k | zip_stat_t lower_st; |
77 | | |
78 | 10.4k | if (src == NULL || length < -1 || (source_archive == NULL && source_index != 0)) { |
79 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
80 | 0 | return NULL; |
81 | 0 | } |
82 | | |
83 | 10.4k | if (length > 0) { |
84 | 10.3k | if (start + (zip_uint64_t)length < start) { |
85 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
86 | 0 | return NULL; |
87 | 0 | } |
88 | 10.3k | } |
89 | | |
90 | 10.4k | zip_stat_init(&lower_st); |
91 | 10.4k | (void)zip_source_stat(src, &lower_st); |
92 | | |
93 | 10.4k | if (lower_st.valid & ZIP_STAT_SIZE) { |
94 | 10.4k | if (start > lower_st.size || (length > 0 && start + (zip_uint64_t)length > lower_st.size)) { |
95 | 553 | zip_error_set(error, ZIP_ER_INVAL, 0); |
96 | 553 | return NULL; |
97 | 553 | } |
98 | 10.4k | } |
99 | | |
100 | 9.88k | if ((ctx = window_new()) == NULL) { |
101 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
102 | 0 | return NULL; |
103 | 0 | } |
104 | | |
105 | 9.88k | ctx->start = start; |
106 | 9.88k | if (length == -1) { |
107 | 0 | ctx->end_valid = false; |
108 | 0 | } |
109 | 9.88k | else { |
110 | 9.88k | ctx->end = start + (zip_uint64_t)length; |
111 | 9.88k | ctx->end_valid = true; |
112 | 9.88k | } |
113 | 9.88k | ctx->stat_invalid = st_invalid; |
114 | 9.88k | if (attributes != NULL) { |
115 | 9.82k | (void)memcpy_s(&ctx->attributes, sizeof(ctx->attributes), attributes, sizeof(ctx->attributes)); |
116 | 9.82k | } |
117 | 9.88k | if (dostime != NULL) { |
118 | 9.82k | ctx->dostime = *dostime; |
119 | 9.82k | ctx->dostime_valid = true; |
120 | 9.82k | } |
121 | 9.88k | ctx->source_archive = source_archive; |
122 | 9.88k | ctx->source_index = source_index; |
123 | 9.88k | ctx->supports = (zip_source_supports(src) & (ZIP_SOURCE_SUPPORTS_SEEKABLE | ZIP_SOURCE_SUPPORTS_REOPEN)) | (zip_source_make_command_bitmap(ZIP_SOURCE_GET_FILE_ATTRIBUTES, ZIP_SOURCE_GET_DOS_TIME, ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, ZIP_SOURCE_FREE, -1)); |
124 | 9.88k | if (ctx->end_valid) { |
125 | 9.88k | ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_AT_EOF); |
126 | 9.88k | } |
127 | 9.88k | ctx->needs_seek = (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_SEEK)) ? true : false; |
128 | | |
129 | 9.88k | if (st) { |
130 | 9.88k | if (_zip_stat_merge(&ctx->stat, st, error) < 0) { |
131 | 0 | window_free(ctx); |
132 | 0 | return NULL; |
133 | 0 | } |
134 | 9.88k | } |
135 | | |
136 | 9.88k | window_source = zip_source_layered_create(src, window_read, ctx, error); |
137 | 9.88k | if (window_source == NULL) { |
138 | 0 | window_free(ctx); |
139 | 0 | return NULL; |
140 | 0 | } |
141 | 9.88k | if (!take_ownership) { |
142 | 9.82k | zip_source_keep(src); |
143 | 9.82k | } |
144 | 9.88k | return window_source; |
145 | 9.88k | } |
146 | | |
147 | | |
148 | 9.82k | int _zip_source_set_source_archive(zip_source_t *src, zip_t *za) { |
149 | 9.82k | src->source_archive = za; |
150 | 9.82k | return _zip_register_source(za, src); |
151 | 9.82k | } |
152 | | |
153 | | |
154 | | /* called by zip_discard to avoid operating on file from closed archive */ |
155 | 0 | void _zip_source_invalidate(zip_source_t *src) { |
156 | 0 | if (ZIP_SOURCE_IS_OPEN_READING(src)) { |
157 | 0 | (void)zip_source_close(src); |
158 | 0 | } |
159 | |
|
160 | 0 | src->source_closed = 1; |
161 | |
|
162 | 0 | if (zip_error_code_zip(&src->error) == ZIP_ER_OK) { |
163 | 0 | zip_error_set(&src->error, ZIP_ER_ZIPCLOSED, 0); |
164 | 0 | } |
165 | 0 | } |
166 | | |
167 | | |
168 | 77.3k | static zip_int64_t window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { |
169 | 77.3k | struct window *ctx; |
170 | 77.3k | zip_int64_t ret; |
171 | 77.3k | zip_uint64_t n, i; |
172 | | |
173 | 77.3k | ctx = (struct window *)_ctx; |
174 | | |
175 | 77.3k | switch (cmd) { |
176 | 1.31k | case ZIP_SOURCE_AT_EOF: |
177 | 1.31k | if (ctx->end_valid) { |
178 | 1.31k | return ctx->offset == ctx->end; |
179 | 1.31k | } |
180 | 0 | else { |
181 | 0 | return zip_source_pass_to_lower_layer(src, data, len, cmd); |
182 | 0 | } |
183 | | |
184 | 8.45k | case ZIP_SOURCE_CLOSE: |
185 | 8.45k | return 0; |
186 | | |
187 | 1.38k | case ZIP_SOURCE_ERROR: |
188 | 1.38k | return zip_error_to_data(&ctx->error, data, len); |
189 | | |
190 | 9.88k | case ZIP_SOURCE_FREE: |
191 | 9.88k | window_free(ctx); |
192 | 9.88k | return 0; |
193 | | |
194 | 9.35k | case ZIP_SOURCE_OPEN: |
195 | 9.35k | if (ctx->source_archive) { |
196 | 9.25k | zip_uint64_t offset; |
197 | | |
198 | 9.25k | if ((offset = _zip_file_get_offset(ctx->source_archive, ctx->source_index, &ctx->error)) == 0) { |
199 | 904 | return -1; |
200 | 904 | } |
201 | 8.34k | if (ctx->end_valid) { |
202 | 8.34k | if (ctx->end + offset < ctx->end) { |
203 | | /* zip archive data claims end of data past zip64 limits */ |
204 | 0 | zip_error_set(&ctx->error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_CDIR_ENTRY_INVALID, ctx->source_index)); |
205 | 0 | return -1; |
206 | 0 | } |
207 | 8.34k | ctx->end += offset; |
208 | 8.34k | } |
209 | 8.34k | ctx->start += offset; |
210 | 8.34k | ctx->source_archive = NULL; |
211 | 8.34k | } |
212 | | |
213 | 8.45k | if (!ctx->needs_seek) { |
214 | 0 | DEFINE_BYTE_ARRAY(b, BUFSIZE); |
215 | |
|
216 | 0 | if (!byte_array_init(b, BUFSIZE)) { |
217 | 0 | zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); |
218 | 0 | return -1; |
219 | 0 | } |
220 | | |
221 | 0 | for (n = 0; n < ctx->start; n += (zip_uint64_t)ret) { |
222 | 0 | i = (ctx->start - n > BUFSIZE ? BUFSIZE : ctx->start - n); |
223 | 0 | if ((ret = zip_source_read(src, b, i)) < 0) { |
224 | 0 | zip_error_set_from_source(&ctx->error, src); |
225 | 0 | byte_array_fini(b); |
226 | 0 | return -1; |
227 | 0 | } |
228 | 0 | if (ret == 0) { |
229 | 0 | zip_error_set(&ctx->error, ZIP_ER_EOF, 0); |
230 | 0 | byte_array_fini(b); |
231 | 0 | return -1; |
232 | 0 | } |
233 | 0 | } |
234 | | |
235 | 0 | byte_array_fini(b); |
236 | 0 | } |
237 | | |
238 | 8.45k | ctx->offset = ctx->start; |
239 | 8.45k | return 0; |
240 | | |
241 | 15.4k | case ZIP_SOURCE_READ: |
242 | 15.4k | if (ctx->end_valid && len > ctx->end - ctx->offset) { |
243 | 14.1k | len = ctx->end - ctx->offset; |
244 | 14.1k | } |
245 | | |
246 | 15.4k | if (len == 0) { |
247 | 6.76k | return 0; |
248 | 6.76k | } |
249 | | |
250 | 8.67k | if (ctx->needs_seek) { |
251 | 8.67k | if (zip_source_seek(src, (zip_int64_t)ctx->offset, SEEK_SET) < 0) { |
252 | 191 | zip_error_set_from_source(&ctx->error, src); |
253 | 191 | if (zip_error_code_zip(&ctx->error) == ZIP_ER_INVAL) { |
254 | | /* We validated ctx->offset in ZIP_SEEK. */ |
255 | 191 | zip_error_set(&ctx->error, ZIP_ER_EOF, 0); |
256 | 191 | } |
257 | 191 | return -1; |
258 | 191 | } |
259 | 8.67k | } |
260 | | |
261 | 8.48k | if ((ret = zip_source_read(src, data, len)) < 0) { |
262 | 0 | zip_error_set_from_source(&ctx->error, src); |
263 | 0 | return -1; |
264 | 0 | } |
265 | | |
266 | 8.48k | if (ret == 0) { |
267 | 285 | if (ctx->end_valid && ctx->offset < ctx->end) { |
268 | 285 | zip_error_set(&ctx->error, ZIP_ER_EOF, 0); |
269 | 285 | return -1; |
270 | 285 | } |
271 | 285 | } |
272 | | |
273 | 8.19k | ctx->offset += (zip_uint64_t)ret; |
274 | 8.19k | return ret; |
275 | | |
276 | 0 | case ZIP_SOURCE_SEEK: { |
277 | 0 | zip_uint64_t length; |
278 | 0 | zip_int64_t new_offset; |
279 | 0 | zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); |
280 | | |
281 | | /* |
282 | | If we don't know the length and seek from the end, we seek in the lower source to get the new offset. |
283 | | */ |
284 | 0 | if (!ctx->end_valid && args->whence == SEEK_END) { |
285 | 0 | zip_int64_t lower_offset; |
286 | |
|
287 | 0 | if (zip_source_seek(src, args->offset, args->whence) < 0) { |
288 | 0 | zip_error_set_from_source(&ctx->error, src); |
289 | 0 | return -1; |
290 | 0 | } |
291 | 0 | if ((lower_offset = zip_source_tell(src)) < 0) { |
292 | 0 | zip_error_set_from_source(&ctx->error, src); |
293 | 0 | return -1; |
294 | 0 | } |
295 | 0 | if ((zip_uint64_t)lower_offset < ctx->start) { |
296 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
297 | 0 | return -1; |
298 | 0 | } |
299 | 0 | ctx->offset = (zip_uint64_t)lower_offset; |
300 | 0 | return 0; |
301 | 0 | } |
302 | | |
303 | | /* |
304 | | If we don't know the length, we disable the end check in zip_source_seek_compute_offset by passing in the largest possible value and seek in the lower source to validate the new offset. |
305 | | */ |
306 | 0 | length = ctx->end_valid ? (ctx->end - ctx->start) : ZIP_INT64_MAX; |
307 | 0 | new_offset = zip_source_seek_compute_offset(ctx->offset - ctx->start, length, data, len, &ctx->error); |
308 | |
|
309 | 0 | if (new_offset < 0) { |
310 | 0 | return -1; |
311 | 0 | } |
312 | | |
313 | 0 | if (new_offset + ctx->start < ctx->start) { |
314 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
315 | 0 | return -1; |
316 | 0 | } |
317 | 0 | new_offset += ctx->start; |
318 | |
|
319 | 0 | if (!ctx->end_valid) { |
320 | 0 | if (zip_source_seek(src, new_offset, SEEK_SET) < 0) { |
321 | 0 | zip_error_set_from_source(&ctx->error, src); |
322 | 0 | return -1; |
323 | 0 | } |
324 | 0 | } |
325 | 0 | ctx->offset = (zip_uint64_t)new_offset; |
326 | 0 | return 0; |
327 | 0 | } |
328 | | |
329 | 14.9k | case ZIP_SOURCE_STAT: { |
330 | 14.9k | zip_stat_t *st; |
331 | | |
332 | 14.9k | st = (zip_stat_t *)data; |
333 | | |
334 | 14.9k | if (_zip_stat_merge(st, &ctx->stat, &ctx->error) < 0) { |
335 | 0 | return -1; |
336 | 0 | } |
337 | | |
338 | 14.9k | if (!(ctx->stat.valid & ZIP_STAT_SIZE)) { |
339 | 0 | if (ctx->end_valid) { |
340 | 0 | st->valid |= ZIP_STAT_SIZE; |
341 | 0 | st->size = ctx->end - ctx->start; |
342 | 0 | } |
343 | 0 | else if (st->valid & ZIP_STAT_SIZE) { |
344 | 0 | if (st->size < ctx->start) { |
345 | 0 | st->valid &= ~ZIP_STAT_SIZE; |
346 | 0 | } |
347 | 0 | else { |
348 | 0 | st->size -= ctx->start; |
349 | 0 | } |
350 | 0 | } |
351 | 0 | } |
352 | | |
353 | 14.9k | st->valid &= ~ctx->stat_invalid; |
354 | | |
355 | 14.9k | return 0; |
356 | 14.9k | } |
357 | | |
358 | 6.71k | case ZIP_SOURCE_GET_FILE_ATTRIBUTES: |
359 | 6.71k | if (len < sizeof(ctx->attributes)) { |
360 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
361 | 0 | return -1; |
362 | 0 | } |
363 | | |
364 | 6.71k | (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes)); |
365 | 6.71k | return sizeof(ctx->attributes); |
366 | | |
367 | 0 | case ZIP_SOURCE_GET_DOS_TIME: |
368 | 0 | if (len < sizeof(ctx->dostime)) { |
369 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
370 | 0 | return -1; |
371 | 0 | } |
372 | 0 | if (ctx->dostime_valid) { |
373 | 0 | (void)memcpy_s(data, sizeof(ctx->dostime), &ctx->dostime, sizeof(ctx->dostime)); |
374 | 0 | return sizeof(ctx->dostime); |
375 | 0 | } |
376 | 0 | else { |
377 | 0 | return 0; |
378 | 0 | } |
379 | | |
380 | 9.88k | case ZIP_SOURCE_SUPPORTS: |
381 | 9.88k | return ctx->supports; |
382 | | |
383 | 0 | case ZIP_SOURCE_TELL: |
384 | 0 | return (zip_int64_t)(ctx->offset - ctx->start); |
385 | | |
386 | 0 | default: |
387 | 0 | return zip_source_pass_to_lower_layer(src, data, len, cmd); |
388 | 77.3k | } |
389 | 77.3k | } |
390 | | |
391 | | |
392 | 9.82k | void _zip_deregister_source(zip_t *za, zip_source_t *src) { |
393 | 9.82k | zip_uint64_t i; |
394 | | |
395 | 9.82k | for (i = 0; i < za->nopen_source; i++) { |
396 | 9.82k | if (za->open_source[i] == src) { |
397 | 9.82k | za->open_source[i] = za->open_source[za->nopen_source - 1]; |
398 | 9.82k | za->nopen_source--; |
399 | 9.82k | break; |
400 | 9.82k | } |
401 | 9.82k | } |
402 | 9.82k | } |
403 | | |
404 | | |
405 | 9.82k | int _zip_register_source(zip_t *za, zip_source_t *src) { |
406 | 9.82k | if (za->nopen_source + 1 >= za->nopen_source_alloc) { |
407 | 3.70k | if (!ZIP_REALLOC(za->open_source, za->nopen_source_alloc, 10, &za->error)) { |
408 | 0 | return -1; |
409 | 0 | } |
410 | 3.70k | } |
411 | | |
412 | 9.82k | za->open_source[za->nopen_source++] = src; |
413 | | |
414 | 9.82k | return 0; |
415 | 9.82k | } |
416 | | |
417 | 9.88k | static window_t *window_new(void) { |
418 | 9.88k | window_t *ctx; |
419 | | |
420 | 9.88k | if ((ctx = (window_t *)malloc(sizeof(window_t))) == NULL) { |
421 | 0 | return NULL; |
422 | 0 | } |
423 | | |
424 | 9.88k | ctx->start = 0; |
425 | 9.88k | ctx->end = 0; |
426 | 9.88k | ctx->end_valid = false; |
427 | 9.88k | ctx->source_archive = NULL; |
428 | 9.88k | ctx->source_index = 0; |
429 | 9.88k | ctx->offset = 0; |
430 | 9.88k | zip_stat_init(&ctx->stat); |
431 | 9.88k | ctx->stat_invalid = 0; |
432 | 9.88k | zip_file_attributes_init(&ctx->attributes); |
433 | 9.88k | ctx->dostime_valid = false; |
434 | 9.88k | zip_error_init(&ctx->error); |
435 | 9.88k | ctx->supports = 0; |
436 | 9.88k | ctx->needs_seek = false; |
437 | | |
438 | 9.88k | return ctx; |
439 | 9.88k | } |
440 | | |
441 | 9.88k | static void window_free(window_t *ctx) { |
442 | 9.88k | if (ctx == NULL) { |
443 | 0 | return; |
444 | 0 | } |
445 | | |
446 | 9.88k | zip_error_fini(&ctx->error); |
447 | 9.88k | free(ctx); |
448 | 9.88k | } |