Rizin
unix-like reverse engineering framework and cli tools
zip_source_zip_new.c
Go to the documentation of this file.
1 /*
2  zip_source_zip_new.c -- prepare data structures for zip_fopen/zip_source_zip
3  Copyright (C) 2012-2021 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 
40 
42  zip_source_t *src, *s2;
43  zip_stat_t st;
44  zip_file_attributes_t attributes;
45  zip_dirent_t *de;
46  bool partial_data, needs_crc, needs_decrypt, needs_decompress;
47 
48  if (srcza == NULL || srcidx >= srcza->nentry || len > ZIP_INT64_MAX) {
50  return NULL;
51  }
52 
53  if ((flags & ZIP_FL_UNCHANGED) == 0 && (ZIP_ENTRY_DATA_CHANGED(srcza->entry + srcidx) || srcza->entry[srcidx].deleted)) {
55  return NULL;
56  }
57 
58  if (zip_stat_index(srcza, srcidx, flags | ZIP_FL_UNCHANGED, &st) < 0) {
60  return NULL;
61  }
62 
63  if (flags & ZIP_FL_ENCRYPTED) {
65  }
66 
67  if ((start > 0 || len > 0) && (flags & ZIP_FL_COMPRESSED)) {
69  return NULL;
70  }
71 
72  /* overflow or past end of file */
73  if ((start > 0 || len > 0) && (start + len < start || start + len > st.size)) {
75  return NULL;
76  }
77 
78  if (len == 0) {
79  len = st.size - start;
80  }
81 
82  partial_data = len < st.size;
83  needs_decrypt = ((flags & ZIP_FL_ENCRYPTED) == 0) && (st.encryption_method != ZIP_EM_NONE);
84  needs_decompress = ((flags & ZIP_FL_COMPRESSED) == 0) && (st.comp_method != ZIP_CM_STORE);
85  /* when reading the whole file, check for CRC errors */
86  needs_crc = ((flags & ZIP_FL_COMPRESSED) == 0 || st.comp_method == ZIP_CM_STORE) && !partial_data;
87 
88  if (needs_decrypt) {
89  if (password == NULL) {
90  password = srcza->default_password;
91  }
92  if (password == NULL) {
94  return NULL;
95  }
96  }
97 
98  if ((de = _zip_get_dirent(srcza, srcidx, flags, error)) == NULL) {
99  return NULL;
100  }
101  _zip_file_attributes_from_dirent(&attributes, de);
102 
103  if (st.comp_size == 0) {
104  return zip_source_buffer_with_attributes_create(NULL, 0, 0, &attributes, error);
105  }
106 
107  if (partial_data && !needs_decrypt && !needs_decompress) {
108  struct zip_stat st2;
109 
110  st2.size = len;
111  st2.comp_size = len;
113  st2.mtime = st.mtime;
115 
116  if ((src = _zip_source_window_new(srcza->src, start, (zip_int64_t)len, &st2, &attributes, srcza, srcidx, error)) == NULL) {
117  return NULL;
118  }
119  }
120  else {
121  if (st.comp_size > ZIP_INT64_MAX) {
123  return NULL;
124  }
125  if ((src = _zip_source_window_new(srcza->src, 0, (zip_int64_t)st.comp_size, &st, &attributes, srcza, srcidx, error)) == NULL) {
126  return NULL;
127  }
128  }
129 
130  if (_zip_source_set_source_archive(src, srcza) < 0) {
132  return NULL;
133  }
134 
135  /* creating a layered source calls zip_keep() on the lower layer, so we free it */
136 
137  if (needs_decrypt) {
139 
142  return NULL;
143  }
144 
145  s2 = enc_impl(srcza, src, st.encryption_method, 0, password);
147  if (s2 == NULL) {
148  return NULL;
149  }
150  src = s2;
151  }
152  if (needs_decompress) {
153  s2 = zip_source_decompress(srcza, src, st.comp_method);
155  if (s2 == NULL) {
156  return NULL;
157  }
158  src = s2;
159  }
160  if (needs_crc) {
163  if (s2 == NULL) {
164  return NULL;
165  }
166  src = s2;
167  }
168 
169  if (partial_data && (needs_decrypt || needs_decompress)) {
172  if (s2 == NULL) {
173  return NULL;
174  }
175  src = s2;
176  }
177 
178  return src;
179 }
180 
181 static void
183  zip_file_attributes_init(attributes);
185  attributes->ascii = de->int_attrib & 1;
186  attributes->host_system = de->version_madeby >> 8;
187  attributes->external_file_attributes = de->ext_attrib;
188  attributes->general_purpose_bit_flags = de->bitflags;
190 }
size_t len
Definition: 6502dis.c:15
lzma_index * src
Definition: index.h:567
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
#define ZIP_ER_INTERNAL
Definition: zip.h:125
#define ZIP_FILE_ATTRIBUTES_ASCII
Definition: zip.h:330
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_CM_STORE
Definition: zip.h:149
ZIP_EXTERN void zip_file_attributes_init(zip_file_attributes_t *_Nonnull)
#define ZIP_FILE_ATTRIBUTES_HOST_SYSTEM
Definition: zip.h:329
#define ZIP_ER_CHANGED
Definition: zip.h:120
#define ZIP_STAT_SIZE
Definition: zip.h:292
#define ZIP_ER_NOPASSWD
Definition: zip.h:131
#define ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES
Definition: zip.h:332
#define ZIP_FL_COMPRESSED
Definition: zip.h:78
#define ZIP_STAT_COMP_METHOD
Definition: zip.h:296
#define ZIP_ER_ENCRNOTSUPP
Definition: zip.h:129
#define ZIP_FL_ENCRYPTED
Definition: zip.h:81
#define ZIP_STAT_COMP_SIZE
Definition: zip.h:293
#define ZIP_EM_NONE
Definition: zip.h:177
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS
Definition: zip.h:333
#define ZIP_ER_INVAL
Definition: zip.h:123
#define ZIP_STAT_MTIME
Definition: zip.h:294
zip_uint32_t zip_flags_t
Definition: zip.h:347
ZIP_EXTERN void zip_source_free(zip_source_t *_Nullable)
ZIP_EXTERN int zip_stat_index(zip_t *_Nonnull, zip_uint64_t, zip_flags_t, zip_stat_t *_Nonnull)
ZIP_EXTERN zip_source_t *_Nullable zip_source_window_create(zip_source_t *_Nonnull, zip_uint64_t, zip_int64_t, zip_error_t *_Nullable)
#define ZIP_FL_UNCHANGED
Definition: zip.h:79
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
zip_uint16_t bitflags
Definition: zipint.h:335
zip_uint16_t int_attrib
Definition: zipint.h:345
zip_uint32_t ext_attrib
Definition: zipint.h:346
zip_uint16_t version_madeby
Definition: zipint.h:333
bool deleted
Definition: zipint.h:412
Definition: zip.h:284
zip_uint8_t host_system
Definition: zip.h:321
zip_uint16_t general_purpose_bit_mask
Definition: zip.h:326
zip_uint8_t ascii
Definition: zip.h:322
zip_uint16_t general_purpose_bit_flags
Definition: zip.h:325
zip_uint32_t external_file_attributes
Definition: zip.h:324
zip_uint64_t valid
Definition: zip.h:319
Definition: zip.h:300
time_t mtime
Definition: zip.h:306
zip_uint16_t encryption_method
Definition: zip.h:309
zip_uint64_t valid
Definition: zip.h:301
zip_uint16_t comp_method
Definition: zip.h:308
zip_uint64_t comp_size
Definition: zip.h:305
zip_uint64_t size
Definition: zip.h:304
Definition: zipint.h:278
zip_source_t * src
Definition: zipint.h:279
zip_entry_t * entry
Definition: zipint.h:294
zip_uint64_t nentry
Definition: zipint.h:292
char * default_password
Definition: zipint.h:286
void error(const char *msg)
Definition: untgz.c:593
zip_dirent_t * _zip_get_dirent(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error)
Definition: zip_dirent.c:1064
zip_encryption_implementation _zip_get_encryption_implementation(zip_uint16_t em, int operation)
zip_source_t * zip_source_buffer_with_attributes_create(const void *data, zip_uint64_t len, int freep, zip_file_attributes_t *attributes, zip_error_t *error)
zip_source_t * zip_source_decompress(zip_t *za, zip_source_t *src, zip_int32_t method)
zip_source_t * zip_source_crc_create(zip_source_t *src, int validate, zip_error_t *error)
int _zip_source_set_source_archive(zip_source_t *src, zip_t *za)
zip_source_t * _zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_int64_t length, zip_stat_t *st, zip_file_attributes_t *attributes, zip_t *source_archive, zip_uint64_t source_index, zip_error_t *error)
zip_source_t * _zip_source_zip_new(zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_uint64_t len, const char *password, zip_error_t *error)
static void _zip_file_attributes_from_dirent(zip_file_attributes_t *attributes, zip_dirent_t *de)
uint64_t zip_uint64_t
Definition: zipconf.h:39
#define ZIP_INT64_MAX
Definition: zipconf.h:54
int64_t zip_int64_t
Definition: zipconf.h:38
zip_source_t *(* zip_encryption_implementation)(zip_t *, zip_source_t *, zip_uint16_t, int, const char *)
Definition: zipint.h:110
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK
Definition: zipint.h:97
#define ZIP_ENTRY_DATA_CHANGED(x)
Definition: zipint.h:476
#define ZIP_CODEC_DECODE
Definition: zipint.h:107