Imported Upstream version 1.3.2
[platform/upstream/libzip.git] / lib / zip_source_zip_new.c
1 /*
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
4
5   This file is part of libzip, a library to manipulate ZIP archives.
6   The authors can be contacted at <libzip@nih.at>
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
40 zip_source_t *
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)
42 {
43     zip_source_t *src, *s2;
44     struct zip_stat st;
45     bool partial_data, needs_crc, needs_decrypt, needs_decompress;
46
47     if (za == NULL)
48         return NULL;
49
50     if (srcza == NULL || srcidx >= srcza->nentry) {
51         zip_error_set(&za->error, ZIP_ER_INVAL, 0);
52         return NULL;
53     }
54
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);
58         return NULL;
59     }
60
61     if (zip_stat_index(srcza, srcidx, flags|ZIP_FL_UNCHANGED, &st) < 0) {
62         zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
63         return NULL;
64     }
65
66     if (flags & ZIP_FL_ENCRYPTED)
67         flags |= ZIP_FL_COMPRESSED;
68
69     if ((start > 0 || len > 0) && (flags & ZIP_FL_COMPRESSED)) {
70         zip_error_set(&za->error, ZIP_ER_INVAL, 0);
71         return NULL;
72     }
73
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);
77         return NULL;
78     }
79
80     if (len == 0) {
81         len = st.size - start;
82     }
83
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;
89     
90     if (needs_decrypt) {
91         if (password == NULL) {
92             password = za->default_password;
93         }
94         if (password == NULL) {
95             zip_error_set(&za->error, ZIP_ER_NOPASSWD, 0);
96             return NULL;
97         }
98     }
99
100     if (st.comp_size == 0) {
101         return zip_source_buffer(za, NULL, 0, 0);
102     }
103
104     if (partial_data && !needs_decrypt && !needs_decompress) {
105         struct zip_stat st2;
106         
107         st2.size = len;
108         st2.comp_size = len;
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;
112         
113         if ((src = _zip_source_window_new(srcza->src, start, len, &st2, 0, srcza, srcidx, &za->error)) == NULL) {
114             return NULL;
115         }
116     }
117     else {
118         zip_dirent_t *de;
119         
120         if ((de = _zip_get_dirent(srcza, srcidx, flags, &za->error)) == NULL) {
121             return NULL;
122         }
123         if ((src = _zip_source_window_new(srcza->src, 0, st.comp_size, &st, (de->bitflags >> 1) & 3, srcza, srcidx, &za->error)) == NULL) {
124             return NULL;
125         }
126     }
127     
128     if (_zip_source_set_source_archive(src, srcza) < 0) {
129         zip_source_free(src);
130         return NULL;
131     }
132
133     /* creating a layered source calls zip_keep() on the lower layer, so we free it */
134         
135     if (needs_decrypt) {
136         zip_encryption_implementation enc_impl;
137         
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);
140             return NULL;
141         }
142
143         s2 = enc_impl(za, src, st.encryption_method, 0, password);
144         zip_source_free(src);
145         if (s2 == NULL) {
146             return NULL;
147         }
148         src = s2;
149     }
150     if (needs_decompress) {
151         s2 = zip_source_decompress(za, src, st.comp_method);
152         zip_source_free(src);
153         if (s2 == NULL) {
154             return NULL;
155         }
156         src = s2;
157     }
158     if (needs_crc) {
159         s2 = zip_source_crc(za, src, 1);
160         zip_source_free(src);
161         if (s2 == NULL) {
162             return NULL;
163         }
164         src = s2;
165     }
166
167     if (partial_data && (needs_decrypt || needs_decompress)) {
168         s2 = zip_source_window(za, src, start, len);
169         zip_source_free(src);
170         if (s2 == NULL) {
171             return NULL;
172         }
173         src = s2;
174     }
175
176     return src;
177 }