Rizin
unix-like reverse engineering framework and cli tools
zip_dirent.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include "zipint.h"

Go to the source code of this file.

Functions

static zip_string_t_zip_dirent_process_ef_utf_8 (const zip_dirent_t *de, zip_uint16_t id, zip_string_t *str)
 
static zip_extra_field_t_zip_ef_utf8 (zip_uint16_t, zip_string_t *, zip_error_t *)
 
static bool _zip_dirent_process_winzip_aes (zip_dirent_t *de, zip_error_t *error)
 
void _zip_cdir_free (zip_cdir_t *cd)
 
zip_cdir_t_zip_cdir_new (zip_uint64_t nentry, zip_error_t *error)
 
bool _zip_cdir_grow (zip_cdir_t *cd, zip_uint64_t additional_entries, zip_error_t *error)
 
zip_int64_t _zip_cdir_write (zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors)
 
zip_dirent_t_zip_dirent_clone (const zip_dirent_t *sde)
 
void _zip_dirent_finalize (zip_dirent_t *zde)
 
void _zip_dirent_free (zip_dirent_t *zde)
 
void _zip_dirent_init (zip_dirent_t *de)
 
bool _zip_dirent_needs_zip64 (const zip_dirent_t *de, zip_flags_t flags)
 
zip_dirent_t_zip_dirent_new (void)
 
zip_int64_t _zip_dirent_read (zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, bool local, zip_error_t *error)
 
zip_int32_t _zip_dirent_size (zip_source_t *src, zip_uint16_t flags, zip_error_t *error)
 
int _zip_dirent_write (zip_t *za, zip_dirent_t *de, zip_flags_t flags)
 
time_t _zip_d2u_time (zip_uint16_t dtime, zip_uint16_t ddate)
 
zip_dirent_t_zip_get_dirent (zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error)
 
void _zip_u2d_time (time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate)
 
void _zip_dirent_apply_attributes (zip_dirent_t *de, zip_file_attributes_t *attributes, bool force_zip64, zip_uint32_t changed)
 

Function Documentation

◆ _zip_cdir_free()

void _zip_cdir_free ( zip_cdir_t cd)

Definition at line 49 of file zip_dirent.c.

49  {
51 
52  if (!cd)
53  return;
54 
55  for (i = 0; i < cd->nentry; i++)
56  _zip_entry_finalize(cd->entry + i);
57  free(cd->entry);
58  _zip_string_free(cd->comment);
59  free(cd);
60 }
lzma_index ** i
Definition: index.h:629
static csh cd
Definition: asm_mips_cs.c:10
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void _zip_entry_finalize(zip_entry_t *e)
Definition: zip_entry.c:38
void _zip_string_free(zip_string_t *s)
Definition: zip_string.c:69
uint64_t zip_uint64_t
Definition: zipconf.h:39

References _zip_entry_finalize(), _zip_string_free(), cd, free(), and i.

Referenced by _zip_cdir_new(), _zip_find_central_dir(), and _zip_read_cdir().

◆ _zip_cdir_grow()

bool _zip_cdir_grow ( zip_cdir_t cd,
zip_uint64_t  additional_entries,
zip_error_t error 
)

Definition at line 88 of file zip_dirent.c.

88  {
89  zip_uint64_t i, new_alloc;
90  zip_entry_t *new_entry;
91 
92  if (additional_entries == 0) {
93  return true;
94  }
95 
96  new_alloc = cd->nentry_alloc + additional_entries;
97 
98  if (new_alloc < additional_entries || new_alloc > SIZE_MAX / sizeof(*(cd->entry))) {
100  return false;
101  }
102 
103  if ((new_entry = (zip_entry_t *)realloc(cd->entry, sizeof(*(cd->entry)) * (size_t)new_alloc)) == NULL) {
105  return false;
106  }
107 
108  cd->entry = new_entry;
109 
110  for (i = cd->nentry; i < new_alloc; i++) {
111  _zip_entry_init(cd->entry + i);
112  }
113 
114  cd->nentry = cd->nentry_alloc = new_alloc;
115 
116  return true;
117 }
#define NULL
Definition: cris-opc.c:27
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_MEMORY
Definition: zip.h:119
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
#define SIZE_MAX
Definition: zipint.h:408
void error(const char *msg)
Definition: untgz.c:593
void _zip_entry_init(zip_entry_t *e)
Definition: zip_entry.c:46

References _zip_entry_init(), cd, error(), i, NULL, realloc(), SIZE_MAX, ZIP_ER_MEMORY, and zip_error_set().

Referenced by _zip_cdir_new(), and _zip_read_cdir().

◆ _zip_cdir_new()

zip_cdir_t* _zip_cdir_new ( zip_uint64_t  nentry,
zip_error_t error 
)

Definition at line 64 of file zip_dirent.c.

64  {
65  zip_cdir_t *cd;
66 
67  if ((cd = (zip_cdir_t *)malloc(sizeof(*cd))) == NULL) {
69  return NULL;
70  }
71 
72  cd->entry = NULL;
73  cd->nentry = cd->nentry_alloc = 0;
74  cd->size = cd->offset = 0;
75  cd->comment = NULL;
76  cd->is_zip64 = false;
77 
78  if (!_zip_cdir_grow(cd, nentry, error)) {
80  return NULL;
81  }
82 
83  return cd;
84 }
void * malloc(size_t size)
Definition: malloc.c:123
void _zip_cdir_free(zip_cdir_t *cd)
Definition: zip_dirent.c:49
bool _zip_cdir_grow(zip_cdir_t *cd, zip_uint64_t additional_entries, zip_error_t *error)
Definition: zip_dirent.c:88

References _zip_cdir_free(), _zip_cdir_grow(), cd, error(), malloc(), NULL, ZIP_ER_MEMORY, and zip_error_set().

Referenced by _zip_read_eocd(), and _zip_read_eocd64().

◆ _zip_cdir_write()

zip_int64_t _zip_cdir_write ( zip_t za,
const zip_filelist_t filelist,
zip_uint64_t  survivors 
)

Definition at line 121 of file zip_dirent.c.

