2 zip_source_zip_new.c -- prepare data structures for zip_fopen/zip_source_zip
3 Copyright (C) 2012-2016 Dieter Baron and Thomas Klausner
5 This file is part of libzip, a library to manipulate ZIP archives.
6 The authors can be contacted at <libzip@nih.at>
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
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
17 3. The names of the authors may not be used to endorse or promote
18 products derived from this software without specific prior
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.
41 _zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_uint64_t len, const char *password)
43 zip_source_t *src, *s2;
45 bool partial_data, needs_crc, needs_decrypt, needs_decompress;
50 if (srcza == NULL || srcidx >= srcza->nentry) {
51 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
55 if ((flags & ZIP_FL_UNCHANGED) == 0
56 && (ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx) || srcza->entry[srcidx].deleted)) {
57 zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
61 if (zip_stat_index(srcza, srcidx, flags|ZIP_FL_UNCHANGED, &st) < 0) {
62 zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
66 if (flags & ZIP_FL_ENCRYPTED)
67 flags |= ZIP_FL_COMPRESSED;
69 if ((start > 0 || len > 0) && (flags & ZIP_FL_COMPRESSED)) {
70 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
74 /* overflow or past end of file */
75 if ((start > 0 || len > 0) && (start + len < start || start + len > st.size)) {
76 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
81 len = st.size - start;
84 partial_data = len < st.size;
85 needs_decrypt = ((flags & ZIP_FL_ENCRYPTED) == 0) && (st.encryption_method != ZIP_EM_NONE);
86 needs_decompress = ((flags & ZIP_FL_COMPRESSED) == 0) && (st.comp_method != ZIP_CM_STORE);
87 /* when reading the whole file, check for CRC errors */
88 needs_crc = ((flags & ZIP_FL_COMPRESSED) == 0 || st.comp_method == ZIP_CM_STORE) && !partial_data;
91 if (password == NULL) {
92 password = za->default_password;
94 if (password == NULL) {
95 zip_error_set(&za->error, ZIP_ER_NOPASSWD, 0);
100 if (st.comp_size == 0) {
101 return zip_source_buffer(za, NULL, 0, 0);
104 if (partial_data && !needs_decrypt && !needs_decompress) {
109 st2.comp_method = ZIP_CM_STORE;
110 st2.mtime = st.mtime;
111 st2.valid = ZIP_STAT_SIZE|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_MTIME;
113 if ((src = _zip_source_window_new(srcza->src, start, len, &st2, 0, srcza, srcidx, &za->error)) == NULL) {
120 if ((de = _zip_get_dirent(srcza, srcidx, flags, &za->error)) == NULL) {
123 if ((src = _zip_source_window_new(srcza->src, 0, st.comp_size, &st, (de->bitflags >> 1) & 3, srcza, srcidx, &za->error)) == NULL) {
128 if (_zip_source_set_source_archive(src, srcza) < 0) {
129 zip_source_free(src);
133 /* creating a layered source calls zip_keep() on the lower layer, so we free it */
136 zip_encryption_implementation enc_impl;
138 if ((enc_impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) {
139 zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
143 s2 = enc_impl(za, src, st.encryption_method, 0, password);
144 zip_source_free(src);
150 if (needs_decompress) {
151 s2 = zip_source_decompress(za, src, st.comp_method);
152 zip_source_free(src);
159 s2 = zip_source_crc(za, src, 1);
160 zip_source_free(src);
167 if (partial_data && (needs_decrypt || needs_decompress)) {
168 s2 = zip_source_window(za, src, start, len);
169 zip_source_free(src);