Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_source_zip_new.c
Line
Count
Source
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
92.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
92.4k
    zip_source_t *src, *s2;
49
92.4k
    zip_stat_t st;
50
92.4k
    zip_file_attributes_t attributes;
51
92.4k
    zip_dirent_t *de;
52
92.4k
    bool partial_data, needs_crc, encrypted, needs_decrypt, compressed, needs_decompress, changed_data, have_size, have_comp_size;
53
92.4k
    zip_flags_t stat_flags;
54
92.4k
    zip_int64_t data_len;
55
92.4k
    bool take_ownership = false;
56
92.4k
    bool empty_data = false;
57
58
92.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
92.4k
    if (flags & ZIP_FL_ENCRYPTED) {
64
0
        flags |= ZIP_FL_COMPRESSED;
65
0
    }
66
67
92.4k
    changed_data = false;
68
92.4k
    if ((flags & ZIP_FL_UNCHANGED) == 0) {
69
92.4k
        zip_entry_t *entry = srcza->entry + srcidx;
70
92.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
92.4k
        else if (entry->deleted) {
79
0
            zip_error_set(error, ZIP_ER_CHANGED, 0);
80
0
            return NULL;
81
0
        }
82
92.4k
    }
83
84
92.4k
    stat_flags = flags;
85
92.4k
    if (!changed_data) {
86
92.4k
        stat_flags |= ZIP_FL_UNCHANGED;
87
92.4k
    }
88
89
92.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
92.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
    /* Check for overflow of end. */
100
92.4k
    if (len > 0 && !UNSIGNED_ADD_FITS(start, len)) {
101
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
102
0
        return NULL;
103
0
    }
104
105
92.4k
    have_size = (st.valid & ZIP_STAT_SIZE) != 0;
106
92.4k
    if (have_size) {
107
        /* Check that start and end are within the size of the file. */
108
92.4k
        if (start > st.size) {
109
0
            zip_error_set(error, ZIP_ER_INVAL, 0);
110
0
            return NULL;
111
0
        }
112
92.4k
        if (len > 0 && start + len > st.size) {
113
0
            zip_error_set(error, ZIP_ER_INVAL, 0);
114
0
            return NULL;
115
0
        }
116
92.4k
    }
117
118
92.4k
    if (len == -1) {
119
92.4k
        if (have_size) {
120
92.4k
            if (st.size - start > ZIP_INT64_MAX) {
121
339
                zip_error_set(error, ZIP_ER_INVAL, 0);
122
339
                return NULL;
123
339
            }
124
92.0k
            data_len = (zip_int64_t)(st.size - start);
125
92.0k
        }
126
0
        else {
127
0
            data_len = -1;
128
0
        }
129
92.4k
    }
130
0
    else {
131
0
        data_len = len;
132
0
    }
133
134
92.0k
    if (have_size) {
135
92.0k
        partial_data = (zip_uint64_t)(data_len) < st.size;
136
92.0k
    }
137
0
    else {
138
0
        partial_data = true;
139
0
    }
140
92.0k
    encrypted = (st.valid & ZIP_STAT_ENCRYPTION_METHOD) && (st.encryption_method != ZIP_EM_NONE);
141
92.0k
    needs_decrypt = ((flags & ZIP_FL_ENCRYPTED) == 0) && encrypted;
142
92.0k
    compressed = (st.valid & ZIP_STAT_COMP_METHOD) && (st.comp_method != ZIP_CM_STORE);
143
92.0k
    needs_decompress = ((flags & ZIP_FL_COMPRESSED) == 0) && compressed;
144
    /* when reading the whole file, check for CRC errors */
145
92.0k
    needs_crc = ((flags & ZIP_FL_COMPRESSED) == 0 || !compressed) && !partial_data;
146
147
92.0k
    if (needs_decrypt) {
148
27.8k
        if (password == NULL) {
149
3.76k
            password = srcza->default_password;
150
3.76k
        }
151
27.8k
        if (password == NULL) {
152
3.76k
            zip_error_set(error, ZIP_ER_NOPASSWD, 0);
153
3.76k
            return NULL;
154
3.76k
        }
155
27.8k
    }