121  {
123  zip_string_t *comment;
127  zip_uint64_t i;
128  bool is_zip64;
129  int ret;
130 
131  if ((off = zip_source_tell_write(za->src)) < 0) {
133  return -1;
134  }
136 
137  is_zip64 = false;
138 
139  for (i = 0; i < survivors; i++) {
140  zip_entry_t *entry = za->entry + filelist[i].idx;
141 
142  if ((ret = _zip_dirent_write(za, entry->changes ? entry->changes : entry->orig, ZIP_FL_CENTRAL)) < 0)
143  return -1;
144  if (ret)
145  is_zip64 = true;
146  }
147 
148  if ((off = zip_source_tell_write(za->src)) < 0) {
150  return -1;
151  }
153 
154  if (offset > ZIP_UINT32_MAX || survivors > ZIP_UINT16_MAX)
155  is_zip64 = true;
156 
157 
158  if ((buffer = _zip_buffer_new(buf, sizeof(buf))) == NULL) {
160  return -1;
161  }
162 
163  if (is_zip64) {
170  _zip_buffer_put_64(buffer, survivors);
171  _zip_buffer_put_64(buffer, survivors);
178  }
179 
182  _zip_buffer_put_16(buffer, (zip_uint16_t)(survivors >= ZIP_UINT16_MAX ? ZIP_UINT16_MAX : survivors));
183  _zip_buffer_put_16(buffer, (zip_uint16_t)(survivors >= ZIP_UINT16_MAX ? ZIP_UINT16_MAX : survivors));
186 
188 
189  _zip_buffer_put_16(buffer, (zip_uint16_t)(comment ? comment->length : 0));
190 
191  if (!_zip_buffer_ok(buffer)) {
194  return -1;
195  }
196 
199  return -1;
200  }
201 
203 
204  if (comment) {
205  if (_zip_write(za, comment->raw, comment->length) < 0) {
206  return -1;
207  }
208  }
209 
210  return (zip_int64_t)size;
211 }
struct buffer buffer
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
#define ZIP_ER_INTERNAL
Definition: zip.h:125
ZIP_EXTERN zip_int64_t zip_source_tell_write(zip_source_t *_Nonnull)
#define ZIP_FL_CENTRAL
Definition: zip.h:86
int off
Definition: pal.c:13
Definition: buffer.h:15
Definition: zipcmp.c:77
zip_uint64_t idx
Definition: zipint.h:458
zip_uint8_t * raw
Definition: zipint.h:419
zip_uint16_t length
Definition: zipint.h:420
zip_error_t error
Definition: zipint.h:281
zip_string_t * comment_changes
Definition: zipint.h:289
zip_source_t * src
Definition: zipint.h:279
zip_entry_t * entry
Definition: zipint.h:294
zip_string_t * comment_orig
Definition: zipint.h:288
bool comment_changed
Definition: zipint.h:290
zip_uint8_t * _zip_buffer_data(zip_buffer_t *buffer)
Definition: zip_buffer.c:40
int _zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i)
Definition: zip_buffer.c:230
zip_buffer_t * _zip_buffer_new(zip_uint8_t *data, zip_uint64_t size)
Definition: zip_buffer.c:146
void _zip_buffer_free(zip_buffer_t *buffer)
Definition: zip_buffer.c:46
bool _zip_buffer_ok(zip_buffer_t *buffer)
Definition: zip_buffer.c:198
int _zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i)
Definition: zip_buffer.c:262
int _zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i)
Definition: zip_buffer.c:245
zip_uint64_t _zip_buffer_offset(zip_buffer_t *buffer)
Definition: zip_buffer.c:192
int _zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length)
Definition: zip_buffer.c:217
int _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags)
Definition: zip_dirent.c:765
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
int _zip_write(zip_t *za, const void *data, zip_uint64_t length)
Definition: zip_io_util.c:121
#define ZIP_UINT16_MAX
Definition: zipconf.h:47
#define ZIP_UINT32_MAX
Definition: zipconf.h:51
uint32_t zip_uint32_t
Definition: zipconf.h:37
uint8_t zip_uint8_t
Definition: zipconf.h:33
uint16_t zip_uint16_t
Definition: zipconf.h:35
int64_t zip_int64_t
Definition: zipconf.h:38
#define EOCD_MAGIC
Definition: zipint.h:53
#define EOCD64LEN
Definition: zipint.h:63
#define EOCDLEN
Definition: zipint.h:61
#define EOCD64_MAGIC
Definition: zipint.h:56
#define EOCD64LOCLEN
Definition: zipint.h:62
#define EOCD64LOC_MAGIC
Definition: zipint.h:55
zip_t * za
Definition: ziptool.c:79

References _zip_buffer_data(), _zip_buffer_free(), _zip_buffer_new(), _zip_buffer_offset(), _zip_buffer_ok(), _zip_buffer_put(), _zip_buffer_put_16(), _zip_buffer_put_32(), _zip_buffer_put_64(), _zip_dirent_write(), _zip_error_set_from_source(), _zip_write(), zip::comment_changed, zip::comment_changes, zip::comment_orig, zip::entry, EOCD64_MAGIC, EOCD64LEN, EOCD64LOC_MAGIC, EOCD64LOCLEN, EOCD_MAGIC, EOCDLEN, zip::error, i, zip_filelist::idx, zip_string::length, NULL, off, zip_string::raw, zip::src, za, ZIP_ER_INTERNAL, ZIP_ER_MEMORY, zip_error_set(), ZIP_FL_CENTRAL, zip_source_tell_write(), ZIP_UINT16_MAX, and ZIP_UINT32_MAX.

Referenced by write_cdir().

◆ _zip_d2u_time()

time_t _zip_d2u_time ( zip_uint16_t  dtime,
zip_uint16_t  ddate 
)

Definition at line 1004 of file zip_dirent.c.

1004  {
1005  struct tm tm;
1006 
1007  memset(&tm, 0, sizeof(tm));
1008 
1009  /* let mktime decide if DST is in effect */
1010  tm.tm_isdst = -1;
1011 
1012  tm.tm_year = ((ddate >> 9) & 127) + 1980 - 1900;
1013  tm.tm_mon = ((ddate >> 5) & 15) - 1;
1014  tm.tm_mday = ddate & 31;
1015 
1016  tm.tm_hour = (dtime >> 11) & 31;
1017  tm.tm_min = (dtime >> 5) & 63;
1018  tm.tm_sec = (dtime << 1) & 62;
1019 
1020  return mktime(&tm);
1021 }
return memset(p, 0, total)

References memset().

Referenced by _zip_dirent_read(), and zip_file_set_dostime().

◆ _zip_dirent_apply_attributes()

void _zip_dirent_apply_attributes ( zip_dirent_t de,
zip_file_attributes_t attributes,
bool  force_zip64,
zip_uint32_t  changed 
)

Definition at line 1117 of file zip_dirent.c.

1117  {
1119 
1122  de->bitflags = (de->bitflags & ~mask) | (attributes->general_purpose_bit_flags & mask);
1123  }
1124  if (attributes->valid & ZIP_FILE_ATTRIBUTES_ASCII) {
1125  de->int_attrib = (de->int_attrib & ~0x1) | (attributes->ascii ? 1 : 0);
1126  }
1127  /* manually set attributes are preferred over attributes provided by source */
1128  if ((changed & ZIP_DIRENT_ATTRIBUTES) == 0 && (attributes->valid & ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES)) {
1129  de->ext_attrib = attributes->external_file_attributes;
1130  }
1131 
1132  if (de->comp_method == ZIP_CM_LZMA) {
1133  de->version_needed = 63;
1134  }
1136  de->version_needed = 51;
1137  }
1138  else if (de->comp_method == ZIP_CM_BZIP2) {
1139  de->version_needed = 46;
1140  }
1141  else if (force_zip64 || _zip_dirent_needs_zip64(de, 0)) {
1142  de->version_needed = 45;
1143  }
1144  else if (de->comp_method == ZIP_CM_DEFLATE || de->encryption_method == ZIP_EM_TRAD_PKWARE) {
1145  de->version_needed = 20;
1146  }
1147  else if ((length = _zip_string_length(de->filename)) > 0 && de->filename->raw[length - 1] == '/') {
1148  de->version_needed = 20;
1149  }
1150  else {
1151  de->version_needed = 10;
1152  }
1153 
1154  if (attributes->valid & ZIP_FILE_ATTRIBUTES_VERSION_NEEDED) {
1155  de->version_needed = ZIP_MAX(de->version_needed, attributes->version_needed);
1156  }
1157 
1158  de->version_madeby = 63 | (de->version_madeby & 0xff00);
1159  if ((changed & ZIP_DIRENT_ATTRIBUTES) == 0 && (attributes->valid & ZIP_FILE_ATTRIBUTES_HOST_SYSTEM)) {
1160  de->version_madeby = (de->version_madeby & 0xff) | (zip_uint16_t)(attributes->host_system << 8);
1161  }
1162 }
#define mask()
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 length
Definition: sflib.h:133
#define ZIP_FILE_ATTRIBUTES_ASCII
Definition: zip.h:330
#define ZIP_FILE_ATTRIBUTES_HOST_SYSTEM
Definition: zip.h:329
#define ZIP_EM_TRAD_PKWARE
Definition: zip.h:178
#define ZIP_EM_AES_256
Definition: zip.h:192
#define ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES
Definition: zip.h:332
#define ZIP_CM_BZIP2
Definition: zip.h:161
#define ZIP_CM_LZMA
Definition: zip.h:163
#define ZIP_EM_AES_192
Definition: zip.h:191
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS
Definition: zip.h:333
#define ZIP_FILE_ATTRIBUTES_VERSION_NEEDED
Definition: zip.h:331
#define ZIP_CM_DEFLATE
Definition: zip.h:157
#define ZIP_EM_AES_128
Definition: zip.h:190
zip_uint16_t bitflags
Definition: zipint.h:335
zip_uint16_t int_attrib
Definition: zipint.h:345
zip_int32_t comp_method
Definition: zipint.h:336
zip_uint16_t version_needed
Definition: zipint.h:334
zip_uint32_t ext_attrib
Definition: zipint.h:346
zip_uint16_t encryption_method
Definition: zipint.h:350
zip_uint16_t version_madeby
Definition: zipint.h:333
zip_string_t * filename
Definition: zipint.h:341
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_uint8_t version_needed
Definition: zip.h:323
zip_uint32_t external_file_attributes
Definition: zip.h:324
zip_uint64_t valid
Definition: zip.h:319
bool _zip_dirent_needs_zip64(const zip_dirent_t *de, zip_flags_t flags)
Definition: zip_dirent.c:296
zip_uint16_t _zip_string_length(const zip_string_t *s)
Definition: zip_string.c:112
#define ZIP_DIRENT_ATTRIBUTES
Definition: zipint.h:320
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK
Definition: zipint.h:97
#define ZIP_MAX(a, b)
Definition: zipint.h:472

