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