xref: /php-src/ext/phar/phar_internal.h (revision 11accb5c)
1 /*
2   +----------------------------------------------------------------------+
3   | phar php single-file executable PHP extension                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | https://www.php.net/license/3_01.txt                                 |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Gregory Beaver <cellog@php.net>                             |
16   |          Marcus Boerger <helly@php.net>                              |
17   +----------------------------------------------------------------------+
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <time.h>
25 #include "php.h"
26 #include "tar.h"
27 #include "php_ini.h"
28 #include "zend_constants.h"
29 #include "zend_execute.h"
30 #include "zend_exceptions.h"
31 #include "zend_hash.h"
32 #include "zend_interfaces.h"
33 #include "zend_operators.h"
34 #include "zend_sort.h"
35 #include "zend_vm.h"
36 #include "zend_smart_str.h"
37 #include "main/php_streams.h"
38 #include "main/streams/php_stream_plain_wrapper.h"
39 #include "main/SAPI.h"
40 #include "main/php_main.h"
41 #include "main/php_open_temporary_file.h"
42 #include "ext/standard/info.h"
43 #include "ext/standard/basic_functions.h"
44 #include "ext/standard/file.h"
45 #include "ext/standard/php_string.h"
46 #include "ext/standard/url.h"
47 #include "ext/standard/crc32.h"
48 #include "ext/standard/md5.h"
49 #include "ext/standard/sha1.h"
50 #include "ext/standard/php_var.h"
51 #include "ext/standard/php_versioning.h"
52 #include "Zend/zend_virtual_cwd.h"
53 #include "ext/spl/spl_array.h"
54 #include "ext/spl/spl_directory.h"
55 #include "ext/spl/spl_exceptions.h"
56 #include "ext/spl/spl_iterators.h"
57 #include "php_phar.h"
58 #include "ext/hash/php_hash.h"
59 #include "ext/hash/php_hash_sha.h"
60 
61 /* PHP_ because this is public information via MINFO */
62 #define PHP_PHAR_API_VERSION      "1.1.1"
63 /* x.y.z maps to 0xyz0 */
64 #define PHAR_API_VERSION          0x1110
65 /* if we bump PHAR_API_VERSION, change this from 0x1100 to PHAR_API_VERSION */
66 #define PHAR_API_VERSION_NODIR    0x1100
67 #define PHAR_API_MIN_DIR          0x1110
68 #define PHAR_API_MIN_READ         0x1000
69 #define PHAR_API_MAJORVERSION     0x1000
70 #define PHAR_API_MAJORVER_MASK    0xF000
71 #define PHAR_API_VER_MASK         0xFFF0
72 
73 #define PHAR_HDR_COMPRESSION_MASK 0x0000F000
74 #define PHAR_HDR_COMPRESSED_NONE  0x00000000
75 #define PHAR_HDR_COMPRESSED_GZ    0x00001000
76 #define PHAR_HDR_COMPRESSED_BZ2   0x00002000
77 #define PHAR_HDR_SIGNATURE        0x00010000
78 
79 /* flags for defining that the entire file should be compressed */
80 #define PHAR_FILE_COMPRESSION_MASK 0x00F00000
81 #define PHAR_FILE_COMPRESSED_NONE  0x00000000
82 #define PHAR_FILE_COMPRESSED_GZ    0x00100000
83 #define PHAR_FILE_COMPRESSED_BZ2   0x00200000
84 
85 #define PHAR_SIG_MD5              0x0001
86 #define PHAR_SIG_SHA1             0x0002
87 #define PHAR_SIG_SHA256           0x0003
88 #define PHAR_SIG_SHA512           0x0004
89 #define PHAR_SIG_OPENSSL          0x0010
90 #define PHAR_SIG_OPENSSL_SHA256   0x0011
91 #define PHAR_SIG_OPENSSL_SHA512   0x0012
92 
93 /* flags byte for each file adheres to these bitmasks.
94    All unused values are reserved */
95 #define PHAR_ENT_COMPRESSION_MASK 0x0000F000
96 #define PHAR_ENT_COMPRESSED_NONE  0x00000000
97 #define PHAR_ENT_COMPRESSED_GZ    0x00001000
98 #define PHAR_ENT_COMPRESSED_BZ2   0x00002000
99 
100 #define PHAR_ENT_PERM_MASK        0x000001FF
101 #define PHAR_ENT_PERM_MASK_USR    0x000001C0
102 #define PHAR_ENT_PERM_SHIFT_USR   6
103 #define PHAR_ENT_PERM_MASK_GRP    0x00000038
104 #define PHAR_ENT_PERM_SHIFT_GRP   3
105 #define PHAR_ENT_PERM_MASK_OTH    0x00000007
106 #define PHAR_ENT_PERM_DEF_FILE    0x000001B6
107 #define PHAR_ENT_PERM_DEF_DIR     0x000001FF
108 
109 #define PHAR_FORMAT_SAME    0
110 #define PHAR_FORMAT_PHAR    1
111 #define PHAR_FORMAT_TAR     2
112 #define PHAR_FORMAT_ZIP     3
113 
114 #define TAR_FILE    '0'
115 #define TAR_LINK    '1'
116 #define TAR_SYMLINK '2'
117 #define TAR_DIR     '5'
118 #define TAR_NEW     '8'
119 #define TAR_GLOBAL_HDR 'g'
120 #define TAR_FILE_HDR   'x'
121 
122 #define PHAR_MUNG_PHP_SELF			(1<<0)
123 #define PHAR_MUNG_REQUEST_URI		(1<<1)
124 #define PHAR_MUNG_SCRIPT_NAME		(1<<2)
125 #define PHAR_MUNG_SCRIPT_FILENAME	(1<<3)
126 
127 typedef struct _phar_entry_fp phar_entry_fp;
128 typedef struct _phar_archive_data phar_archive_data;
129 
130 ZEND_BEGIN_MODULE_GLOBALS(phar)
131 	/* a list of phar_archive_data objects that reference a cached phar, so
132 	   that if copy-on-write is performed, we can swap them out for the new value */
133 	HashTable   phar_persist_map;
134 	HashTable   phar_fname_map;
135 	/* for cached phars, this is a per-process store of fp/ufp */
136 	phar_entry_fp *cached_fp;
137 	HashTable   phar_alias_map;
138 	int         phar_SERVER_mung_list;
139 	char*       cache_list;
140 	bool        readonly;
141 	bool        manifest_cached;
142 	bool        persist;
143 	bool        has_zlib;
144 	bool        has_bz2;
145 	bool   readonly_orig;
146 	bool   require_hash_orig;
147 	bool   intercepted;
148 	bool        request_init;
149 	bool        require_hash;
150 	bool        request_done;
151 	bool        request_ends;
152 	zif_handler orig_fopen;
153 	zif_handler orig_file_get_contents;
154 	zif_handler orig_is_file;
155 	zif_handler orig_is_link;
156 	zif_handler orig_is_dir;
157 	zif_handler orig_opendir;
158 	zif_handler orig_file_exists;
159 	zif_handler orig_fileperms;
160 	zif_handler orig_fileinode;
161 	zif_handler orig_filesize;
162 	zif_handler orig_fileowner;
163 	zif_handler orig_filegroup;
164 	zif_handler orig_fileatime;
165 	zif_handler orig_filemtime;
166 	zif_handler orig_filectime;
167 	zif_handler orig_filetype;
168 	zif_handler orig_is_writable;
169 	zif_handler orig_is_readable;
170 	zif_handler orig_is_executable;
171 	zif_handler orig_lstat;
172 	zif_handler orig_readfile;
173 	zif_handler orig_stat;
174 	/* used for includes with . in them inside front controller */
175 	char*       cwd;
176 	uint32_t    cwd_len;
177 	bool        cwd_init;
178 	char        *openssl_privatekey;
179 	uint32_t    openssl_privatekey_len;
180 	/* phar_get_archive cache */
181 	char*       last_phar_name;
182 	uint32_t    last_phar_name_len;
183 	char*       last_alias;
184 	uint32_t    last_alias_len;
185 	phar_archive_data* last_phar;
186 	HashTable mime_types;
187 ZEND_END_MODULE_GLOBALS(phar)
188 
189 ZEND_EXTERN_MODULE_GLOBALS(phar)
190 #define PHAR_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(phar, v)
191 
192 #if defined(ZTS) && defined(COMPILE_DL_PHAR)
193 ZEND_TSRMLS_CACHE_EXTERN()
194 #endif
195 
196 #include "pharzip.h"
197 
198 typedef union _phar_archive_object  phar_archive_object;
199 typedef union _phar_entry_object    phar_entry_object;
200 
201 /*
202  * used in phar_entry_info->fp_type to
203  */
204 enum phar_fp_type {
205 	/* regular file pointer phar_archive_data->fp */
206 	PHAR_FP,
207 	/* uncompressed file pointer phar_archive_data->uncompressed_fp */
208 	PHAR_UFP,
209 	/* modified file pointer phar_entry_info->fp */
210 	PHAR_MOD,
211 	/* temporary manifest entry (file outside of the phar mapped to a location inside the phar)
212 	   this entry stores the stream to open in link (normally used for tars, but we steal it here) */
213 	PHAR_TMP
214 };
215 
216 /*
217  * Represents the metadata of the phar file or a file entry within the phar.
218  * Can contain any combination of serialized data and the value as needed.
219  */
220 typedef struct _phar_metadata_tracker {
221 	/* Can be IS_UNDEF or a regular value */
222 	zval val;
223 	/* Nullable string with the serialized value, if the serialization was performed or read from a file. */
224 	zend_string *str;
225 } phar_metadata_tracker;
226 
227 /* entry for one file in a phar file */
228 typedef struct _phar_entry_info {
229 	/* first bytes are exactly as in file */
230 	uint32_t                 uncompressed_filesize;
231 	uint32_t                 timestamp;
232 	uint32_t                 compressed_filesize;
233 	uint32_t                 crc32;
234 	uint32_t                 flags;
235 	/* remainder */
236 	/* when changing compression, save old flags in case fp is NULL */
237 	uint32_t                 old_flags;
238 	phar_metadata_tracker metadata_tracker;
239 	uint32_t                 filename_len;
240 	char                     *filename;
241 	enum phar_fp_type        fp_type;
242 	/* offset within original phar file of the file contents */
243 	zend_long                     offset_abs;
244 	/* offset within fp of the file contents */
245 	zend_long                     offset;
246 	/* offset within original phar file of the file header (for zip-based/tar-based) */
247 	zend_long                     header_offset;
248 	php_stream               *fp;
249 	php_stream               *cfp;
250 	int                      fp_refcount;
251 	char                     *tmp;
252 	phar_archive_data        *phar;
253 	char                     *link; /* symbolic link to another file */
254 	char                     tar_type;
255 	/* position in the manifest */
256 	uint32_t                     manifest_pos;
257 	/* for stat */
258 	unsigned short           inode;
259 
260 	uint32_t             is_crc_checked:1;
261 	uint32_t             is_modified:1;
262 	uint32_t             is_deleted:1;
263 	uint32_t             is_dir:1;
264 	/* this flag is used for mounted entries (external files mapped to location
265 	   inside a phar */
266 	uint32_t             is_mounted:1;
267 	/* used when iterating */
268 	uint32_t             is_temp_dir:1;
269 	/* tar-based phar file stuff */
270 	uint32_t             is_tar:1;
271 	/* zip-based phar file stuff */
272 	uint32_t             is_zip:1;
273 	/* for cached phar entries */
274 	uint32_t             is_persistent:1;
275 } phar_entry_info;
276 
277 /* information about a phar file (the archive itself) */
278 struct _phar_archive_data {
279 	char                     *fname;
280 	uint32_t                 fname_len;
281 	/* for phar_detect_fname_ext, this stores the location of the file extension within fname */
282 	char                     *ext;
283 	uint32_t                 ext_len;
284 	char                     *alias;
285 	uint32_t                      alias_len;
286 	char                     version[12];
287 	size_t                   halt_offset;
288 	HashTable                manifest;
289 	/* hash of virtual directories, as in path/to/file.txt has path/to and path as virtual directories */
290 	HashTable                virtual_dirs;
291 	/* hash of mounted directory paths */
292 	HashTable                mounted_dirs;
293 	uint32_t                 flags;
294 	uint32_t                 min_timestamp;
295 	uint32_t                 max_timestamp;
296 	php_stream               *fp;
297 	/* decompressed file contents are stored here */
298 	php_stream               *ufp;
299 	int                      refcount;
300 	uint32_t                 sig_flags;
301 	uint32_t                 sig_len;
302 	char                     *signature;
303 	phar_metadata_tracker metadata_tracker;
304 	uint32_t                 phar_pos;
305 	/* if 1, then this alias was manually specified by the user and is not a permanent alias */
306 	uint32_t             is_temporary_alias:1;
307 	uint32_t             is_modified:1;
308 	uint32_t             is_writeable:1;
309 	uint32_t             is_brandnew:1;
310 	/* defer phar creation */
311 	uint32_t             donotflush:1;
312 	/* zip-based phar variables */
313 	uint32_t             is_zip:1;
314 	/* tar-based phar variables */
315 	uint32_t             is_tar:1;
316 	/* PharData variables       */
317 	uint32_t             is_data:1;
318 	/* for cached phar manifests */
319 	uint32_t             is_persistent:1;
320 };
321 
322 typedef struct _phar_entry_fp_info {
323 	enum phar_fp_type        fp_type;
324 	/* offset within fp of the file contents */
325 	zend_long                     offset;
326 } phar_entry_fp_info;
327 
328 struct _phar_entry_fp {
329 	php_stream *fp;
330 	php_stream *ufp;
331 	phar_entry_fp_info *manifest;
332 };
333 
phar_get_entrypfp(phar_entry_info * entry)334 static inline php_stream *phar_get_entrypfp(phar_entry_info *entry)
335 {
336 	if (!entry->is_persistent) {
337 		return entry->phar->fp;
338 	}
339 	return PHAR_G(cached_fp)[entry->phar->phar_pos].fp;
340 }
341 
phar_get_entrypufp(phar_entry_info * entry)342 static inline php_stream *phar_get_entrypufp(phar_entry_info *entry)
343 {
344 	if (!entry->is_persistent) {
345 		return entry->phar->ufp;
346 	}
347 	return PHAR_G(cached_fp)[entry->phar->phar_pos].ufp;
348 }
349 
phar_set_entrypfp(phar_entry_info * entry,php_stream * fp)350 static inline void phar_set_entrypfp(phar_entry_info *entry, php_stream *fp)
351 {
352 	if (!entry->phar->is_persistent) {
353 		entry->phar->fp =  fp;
354 		return;
355 	}
356 
357 	PHAR_G(cached_fp)[entry->phar->phar_pos].fp = fp;
358 }
359 
phar_set_entrypufp(phar_entry_info * entry,php_stream * fp)360 static inline void phar_set_entrypufp(phar_entry_info *entry, php_stream *fp)
361 {
362 	if (!entry->phar->is_persistent) {
363 		entry->phar->ufp =  fp;
364 		return;
365 	}
366 
367 	PHAR_G(cached_fp)[entry->phar->phar_pos].ufp = fp;
368 }
369 
phar_get_pharfp(phar_archive_data * phar)370 static inline php_stream *phar_get_pharfp(phar_archive_data *phar)
371 {
372 	if (!phar->is_persistent) {
373 		return phar->fp;
374 	}
375 	return PHAR_G(cached_fp)[phar->phar_pos].fp;
376 }
377 
phar_get_pharufp(phar_archive_data * phar)378 static inline php_stream *phar_get_pharufp(phar_archive_data *phar)
379 {
380 	if (!phar->is_persistent) {
381 		return phar->ufp;
382 	}
383 	return PHAR_G(cached_fp)[phar->phar_pos].ufp;
384 }
385 
phar_set_pharfp(phar_archive_data * phar,php_stream * fp)386 static inline void phar_set_pharfp(phar_archive_data *phar, php_stream *fp)
387 {
388 	if (!phar->is_persistent) {
389 		phar->fp =  fp;
390 		return;
391 	}
392 
393 	PHAR_G(cached_fp)[phar->phar_pos].fp = fp;
394 }
395 
phar_set_pharufp(phar_archive_data * phar,php_stream * fp)396 static inline void phar_set_pharufp(phar_archive_data *phar, php_stream *fp)
397 {
398 	if (!phar->is_persistent) {
399 		phar->ufp =  fp;
400 		return;
401 	}
402 
403 	PHAR_G(cached_fp)[phar->phar_pos].ufp = fp;
404 }
405 
phar_set_fp_type(phar_entry_info * entry,enum phar_fp_type type,zend_off_t offset)406 static inline void phar_set_fp_type(phar_entry_info *entry, enum phar_fp_type type, zend_off_t offset)
407 {
408 	phar_entry_fp_info *data;
409 
410 	if (!entry->is_persistent) {
411 		entry->fp_type = type;
412 		entry->offset = offset;
413 		return;
414 	}
415 	data = &(PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos]);
416 	data->fp_type = type;
417 	data->offset = offset;
418 }
419 
phar_get_fp_type(phar_entry_info * entry)420 static inline enum phar_fp_type phar_get_fp_type(phar_entry_info *entry)
421 {
422 	if (!entry->is_persistent) {
423 		return entry->fp_type;
424 	}
425 	return PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].fp_type;
426 }
427 
phar_get_fp_offset(phar_entry_info * entry)428 static inline zend_off_t phar_get_fp_offset(phar_entry_info *entry)
429 {
430 	if (!entry->is_persistent) {
431 		return entry->offset;
432 	}
433 	if (PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].fp_type == PHAR_FP) {
434 		if (!PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].offset) {
435 			PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].offset = entry->offset;
436 		}
437 	}
438 	return PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].offset;
439 }
440 
441 #define PHAR_MIME_PHP '\0'
442 #define PHAR_MIME_PHPS '\1'
443 #define PHAR_MIME_OTHER '\2'
444 
445 typedef struct _phar_mime_type {
446 	char *mime;
447 	uint32_t len;
448 	/* one of PHAR_MIME_* */
449 	char type;
450 } phar_mime_type;
451 
452 /* stream access data for one file entry in a phar file */
453 typedef struct _phar_entry_data {
454 	phar_archive_data        *phar;
455 	php_stream               *fp;
456 	/* stream position proxy, allows multiple open streams referring to the same fp */
457 	zend_off_t                    position;
458 	/* for copies of the phar fp, defines where 0 is */
459 	zend_off_t                    zero;
460 	phar_entry_info          *internal_file;
461 } phar_entry_data;
462 
463 /* archive php object */
464 union _phar_archive_object {
465 	spl_filesystem_object    spl;
466 	phar_archive_data        *archive;
467 };
468 
469 /* entry php object */
470 union _phar_entry_object {
471 	spl_filesystem_object    spl;
472 	phar_entry_info          *entry;
473 };
474 
BEGIN_EXTERN_C()475 BEGIN_EXTERN_C()
476 
477 #ifdef PHP_WIN32
478 static inline void phar_unixify_path_separators(char *path, size_t path_len)
479 {
480 	char *s;
481 
482 	/* unixify win paths */
483 	for (s = path; (size_t)(s - path) < path_len; ++s) {
484 		if (*s == '\\') {
485 			*s = '/';
486 		}
487 	}
488 }
489 #endif
490 /**
491  * validate an alias, returns 1 for success, 0 for failure
492  */
phar_validate_alias(const char * alias,size_t alias_len)493 static inline int phar_validate_alias(const char *alias, size_t alias_len) /* {{{ */
494 {
495 	return !(memchr(alias, '/', alias_len) || memchr(alias, '\\', alias_len) || memchr(alias, ':', alias_len) ||
496 		memchr(alias, ';', alias_len) || memchr(alias, '\n', alias_len) || memchr(alias, '\r', alias_len));
497 }
498 /* }}} */
499 
phar_set_inode(phar_entry_info * entry)500 static inline void phar_set_inode(phar_entry_info *entry) /* {{{ */
501 {
502 	char tmp[MAXPATHLEN];
503 	size_t tmp_len;
504 	size_t len1, len2;
505 
506 	tmp_len = MIN(MAXPATHLEN, entry->filename_len + entry->phar->fname_len);
507 
508 	len1 = MIN(entry->phar->fname_len, tmp_len);
509 	if (entry->phar->fname) {
510 		memcpy(tmp, entry->phar->fname, len1);
511 	}
512 
513 	len2 = MIN(tmp_len - len1, entry->filename_len);
514 	memcpy(tmp + len1, entry->filename, len2);
515 
516 	entry->inode = (unsigned short) zend_hash_func(tmp, tmp_len);
517 }
518 /* }}} */
519 
520 void phar_request_initialize(void);
521 
522 void phar_object_init(void);
523 void phar_destroy_phar_data(phar_archive_data *phar);
524 
525 int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip);
526 int phar_open_from_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, uint32_t options, phar_archive_data** pphar, char **error);
527 int phar_open_or_create_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
528 int phar_create_or_parse_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
529 int phar_open_executed_filename(char *alias, size_t alias_len, char **error);
530 int phar_free_alias(phar_archive_data *phar, char *alias, size_t alias_len);
531 int phar_get_archive(phar_archive_data **archive, char *fname, size_t fname_len, char *alias, size_t alias_len, char **error);
532 int phar_open_parsed_phar(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
533 int phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t sig_type, char *sig, size_t sig_len, char *fname, char **signature, size_t *signature_len, char **error);
534 int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, size_t *signature_length, char **error);
535 
536 /* utility functions */
537 zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error);
538 char *phar_decompress_filter(phar_entry_info * entry, int return_unknown);
539 char *phar_compress_filter(phar_entry_info * entry, int return_unknown);
540 
541 /* void phar_remove_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len); */
542 void phar_add_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len);
543 int phar_mount_entry(phar_archive_data *phar, char *filename, size_t filename_len, char *path, size_t path_len);
544 zend_string *phar_find_in_include_path(zend_string *file, phar_archive_data **pphar);
545 char *phar_fix_filepath(char *path, size_t *new_len, int use_cwd);
546 phar_entry_info * phar_open_jit(phar_archive_data *phar, phar_entry_info *entry, char **error);
547 void phar_parse_metadata_lazy(const char *buffer, phar_metadata_tracker *tracker, uint32_t zip_metadata_len, int persistent);
548 bool phar_metadata_tracker_has_data(const phar_metadata_tracker* tracker, int persistent);
549 /* If this has data, free it and set all values to undefined. */
550 void phar_metadata_tracker_free(phar_metadata_tracker* val, int persistent);
551 void phar_metadata_tracker_copy(phar_metadata_tracker* dest, const phar_metadata_tracker *source, int persistent);
552 void phar_metadata_tracker_clone(phar_metadata_tracker* tracker);
553 void phar_metadata_tracker_try_ensure_has_serialized_data(phar_metadata_tracker* tracker, int persistent);
554 int phar_metadata_tracker_unserialize_or_copy(phar_metadata_tracker* tracker, zval *value, int persistent, HashTable *unserialize_options, const char* method_name);
555 void destroy_phar_manifest_entry(zval *zv);
556 int phar_seek_efp(phar_entry_info *entry, zend_off_t offset, int whence, zend_off_t position, int follow_links);
557 php_stream *phar_get_efp(phar_entry_info *entry, int follow_links);
558 int phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error);
559 int phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links);
560 phar_entry_info *phar_get_link_source(phar_entry_info *entry);
561 int phar_create_writeable_entry(phar_archive_data *phar, phar_entry_info *entry, char **error);
562 int phar_separate_entry_fp(phar_entry_info *entry, char **error);
563 int phar_open_archive_fp(phar_archive_data *phar);
564 int phar_copy_on_write(phar_archive_data **pphar);
565 
566 /* tar functions in tar.c */
567 int phar_is_tar(char *buf, char *fname);
568 int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, int is_data, uint32_t compression, char **error);
569 int phar_open_or_create_tar(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error);
570 int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error);
571 
572 /* zip functions in zip.c */
573 int phar_parse_zipfile(php_stream *fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, char **error);
574 int phar_open_or_create_zip(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error);
575 int phar_zip_flush(phar_archive_data *archive, char *user_stub, zend_long len, int defaultstub, char **error);
576 
577 #ifdef PHAR_MAIN
578 static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char *alias, size_t alias_len, uint32_t options, phar_archive_data** pphar, int is_data, char **error);
579 extern const php_stream_wrapper php_stream_phar_wrapper;
580 #else
581 extern HashTable cached_phars;
582 extern HashTable cached_alias;
583 #endif
584 
585 int phar_archive_delref(phar_archive_data *phar);
586 void phar_entry_delref(phar_entry_data *idata);
587 
588 phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, int security);
589 phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security);
590 phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security);
591 int phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security);
592 int phar_flush(phar_archive_data *archive, char *user_stub, zend_long len, int convert, char **error);
593 int phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, int is_complete);
594 int phar_split_fname(const char *filename, size_t filename_len, char **arch, size_t *arch_len, char **entry, size_t *entry_len, int executable, int for_create);
595 
596 typedef enum {
597 	pcr_use_query,
598 	pcr_is_ok,
599 	pcr_err_double_slash,
600 	pcr_err_up_dir,
601 	pcr_err_curr_dir,
602 	pcr_err_back_slash,
603 	pcr_err_star,
604 	pcr_err_illegal_char,
605 	pcr_err_empty_entry
606 } phar_path_check_result;
607 
608 phar_path_check_result phar_path_check(char **p, size_t *len, const char **error);
609 
610 END_EXTERN_C()
611