References _zip_dirent_needs_zip64(), _zip_string_length(), zip_file_attributes::ascii, zip_dirent::bitflags, zip_dirent::comp_method, zip_dirent::encryption_method, zip_dirent::ext_attrib, zip_file_attributes::external_file_attributes, zip_dirent::filename, zip_file_attributes::general_purpose_bit_flags, zip_file_attributes::general_purpose_bit_mask, zip_file_attributes::host_system, zip_dirent::int_attrib, length, mask, zip_string::raw, zip_file_attributes::valid, zip_dirent::version_madeby, zip_file_attributes::version_needed, zip_dirent::version_needed, ZIP_CM_BZIP2, ZIP_CM_DEFLATE, ZIP_CM_LZMA, ZIP_DIRENT_ATTRIBUTES, ZIP_EM_AES_128, ZIP_EM_AES_192, ZIP_EM_AES_256, ZIP_EM_TRAD_PKWARE, 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, ZIP_FILE_ATTRIBUTES_VERSION_NEEDED, and ZIP_MAX.

Referenced by add_data().

◆ _zip_dirent_clone()

zip_dirent_t* _zip_dirent_clone ( const zip_dirent_t sde)

Definition at line 215 of file zip_dirent.c.

215  {
216  zip_dirent_t *tde;
217 
218  if ((tde = (zip_dirent_t *)malloc(sizeof(*tde))) == NULL)
219  return NULL;
220 
221  if (sde)
222  memcpy(tde, sde, sizeof(*sde));
223  else
224  _zip_dirent_init(tde);
225 
226  tde->changed = 0;
227  tde->cloned = 1;
228 
229  return tde;
230 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
bool cloned
Definition: zipint.h:329
zip_uint32_t changed
Definition: zipint.h:327
void _zip_dirent_init(zip_dirent_t *de)
Definition: zip_dirent.c:268

References _zip_dirent_init(), zip_dirent::changed, zip_dirent::cloned, malloc(), memcpy(), and NULL.

Referenced by _zip_file_extra_field_prepare_for_change(), _zip_file_replace(), _zip_set_name(), zip_close(), zip_file_set_comment(), zip_file_set_encryption(), zip_file_set_external_attributes(), zip_file_set_mtime(), and zip_set_file_compression().

◆ _zip_dirent_finalize()

void _zip_dirent_finalize ( zip_dirent_t zde)

Definition at line 234 of file zip_dirent.c.

234  {
235  if (!zde->cloned || zde->changed & ZIP_DIRENT_FILENAME) {
237  zde->filename = NULL;
238  }
239  if (!zde->cloned || zde->changed & ZIP_DIRENT_EXTRA_FIELD) {
241  zde->extra_fields = NULL;
242  }
243  if (!zde->cloned || zde->changed & ZIP_DIRENT_COMMENT) {
245  zde->comment = NULL;
246  }
247  if (!zde->cloned || zde->changed & ZIP_DIRENT_PASSWORD) {
248  if (zde->password) {
249  _zip_crypto_clear(zde->password, strlen(zde->password));
250  }
251  free(zde->password);
252  zde->password = NULL;
253  }
254 }
char * password
Definition: zipint.h:351
zip_extra_field_t * extra_fields
Definition: zipint.h:342
zip_string_t * comment
Definition: zipint.h:343
void _zip_ef_free(zip_extra_field_t *ef)
#define ZIP_DIRENT_PASSWORD
Definition: zipint.h:323
#define ZIP_DIRENT_EXTRA_FIELD
Definition: zipint.h:319
#define ZIP_DIRENT_FILENAME
Definition: zipint.h:317
#define _zip_crypto_clear(b, l)
Definition: zipint.h:489
#define ZIP_DIRENT_COMMENT
Definition: zipint.h:318

References _zip_crypto_clear, _zip_ef_free(), _zip_string_free(), zip_dirent::changed, zip_dirent::cloned, zip_dirent::comment, zip_dirent::extra_fields, zip_dirent::filename, free(), NULL, zip_dirent::password, ZIP_DIRENT_COMMENT, ZIP_DIRENT_EXTRA_FIELD, ZIP_DIRENT_FILENAME, and ZIP_DIRENT_PASSWORD.

Referenced by _zip_checkcons(), and _zip_dirent_free().

◆ _zip_dirent_free()

void _zip_dirent_free ( zip_dirent_t zde)

Definition at line 258 of file zip_dirent.c.

258  {
259  if (zde == NULL)
260  return;
261 
263  free(zde);
264 }
void _zip_dirent_finalize(zip_dirent_t *zde)
Definition: zip_dirent.c:234

References _zip_dirent_finalize(), free(), and NULL.

Referenced by _zip_entry_finalize(), _zip_set_name(), _zip_unchange(), _zip_unchange_data(), zip_file_set_comment(), zip_file_set_encryption(), zip_file_set_external_attributes(), and zip_set_file_compression().

◆ _zip_dirent_init()

void _zip_dirent_init ( zip_dirent_t de)

Definition at line 268 of file zip_dirent.c.

268  {
269  de->changed = 0;
270  de->local_extra_fields_read = 0;
271  de->cloned = 0;
272 
273  de->crc_valid = true;
274  de->version_madeby = 63 | (ZIP_OPSYS_DEFAULT << 8);
275  de->version_needed = 10; /* 1.0 */
276  de->bitflags = 0;
278  de->last_mod = 0;
279  de->crc = 0;
280  de->comp_size = 0;
281  de->uncomp_size = 0;
282  de->filename = NULL;
283  de->extra_fields = NULL;
284  de->comment = NULL;
285  de->disk_number = 0;
286  de->int_attrib = 0;
288  de->offset = 0;
289  de->compression_level = 0;
291  de->password = NULL;
292 }
#define ZIP_EM_NONE
Definition: zip.h:177
#define ZIP_CM_DEFAULT
Definition: zip.h:148
#define ZIP_OPSYS_DEFAULT
Definition: zip.h:216
zip_uint32_t disk_number
Definition: zipint.h:344
bool local_extra_fields_read
Definition: zipint.h:328
time_t last_mod
Definition: zipint.h:337
zip_uint64_t uncomp_size
Definition: zipint.h:340
zip_uint32_t crc
Definition: zipint.h:338
zip_uint64_t offset
Definition: zipint.h:347
zip_uint64_t comp_size
Definition: zipint.h:339
bool crc_valid
Definition: zipint.h:331
zip_uint16_t compression_level
Definition: zipint.h:349
#define ZIP_EXT_ATTRIB_DEFAULT
Definition: zipint.h:93

References zip_dirent::bitflags, zip_dirent::changed, zip_dirent::cloned, zip_dirent::comment, zip_dirent::comp_method, zip_dirent::comp_size, zip_dirent::compression_level, zip_dirent::crc, zip_dirent::crc_valid, zip_dirent::disk_number, zip_dirent::encryption_method, zip_dirent::ext_attrib, zip_dirent::extra_fields, zip_dirent::filename, zip_dirent::int_attrib, zip_dirent::last_mod, zip_dirent::local_extra_fields_read, NULL, zip_dirent::offset, zip_dirent::password, zip_dirent::uncomp_size, zip_dirent::version_madeby, zip_dirent::version_needed, ZIP_CM_DEFAULT, ZIP_EM_NONE, ZIP_EXT_ATTRIB_DEFAULT, and ZIP_OPSYS_DEFAULT.

Referenced by _zip_checkcons(), _zip_dirent_clone(), _zip_dirent_new(), and _zip_dirent_read().

◆ _zip_dirent_needs_zip64()

bool _zip_dirent_needs_zip64 ( const zip_dirent_t de,
zip_flags_t  flags 
)

Definition at line 296 of file zip_dirent.c.

296  {
298  return true;
299 
300  return false;
301 }
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123

References zip_dirent::comp_size, flags, zip_dirent::offset, zip_dirent::uncomp_size, ZIP_FL_CENTRAL, and ZIP_UINT32_MAX.

Referenced by _zip_dirent_apply_attributes(), _zip_dirent_write(), _zip_file_get_end(), and zip_close().

◆ _zip_dirent_new()

zip_dirent_t* _zip_dirent_new ( void  )

Definition at line 305 of file zip_dirent.c.

305  {
306  zip_dirent_t *de;
307 
308  if ((de = (zip_dirent_t *)malloc(sizeof(*de))) == NULL)
309  return NULL;
310 
311  _zip_dirent_init(de);
312  return de;
313 }

References _zip_dirent_init(), malloc(), and NULL.

Referenced by _zip_read_cdir().

◆ _zip_dirent_process_ef_utf_8()

static zip_string_t * _zip_dirent_process_ef_utf_8 ( const zip_dirent_t de,
zip_uint16_t  id,
zip_string_t str 
)
static

Definition at line 604 of file zip_dirent.c.

604  {
605  zip_uint16_t ef_len;
606  zip_uint32_t ef_crc;
608 
609  const zip_uint8_t *ef = _zip_ef_get_by_id(de->extra_fields, &ef_len, id, 0, ZIP_EF_BOTH, NULL);
610 
611  if (ef == NULL || ef_len < 5 || ef[0] != 1) {
612  return str;
613  }
614 
615  if ((buffer = _zip_buffer_new((zip_uint8_t *)ef, ef_len)) == NULL) {
616  return str;
617  }
618 
620  ef_crc = _zip_buffer_get_32(buffer);
621 
622  if (_zip_string_crc32(str) == ef_crc) {
625 
626  if (ef_str != NULL) {
628  str = ef_str;
629  }
630  }
631 
633 
634  return str;
635 }
size_t len
Definition: 6502dis.c:15
#define ZIP_FL_ENC_UTF_8
Definition: zip.h:88
Definition: zipcmp.c:69
zip_uint32_t _zip_buffer_get_32(zip_buffer_t *buffer)
Definition: zip_buffer.c:92
zip_uint64_t _zip_buffer_left(zip_buffer_t *buffer)
Definition: zip_buffer.c:128
zip_uint8_t _zip_buffer_get_8(zip_buffer_t *buffer)
Definition: zip_buffer.c:116
zip_uint8_t * _zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length)
Definition: zip_buffer.c:66
const zip_uint8_t * _zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags, zip_error_t *error)
zip_uint32_t _zip_string_crc32(const zip_string_t *s)
Definition: zip_string.c:42
zip_string_t * _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error)
Definition: zip_string.c:121
#define ZIP_EF_BOTH
Definition: zipint.h:245

