2007-01-21 23:25:50 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| phar php single-file executable PHP extension |
|
|
|
|
+----------------------------------------------------------------------+
|
2018-01-02 12:57:58 +08:00
|
|
|
| Copyright (c) 2006-2018 The PHP Group |
|
2007-01-21 23:25:50 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_01.txt. |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Gregory Beaver <cellog@php.net> |
|
|
|
|
| Marcus Boerger <helly@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include "php.h"
|
2008-01-03 12:45:00 +08:00
|
|
|
#include "tar.h"
|
2007-01-21 23:25:50 +08:00
|
|
|
#include "php_ini.h"
|
|
|
|
#include "zend_constants.h"
|
|
|
|
#include "zend_execute.h"
|
|
|
|
#include "zend_exceptions.h"
|
|
|
|
#include "zend_hash.h"
|
|
|
|
#include "zend_interfaces.h"
|
|
|
|
#include "zend_operators.h"
|
2015-01-14 17:22:58 +08:00
|
|
|
#include "zend_sort.h"
|
2007-12-22 15:46:53 +08:00
|
|
|
#include "zend_vm.h"
|
2014-09-21 04:42:02 +08:00
|
|
|
#include "zend_smart_str.h"
|
2007-01-21 23:25:50 +08:00
|
|
|
#include "main/php_streams.h"
|
2007-12-19 01:01:24 +08:00
|
|
|
#include "main/streams/php_stream_plain_wrapper.h"
|
2007-12-24 05:12:42 +08:00
|
|
|
#include "main/SAPI.h"
|
|
|
|
#include "main/php_main.h"
|
2008-01-16 15:24:39 +08:00
|
|
|
#include "main/php_open_temporary_file.h"
|
2007-01-21 23:25:50 +08:00
|
|
|
#include "ext/standard/info.h"
|
2007-02-07 01:09:37 +08:00
|
|
|
#include "ext/standard/basic_functions.h"
|
2007-12-19 12:37:25 +08:00
|
|
|
#include "ext/standard/file.h"
|
2008-01-10 23:13:00 +08:00
|
|
|
#include "ext/standard/php_string.h"
|
2007-01-21 23:25:50 +08:00
|
|
|
#include "ext/standard/url.h"
|
|
|
|
#include "ext/standard/crc32.h"
|
|
|
|
#include "ext/standard/md5.h"
|
|
|
|
#include "ext/standard/sha1.h"
|
2007-01-22 04:12:50 +08:00
|
|
|
#include "ext/standard/php_var.h"
|
2008-01-18 13:42:16 +08:00
|
|
|
#include "ext/standard/php_versioning.h"
|
2007-12-25 05:40:54 +08:00
|
|
|
#ifndef PHP_WIN32
|
2007-12-21 07:12:40 +08:00
|
|
|
#include "TSRM/tsrm_strtok_r.h"
|
2007-12-25 05:40:54 +08:00
|
|
|
#endif
|
2013-10-19 01:02:19 +08:00
|
|
|
#include "Zend/zend_virtual_cwd.h"
|
2007-01-21 23:25:50 +08:00
|
|
|
#include "ext/spl/spl_array.h"
|
|
|
|
#include "ext/spl/spl_directory.h"
|
|
|
|
#include "ext/spl/spl_engine.h"
|
|
|
|
#include "ext/spl/spl_exceptions.h"
|
2007-02-09 06:50:15 +08:00
|
|
|
#include "ext/spl/spl_iterators.h"
|
2007-01-21 23:25:50 +08:00
|
|
|
#include "php_phar.h"
|
2008-10-09 08:51:27 +08:00
|
|
|
#ifdef PHAR_HASH_OK
|
2007-03-26 05:43:49 +08:00
|
|
|
#include "ext/hash/php_hash.h"
|
|
|
|
#include "ext/hash/php_hash_sha.h"
|
|
|
|
#endif
|
2007-01-21 23:25:50 +08:00
|
|
|
|
2008-03-23 05:54:15 +08:00
|
|
|
/* PHP_ because this is public information via MINFO */
|
|
|
|
#define PHP_PHAR_API_VERSION "1.1.1"
|
2007-01-21 23:25:50 +08:00
|
|
|
/* x.y.z maps to 0xyz0 */
|
2008-01-20 02:30:30 +08:00
|
|
|
#define PHAR_API_VERSION 0x1110
|
|
|
|
/* if we bump PHAR_API_VERSION, change this from 0x1100 to PHAR_API_VERSION */
|
|
|
|
#define PHAR_API_VERSION_NODIR 0x1100
|
|
|
|
#define PHAR_API_MIN_DIR 0x1110
|
2007-01-29 11:59:55 +08:00
|
|
|
#define PHAR_API_MIN_READ 0x1000
|
2007-01-28 11:59:30 +08:00
|
|
|
#define PHAR_API_MAJORVERSION 0x1000
|
2007-01-21 23:25:50 +08:00
|
|
|
#define PHAR_API_MAJORVER_MASK 0xF000
|
|
|
|
#define PHAR_API_VER_MASK 0xFFF0
|
|
|
|
|
|
|
|
#define PHAR_HDR_COMPRESSION_MASK 0x0000F000
|
|
|
|
#define PHAR_HDR_COMPRESSED_NONE 0x00000000
|
|
|
|
#define PHAR_HDR_COMPRESSED_GZ 0x00001000
|
|
|
|
#define PHAR_HDR_COMPRESSED_BZ2 0x00002000
|
|
|
|
#define PHAR_HDR_SIGNATURE 0x00010000
|
|
|
|
|
2008-01-09 15:09:04 +08:00
|
|
|
/* flags for defining that the entire file should be compressed */
|
|
|
|
#define PHAR_FILE_COMPRESSION_MASK 0x00F00000
|
|
|
|
#define PHAR_FILE_COMPRESSED_NONE 0x00000000
|
|
|
|
#define PHAR_FILE_COMPRESSED_GZ 0x00100000
|
|
|
|
#define PHAR_FILE_COMPRESSED_BZ2 0x00200000
|
|
|
|
|
2007-01-21 23:25:50 +08:00
|
|
|
#define PHAR_SIG_MD5 0x0001
|
|
|
|
#define PHAR_SIG_SHA1 0x0002
|
2007-03-26 05:43:49 +08:00
|
|
|
#define PHAR_SIG_SHA256 0x0003
|
|
|
|
#define PHAR_SIG_SHA512 0x0004
|
2008-08-01 21:45:06 +08:00
|
|
|
#define PHAR_SIG_OPENSSL 0x0010
|
2007-01-21 23:25:50 +08:00
|
|
|
|
|
|
|
/* flags byte for each file adheres to these bitmasks.
|
|
|
|
All unused values are reserved */
|
|
|
|
#define PHAR_ENT_COMPRESSION_MASK 0x0000F000
|
|
|
|
#define PHAR_ENT_COMPRESSED_NONE 0x00000000
|
|
|
|
#define PHAR_ENT_COMPRESSED_GZ 0x00001000
|
|
|
|
#define PHAR_ENT_COMPRESSED_BZ2 0x00002000
|
|
|
|
|
|
|
|
#define PHAR_ENT_PERM_MASK 0x000001FF
|
|
|
|
#define PHAR_ENT_PERM_MASK_USR 0x000001C0
|
|
|
|
#define PHAR_ENT_PERM_SHIFT_USR 6
|
|
|
|
#define PHAR_ENT_PERM_MASK_GRP 0x00000038
|
|
|
|
#define PHAR_ENT_PERM_SHIFT_GRP 3
|
|
|
|
#define PHAR_ENT_PERM_MASK_OTH 0x00000007
|
|
|
|
#define PHAR_ENT_PERM_DEF_FILE 0x000001B6
|
|
|
|
#define PHAR_ENT_PERM_DEF_DIR 0x000001FF
|
|
|
|
|
2008-04-24 02:44:43 +08:00
|
|
|
#define PHAR_FORMAT_SAME 0
|
|
|
|
#define PHAR_FORMAT_PHAR 1
|
|
|
|
#define PHAR_FORMAT_TAR 2
|
|
|
|
#define PHAR_FORMAT_ZIP 3
|
2008-04-11 12:20:15 +08:00
|
|
|
|
2008-01-09 06:14:16 +08:00
|
|
|
#define TAR_FILE '0'
|
|
|
|
#define TAR_LINK '1'
|
|
|
|
#define TAR_SYMLINK '2'
|
|
|
|
#define TAR_DIR '5'
|
|
|
|
#define TAR_NEW '8'
|
2015-03-30 19:09:32 +08:00
|
|
|
#define TAR_GLOBAL_HDR 'g'
|
|
|
|
#define TAR_FILE_HDR 'x'
|
2008-01-09 06:14:16 +08:00
|
|
|
|
2008-08-01 21:45:06 +08:00
|
|
|
#define PHAR_MUNG_PHP_SELF (1<<0)
|
|
|
|
#define PHAR_MUNG_REQUEST_URI (1<<1)
|
|
|
|
#define PHAR_MUNG_SCRIPT_NAME (1<<2)
|
|
|
|
#define PHAR_MUNG_SCRIPT_FILENAME (1<<3)
|
|
|
|
|
|
|
|
typedef struct _phar_entry_fp phar_entry_fp;
|
|
|
|
typedef struct _phar_archive_data phar_archive_data;
|
|
|
|
|
2007-01-21 23:25:50 +08:00
|
|
|
ZEND_BEGIN_MODULE_GLOBALS(phar)
|
2008-10-13 03:40:30 +08:00
|
|
|
/* a list of phar_archive_data objects that reference a cached phar, so
|
|
|
|
that if copy-on-write is performed, we can swap them out for the new value */
|
|
|
|
HashTable phar_persist_map;
|
2007-01-21 23:25:50 +08:00
|
|
|
HashTable phar_fname_map;
|
2008-08-01 21:45:06 +08:00
|
|
|
/* for cached phars, this is a per-process store of fp/ufp */
|
|
|
|
phar_entry_fp *cached_fp;
|
2007-01-21 23:25:50 +08:00
|
|
|
HashTable phar_alias_map;
|
2008-08-01 21:45:06 +08:00
|
|
|
int phar_SERVER_mung_list;
|
2007-01-21 23:25:50 +08:00
|
|
|
int readonly;
|
2008-08-01 21:45:06 +08:00
|
|
|
char* cache_list;
|
|
|
|
int manifest_cached;
|
|
|
|
int persist;
|
2008-05-18 04:07:38 +08:00
|
|
|
int has_zlib;
|
|
|
|
int has_bz2;
|
2007-01-21 23:25:50 +08:00
|
|
|
zend_bool readonly_orig;
|
|
|
|
zend_bool require_hash_orig;
|
2008-08-01 21:45:06 +08:00
|
|
|
zend_bool intercepted;
|
2007-05-17 07:16:51 +08:00
|
|
|
int request_init;
|
2007-03-26 03:03:38 +08:00
|
|
|
int require_hash;
|
2007-01-27 23:31:24 +08:00
|
|
|
int request_done;
|
2007-03-28 05:28:22 +08:00
|
|
|
int request_ends;
|
2017-06-08 21:52:39 +08:00
|
|
|
zif_handler orig_fopen;
|
|
|
|
zif_handler orig_file_get_contents;
|
|
|
|
zif_handler orig_is_file;
|
|
|
|
zif_handler orig_is_link;
|
|
|
|
zif_handler orig_is_dir;
|
|
|
|
zif_handler orig_opendir;
|
|
|
|
zif_handler orig_file_exists;
|
|
|
|
zif_handler orig_fileperms;
|
|
|
|
zif_handler orig_fileinode;
|
|
|
|
zif_handler orig_filesize;
|
|
|
|
zif_handler orig_fileowner;
|
|
|
|
zif_handler orig_filegroup;
|
|
|
|
zif_handler orig_fileatime;
|
|
|
|
zif_handler orig_filemtime;
|
|
|
|
zif_handler orig_filectime;
|
|
|
|
zif_handler orig_filetype;
|
|
|
|
zif_handler orig_is_writable;
|
|
|
|
zif_handler orig_is_readable;
|
|
|
|
zif_handler orig_is_executable;
|
|
|
|
zif_handler orig_lstat;
|
|
|
|
zif_handler orig_readfile;
|
|
|
|
zif_handler orig_stat;
|
2008-01-10 23:13:00 +08:00
|
|
|
/* used for includes with . in them inside front controller */
|
|
|
|
char* cwd;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t cwd_len;
|
2008-02-13 23:00:31 +08:00
|
|
|
int cwd_init;
|
2008-08-01 21:45:06 +08:00
|
|
|
char *openssl_privatekey;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t openssl_privatekey_len;
|
2008-08-01 21:45:06 +08:00
|
|
|
/* phar_get_archive cache */
|
|
|
|
char* last_phar_name;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t last_phar_name_len;
|
2008-08-01 21:45:06 +08:00
|
|
|
char* last_alias;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t last_alias_len;
|
2008-08-01 21:45:06 +08:00
|
|
|
phar_archive_data* last_phar;
|
|
|
|
HashTable mime_types;
|
2007-01-21 23:25:50 +08:00
|
|
|
ZEND_END_MODULE_GLOBALS(phar)
|
|
|
|
|
|
|
|
ZEND_EXTERN_MODULE_GLOBALS(phar)
|
2015-07-29 18:26:23 +08:00
|
|
|
#define PHAR_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(phar, v)
|
2007-01-21 23:25:50 +08:00
|
|
|
|
2015-07-29 18:26:23 +08:00
|
|
|
#if defined(ZTS) && defined(COMPILE_DL_PHAR)
|
2016-03-03 23:46:04 +08:00
|
|
|
ZEND_TSRMLS_CACHE_EXTERN()
|
2007-01-21 23:25:50 +08:00
|
|
|
#endif
|
|
|
|
|
2008-01-28 16:52:08 +08:00
|
|
|
#include "pharzip.h"
|
2007-01-21 23:25:50 +08:00
|
|
|
|
|
|
|
typedef union _phar_archive_object phar_archive_object;
|
|
|
|
typedef union _phar_entry_object phar_entry_object;
|
|
|
|
|
2008-01-28 16:52:08 +08:00
|
|
|
/*
|
|
|
|
* used in phar_entry_info->fp_type to
|
|
|
|
*/
|
|
|
|
enum phar_fp_type {
|
|
|
|
/* regular file pointer phar_archive_data->fp */
|
|
|
|
PHAR_FP,
|
|
|
|
/* uncompressed file pointer phar_archive_data->uncompressed_fp */
|
|
|
|
PHAR_UFP,
|
|
|
|
/* modified file pointer phar_entry_info->fp */
|
2008-02-15 13:01:40 +08:00
|
|
|
PHAR_MOD,
|
|
|
|
/* temporary manifest entry (file outside of the phar mapped to a location inside the phar)
|
|
|
|
this entry stores the stream to open in link (normally used for tars, but we steal it here) */
|
|
|
|
PHAR_TMP
|
2008-01-28 16:52:08 +08:00
|
|
|
};
|
|
|
|
|
2007-01-21 23:25:50 +08:00
|
|
|
/* entry for one file in a phar file */
|
|
|
|
typedef struct _phar_entry_info {
|
|
|
|
/* first bytes are exactly as in file */
|
2016-01-29 00:11:53 +08:00
|
|
|
uint32_t uncompressed_filesize;
|
|
|
|
uint32_t timestamp;
|
|
|
|
uint32_t compressed_filesize;
|
|
|
|
uint32_t crc32;
|
|
|
|
uint32_t flags;
|
2007-01-21 23:25:50 +08:00
|
|
|
/* remainder */
|
2007-08-17 12:47:50 +08:00
|
|
|
/* when changing compression, save old flags in case fp is NULL */
|
2016-01-29 00:11:53 +08:00
|
|
|
uint32_t old_flags;
|
2014-05-08 22:30:07 +08:00
|
|
|
zval metadata;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t metadata_len; /* only used for cached manifests */
|
2016-01-29 00:11:53 +08:00
|
|
|
uint32_t filename_len;
|
2007-01-21 23:25:50 +08:00
|
|
|
char *filename;
|
2008-01-28 16:52:08 +08:00
|
|
|
enum phar_fp_type fp_type;
|
|
|
|
/* offset within original phar file of the file contents */
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long offset_abs;
|
2008-01-28 16:52:08 +08:00
|
|
|
/* offset within fp of the file contents */
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long offset;
|
2008-01-28 16:52:08 +08:00
|
|
|
/* offset within original phar file of the file header (for zip-based/tar-based) */
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long header_offset;
|
2007-01-21 23:25:50 +08:00
|
|
|
php_stream *fp;
|
2007-01-27 23:31:24 +08:00
|
|
|
php_stream *cfp;
|
2007-01-26 22:52:10 +08:00
|
|
|
int fp_refcount;
|
2008-04-21 14:17:51 +08:00
|
|
|
char *tmp;
|
2007-01-22 08:13:20 +08:00
|
|
|
phar_archive_data *phar;
|
2007-05-15 03:14:00 +08:00
|
|
|
smart_str metadata_str;
|
2008-01-03 12:45:00 +08:00
|
|
|
char *link; /* symbolic link to another file */
|
|
|
|
char tar_type;
|
2008-08-01 21:45:06 +08:00
|
|
|
/* position in the manifest */
|
2016-11-26 22:18:42 +08:00
|
|
|
uint32_t manifest_pos;
|
2008-08-01 21:45:06 +08:00
|
|
|
/* for stat */
|
|
|
|
unsigned short inode;
|
2008-09-14 06:31:18 +08:00
|
|
|
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_crc_checked:1;
|
|
|
|
uint32_t is_modified:1;
|
|
|
|
uint32_t is_deleted:1;
|
|
|
|
uint32_t is_dir:1;
|
2008-09-14 06:31:18 +08:00
|
|
|
/* this flag is used for mounted entries (external files mapped to location
|
|
|
|
inside a phar */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_mounted:1;
|
2008-09-14 06:31:18 +08:00
|
|
|
/* used when iterating */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_temp_dir:1;
|
2008-09-14 06:31:18 +08:00
|
|
|
/* tar-based phar file stuff */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_tar:1;
|
2008-09-14 06:31:18 +08:00
|
|
|
/* zip-based phar file stuff */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_zip:1;
|
2008-09-14 06:31:18 +08:00
|
|
|
/* for cached phar entries */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_persistent:1;
|
2007-01-21 23:25:50 +08:00
|
|
|
} phar_entry_info;
|
|
|
|
|
|
|
|
/* information about a phar file (the archive itself) */
|
2007-01-22 08:13:20 +08:00
|
|
|
struct _phar_archive_data {
|
2007-01-21 23:25:50 +08:00
|
|
|
char *fname;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t fname_len;
|
2008-05-08 01:24:22 +08:00
|
|
|
/* for phar_detect_fname_ext, this stores the location of the file extension within fname */
|
|
|
|
char *ext;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t ext_len;
|
2007-01-21 23:25:50 +08:00
|
|
|
char *alias;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t alias_len;
|
2007-01-21 23:25:50 +08:00
|
|
|
char version[12];
|
|
|
|
size_t internal_file_start;
|
|
|
|
size_t halt_offset;
|
|
|
|
HashTable manifest;
|
2008-08-01 21:45:06 +08:00
|
|
|
/* hash of virtual directories, as in path/to/file.txt has path/to and path as virtual directories */
|
|
|
|
HashTable virtual_dirs;
|
2008-02-18 12:45:42 +08:00
|
|
|
/* hash of mounted directory paths */
|
|
|
|
HashTable mounted_dirs;
|
2016-01-29 00:11:53 +08:00
|
|
|
uint32_t flags;
|
|
|
|
uint32_t min_timestamp;
|
|
|
|
uint32_t max_timestamp;
|
2007-01-21 23:25:50 +08:00
|
|
|
php_stream *fp;
|
2008-01-28 16:52:08 +08:00
|
|
|
/* decompressed file contents are stored here */
|
|
|
|
php_stream *ufp;
|
2007-01-21 23:25:50 +08:00
|
|
|
int refcount;
|
2016-01-29 00:11:53 +08:00
|
|
|
uint32_t sig_flags;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t sig_len;
|
2007-01-21 23:25:50 +08:00
|
|
|
char *signature;
|
2014-05-08 22:30:07 +08:00
|
|
|
zval metadata;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t metadata_len; /* only used for cached manifests */
|
|
|
|
uint32_t phar_pos;
|
2008-02-11 14:46:44 +08:00
|
|
|
/* if 1, then this alias was manually specified by the user and is not a permanent alias */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_temporary_alias:1;
|
|
|
|
uint32_t is_modified:1;
|
|
|
|
uint32_t is_writeable:1;
|
|
|
|
uint32_t is_brandnew:1;
|
2007-01-26 22:52:10 +08:00
|
|
|
/* defer phar creation */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t donotflush:1;
|
2008-01-01 06:42:40 +08:00
|
|
|
/* zip-based phar variables */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_zip:1;
|
2008-01-03 12:45:00 +08:00
|
|
|
/* tar-based phar variables */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_tar:1;
|
2008-02-28 05:34:26 +08:00
|
|
|
/* PharData variables */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_data:1;
|
2008-08-01 21:45:06 +08:00
|
|
|
/* for cached phar manifests */
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t is_persistent:1;
|
2008-08-01 21:45:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _phar_entry_fp_info {
|
|
|
|
enum phar_fp_type fp_type;
|
|
|
|
/* offset within fp of the file contents */
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_long offset;
|
2008-08-01 21:45:06 +08:00
|
|
|
} phar_entry_fp_info;
|
|
|
|
|
|
|
|
struct _phar_entry_fp {
|
|
|
|
php_stream *fp;
|
|
|
|
php_stream *ufp;
|
|
|
|
phar_entry_fp_info *manifest;
|
2007-01-22 08:13:20 +08:00
|
|
|
};
|
2007-01-21 23:25:50 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline php_stream *phar_get_entrypfp(phar_entry_info *entry)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!entry->is_persistent) {
|
|
|
|
return entry->phar->fp;
|
|
|
|
}
|
2015-03-09 22:58:53 +08:00
|
|
|
return PHAR_G(cached_fp)[entry->phar->phar_pos].fp;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline php_stream *phar_get_entrypufp(phar_entry_info *entry)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!entry->is_persistent) {
|
|
|
|
return entry->phar->ufp;
|
|
|
|
}
|
2015-03-09 22:58:53 +08:00
|
|
|
return PHAR_G(cached_fp)[entry->phar->phar_pos].ufp;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline void phar_set_entrypfp(phar_entry_info *entry, php_stream *fp)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!entry->phar->is_persistent) {
|
|
|
|
entry->phar->fp = fp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:58:53 +08:00
|
|
|
PHAR_G(cached_fp)[entry->phar->phar_pos].fp = fp;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline void phar_set_entrypufp(phar_entry_info *entry, php_stream *fp)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!entry->phar->is_persistent) {
|
|
|
|
entry->phar->ufp = fp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:58:53 +08:00
|
|
|
PHAR_G(cached_fp)[entry->phar->phar_pos].ufp = fp;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline php_stream *phar_get_pharfp(phar_archive_data *phar)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!phar->is_persistent) {
|
|
|
|
return phar->fp;
|
|
|
|
}
|
2015-03-09 22:58:53 +08:00
|
|
|
return PHAR_G(cached_fp)[phar->phar_pos].fp;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline php_stream *phar_get_pharufp(phar_archive_data *phar)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!phar->is_persistent) {
|
|
|
|
return phar->ufp;
|
|
|
|
}
|
2015-03-09 22:58:53 +08:00
|
|
|
return PHAR_G(cached_fp)[phar->phar_pos].ufp;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline void phar_set_pharfp(phar_archive_data *phar, php_stream *fp)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!phar->is_persistent) {
|
|
|
|
phar->fp = fp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:58:53 +08:00
|
|
|
PHAR_G(cached_fp)[phar->phar_pos].fp = fp;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline void phar_set_pharufp(phar_archive_data *phar, php_stream *fp)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!phar->is_persistent) {
|
|
|
|
phar->ufp = fp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:58:53 +08:00
|
|
|
PHAR_G(cached_fp)[phar->phar_pos].ufp = fp;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline void phar_set_fp_type(phar_entry_info *entry, enum phar_fp_type type, zend_off_t offset)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
phar_entry_fp_info *data;
|
|
|
|
|
|
|
|
if (!entry->is_persistent) {
|
|
|
|
entry->fp_type = type;
|
|
|
|
entry->offset = offset;
|
|
|
|
return;
|
|
|
|
}
|
2015-03-09 22:58:53 +08:00
|
|
|
data = &(PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos]);
|
2008-08-01 21:45:06 +08:00
|
|
|
data->fp_type = type;
|
|
|
|
data->offset = offset;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline enum phar_fp_type phar_get_fp_type(phar_entry_info *entry)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!entry->is_persistent) {
|
|
|
|
return entry->fp_type;
|
|
|
|
}
|
2015-03-09 22:58:53 +08:00
|
|
|
return PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].fp_type;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline zend_off_t phar_get_fp_offset(phar_entry_info *entry)
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
if (!entry->is_persistent) {
|
|
|
|
return entry->offset;
|
|
|
|
}
|
2015-03-09 22:58:53 +08:00
|
|
|
if (PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].fp_type == PHAR_FP) {
|
|
|
|
if (!PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].offset) {
|
|
|
|
PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].offset = entry->offset;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-09 22:58:53 +08:00
|
|
|
return PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].offset;
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
|
2007-12-22 15:46:53 +08:00
|
|
|
#define PHAR_MIME_PHP '\0'
|
|
|
|
#define PHAR_MIME_PHPS '\1'
|
|
|
|
#define PHAR_MIME_OTHER '\2'
|
|
|
|
|
|
|
|
typedef struct _phar_mime_type {
|
|
|
|
char *mime;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t len;
|
2007-12-22 15:46:53 +08:00
|
|
|
/* one of PHAR_MIME_* */
|
|
|
|
char type;
|
|
|
|
} phar_mime_type;
|
2007-01-26 22:52:10 +08:00
|
|
|
|
2007-01-21 23:25:50 +08:00
|
|
|
/* stream access data for one file entry in a phar file */
|
|
|
|
typedef struct _phar_entry_data {
|
|
|
|
phar_archive_data *phar;
|
|
|
|
php_stream *fp;
|
2008-01-28 16:52:08 +08:00
|
|
|
/* stream position proxy, allows multiple open streams referring to the same fp */
|
2014-08-26 02:22:49 +08:00
|
|
|
zend_off_t position;
|
2007-08-30 10:30:16 +08:00
|
|
|
/* for copies of the phar fp, defines where 0 is */
|
2014-08-26 02:22:49 +08:00
|
|
|
zend_off_t zero;
|
2018-04-17 21:09:31 +08:00
|
|
|
uint32_t for_write:1;
|
|
|
|
uint32_t is_zip:1;
|
|
|
|
uint32_t is_tar:1;
|
2007-01-21 23:25:50 +08:00
|
|
|
phar_entry_info *internal_file;
|
|
|
|
} phar_entry_data;
|
|
|
|
|
|
|
|
/* archive php object */
|
|
|
|
union _phar_archive_object {
|
|
|
|
spl_filesystem_object spl;
|
2014-05-09 01:18:59 +08:00
|
|
|
phar_archive_data *archive;
|
2007-01-21 23:25:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* entry php object */
|
|
|
|
union _phar_entry_object {
|
|
|
|
spl_filesystem_object spl;
|
2014-05-09 01:18:59 +08:00
|
|
|
phar_entry_info *entry;
|
2007-01-21 23:25:50 +08:00
|
|
|
};
|
|
|
|
|
2008-04-14 12:47:34 +08:00
|
|
|
#ifndef PHAR_MAIN
|
2018-04-17 21:09:31 +08:00
|
|
|
extern zend_string *(*phar_save_resolve_path)(const char *filename, size_t filename_len);
|
2009-04-29 11:24:09 +08:00
|
|
|
#endif
|
|
|
|
|
2008-05-15 05:27:31 +08:00
|
|
|
BEGIN_EXTERN_C()
|
2008-02-02 06:23:37 +08:00
|
|
|
|
2008-03-02 08:42:29 +08:00
|
|
|
#ifdef PHP_WIN32
|
2007-12-25 05:40:54 +08:00
|
|
|
char *tsrm_strtok_r(char *s, const char *delim, char **last);
|
2008-01-28 22:39:17 +08:00
|
|
|
|
2018-04-17 21:09:31 +08:00
|
|
|
static inline void phar_unixify_path_separators(char *path, size_t path_len)
|
2008-01-28 22:39:17 +08:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
/* unixify win paths */
|
2018-04-17 21:09:31 +08:00
|
|
|
for (s = path; (size_t)(s - path) < path_len; ++s) {
|
2008-01-28 22:39:17 +08:00
|
|
|
if (*s == '\\') {
|
|
|
|
*s = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-25 05:40:54 +08:00
|
|
|
#endif
|
2008-03-24 09:33:30 +08:00
|
|
|
/**
|
|
|
|
* validate an alias, returns 1 for success, 0 for failure
|
|
|
|
*/
|
2018-04-17 21:09:31 +08:00
|
|
|
static inline int phar_validate_alias(const char *alias, size_t alias_len) /* {{{ */
|
2008-03-24 09:33:30 +08:00
|
|
|
{
|
|
|
|
return !(memchr(alias, '/', alias_len) || memchr(alias, '\\', alias_len) || memchr(alias, ':', alias_len) ||
|
2008-09-14 14:32:52 +08:00
|
|
|
memchr(alias, ';', alias_len) || memchr(alias, '\n', alias_len) || memchr(alias, '\r', alias_len));
|
2008-03-24 09:33:30 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static inline void phar_set_inode(phar_entry_info *entry) /* {{{ */
|
2008-08-01 21:45:06 +08:00
|
|
|
{
|
|
|
|
char tmp[MAXPATHLEN];
|
2018-04-17 21:09:31 +08:00
|
|
|
size_t tmp_len;
|
2015-07-27 08:31:12 +08:00
|
|
|
size_t len1, len2;
|
2008-08-01 21:45:06 +08:00
|
|
|
|
2015-04-14 15:03:50 +08:00
|
|
|
tmp_len = MIN(MAXPATHLEN, entry->filename_len + entry->phar->fname_len);
|
2015-07-27 08:31:12 +08:00
|
|
|
|
|
|
|
len1 = MIN(entry->phar->fname_len, tmp_len);
|
|
|
|
memcpy(tmp, entry->phar->fname, len1);
|
|
|
|
|
|
|
|
len2 = MIN(tmp_len - len1, entry->filename_len);
|
|
|
|
memcpy(tmp + len1, entry->filename, len2);
|
|
|
|
|
2015-08-05 07:14:24 +08:00
|
|
|
entry->inode = (unsigned short) zend_hash_func(tmp, tmp_len);
|
2008-08-01 21:45:06 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2007-01-21 23:25:50 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
void phar_request_initialize(void);
|
2007-05-17 07:16:51 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
void phar_object_init(void);
|
|
|
|
void phar_destroy_phar_data(phar_archive_data *phar);
|
2007-01-21 23:25:50 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
int phar_open_entry_file(phar_archive_data *phar, phar_entry_info *entry, char **error);
|
2016-01-29 00:11:53 +08:00
|
|
|
int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip);
|
2018-04-17 21:09:31 +08:00
|
|
|
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);
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
int phar_open_executed_filename(char *alias, size_t alias_len, char **error);
|
|
|
|
int phar_free_alias(phar_archive_data *phar, char *alias, size_t alias_len);
|
|
|
|
int phar_get_archive(phar_archive_data **archive, char *fname, size_t fname_len, char *alias, size_t alias_len, char **error);
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, size_t *signature_length, char **error);
|
2008-01-28 16:52:08 +08:00
|
|
|
|
|
|
|
/* utility functions */
|
2015-07-03 01:29:06 +08:00
|
|
|
zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error);
|
2008-03-24 06:42:45 +08:00
|
|
|
char *phar_decompress_filter(phar_entry_info * entry, int return_unknown);
|
|
|
|
char *phar_compress_filter(phar_entry_info * entry, int return_unknown);
|
2008-01-09 03:40:23 +08:00
|
|
|
|
2018-04-17 21:09:31 +08:00
|
|
|
/* void phar_remove_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len); */
|
|
|
|
void phar_add_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len);
|
|
|
|
int phar_mount_entry(phar_archive_data *phar, char *filename, size_t filename_len, char *path, size_t path_len);
|
|
|
|
zend_string *phar_find_in_include_path(char *file, size_t file_len, phar_archive_data **pphar);
|
|
|
|
char *phar_fix_filepath(char *path, size_t *new_len, int use_cwd);
|
2014-12-14 06:06:14 +08:00
|
|
|
phar_entry_info * phar_open_jit(phar_archive_data *phar, phar_entry_info *entry, char **error);
|
2016-01-29 00:11:53 +08:00
|
|
|
int phar_parse_metadata(char **buffer, zval *metadata, uint32_t zip_metadata_len);
|
2014-05-08 22:30:07 +08:00
|
|
|
void destroy_phar_manifest_entry(zval *zv);
|
2014-12-14 06:06:14 +08:00
|
|
|
int phar_seek_efp(phar_entry_info *entry, zend_off_t offset, int whence, zend_off_t position, int follow_links);
|
|
|
|
php_stream *phar_get_efp(phar_entry_info *entry, int follow_links);
|
|
|
|
int phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error);
|
|
|
|
int phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links);
|
|
|
|
phar_entry_info *phar_get_link_source(phar_entry_info *entry);
|
|
|
|
int phar_create_writeable_entry(phar_archive_data *phar, phar_entry_info *entry, char **error);
|
|
|
|
int phar_separate_entry_fp(phar_entry_info *entry, char **error);
|
|
|
|
int phar_open_archive_fp(phar_archive_data *phar);
|
|
|
|
int phar_copy_on_write(phar_archive_data **pphar);
|
2008-01-09 03:40:23 +08:00
|
|
|
|
|
|
|
/* tar functions in tar.c */
|
2008-04-21 01:28:54 +08:00
|
|
|
int phar_is_tar(char *buf, char *fname);
|
2018-04-17 21:09:31 +08:00
|
|
|
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);
|
|
|
|
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);
|
2014-12-14 06:06:14 +08:00
|
|
|
int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error);
|
2007-01-21 23:25:50 +08:00
|
|
|
|
2008-01-09 03:40:23 +08:00
|
|
|
/* zip functions in zip.c */
|
2018-04-17 21:09:31 +08:00
|
|
|
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);
|
|
|
|
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);
|
2014-12-14 06:06:14 +08:00
|
|
|
int phar_zip_flush(phar_archive_data *archive, char *user_stub, zend_long len, int defaultstub, char **error);
|
2008-01-09 03:40:23 +08:00
|
|
|
|
2007-12-19 01:01:24 +08:00
|
|
|
#ifdef PHAR_MAIN
|
2018-04-17 21:09:31 +08:00
|
|
|
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);
|
2017-12-15 03:46:22 +08:00
|
|
|
extern const php_stream_wrapper php_stream_phar_wrapper;
|
2008-08-01 21:45:06 +08:00
|
|
|
#else
|
|
|
|
extern HashTable cached_phars;
|
|
|
|
extern HashTable cached_alias;
|
2007-01-21 23:25:50 +08:00
|
|
|
#endif
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
int phar_archive_delref(phar_archive_data *phar);
|
|
|
|
int phar_entry_delref(phar_entry_data *idata);
|
2007-01-27 23:31:24 +08:00
|
|
|
|
2018-04-17 21:09:31 +08:00
|
|
|
phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, int security);
|
|
|
|
phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security);
|
|
|
|
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);
|
|
|
|
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);
|
2014-12-14 06:06:14 +08:00
|
|
|
int phar_flush(phar_archive_data *archive, char *user_stub, zend_long len, int convert, char **error);
|
2018-04-17 21:09:31 +08:00
|
|
|
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);
|
|
|
|
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);
|
2007-01-21 23:25:50 +08:00
|
|
|
|
2007-02-04 21:21:40 +08:00
|
|
|
typedef enum {
|
|
|
|
pcr_use_query,
|
|
|
|
pcr_is_ok,
|
|
|
|
pcr_err_double_slash,
|
|
|
|
pcr_err_up_dir,
|
|
|
|
pcr_err_curr_dir,
|
|
|
|
pcr_err_back_slash,
|
2007-02-05 00:25:25 +08:00
|
|
|
pcr_err_star,
|
2007-02-05 15:58:29 +08:00
|
|
|
pcr_err_illegal_char,
|
2007-02-04 21:21:40 +08:00
|
|
|
pcr_err_empty_entry
|
|
|
|
} phar_path_check_result;
|
|
|
|
|
2018-04-17 21:09:31 +08:00
|
|
|
phar_path_check_result phar_path_check(char **p, size_t *len, const char **error);
|
2007-02-04 21:21:40 +08:00
|
|
|
|
2007-01-21 23:25:50 +08:00
|
|
|
END_EXTERN_C()
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|