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 | 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 #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
92 /* flags byte for each file adheres to these bitmasks.
93 All unused values are reserved */
94 #define PHAR_ENT_COMPRESSION_MASK 0x0000F000
95 #define PHAR_ENT_COMPRESSED_NONE 0x00000000
96 #define PHAR_ENT_COMPRESSED_GZ 0x00001000
97 #define PHAR_ENT_COMPRESSED_BZ2 0x00002000
98
99 #define PHAR_ENT_PERM_MASK 0x000001FF
100 #define PHAR_ENT_PERM_MASK_USR 0x000001C0
101 #define PHAR_ENT_PERM_SHIFT_USR 6
102 #define PHAR_ENT_PERM_MASK_GRP 0x00000038
103 #define PHAR_ENT_PERM_SHIFT_GRP 3
104 #define PHAR_ENT_PERM_MASK_OTH 0x00000007
105 #define PHAR_ENT_PERM_DEF_FILE 0x000001B6
106 #define PHAR_ENT_PERM_DEF_DIR 0x000001FF
107
108 #define PHAR_FORMAT_SAME 0
109 #define PHAR_FORMAT_PHAR 1
110 #define PHAR_FORMAT_TAR 2
111 #define PHAR_FORMAT_ZIP 3
112
113 #define TAR_FILE '0'
114 #define TAR_LINK '1'
115 #define TAR_SYMLINK '2'
116 #define TAR_DIR '5'
117 #define TAR_NEW '8'
118 #define TAR_GLOBAL_HDR 'g'
119 #define TAR_FILE_HDR 'x'
120
121 #define PHAR_MUNG_PHP_SELF (1<<0)
122 #define PHAR_MUNG_REQUEST_URI (1<<1)
123 #define PHAR_MUNG_SCRIPT_NAME (1<<2)
124 #define PHAR_MUNG_SCRIPT_FILENAME (1<<3)
125
126 typedef struct _phar_entry_fp phar_entry_fp;
127 typedef struct _phar_archive_data phar_archive_data;
128
129 ZEND_BEGIN_MODULE_GLOBALS(phar)
130 /* a list of phar_archive_data objects that reference a cached phar, so
131 that if copy-on-write is performed, we can swap them out for the new value */
132 HashTable phar_persist_map;
133 HashTable phar_fname_map;
134 /* for cached phars, this is a per-process store of fp/ufp */
135 phar_entry_fp *cached_fp;
136 HashTable phar_alias_map;
137 int phar_SERVER_mung_list;
138 int readonly;
139 char* cache_list;
140 int manifest_cached;
141 int persist;
142 int has_zlib;
143 int has_bz2;
144 zend_bool readonly_orig;
145 zend_bool require_hash_orig;
146 zend_bool intercepted;
147 int request_init;
148 int require_hash;
149 int request_done;
150 int request_ends;
151 zif_handler orig_fopen;
152 zif_handler orig_file_get_contents;
153 zif_handler orig_is_file;
154 zif_handler orig_is_link;
155 zif_handler orig_is_dir;
156 zif_handler orig_opendir;
157 zif_handler orig_file_exists;
158 zif_handler orig_fileperms;
159 zif_handler orig_fileinode;
160 zif_handler orig_filesize;
161 zif_handler orig_fileowner;
162 zif_handler orig_filegroup;
163 zif_handler orig_fileatime;
164 zif_handler orig_filemtime;
165 zif_handler orig_filectime;
166 zif_handler orig_filetype;
167 zif_handler orig_is_writable;
168 zif_handler orig_is_readable;
169 zif_handler orig_is_executable;
170 zif_handler orig_lstat;
171 zif_handler orig_readfile;
172 zif_handler orig_stat;
173 /* used for includes with . in them inside front controller */
174 char* cwd;
175 uint32_t cwd_len;
176 int cwd_init;
177 char *openssl_privatekey;
178 uint32_t openssl_privatekey_len;
179 /* phar_get_archive cache */
180 char* last_phar_name;
181 uint32_t last_phar_name_len;
182 char* last_alias;
183 uint32_t last_alias_len;
184 phar_archive_data* last_phar;
185 HashTable mime_types;
186 ZEND_END_MODULE_GLOBALS(phar)
187
188 ZEND_EXTERN_MODULE_GLOBALS(phar)
189 #define PHAR_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(phar, v)
190
191 #if defined(ZTS) && defined(COMPILE_DL_PHAR)
192 ZEND_TSRMLS_CACHE_EXTERN()
193 #endif
194
195 #include "pharzip.h"
196
197 typedef union _phar_archive_object phar_archive_object;
198 typedef union _phar_entry_object phar_entry_object;
199
200 /*
201 * used in phar_entry_info->fp_type to
202 */
203 enum phar_fp_type {
204 /* regular file pointer phar_archive_data->fp */
205 PHAR_FP,
206 /* uncompressed file pointer phar_archive_data->uncompressed_fp */
207 PHAR_UFP,
208 /* modified file pointer phar_entry_info->fp */
209 PHAR_MOD,
210 /* temporary manifest entry (file outside of the phar mapped to a location inside the phar)
211 this entry stores the stream to open in link (normally used for tars, but we steal it here) */
212 PHAR_TMP
213 };
214
215 /*
216 * Represents the metadata of the phar file or a file entry within the phar.
217 * Can contain any combination of serialized data and the value as needed.
218 */
219 typedef struct _phar_metadata_tracker {
220 /* Can be IS_UNDEF or a regular value */
221 zval val;
222 /* Nullable string with the serialized value, if the serialization was performed or read from a file. */
223 zend_string *str;
224 } phar_metadata_tracker;
225
226 /* entry for one file in a phar file */
227 typedef struct _phar_entry_info {
228 /* first bytes are exactly as in file */
229 uint32_t uncompressed_filesize;
230 uint32_t timestamp;
231 uint32_t compressed_filesize;
232 uint32_t crc32;
233 uint32_t flags;
234 /* remainder */
235 /* when changing compression, save old flags in case fp is NULL */
236 uint32_t old_flags;
237 phar_metadata_tracker metadata_tracker;
238 uint32_t filename_len;
239 char *filename;
240 enum phar_fp_type fp_type;
241 /* offset within original phar file of the file contents */
242 zend_long offset_abs;
243 /* offset within fp of the file contents */
244 zend_long offset;
245 /* offset within original phar file of the file header (for zip-based/tar-based) */
246 zend_long header_offset;
247 php_stream *fp;
248 php_stream *cfp;
249 int fp_refcount;
250 char *tmp;
251 phar_archive_data *phar;
252 char *link; /* symbolic link to another file */
253 char tar_type;
254 /* position in the manifest */
255 uint32_t manifest_pos;
256 /* for stat */
257 unsigned short inode;
258
259 uint32_t is_crc_checked:1;
260 uint32_t is_modified:1;
261 uint32_t is_deleted:1;
262 uint32_t is_dir:1;
263 /* this flag is used for mounted entries (external files mapped to location
264 inside a phar */
265 uint32_t is_mounted:1;
266 /* used when iterating */
267 uint32_t is_temp_dir:1;
268 /* tar-based phar file stuff */
269 uint32_t is_tar:1;
270 /* zip-based phar file stuff */
271 uint32_t is_zip:1;
272 /* for cached phar entries */
273 uint32_t is_persistent:1;
274 } phar_entry_info;
275
276 /* information about a phar file (the archive itself) */
277 struct _phar_archive_data {
278 char *fname;
279 uint32_t fname_len;
280 /* for phar_detect_fname_ext, this stores the location of the file extension within fname */
281 char *ext;
282 uint32_t ext_len;
283 char *alias;
284 uint32_t alias_len;
285 char version[12];
286 size_t internal_file_start;
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 uint32_t for_write:1;
461 uint32_t is_zip:1;
462 uint32_t is_tar:1;
463 phar_entry_info *internal_file;
464 } phar_entry_data;
465
466 /* archive php object */
467 union _phar_archive_object {
468 spl_filesystem_object spl;
469 phar_archive_data *archive;
470 };
471
472 /* entry php object */
473 union _phar_entry_object {
474 spl_filesystem_object spl;
475 phar_entry_info *entry;
476 };
477
478 #ifndef PHAR_MAIN
479 extern zend_string *(*phar_save_resolve_path)(const char *filename, size_t filename_len);
480 #endif
481
BEGIN_EXTERN_C()482 BEGIN_EXTERN_C()
483
484 #ifdef PHP_WIN32
485 static inline void phar_unixify_path_separators(char *path, size_t path_len)
486 {
487 char *s;
488
489 /* unixify win paths */
490 for (s = path; (size_t)(s - path) < path_len; ++s) {
491 if (*s == '\\') {
492 *s = '/';
493 }
494 }
495 }
496 #endif
497 /**
498 * validate an alias, returns 1 for success, 0 for failure
499 */
phar_validate_alias(const char * alias,size_t alias_len)500 static inline int phar_validate_alias(const char *alias, size_t alias_len) /* {{{ */
501 {
502 return !(memchr(alias, '/', alias_len) || memchr(alias, '\\', alias_len) || memchr(alias, ':', alias_len) ||
503 memchr(alias, ';', alias_len) || memchr(alias, '\n', alias_len) || memchr(alias, '\r', alias_len));
504 }
505 /* }}} */
506
phar_set_inode(phar_entry_info * entry)507 static inline void phar_set_inode(phar_entry_info *entry) /* {{{ */
508 {
509 char tmp[MAXPATHLEN];
510 size_t tmp_len;
511 size_t len1, len2;
512
513 tmp_len = MIN(MAXPATHLEN, entry->filename_len + entry->phar->fname_len);
514
515 len1 = MIN(entry->phar->fname_len, tmp_len);
516 if (entry->phar->fname) {
517 memcpy(tmp, entry->phar->fname, len1);
518 }
519
520 len2 = MIN(tmp_len - len1, entry->filename_len);
521 memcpy(tmp + len1, entry->filename, len2);
522
523 entry->inode = (unsigned short) zend_hash_func(tmp, tmp_len);
524 }
525 /* }}} */
526
527 void phar_request_initialize(void);
528
529 void phar_object_init(void);
530 void phar_destroy_phar_data(phar_archive_data *phar);
531
532 int phar_open_entry_file(phar_archive_data *phar, phar_entry_info *entry, char **error);
533 int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip);
534 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);
535 int phar_open_or_create_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, zend_bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
536 int phar_create_or_parse_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, zend_bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
537 int phar_open_executed_filename(char *alias, size_t alias_len, char **error);
538 int phar_free_alias(phar_archive_data *phar, char *alias, size_t alias_len);
539 int phar_get_archive(phar_archive_data **archive, char *fname, size_t fname_len, char *alias, size_t alias_len, char **error);
540 int phar_open_parsed_phar(char *fname, size_t fname_len, char *alias, size_t alias_len, zend_bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
541 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);
542 int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, size_t *signature_length, char **error);
543
544 /* utility functions */
545 zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error);
546 char *phar_decompress_filter(phar_entry_info * entry, int return_unknown);
547 char *phar_compress_filter(phar_entry_info * entry, int return_unknown);
548
549 /* void phar_remove_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len); */
550 void phar_add_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len);
551 int phar_mount_entry(phar_archive_data *phar, char *filename, size_t filename_len, char *path, size_t path_len);
552 zend_string *phar_find_in_include_path(char *file, size_t file_len, phar_archive_data **pphar);
553 char *phar_fix_filepath(char *path, size_t *new_len, int use_cwd);
554 phar_entry_info * phar_open_jit(phar_archive_data *phar, phar_entry_info *entry, char **error);
555 void phar_parse_metadata_lazy(const char *buffer, phar_metadata_tracker *tracker, uint32_t zip_metadata_len, int persistent);
556 zend_bool phar_metadata_tracker_has_data(const phar_metadata_tracker* tracker, int persistent);
557 /* If this has data, free it and set all values to undefined. */
558 void phar_metadata_tracker_free(phar_metadata_tracker* val, int persistent);
559 void phar_metadata_tracker_copy(phar_metadata_tracker* dest, const phar_metadata_tracker *source, int persistent);
560 void phar_metadata_tracker_clone(phar_metadata_tracker* tracker);
561 void phar_metadata_tracker_try_ensure_has_serialized_data(phar_metadata_tracker* tracker, int persistent);
562 int phar_metadata_tracker_unserialize_or_copy(phar_metadata_tracker* tracker, zval *value, int persistent, HashTable *unserialize_options, const char* method_name);
563 void phar_release_entry_metadata(phar_entry_info *entry);
564 void phar_release_archive_metadata(phar_archive_data *phar);
565 void destroy_phar_manifest_entry(zval *zv);
566 int phar_seek_efp(phar_entry_info *entry, zend_off_t offset, int whence, zend_off_t position, int follow_links);
567 php_stream *phar_get_efp(phar_entry_info *entry, int follow_links);
568 int phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error);
569 int phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links);
570 phar_entry_info *phar_get_link_source(phar_entry_info *entry);
571 int phar_create_writeable_entry(phar_archive_data *phar, phar_entry_info *entry, char **error);
572 int phar_separate_entry_fp(phar_entry_info *entry, char **error);
573 int phar_open_archive_fp(phar_archive_data *phar);
574 int phar_copy_on_write(phar_archive_data **pphar);
575
576 /* tar functions in tar.c */
577 int phar_is_tar(char *buf, char *fname);
578 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);
579 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);
580 int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error);
581
582 /* zip functions in zip.c */
583 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);
584 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);
585 int phar_zip_flush(phar_archive_data *archive, char *user_stub, zend_long len, int defaultstub, char **error);
586
587 #ifdef PHAR_MAIN
588 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);
589 extern const php_stream_wrapper php_stream_phar_wrapper;
590 #else
591 extern HashTable cached_phars;
592 extern HashTable cached_alias;
593 #endif
594
595 int phar_archive_delref(phar_archive_data *phar);
596 int phar_entry_delref(phar_entry_data *idata);
597
598 phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, int security);
599 phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security);
600 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);
601 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);
602 int phar_flush(phar_archive_data *archive, char *user_stub, zend_long len, int convert, char **error);
603 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);
604 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);
605
606 typedef enum {
607 pcr_use_query,
608 pcr_is_ok,
609 pcr_err_double_slash,
610 pcr_err_up_dir,
611 pcr_err_curr_dir,
612 pcr_err_back_slash,
613 pcr_err_star,
614 pcr_err_illegal_char,
615 pcr_err_empty_entry
616 } phar_path_check_result;
617
618 phar_path_check_result phar_path_check(char **p, size_t *len, const char **error);
619
620 END_EXTERN_C()
621