References _zip_buffer_free(), _zip_buffer_get(), _zip_buffer_get_32(), _zip_buffer_get_8(), _zip_buffer_left(), _zip_buffer_new(), _zip_ef_get_by_id(), _zip_string_crc32(), _zip_string_free(), _zip_string_new(), zip_dirent::extra_fields, len, NULL, cmd_descs_generate::str, ZIP_EF_BOTH, and ZIP_FL_ENC_UTF_8.

Referenced by _zip_dirent_read().

◆ _zip_dirent_process_winzip_aes()

static bool _zip_dirent_process_winzip_aes ( zip_dirent_t de,
zip_error_t error 
)
static

Definition at line 639 of file zip_dirent.c.

639  {
640  zip_uint16_t ef_len;
642  const zip_uint8_t *ef;
643  bool crc_valid;
644  zip_uint16_t enc_method;
645 
646 
647  if (de->comp_method != ZIP_CM_WINZIP_AES) {
648  return true;
649  }
650 
652 
653  if (ef == NULL || ef_len < 7) {
655  return false;
656  }
657 
658  if ((buffer = _zip_buffer_new((zip_uint8_t *)ef, ef_len)) == NULL) {
660  return false;
661  }
662 
663  /* version */
664 
665  crc_valid = true;
666  switch (_zip_buffer_get_16(buffer)) {
667  case 1:
668  break;
669 
670  case 2:
671  crc_valid = false;
672  /* TODO: When checking consistency, check that crc is 0. */
673  break;
674 
675  default:
678  return false;
679  }
680 
681  /* vendor */
682  if (memcmp(_zip_buffer_get(buffer, 2), "AE", 2) != 0) {
685  return false;
686  }
687 
688  /* mode */
689  switch (_zip_buffer_get_8(buffer)) {
690  case 1:
691  enc_method = ZIP_EM_AES_128;
692  break;
693  case 2:
694  enc_method = ZIP_EM_AES_192;
695  break;
696  case 3:
697  enc_method = ZIP_EM_AES_256;
698  break;
699  default:
702  return false;
703  }
704 
705  if (ef_len != 7) {
708  return false;
709  }
710 
711  de->crc_valid = crc_valid;
712  de->encryption_method = enc_method;
714 
716  return true;
717 }
#define ef(frag,...)
#define ZIP_ER_ENCRNOTSUPP
Definition: zip.h:129
#define ZIP_ER_INCONS
Definition: zip.h:126
zip_uint16_t _zip_buffer_get_16(zip_buffer_t *buffer)
Definition: zip_buffer.c:80
#define ZIP_EF_WINZIP_AES
Definition: zipint.h:87
#define ZIP_ER_DETAIL_INVALID_WINZIPAES_EF
Definition: zipint.h:229
#define ZIP_CM_WINZIP_AES
Definition: zipint.h:73

References _zip_buffer_free(), _zip_buffer_get(), _zip_buffer_get_16(), _zip_buffer_get_8(), _zip_buffer_new(), _zip_ef_get_by_id(), zip_dirent::comp_method, zip_dirent::crc_valid, ef, zip_dirent::encryption_method, error(), zip_dirent::extra_fields, NULL, ZIP_CM_WINZIP_AES, ZIP_EF_BOTH, ZIP_EF_WINZIP_AES, ZIP_EM_AES_128, ZIP_EM_AES_192, ZIP_EM_AES_256, ZIP_ER_DETAIL_INVALID_WINZIPAES_EF, ZIP_ER_ENCRNOTSUPP, ZIP_ER_INCONS, ZIP_ER_INTERNAL, and zip_error_set().

