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
38 #define __STDC_LIMIT_MACROS
39
40 #include <zlib.h>
41
42 #ifdef PHP_WIN32
43
44 #include <io.h>
45 #include "config.w32.h"
46 #endif
47
48 #ifndef _ZIP_COMPILING_DEPRECATED
49 #define ZIP_DISABLE_DEPRECATED
50 #endif
51
52 #include "zip.h"
53 #ifdef PHP_WIN32
54 # include "php_zip_config.w32.h"
55 #else
56 # include "config.h"
57 #endif
58
59 #ifdef HAVE_MOVEFILEEXA
60 #include <windows.h>
61 #define _zip_rename(s, t) (!MoveFileExA((s), (t), MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING))
62 #else
63 #define _zip_rename rename
64 #endif
65
66 #ifdef _WIN32
67 #if defined(HAVE__CLOSE)
68 #define close _close
69 #endif
70 #if defined(HAVE__DUP)
71 #define dup _dup
72 #endif
73
74 #if defined(HAVE__FDOPEN)
75 #define fdopen _fdopen
76 #endif
77 #if defined(HAVE__FILENO)
78 #define fileno _fileno
79 #endif
80
81 #if defined(HAVE__OPEN)
82 #define open(a, b, c) _open((a), (b))
83 #endif
84 #if defined(HAVE__SNPRINTF) && !defined(PHP_WIN32)
85 #define snprintf _snprintf
86 #endif
87 #if defined(HAVE__STRDUP) && !defined(HAVE_STRDUP)
88 #define strdup _strdup
89 #endif
90 #endif
91
92 #ifndef HAVE_FSEEKO
93 #define fseeko(s, o, w) (fseek((s), (long int)(o), (w)))
94 #endif
95
96 #ifndef HAVE_FTELLO
97 #define ftello(s) ((long)ftell((s)))
98 #endif
99
100 #ifndef HAVE_MKSTEMP
101 int _zip_mkstemp(char *);
102 #define mkstemp _zip_mkstemp
103 #endif
104
105 #if !defined(HAVE_STRCASECMP)
106 #if defined(HAVE__STRICMP)
107 #define strcasecmp _stricmp
108 #endif
109 #endif
110
111 #if SIZEOF_OFF_T == 8
112 #define ZIP_OFF_MAX ZIP_INT64_MAX
113 #elif SIZEOF_OFF_T == 4
114 #define ZIP_OFF_MAX ZIP_INT32_MAX
115 #elif SIZEOF_OFF_T == 2
116 #define ZIP_OFF_MAX ZIP_INT16_MAX
117 #else
118 #error unsupported size of off_t
119 #endif
120
121 #ifndef SIZE_MAX
122 #if SIZEOF_SIZE_T == 8
123 #define SIZE_MAX ZIP_INT64_MAX
124 #elif SIZEOF_SIZE_T == 4
125 #define SIZE_MAX ZIP_INT32_MAX
126 #elif SIZEOF_SIZE_T == 2
127 #define SIZE_MAX ZIP_INT16_MAX
128 #else
129 #error unsupported size of size_t
130 #endif
131 #endif
132
133 #define CENTRAL_MAGIC "PK\1\2"
134 #define LOCAL_MAGIC "PK\3\4"
135 #define EOCD_MAGIC "PK\5\6"
136 #define DATADES_MAGIC "PK\7\8"
137 #define EOCD64LOC_MAGIC "PK\6\7"
138 #define EOCD64_MAGIC "PK\6\6"
139 #define TORRENT_SIG "TORRENTZIPPED-"
140 #define TORRENT_SIG_LEN 14
141 #define TORRENT_CRC_LEN 8
142 #define TORRENT_MEM_LEVEL 8
143 #define CDENTRYSIZE 46u
144 #define LENTRYSIZE 30
145 #define MAXCOMLEN 65536
146 #define MAXEXTLEN 65536
147 #define EOCDLEN 22
148 #define EOCD64LOCLEN 20
149 #define EOCD64LEN 56
150 #define CDBUFSIZE (MAXCOMLEN+EOCDLEN+EOCD64LOCLEN)
151 #define BUFSIZE 8192
152
153 #define ZIP_CM_REPLACED_DEFAULT (-2)
154
155 #define ZIP_CM_IS_DEFAULT(x) ((x) == ZIP_CM_DEFAULT || (x) == ZIP_CM_REPLACED_DEFAULT)
156
157 #define ZIP_EF_UTF_8_COMMENT 0x6375
158 #define ZIP_EF_UTF_8_NAME 0x7075
159 #define ZIP_EF_ZIP64 0x0001
160
161 #define ZIP_EF_IS_INTERNAL(id) ((id) == ZIP_EF_UTF_8_COMMENT || (id) == ZIP_EF_UTF_8_NAME || (id) == ZIP_EF_ZIP64)
162
163
164 #define ZIP_EXT_ATTRIB_DEFAULT (0100666<<16)
165
166 #define ZIP_EXT_ATTRIB_DEFAULT_DIR (0040777<<16)
167
168
169
170
171
172 typedef struct zip_source *(*zip_compression_implementation)(struct zip *,
173 struct zip_source *,
174 zip_int32_t, int);
175 typedef struct zip_source *(*zip_encryption_implementation)(struct zip *,
176 struct zip_source *,
177 zip_uint16_t, int,
178 const char *);
179
180 zip_compression_implementation _zip_get_compression_implementation(zip_int32_t);
181 zip_encryption_implementation _zip_get_encryption_implementation(zip_uint16_t);
182
183
184
185
186
187
188 const zip_uint8_t *zip_get_extra_field_by_id(struct zip *, int, int, zip_uint16_t, int, zip_uint16_t *);
189
190
191
192
193
194 typedef zip_int64_t (*zip_source_layered_callback)(struct zip_source *, void *,
195 void *, zip_uint64_t,
196 enum zip_source_cmd);
197
198 void zip_source_close(struct zip_source *);
199 struct zip_source *zip_source_crc(struct zip *, struct zip_source *,
200 int);
201 struct zip_source *zip_source_deflate(struct zip *,
202 struct zip_source *,
203 zip_int32_t, int);
204 void zip_source_error(struct zip_source *, int *, int *);
205 struct zip_source *zip_source_layered(struct zip *,
206 struct zip_source *,
207 zip_source_layered_callback,
208 void *);
209 int zip_source_open(struct zip_source *);
210 struct zip_source *zip_source_pkware(struct zip *,
211 struct zip_source *,
212 zip_uint16_t, int,
213 const char *);
214 zip_int64_t zip_source_read(struct zip_source *, void *,
215 zip_uint64_t);
216 int zip_source_stat(struct zip_source *, struct zip_stat *);
217 struct zip_source *zip_source_window(struct zip *, struct zip_source *,
218 zip_uint64_t, zip_uint64_t);
219
220
221
222
223
224
225 struct zip_source *zip_source_pop(struct zip_source *);
226
227
228
229
230
231 enum zip_les { ZIP_LES_NONE, ZIP_LES_UPPER, ZIP_LES_LOWER, ZIP_LES_INVAL };
232
233
234
235 #define ZIP_GPBF_ENCRYPTED 0x0001
236 #define ZIP_GPBF_DATA_DESCRIPTOR 0x0008
237 #define ZIP_GPBF_STRONG_ENCRYPTION 0x0040
238 #define ZIP_GPBF_ENCODING_UTF_8 0x0800
239
240
241
242 #define ZIP_EF_LOCAL ZIP_FL_LOCAL
243 #define ZIP_EF_CENTRAL ZIP_FL_CENTRAL
244 #define ZIP_EF_BOTH (ZIP_EF_LOCAL|ZIP_EF_CENTRAL)
245
246 #define ZIP_FL_FORCE_ZIP64 1024
247
248 #define ZIP_FL_ENCODING_ALL (ZIP_FL_ENC_GUESS|ZIP_FL_ENC_CP437|ZIP_FL_ENC_UTF_8)
249
250
251
252 enum zip_encoding_type {
253 ZIP_ENCODING_UNKNOWN,
254 ZIP_ENCODING_ASCII,
255 ZIP_ENCODING_UTF8_KNOWN,
256 ZIP_ENCODING_UTF8_GUESSED,
257 ZIP_ENCODING_CP437,
258 ZIP_ENCODING_ERROR
259 };
260
261
262
263 struct zip_error {
264 int zip_err;
265 int sys_err;
266 char *str;
267 };
268
269
270
271 struct zip {
272 char *zn;
273 FILE *zp;
274 unsigned int open_flags;
275 struct zip_error error;
276
277 unsigned int flags;
278 unsigned int ch_flags;
279
280 char *default_password;
281
282 struct zip_string *comment_orig;
283 struct zip_string *comment_changes;
284 int comment_changed;
285
286 zip_uint64_t nentry;
287 zip_uint64_t nentry_alloc;
288 struct zip_entry *entry;
289
290 unsigned int nfile;
291 unsigned int nfile_alloc;
292 struct zip_file **file;
293
294 char *tempdir;
295 };
296
297
298
299 struct zip_file {
300 struct zip *za;
301 struct zip_error error;
302 int eof;
303 struct zip_source *src;
304 };
305
306
307
308 #define ZIP_DIRENT_COMP_METHOD 0x0001u
309 #define ZIP_DIRENT_FILENAME 0x0002u
310 #define ZIP_DIRENT_COMMENT 0x0004u
311 #define ZIP_DIRENT_EXTRA_FIELD 0x0008u
312 #define ZIP_DIRENT_ATTRIBUTES 0x0010u
313 #define ZIP_DIRENT_ALL 0xffffu
314
315 struct zip_dirent {
316 zip_uint32_t changed;
317 int local_extra_fields_read;
318 int cloned;
319
320 zip_uint16_t version_madeby;
321 zip_uint16_t version_needed;
322 zip_uint16_t bitflags;
323 zip_int32_t comp_method;
324 time_t last_mod;
325 zip_uint32_t crc;
326 zip_uint64_t comp_size;
327 zip_uint64_t uncomp_size;
328 struct zip_string *filename;
329 struct zip_extra_field *extra_fields;
330 struct zip_string *comment;
331 zip_uint32_t disk_number;
332 zip_uint16_t int_attrib;
333 zip_uint32_t ext_attrib;
334 zip_uint64_t offset;
335 };
336
337
338
339 struct zip_cdir {
340 struct zip_entry *entry;
341 zip_uint64_t nentry;
342 zip_uint64_t nentry_alloc;
343
344 off_t size;
345 off_t offset;
346 struct zip_string *comment;
347 };
348
349 struct zip_extra_field {
350 struct zip_extra_field *next;
351 zip_flags_t flags;
352 zip_uint16_t id;
353 zip_uint16_t size;
354 zip_uint8_t *data;
355 };
356
357
358
359 struct zip_source {
360 struct zip_source *src;
361 union {
362 zip_source_callback f;
363 zip_source_layered_callback l;
364 } cb;
365 void *ud;
366 enum zip_les error_source;
367 int is_open;
368 };
369
370
371
372 struct zip_entry {
373 struct zip_dirent *orig;
374 struct zip_dirent *changes;
375 struct zip_source *source;
376 int deleted;
377 };
378
379
380
381
382
383 struct zip_string {
384 zip_uint8_t *raw;
385 zip_uint16_t length;
386 enum zip_encoding_type encoding;
387 zip_uint8_t *converted;
388 zip_uint32_t converted_length;
389 };
390
391
392
393
394
395 struct zip_filelist {
396 zip_uint64_t idx;
397 const char *name;
398 };
399
400
401
402 extern const char * const _zip_err_str[];
403 extern const int _zip_nerr_str;
404 extern const int _zip_err_type[];
405
406
407
408 #define ZIP_ENTRY_CHANGED(e, f) ((e)->changes && ((e)->changes->changed & (f)))
409
410 #define ZIP_ENTRY_DATA_CHANGED(x) ((x)->source != NULL)
411
412 #define ZIP_IS_RDONLY(za) ((za)->ch_flags & ZIP_AFL_RDONLY)
413
414
415
416 zip_int64_t _zip_add_entry(struct zip *);
417
418 int _zip_cdir_compute_crc(struct zip *, uLong *);
419 void _zip_cdir_free(struct zip_cdir *);
420 int _zip_cdir_grow(struct zip_cdir *, zip_uint64_t, struct zip_error *);
421 struct zip_cdir *_zip_cdir_new(zip_uint64_t, struct zip_error *);
422 zip_int64_t _zip_cdir_write(struct zip *, const struct zip_filelist *, zip_uint64_t, FILE *);
423
424 struct zip_dirent *_zip_dirent_clone(const struct zip_dirent *);
425 void _zip_dirent_free(struct zip_dirent *);
426 void _zip_dirent_finalize(struct zip_dirent *);
427 void _zip_dirent_init(struct zip_dirent *);
428 int _zip_dirent_needs_zip64(const struct zip_dirent *, zip_flags_t);
429 struct zip_dirent *_zip_dirent_new(void);
430 int _zip_dirent_read(struct zip_dirent *, FILE *, const unsigned char **,
431 zip_uint64_t *, int, struct zip_error *);
432 zip_int32_t _zip_dirent_size(FILE *, zip_uint16_t, struct zip_error *);
433 void _zip_dirent_torrent_normalize(struct zip_dirent *);
434 int _zip_dirent_write(struct zip_dirent *, FILE *, zip_flags_t, struct zip_error *);
435
436 struct zip_extra_field *_zip_ef_clone(const struct zip_extra_field *, struct zip_error *);
437 struct zip_extra_field *_zip_ef_delete_by_id(struct zip_extra_field *, zip_uint16_t, zip_uint16_t, zip_flags_t);
438 void _zip_ef_free(struct zip_extra_field *);
439 const zip_uint8_t *_zip_ef_get_by_id(const struct zip_extra_field *, zip_uint16_t *, zip_uint16_t, zip_uint16_t, zip_flags_t, struct zip_error *);
440 struct zip_extra_field *_zip_ef_merge(struct zip_extra_field *, struct zip_extra_field *);
441 struct zip_extra_field *_zip_ef_new(zip_uint16_t, zip_uint16_t, const zip_uint8_t *, zip_flags_t);
442 struct zip_extra_field *_zip_ef_parse(const zip_uint8_t *, zip_uint16_t, zip_flags_t, struct zip_error *);
443 struct zip_extra_field *_zip_ef_remove_internal(struct zip_extra_field *);
444 zip_uint16_t _zip_ef_size(const struct zip_extra_field *, zip_flags_t);
445 void _zip_ef_write(const struct zip_extra_field *, zip_flags_t, FILE *);
446
447 void _zip_entry_finalize(struct zip_entry *);
448 void _zip_entry_init(struct zip_entry *);
449
450 void _zip_error_clear(struct zip_error *);
451 void _zip_error_copy(struct zip_error *, const struct zip_error *);
452 void _zip_error_fini(struct zip_error *);
453 void _zip_error_get(const struct zip_error *, int *, int *);
454 void _zip_error_init(struct zip_error *);
455 void _zip_error_set(struct zip_error *, int, int);
456 void _zip_error_set_from_source(struct zip_error *, struct zip_source *);
457 const char *_zip_error_strerror(struct zip_error *);
458
459 const zip_uint8_t *_zip_extract_extra_field_by_id(struct zip_error *, zip_uint16_t, int, const zip_uint8_t *, zip_uint16_t, zip_uint16_t *);
460
461 int _zip_file_extra_field_prepare_for_change(struct zip *, zip_uint64_t);
462 int _zip_file_fillbuf(void *, size_t, struct zip_file *);
463 zip_uint64_t _zip_file_get_offset(const struct zip *, zip_uint64_t, struct zip_error *);
464
465 int _zip_filerange_crc(FILE *, off_t, off_t, uLong *, struct zip_error *);
466
467 struct zip_dirent *_zip_get_dirent(struct zip *, zip_uint64_t, zip_flags_t, struct zip_error *);
468
469 enum zip_encoding_type _zip_guess_encoding(struct zip_string *, enum zip_encoding_type);
470 zip_uint8_t *_zip_cp437_to_utf8(const zip_uint8_t * const, zip_uint32_t,
471 zip_uint32_t *, struct zip_error *error);
472
473 struct zip *_zip_open(const char *, FILE *, unsigned int, int *);
474
475 int _zip_read_local_ef(struct zip *, zip_uint64_t);
476
477 struct zip_source *_zip_source_file_or_p(struct zip *, const char *, FILE *,
478 zip_uint64_t, zip_int64_t, int,
479 const struct zip_stat *);
480 struct zip_source *_zip_source_new(struct zip *);
481 struct zip_source *_zip_source_zip_new(struct zip *, struct zip *, zip_uint64_t, zip_flags_t,
482 zip_uint64_t, zip_uint64_t, const char *);
483
484 int _zip_string_equal(const struct zip_string *, const struct zip_string *);
485 void _zip_string_free(struct zip_string *);
486 zip_uint32_t _zip_string_crc32(const struct zip_string *);
487 const zip_uint8_t *_zip_string_get(struct zip_string *, zip_uint32_t *, zip_flags_t, struct zip_error *);
488 zip_uint16_t _zip_string_length(const struct zip_string *);
489 struct zip_string *_zip_string_new(const zip_uint8_t *, zip_uint16_t, zip_flags_t, struct zip_error *);
490 void _zip_string_write(const struct zip_string *, FILE *);
491
492 int _zip_changed(const struct zip *, zip_uint64_t *);
493 const char *_zip_get_name(struct zip *, zip_uint64_t, zip_flags_t, struct zip_error *);
494 int _zip_local_header_read(struct zip *, int);
495 void *_zip_memdup(const void *, size_t, struct zip_error *);
496 zip_int64_t _zip_name_locate(struct zip *, const char *, zip_flags_t, struct zip_error *);
497 struct zip *_zip_new(struct zip_error *);
498 zip_uint16_t _zip_read2(const zip_uint8_t **);
499 zip_uint32_t _zip_read4(const zip_uint8_t **);
500 zip_uint64_t _zip_read8(const zip_uint8_t **);
501 zip_uint8_t *_zip_read_data(const zip_uint8_t **, FILE *, size_t, int, struct zip_error *);
502 zip_int64_t _zip_file_replace(struct zip *, zip_uint64_t, const char *, struct zip_source *, zip_flags_t);
503 int _zip_set_name(struct zip *, zip_uint64_t, const char *, zip_flags_t);
504 void _zip_u2d_time(time_t, zip_uint16_t *, zip_uint16_t *);
505 int _zip_unchange(struct zip *, zip_uint64_t, int);
506 void _zip_unchange_data(struct zip_entry *);
507
508 void _zip_poke4(zip_uint32_t, zip_uint8_t **);
509 void _zip_poke8(zip_uint64_t, zip_uint8_t **);
510 void _zip_write2(zip_uint16_t, FILE *);
511 void _zip_write4(zip_uint32_t, FILE *);
512 void _zip_write8(zip_uint64_t, FILE *);
513
514
515 #endif