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