Referenced by _zip_dirent_read().

◆ _zip_dirent_read()

zip_int64_t _zip_dirent_read ( zip_dirent_t zde,
zip_source_t src,
zip_buffer_t buffer,
bool  local,
zip_error_t error 
)

Definition at line 327 of file zip_dirent.c.

327  {
329  zip_uint16_t dostime, dosdate;
330  zip_uint32_t size, variable_size;
331  zip_uint16_t filename_len, comment_len, ef_len;
332 
333  bool from_buffer = (buffer != NULL);
334 
336 
337  if (buffer) {
338  if (_zip_buffer_left(buffer) < size) {
340  return -1;
341  }
342  }
343  else {
345  return -1;
346  }
347  }
348 
349  if (memcmp(_zip_buffer_get(buffer, 4), (local ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) {
351  if (!from_buffer) {
353  }
354  return -1;
355  }
356 
357  /* convert buffercontents to zip_dirent */
358 
359  _zip_dirent_init(zde);
360  if (!local)
362  else
363  zde->version_madeby = 0;
367 
368  /* convert to time_t */
369  dostime = _zip_buffer_get_16(buffer);
370  dosdate = _zip_buffer_get_16(buffer);
371  zde->last_mod = _zip_d2u_time(dostime, dosdate);
372 
373  zde->crc = _zip_buffer_get_32(buffer);
376 
377  filename_len = _zip_buffer_get_16(buffer);
378  ef_len = _zip_buffer_get_16(buffer);
379 
380  if (local) {
381  comment_len = 0;
382  zde->disk_number = 0;
383  zde->int_attrib = 0;
384  zde->ext_attrib = 0;
385  zde->offset = 0;
386  }
387  else {
388  comment_len = _zip_buffer_get_16(buffer);
393  }
394 
395  if (!_zip_buffer_ok(buffer)) {
397  if (!from_buffer) {
399  }
400  return -1;
401  }
402 
403  if (zde->bitflags & ZIP_GPBF_ENCRYPTED) {
405  /* TODO */
407  }
408  else {
410  }
411  }
412  else {
414  }
415 
416  zde->filename = NULL;
417  zde->extra_fields = NULL;
418  zde->comment = NULL;
419 
420  variable_size = (zip_uint32_t)filename_len + (zip_uint32_t)ef_len + (zip_uint32_t)comment_len;
421 
422  if (from_buffer) {
423  if (_zip_buffer_left(buffer) < variable_size) {
425  return -1;
426  }
427  }
428  else {
430 
431  if ((buffer = _zip_buffer_new_from_source(src, variable_size, NULL, error)) == NULL) {
432  return -1;
433  }
434  }
435 
436  if (filename_len) {
437  zde->filename = _zip_read_string(buffer, src, filename_len, 1, error);
438  if (!zde->filename) {
441  }
442  if (!from_buffer) {
444  }
445  return -1;
446  }
447 
448  if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) {
451  if (!from_buffer) {
453  }
454  return -1;
455  }
456  }
457  }
458 
459  if (ef_len) {
460  zip_uint8_t *ef = _zip_read_data(buffer, src, ef_len, 0, error);
461 
462  if (ef == NULL) {
463  if (!from_buffer) {
465  }
466  return -1;
467  }
468  if (!_zip_ef_parse(ef, ef_len, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, &zde->extra_fields, error)) {
469  free(ef);
470  if (!from_buffer) {
472  }
473  return -1;
474  }
475  free(ef);
476  if (local)
477  zde->local_extra_fields_read = 1;
478  }
479 
480  if (comment_len) {
481  zde->comment = _zip_read_string(buffer, src, comment_len, 0, error);
482  if (!zde->comment) {
483  if (!from_buffer) {
485  }
486  return -1;
487  }
488  if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) {
491  if (!from_buffer) {
493  }
494  return -1;
495  }
496  }
497  }
498 
501 
502  /* Zip64 */
503 
504  if (zde->uncomp_size == ZIP_UINT32_MAX || zde->comp_size == ZIP_UINT32_MAX || zde->offset == ZIP_UINT32_MAX) {
505  zip_uint16_t got_len;
506  zip_buffer_t *ef_buffer;
508  /* TODO: if got_len == 0 && !ZIP64_EOCD: no error, 0xffffffff is valid value */
509  if (ef == NULL) {
510  if (!from_buffer) {
512  }
513  return -1;
514  }
515 
516  if ((ef_buffer = _zip_buffer_new((zip_uint8_t *)ef, got_len)) == NULL) {
518  if (!from_buffer) {
520  }
521  return -1;
522  }
523 
524  if (zde->uncomp_size == ZIP_UINT32_MAX) {
525  zde->uncomp_size = _zip_buffer_get_64(ef_buffer);
526  }
527  else if (local) {
528  /* From appnote.txt: This entry in the Local header MUST
529  include BOTH original and compressed file size fields. */
530  (void)_zip_buffer_skip(ef_buffer, 8); /* error is caught by _zip_buffer_eof() call */
531  }
532  if (zde->comp_size == ZIP_UINT32_MAX) {
533  zde->comp_size = _zip_buffer_get_64(ef_buffer);
534  }
535  if (!local) {
536  if (zde->offset == ZIP_UINT32_MAX) {
537  zde->offset = _zip_buffer_get_64(ef_buffer);
538  }
539  if (zde->disk_number == ZIP_UINT16_MAX) {
540  zde->disk_number = _zip_buffer_get_32(ef_buffer);
541  }
542  }
543 
544  if (!_zip_buffer_eof(ef_buffer)) {
545  /* accept additional fields if values match */
546  bool ok = true;
547  switch (got_len) {
548  case 28:
549  _zip_buffer_set_offset(ef_buffer, 24);
550  if (zde->disk_number != _zip_buffer_get_32(ef_buffer)) {
551  ok = false;
552  }
553  /* fallthrough */
554  case 24:
555  _zip_buffer_set_offset(ef_buffer, 0);
556  if ((zde->uncomp_size != _zip_buffer_get_64(ef_buffer)) || (zde->comp_size != _zip_buffer_get_64(ef_buffer)) || (zde->offset != _zip_buffer_get_64(ef_buffer))) {
557  ok = false;
558  }
559  break;
560 
561  default:
562  ok = false;
563  }
564  if (!ok) {
566  _zip_buffer_free(ef_buffer);
567  if (!from_buffer) {
569  }
570  return -1;
571  }
572  }
573  _zip_buffer_free(ef_buffer);
574  }
575 
576  if (!_zip_buffer_ok(buffer)) {
578  if (!from_buffer) {
580  }
581  return -1;
582  }
583  if (!from_buffer) {
585  }
586 
587  /* zip_source_seek / zip_source_tell don't support values > ZIP_INT64_MAX */
588  if (zde->offset > ZIP_INT64_MAX) {
590  return -1;
591  }
592 
594  return -1;
595  }
596 
598 
599  return (zip_int64_t)size + (zip_int64_t)variable_size;
600 }
lzma_index * src
Definition: index.h:567
#define local
Definition: blast.c:36
#define ZIP_ER_SEEK
Definition: zip.h:109
#define ZIP_EM_UNKNOWN
Definition: zip.h:193
ZIP_EXTERN int zip_error_code_zip(const zip_error_t *_Nonnull)
Definition: zip_error.c:46
#define ZIP_ER_EOF
Definition: zip.h:122
#define ZIP_ER_NOZIP
Definition: zip.h:124
@ ok
Definition: lz4.c:1706
#define EFBIG
Definition: sftypes.h:137
zip_buffer_t * _zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error)
Definition: zip_buffer.c:174
int _zip_buffer_skip(zip_buffer_t *buffer, zip_uint64_t length)
Definition: zip_buffer.c:311
bool _zip_buffer_eof(zip_buffer_t *buffer)
Definition: zip_buffer.c:60
zip_uint64_t _zip_buffer_get_64(zip_buffer_t *buffer)
Definition: zip_buffer.c:104
int _zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset)
Definition: zip_buffer.c:297
static bool _zip_dirent_process_winzip_aes(zip_dirent_t *de, zip_error_t *error)
Definition: zip_dirent.c:639
static zip_string_t * _zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string_t *str)
Definition: zip_dirent.c:604
time_t _zip_d2u_time(zip_uint16_t dtime, zip_uint16_t ddate)
Definition: zip_dirent.c:1004
bool _zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_extra_field_t **ef_head_p, zip_error_t *error)
zip_extra_field_t * _zip_ef_remove_internal(zip_extra_field_t *ef)
zip_uint8_t * _zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error)
Definition: zip_io_util.c:63
zip_string_t * _zip_read_string(zip_buffer_t *buffer, zip_source_t *src, zip_uint16_t len, bool nulp, zip_error_t *error)
Definition: zip_io_util.c:107
zip_encoding_type_t _zip_guess_encoding(zip_string_t *str, zip_encoding_type_t expected_encoding)
Definition: zip_utf-8.c:100
#define ZIP_INT64_MAX
Definition: zipconf.h:54
#define LENTRYSIZE
Definition: zipint.h:58
#define ZIP_ER_DETAIL_INVALID_UTF8_IN_COMMENT
Definition: zipint.h:227
#define ZIP_EF_CENTRAL
Definition: zipint.h:244
#define CDENTRYSIZE
Definition: zipint.h:57
#define CENTRAL_MAGIC
Definition: zipint.h:51
#define ZIP_EF_LOCAL
Definition: zipint.h:243
#define ZIP_ER_DETAIL_INVALID_ZIP64_EF
Definition: zipint.h:228
#define ZIP_EF_UTF_8_NAME
Definition: zipint.h:86
#define ZIP_ER_DETAIL_VARIABLE_SIZE_OVERFLOW
Definition: zipint.h:225
#define ZIP_GPBF_ENCODING_UTF_8
Definition: zipint.h:239
#define ZIP_GPBF_STRONG_ENCRYPTION
Definition: zipint.h:238
#define ZIP_EF_ZIP64
Definition: zipint.h:88
#define ZIP_EF_UTF_8_COMMENT
Definition: zipint.h:85
#define ZIP_ER_DETAIL_INVALID_UTF8_IN_FILENAME
Definition: zipint.h:226
#define LOCAL_MAGIC
Definition: zipint.h:52
#define ZIP_GPBF_ENCRYPTED
Definition: zipint.h:236
@ ZIP_ENCODING_ERROR
Definition: zipint.h:259
@ ZIP_ENCODING_UTF8_KNOWN
Definition: zipint.h:256