156
157
88.3k
    if ((de = _zip_get_dirent(srcza, srcidx, flags, error)) == NULL) {
158
0
        return NULL;
159
0
    }
160
88.3k
    _zip_file_attributes_from_dirent(&attributes, de);
161
162
88.3k
    have_comp_size = (st.valid & ZIP_STAT_COMP_SIZE) != 0;
163
88.3k
    if (needs_decrypt || needs_decompress) {
164
75.8k
        empty_data = (have_comp_size && st.comp_size == 0);
165
75.8k
    }
166
12.4k
    else {
167
12.4k
        empty_data = (have_size && st.size == 0);
168
12.4k
    }
169
88.3k
    if (empty_data) {
170
12.5k
        if ((src = zip_source_buffer_with_attributes_create(NULL, 0, 0, &attributes, error)) == NULL) {
171
0
            return NULL;
172
0
        }
173
        /* If we created source buffer above, we want the window source to take ownership of it. */
174
12.5k
        take_ownership = true;
175
        /* if we created a buffer source above, then treat it as if
176
           reading the changed data - that way we don't need add another
177
           special case to the code below that wraps it in the window
178
           source */
179
12.5k
        changed_data = true;
180
12.5k
    }
181
75.7k
    else {
182
75.7k
        src = NULL;
183
75.7k
    }
184
185
88.3k
    if (partial_data && !needs_decrypt && !needs_decompress) {
186
0
        struct zip_stat st2;
187
0
        zip_t *source_archive;
188
0
        zip_uint64_t source_index;
189
190
0
        if (have_comp_size && data_len > 0 && start + (zip_uint64_t)data_len > st.comp_size) {
191
0
            zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_STORED_SIZE_MISMATCH, srcidx));
192
0
            if (take_ownership) {
193
0
                zip_source_free(src);
194
0
            }
195
0
            return NULL;
196
0
        }
197
198
0
        if (changed_data) {
199
0
            if (src == NULL) {
200
0
                src = srcza->entry[srcidx].source;
201
0
            }
202
0
            source_archive = NULL;
203
0
            source_index = 0;
204
0
        }
205
0
        else {
206
0
            src = srcza->src;
207
0
            source_archive = srcza;
208
0
            source_index = srcidx;
209
0
        }
210
211
0
        st2.comp_method = ZIP_CM_STORE;
212
0
        st2.valid = ZIP_STAT_COMP_METHOD;
213
0
        if (data_len >= 0) {
214
0
            st2.size = (zip_uint64_t)data_len;
215
0
            st2.comp_size = (zip_uint64_t)data_len;
216
0
            st2.valid |= ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE;
217
0
        }
218
0
        if (st.valid & ZIP_STAT_MTIME) {
219
0
            st2.mtime = st.mtime;
220
0
            st2.valid |= ZIP_STAT_MTIME;
221
0
        }
222
223
0
        s2 = _zip_source_window_new(src, start, data_len, &st2, ZIP_STAT_NAME, &attributes, &de->last_mod, source_archive, source_index, take_ownership, error);
224
0
        if (s2 == NULL) {
225
0
            if (take_ownership) {
226
0
                zip_source_free(src);
227
0
            }
228
0
            return NULL;
229
0
        }
230
0
        src = s2;
231
0
    }
232
    /* here we restrict src to file data, so no point in doing it for
233
       source that already represents only the file data */
234
88.3k
    else if (!changed_data) {
235
        /* this branch is executed only for archive sources; we know
236
           that stat data come from the archive too, so it's safe to
237
           assume that st has a comp_size specified */
238
75.7k
        if (take_ownership) {
239
0
            zip_source_free(src);
240
0
        }
241
75.7k
        if (st.comp_size > ZIP_INT64_MAX) {
242
365
            zip_error_set(error, ZIP_ER_INVAL, 0);
243
365
            return NULL;
244
365
        }
245
        /* despite the fact that we want the whole data file, we still
246
           wrap the source into a window source to add st and
247
           attributes and to have a source that positions the read
248
           offset properly before each read for multiple zip_file_t
249
           referring to the same underlying source */
250
75.4k
        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) {
251
14.5k
            return NULL;
252
14.5k
        }
253
75.4k
    }
