xref: /PHP-5.6/ext/zip/lib/zipint.h (revision 0579e827)
1 #ifndef _HAD_ZIPINT_H
2 #define _HAD_ZIPINT_H
3 
4 /*
5   zipint.h -- internal declarations.
6   Copyright (C) 1999-2015 Dieter Baron and Thomas Klausner
7 
8   This file is part of libzip, a library to manipulate ZIP archives.
9   The authors can be contacted at <libzip@nih.at>
10 
11   Redistribution and use in source and binary forms, with or without
12   modification, are permitted provided that the following conditions
13   are met:
14   1. Redistributions of source code must retain the above copyright
15      notice, this list of conditions and the following disclaimer.
16   2. Redistributions in binary form must reproduce the above copyright
17      notice, this list of conditions and the following disclaimer in
18      the documentation and/or other materials provided with the
19      distribution.
20   3. The names of the authors may not be used to endorse or promote
21      products derived from this software without specific prior
22      written permission.
23 
24   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
25   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
28   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 
37 /* to have *_MAX definitions for all types when compiling with g++ */
38 #define __STDC_LIMIT_MACROS
39 
40 #include <zlib.h>
41 
42 #ifdef PHP_WIN32
43 /* for dup(), close(), etc. */
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 /* crashes reported when using fdopen instead of _fdopen on Windows/Visual Studio 10/Win64 */
74 #if defined(HAVE__FDOPEN)
75 #define fdopen		_fdopen
76 #endif
77 #if defined(HAVE__FILENO)
78 #define fileno		_fileno
79 #endif
80 /* Windows' open() doesn't understand Unix permissions */
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 /* according to unzip-6.0's zipinfo.c, this corresponds to a regular file with rw permissions for everyone */
164 #define ZIP_EXT_ATTRIB_DEFAULT		(0100666<<16)
165 /* according to unzip-6.0's zipinfo.c, this corresponds to a directory with rwx permissions for everyone */
166 #define ZIP_EXT_ATTRIB_DEFAULT_DIR	(0040777<<16)
167 
168 
169 /* This section contains API that won't materialize like this.  It's
170    placed in the internal section, pending cleanup. */
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 /* This API is not final yet, but we need it internally, so it's private for now. */
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 /* This section contains API that is of limited use until support for
191    user-supplied compression/encryption implementation is finished.
192    Thus we will keep it private for now. */
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 /* This function will probably remain private.  It is not needed to
222    implement compression/encryption routines.  (We should probably
223    rename it to _zip_source_pop.) */
224 
225 struct zip_source *zip_source_pop(struct zip_source *);
226 
227 
228 
229 /* error source for layered sources */
230 
231 enum zip_les { ZIP_LES_NONE, ZIP_LES_UPPER, ZIP_LES_LOWER, ZIP_LES_INVAL };
232 
233 /* directory entry: general purpose bit flags */
234 
235 #define ZIP_GPBF_ENCRYPTED		0x0001	/* is encrypted */
236 #define ZIP_GPBF_DATA_DESCRIPTOR	0x0008	/* crc/size after file data */
237 #define ZIP_GPBF_STRONG_ENCRYPTION	0x0040  /* uses strong encryption */
238 #define ZIP_GPBF_ENCODING_UTF_8		0x0800  /* file name encoding is UTF-8 */
239 
240 
241 /* extra fields */
242 #define ZIP_EF_LOCAL		ZIP_FL_LOCAL			/* include in local header */
243 #define ZIP_EF_CENTRAL		ZIP_FL_CENTRAL			/* include in central directory */
244 #define ZIP_EF_BOTH		(ZIP_EF_LOCAL|ZIP_EF_CENTRAL)	/* include in both */
245 
246 #define ZIP_FL_FORCE_ZIP64	1024  /* force zip64 extra field (_zip_dirent_write) */
247 
248 #define ZIP_FL_ENCODING_ALL	(ZIP_FL_ENC_GUESS|ZIP_FL_ENC_CP437|ZIP_FL_ENC_UTF_8)
249 
250 
251 /* encoding type */
252 enum zip_encoding_type {
253     ZIP_ENCODING_UNKNOWN,       /* not yet analyzed */
254     ZIP_ENCODING_ASCII,         /* plain ASCII */
255     ZIP_ENCODING_UTF8_KNOWN,    /* is UTF-8 */
256     ZIP_ENCODING_UTF8_GUESSED,  /* possibly UTF-8 */
257     ZIP_ENCODING_CP437,         /* Code Page 437 */
258     ZIP_ENCODING_ERROR          /* should be UTF-8 but isn't */
259 };
260 
261 /* error information */
262 
263 struct zip_error {
264     int zip_err;	/* libzip error code (ZIP_ER_*) */
265     int sys_err;	/* copy of errno (E*) or zlib error code */
266     char *str;		/* string representation or NULL */
267 };
268 
269 /* zip archive, part of API */
270 
271 struct zip {
272     char *zn;				/* file name */
273     FILE *zp;				/* file */
274     unsigned int open_flags;		/* flags passed to zip_open */
275     struct zip_error error;		/* error information */
276 
277     unsigned int flags;			/* archive global flags */
278     unsigned int ch_flags;		/* changed archive global flags */
279 
280     char *default_password;		/* password used when no other supplied */
281 
282     struct zip_string *comment_orig;	/* archive comment */
283     struct zip_string *comment_changes; /* changed archive comment */
284     int comment_changed;		/* whether archive comment was changed */
285 
286     zip_uint64_t nentry;		/* number of entries */
287     zip_uint64_t nentry_alloc;		/* number of entries allocated */
288     struct zip_entry *entry;		/* entries */
289 
290     unsigned int nfile;			/* number of opened files within archive */
291     unsigned int nfile_alloc;		/* number of files allocated */
292     struct zip_file **file;		/* opened files within archive */
293 
294     char *tempdir;                      /* custom temp dir (needed e.g. for OS X sandboxing) */
295 };
296 
297 /* file in zip archive, part of API */
298 
299 struct zip_file {
300     struct zip *za;		/* zip archive containing this file */
301     struct zip_error error;	/* error information */
302     int eof;
303     struct zip_source *src;	/* data source */
304 };
305 
306 /* zip archive directory entry (central or local) */
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;		/*      whether we already read in local header extra fields */
318     int cloned;                                 /*      whether this instance is cloned, and thus shares non-changed strings */
319 
320     zip_uint16_t version_madeby;		/* (c)  version of creator */
321     zip_uint16_t version_needed;		/* (cl) version needed to extract */
322     zip_uint16_t bitflags;			/* (cl) general purpose bit flag */
323     zip_int32_t comp_method;			/* (cl) compression method used (uint16 and ZIP_CM_DEFAULT (-1)) */
324     time_t last_mod;				/* (cl) time of last modification */
325     zip_uint32_t crc;				/* (cl) CRC-32 of uncompressed data */
326     zip_uint64_t comp_size;			/* (cl) size of compressed data */
327     zip_uint64_t uncomp_size;			/* (cl) size of uncompressed data */
328     struct zip_string *filename;		/* (cl) file name (NUL-terminated) */
329     struct zip_extra_field *extra_fields;	/* (cl) extra fields, parsed */
330     struct zip_string *comment;			/* (c)  file comment */
331     zip_uint32_t disk_number;			/* (c)  disk number start */
332     zip_uint16_t int_attrib;			/* (c)  internal file attributes */
333     zip_uint32_t ext_attrib;			/* (c)  external file attributes */
334     zip_uint64_t offset;			/* (c)  offset of local header */
335 };
336 
337 /* zip archive central directory */
338 
339 struct zip_cdir {
340     struct zip_entry *entry;	 		/* directory entries */
341     zip_uint64_t nentry;			/* number of entries */
342     zip_uint64_t nentry_alloc;			/* number of entries allocated */
343 
344     off_t size;                                 /* size of central directory */
345     off_t offset;		 		/* offset of central directory in file */
346     struct zip_string *comment;			/* zip archive comment */
347 };
348 
349 struct zip_extra_field {
350     struct zip_extra_field *next;
351     zip_flags_t flags;				/* in local/central header */
352     zip_uint16_t id;				/* header id */
353     zip_uint16_t size;				/* data 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 /* entry in zip archive directory */
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 /* file or archive comment, or filename */
382 
383 struct zip_string {
384     zip_uint8_t *raw;			/* raw string */
385     zip_uint16_t length;		/* length of raw string */
386     enum zip_encoding_type encoding; 	/* autorecognized encoding */
387     zip_uint8_t *converted;     	/* autoconverted string */
388     zip_uint32_t converted_length;	/* length of converted */
389 };
390 
391 
392 
393 /* which files to write, and in which order (name is for torrentzip sorting) */
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 /* zipint.h */
516