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