1 #ifndef _HAD_ZIPINT_H
2 #define _HAD_ZIPINT_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 #ifdef PHP_WIN32
38 # include "php_zip_config.w32.h"
39 #else
40 # include "config.h"
41 #endif
42
43
44 #define __STDC_LIMIT_MACROS
45
46 #include <zlib.h>
47
48 #ifdef PHP_WIN32
49
50 #include <io.h>
51 #include "config.w32.h"
52 #endif
53
54 #ifndef _ZIP_COMPILING_DEPRECATED
55 #define ZIP_DISABLE_DEPRECATED
56 #endif
57
58 #include "zip.h"
59
60 #ifdef HAVE_STDBOOL_H
61 #include <stdbool.h>
62 #else
63 typedef char bool;
64 #define true 1
65 #define false 0
66 #endif
67
68 #include <errno.h>
69
70
71
72
73 #ifndef EOPNOTSUPP
74 #define EOPNOTSUPP EINVAL
75 #endif
76
77
78
79
80 #ifndef EOVERFLOW
81 #define EOVERFLOW EFBIG
82 #endif
83
84 #ifdef _WIN32
85 #if defined(HAVE__CLOSE)
86 #define close _close
87 #endif
88 #if defined(HAVE__DUP)
89 #define dup _dup
90 #endif
91
92 #if defined(HAVE__FDOPEN)
93 #define fdopen _fdopen
94 #endif
95 #if !defined(HAVE_FILENO) && defined(HAVE__FILENO)
96 #define fileno _fileno
97 #endif
98
99 #if defined(HAVE__OPEN)
100 #define open(a, b, c) _open((a), (b))
101 #endif
102 #if defined(HAVE__SNPRINTF)
103 #define snprintf _snprintf
104 #endif
105 #if defined(HAVE__STRDUP)
106 #if !defined(HAVE_STRDUP) || defined(_WIN32)
107 #undef strdup
108 #define strdup _strdup
109 #endif
110 #endif
111 #if !defined(HAVE__SETMODE) && defined(HAVE_SETMODE)
112 #define _setmode setmode
113 #endif
114 #endif
115
116 #ifndef HAVE_FSEEKO
117 #define fseeko(s, o, w) (fseek((s), (long int)(o), (w)))
118 #endif
119
120 #ifndef HAVE_FTELLO
121 #define ftello(s) ((long)ftell((s)))
122 #endif
123
124 #ifndef HAVE_MKSTEMP
125 int _zip_mkstemp(char *);
126 #define mkstemp _zip_mkstemp
127 #endif
128
129 #if !defined(HAVE_STRCASECMP)
130 #if defined(HAVE__STRICMP)
131 #define strcasecmp _stricmp
132 #elif defined(HAVE_STRICMP)
133 #define strcasecmp stricmp
134 #endif
135 #endif
136
137 #if SIZEOF_OFF_T == 8
138 #define ZIP_OFF_MAX ZIP_INT64_MAX
139 #define ZIP_OFF_MIN ZIP_INT64_MIN
140 #elif SIZEOF_OFF_T == 4
141 #define ZIP_OFF_MAX ZIP_INT32_MAX
142 #define ZIP_OFF_MIN ZIP_INT32_MIN
143 #elif SIZEOF_OFF_T == 2
144 #define ZIP_OFF_MAX ZIP_INT16_MAX
145 #define ZIP_OFF_MIN ZIP_INT16_MIN
146 #else
147 #error unsupported size of off_t
148 #endif
149
150 #if defined(HAVE_FTELLO) && defined(HAVE_FSEEKO)
151 #define ZIP_FSEEK_MAX ZIP_OFF_MAX
152 #define ZIP_FSEEK_MIN ZIP_OFF_MIN
153 #else
154 #include <limits.h>
155 #define ZIP_FSEEK_MAX LONG_MAX
156 #define ZIP_FSEEK_MIN LONG_MIN
157 #endif
158
159 #ifndef SIZE_MAX
160 #if SIZEOF_SIZE_T == 8
161 #define SIZE_MAX ZIP_INT64_MAX
162 #elif SIZEOF_SIZE_T == 4
163 #define SIZE_MAX ZIP_INT32_MAX
164 #elif SIZEOF_SIZE_T == 2
165 #define SIZE_MAX ZIP_INT16_MAX
166 #else
167 #error unsupported size of size_t
168 #endif
169 #endif
170
171 #define CENTRAL_MAGIC "PK\1\2"
172 #define LOCAL_MAGIC "PK\3\4"
173 #define EOCD_MAGIC "PK\5\6"
174 #define DATADES_MAGIC "PK\7\8"
175 #define EOCD64LOC_MAGIC "PK\6\7"
176 #define EOCD64_MAGIC "PK\6\6"
177 #define CDENTRYSIZE 46u
178 #define LENTRYSIZE 30
179 #define MAXCOMLEN 65536
180 #define MAXEXTLEN 65536
181 #define EOCDLEN 22
182 #define EOCD64LOCLEN 20
183 #define EOCD64LEN 56
184 #define CDBUFSIZE (MAXCOMLEN+EOCDLEN+EOCD64LOCLEN)
185 #define BUFSIZE 8192
186 #define EFZIP64SIZE 28
187
188 #define ZIP_CM_REPLACED_DEFAULT (-2)
189
190 #define ZIP_CM_IS_DEFAULT(x) ((x) == ZIP_CM_DEFAULT || (x) == ZIP_CM_REPLACED_DEFAULT)
191
192 #define ZIP_EF_UTF_8_COMMENT 0x6375
193 #define ZIP_EF_UTF_8_NAME 0x7075
194 #define ZIP_EF_ZIP64 0x0001
195
196 #define ZIP_EF_IS_INTERNAL(id) ((id) == ZIP_EF_UTF_8_COMMENT || (id) == ZIP_EF_UTF_8_NAME || (id) == ZIP_EF_ZIP64)
197
198
199 #define ZIP_EXT_ATTRIB_DEFAULT (0100666u<<16)
200
201 #define ZIP_EXT_ATTRIB_DEFAULT_DIR (0040777u<<16)
202
203
204 #define ZIP_MAX(a, b) ((a) > (b) ? (a) : (b))
205 #define ZIP_MIN(a, b) ((a) < (b) ? (a) : (b))
206
207
208
209
210
211
212 #define ZIP_CODEC_DECODE 0
213 #define ZIP_CODEC_ENCODE 1
214
215
216 typedef zip_source_t *(*zip_compression_implementation)(zip_t *, zip_source_t *, zip_int32_t, int);
217 typedef zip_source_t *(*zip_encryption_implementation)(zip_t *, zip_source_t *, zip_uint16_t, int, const char *);
218
219 zip_compression_implementation _zip_get_compression_implementation(zip_int32_t);
220 zip_encryption_implementation _zip_get_encryption_implementation(zip_uint16_t);
221
222
223
224
225
226 const zip_uint8_t *zip_get_extra_field_by_id(zip_t *, int, int, zip_uint16_t, int, zip_uint16_t *);
227
228
229
230
231
232 typedef zip_int64_t (*zip_source_layered_callback)(zip_source_t *, void *, void *, zip_uint64_t, enum zip_source_cmd);
233 zip_source_t *zip_source_crc(zip_t *, zip_source_t *, int);
234 zip_source_t *zip_source_deflate(zip_t *, zip_source_t *, zip_int32_t, int);
235 zip_source_t *zip_source_layered(zip_t *, zip_source_t *, zip_source_layered_callback, void *);
236 zip_source_t *zip_source_layered_create(zip_source_t *src, zip_source_layered_callback cb, void *ud, zip_error_t *error);
237 zip_source_t *zip_source_pkware(zip_t *, zip_source_t *, zip_uint16_t, int, const char *);
238 int zip_source_remove(zip_source_t *);
239 zip_int64_t zip_source_supports(zip_source_t *src);
240 zip_source_t *zip_source_window(zip_t *, zip_source_t *, zip_uint64_t, zip_uint64_t);
241
242
243
244
245 enum zip_les { ZIP_LES_NONE, ZIP_LES_UPPER, ZIP_LES_LOWER, ZIP_LES_INVAL };
246
247
248
249 #define ZIP_GPBF_ENCRYPTED 0x0001
250 #define ZIP_GPBF_DATA_DESCRIPTOR 0x0008
251 #define ZIP_GPBF_STRONG_ENCRYPTION 0x0040
252 #define ZIP_GPBF_ENCODING_UTF_8 0x0800
253
254
255
256 #define ZIP_EF_LOCAL ZIP_FL_LOCAL
257 #define ZIP_EF_CENTRAL ZIP_FL_CENTRAL
258 #define ZIP_EF_BOTH (ZIP_EF_LOCAL|ZIP_EF_CENTRAL)
259
260 #define ZIP_FL_FORCE_ZIP64 1024
261
262 #define ZIP_FL_ENCODING_ALL (ZIP_FL_ENC_GUESS|ZIP_FL_ENC_CP437|ZIP_FL_ENC_UTF_8)
263
264
265
266 enum zip_encoding_type {
267 ZIP_ENCODING_UNKNOWN,
268 ZIP_ENCODING_ASCII,
269 ZIP_ENCODING_UTF8_KNOWN,
270 ZIP_ENCODING_UTF8_GUESSED,
271 ZIP_ENCODING_CP437,
272 ZIP_ENCODING_ERROR
273 };
274
275 typedef enum zip_encoding_type zip_encoding_type_t;
276
277 #ifndef ZIP_HASH_TABLE_SIZE
278 #define ZIP_HASH_TABLE_SIZE 8192
279 #endif
280
281 struct zip_hash;
282
283 typedef struct zip_cdir zip_cdir_t;
284 typedef struct zip_dirent zip_dirent_t;
285 typedef struct zip_entry zip_entry_t;
286 typedef struct zip_extra_field zip_extra_field_t;
287 typedef struct zip_string zip_string_t;
288 typedef struct zip_buffer zip_buffer_t;
289 typedef struct zip_hash zip_hash_t;
290
291
292
293 struct zip {
294 zip_source_t *src;
295 unsigned int open_flags;
296 zip_error_t error;
297
298 unsigned int flags;
299 unsigned int ch_flags;
300
301 char *default_password;
302
303 zip_string_t *comment_orig;
304 zip_string_t *comment_changes;
305 bool comment_changed;
306
307 zip_uint64_t nentry;
308 zip_uint64_t nentry_alloc;
309 zip_entry_t *entry;
310
311 unsigned int nopen_source;
312 unsigned int nopen_source_alloc;
313 zip_source_t **open_source;
314
315 zip_hash_t *names;
316
317 char *tempdir;
318 };
319
320
321
322 struct zip_file {
323 zip_t *za;
324 zip_error_t error;
325 bool eof;
326 zip_source_t *src;
327 };
328
329
330
331 #define ZIP_DIRENT_COMP_METHOD 0x0001u
332 #define ZIP_DIRENT_FILENAME 0x0002u
333 #define ZIP_DIRENT_COMMENT 0x0004u
334 #define ZIP_DIRENT_EXTRA_FIELD 0x0008u
335 #define ZIP_DIRENT_ATTRIBUTES 0x0010u
336 #define ZIP_DIRENT_LAST_MOD 0x0020u
337 #define ZIP_DIRENT_ALL 0xffffu
338
339 struct zip_dirent {
340 zip_uint32_t changed;
341 bool local_extra_fields_read;
342 bool cloned;
343
344 zip_uint16_t version_madeby;
345 zip_uint16_t version_needed;
346 zip_uint16_t bitflags;
347 zip_int32_t comp_method;
348 time_t last_mod;
349 zip_uint32_t crc;
350 zip_uint64_t comp_size;
351 zip_uint64_t uncomp_size;
352 zip_string_t *filename;
353 zip_extra_field_t *extra_fields;
354 zip_string_t *comment;
355 zip_uint32_t disk_number;
356 zip_uint16_t int_attrib;
357 zip_uint32_t ext_attrib;
358 zip_uint64_t offset;
359 };
360
361
362
363 struct zip_cdir {
364 zip_entry_t *entry;
365 zip_uint64_t nentry;
366 zip_uint64_t nentry_alloc;
367
368 zip_uint64_t size;
369 zip_uint64_t offset;
370 zip_string_t *comment;
371 };
372
373 struct zip_extra_field {
374 zip_extra_field_t *next;
375 zip_flags_t flags;
376 zip_uint16_t id;
377 zip_uint16_t size;
378 zip_uint8_t *data;
379 };
380
381 enum zip_source_write_state {
382 ZIP_SOURCE_WRITE_CLOSED,
383 ZIP_SOURCE_WRITE_OPEN,
384 ZIP_SOURCE_WRITE_FAILED,
385 ZIP_SOURCE_WRITE_REMOVED
386 };
387 typedef enum zip_source_write_state zip_source_write_state_t;
388
389 struct zip_source {
390 zip_source_t *src;
391 union {
392 zip_source_callback f;
393 zip_source_layered_callback l;
394 } cb;
395 void *ud;
396 zip_error_t error;
397 zip_int64_t supports;
398 unsigned int open_count;
399 zip_source_write_state_t write_state;
400 bool source_closed;
401 zip_t *source_archive;
402 unsigned int refcount;
403 };
404
405 #define ZIP_SOURCE_IS_OPEN_READING(src) ((src)->open_count > 0)
406 #define ZIP_SOURCE_IS_OPEN_WRITING(src) ((src)->write_state == ZIP_SOURCE_WRITE_OPEN)
407 #define ZIP_SOURCE_IS_LAYERED(src) ((src)->src != NULL)
408
409
410
411 struct zip_entry {
412 zip_dirent_t *orig;
413 zip_dirent_t *changes;
414 zip_source_t *source;
415 bool deleted;
416 };
417
418
419
420
421 struct zip_string {
422 zip_uint8_t *raw;
423 zip_uint16_t length;
424 enum zip_encoding_type encoding;
425 zip_uint8_t *converted;
426 zip_uint32_t converted_length;
427 };
428
429
430
431
432 struct zip_buffer {
433 bool ok;
434 bool free_data;
435
436 zip_uint8_t *data;
437 zip_uint64_t size;
438 zip_uint64_t offset;
439 };
440
441
442
443 struct zip_filelist {
444 zip_uint64_t idx;
445
446 };
447
448 typedef struct zip_filelist zip_filelist_t;
449
450
451 extern const char * const _zip_err_str[];
452 extern const int _zip_nerr_str;
453 extern const int _zip_err_type[];
454
455
456 #define ZIP_ENTRY_CHANGED(e, f) ((e)->changes && ((e)->changes->changed & (f)))
457
458 #define ZIP_ENTRY_DATA_CHANGED(x) ((x)->source != NULL)
459
460 #define ZIP_IS_RDONLY(za) ((za)->ch_flags & ZIP_AFL_RDONLY)
461
462
463 zip_int64_t _zip_add_entry(zip_t *);
464
465 zip_uint8_t *_zip_buffer_data(zip_buffer_t *buffer);
466 bool _zip_buffer_eof(zip_buffer_t *buffer);
467 void _zip_buffer_free(zip_buffer_t *buffer);
468 zip_uint8_t *_zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length);
469 zip_uint16_t _zip_buffer_get_16(zip_buffer_t *buffer);
470 zip_uint32_t _zip_buffer_get_32(zip_buffer_t *buffer);
471 zip_uint64_t _zip_buffer_get_64(zip_buffer_t *buffer);
472 zip_uint8_t _zip_buffer_get_8(zip_buffer_t *buffer);
473 zip_uint64_t _zip_buffer_left(zip_buffer_t *buffer);
474 zip_buffer_t *_zip_buffer_new(zip_uint8_t *data, zip_uint64_t size);
475 zip_buffer_t *_zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error);
476 zip_uint64_t _zip_buffer_offset(zip_buffer_t *buffer);
477 bool _zip_buffer_ok(zip_buffer_t *buffer);
478 int _zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length);
479 int _zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i);
480 int _zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i);
481 int _zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i);
482 int _zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i);
483 int _zip_buffer_skip(zip_buffer_t *buffer, zip_uint64_t length);
484 int _zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset);
485 zip_uint64_t _zip_buffer_size(zip_buffer_t *buffer);
486
487 int _zip_cdir_compute_crc(zip_t *, uLong *);
488 void _zip_cdir_free(zip_cdir_t *);
489 zip_cdir_t *_zip_cdir_new(zip_uint64_t, zip_error_t *);
490 zip_int64_t _zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors);
491 void _zip_deregister_source(zip_t *za, zip_source_t *src);
492
493 zip_dirent_t *_zip_dirent_clone(const zip_dirent_t *);
494 void _zip_dirent_free(zip_dirent_t *);
495 void _zip_dirent_finalize(zip_dirent_t *);
496 void _zip_dirent_init(zip_dirent_t *);
497 bool _zip_dirent_needs_zip64(const zip_dirent_t *, zip_flags_t);
498 zip_dirent_t *_zip_dirent_new(void);
499 zip_int64_t _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, bool local, zip_error_t *error);
500 zip_int32_t _zip_dirent_size(zip_source_t *src, zip_uint16_t, zip_error_t *);
501 int _zip_dirent_write(zip_t *za, zip_dirent_t *dirent, zip_flags_t flags);
502
503 zip_extra_field_t *_zip_ef_clone(const zip_extra_field_t *, zip_error_t *);
504 zip_extra_field_t *_zip_ef_delete_by_id(zip_extra_field_t *, zip_uint16_t, zip_uint16_t, zip_flags_t);
505 void _zip_ef_free(zip_extra_field_t *);
506 const zip_uint8_t *_zip_ef_get_by_id(const zip_extra_field_t *, zip_uint16_t *, zip_uint16_t, zip_uint16_t, zip_flags_t, zip_error_t *);
507 zip_extra_field_t *_zip_ef_merge(zip_extra_field_t *, zip_extra_field_t *);
508 zip_extra_field_t *_zip_ef_new(zip_uint16_t, zip_uint16_t, const zip_uint8_t *, zip_flags_t);
509 bool _zip_ef_parse(const zip_uint8_t *, zip_uint16_t, zip_flags_t, zip_extra_field_t **, zip_error_t *);
510 zip_extra_field_t *_zip_ef_remove_internal(zip_extra_field_t *);
511 zip_uint16_t _zip_ef_size(const zip_extra_field_t *, zip_flags_t);
512 int _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags);
513
514 void _zip_entry_finalize(zip_entry_t *);
515 void _zip_entry_init(zip_entry_t *);
516
517 void _zip_error_clear(zip_error_t *);
518 void _zip_error_get(const zip_error_t *, int *, int *);
519
520 void _zip_error_copy(zip_error_t *dst, const zip_error_t *src);
521 void _zip_error_set_from_source(zip_error_t *, zip_source_t *);
522
523 const zip_uint8_t *_zip_extract_extra_field_by_id(zip_error_t *, zip_uint16_t, int, const zip_uint8_t *, zip_uint16_t, zip_uint16_t *);
524
525 int _zip_file_extra_field_prepare_for_change(zip_t *, zip_uint64_t);
526 int _zip_file_fillbuf(void *, size_t, zip_file_t *);
527 zip_uint64_t _zip_file_get_offset(const zip_t *, zip_uint64_t, zip_error_t *);
528
529 int _zip_filerange_crc(zip_source_t *src, zip_uint64_t offset, zip_uint64_t length, uLong *crcp, zip_error_t *error);
530
531 zip_dirent_t *_zip_get_dirent(zip_t *, zip_uint64_t, zip_flags_t, zip_error_t *);
532
533 enum zip_encoding_type _zip_guess_encoding(zip_string_t *, enum zip_encoding_type);
534 zip_uint8_t *_zip_cp437_to_utf8(const zip_uint8_t * const, zip_uint32_t, zip_uint32_t *, zip_error_t *);
535
536 bool _zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip_flags_t flags, zip_error_t *error);
537 bool _zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *key, zip_error_t *error);
538 void _zip_hash_free(zip_hash_t *hash);
539 zip_int64_t _zip_hash_lookup(zip_hash_t *hash, const zip_uint8_t *name, zip_flags_t flags, zip_error_t *error);
540 zip_hash_t *_zip_hash_new(zip_uint16_t hash_size, zip_error_t *error);
541 void _zip_hash_revert(zip_hash_t *hash);
542
543 zip_t *_zip_open(zip_source_t *, unsigned int, zip_error_t *);
544
545 int _zip_read(zip_source_t *src, zip_uint8_t *data, zip_uint64_t length, zip_error_t *error);
546 int _zip_read_at_offset(zip_source_t *src, zip_uint64_t offset, unsigned char *b, size_t length, zip_error_t *error);
547 zip_uint8_t *_zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error);
548 int _zip_read_local_ef(zip_t *, zip_uint64_t);
549 zip_string_t *_zip_read_string(zip_buffer_t *buffer, zip_source_t *src, zip_uint16_t lenght, bool nulp, zip_error_t *error);
550 int _zip_register_source(zip_t *za, zip_source_t *src);
551
552 void _zip_set_open_error(int *zep, const zip_error_t *err, int ze);
553
554 zip_int64_t _zip_source_call(zip_source_t *src, void *data, zip_uint64_t length, zip_source_cmd_t command);
555 zip_source_t *_zip_source_file_or_p(const char *, FILE *, zip_uint64_t, zip_int64_t, const zip_stat_t *, zip_error_t *error);
556 void _zip_source_invalidate(zip_source_t *src);
557 zip_source_t *_zip_source_new(zip_error_t *error);
558 int _zip_source_set_source_archive(zip_source_t *, zip_t *);
559 zip_source_t *_zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_uint64_t length, zip_stat_t *st, zip_error_t *error);
560 zip_source_t *_zip_source_zip_new(zip_t *, zip_t *, zip_uint64_t, zip_flags_t, zip_uint64_t, zip_uint64_t, const char *);
561
562 int _zip_stat_merge(zip_stat_t *dst, const zip_stat_t *src, zip_error_t *error);
563 int _zip_string_equal(const zip_string_t *, const zip_string_t *);
564 void _zip_string_free(zip_string_t *);
565 zip_uint32_t _zip_string_crc32(const zip_string_t *);
566 const zip_uint8_t *_zip_string_get(zip_string_t *, zip_uint32_t *, zip_flags_t, zip_error_t *);
567 zip_uint16_t _zip_string_length(const zip_string_t *);
568 zip_string_t *_zip_string_new(const zip_uint8_t *, zip_uint16_t, zip_flags_t, zip_error_t *);
569 int _zip_string_write(zip_t *za, const zip_string_t *string);
570
571 int _zip_changed(const zip_t *, zip_uint64_t *);
572 const char *_zip_get_name(zip_t *, zip_uint64_t, zip_flags_t, zip_error_t *);
573 int _zip_local_header_read(zip_t *, int);
574 void *_zip_memdup(const void *, size_t, zip_error_t *);
575 zip_int64_t _zip_name_locate(zip_t *, const char *, zip_flags_t, zip_error_t *);
576 zip_t *_zip_new(zip_error_t *);
577
578 zip_int64_t _zip_file_replace(zip_t *, zip_uint64_t, const char *, zip_source_t *, zip_flags_t);
579 int _zip_set_name(zip_t *, zip_uint64_t, const char *, zip_flags_t);
580 void _zip_u2d_time(time_t, zip_uint16_t *, zip_uint16_t *);
581 int _zip_unchange(zip_t *, zip_uint64_t, int);
582 void _zip_unchange_data(zip_entry_t *);
583 int _zip_write(zip_t *za, const void *data, zip_uint64_t length);
584
585 #endif