Rizin
unix-like reverse engineering framework and cli tools
zip_source_zip_new.c File Reference
#include <stdlib.h>
#include "zipint.h"

Go to the source code of this file.

Functions

static void _zip_file_attributes_from_dirent (zip_file_attributes_t *attributes, zip_dirent_t *de)
 
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)
 

Function Documentation

◆ _zip_file_attributes_from_dirent()

static void _zip_file_attributes_from_dirent ( zip_file_attributes_t attributes,
zip_dirent_t de 
)
static

Definition at line 182 of file zip_source_zip_new.c.

182  {
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 }
#define ZIP_FILE_ATTRIBUTES_ASCII
Definition: zip.h:330
ZIP_EXTERN void zip_file_attributes_init(zip_file_attributes_t *_Nonnull)
#define ZIP_FILE_ATTRIBUTES_HOST_SYSTEM
Definition: zip.h:329
#define ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES
Definition: zip.h:332
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS
Definition: zip.h:333
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
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
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK
Definition: zipint.h:97

References zip_file_attributes::ascii, zip_dirent::bitflags, zip_dirent::ext_attrib, zip_file_attributes::external_file_attributes, zip_file_attributes::general_purpose_bit_flags, zip_file_attributes::general_purpose_bit_mask, zip_file_attributes::host_system, zip_dirent::int_attrib, zip_file_attributes::valid, zip_dirent::version_madeby, ZIP_FILE_ATTRIBUTES_ASCII, ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES, ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS, ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK, ZIP_FILE_ATTRIBUTES_HOST_SYSTEM, and zip_file_attributes_init().

Referenced by _zip_source_zip_new().

◆ _zip_source_zip_new()

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 
)

Definition at line 41 of file zip_source_zip_new.c.

41  {
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;
112  st2.comp_method = ZIP_CM_STORE;
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 }
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
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
#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_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_ER_INVAL
Definition: zip.h:123
#define ZIP_STAT_MTIME
Definition: zip.h:294
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
bool deleted
Definition: zipint.h:412
Definition: zip.h:300
time_t mtime
Definition: zip.h:306
zip_uint16_t encryption_method
Definition: zip.h:309
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
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)
static void _zip_file_attributes_from_dirent(zip_file_attributes_t *attributes, zip_dirent_t *de)
#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_ENTRY_DATA_CHANGED(x)
Definition: zipint.h:476
#define ZIP_CODEC_DECODE
Definition: zipint.h:107

References _zip_file_attributes_from_dirent(), _zip_get_dirent(), _zip_get_encryption_implementation(), _zip_source_set_source_archive(), _zip_source_window_new(), zip_stat::comp_method, zip_stat::comp_size, zip::default_password, zip_entry::deleted, zip_stat::encryption_method, zip::entry, error(), flags, len, zip_stat::mtime, zip::nentry, NULL, s2, zip_stat::size, zip::src, src, start, zip_stat::valid, ZIP_CM_STORE, ZIP_CODEC_DECODE, ZIP_EM_NONE, ZIP_ENTRY_DATA_CHANGED, ZIP_ER_CHANGED, ZIP_ER_ENCRNOTSUPP, ZIP_ER_INTERNAL, ZIP_ER_INVAL, ZIP_ER_NOPASSWD, zip_error_set(), ZIP_FL_COMPRESSED, ZIP_FL_ENCRYPTED, ZIP_FL_UNCHANGED, ZIP_INT64_MAX, zip_source_buffer_with_attributes_create(), zip_source_crc_create(), zip_source_decompress(), zip_source_free(), zip_source_window_create(), ZIP_STAT_COMP_METHOD, ZIP_STAT_COMP_SIZE, zip_stat_index(), ZIP_STAT_MTIME, and ZIP_STAT_SIZE.

Referenced by zip_close(), zip_fopen_index_encrypted(), and zip_source_zip_create().