References _zip_buffer_eof(), _zip_buffer_free(), _zip_buffer_get(), _zip_buffer_get_16(), _zip_buffer_get_32(), _zip_buffer_get_64(), _zip_buffer_left(), _zip_buffer_new(), _zip_buffer_new_from_source(), _zip_buffer_ok(), _zip_buffer_set_offset(), _zip_buffer_skip(), _zip_d2u_time(), _zip_dirent_init(), _zip_dirent_process_ef_utf_8(), _zip_dirent_process_winzip_aes(), _zip_ef_get_by_id(), _zip_ef_parse(), _zip_ef_remove_internal(), _zip_guess_encoding(), _zip_read_data(), _zip_read_string(), zip_dirent::bitflags, CDENTRYSIZE, CENTRAL_MAGIC, zip_dirent::comment, zip_dirent::comp_method, zip_dirent::comp_size, zip_dirent::crc, zip_dirent::disk_number, EFBIG, zip_dirent::encryption_method, error(), zip_dirent::ext_attrib, zip_dirent::extra_fields, zip_dirent::filename, free(), zip_dirent::int_attrib, zip_dirent::last_mod, LENTRYSIZE, local, zip_dirent::local_extra_fields_read, LOCAL_MAGIC, NULL, zip_dirent::offset, ok, src, zip_dirent::uncomp_size, zip_dirent::version_madeby, zip_dirent::version_needed, ZIP_EF_CENTRAL, ZIP_EF_LOCAL, ZIP_EF_UTF_8_COMMENT, ZIP_EF_UTF_8_NAME, ZIP_EF_ZIP64, ZIP_EM_NONE, ZIP_EM_TRAD_PKWARE, ZIP_EM_UNKNOWN, ZIP_ENCODING_ERROR, ZIP_ENCODING_UTF8_KNOWN, ZIP_ER_DETAIL_INVALID_UTF8_IN_COMMENT, ZIP_ER_DETAIL_INVALID_UTF8_IN_FILENAME, ZIP_ER_DETAIL_INVALID_ZIP64_EF, ZIP_ER_DETAIL_VARIABLE_SIZE_OVERFLOW, ZIP_ER_EOF, ZIP_ER_INCONS, ZIP_ER_INTERNAL, ZIP_ER_MEMORY, ZIP_ER_NOZIP, ZIP_ER_SEEK, zip_error_code_zip(), zip_error_set(), ZIP_GPBF_ENCODING_UTF_8, ZIP_GPBF_ENCRYPTED, ZIP_GPBF_STRONG_ENCRYPTION, ZIP_INT64_MAX, ZIP_UINT16_MAX, and ZIP_UINT32_MAX.

Referenced by _zip_checkcons(), and _zip_read_cdir().

◆ _zip_dirent_size()

zip_int32_t _zip_dirent_size ( zip_source_t src,
zip_uint16_t  flags,
zip_error_t error 
)

Definition at line 721 of file zip_dirent.c.

721  {
723  bool local = (flags & ZIP_EF_LOCAL) != 0;
724  int i;
725  zip_uint8_t b[6];
727 
729 
730  if (zip_source_seek(src, local ? 26 : 28, SEEK_CUR) < 0) {
732  return -1;
733  }
734 
735  if ((buffer = _zip_buffer_new_from_source(src, local ? 4 : 6, b, error)) == NULL) {
736  return -1;
737  }
738 
739  for (i = 0; i < (local ? 2 : 3); i++) {
741  }
742 
743  if (!_zip_buffer_eof(buffer)) {
746  return -1;
747  }
748 
750  return size;
751 }
ZIP_EXTERN int zip_source_seek(zip_source_t *_Nonnull, zip_int64_t, int)
#define b(i)
Definition: sha256.c:42
#define SEEK_CUR
Definition: zip.c:80
int32_t zip_int32_t
Definition: zipconf.h:36

References _zip_buffer_eof(), _zip_buffer_free(), _zip_buffer_get_16(), _zip_buffer_new_from_source(), _zip_error_set_from_source(), b, CDENTRYSIZE, error(), flags, i, LENTRYSIZE, local, NULL, SEEK_CUR, src, ZIP_EF_LOCAL, ZIP_ER_INTERNAL, zip_error_set(), and zip_source_seek().

Referenced by _zip_file_get_offset().

◆ _zip_dirent_write()

int _zip_dirent_write ( zip_t za,
zip_dirent_t de,
zip_flags_t  flags 
)

Definition at line 765 of file zip_dirent.c.