254
12.5k
    else {
255
        /* this branch gets executed when reading the whole changed
256
           data file or when "reading" from a zero-sized source buffer
257
           that we created above */
258
12.5k
        if (src == NULL) {
259
0
            src = srcza->entry[srcidx].source;
260
0
        }
261
        /* despite the fact that we want the whole data file, we still
262
           wrap the source into a window source to add st and
263
           attributes and to have a source that positions the read
264
           offset properly before each read for multiple zip_file_t
265
           referring to the same underlying source */
266
12.5k
        s2 = _zip_source_window_new(src, 0, data_len, &st, ZIP_STAT_NAME, &attributes, &de->last_mod, NULL, 0, take_ownership, error);
267
12.5k
        if (s2 == NULL) {
268
2.28k
            if (take_ownership) {
269
2.28k
                zip_source_free(src);
270
2.28k
            }
271
2.28k
            return NULL;
272
2.28k
        }
273
10.2k
        src = s2;
274
10.2k
    }
275
276
    /* In all cases, src is a window source and therefore is owned by this function. */
277
278
71.0k
    if (_zip_source_set_source_archive(src, srcza) < 0) {
279
0
        zip_source_free(src);
280
0
        return NULL;
281
0
    }
282
283
    /* creating a layered source calls zip_keep() on the lower layer, so we free it */
284
285
71.0k
    if (needs_decrypt) {
286
20.5k
        zip_encryption_implementation enc_impl;
287
288
20.5k
        if ((enc_impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) {
289
1.06k
            zip_source_free(src);
290
1.06k
            zip_error_set(error, ZIP_ER_ENCRNOTSUPP, 0);
291
1.06k
            return NULL;
292
1.06k
        }
293
294
19.4k
        s2 = enc_impl(srcza, src, st.encryption_method, 0, password);
295
19.4k
        if (s2 == NULL) {
296
780
            zip_source_free(src);
297
780
            return NULL;
298
780
        }
299
300
18.6k
        src = s2;
301
18.6k
    }
302
69.2k
    if (needs_decompress) {
303
49.1k
        s2 = zip_source_decompress(srcza, src, st.comp_method);
304
49.1k
        if (s2 == NULL) {
305
3.13k
            zip_source_free(src);
306
3.13k
            return NULL;
307
3.13k
        }
308
46.0k
        src = s2;
309
46.0k
    }
310
66.1k
    if (needs_crc) {
311
66.1k
        s2 = zip_source_crc_create(src, 1, error);
312
66.1k
        if (s2 == NULL) {
313
0
            zip_source_free(src);
314
0
            return NULL;
315
0
        }
316
66.1k
        src = s2;
317
66.1k
    }
318
319
66.1k
    if (partial_data && (needs_decrypt || needs_decompress)) {
320
0
        zip_stat_t st2;
321
0
        zip_stat_init(&st2);
322
0
        if (data_len >= 0) {
323
0
            st2.valid = ZIP_STAT_SIZE;
324
0
            st2.size = (zip_uint64_t)data_len;
325
0
        }
326
0
        s2 = _zip_source_window_new(src, start, data_len, &st2, ZIP_STAT_NAME, NULL, NULL, NULL, 0, true, error);
327
0
        if (s2 == NULL) {
328
0
            zip_source_free(src);
329
0
            return NULL;
330
0
        }
331
0
        src = s2;
332
0
    }
333
334
66.1k
    return src;
335
66.1k
}
336
337
88.3k
static void _zip_file_attributes_from_dirent(zip_file_attributes_t *attributes, zip_dirent_t *de) {
338
88.3k
    zip_file_attributes_init(attributes);
339
88.3k
    attributes->valid = ZIP_FILE_ATTRIBUTES_ASCII | ZIP_FILE_ATTRIBUTES_HOST_SYSTEM | ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES | ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS;
340
88.3k
    attributes->ascii = de->int_attrib & 1;
341
88.3k
    attributes->host_system = de->version_madeby >> 8;
342
88.3k
    attributes->external_file_attributes = de->ext_attrib;
343
88.3k
    attributes->general_purpose_bit_flags = de->bitflags;
344
88.3k
    attributes->general_purpose_bit_mask = ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK;
345
88.3k
}