Back to index

php5  5.3.10
phar_internal.h
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | phar php single-file executable PHP extension                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 2006-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt.                                 |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Authors: Gregory Beaver <cellog@php.net>                             |
00016   |          Marcus Boerger <helly@php.net>                              |
00017   +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: phar_internal.h 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 
00026 #include <time.h>
00027 #include "php.h"
00028 #include "tar.h"
00029 #include "php_ini.h"
00030 #include "zend_constants.h"
00031 #include "zend_execute.h"
00032 #include "zend_exceptions.h"
00033 #include "zend_hash.h"
00034 #include "zend_interfaces.h"
00035 #include "zend_operators.h"
00036 #include "zend_qsort.h"
00037 #include "zend_vm.h"
00038 #include "main/php_streams.h"
00039 #include "main/streams/php_stream_plain_wrapper.h"
00040 #include "main/SAPI.h"
00041 #include "main/php_main.h"
00042 #include "main/php_open_temporary_file.h"
00043 #include "ext/standard/info.h"
00044 #include "ext/standard/basic_functions.h"
00045 #include "ext/standard/file.h"
00046 #include "ext/standard/php_string.h"
00047 #include "ext/standard/url.h"
00048 #include "ext/standard/crc32.h"
00049 #include "ext/standard/md5.h"
00050 #include "ext/standard/sha1.h"
00051 #include "ext/standard/php_var.h"
00052 #include "ext/standard/php_smart_str.h"
00053 #include "ext/standard/php_versioning.h"
00054 #ifndef PHP_WIN32
00055 #include "TSRM/tsrm_strtok_r.h"
00056 #endif
00057 #include "TSRM/tsrm_virtual_cwd.h"
00058 #if HAVE_SPL
00059 #include "ext/spl/spl_array.h"
00060 #include "ext/spl/spl_directory.h"
00061 #include "ext/spl/spl_engine.h"
00062 #include "ext/spl/spl_exceptions.h"
00063 #include "ext/spl/spl_iterators.h"
00064 #endif
00065 #include "php_phar.h"
00066 #ifdef HAVE_STDINT_H
00067 #include <stdint.h>
00068 #endif
00069 #ifdef PHAR_HASH_OK
00070 #include "ext/hash/php_hash.h"
00071 #include "ext/hash/php_hash_sha.h"
00072 #endif
00073 
00074 #ifndef E_RECOVERABLE_ERROR
00075 # define E_RECOVERABLE_ERROR E_ERROR
00076 #endif
00077 
00078 #ifndef pestrndup
00079 # define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
00080 #endif
00081 
00082 #ifndef ALLOC_PERMANENT_ZVAL
00083 # define ALLOC_PERMANENT_ZVAL(z) \
00084        (z) = (zval*)malloc(sizeof(zval))
00085 #endif
00086 
00087 /* PHP_ because this is public information via MINFO */
00088 #define PHP_PHAR_API_VERSION      "1.1.1"
00089 /* x.y.z maps to 0xyz0 */
00090 #define PHAR_API_VERSION          0x1110
00091 /* if we bump PHAR_API_VERSION, change this from 0x1100 to PHAR_API_VERSION */
00092 #define PHAR_API_VERSION_NODIR    0x1100
00093 #define PHAR_API_MIN_DIR          0x1110
00094 #define PHAR_API_MIN_READ         0x1000
00095 #define PHAR_API_MAJORVERSION     0x1000
00096 #define PHAR_API_MAJORVER_MASK    0xF000
00097 #define PHAR_API_VER_MASK         0xFFF0
00098 
00099 #define PHAR_HDR_COMPRESSION_MASK 0x0000F000
00100 #define PHAR_HDR_COMPRESSED_NONE  0x00000000
00101 #define PHAR_HDR_COMPRESSED_GZ    0x00001000
00102 #define PHAR_HDR_COMPRESSED_BZ2   0x00002000
00103 #define PHAR_HDR_SIGNATURE        0x00010000
00104 
00105 /* flags for defining that the entire file should be compressed */
00106 #define PHAR_FILE_COMPRESSION_MASK 0x00F00000
00107 #define PHAR_FILE_COMPRESSED_NONE  0x00000000
00108 #define PHAR_FILE_COMPRESSED_GZ    0x00100000
00109 #define PHAR_FILE_COMPRESSED_BZ2   0x00200000
00110 
00111 #define PHAR_SIG_MD5              0x0001
00112 #define PHAR_SIG_SHA1             0x0002
00113 #define PHAR_SIG_SHA256           0x0003
00114 #define PHAR_SIG_SHA512           0x0004
00115 #define PHAR_SIG_OPENSSL          0x0010
00116 
00117 /* flags byte for each file adheres to these bitmasks.
00118    All unused values are reserved */
00119 #define PHAR_ENT_COMPRESSION_MASK 0x0000F000
00120 #define PHAR_ENT_COMPRESSED_NONE  0x00000000
00121 #define PHAR_ENT_COMPRESSED_GZ    0x00001000
00122 #define PHAR_ENT_COMPRESSED_BZ2   0x00002000
00123 
00124 #define PHAR_ENT_PERM_MASK        0x000001FF
00125 #define PHAR_ENT_PERM_MASK_USR    0x000001C0
00126 #define PHAR_ENT_PERM_SHIFT_USR   6
00127 #define PHAR_ENT_PERM_MASK_GRP    0x00000038
00128 #define PHAR_ENT_PERM_SHIFT_GRP   3
00129 #define PHAR_ENT_PERM_MASK_OTH    0x00000007
00130 #define PHAR_ENT_PERM_DEF_FILE    0x000001B6
00131 #define PHAR_ENT_PERM_DEF_DIR     0x000001FF
00132 
00133 #define PHAR_FORMAT_SAME    0
00134 #define PHAR_FORMAT_PHAR    1
00135 #define PHAR_FORMAT_TAR     2
00136 #define PHAR_FORMAT_ZIP     3
00137 
00138 #define TAR_FILE    '0'
00139 #define TAR_LINK    '1'
00140 #define TAR_SYMLINK '2'
00141 #define TAR_DIR     '5'
00142 #define TAR_NEW     '8'
00143 
00144 #define PHAR_MUNG_PHP_SELF                (1<<0)
00145 #define PHAR_MUNG_REQUEST_URI             (1<<1)
00146 #define PHAR_MUNG_SCRIPT_NAME             (1<<2)
00147 #define PHAR_MUNG_SCRIPT_FILENAME  (1<<3)
00148 
00149 typedef struct _phar_entry_fp phar_entry_fp;
00150 typedef struct _phar_archive_data phar_archive_data;
00151 
00152 ZEND_BEGIN_MODULE_GLOBALS(phar)
00153        /* a list of phar_archive_data objects that reference a cached phar, so
00154           that if copy-on-write is performed, we can swap them out for the new value */
00155        HashTable   phar_persist_map;
00156        HashTable   phar_fname_map;
00157        /* for cached phars, this is a per-process store of fp/ufp */
00158        phar_entry_fp *cached_fp;
00159        HashTable   phar_alias_map;
00160        int         phar_SERVER_mung_list;
00161        int         readonly;
00162        char*       cache_list;
00163        int         manifest_cached;
00164        int         persist;
00165        int         has_zlib;
00166        int         has_bz2;
00167        zend_bool   readonly_orig;
00168        zend_bool   require_hash_orig;
00169        zend_bool   intercepted;
00170        int         request_init;
00171        int         require_hash;
00172        int         request_done;
00173        int         request_ends;
00174        void        (*orig_fopen)(INTERNAL_FUNCTION_PARAMETERS);
00175        void        (*orig_file_get_contents)(INTERNAL_FUNCTION_PARAMETERS);
00176        void        (*orig_is_file)(INTERNAL_FUNCTION_PARAMETERS);
00177        void        (*orig_is_link)(INTERNAL_FUNCTION_PARAMETERS);
00178        void        (*orig_is_dir)(INTERNAL_FUNCTION_PARAMETERS);
00179        void        (*orig_opendir)(INTERNAL_FUNCTION_PARAMETERS);
00180        void        (*orig_file_exists)(INTERNAL_FUNCTION_PARAMETERS);
00181        void        (*orig_fileperms)(INTERNAL_FUNCTION_PARAMETERS);
00182        void        (*orig_fileinode)(INTERNAL_FUNCTION_PARAMETERS);
00183        void        (*orig_filesize)(INTERNAL_FUNCTION_PARAMETERS);
00184        void        (*orig_fileowner)(INTERNAL_FUNCTION_PARAMETERS);
00185        void        (*orig_filegroup)(INTERNAL_FUNCTION_PARAMETERS);
00186        void        (*orig_fileatime)(INTERNAL_FUNCTION_PARAMETERS);
00187        void        (*orig_filemtime)(INTERNAL_FUNCTION_PARAMETERS);
00188        void        (*orig_filectime)(INTERNAL_FUNCTION_PARAMETERS);
00189        void        (*orig_filetype)(INTERNAL_FUNCTION_PARAMETERS);
00190        void        (*orig_is_writable)(INTERNAL_FUNCTION_PARAMETERS);
00191        void        (*orig_is_readable)(INTERNAL_FUNCTION_PARAMETERS);
00192        void        (*orig_is_executable)(INTERNAL_FUNCTION_PARAMETERS);
00193        void        (*orig_lstat)(INTERNAL_FUNCTION_PARAMETERS);
00194        void        (*orig_readfile)(INTERNAL_FUNCTION_PARAMETERS);
00195        void        (*orig_stat)(INTERNAL_FUNCTION_PARAMETERS);
00196        /* used for includes with . in them inside front controller */
00197        char*       cwd;
00198        int         cwd_len;
00199        int         cwd_init;
00200        char        *openssl_privatekey;
00201        int         openssl_privatekey_len;
00202        /* phar_get_archive cache */
00203        char*       last_phar_name;
00204        int         last_phar_name_len;
00205        char*       last_alias;
00206        int         last_alias_len;
00207        phar_archive_data* last_phar;
00208        HashTable mime_types;
00209 ZEND_END_MODULE_GLOBALS(phar)
00210 
00211 ZEND_EXTERN_MODULE_GLOBALS(phar)
00212 
00213 #ifdef ZTS
00214 #      include "TSRM.h"
00215 #      define PHAR_G(v) TSRMG(phar_globals_id, zend_phar_globals *, v)
00216 #      define PHAR_GLOBALS ((zend_phar_globals *) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(phar_globals_id)])
00217 #else
00218 #      define PHAR_G(v) (phar_globals.v)
00219 #      define PHAR_GLOBALS (&phar_globals)
00220 #endif
00221 
00222 #ifndef php_uint16
00223 # if SIZEOF_SHORT == 2
00224 #  define php_uint16 unsigned short
00225 # else
00226 #  define php_uint16 uint16_t
00227 # endif
00228 #endif
00229 #include "pharzip.h"
00230 
00231 #if HAVE_SPL
00232 typedef union _phar_archive_object  phar_archive_object;
00233 typedef union _phar_entry_object    phar_entry_object;
00234 #endif
00235 
00236 /*
00237  * used in phar_entry_info->fp_type to
00238  */
00239 enum phar_fp_type {
00240        /* regular file pointer phar_archive_data->fp */
00241        PHAR_FP,
00242        /* uncompressed file pointer phar_archive_data->uncompressed_fp */
00243        PHAR_UFP,
00244        /* modified file pointer phar_entry_info->fp */
00245        PHAR_MOD,
00246        /* temporary manifest entry (file outside of the phar mapped to a location inside the phar)
00247           this entry stores the stream to open in link (normally used for tars, but we steal it here) */
00248        PHAR_TMP
00249 };
00250 
00251 /* entry for one file in a phar file */
00252 typedef struct _phar_entry_info {
00253        /* first bytes are exactly as in file */
00254        php_uint32               uncompressed_filesize;
00255        php_uint32               timestamp;
00256        php_uint32               compressed_filesize;
00257        php_uint32               crc32;
00258        php_uint32               flags;
00259        /* remainder */
00260        /* when changing compression, save old flags in case fp is NULL */
00261        php_uint32               old_flags;
00262        zval                     *metadata;
00263        int                      metadata_len; /* only used for cached manifests */
00264        php_uint32               filename_len;
00265        char                     *filename;
00266        enum phar_fp_type        fp_type;
00267        /* offset within original phar file of the file contents */
00268        long                     offset_abs;
00269        /* offset within fp of the file contents */
00270        long                     offset;
00271        /* offset within original phar file of the file header (for zip-based/tar-based) */
00272        long                     header_offset;
00273        php_stream               *fp;
00274        php_stream               *cfp;
00275        int                      fp_refcount;
00276        char                     *tmp;
00277        phar_archive_data        *phar;
00278        smart_str                metadata_str;
00279        char                     *link; /* symbolic link to another file */
00280        char                     tar_type;
00281        /* position in the manifest */
00282        uint                     manifest_pos;
00283        /* for stat */
00284        unsigned short           inode;
00285 
00286        unsigned int             is_crc_checked:1;
00287        unsigned int             is_modified:1;
00288        unsigned int             is_deleted:1;
00289        unsigned int             is_dir:1;
00290        /* this flag is used for mounted entries (external files mapped to location
00291           inside a phar */
00292        unsigned int             is_mounted:1;
00293        /* used when iterating */
00294        unsigned int             is_temp_dir:1;
00295        /* tar-based phar file stuff */
00296        unsigned int             is_tar:1;
00297        /* zip-based phar file stuff */
00298        unsigned int             is_zip:1;
00299        /* for cached phar entries */
00300        unsigned int             is_persistent:1;
00301 } phar_entry_info;
00302 
00303 /* information about a phar file (the archive itself) */
00304 struct _phar_archive_data {
00305        char                     *fname;
00306        int                      fname_len;
00307        /* for phar_detect_fname_ext, this stores the location of the file extension within fname */
00308        char                     *ext;
00309        int                      ext_len;
00310        char                     *alias;
00311        int                      alias_len;
00312        char                     version[12];
00313        size_t                   internal_file_start;
00314        size_t                   halt_offset;
00315        HashTable                manifest;
00316        /* hash of virtual directories, as in path/to/file.txt has path/to and path as virtual directories */
00317        HashTable                virtual_dirs;
00318        /* hash of mounted directory paths */
00319        HashTable                mounted_dirs;
00320        php_uint32               flags;
00321        php_uint32               min_timestamp;
00322        php_uint32               max_timestamp;
00323        php_stream               *fp;
00324        /* decompressed file contents are stored here */
00325        php_stream               *ufp;
00326        int                      refcount;
00327        php_uint32               sig_flags;
00328        int                      sig_len;
00329        char                     *signature;
00330        zval                     *metadata;
00331        int                      metadata_len; /* only used for cached manifests */
00332        uint                     phar_pos;
00333        /* if 1, then this alias was manually specified by the user and is not a permanent alias */
00334        unsigned int             is_temporary_alias:1;
00335        unsigned int             is_modified:1;
00336        unsigned int             is_writeable:1;
00337        unsigned int             is_brandnew:1;
00338        /* defer phar creation */
00339        unsigned int             donotflush:1;
00340        /* zip-based phar variables */
00341        unsigned int             is_zip:1;
00342        /* tar-based phar variables */
00343        unsigned int             is_tar:1;
00344        /* PharData variables       */
00345        unsigned int             is_data:1;
00346        /* for cached phar manifests */
00347        unsigned int             is_persistent:1;
00348 };
00349 
00350 typedef struct _phar_entry_fp_info {
00351        enum phar_fp_type        fp_type;
00352        /* offset within fp of the file contents */
00353        long                     offset;
00354 } phar_entry_fp_info;
00355 
00356 struct _phar_entry_fp {
00357        php_stream *fp;
00358        php_stream *ufp;
00359        phar_entry_fp_info *manifest;
00360 };
00361 
00362 static inline php_stream *phar_get_entrypfp(phar_entry_info *entry TSRMLS_DC)
00363 {
00364        if (!entry->is_persistent) {
00365               return entry->phar->fp;
00366        }
00367        return PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].fp;
00368 }
00369 
00370 static inline php_stream *phar_get_entrypufp(phar_entry_info *entry TSRMLS_DC)
00371 {
00372        if (!entry->is_persistent) {
00373               return entry->phar->ufp;
00374        }
00375        return PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].ufp;
00376 }
00377 
00378 static inline void phar_set_entrypfp(phar_entry_info *entry, php_stream *fp TSRMLS_DC)
00379 {
00380        if (!entry->phar->is_persistent) {
00381               entry->phar->fp =  fp;
00382               return;
00383        }
00384 
00385        PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].fp = fp;
00386 }
00387 
00388 static inline void phar_set_entrypufp(phar_entry_info *entry, php_stream *fp TSRMLS_DC)
00389 {
00390        if (!entry->phar->is_persistent) {
00391               entry->phar->ufp =  fp;
00392               return;
00393        }
00394 
00395        PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].ufp = fp;
00396 }
00397 
00398 static inline php_stream *phar_get_pharfp(phar_archive_data *phar TSRMLS_DC)
00399 {
00400        if (!phar->is_persistent) {
00401               return phar->fp;
00402        }
00403        return PHAR_GLOBALS->cached_fp[phar->phar_pos].fp;
00404 }
00405 
00406 static inline php_stream *phar_get_pharufp(phar_archive_data *phar TSRMLS_DC)
00407 {
00408        if (!phar->is_persistent) {
00409               return phar->ufp;
00410        }
00411        return PHAR_GLOBALS->cached_fp[phar->phar_pos].ufp;
00412 }
00413 
00414 static inline void phar_set_pharfp(phar_archive_data *phar, php_stream *fp TSRMLS_DC)
00415 {
00416        if (!phar->is_persistent) {
00417               phar->fp =  fp;
00418               return;
00419        }
00420 
00421        PHAR_GLOBALS->cached_fp[phar->phar_pos].fp = fp;
00422 }
00423 
00424 static inline void phar_set_pharufp(phar_archive_data *phar, php_stream *fp TSRMLS_DC)
00425 {
00426        if (!phar->is_persistent) {
00427               phar->ufp =  fp;
00428               return;
00429        }
00430 
00431        PHAR_GLOBALS->cached_fp[phar->phar_pos].ufp = fp;
00432 }
00433 
00434 static inline void phar_set_fp_type(phar_entry_info *entry, enum phar_fp_type type, off_t offset TSRMLS_DC)
00435 {
00436        phar_entry_fp_info *data;
00437 
00438        if (!entry->is_persistent) {
00439               entry->fp_type = type;
00440               entry->offset = offset;
00441               return;
00442        }
00443        data = &(PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].manifest[entry->manifest_pos]);
00444        data->fp_type = type;
00445        data->offset = offset;
00446 }
00447 
00448 static inline enum phar_fp_type phar_get_fp_type(phar_entry_info *entry TSRMLS_DC)
00449 {
00450        if (!entry->is_persistent) {
00451               return entry->fp_type;
00452        }
00453        return PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].manifest[entry->manifest_pos].fp_type;
00454 }
00455 
00456 static inline off_t phar_get_fp_offset(phar_entry_info *entry TSRMLS_DC)
00457 {
00458        if (!entry->is_persistent) {
00459               return entry->offset;
00460        }
00461        if (PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].manifest[entry->manifest_pos].fp_type == PHAR_FP) {
00462               if (!PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].manifest[entry->manifest_pos].offset) {
00463                      PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].manifest[entry->manifest_pos].offset = entry->offset;
00464               }
00465        }
00466        return PHAR_GLOBALS->cached_fp[entry->phar->phar_pos].manifest[entry->manifest_pos].offset;
00467 }
00468 
00469 #define PHAR_MIME_PHP '\0'
00470 #define PHAR_MIME_PHPS '\1'
00471 #define PHAR_MIME_OTHER '\2'
00472 
00473 typedef struct _phar_mime_type {
00474        char *mime;
00475        int len;
00476        /* one of PHAR_MIME_* */
00477        char type;
00478 } phar_mime_type;
00479 
00480 /* stream access data for one file entry in a phar file */
00481 typedef struct _phar_entry_data {
00482        phar_archive_data        *phar;
00483        php_stream               *fp;
00484        /* stream position proxy, allows multiple open streams referring to the same fp */
00485        off_t                    position;
00486        /* for copies of the phar fp, defines where 0 is */
00487        off_t                    zero;
00488        int                      for_write:1;
00489        int                      is_zip:1;
00490        int                      is_tar:1;
00491        phar_entry_info          *internal_file;
00492 } phar_entry_data;
00493 
00494 #if HAVE_SPL
00495 /* archive php object */
00496 union _phar_archive_object {
00497        zend_object              std;
00498        spl_filesystem_object    spl;
00499        struct {
00500               zend_object          std;
00501               phar_archive_data    *archive;
00502        } arc;
00503 };
00504 #endif
00505 
00506 #if HAVE_SPL
00507 /* entry php object */
00508 union _phar_entry_object {
00509        zend_object              std;
00510        spl_filesystem_object    spl;
00511        struct {
00512               zend_object          std;
00513               phar_entry_info      *entry;
00514        } ent;
00515 };
00516 #endif
00517 
00518 #ifndef PHAR_MAIN
00519 # if PHP_VERSION_ID >= 50300
00520 extern char *(*phar_save_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
00521 # endif
00522 #endif
00523 
00524 #if PHP_VERSION_ID < 50209
00525 static inline size_t phar_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen, size_t *len STREAMS_DC TSRMLS_DC)
00526 {
00527        size_t ret = php_stream_copy_to_stream(src, dest, maxlen);
00528        if (len) {
00529               *len = ret;
00530        }
00531        if (ret) {
00532               return SUCCESS;
00533        }
00534        return FAILURE;
00535 }
00536 #else
00537 # define phar_stream_copy_to_stream(src, dest, maxlen, len)    _php_stream_copy_to_stream_ex((src), (dest), (maxlen), (len) STREAMS_CC TSRMLS_CC)
00538 
00539 #endif
00540 
00541 #if PHP_VERSION_ID >= 60000
00542 typedef zstr phar_zstr;
00543 #define PHAR_STR(a, b)      \
00544        spprintf(&b, 0, "%s", a.s);
00545 #define PHAR_ZSTR(a, b)     \
00546        b = ZSTR(a);
00547 #define PHAR_STR_FREE(a) \
00548        efree(a);
00549 static inline int phar_make_unicode(zstr *c_var, char *arKey, uint nKeyLength TSRMLS_DC)
00550 {
00551        int c_var_len;
00552        UConverter *conv = ZEND_U_CONVERTER(UG(runtime_encoding_conv));
00553 
00554        c_var->u = NULL;
00555        if (zend_string_to_unicode(conv, &c_var->u, &c_var_len, arKey, nKeyLength TSRMLS_CC) == FAILURE) {
00556 
00557               if (c_var->u) {
00558                      efree(c_var->u);
00559               }
00560               return 0;
00561 
00562        }
00563        return c_var_len;
00564 }
00565 static inline int phar_find_key(HashTable *_SERVER, char *key, int len, void **stuff TSRMLS_DC)
00566 {
00567        if (SUCCESS == zend_hash_find(_SERVER, key, len, stuff)) {
00568               return 1;
00569        } else {
00570               int s = len;
00571               zstr var;
00572               s = phar_make_unicode(&var, key, len TSRMLS_CC);
00573               if (SUCCESS == zend_u_hash_find(_SERVER, IS_UNICODE, var, s, stuff)) {
00574                      efree(var.u);
00575                      return 1;
00576               }
00577               efree(var.u);
00578               return 0;
00579        }
00580 }
00581 #else
00582 typedef char *phar_zstr;
00583 #define PHAR_STR(a, b)      \
00584        b = a;
00585 #define PHAR_ZSTR(a, b)     \
00586        b = a;
00587 #define PHAR_STR_FREE(a)
00588 #endif
00589 
00590 BEGIN_EXTERN_C()
00591 
00592 #ifdef PHP_WIN32
00593 char *tsrm_strtok_r(char *s, const char *delim, char **last);
00594 
00595 static inline void phar_unixify_path_separators(char *path, int path_len)
00596 {
00597        char *s;
00598 
00599        /* unixify win paths */
00600        for (s = path; s - path < path_len; ++s) {
00601               if (*s == '\\') {
00602                      *s = '/';
00603               }
00604        }
00605 }
00606 #endif
00607 
00610 static inline int phar_validate_alias(const char *alias, int alias_len) /* {{{ */
00611 {
00612        return !(memchr(alias, '/', alias_len) || memchr(alias, '\\', alias_len) || memchr(alias, ':', alias_len) ||
00613               memchr(alias, ';', alias_len) || memchr(alias, '\n', alias_len) || memchr(alias, '\r', alias_len));
00614 }
00615 /* }}} */
00616 
00617 static inline void phar_set_inode(phar_entry_info *entry TSRMLS_DC) /* {{{ */
00618 {
00619        char tmp[MAXPATHLEN];
00620        int tmp_len;
00621 
00622        tmp_len = entry->filename_len + entry->phar->fname_len;
00623        memcpy(tmp, entry->phar->fname, entry->phar->fname_len);
00624        memcpy(tmp + entry->phar->fname_len, entry->filename, entry->filename_len);
00625        entry->inode = (unsigned short)zend_get_hash_value(tmp, tmp_len);
00626 }
00627 /* }}} */
00628 
00629 void phar_request_initialize(TSRMLS_D);
00630 
00631 void phar_object_init(TSRMLS_D);
00632 void phar_destroy_phar_data(phar_archive_data *phar TSRMLS_DC);
00633 
00634 int phar_open_entry_file(phar_archive_data *phar, phar_entry_info *entry, char **error TSRMLS_DC);
00635 int phar_postprocess_file(phar_entry_data *idata, php_uint32 crc32, char **error, int process_zip TSRMLS_DC);
00636 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);
00637 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);
00638 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);
00639 int phar_open_executed_filename(char *alias, int alias_len, char **error TSRMLS_DC);
00640 int phar_free_alias(phar_archive_data *phar, char *alias, int alias_len TSRMLS_DC);
00641 int phar_get_archive(phar_archive_data **archive, char *fname, int fname_len, char *alias, int alias_len, char **error TSRMLS_DC);
00642 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);
00643 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);
00644 int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, int *signature_length, char **error TSRMLS_DC);
00645 
00646 /* utility functions */
00647 char *phar_create_default_stub(const char *index_php, const char *web_index, size_t *len, char **error TSRMLS_DC);
00648 char *phar_decompress_filter(phar_entry_info * entry, int return_unknown);
00649 char *phar_compress_filter(phar_entry_info * entry, int return_unknown);
00650 
00651 void phar_remove_virtual_dirs(phar_archive_data *phar, char *filename, int filename_len TSRMLS_DC);
00652 void phar_add_virtual_dirs(phar_archive_data *phar, char *filename, int filename_len TSRMLS_DC);
00653 int phar_mount_entry(phar_archive_data *phar, char *filename, int filename_len, char *path, int path_len TSRMLS_DC);
00654 char *phar_find_in_include_path(char *file, int file_len, phar_archive_data **pphar TSRMLS_DC);
00655 char *phar_fix_filepath(char *path, int *new_len, int use_cwd TSRMLS_DC);
00656 phar_entry_info * phar_open_jit(phar_archive_data *phar, phar_entry_info *entry, char **error TSRMLS_DC);
00657 int phar_parse_metadata(char **buffer, zval **metadata, int zip_metadata_len TSRMLS_DC);
00658 void destroy_phar_manifest_entry(void *pDest);
00659 int phar_seek_efp(phar_entry_info *entry, off_t offset, int whence, off_t position, int follow_links TSRMLS_DC);
00660 php_stream *phar_get_efp(phar_entry_info *entry, int follow_links TSRMLS_DC);
00661 int phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error TSRMLS_DC);
00662 int phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links TSRMLS_DC);
00663 phar_entry_info *phar_get_link_source(phar_entry_info *entry TSRMLS_DC);
00664 int phar_create_writeable_entry(phar_archive_data *phar, phar_entry_info *entry, char **error TSRMLS_DC);
00665 int phar_separate_entry_fp(phar_entry_info *entry, char **error TSRMLS_DC);
00666 int phar_open_archive_fp(phar_archive_data *phar TSRMLS_DC);
00667 int phar_copy_on_write(phar_archive_data **pphar TSRMLS_DC);
00668 
00669 /* tar functions in tar.c */
00670 int phar_is_tar(char *buf, char *fname);
00671 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);
00672 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);
00673 int phar_tar_flush(phar_archive_data *phar, char *user_stub, long len, int defaultstub, char **error TSRMLS_DC);
00674 
00675 /* zip functions in zip.c */
00676 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);
00677 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);
00678 int phar_zip_flush(phar_archive_data *archive, char *user_stub, long len, int defaultstub, char **error TSRMLS_DC);
00679 
00680 #ifdef PHAR_MAIN
00681 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);
00682 extern php_stream_wrapper php_stream_phar_wrapper;
00683 #else
00684 extern HashTable cached_phars;
00685 extern HashTable cached_alias;
00686 #endif
00687 
00688 int phar_archive_delref(phar_archive_data *phar TSRMLS_DC);
00689 int phar_entry_delref(phar_entry_data *idata TSRMLS_DC);
00690 
00691 phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, int path_len, char **error, int security TSRMLS_DC);
00692 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);
00693 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);
00694 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);
00695 int phar_flush(phar_archive_data *archive, char *user_stub, long len, int convert, char **error TSRMLS_DC);
00696 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);
00697 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);
00698 
00699 typedef enum {
00700        pcr_use_query,
00701        pcr_is_ok,
00702        pcr_err_double_slash,
00703        pcr_err_up_dir,
00704        pcr_err_curr_dir,
00705        pcr_err_back_slash,
00706        pcr_err_star,
00707        pcr_err_illegal_char,
00708        pcr_err_empty_entry
00709 } phar_path_check_result;
00710 
00711 phar_path_check_result phar_path_check(char **p, int *len, const char **error);
00712 
00713 END_EXTERN_C()
00714 
00715 /*
00716  * Local variables:
00717  * tab-width: 4
00718  * c-basic-offset: 4
00719  * End:
00720  * vim600: noet sw=4 ts=4 fdm=marker
00721  * vim<600: noet sw=4 ts=4
00722  */