phar_internal.h 23 KB

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