765  {
766  zip_uint16_t dostime, dosdate;
767  zip_encoding_type_t com_enc, name_enc;
769  zip_extra_field_t *ef64;
770  zip_uint32_t ef_total_size;
771  bool is_zip64;
772  bool is_really_zip64;
773  bool is_winzip_aes;
776 
777  ef = NULL;
778 
781 
782  if ((name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_ASCII) || (name_enc == ZIP_ENCODING_ASCII && com_enc == ZIP_ENCODING_UTF8_KNOWN) || (name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_UTF8_KNOWN))
784  else {
786  if (name_enc == ZIP_ENCODING_UTF8_KNOWN) {
788  if (ef == NULL)
789  return -1;
790  }
791  if ((flags & ZIP_FL_LOCAL) == 0 && com_enc == ZIP_ENCODING_UTF8_KNOWN) {
793  if (ef2 == NULL) {
794  _zip_ef_free(ef);
795  return -1;
796  }
797  ef2->next = ef;
798  ef = ef2;
799  }
800  }
801 
802  if (de->encryption_method == ZIP_EM_NONE) {
804  }
805  else {
807  }
808 
809  is_really_zip64 = _zip_dirent_needs_zip64(de, flags);
810  is_zip64 = (flags & (ZIP_FL_LOCAL | ZIP_FL_FORCE_ZIP64)) == (ZIP_FL_LOCAL | ZIP_FL_FORCE_ZIP64) || is_really_zip64;
812 
813  if (is_zip64) {
814  zip_uint8_t ef_zip64[EFZIP64SIZE];
815  zip_buffer_t *ef_buffer = _zip_buffer_new(ef_zip64, sizeof(ef_zip64));
816  if (ef_buffer == NULL) {
818  _zip_ef_free(ef);
819  return -1;
820  }
821 
822  if (flags & ZIP_FL_LOCAL) {
824  _zip_buffer_put_64(ef_buffer, de->uncomp_size);
825  _zip_buffer_put_64(ef_buffer, de->comp_size);
826  }
827  }
828  else {
830  if (de->uncomp_size >= ZIP_UINT32_MAX) {
831  _zip_buffer_put_64(ef_buffer, de->uncomp_size);
832  }
833  if (de->comp_size >= ZIP_UINT32_MAX) {
834  _zip_buffer_put_64(ef_buffer, de->comp_size);
835  }
836  if (de->offset >= ZIP_UINT32_MAX) {
837  _zip_buffer_put_64(ef_buffer, de->offset);
838  }
839  }
840  }
841 
842  if (!_zip_buffer_ok(ef_buffer)) {
844  _zip_buffer_free(ef_buffer);
845  _zip_ef_free(ef);
846  return -1;
847  }
848 
849  ef64 = _zip_ef_new(ZIP_EF_ZIP64, (zip_uint16_t)(_zip_buffer_offset(ef_buffer)), ef_zip64, ZIP_EF_BOTH);
850  _zip_buffer_free(ef_buffer);
851  ef64->next = ef;
852  ef = ef64;
853  }
854 
855  if (is_winzip_aes) {
857  zip_buffer_t *ef_buffer = _zip_buffer_new(data, sizeof(data));
858  zip_extra_field_t *ef_winzip;
859 
860  if (ef_buffer == NULL) {
862  _zip_ef_free(ef);
863  return -1;
864  }
865 
866  _zip_buffer_put_16(ef_buffer, 2);
867  _zip_buffer_put(ef_buffer, "AE", 2);
868  _zip_buffer_put_8(ef_buffer, (zip_uint8_t)(de->encryption_method & 0xff));
870 
871  if (!_zip_buffer_ok(ef_buffer)) {
873  _zip_buffer_free(ef_buffer);
874  _zip_ef_free(ef);
875  return -1;
876  }
877 
879  _zip_buffer_free(ef_buffer);
880  ef_winzip->next = ef;
881  ef = ef_winzip;
882  }
883 
884  if ((buffer = _zip_buffer_new(buf, sizeof(buf))) == NULL) {
886  _zip_ef_free(ef);
887  return -1;
888  }
889 
891 
892  if ((flags & ZIP_FL_LOCAL) == 0) {
894  }
895  _zip_buffer_put_16(buffer, ZIP_MAX(is_really_zip64 ? 45 : 0, de->version_needed));
897  if (is_winzip_aes) {
899  }
900  else {
902  }
903 
904  _zip_u2d_time(de->last_mod, &dostime, &dosdate);
905  _zip_buffer_put_16(buffer, dostime);
906  _zip_buffer_put_16(buffer, dosdate);
907 
908  if (is_winzip_aes && de->uncomp_size < 20) {
910  }
911  else {
913  }
914 
915  if (((flags & ZIP_FL_LOCAL) == ZIP_FL_LOCAL) && ((de->comp_size >= ZIP_UINT32_MAX) || (de->uncomp_size >= ZIP_UINT32_MAX))) {
916  /* In local headers, if a ZIP64 EF is written, it MUST contain
917  * both compressed and uncompressed sizes (even if one of the
918  * two is smaller than 0xFFFFFFFF); on the other hand, those
919  * may only appear when the corresponding standard entry is
920  * 0xFFFFFFFF. (appnote.txt 4.5.3) */
923  }
924  else {
925  if (de->comp_size < ZIP_UINT32_MAX) {
927  }
928  else {
930  }
931  if (de->uncomp_size < ZIP_UINT32_MAX) {
933  }
934  else {
936  }
937  }
938 
940  /* TODO: check for overflow */
942  _zip_buffer_put_16(buffer, (zip_uint16_t)ef_total_size);
943 
944  if ((flags & ZIP_FL_LOCAL) == 0) {
949  if (de->offset < ZIP_UINT32_MAX)
951  else
953  }
954 
955  if (!_zip_buffer_ok(buffer)) {
958  _zip_ef_free(ef);
959  return -1;
960  }
961 
962  if (_zip_write(za, buf, _zip_buffer_offset(buffer)) < 0) {
964  _zip_ef_free(ef);
965  return -1;
966  }
967 
969 
970  if (de->filename) {
971  if (_zip_string_write(za, de->filename) < 0) {
972  _zip_ef_free(ef);
973  return -1;
974  }
975  }
976 
977  if (ef) {
978  if (_zip_ef_write(za, ef, ZIP_EF_BOTH) < 0) {
979  _zip_ef_free(ef);
980  return -1;
981  }
982  }
983  _zip_ef_free(ef);
984  if (de->extra_fields) {
985  if (_zip_ef_write(za, de->extra_fields, flags) < 0) {
986  return -1;
987  }
988  }
989 
990  if ((flags & ZIP_FL_LOCAL) == 0) {
991  if (de->comment) {
992  if (_zip_string_write(za, de->comment) < 0) {
993  return -1;
994  }
995  }
996  }
997 
998 
999  return is_zip64;
1000 }
#define ZIP_FL_LOCAL
Definition: zip.h:85
zip_extra_field_t * next
Definition: zipint.h:368
int _zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i)
Definition: zip_buffer.c:283
void _zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate)
Definition: zip_dirent.c:1090
static zip_extra_field_t * _zip_ef_utf8(zip_uint16_t, zip_string_t *, zip_error_t *)
Definition: zip_dirent.c:1025
zip_uint16_t _zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags)
zip_extra_field_t * _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags)
int _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags)
int _zip_string_write(zip_t *za, const zip_string_t *s)
Definition: zip_string.c:173
#define ZIP_FL_FORCE_ZIP64
Definition: zipint.h:247
#define EFZIP64SIZE
Definition: zipint.h:66
enum zip_encoding_type zip_encoding_type_t
Definition: zipint.h:262
#define EF_WINZIP_AES_SIZE
Definition: zipint.h:67
@ ZIP_ENCODING_UNKNOWN
Definition: zipint.h:254
@ ZIP_ENCODING_ASCII
Definition: zipint.h:255

References _zip_buffer_free(), _zip_buffer_new(), _zip_buffer_offset(), _zip_buffer_ok(), _zip_buffer_put(), _zip_buffer_put_16(), _zip_buffer_put_32(), _zip_buffer_put_64(), _zip_buffer_put_8(), _zip_dirent_needs_zip64(), _zip_ef_free(), _zip_ef_new(), _zip_ef_size(), _zip_ef_utf8(), _zip_ef_write(), _zip_guess_encoding(), _zip_string_length(), _zip_string_write(), _zip_u2d_time(), _zip_write(), zip_dirent::bitflags, CDENTRYSIZE, CENTRAL_MAGIC, zip_dirent::comment, zip_dirent::comp_method, zip_dirent::comp_size, zip_dirent::crc, zip_dirent::disk_number, ef, EF_WINZIP_AES_SIZE, EFZIP64SIZE, zip_dirent::encryption_method, zip::error, zip_dirent::ext_attrib, zip_dirent::extra_fields, zip_dirent::filename, flags, zip_dirent::int_attrib, zip_dirent::last_mod, LOCAL_MAGIC, zip_extra_field::next, NULL, zip_dirent::offset, zip_dirent::uncomp_size, zip_dirent::version_madeby, zip_dirent::version_needed, za, ZIP_CM_WINZIP_AES, ZIP_EF_BOTH, ZIP_EF_UTF_8_COMMENT, ZIP_EF_UTF_8_NAME, ZIP_EF_WINZIP_AES, ZIP_EF_ZIP64, ZIP_EM_AES_128, ZIP_EM_AES_192, ZIP_EM_AES_256, ZIP_EM_NONE, ZIP_ENCODING_ASCII, ZIP_ENCODING_UNKNOWN, ZIP_ENCODING_UTF8_KNOWN, ZIP_ER_INTERNAL, ZIP_ER_MEMORY, zip_error_set(), ZIP_FL_FORCE_ZIP64, ZIP_FL_LOCAL, ZIP_GPBF_ENCODING_UTF_8, ZIP_GPBF_ENCRYPTED, ZIP_MAX, and ZIP_UINT32_MAX.

Referenced by _zip_cdir_write(), add_data(), and zip_close().

◆ _zip_ef_utf8()

static zip_extra_field_t * _zip_ef_utf8 ( zip_uint16_t  id,
zip_string_t str,
zip_error_t error 
)
static

Definition at line 1025 of file zip_dirent.c.

1025  {
1026  const zip_uint8_t *raw;
1027  zip_uint32_t len;
1030 
1031  if ((raw = _zip_string_get(str, &len, ZIP_FL_ENC_RAW, NULL)) == NULL) {
1032  /* error already set */
1033  return NULL;
1034  }
1035 
1036  if (len + 5 > ZIP_UINT16_MAX) {
1037  zip_error_set(error, ZIP_ER_INVAL, 0); /* TODO: better error code? */
1038  return NULL;
1039  }
1040 
1041  if ((buffer = _zip_buffer_new(NULL, len + 5)) == NULL) {
1043  return NULL;
1044  }
1045 
1048  _zip_buffer_put(buffer, raw, len);
1049 
1050  if (!_zip_buffer_ok(buffer)) {
1053  return NULL;
1054  }
1055 
1058 
1059  return ef;
1060 }
#define ZIP_ER_INVAL
Definition: zip.h:123
#define ZIP_FL_ENC_RAW
Definition: zip.h:83
const zip_uint8_t * _zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error)
Definition: zip_string.c:80

References _zip_buffer_data(), _zip_buffer_free(), _zip_buffer_new(), _zip_buffer_offset(), _zip_buffer_ok(), _zip_buffer_put(), _zip_buffer_put_32(), _zip_buffer_put_8(), _zip_ef_new(), _zip_string_crc32(), _zip_string_get(), ef, error(), len, NULL, cmd_descs_generate::str, ZIP_EF_BOTH, ZIP_ER_INTERNAL, ZIP_ER_INVAL, ZIP_ER_MEMORY, zip_error_set(), ZIP_FL_ENC_RAW, and ZIP_UINT16_MAX.

Referenced by _zip_dirent_write().

◆ _zip_get_dirent()

zip_dirent_t* _zip_get_dirent ( zip_t za,
zip_uint64_t  idx,
zip_flags_t  flags,
zip_error_t error 
)

Definition at line 1064 of file zip_dirent.c.

1064  {
1065  if (error == NULL)
1066  error = &za->error;
1067 
1068  if (idx >= za->nentry) {
1070  return NULL;
1071  }
1072 
1073  if ((flags & ZIP_FL_UNCHANGED) || za->entry[idx].changes == NULL) {
1074  if (za->entry[idx].orig == NULL) {
1076  return NULL;
1077  }
1078  if (za->entry[idx].deleted && (flags & ZIP_FL_UNCHANGED) == 0) {
1080  return NULL;
1081  }
1082  return za->entry[idx].orig;
1083  }
1084  else
1085  return za->entry[idx].changes;
1086 }
#define ZIP_ER_DELETED
Definition: zip.h:128
#define ZIP_FL_UNCHANGED
Definition: zip.h:79
int idx
Definition: setup.py:197
zip_dirent_t * orig
Definition: zipint.h:409
zip_dirent_t * changes
Definition: zipint.h:410
bool deleted
Definition: zipint.h:412
zip_uint64_t nentry
Definition: zipint.h:292

References zip_entry::changes, zip_entry::deleted, zip::entry, zip::error, error(), flags, setup::idx, zip::nentry, NULL, zip_entry::orig, za, ZIP_ER_DELETED, ZIP_ER_INVAL, zip_error_set(), and ZIP_FL_UNCHANGED.

Referenced by _zip_get_name(), _zip_source_zip_new(), zip_file_extra_field_delete(), zip_file_extra_field_delete_by_id(), zip_file_extra_field_get(), zip_file_extra_field_get_by_id(), zip_file_extra_field_set(), zip_file_extra_fields_count(), zip_file_extra_fields_count_by_id(), zip_file_get_comment(), zip_file_get_external_attributes(), zip_file_set_comment(), zip_file_set_external_attributes(), zip_file_set_mtime(), and zip_stat_index().

◆ _zip_u2d_time()

void _zip_u2d_time ( time_t  intime,
zip_uint16_t dtime,
zip_uint16_t ddate 
)

Definition at line 1090 of file zip_dirent.c.

1090  {
1091  struct tm *tpm;
1092 
1093 #ifdef HAVE_LOCALTIME_R
1094  struct tm tm;
1095  tpm = localtime_r(&intime, &tm);
1096 #else
1097  tpm = localtime(&intime);
1098 #endif
1099  if (tpm == NULL) {
1100  /* if localtime() fails, return an arbitrary date (1980-01-01 00:00:00) */
1101  *ddate = (1 << 5) + 1;
1102  *dtime = 0;
1103  return;
1104  }
1105  if (tpm->tm_year < 80) {
1106  tpm->tm_year = 80;
1107  }
1108 
1109  *ddate = (zip_uint16_t)(((tpm->tm_year + 1900 - 1980) << 9) + ((tpm->tm_mon + 1) << 5) + tpm->tm_mday);
1110  *dtime = (zip_uint16_t)(((tpm->tm_hour) << 11) + ((tpm->tm_min) << 5) + ((tpm->tm_sec) >> 1));
1111 
1112  return;
1113 }

References NULL.

Referenced by _zip_dirent_write(), decrypt_header(), and encrypt_header().