PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/pcre - php_pcre.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 772 843 91.6 %
Date: 2015-03-28 Functions: 31 32 96.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Andrei Zmievski <andrei@php.net>                             |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "php.h"
      22             : #include "php_ini.h"
      23             : #include "php_globals.h"
      24             : #include "php_pcre.h"
      25             : #include "ext/standard/info.h"
      26             : #include "ext/standard/basic_functions.h"
      27             : #include "zend_smart_str.h"
      28             : 
      29             : #if HAVE_PCRE || HAVE_BUNDLED_PCRE
      30             : 
      31             : #include "ext/standard/php_string.h"
      32             : 
      33             : #define PREG_PATTERN_ORDER                      1
      34             : #define PREG_SET_ORDER                          2
      35             : #define PREG_OFFSET_CAPTURE                     (1<<8)
      36             : 
      37             : #define PREG_SPLIT_NO_EMPTY                     (1<<0)
      38             : #define PREG_SPLIT_DELIM_CAPTURE        (1<<1)
      39             : #define PREG_SPLIT_OFFSET_CAPTURE       (1<<2)
      40             : 
      41             : #define PREG_REPLACE_EVAL                       (1<<0)
      42             : 
      43             : #define PREG_GREP_INVERT                        (1<<0)
      44             : 
      45             : #define PCRE_CACHE_SIZE 4096
      46             : 
      47             : enum {
      48             :         PHP_PCRE_NO_ERROR = 0,
      49             :         PHP_PCRE_INTERNAL_ERROR,
      50             :         PHP_PCRE_BACKTRACK_LIMIT_ERROR,
      51             :         PHP_PCRE_RECURSION_LIMIT_ERROR,
      52             :         PHP_PCRE_BAD_UTF8_ERROR,
      53             :         PHP_PCRE_BAD_UTF8_OFFSET_ERROR
      54             : };
      55             : 
      56             : 
      57             : ZEND_DECLARE_MODULE_GLOBALS(pcre)
      58             : 
      59             : 
      60           9 : static void pcre_handle_exec_error(int pcre_code) /* {{{ */
      61             : {
      62           9 :         int preg_code = 0;
      63             : 
      64           9 :         switch (pcre_code) {
      65             :                 case PCRE_ERROR_MATCHLIMIT:
      66           4 :                         preg_code = PHP_PCRE_BACKTRACK_LIMIT_ERROR;
      67           4 :                         break;
      68             : 
      69             :                 case PCRE_ERROR_RECURSIONLIMIT:
      70           2 :                         preg_code = PHP_PCRE_RECURSION_LIMIT_ERROR;
      71           2 :                         break;
      72             : 
      73             :                 case PCRE_ERROR_BADUTF8:
      74           2 :                         preg_code = PHP_PCRE_BAD_UTF8_ERROR;
      75           2 :                         break;
      76             : 
      77             :                 case PCRE_ERROR_BADUTF8_OFFSET:
      78           1 :                         preg_code = PHP_PCRE_BAD_UTF8_OFFSET_ERROR;
      79           1 :                         break;
      80             : 
      81             :                 default:
      82           0 :                         preg_code = PHP_PCRE_INTERNAL_ERROR;
      83             :                         break;
      84             :         }
      85             : 
      86           9 :         PCRE_G(error_code) = preg_code;
      87           9 : }
      88             : /* }}} */
      89             : 
      90       24386 : static void php_free_pcre_cache(zval *data) /* {{{ */
      91             : {
      92       24386 :         pcre_cache_entry *pce = (pcre_cache_entry *) Z_PTR_P(data);
      93       24386 :         if (!pce) return;
      94       24386 :         pefree(pce->re, 1);
      95       24386 :         if (pce->extra) {
      96       24381 :                 pcre_free_study(pce->extra);
      97             :         }
      98             : #if HAVE_SETLOCALE
      99       24386 :         if ((void*)pce->tables) pefree((void*)pce->tables, 1);
     100       24386 :         if (pce->locale) {
     101           8 :                 zend_string_release(pce->locale);
     102             :         }
     103             : #endif
     104       24386 :         pefree(pce, 1);
     105             : }
     106             : /* }}} */
     107             : 
     108       20871 : static PHP_GINIT_FUNCTION(pcre) /* {{{ */
     109             : {
     110       20871 :         zend_hash_init(&pcre_globals->pcre_cache, 0, NULL, php_free_pcre_cache, 1);
     111       20871 :         pcre_globals->backtrack_limit = 0;
     112       20871 :         pcre_globals->recursion_limit = 0;
     113       20871 :         pcre_globals->error_code      = PHP_PCRE_NO_ERROR;
     114       20871 : }
     115             : /* }}} */
     116             : 
     117       20905 : static PHP_GSHUTDOWN_FUNCTION(pcre) /* {{{ */
     118             : {
     119       20905 :         zend_hash_destroy(&pcre_globals->pcre_cache);
     120       20905 : }
     121             : /* }}} */
     122             : 
     123             : PHP_INI_BEGIN()
     124             :         STD_PHP_INI_ENTRY("pcre.backtrack_limit", "1000000", PHP_INI_ALL, OnUpdateLong, backtrack_limit, zend_pcre_globals, pcre_globals)
     125             :         STD_PHP_INI_ENTRY("pcre.recursion_limit", "100000",  PHP_INI_ALL, OnUpdateLong, recursion_limit, zend_pcre_globals, pcre_globals)
     126             : #ifdef PCRE_STUDY_JIT_COMPILE
     127             :         STD_PHP_INI_ENTRY("pcre.jit",             "1",       PHP_INI_ALL, OnUpdateBool, jit,             zend_pcre_globals, pcre_globals)
     128             : #endif
     129             : PHP_INI_END()
     130             : 
     131             : 
     132             : /* {{{ PHP_MINFO_FUNCTION(pcre) */
     133         145 : static PHP_MINFO_FUNCTION(pcre)
     134             : {
     135         145 :         int jit_yes = 0;
     136             : 
     137         145 :         php_info_print_table_start();
     138         145 :         php_info_print_table_row(2, "PCRE (Perl Compatible Regular Expressions) Support", "enabled" );
     139         145 :         php_info_print_table_row(2, "PCRE Library Version", pcre_version() );
     140             : 
     141         145 :         if (!pcre_config(PCRE_CONFIG_JIT, &jit_yes)) {
     142         145 :                 php_info_print_table_row(2, "PCRE JIT Support", jit_yes ? "enabled" : "disabled");
     143             :         } else {
     144           0 :                 php_info_print_table_row(2, "PCRE JIT Support", "unknown" );
     145             :         }
     146             : 
     147         145 :         php_info_print_table_end();
     148             : 
     149         145 :         DISPLAY_INI_ENTRIES();
     150         145 : }
     151             : /* }}} */
     152             : 
     153             : /* {{{ PHP_MINIT_FUNCTION(pcre) */
     154       20871 : static PHP_MINIT_FUNCTION(pcre)
     155             : {
     156       20871 :         REGISTER_INI_ENTRIES();
     157             : 
     158       20871 :         REGISTER_LONG_CONSTANT("PREG_PATTERN_ORDER", PREG_PATTERN_ORDER, CONST_CS | CONST_PERSISTENT);
     159       20871 :         REGISTER_LONG_CONSTANT("PREG_SET_ORDER", PREG_SET_ORDER, CONST_CS | CONST_PERSISTENT);
     160       20871 :         REGISTER_LONG_CONSTANT("PREG_OFFSET_CAPTURE", PREG_OFFSET_CAPTURE, CONST_CS | CONST_PERSISTENT);
     161       20871 :         REGISTER_LONG_CONSTANT("PREG_SPLIT_NO_EMPTY", PREG_SPLIT_NO_EMPTY, CONST_CS | CONST_PERSISTENT);
     162       20871 :         REGISTER_LONG_CONSTANT("PREG_SPLIT_DELIM_CAPTURE", PREG_SPLIT_DELIM_CAPTURE, CONST_CS | CONST_PERSISTENT);
     163       20871 :         REGISTER_LONG_CONSTANT("PREG_SPLIT_OFFSET_CAPTURE", PREG_SPLIT_OFFSET_CAPTURE, CONST_CS | CONST_PERSISTENT);
     164       20871 :         REGISTER_LONG_CONSTANT("PREG_GREP_INVERT", PREG_GREP_INVERT, CONST_CS | CONST_PERSISTENT);
     165             : 
     166       20871 :         REGISTER_LONG_CONSTANT("PREG_NO_ERROR", PHP_PCRE_NO_ERROR, CONST_CS | CONST_PERSISTENT);
     167       20871 :         REGISTER_LONG_CONSTANT("PREG_INTERNAL_ERROR", PHP_PCRE_INTERNAL_ERROR, CONST_CS | CONST_PERSISTENT);
     168       20871 :         REGISTER_LONG_CONSTANT("PREG_BACKTRACK_LIMIT_ERROR", PHP_PCRE_BACKTRACK_LIMIT_ERROR, CONST_CS | CONST_PERSISTENT);
     169       20871 :         REGISTER_LONG_CONSTANT("PREG_RECURSION_LIMIT_ERROR", PHP_PCRE_RECURSION_LIMIT_ERROR, CONST_CS | CONST_PERSISTENT);
     170       20871 :         REGISTER_LONG_CONSTANT("PREG_BAD_UTF8_ERROR", PHP_PCRE_BAD_UTF8_ERROR, CONST_CS | CONST_PERSISTENT);
     171       20871 :         REGISTER_LONG_CONSTANT("PREG_BAD_UTF8_OFFSET_ERROR", PHP_PCRE_BAD_UTF8_OFFSET_ERROR, CONST_CS | CONST_PERSISTENT);
     172       20871 :         REGISTER_STRING_CONSTANT("PCRE_VERSION", (char *)pcre_version(), CONST_CS | CONST_PERSISTENT);
     173             : 
     174       20871 :         return SUCCESS;
     175             : }
     176             : /* }}} */
     177             : 
     178             : /* {{{ PHP_MSHUTDOWN_FUNCTION(pcre) */
     179       20905 : static PHP_MSHUTDOWN_FUNCTION(pcre)
     180             : {
     181       20905 :         UNREGISTER_INI_ENTRIES();
     182             : 
     183       20905 :         return SUCCESS;
     184             : }
     185             : /* }}} */
     186             : 
     187             : /* {{{ static pcre_clean_cache */
     188       36864 : static int pcre_clean_cache(zval *data, void *arg)
     189             : {
     190       36864 :         int *num_clean = (int *)arg;
     191             : 
     192       36864 :         if (*num_clean > 0) {
     193        4608 :                 (*num_clean)--;
     194        4608 :                 return 1;
     195             :         } else {
     196       32256 :                 return 0;
     197             :         }
     198             : }
     199             : /* }}} */
     200             : 
     201             : /* {{{ static make_subpats_table */
     202          10 : static char **make_subpats_table(int num_subpats, pcre_cache_entry *pce)
     203             : {
     204          10 :         pcre_extra *extra = pce->extra;
     205          10 :         int name_cnt = pce->name_count, name_size, ni = 0;
     206             :         int rc;
     207             :         char *name_table;
     208             :         unsigned short name_idx;
     209             :         char **subpat_names;
     210             :         int rc1, rc2;
     211             : 
     212          10 :         rc1 = pcre_fullinfo(pce->re, extra, PCRE_INFO_NAMETABLE, &name_table);
     213          10 :         rc2 = pcre_fullinfo(pce->re, extra, PCRE_INFO_NAMEENTRYSIZE, &name_size);
     214          10 :         rc = rc2 ? rc2 : rc1;
     215          10 :         if (rc < 0) {
     216           0 :                 php_error_docref(NULL, E_WARNING, "Internal pcre_fullinfo() error %d", rc);
     217           0 :                 return NULL;
     218             :         }
     219             : 
     220          10 :         subpat_names = (char **)ecalloc(num_subpats, sizeof(char *));
     221         174 :         while (ni++ < name_cnt) {
     222         154 :                 name_idx = 0xff * (unsigned char)name_table[0] + (unsigned char)name_table[1];
     223         154 :                 subpat_names[name_idx] = name_table + 2;
     224         308 :                 if (is_numeric_string(subpat_names[name_idx], strlen(subpat_names[name_idx]), NULL, NULL, 0) > 0) {
     225           0 :                         php_error_docref(NULL, E_WARNING, "Numeric named subpatterns are not allowed");
     226           0 :                         efree(subpat_names);
     227           0 :                         return NULL;
     228             :                 }
     229         154 :                 name_table += name_size;
     230             :         }
     231          10 :         return subpat_names;
     232             : }
     233             : /* }}} */
     234             : 
     235             : /* {{{ pcre_get_compiled_regex_cache
     236             :  */
     237     1810453 : PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
     238             : {
     239     1810453 :         pcre                            *re = NULL;
     240             :         pcre_extra                      *extra;
     241     1810453 :         int                                      coptions = 0;
     242     1810453 :         int                                      soptions = 0;
     243             :         const char                      *error;
     244             :         int                                      erroffset;
     245             :         char                             delimiter;
     246             :         char                             start_delimiter;
     247             :         char                             end_delimiter;
     248             :         char                            *p, *pp;
     249             :         char                            *pattern;
     250     1810453 :         int                                      do_study = 0;
     251     1810453 :         int                                      poptions = 0;
     252     1810453 :         unsigned const char *tables = NULL;
     253             :         pcre_cache_entry        *pce;
     254             :         pcre_cache_entry         new_entry;
     255             :         int                                      rc;
     256             : 
     257             :         /* Try to lookup the cached regex entry, and if successful, just pass
     258             :            back the compiled pattern, otherwise go on and compile it. */
     259     1810453 :         pce = zend_hash_find_ptr(&PCRE_G(pcre_cache), regex);
     260     1810453 :         if (pce) {
     261             : #if HAVE_SETLOCALE
     262     1786103 :                 if (pce->locale == BG(locale_string) ||
     263          40 :                     (pce->locale && BG(locale_string) &&
     264          20 :                      pce->locale->len == BG(locale_string)->len &&
     265          18 :                      !memcmp(pce->locale->val, BG(locale_string)->val, pce->locale->len))) {
     266     1786023 :                         return pce;
     267             :                 }
     268             : #else
     269             :                 return pce;
     270             : #endif
     271             :         }
     272             : 
     273       24430 :         p = regex->val;
     274             : 
     275             :         /* Parse through the leading whitespace, and display a warning if we
     276             :            get to the end without encountering a delimiter. */
     277       24430 :         while (isspace((int)*(unsigned char *)p)) p++;
     278       24430 :         if (*p == 0) {
     279           5 :                 php_error_docref(NULL, E_WARNING,
     280           5 :                                                  p < regex->val + regex->len ? "Null byte in regex" : "Empty regular expression");
     281           5 :                 return NULL;
     282             :         }
     283             : 
     284             :         /* Get the delimiter and display a warning if it is alphanumeric
     285             :            or a backslash. */
     286       24425 :         delimiter = *p++;
     287       24425 :         if (isalnum((int)*(unsigned char *)&delimiter) || delimiter == '\\') {
     288           7 :                 php_error_docref(NULL,E_WARNING, "Delimiter must not be alphanumeric or backslash");
     289           7 :                 return NULL;
     290             :         }
     291             : 
     292       24418 :         start_delimiter = delimiter;
     293       24418 :         if ((pp = strchr("([{< )]}> )]}>", delimiter)))
     294          29 :                 delimiter = pp[5];
     295       24418 :         end_delimiter = delimiter;
     296             : 
     297       24418 :         pp = p;
     298             : 
     299       24418 :         if (start_delimiter == end_delimiter) {
     300             :                 /* We need to iterate through the pattern, searching for the ending delimiter,
     301             :                    but skipping the backslashed delimiters.  If the ending delimiter is not
     302             :                    found, display a warning. */
     303    16324669 :                 while (*pp != 0) {
     304    16300271 :                         if (*pp == '\\' && pp[1] != 0) pp++;
     305    15133610 :                         else if (*pp == delimiter)
     306       24380 :                                 break;
     307    16275891 :                         pp++;
     308             :                 }
     309             :         } else {
     310             :                 /* We iterate through the pattern, searching for the matching ending
     311             :                  * delimiter. For each matching starting delimiter, we increment nesting
     312             :                  * level, and decrement it for each matching ending delimiter. If we
     313             :                  * reach the end of the pattern without matching, display a warning.
     314             :                  */
     315          29 :                 int brackets = 1;       /* brackets nesting level */
     316         281 :                 while (*pp != 0) {
     317         249 :                         if (*pp == '\\' && pp[1] != 0) pp++;
     318         226 :                         else if (*pp == end_delimiter && --brackets <= 0)
     319             :                                 break;
     320         200 :                         else if (*pp == start_delimiter)
     321           1 :                                 brackets++;
     322         223 :                         pp++;
     323             :                 }
     324             :         }
     325             : 
     326       24418 :         if (*pp == 0) {
     327          12 :                 if (pp < regex->val + regex->len) {
     328           4 :                         php_error_docref(NULL,E_WARNING, "Null byte in regex");
     329           8 :                 } else if (start_delimiter == end_delimiter) {
     330           7 :                         php_error_docref(NULL,E_WARNING, "No ending delimiter '%c' found", delimiter);
     331             :                 } else {
     332           1 :                         php_error_docref(NULL,E_WARNING, "No ending matching delimiter '%c' found", delimiter);
     333             :                 }
     334          12 :                 return NULL;
     335             :         }
     336             : 
     337             :         /* Make a copy of the actual pattern. */
     338       24406 :         pattern = estrndup(p, pp-p);
     339             : 
     340             :         /* Move on to the options */
     341       24406 :         pp++;
     342             : 
     343             :         /* Parse through the options, setting appropriate flags.  Display
     344             :            a warning if we encounter an unknown modifier. */
     345       62678 :         while (pp < regex->val + regex->len) {
     346       13884 :                 switch (*pp++) {
     347             :                         /* Perl compatible options */
     348        1922 :                         case 'i':       coptions |= PCRE_CASELESS;              break;
     349        2124 :                         case 'm':       coptions |= PCRE_MULTILINE;             break;
     350        9709 :                         case 's':       coptions |= PCRE_DOTALL;                break;
     351           5 :                         case 'x':       coptions |= PCRE_EXTENDED;              break;
     352             : 
     353             :                         /* PCRE specific options */
     354           2 :                         case 'A':       coptions |= PCRE_ANCHORED;              break;
     355           9 :                         case 'D':       coptions |= PCRE_DOLLAR_ENDONLY;break;
     356          25 :                         case 'S':       do_study  = 1;                                  break;
     357          31 :                         case 'U':       coptions |= PCRE_UNGREEDY;              break;
     358           1 :                         case 'X':       coptions |= PCRE_EXTRA;                 break;
     359          34 :                         case 'u':       coptions |= PCRE_UTF8;
     360             :         /* In  PCRE,  by  default, \d, \D, \s, \S, \w, and \W recognize only ASCII
     361             :        characters, even in UTF-8 mode. However, this can be changed by setting
     362             :        the PCRE_UCP option. */
     363             : #ifdef PCRE_UCP
     364          34 :                                                 coptions |= PCRE_UCP;
     365             : #endif
     366          34 :                                 break;
     367             : 
     368             :                         /* Custom preg options */
     369           2 :                         case 'e':       poptions |= PREG_REPLACE_EVAL;  break;
     370             : 
     371             :                         case ' ':
     372             :                         case '\n':
     373           2 :                                 break;
     374             : 
     375             :                         default:
     376          18 :                                 if (pp[-1]) {
     377          13 :                                         php_error_docref(NULL,E_WARNING, "Unknown modifier '%c'", pp[-1]);
     378             :                                 } else {
     379           5 :                                         php_error_docref(NULL,E_WARNING, "Null byte in regex");
     380             :                                 }
     381          18 :                                 efree(pattern);
     382          18 :                                 return NULL;
     383             :                 }
     384             :         }
     385             : 
     386             : #if HAVE_SETLOCALE
     387       24400 :         if (BG(locale_string) &&
     388          12 :             (BG(locale_string)->len != 1 || BG(locale_string)->val[0] != 'C')) {
     389           4 :                 tables = pcre_maketables();
     390             :         }
     391             : #endif
     392             : 
     393             :         /* Compile pattern and display a warning if compilation failed. */
     394       24388 :         re = pcre_compile(pattern,
     395             :                                           coptions,
     396             :                                           &error,
     397             :                                           &erroffset,
     398             :                                           tables);
     399             : 
     400       24388 :         if (re == NULL) {
     401           6 :                 php_error_docref(NULL,E_WARNING, "Compilation failed: %s at offset %d", error, erroffset);
     402           6 :                 efree(pattern);
     403           6 :                 if (tables) {
     404           0 :                         pefree((void*)tables, 1);
     405             :                 }
     406           6 :                 return NULL;
     407             :         }
     408             : 
     409             : #ifdef PCRE_STUDY_JIT_COMPILE
     410       24382 :         if (PCRE_G(jit)) {
     411             :                 /* Enable PCRE JIT compiler */
     412       24377 :                 do_study = 1;
     413       24377 :                 soptions |= PCRE_STUDY_JIT_COMPILE;
     414             :         }
     415             : #endif
     416             : 
     417             :         /* If study option was specified, study the pattern and
     418             :            store the result in extra for passing to pcre_exec. */
     419       24382 :         if (do_study) {
     420       24377 :                 extra = pcre_study(re, soptions, &error);
     421       24377 :                 if (extra) {
     422       24377 :                         extra->flags |= PCRE_EXTRA_MATCH_LIMIT | PCRE_EXTRA_MATCH_LIMIT_RECURSION;
     423       24377 :                         extra->match_limit = (unsigned long)PCRE_G(backtrack_limit);
     424       24377 :                         extra->match_limit_recursion = (unsigned long)PCRE_G(recursion_limit);
     425             :                 }
     426       24377 :                 if (error != NULL) {
     427           0 :                         php_error_docref(NULL, E_WARNING, "Error while studying pattern");
     428             :                 }
     429             :         } else {
     430           5 :                 extra = NULL;
     431             :         }
     432             : 
     433       24382 :         efree(pattern);
     434             : 
     435             :         /*
     436             :          * If we reached cache limit, clean out the items from the head of the list;
     437             :          * these are supposedly the oldest ones (but not necessarily the least used
     438             :          * ones).
     439             :          */
     440       24382 :         if (zend_hash_num_elements(&PCRE_G(pcre_cache)) == PCRE_CACHE_SIZE) {
     441           9 :                 int num_clean = PCRE_CACHE_SIZE / 8;
     442           9 :                 zend_hash_apply_with_argument(&PCRE_G(pcre_cache), pcre_clean_cache, &num_clean);
     443             :         }
     444             : 
     445             :         /* Store the compiled pattern and extra info in the cache. */
     446       24382 :         new_entry.re = re;
     447       24382 :         new_entry.extra = extra;
     448       24382 :         new_entry.preg_options = poptions;
     449       24382 :         new_entry.compile_options = coptions;
     450             : #if HAVE_SETLOCALE
     451       24390 :         new_entry.locale = BG(locale_string) ?
     452          16 :                 ((GC_FLAGS(BG(locale_string)) & IS_STR_PERSISTENT) ?
     453           0 :                         zend_string_copy(BG(locale_string)) :
     454           8 :                         zend_string_init(BG(locale_string)->val, BG(locale_string)->len, 1)) :
     455             :                 NULL;
     456       24382 :         new_entry.tables = tables;
     457             : #endif
     458             : 
     459       24382 :         rc = pcre_fullinfo(re, extra, PCRE_INFO_CAPTURECOUNT, &new_entry.capture_count);
     460       24382 :         if (rc < 0) {
     461           0 :                 php_error_docref(NULL, E_WARNING, "Internal pcre_fullinfo() error %d", rc);
     462           0 :                 return NULL;
     463             :         }
     464             : 
     465       24382 :         rc = pcre_fullinfo(re, extra, PCRE_INFO_NAMECOUNT, &new_entry.name_count);
     466       24382 :         if (rc < 0) {
     467           0 :                 php_error_docref(NULL, E_WARNING, "Internal pcre_fullinfo() error %d", rc);
     468           0 :                 return NULL;
     469             :         }
     470             : 
     471             :         /*
     472             :          * Interned strings are not duplicated when stored in HashTable,
     473             :          * but all the interned strings created during HTTP request are removed
     474             :          * at end of request. However PCRE_G(pcre_cache) must be consistent
     475             :          * on the next request as well. So we disable usage of interned strings
     476             :          * as hash keys especually for this table.
     477             :          * See bug #63180
     478             :          */
     479       48764 :         pce = zend_hash_str_update_mem(&PCRE_G(pcre_cache), regex->val, regex->len, &new_entry, sizeof(pcre_cache_entry));
     480             : 
     481       24382 :         return pce;
     482             : }
     483             : /* }}} */
     484             : 
     485             : /* {{{ pcre_get_compiled_regex
     486             :  */
     487      127855 : PHPAPI pcre* pcre_get_compiled_regex(zend_string *regex, pcre_extra **extra, int *preg_options)
     488             : {
     489      127855 :         pcre_cache_entry * pce = pcre_get_compiled_regex_cache(regex);
     490             : 
     491      127855 :         if (extra) {
     492      127855 :                 *extra = pce ? pce->extra : NULL;
     493             :         }
     494      127855 :         if (preg_options) {
     495      127855 :                 *preg_options = pce ? pce->preg_options : 0;
     496             :         }
     497             : 
     498      127855 :         return pce ? pce->re : NULL;
     499             : }
     500             : /* }}} */
     501             : 
     502             : /* {{{ pcre_get_compiled_regex_ex
     503             :  */
     504           0 : PHPAPI pcre* pcre_get_compiled_regex_ex(zend_string *regex, pcre_extra **extra, int *preg_options, int *compile_options)
     505             : {
     506           0 :         pcre_cache_entry * pce = pcre_get_compiled_regex_cache(regex);
     507             : 
     508           0 :         if (extra) {
     509           0 :                 *extra = pce ? pce->extra : NULL;
     510             :         }
     511           0 :         if (preg_options) {
     512           0 :                 *preg_options = pce ? pce->preg_options : 0;
     513             :         }
     514           0 :         if (compile_options) {
     515           0 :                 *compile_options = pce ? pce->compile_options : 0;
     516             :         }
     517             : 
     518           0 :         return pce ? pce->re : NULL;
     519             : }
     520             : /* }}} */
     521             : 
     522             : /* {{{ add_offset_pair */
     523          73 : static inline void add_offset_pair(zval *result, char *str, int len, int offset, char *name)
     524             : {
     525             :         zval match_pair, tmp;
     526             : 
     527          73 :         array_init_size(&match_pair, 2);
     528             : 
     529             :         /* Add (match, offset) to the return value */
     530         146 :         ZVAL_STRINGL(&tmp, str, len);
     531          73 :         zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
     532          73 :         ZVAL_LONG(&tmp, offset);
     533          73 :         zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
     534             : 
     535          73 :         if (name) {
     536             :                 Z_ADDREF(match_pair);
     537           2 :                 zend_hash_str_update(Z_ARRVAL_P(result), name, strlen(name), &match_pair);
     538             :         }
     539          73 :         zend_hash_next_index_insert(Z_ARRVAL_P(result), &match_pair);
     540          73 : }
     541             : /* }}} */
     542             : 
     543     1653599 : static void php_do_pcre_match(INTERNAL_FUNCTION_PARAMETERS, int global) /* {{{ */
     544             : {
     545             :         /* parameters */
     546             :         zend_string              *regex;                        /* Regular expression */
     547             :         zend_string              *subject;                      /* String to match against */
     548             :         pcre_cache_entry *pce;                          /* Compiled regular expression */
     549     1653599 :         zval                     *subpats = NULL;       /* Array for subpatterns */
     550     1653599 :         zend_long                 flags = 0;            /* Match control flags */
     551     1653599 :         zend_long                 start_offset = 0;     /* Where the new search starts */
     552             : 
     553             : #ifndef FAST_ZPP
     554             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|z/ll", &regex,
     555             :                                                           &subject, &subpats, &flags, &start_offset) == FAILURE) {
     556             :                 RETURN_FALSE;
     557             :         }
     558             : #else
     559     1653599 :         ZEND_PARSE_PARAMETERS_START(2, 5)
     560     4960773 :                 Z_PARAM_STR(regex)
     561     4960761 :                 Z_PARAM_STR(subject)
     562     1653583 :                 Z_PARAM_OPTIONAL
     563     3871436 :                 Z_PARAM_ZVAL_EX(subpats, 0, 1)
     564     1116855 :                 Z_PARAM_LONG(flags)
     565          56 :                 Z_PARAM_LONG(start_offset)
     566     1653599 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     567             : #endif
     568             : 
     569             :         /* Compile regex or get it from cache. */
     570     1653583 :         if ((pce = pcre_get_compiled_regex_cache(regex)) == NULL) {
     571          27 :                 RETURN_FALSE;
     572             :         }
     573             : 
     574     1653556 :         php_pcre_match_impl(pce, subject->val, (int)subject->len, return_value, subpats,
     575             :                 global, ZEND_NUM_ARGS() >= 4, flags, start_offset);
     576             : }
     577             : /* }}} */
     578             : 
     579             : /* {{{ php_pcre_match_impl() */
     580     1654402 : PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subject_len, zval *return_value,
     581             :         zval *subpats, int global, int use_flags, zend_long flags, zend_long start_offset)
     582             : {
     583             :         zval                     result_set,            /* Holds a set of subpatterns after
     584             :                                                                                    a global match */
     585     1654402 :                                     *match_sets = NULL; /* An array of sets of matches for each
     586             :                                                                                    subpattern after a global match */
     587     1654402 :         pcre_extra              *extra = pce->extra;/* Holds results of studying */
     588             :         pcre_extra               extra_data;            /* Used locally for exec options */
     589     1654402 :         int                              exoptions = 0;         /* Execution options */
     590     1654402 :         int                              count = 0;                     /* Count of matched subpatterns */
     591             :         int                             *offsets;                       /* Array of subpattern offsets */
     592             :         int                              num_subpats;           /* Number of captured subpatterns */
     593             :         int                              size_offsets;          /* Size of the offsets array */
     594             :         int                              matched;                       /* Has anything matched */
     595     1654402 :         int                              g_notempty = 0;        /* If the match should not be empty */
     596             :         const char         **stringlist;                /* Holds list of subpatterns */
     597             :         char               **subpat_names;              /* Array for named subpatterns */
     598             :         int                              i;
     599             :         int                              subpats_order;         /* Order of subpattern matches */
     600             :         int                              offset_capture;    /* Capture match offsets: yes/no */
     601     1654402 :         unsigned char   *mark = NULL;       /* Target for MARK name */
     602             :         zval            marks;                  /* Array of marks for PREG_PATTERN_ORDER */
     603             :         ALLOCA_FLAG(use_heap);
     604             : 
     605     1654402 :         ZVAL_UNDEF(&marks);
     606             : 
     607             :         /* Overwrite the passed-in value for subpatterns with an empty array. */
     608     1654402 :         if (subpats != NULL) {
     609             :                 zval_dtor(subpats);
     610     1117624 :                 array_init(subpats);
     611             :         }
     612             : 
     613     1654402 :         subpats_order = global ? PREG_PATTERN_ORDER : 0;
     614             : 
     615     1654402 :         if (use_flags) {
     616         819 :                 offset_capture = flags & PREG_OFFSET_CAPTURE;
     617             : 
     618             :                 /*
     619             :                  * subpats_order is pre-set to pattern mode so we change it only if
     620             :                  * necessary.
     621             :                  */
     622         819 :                 if (flags & 0xff) {
     623          23 :                         subpats_order = flags & 0xff;
     624             :                 }
     625         819 :                 if ((global && (subpats_order < PREG_PATTERN_ORDER || subpats_order > PREG_SET_ORDER)) ||
     626             :                         (!global && subpats_order != 0)) {
     627           1 :                         php_error_docref(NULL, E_WARNING, "Invalid flags specified");
     628           1 :                         return;
     629             :                 }
     630             :         } else {
     631     1653583 :                 offset_capture = 0;
     632             :         }
     633             : 
     634             :         /* Negative offset counts from the end of the string. */
     635     1654401 :         if (start_offset < 0) {
     636           5 :                 start_offset = subject_len + start_offset;
     637           5 :                 if (start_offset < 0) {
     638           1 :                         start_offset = 0;
     639             :                 }
     640             :         }
     641             : 
     642     1654401 :         if (extra == NULL) {
     643           3 :                 extra_data.flags = PCRE_EXTRA_MATCH_LIMIT | PCRE_EXTRA_MATCH_LIMIT_RECURSION;
     644           3 :                 extra = &extra_data;
     645             :         }
     646     1654401 :         extra->match_limit = (unsigned long)PCRE_G(backtrack_limit);
     647     1654401 :         extra->match_limit_recursion = (unsigned long)PCRE_G(recursion_limit);
     648             : #ifdef PCRE_EXTRA_MARK
     649     1654401 :         extra->mark = &mark;
     650     1654401 :         extra->flags |= PCRE_EXTRA_MARK;
     651             : #endif
     652             : 
     653             :         /* Calculate the size of the offsets array, and allocate memory for it. */
     654     1654401 :         num_subpats = pce->capture_count + 1;
     655     1654401 :         size_offsets = num_subpats * 3;
     656             : 
     657             :         /*
     658             :          * Build a mapping from subpattern numbers to their names. We will
     659             :          * allocate the table only if there are any named subpatterns.
     660             :          */
     661     1654401 :         subpat_names = NULL;
     662     1654401 :         if (pce->name_count > 0) {
     663           9 :                 subpat_names = make_subpats_table(num_subpats, pce);
     664           9 :                 if (!subpat_names) {
     665           0 :                         RETURN_FALSE;
     666             :                 }
     667             :         }
     668             : 
     669     1654401 :         if (size_offsets <= 32) {
     670     1654390 :                 offsets = (int *)do_alloca(size_offsets * sizeof(int), use_heap);
     671             :         } else {
     672          11 :                 offsets = (int *)safe_emalloc(size_offsets, sizeof(int), 0);
     673             :         }
     674     1654401 :         memset(offsets, 0, size_offsets*sizeof(int));
     675             :         /* Allocate match sets array and initialize the values. */
     676     1654401 :         if (global && subpats && subpats_order == PREG_PATTERN_ORDER) {
     677         895 :                 match_sets = (zval *)safe_emalloc(num_subpats, sizeof(zval), 0);
     678        2023 :                 for (i=0; i<num_subpats; i++) {
     679        1128 :                         array_init(&match_sets[i]);
     680             :                 }
     681             :         }
     682             : 
     683     1654401 :         matched = 0;
     684     1654401 :         PCRE_G(error_code) = PHP_PCRE_NO_ERROR;
     685             : 
     686             :         do {
     687             :                 /* Execute the regular expression. */
     688     1654596 :                 count = pcre_exec(pce->re, extra, subject, (int)subject_len, (int)start_offset,
     689             :                                                   exoptions|g_notempty, offsets, size_offsets);
     690             : 
     691             :                 /* the string was already proved to be valid UTF-8 */
     692     1654596 :                 exoptions |= PCRE_NO_UTF8_CHECK;
     693             : 
     694             :                 /* Check for too many substrings condition. */
     695     1654596 :                 if (count == 0) {
     696           0 :                         php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
     697           0 :                         count = size_offsets/3;
     698             :                 }
     699             : 
     700             :                 /* If something has matched */
     701     1654596 :                 if (count > 0) {
     702       89900 :                         matched++;
     703             : 
     704             :                         /* If subpatterns array has been passed, fill it in with values. */
     705       89900 :                         if (subpats != NULL) {
     706             :                                 /* Try to get the list of substrings and display a warning if failed. */
     707       41677 :                                 if (pcre_get_substring_list(subject, offsets, count, &stringlist) < 0) {
     708           0 :                                         if (subpat_names) {
     709           0 :                                                 efree(subpat_names);
     710             :                                         }
     711           0 :                                         if (size_offsets <= 32) {
     712           0 :                                                 free_alloca(offsets, use_heap);
     713             :                                         } else {
     714           0 :                                                 efree(offsets);
     715             :                                         }
     716           0 :                                         if (match_sets) efree(match_sets);
     717           0 :                                         php_error_docref(NULL, E_WARNING, "Get subpatterns list failed");
     718           0 :                                         RETURN_FALSE;
     719             :                                 }
     720             : 
     721       41677 :                                 if (global) {   /* global pattern matching */
     722         324 :                                         if (subpats && subpats_order == PREG_PATTERN_ORDER) {
     723             :                                                 /* For each subpattern, insert it into the appropriate array. */
     724         140 :                                                 if (offset_capture) {
     725          27 :                                                         for (i = 0; i < count; i++) {
     726          30 :                                                                 add_offset_pair(&match_sets[i], (char *)stringlist[i],
     727          30 :                                                                                                 offsets[(i<<1)+1] - offsets[i<<1], offsets[i<<1], NULL);
     728             :                                                         }
     729             :                                                 } else {
     730         337 :                                                         for (i = 0; i < count; i++) {
     731         209 :                                                                 add_next_index_stringl(&match_sets[i], (char *)stringlist[i],
     732         209 :                                                                                                            offsets[(i<<1)+1] - offsets[i<<1]);
     733             :                                                         }
     734             :                                                 }
     735             :                                                 /* Add MARK, if available */
     736         140 :                                                 if (mark) {
     737           2 :                                                         if (Z_TYPE(marks) == IS_UNDEF) {
     738           1 :                                                                 array_init(&marks);
     739             :                                                         }
     740           2 :                                                         add_index_string(&marks, matched - 1, (char *) mark);
     741             :                                                 }
     742             :                                                 /*
     743             :                                                  * If the number of captured subpatterns on this run is
     744             :                                                  * less than the total possible number, pad the result
     745             :                                                  * arrays with empty strings.
     746             :                                                  */
     747         140 :                                                 if (count < num_subpats) {
     748          11 :                                                         for (; i < num_subpats; i++) {
     749           7 :                                                                 add_next_index_string(&match_sets[i], "");
     750             :                                                         }
     751             :                                                 }
     752             :                                         } else {
     753             :                                                 /* Allocate the result set array */
     754          44 :                                                 array_init_size(&result_set, count + (mark ? 1 : 0));
     755             : 
     756             :                                                 /* Add all the subpatterns to it */
     757          44 :                                                 if (subpat_names) {
     758           2 :                                                         if (offset_capture) {
     759           0 :                                                                 for (i = 0; i < count; i++) {
     760           0 :                                                                         add_offset_pair(&result_set, (char *)stringlist[i],
     761           0 :                                                                                                         offsets[(i<<1)+1] - offsets[i<<1], offsets[i<<1], subpat_names[i]);
     762             :                                                                 }
     763             :                                                         } else {
     764          14 :                                                                 for (i = 0; i < count; i++) {
     765          12 :                                                                         if (subpat_names[i]) {
     766           8 :                                                                                 add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i],
     767             :                                                                                                                            offsets[(i<<1)+1] - offsets[i<<1]);
     768             :                                                                         }
     769          12 :                                                                         add_next_index_stringl(&result_set, (char *)stringlist[i],
     770          12 :                                                                                                                    offsets[(i<<1)+1] - offsets[i<<1]);
     771             :                                                                 }
     772             :                                                         }
     773             :                                                 } else {
     774          42 :                                                         if (offset_capture) {
     775          10 :                                                                 for (i = 0; i < count; i++) {
     776          14 :                                                                         add_offset_pair(&result_set, (char *)stringlist[i],
     777          14 :                                                                                                         offsets[(i<<1)+1] - offsets[i<<1], offsets[i<<1], NULL);
     778             :                                                                 }
     779             :                                                         } else {
     780         343 :                                                                 for (i = 0; i < count; i++) {
     781         304 :                                                                         add_next_index_stringl(&result_set, (char *)stringlist[i],
     782         304 :                                                                                                                    offsets[(i<<1)+1] - offsets[i<<1]);
     783             :                                                                 }
     784             :                                                         }
     785             :                                                 }
     786             :                                                 /* Add MARK, if available */
     787          44 :                                                 if (mark) {
     788           2 :                                                         add_assoc_string_ex(&result_set, "MARK", sizeof("MARK") - 1, (char *)mark);
     789             :                                                 }
     790             :                                                 /* And add it to the output array */
     791          44 :                                                 zend_hash_next_index_insert(Z_ARRVAL_P(subpats), &result_set);
     792             :                                         }
     793             :                                 } else {                        /* single pattern matching */
     794             :                                         /* For each subpattern, insert it into the subpatterns array. */
     795       41493 :                                         if (subpat_names) {
     796           5 :                                                 if (offset_capture) {
     797           5 :                                                         for (i = 0; i < count; i++) {
     798          12 :                                                                 add_offset_pair(subpats, (char *)stringlist[i],
     799           4 :                                                                                                 offsets[(i<<1)+1] - offsets[i<<1],
     800           8 :                                                                                                 offsets[i<<1], subpat_names[i]);
     801             :                                                         }
     802             :                                                 } else {
     803          24 :                                                         for (i = 0; i < count; i++) {
     804          20 :                                                                 if (subpat_names[i]) {
     805          13 :                                                                         add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i],
     806             :                                                                                                           offsets[(i<<1)+1] - offsets[i<<1]);
     807             :                                                                 }
     808          20 :                                                                 add_next_index_stringl(subpats, (char *)stringlist[i],
     809          20 :                                                                                                            offsets[(i<<1)+1] - offsets[i<<1]);
     810             :                                                         }
     811             :                                                 }
     812             :                                         } else {
     813       41488 :                                                 if (offset_capture) {
     814          10 :                                                         for (i = 0; i < count; i++) {
     815          12 :                                                                 add_offset_pair(subpats, (char *)stringlist[i],
     816           6 :                                                                                                 offsets[(i<<1)+1] - offsets[i<<1],
     817           6 :                                                                                                 offsets[i<<1], NULL);
     818             :                                                         }
     819             :                                                 } else {
     820      126614 :                                                         for (i = 0; i < count; i++) {
     821       85130 :                                                                 add_next_index_stringl(subpats, (char *)stringlist[i],
     822       85130 :                                                                                                            offsets[(i<<1)+1] - offsets[i<<1]);
     823             :                                                         }
     824             :                                                 }
     825             :                                         }
     826             :                                         /* Add MARK, if available */
     827       41493 :                                         if (mark) {
     828           1 :                                                 add_assoc_string_ex(subpats, "MARK", sizeof("MARK") - 1, (char *)mark);
     829             :                                         }
     830             :                                 }
     831             : 
     832       41677 :                                 pcre_free((void *) stringlist);
     833             :                         }
     834     1564696 :                 } else if (count == PCRE_ERROR_NOMATCH) {
     835             :                         /* If we previously set PCRE_NOTEMPTY after a null match,
     836             :                            this is not necessarily the end. We need to advance
     837             :                            the start offset, and continue. Fudge the offset values
     838             :                            to achieve this, unless we're already at the end of the string. */
     839     1564692 :                         if (g_notempty != 0 && start_offset < subject_len) {
     840           2 :                                 offsets[0] = (int)start_offset;
     841           2 :                                 offsets[1] = (int)(start_offset + 1);
     842             :                         } else
     843             :                                 break;
     844             :                 } else {
     845           4 :                         pcre_handle_exec_error(count);
     846           4 :                         break;
     847             :                 }
     848             : 
     849             :                 /* If we have matched an empty string, mimic what Perl's /g options does.
     850             :                    This turns out to be rather cunning. First we set PCRE_NOTEMPTY and try
     851             :                    the match again at the same point. If this fails (picked up above) we
     852             :                    advance to the next character. */
     853       89902 :                 g_notempty = (offsets[1] == offsets[0])? PCRE_NOTEMPTY | PCRE_ANCHORED : 0;
     854             : 
     855             :                 /* Advance to the position right after the last full match */
     856       89902 :                 start_offset = offsets[1];
     857       89902 :         } while (global);
     858             : 
     859             :         /* Add the match sets to the output array and clean up */
     860     1654401 :         if (global && subpats && subpats_order == PREG_PATTERN_ORDER) {
     861         895 :                 if (subpat_names) {
     862          10 :                         for (i = 0; i < num_subpats; i++) {
     863           8 :                                 if (subpat_names[i]) {
     864           5 :                                         zend_hash_str_update(Z_ARRVAL_P(subpats), subpat_names[i],
     865             :                                                                          strlen(subpat_names[i]), &match_sets[i]);
     866           5 :                                         Z_ADDREF(match_sets[i]);
     867             :                                 }
     868           8 :                                 zend_hash_next_index_insert(Z_ARRVAL_P(subpats), &match_sets[i]);
     869             :                         }
     870             :                 } else {
     871        2013 :                         for (i = 0; i < num_subpats; i++) {
     872        1120 :                                 zend_hash_next_index_insert(Z_ARRVAL_P(subpats), &match_sets[i]);
     873             :                         }
     874             :                 }
     875         895 :                 efree(match_sets);
     876             : 
     877         895 :                 if (Z_TYPE(marks) != IS_UNDEF) {
     878           1 :                         add_assoc_zval(subpats, "MARK", &marks);
     879             :                 }
     880             :         }
     881             : 
     882     1654401 :         if (size_offsets <= 32) {
     883     1654390 :                 free_alloca(offsets, use_heap);
     884             :         } else {
     885          11 :                 efree(offsets);
     886             :         }
     887     1654401 :         if (subpat_names) {
     888           9 :                 efree(subpat_names);
     889             :         }
     890             : 
     891             :         /* Did we encounter an error? */
     892     1654401 :         if (PCRE_G(error_code) == PHP_PCRE_NO_ERROR) {
     893     1654397 :                 RETVAL_LONG(matched);
     894             :         } else {
     895           4 :                 RETVAL_FALSE;
     896             :         }
     897             : }
     898             : /* }}} */
     899             : 
     900             : /* {{{ proto int preg_match(string pattern, string subject [, array &subpatterns [, int flags [, int offset]]])
     901             :    Perform a Perl-style regular expression match */
     902     1653492 : static PHP_FUNCTION(preg_match)
     903             : {
     904     1653492 :         php_do_pcre_match(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     905     1653492 : }
     906             : /* }}} */
     907             : 
     908             : /* {{{ proto int preg_match_all(string pattern, string subject [, array &subpatterns [, int flags [, int offset]]])
     909             :    Perform a Perl-style global regular expression match */
     910         107 : static PHP_FUNCTION(preg_match_all)
     911             : {
     912         107 :         php_do_pcre_match(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     913         107 : }
     914             : /* }}} */
     915             : 
     916             : /* {{{ preg_get_backref
     917             :  */
     918         114 : static int preg_get_backref(char **str, int *backref)
     919             : {
     920         114 :         register char in_brace = 0;
     921         114 :         register char *walk = *str;
     922             : 
     923         114 :         if (walk[1] == 0)
     924           8 :                 return 0;
     925             : 
     926         106 :         if (*walk == '$' && walk[1] == '{') {
     927          14 :                 in_brace = 1;
     928          14 :                 walk++;
     929             :         }
     930         106 :         walk++;
     931             : 
     932         212 :         if (*walk >= '0' && *walk <= '9') {
     933         106 :                 *backref = *walk - '0';
     934         106 :                 walk++;
     935             :         } else
     936           0 :                 return 0;
     937             : 
     938         106 :         if (*walk && *walk >= '0' && *walk <= '9') {
     939           2 :                 *backref = *backref * 10 + *walk - '0';
     940           2 :                 walk++;
     941             :         }
     942             : 
     943         106 :         if (in_brace) {
     944          14 :                 if (*walk == 0 || *walk != '}')
     945           6 :                         return 0;
     946             :                 else
     947           8 :                         walk++;
     948             :         }
     949             : 
     950         100 :         *str = walk;
     951         100 :         return 1;
     952             : }
     953             : /* }}} */
     954             : 
     955             : /* {{{ preg_do_repl_func
     956             :  */
     957          52 : static zend_string *preg_do_repl_func(zval *function, char *subject, int *offsets, char **subpat_names, int count, unsigned char *mark)
     958             : {
     959             :         zend_string *result_str;
     960             :         zval             retval;                        /* Function return value */
     961             :         zval         args[1];                   /* Argument to pass to function */
     962             :         int                      i;
     963             : 
     964          52 :         array_init_size(&args[0], count + (mark ? 1 : 0));
     965          52 :         if (subpat_names) {
     966           3 :                 for (i = 0; i < count; i++) {
     967           2 :                         if (subpat_names[i]) {
     968           1 :                                 add_assoc_stringl(&args[0], subpat_names[i], &subject[offsets[i<<1]] , offsets[(i<<1)+1] - offsets[i<<1]);
     969             :                         }
     970           2 :                         add_next_index_stringl(&args[0], &subject[offsets[i<<1]], offsets[(i<<1)+1] - offsets[i<<1]);
     971             :                 }
     972             :         } else {
     973         133 :                 for (i = 0; i < count; i++) {
     974          82 :                         add_next_index_stringl(&args[0], &subject[offsets[i<<1]], offsets[(i<<1)+1] - offsets[i<<1]);
     975             :                 }
     976             :         }
     977          52 :         if (mark) {
     978           2 :                 add_assoc_string(&args[0], "MARK", (char *) mark);
     979             :         }
     980             : 
     981         155 :         if (call_user_function_ex(EG(function_table), NULL, function, &retval, 1, args, 0, NULL) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
     982          51 :                 result_str = zval_get_string(&retval);
     983          51 :                 zval_ptr_dtor(&retval);
     984             :         } else {
     985           1 :                 if (!EG(exception)) {
     986           0 :                         php_error_docref(NULL, E_WARNING, "Unable to call custom replacement function");
     987             :                 }
     988             : 
     989           2 :                 result_str = zend_string_init(&subject[offsets[0]], offsets[1] - offsets[0], 0);
     990             :         }
     991             : 
     992          52 :         zval_ptr_dtor(&args[0]);
     993             : 
     994          52 :         return result_str;
     995             : }
     996             : /* }}} */
     997             : 
     998             : /* {{{ php_pcre_replace
     999             :  */
    1000       25725 : PHPAPI zend_string *php_pcre_replace(zend_string *regex,
    1001             :                                                           zend_string *subject_str,
    1002             :                                                           char *subject, int subject_len,
    1003             :                                                           zval *replace_val, int is_callable_replace,
    1004             :                                                           int limit, int *replace_count)
    1005             : {
    1006             :         pcre_cache_entry        *pce;                       /* Compiled regular expression */
    1007             : 
    1008             :         /* Compile regex or get it from cache. */
    1009       25725 :         if ((pce = pcre_get_compiled_regex_cache(regex)) == NULL) {
    1010          11 :                 return NULL;
    1011             :         }
    1012             : 
    1013       25714 :         return php_pcre_replace_impl(pce, subject_str, subject, subject_len, replace_val,
    1014             :                 is_callable_replace, limit, replace_count);
    1015             : }
    1016             : /* }}} */
    1017             : 
    1018             : /* {{{ php_pcre_replace_impl() */
    1019       25726 : PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, zend_string *subject_str, char *subject, int subject_len, zval *replace_val,
    1020             :         int is_callable_replace, int limit, int *replace_count)
    1021             : {
    1022       25726 :         pcre_extra              *extra = pce->extra;/* Holds results of studying */
    1023             :         pcre_extra               extra_data;            /* Used locally for exec options */
    1024       25726 :         int                              exoptions = 0;         /* Execution options */
    1025       25726 :         int                              count = 0;                     /* Count of matched subpatterns */
    1026             :         int                             *offsets;                       /* Array of subpattern offsets */
    1027             :         char                    **subpat_names;         /* Array for named subpatterns */
    1028             :         int                              num_subpats;           /* Number of captured subpatterns */
    1029             :         int                              size_offsets;          /* Size of the offsets array */
    1030             :         int                              new_len;                       /* Length of needed storage */
    1031             :         int                              alloc_len;                     /* Actual allocated length */
    1032             :         int                              match_len;                     /* Length of the current match */
    1033             :         int                              backref;                       /* Backreference number */
    1034             :         int                              start_offset;          /* Where the new search starts */
    1035       25726 :         int                              g_notempty=0;          /* If the match should not be empty */
    1036       25726 :         int                              replace_len=0;         /* Length of replacement string */
    1037       25726 :         char                    *replace=NULL,          /* Replacement string */
    1038             :                                         *walkbuf,                       /* Location of current replacement in the result */
    1039             :                                         *walk,                          /* Used to walk the replacement string */
    1040             :                                         *match,                         /* The current match */
    1041             :                                         *piece,                         /* The current piece of subject */
    1042       25726 :                                         *replace_end=NULL,      /* End of replacement string */
    1043             :                                          walk_last;                     /* Last walked character */
    1044             :         int                              result_len;            /* Length of result */
    1045       25726 :         unsigned char   *mark = NULL;       /* Target for MARK name */
    1046             :         zend_string             *result;                        /* Result of replacement */
    1047       25726 :         zend_string     *eval_result=NULL;  /* Result of custom function */
    1048             :         ALLOCA_FLAG(use_heap);
    1049             : 
    1050       25726 :         if (extra == NULL) {
    1051           2 :                 extra_data.flags = PCRE_EXTRA_MATCH_LIMIT | PCRE_EXTRA_MATCH_LIMIT_RECURSION;
    1052           2 :                 extra = &extra_data;
    1053             :         }
    1054       25726 :         extra->match_limit = (unsigned long)PCRE_G(backtrack_limit);
    1055       25726 :         extra->match_limit_recursion = (unsigned long)PCRE_G(recursion_limit);
    1056             : 
    1057       25726 :         if (pce->preg_options & PREG_REPLACE_EVAL) {
    1058           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The /e modifier is no longer supported, use preg_replace_callback instead");
    1059           1 :                 return NULL;
    1060             :         }
    1061       25725 :         if (!is_callable_replace) {
    1062       25692 :                 replace = Z_STRVAL_P(replace_val);
    1063       25692 :                 replace_len = (int)Z_STRLEN_P(replace_val);
    1064       25692 :                 replace_end = replace + replace_len;
    1065             :         }
    1066             : 
    1067             :         /* Calculate the size of the offsets array, and allocate memory for it. */
    1068       25725 :         num_subpats = pce->capture_count + 1;
    1069       25725 :         size_offsets = num_subpats * 3;
    1070       25725 :         if (size_offsets <= 32) {
    1071       25722 :                 offsets = (int *)do_alloca(size_offsets * sizeof(int), use_heap);
    1072             :         } else {
    1073           3 :                 offsets = (int *)safe_emalloc(size_offsets, sizeof(int), 0);
    1074             :         }
    1075             : 
    1076             :         /*
    1077             :          * Build a mapping from subpattern numbers to their names. We will
    1078             :          * allocate the table only if there are any named subpatterns.
    1079             :          */
    1080       25725 :         subpat_names = NULL;
    1081       25725 :         if (pce->name_count > 0) {
    1082           1 :                 subpat_names = make_subpats_table(num_subpats, pce);
    1083           1 :                 if (!subpat_names) {
    1084           0 :                         return NULL;
    1085             :                 }
    1086             :         }
    1087             : 
    1088       25725 :         alloc_len = 0;
    1089       25725 :         result = NULL;
    1090             : 
    1091             :         /* Initialize */
    1092       25725 :         match = NULL;
    1093       25725 :         start_offset = 0;
    1094       25725 :         result_len = 0;
    1095       25725 :         PCRE_G(error_code) = PHP_PCRE_NO_ERROR;
    1096             : 
    1097             :         while (1) {
    1098             : #ifdef PCRE_EXTRA_MARK
    1099       31608 :                 extra->mark = &mark;
    1100       31608 :                 extra->flags |= PCRE_EXTRA_MARK;
    1101             : #endif
    1102             :                 /* Execute the regular expression. */
    1103       31608 :                 count = pcre_exec(pce->re, extra, subject, subject_len, start_offset,
    1104             :                                                   exoptions|g_notempty, offsets, size_offsets);
    1105             : 
    1106             :                 /* the string was already proved to be valid UTF-8 */
    1107       31608 :                 exoptions |= PCRE_NO_UTF8_CHECK;
    1108             : 
    1109             :                 /* Check for too many substrings condition. */
    1110       31608 :                 if (count == 0) {
    1111           0 :                         php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
    1112           0 :                         count = size_offsets/3;
    1113             :                 }
    1114             : 
    1115       31608 :                 piece = subject + start_offset;
    1116             : 
    1117       37491 :                 if (count > 0 && (limit == -1 || limit > 0)) {
    1118        5883 :                         if (replace_count) {
    1119        5883 :                                 ++*replace_count;
    1120             :                         }
    1121             :                         /* Set the match location in subject */
    1122        5883 :                         match = subject + offsets[0];
    1123             : 
    1124        5883 :                         new_len = result_len + offsets[0] - start_offset; /* part before the match */
    1125             :                         
    1126        5883 :                         if (is_callable_replace) {
    1127             :                                 /* Use custom function to get replacement string and its length. */
    1128          52 :                                 eval_result = preg_do_repl_func(replace_val, subject, offsets, subpat_names, count, mark);
    1129          52 :                                 new_len += (int)eval_result->len;
    1130             :                         } else { /* do regular substitution */
    1131        5831 :                                 walk = replace;
    1132        5831 :                                 walk_last = 0;
    1133       17580 :                                 while (walk < replace_end) {
    1134        5918 :                                         if ('\\' == *walk || '$' == *walk) {
    1135          57 :                                                 if (walk_last == '\\') {
    1136           0 :                                                         walk++;
    1137           0 :                                                         walk_last = 0;
    1138           0 :                                                         continue;
    1139             :                                                 }
    1140          57 :                                                 if (preg_get_backref(&walk, &backref)) {
    1141          50 :                                                         if (backref < count)
    1142          49 :                                                                 new_len += offsets[(backref<<1)+1] - offsets[backref<<1];
    1143          50 :                                                         continue;
    1144             :                                                 }
    1145             :                                         }
    1146        5868 :                                         new_len++;
    1147        5868 :                                         walk++;
    1148        5868 :                                         walk_last = walk[-1];
    1149             :                                 }
    1150             :                         }
    1151             : 
    1152        5883 :                         if (new_len >= alloc_len) {
    1153         663 :                                 if (alloc_len == 0) {
    1154         657 :                                         alloc_len = 2 * subject_len;
    1155         657 :                                         if (new_len >= alloc_len) {
    1156           7 :                                                 alloc_len = alloc_len + 2 * new_len;
    1157             :                                         }
    1158        1314 :                                         result = zend_string_alloc(alloc_len, 0);
    1159             :                                 } else {
    1160           6 :                                         alloc_len = alloc_len + 2 * new_len;
    1161          12 :                                         result = zend_string_realloc(result, alloc_len, 0);
    1162             :                                 }
    1163             :                         }
    1164             :                         /* copy the part of the string before the match */
    1165        5883 :                         memcpy(&result->val[result_len], piece, match-piece);
    1166        5883 :                         result_len += (int)(match-piece);
    1167             : 
    1168             :                         /* copy replacement and backrefs */
    1169        5883 :                         walkbuf = result->val + result_len;
    1170             :                         
    1171             :                         /* If using custom function, copy result to the buffer and clean up. */
    1172        5883 :                         if (is_callable_replace) {
    1173          52 :                                 memcpy(walkbuf, eval_result->val, eval_result->len);
    1174          52 :                                 result_len += (int)eval_result->len;
    1175          52 :                                 if (eval_result) zend_string_release(eval_result);
    1176             :                         } else { /* do regular backreference copying */
    1177        5831 :                                 walk = replace;
    1178        5831 :                                 walk_last = 0;
    1179       17580 :                                 while (walk < replace_end) {
    1180        5918 :                                         if ('\\' == *walk || '$' == *walk) {
    1181          57 :                                                 if (walk_last == '\\') {
    1182           0 :                                                         *(walkbuf-1) = *walk++;
    1183           0 :                                                         walk_last = 0;
    1184           0 :                                                         continue;
    1185             :                                                 }
    1186          57 :                                                 if (preg_get_backref(&walk, &backref)) {
    1187          50 :                                                         if (backref < count) {
    1188          49 :                                                                 match_len = offsets[(backref<<1)+1] - offsets[backref<<1];
    1189          49 :                                                                 memcpy(walkbuf, subject + offsets[backref<<1], match_len);
    1190          49 :                                                                 walkbuf += match_len;
    1191             :                                                         }
    1192          50 :                                                         continue;
    1193             :                                                 }
    1194             :                                         }
    1195        5868 :                                         *walkbuf++ = *walk++;
    1196        5868 :                                         walk_last = walk[-1];
    1197             :                                 }
    1198        5831 :                                 *walkbuf = '\0';
    1199             :                                 /* increment the result length by how much we've added to the string */
    1200        5831 :                                 result_len += (int)(walkbuf - (result->val + result_len));
    1201             :                         }
    1202             : 
    1203        5883 :                         if (limit != -1)
    1204          32 :                                 limit--;
    1205             : 
    1206       25725 :                 } else if (count == PCRE_ERROR_NOMATCH || limit == 0) {
    1207             :                         /* If we previously set PCRE_NOTEMPTY after a null match,
    1208             :                            this is not necessarily the end. We need to advance
    1209             :                            the start offset, and continue. Fudge the offset values
    1210             :                            to achieve this, unless we're already at the end of the string. */
    1211       25721 :                         if (g_notempty != 0 && start_offset < subject_len) {
    1212           0 :                                 offsets[0] = start_offset;
    1213           0 :                                 offsets[1] = start_offset + 1;
    1214           0 :                                 memcpy(&result->val[result_len], piece, 1);
    1215           0 :                                 result_len++;
    1216             :                         } else {
    1217       25721 :                                 if (!result && subject_str) {
    1218       25064 :                                         result = zend_string_copy(subject_str);
    1219       25064 :                                         break;
    1220             :                                 }
    1221         657 :                                 new_len = result_len + subject_len - start_offset;
    1222         657 :                                 if (new_len > alloc_len) {
    1223           0 :                                         alloc_len = new_len; /* now we know exactly how long it is */
    1224           0 :                                         result = zend_string_realloc(result, alloc_len, 0);
    1225             :                                 }
    1226             :                                 /* stick that last bit of string on our output */
    1227         657 :                                 memcpy(&result->val[result_len], piece, subject_len - start_offset);
    1228         657 :                                 result_len += subject_len - start_offset;
    1229         657 :                                 result->val[result_len] = '\0';
    1230         657 :                                 result->len = result_len;
    1231         657 :                                 break;
    1232             :                         }
    1233             :                 } else {
    1234           4 :                         pcre_handle_exec_error(count);
    1235           4 :                         if (result) {
    1236             :                                 zend_string_free(result);
    1237           0 :                                 result = NULL;
    1238             :                         }
    1239           4 :                         break;
    1240             :                 }
    1241             : 
    1242             :                 /* If we have matched an empty string, mimic what Perl's /g options does.
    1243             :                    This turns out to be rather cunning. First we set PCRE_NOTEMPTY and try
    1244             :                    the match again at the same point. If this fails (picked up above) we
    1245             :                    advance to the next character. */
    1246        5883 :                 g_notempty = (offsets[1] == offsets[0])? PCRE_NOTEMPTY | PCRE_ANCHORED : 0;
    1247             : 
    1248             :                 /* Advance to the next piece. */
    1249        5883 :                 start_offset = offsets[1];
    1250        5883 :         }
    1251             : 
    1252       25725 :         if (size_offsets <= 32) {
    1253       25722 :                 free_alloca(offsets, use_heap);
    1254             :         } else {
    1255           3 :                 efree(offsets);
    1256             :         }
    1257       25725 :         if (subpat_names) {
    1258           1 :                 efree(subpat_names);
    1259             :         }
    1260             : 
    1261       25725 :         return result;
    1262             : }
    1263             : /* }}} */
    1264             : 
    1265             : /* {{{ php_replace_in_subject
    1266             :  */
    1267       25683 : static zend_string *php_replace_in_subject(zval *regex, zval *replace, zval *subject, int limit, int is_callable_replace, int *replace_count)
    1268             : {
    1269             :         zval            *regex_entry,
    1270       25683 :                                 *replace_entry = NULL,
    1271             :                                 *replace_value,
    1272             :                                  empty_replace;
    1273             :         zend_string *result;
    1274       25683 :         zend_string     *subject_str = zval_get_string(subject);
    1275             :         uint32_t replace_idx;
    1276             : 
    1277             :         /* FIXME: This might need to be changed to STR_EMPTY_ALLOC(). Check if this zval could be dtor()'ed somehow */
    1278       25683 :         ZVAL_EMPTY_STRING(&empty_replace);
    1279             : 
    1280             :         /* If regex is an array */
    1281       25683 :         if (Z_TYPE_P(regex) == IS_ARRAY) {
    1282          21 :                 replace_value = replace;
    1283          21 :                 replace_idx = 0;
    1284             : 
    1285             :                 /* For each entry in the regex array, get the entry */
    1286         146 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(regex), regex_entry) {
    1287             :                         /* Make sure we're dealing with strings. */
    1288          63 :                         zend_string *regex_str = zval_get_string(regex_entry);
    1289             : 
    1290             :                         /* If replace is an array and not a callable construct */
    1291          63 :                         if (Z_TYPE_P(replace) == IS_ARRAY && !is_callable_replace) {
    1292             :                                 /* Get current entry */
    1293          48 :                                 replace_entry = NULL;
    1294          96 :                                 while (replace_idx < Z_ARRVAL_P(replace)->nNumUsed) {
    1295          92 :                                         if (Z_TYPE(Z_ARRVAL_P(replace)->arData[replace_idx].val) != IS_UNUSED) {
    1296          46 :                                                 replace_entry = &Z_ARRVAL_P(replace)->arData[replace_idx].val;
    1297          46 :                                                 break;
    1298             :                                         }
    1299           0 :                                         replace_idx++;
    1300             :                                 }
    1301          48 :                                 if (replace_entry != NULL) {
    1302          46 :                                         if (!is_callable_replace) {
    1303          46 :                                                 convert_to_string_ex(replace_entry);
    1304             :                                         }
    1305          46 :                                         replace_value = replace_entry;
    1306          46 :                                         replace_idx++;
    1307             :                                 } else {
    1308             :                                         /* We've run out of replacement strings, so use an empty one */
    1309           2 :                                         replace_value = &empty_replace;
    1310             :                                 }
    1311             :                         }
    1312             : 
    1313             :                         /* Do the actual replacement and put the result back into subject_str
    1314             :                            for further replacements. */
    1315          63 :                         if ((result = php_pcre_replace(regex_str,
    1316             :                                                                                    subject_str,
    1317             :                                                                                    subject_str->val,
    1318             :                                                                                    (int)subject_str->len,
    1319             :                                                                                    replace_value,
    1320             :                                                                                    is_callable_replace,
    1321             :                                                                                    limit,
    1322             :                                                                                    replace_count)) != NULL) {
    1323             :                                 zend_string_release(subject_str);
    1324          62 :                                 subject_str = result;
    1325             :                         } else {
    1326             :                                 zend_string_release(subject_str);
    1327             :                                 zend_string_release(regex_str);
    1328           1 :                                 return NULL;
    1329             :                         }
    1330             : 
    1331             :                         zend_string_release(regex_str);
    1332             :                 } ZEND_HASH_FOREACH_END();
    1333             : 
    1334          20 :                 return subject_str;
    1335             :         } else {
    1336       25662 :                 result = php_pcre_replace(Z_STR_P(regex),
    1337             :                                                                   subject_str,
    1338             :                                                                   subject_str->val,
    1339             :                                                                   (int)subject_str->len,
    1340             :                                                                   replace,
    1341             :                                                                   is_callable_replace,
    1342             :                                                                   limit,
    1343             :                                                                   replace_count);
    1344             :                 zend_string_release(subject_str);
    1345       25662 :                 return result;
    1346             :         }
    1347             : }
    1348             : /* }}} */
    1349             : 
    1350             : /* {{{ preg_replace_impl
    1351             :  */
    1352       25690 : static void preg_replace_impl(INTERNAL_FUNCTION_PARAMETERS, int is_callable_replace, int is_filter)
    1353             : {
    1354             :         zval                *regex,
    1355             :                                     *replace,
    1356             :                                     *subject,
    1357             :                                     *subject_entry,
    1358       25690 :                                     *zcount = NULL;
    1359       25690 :         int                              limit_val = -1;
    1360       25690 :         zend_long                limit = -1;
    1361             :         zend_string             *result;
    1362             :         zend_string             *string_key;
    1363             :         zend_ulong               num_key;
    1364             :         zend_string             *callback_name;
    1365       25690 :         int                              replace_count=0, old_replace_count;
    1366             : 
    1367             : #ifndef FAST_ZPP
    1368             :         /* Get function parameters and do error-checking. */
    1369             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zzz|lz/", &regex, &replace, &subject, &limit, &zcount) == FAILURE) {
    1370             :                 return;
    1371             :         }
    1372             : #else
    1373       25690 :         ZEND_PARSE_PARAMETERS_START(3, 5)
    1374       25681 :                 Z_PARAM_ZVAL(regex)
    1375       25681 :                 Z_PARAM_ZVAL(replace)
    1376       25681 :                 Z_PARAM_ZVAL(subject)
    1377       25681 :                 Z_PARAM_OPTIONAL
    1378       25733 :                 Z_PARAM_LONG(limit)
    1379          33 :                 Z_PARAM_ZVAL_EX(zcount, 0, 1)
    1380       25690 :         ZEND_PARSE_PARAMETERS_END();
    1381             : #endif
    1382             : 
    1383       51329 :         if (!is_callable_replace && Z_TYPE_P(replace) == IS_ARRAY && Z_TYPE_P(regex) != IS_ARRAY) {
    1384           3 :                 php_error_docref(NULL, E_WARNING, "Parameter mismatch, pattern is a string while replacement is an array");
    1385           3 :                 RETURN_FALSE;
    1386             :         }
    1387             : 
    1388       77021 :         if (Z_TYPE_P(replace) != IS_ARRAY && (Z_TYPE_P(replace) != IS_OBJECT || !is_callable_replace)) {
    1389       25690 :                 SEPARATE_ZVAL(replace);
    1390       51330 :                 convert_to_string_ex(replace);
    1391             :         }
    1392       25674 :         if (is_callable_replace) {
    1393          37 :                 if (!zend_is_callable(replace, 0, &callback_name)) {
    1394           3 :                         php_error_docref(NULL, E_WARNING, "Requires argument 2, '%s', to be a valid callback", callback_name->val);
    1395           3 :                         zend_string_release(callback_name);
    1396           3 :                         ZVAL_DUP(return_value, subject);
    1397           3 :                         return;
    1398             :                 }
    1399          34 :                 zend_string_release(callback_name);
    1400             :         }
    1401             : 
    1402       25671 :         if (ZEND_NUM_ARGS() > 3) {
    1403          22 :                 limit_val = (int)limit;
    1404             :         }
    1405             : 
    1406       51342 :         if (Z_TYPE_P(regex) != IS_ARRAY) {
    1407       25695 :                 SEPARATE_ZVAL(regex);
    1408       51323 :                 convert_to_string_ex(regex);
    1409             :         }
    1410             : 
    1411             :         /* if subject is an array */
    1412       51340 :         if (Z_TYPE_P(subject) == IS_ARRAY) {
    1413           6 :                 array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(subject)));
    1414             : 
    1415             :                 /* For each subject entry, convert it to string, then perform replacement
    1416             :                    and add the result to the return_value array. */
    1417          44 :                 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(subject), num_key, string_key, subject_entry) {
    1418          19 :                         old_replace_count = replace_count;
    1419          19 :                         if ((result = php_replace_in_subject(regex, replace, subject_entry, limit_val, is_callable_replace, &replace_count)) != NULL) {
    1420          36 :                                 if (!is_filter || replace_count > old_replace_count) {
    1421             :                                         /* Add to return array */
    1422          17 :                                         if (string_key) {
    1423           1 :                                                 add_assoc_str_ex(return_value, string_key->val, string_key->len, result);
    1424             :                                         } else {
    1425          16 :                                                 add_index_str(return_value, num_key, result);
    1426             :                                         }
    1427             :                                 } else {
    1428             :                                         zend_string_release(result);
    1429             :                                 }
    1430             :                         }
    1431             :                 } ZEND_HASH_FOREACH_END();
    1432             :         } else {        /* if subject is not an array */
    1433       25664 :                 old_replace_count = replace_count;
    1434       25664 :                 if ((result = php_replace_in_subject(regex, replace, subject, limit_val, is_callable_replace, &replace_count)) != NULL) {
    1435       51296 :                         if (!is_filter || replace_count > old_replace_count) {
    1436       25648 :                                 RETVAL_STR(result);
    1437             :                         } else {
    1438             :                                 zend_string_release(result);
    1439             :                         }
    1440             :                 }
    1441             :         }
    1442       25670 :         if (ZEND_NUM_ARGS() > 4) {
    1443           8 :                 zval_dtor(zcount);
    1444           8 :                 ZVAL_LONG(zcount, replace_count);
    1445             :         }
    1446             : 
    1447             : }
    1448             : /* }}} */
    1449             : 
    1450             : /* {{{ proto mixed preg_replace(mixed regex, mixed replace, mixed subject [, int limit [, int &count]])
    1451             :    Perform Perl-style regular expression replacement. */
    1452       25644 : static PHP_FUNCTION(preg_replace)
    1453             : {
    1454       25644 :         preg_replace_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
    1455       25641 : }
    1456             : /* }}} */
    1457             : 
    1458             : /* {{{ proto mixed preg_replace_callback(mixed regex, mixed callback, mixed subject [, int limit [, int &count]])
    1459             :    Perform Perl-style regular expression replacement using replacement callback. */
    1460          45 : static PHP_FUNCTION(preg_replace_callback)
    1461             : {
    1462          45 :         preg_replace_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
    1463          45 : }
    1464             : /* }}} */
    1465             : 
    1466             : /* {{{ proto mixed preg_filter(mixed regex, mixed replace, mixed subject [, int limit [, int &count]])
    1467             :    Perform Perl-style regular expression replacement and only return matches. */
    1468           1 : static PHP_FUNCTION(preg_filter)
    1469             : {
    1470           1 :         preg_replace_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
    1471           1 : }
    1472             : /* }}} */
    1473             : 
    1474             : /* {{{ proto array preg_split(string pattern, string subject [, int limit [, int flags]])
    1475             :    Split string into an array using a perl-style regular expression as a delimiter */
    1476        2451 : static PHP_FUNCTION(preg_split)
    1477             : {
    1478             :         zend_string                     *regex;                 /* Regular expression */
    1479             :         zend_string                     *subject;               /* String to match against */
    1480        2451 :         zend_long                        limit_val = -1;/* Integer value of limit */
    1481        2451 :         zend_long                        flags = 0;             /* Match control flags */
    1482             :         pcre_cache_entry        *pce;                   /* Compiled regular expression */
    1483             : 
    1484             :         /* Get function parameters and do error checking */
    1485             : #ifndef FAST_ZPP
    1486             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|ll", &regex,
    1487             :                                                           &subject, &limit_val, &flags) == FAILURE) {
    1488             :                 RETURN_FALSE;
    1489             :         }
    1490             : #else
    1491        2451 :         ZEND_PARSE_PARAMETERS_START(2, 4)
    1492        7341 :                 Z_PARAM_STR(regex)
    1493        7335 :                 Z_PARAM_STR(subject)
    1494        2443 :                 Z_PARAM_OPTIONAL
    1495        2483 :                 Z_PARAM_LONG(limit_val)
    1496          52 :                 Z_PARAM_LONG(flags)
    1497        2451 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
    1498             : #endif
    1499             : 
    1500             :         /* Compile regex or get it from cache. */
    1501        2443 :         if ((pce = pcre_get_compiled_regex_cache(regex)) == NULL) {
    1502           5 :                 RETURN_FALSE;
    1503             :         }
    1504             : 
    1505        2438 :         php_pcre_split_impl(pce, subject->val, (int)subject->len, return_value, (int)limit_val, flags);
    1506             : }
    1507             : /* }}} */
    1508             : 
    1509             : /* {{{ php_pcre_split
    1510             :  */
    1511        2453 : PHPAPI void php_pcre_split_impl(pcre_cache_entry *pce, char *subject, int subject_len, zval *return_value,
    1512             :         zend_long limit_val, zend_long flags)
    1513             : {
    1514        2453 :         pcre_extra              *extra = pce->extra;/* Holds results of studying */
    1515        2453 :         pcre                    *re_bump = NULL;        /* Regex instance for empty matches */
    1516        2453 :         pcre_extra              *extra_bump = NULL;     /* Almost dummy */
    1517             :         pcre_extra               extra_data;            /* Used locally for exec options */
    1518             :         int                             *offsets;                       /* Array of subpattern offsets */
    1519             :         int                              size_offsets;          /* Size of the offsets array */
    1520        2453 :         int                              exoptions = 0;         /* Execution options */
    1521        2453 :         int                              count = 0;                     /* Count of matched subpatterns */
    1522             :         int                              start_offset;          /* Where the new search starts */
    1523             :         int                              next_offset;           /* End of the last delimiter match + 1 */
    1524        2453 :         int                              g_notempty = 0;        /* If the match should not be empty */
    1525             :         char                    *last_match;            /* Location of last match */
    1526             :         int                              no_empty;                      /* If NO_EMPTY flag is set */
    1527             :         int                              delim_capture;         /* If delimiters should be captured */
    1528             :         int                              offset_capture;        /* If offsets should be captured */
    1529             :         zval                     tmp;
    1530             :         ALLOCA_FLAG(use_heap);
    1531             : 
    1532        2453 :         no_empty = flags & PREG_SPLIT_NO_EMPTY;
    1533        2453 :         delim_capture = flags & PREG_SPLIT_DELIM_CAPTURE;
    1534        2453 :         offset_capture = flags & PREG_SPLIT_OFFSET_CAPTURE;
    1535             : 
    1536        2453 :         if (limit_val == 0) {
    1537           1 :                 limit_val = -1;
    1538             :         }
    1539             : 
    1540        2453 :         if (extra == NULL) {
    1541           7 :                 extra_data.flags = PCRE_EXTRA_MATCH_LIMIT | PCRE_EXTRA_MATCH_LIMIT_RECURSION;
    1542           7 :                 extra = &extra_data;
    1543             :         }
    1544        2453 :         extra->match_limit = (unsigned long)PCRE_G(backtrack_limit);
    1545        2453 :         extra->match_limit_recursion = (unsigned long)PCRE_G(recursion_limit);
    1546             : #ifdef PCRE_EXTRA_MARK
    1547        2453 :         extra->flags &= ~PCRE_EXTRA_MARK;
    1548             : #endif
    1549             : 
    1550             :         /* Initialize return value */
    1551        2453 :         array_init(return_value);
    1552             : 
    1553             :         /* Calculate the size of the offsets array, and allocate memory for it. */
    1554        2453 :         size_offsets = (pce->capture_count + 1) * 3;
    1555        2453 :         if (size_offsets <= 32) {
    1556        2453 :                 offsets = (int *)do_alloca(size_offsets * sizeof(int), use_heap);
    1557             :         } else {
    1558           0 :                 offsets = (int *)safe_emalloc(size_offsets, sizeof(int), 0);
    1559             :         }
    1560             : 
    1561             :         /* Start at the beginning of the string */
    1562        2453 :         start_offset = 0;
    1563        2453 :         next_offset = 0;
    1564        2453 :         last_match = subject;
    1565        2453 :         PCRE_G(error_code) = PHP_PCRE_NO_ERROR;
    1566             : 
    1567             :         /* Get next piece if no limit or limit not yet reached and something matched*/
    1568        9111 :         while ((limit_val == -1 || limit_val > 1)) {
    1569        6656 :                 count = pcre_exec(pce->re, extra, subject,
    1570             :                                                   subject_len, start_offset,
    1571             :                                                   exoptions|g_notempty, offsets, size_offsets);
    1572             : 
    1573             :                 /* the string was already proved to be valid UTF-8 */
    1574        6656 :                 exoptions |= PCRE_NO_UTF8_CHECK;
    1575             : 
    1576             :                 /* Check for too many substrings condition. */
    1577        6656 :                 if (count == 0) {
    1578           0 :                         php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
    1579           0 :                         count = size_offsets/3;
    1580             :                 }
    1581             : 
    1582             :                 /* If something matched */
    1583        6656 :                 if (count > 0) {
    1584        4147 :                         if (!no_empty || &subject[offsets[0]] != last_match) {
    1585             : 
    1586        4088 :                                 if (offset_capture) {
    1587             :                                         /* Add (match, offset) pair to the return value */
    1588          26 :                                         add_offset_pair(return_value, last_match, (int)(&subject[offsets[0]]-last_match), next_offset, NULL);
    1589             :                                 } else {
    1590             :                                         /* Add the piece to the return value */
    1591        8124 :                                         ZVAL_STRINGL(&tmp, last_match, &subject[offsets[0]]-last_match);
    1592        4062 :                                         zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
    1593             :                                 }
    1594             : 
    1595             :                                 /* One less left to do */
    1596        4088 :                                 if (limit_val != -1)
    1597           1 :                                         limit_val--;
    1598             :                         }
    1599             : 
    1600        4147 :                         last_match = &subject[offsets[1]];
    1601        4147 :                         next_offset = offsets[1];
    1602             : 
    1603        4147 :                         if (delim_capture) {
    1604             :                                 int i, match_len;
    1605          62 :                                 for (i = 1; i < count; i++) {
    1606          31 :                                         match_len = offsets[(i<<1)+1] - offsets[i<<1];
    1607             :                                         /* If we have matched a delimiter */
    1608          31 :                                         if (!no_empty || match_len > 0) {
    1609          21 :                                                 if (offset_capture) {
    1610          10 :                                                         add_offset_pair(return_value, &subject[offsets[i<<1]], match_len, offsets[i<<1], NULL);
    1611             :                                                 } else {
    1612          22 :                                                         ZVAL_STRINGL(&tmp, &subject[offsets[i<<1]], match_len);
    1613          11 :                                                         zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
    1614             :                                                 }
    1615             :                                         }
    1616             :                                 }
    1617             :                         }
    1618        2509 :                 } else if (count == PCRE_ERROR_NOMATCH) {
    1619             :                         /* If we previously set PCRE_NOTEMPTY after a null match,
    1620             :                            this is not necessarily the end. We need to advance
    1621             :                            the start offset, and continue. Fudge the offset values
    1622             :                            to achieve this, unless we're already at the end of the string. */
    1623        2508 :                         if (g_notempty != 0 && start_offset < subject_len) {
    1624          58 :                                 if (pce->compile_options & PCRE_UTF8) {
    1625          12 :                                         if (re_bump == NULL) {
    1626             :                                                 int dummy;
    1627           2 :                                                 zend_string *regex = zend_string_init("/./us", sizeof("/./us")-1, 0);
    1628           2 :                                                 re_bump = pcre_get_compiled_regex(regex, &extra_bump, &dummy);
    1629             :                                                 zend_string_release(regex);
    1630           2 :                                                 if (re_bump == NULL) {
    1631           0 :                                                         RETURN_FALSE;
    1632             :                                                 }
    1633             :                                         }
    1634          12 :                                         count = pcre_exec(re_bump, extra_bump, subject,
    1635             :                                                           subject_len, start_offset,
    1636             :                                                           exoptions, offsets, size_offsets);
    1637          12 :                                         if (count < 1) {
    1638           0 :                                                 php_error_docref(NULL, E_WARNING, "Unknown error");
    1639           0 :                                                 RETURN_FALSE;
    1640             :                                         }
    1641             :                                 } else {
    1642          46 :                                         offsets[0] = start_offset;
    1643          46 :                                         offsets[1] = start_offset + 1;
    1644             :                                 }
    1645             :                         } else
    1646             :                                 break;
    1647             :                 } else {
    1648           1 :                         pcre_handle_exec_error(count);
    1649           1 :                         break;
    1650             :                 }
    1651             : 
    1652             :                 /* If we have matched an empty string, mimic what Perl's /g options does.
    1653             :                    This turns out to be rather cunning. First we set PCRE_NOTEMPTY and try
    1654             :                    the match again at the same point. If this fails (picked up above) we
    1655             :                    advance to the next character. */
    1656        4205 :                 g_notempty = (offsets[1] == offsets[0])? PCRE_NOTEMPTY | PCRE_ANCHORED : 0;
    1657             : 
    1658             :                 /* Advance to the position right after the last full match */
    1659        4205 :                 start_offset = offsets[1];
    1660             :         }
    1661             : 
    1662             : 
    1663        2453 :         start_offset = (int)(last_match - subject); /* the offset might have been incremented, but without further successful matches */
    1664             : 
    1665        2453 :         if (!no_empty || start_offset < subject_len)
    1666             :         {
    1667        2445 :                 if (offset_capture) {
    1668             :                         /* Add the last (match, offset) pair to the return value */
    1669           5 :                         add_offset_pair(return_value, &subject[start_offset], subject_len - start_offset, start_offset, NULL);
    1670             :                 } else {
    1671             :                         /* Add the last piece to the return value */
    1672        4880 :                         ZVAL_STRINGL(&tmp, last_match, subject + subject_len - last_match);
    1673        2440 :                         zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
    1674             :                 }
    1675             :         }
    1676             : 
    1677             : 
    1678             :         /* Clean up */
    1679        2453 :         if (size_offsets <= 32) {
    1680        2453 :                 free_alloca(offsets, use_heap);
    1681             :         } else {
    1682           0 :                 efree(offsets);
    1683             :         }
    1684             : }
    1685             : /* }}} */
    1686             : 
    1687             : /* {{{ proto string preg_quote(string str [, string delim_char])
    1688             :    Quote regular expression characters plus an optional character */
    1689        8254 : static PHP_FUNCTION(preg_quote)
    1690             : {
    1691             :         size_t           in_str_len;
    1692             :         char    *in_str;                /* Input string argument */
    1693             :         char    *in_str_end;    /* End of the input string */
    1694        8254 :         size_t           delim_len = 0;
    1695        8254 :         char    *delim = NULL;  /* Additional delimiter argument */
    1696             :         zend_string     *out_str;       /* Output string with quoted characters */
    1697             :         char    *p,                             /* Iterator for input string */
    1698             :                         *q,                             /* Iterator for output string */
    1699        8254 :                          delim_char=0,  /* Delimiter character to be quoted */
    1700             :                          c;                             /* Current character */
    1701        8254 :         zend_bool quote_delim = 0; /* Whether to quote additional delim char */
    1702             : 
    1703             :         /* Get the arguments and check for errors */
    1704             : #ifndef FAST_ZPP
    1705             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &in_str, &in_str_len,
    1706             :                                                           &delim, &delim_len) == FAILURE) {
    1707             :                 return;
    1708             :         }
    1709             : #else
    1710        8254 :         ZEND_PARSE_PARAMETERS_START(1, 2)
    1711       24753 :                 Z_PARAM_STRING(in_str, in_str_len)
    1712        8249 :                 Z_PARAM_OPTIONAL
    1713       24739 :                 Z_PARAM_STRING(delim, delim_len)
    1714        8254 :         ZEND_PARSE_PARAMETERS_END();
    1715             : #endif
    1716             : 
    1717        8249 :         in_str_end = in_str + in_str_len;
    1718             : 
    1719             :         /* Nothing to do if we got an empty string */
    1720        8249 :         if (in_str == in_str_end) {
    1721           5 :                 RETURN_EMPTY_STRING();
    1722             :         }
    1723             : 
    1724        8244 :         if (delim && *delim) {
    1725        8241 :                 delim_char = delim[0];
    1726        8241 :                 quote_delim = 1;
    1727             :         }
    1728             : 
    1729             :         /* Allocate enough memory so that even if each character
    1730             :            is quoted, we won't run out of room */
    1731       16488 :         out_str = zend_string_safe_alloc(4, in_str_len, 0, 0);
    1732             : 
    1733             :         /* Go through the string and quote necessary characters */
    1734     7536760 :         for (p = in_str, q = out_str->val; p != in_str_end; p++) {
    1735     7528516 :                 c = *p;
    1736     7528516 :                 switch(c) {
    1737             :                         case '.':
    1738             :                         case '\\':
    1739             :                         case '+':
    1740             :                         case '*':
    1741             :                         case '?':
    1742             :                         case '[':
    1743             :                         case '^':
    1744             :                         case ']':
    1745             :                         case '$':
    1746             :                         case '(':
    1747             :                         case ')':
    1748             :                         case '{':
    1749             :                         case '}':
    1750             :                         case '=':
    1751             :                         case '!':
    1752             :                         case '>':
    1753             :                         case '<':
    1754             :                         case '|':
    1755             :                         case ':':
    1756             :                         case '-':
    1757     1066460 :                                 *q++ = '\\';
    1758     1066460 :                                 *q++ = c;
    1759     1066460 :                                 break;
    1760             : 
    1761             :                         case '\0':
    1762        1031 :                                 *q++ = '\\';
    1763        1031 :                                 *q++ = '0';
    1764        1031 :                                 *q++ = '0';
    1765        1031 :                                 *q++ = '0';
    1766        1031 :                                 break;
    1767             : 
    1768             :                         default:
    1769     6461025 :                                 if (quote_delim && c == delim_char)
    1770       15053 :                                         *q++ = '\\';
    1771     6461025 :                                 *q++ = c;
    1772             :                                 break;
    1773             :                 }
    1774             :         }
    1775        8244 :         *q = '\0';
    1776             : 
    1777             :         /* Reallocate string and return it */
    1778       16488 :         out_str = zend_string_realloc(out_str, q - out_str->val, 0);
    1779        8244 :         RETURN_STR(out_str);
    1780             : }
    1781             : /* }}} */
    1782             : 
    1783             : /* {{{ proto array preg_grep(string regex, array input [, int flags])
    1784             :    Searches array and returns entries which match regex */
    1785          29 : static PHP_FUNCTION(preg_grep)
    1786             : {
    1787             :         zend_string                     *regex;                 /* Regular expression */
    1788             :         zval                            *input;                 /* Input array */
    1789          29 :         zend_long                        flags = 0;             /* Match control flags */
    1790             :         pcre_cache_entry        *pce;                   /* Compiled regular expression */
    1791             : 
    1792             :         /* Get arguments and do error checking */
    1793             : #ifndef FAST_ZPP
    1794             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sa|l", &regex,
    1795             :                                                           &input, &flags) == FAILURE) {
    1796             :                 return;
    1797             :         }
    1798             : #else
    1799          29 :         ZEND_PARSE_PARAMETERS_START(2, 3)
    1800          75 :                 Z_PARAM_STR(regex)
    1801          69 :                 Z_PARAM_ARRAY(input)
    1802          20 :                 Z_PARAM_OPTIONAL
    1803          26 :                 Z_PARAM_LONG(flags)
    1804          29 :         ZEND_PARSE_PARAMETERS_END();
    1805             : #endif
    1806             : 
    1807             :         /* Compile regex or get it from cache. */
    1808          20 :         if ((pce = pcre_get_compiled_regex_cache(regex)) == NULL) {
    1809           5 :                 RETURN_FALSE;
    1810             :         }
    1811             : 
    1812          15 :         php_pcre_grep_impl(pce, input, return_value, flags);
    1813             : }
    1814             : /* }}} */
    1815             : 
    1816          15 : PHPAPI void  php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return_value, zend_long flags) /* {{{ */
    1817             : {
    1818             :         zval                *entry;                             /* An entry in the input array */
    1819          15 :         pcre_extra              *extra = pce->extra;/* Holds results of studying */
    1820             :         pcre_extra               extra_data;            /* Used locally for exec options */
    1821             :         int                             *offsets;                       /* Array of subpattern offsets */
    1822             :         int                              size_offsets;          /* Size of the offsets array */
    1823          15 :         int                              count = 0;                     /* Count of matched subpatterns */
    1824             :         zend_string             *string_key;
    1825             :         zend_ulong               num_key;
    1826             :         zend_bool                invert;                        /* Whether to return non-matching
    1827             :                                                                                    entries */
    1828             :         ALLOCA_FLAG(use_heap);
    1829             : 
    1830          15 :         invert = flags & PREG_GREP_INVERT ? 1 : 0;
    1831             : 
    1832          15 :         if (extra == NULL) {
    1833           0 :                 extra_data.flags = PCRE_EXTRA_MATCH_LIMIT | PCRE_EXTRA_MATCH_LIMIT_RECURSION;
    1834           0 :                 extra = &extra_data;
    1835             :         }
    1836          15 :         extra->match_limit = (unsigned long)PCRE_G(backtrack_limit);
    1837          15 :         extra->match_limit_recursion = (unsigned long)PCRE_G(recursion_limit);
    1838             : #ifdef PCRE_EXTRA_MARK
    1839          15 :         extra->flags &= ~PCRE_EXTRA_MARK;
    1840             : #endif
    1841             : 
    1842             :         /* Calculate the size of the offsets array, and allocate memory for it. */
    1843          15 :         size_offsets = (pce->capture_count + 1) * 3;
    1844          15 :         if (size_offsets <= 32) {
    1845          15 :                 offsets = (int *)do_alloca(size_offsets * sizeof(int), use_heap);
    1846             :         } else {
    1847           0 :                 offsets = (int *)safe_emalloc(size_offsets, sizeof(int), 0);
    1848             :         }
    1849             : 
    1850             :         /* Initialize return array */
    1851          15 :         array_init(return_value);
    1852             : 
    1853          15 :         PCRE_G(error_code) = PHP_PCRE_NO_ERROR;
    1854             : 
    1855             :         /* Go through the input array */
    1856         171 :         ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input), num_key, string_key, entry) {
    1857          78 :                 zend_string *subject_str = zval_get_string(entry);
    1858             : 
    1859             :                 /* Perform the match */
    1860          78 :                 count = pcre_exec(pce->re, extra, subject_str->val,
    1861             :                                                   (int)subject_str->len, 0,
    1862             :                                                   0, offsets, size_offsets);
    1863             : 
    1864             :                 /* Check for too many substrings condition. */
    1865          78 :                 if (count == 0) {
    1866           0 :                         php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
    1867           0 :                         count = size_offsets/3;
    1868          78 :                 } else if (count < 0 && count != PCRE_ERROR_NOMATCH) {
    1869           0 :                         pcre_handle_exec_error(count);
    1870             :                         zend_string_release(subject_str);
    1871           0 :                         break;
    1872             :                 }
    1873             : 
    1874             :                 /* If the entry fits our requirements */
    1875          78 :                 if ((count > 0 && !invert) || (count == PCRE_ERROR_NOMATCH && invert)) {
    1876          33 :                         if (Z_REFCOUNTED_P(entry)) {
    1877             :                                 Z_ADDREF_P(entry);
    1878             :                         }
    1879             : 
    1880             :                         /* Add to return array */
    1881          33 :                         if (string_key) {
    1882           3 :                                 zend_hash_update(Z_ARRVAL_P(return_value), string_key, entry);
    1883             :                         } else {
    1884          30 :                                 zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
    1885             :                         }
    1886             :                 }
    1887             : 
    1888             :                 zend_string_release(subject_str);
    1889             :         } ZEND_HASH_FOREACH_END();
    1890             : 
    1891             :         /* Clean up */
    1892          15 :         if (size_offsets <= 32) {
    1893          15 :                 free_alloca(offsets, use_heap);
    1894             :         } else {
    1895           0 :                 efree(offsets);
    1896             :         }
    1897          15 : }
    1898             : /* }}} */
    1899             : 
    1900             : /* {{{ proto int preg_last_error()
    1901             :    Returns the error code of the last regexp execution. */
    1902          17 : static PHP_FUNCTION(preg_last_error)
    1903             : {
    1904             : #ifndef FAST_ZPP
    1905             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "") == FAILURE) {
    1906             :                 return;
    1907             :         }
    1908             : #else
    1909          17 :         ZEND_PARSE_PARAMETERS_START(0, 0)
    1910          17 :         ZEND_PARSE_PARAMETERS_END();
    1911             : #endif
    1912             : 
    1913          15 :         RETURN_LONG(PCRE_G(error_code));
    1914             : }
    1915             : /* }}} */
    1916             : 
    1917             : /* {{{ module definition structures */
    1918             : 
    1919             : /* {{{ arginfo */
    1920             : ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_match, 0, 0, 2)
    1921             :     ZEND_ARG_INFO(0, pattern)
    1922             :     ZEND_ARG_INFO(0, subject)
    1923             :     ZEND_ARG_INFO(1, subpatterns) /* array */
    1924             :     ZEND_ARG_INFO(0, flags)
    1925             :     ZEND_ARG_INFO(0, offset)
    1926             : ZEND_END_ARG_INFO()
    1927             : 
    1928             : ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_match_all, 0, 0, 2)
    1929             :     ZEND_ARG_INFO(0, pattern)
    1930             :     ZEND_ARG_INFO(0, subject)
    1931             :     ZEND_ARG_INFO(1, subpatterns) /* array */
    1932             :     ZEND_ARG_INFO(0, flags)
    1933             :     ZEND_ARG_INFO(0, offset)
    1934             : ZEND_END_ARG_INFO()
    1935             : 
    1936             : ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_replace, 0, 0, 3)
    1937             :     ZEND_ARG_INFO(0, regex)
    1938             :     ZEND_ARG_INFO(0, replace)
    1939             :     ZEND_ARG_INFO(0, subject)
    1940             :     ZEND_ARG_INFO(0, limit)
    1941             :     ZEND_ARG_INFO(1, count)
    1942             : ZEND_END_ARG_INFO()
    1943             : 
    1944             : ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_replace_callback, 0, 0, 3)
    1945             :     ZEND_ARG_INFO(0, regex)
    1946             :     ZEND_ARG_INFO(0, callback)
    1947             :     ZEND_ARG_INFO(0, subject)
    1948             :     ZEND_ARG_INFO(0, limit)
    1949             :     ZEND_ARG_INFO(1, count)
    1950             : ZEND_END_ARG_INFO()
    1951             : 
    1952             : ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_split, 0, 0, 2)
    1953             :     ZEND_ARG_INFO(0, pattern)
    1954             :     ZEND_ARG_INFO(0, subject)
    1955             :     ZEND_ARG_INFO(0, limit)
    1956             :     ZEND_ARG_INFO(0, flags)
    1957             : ZEND_END_ARG_INFO()
    1958             : 
    1959             : ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_quote, 0, 0, 1)
    1960             :     ZEND_ARG_INFO(0, str)
    1961             :     ZEND_ARG_INFO(0, delim_char)
    1962             : ZEND_END_ARG_INFO()
    1963             : 
    1964             : ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_grep, 0, 0, 2)
    1965             :     ZEND_ARG_INFO(0, regex)
    1966             :     ZEND_ARG_INFO(0, input) /* array */
    1967             :     ZEND_ARG_INFO(0, flags)
    1968             : ZEND_END_ARG_INFO()
    1969             : 
    1970             : ZEND_BEGIN_ARG_INFO(arginfo_preg_last_error, 0)
    1971             : ZEND_END_ARG_INFO()
    1972             : /* }}} */
    1973             : 
    1974             : static const zend_function_entry pcre_functions[] = {
    1975             :         PHP_FE(preg_match,                              arginfo_preg_match)
    1976             :         PHP_FE(preg_match_all,                  arginfo_preg_match_all)
    1977             :         PHP_FE(preg_replace,                    arginfo_preg_replace)
    1978             :         PHP_FE(preg_replace_callback,   arginfo_preg_replace_callback)
    1979             :         PHP_FE(preg_filter,                             arginfo_preg_replace)
    1980             :         PHP_FE(preg_split,                              arginfo_preg_split)
    1981             :         PHP_FE(preg_quote,                              arginfo_preg_quote)
    1982             :         PHP_FE(preg_grep,                               arginfo_preg_grep)
    1983             :         PHP_FE(preg_last_error,                 arginfo_preg_last_error)
    1984             :         PHP_FE_END
    1985             : };
    1986             : 
    1987             : zend_module_entry pcre_module_entry = {
    1988             :         STANDARD_MODULE_HEADER,
    1989             :    "pcre",
    1990             :         pcre_functions,
    1991             :         PHP_MINIT(pcre),
    1992             :         PHP_MSHUTDOWN(pcre),
    1993             :         NULL,
    1994             :         NULL,
    1995             :         PHP_MINFO(pcre),
    1996             :         NO_VERSION_YET,
    1997             :         PHP_MODULE_GLOBALS(pcre),
    1998             :         PHP_GINIT(pcre),
    1999             :         PHP_GSHUTDOWN(pcre),
    2000             :         NULL,
    2001             :         STANDARD_MODULE_PROPERTIES_EX
    2002             : };
    2003             : 
    2004             : #ifdef COMPILE_DL_PCRE
    2005             : ZEND_GET_MODULE(pcre)
    2006             : #endif
    2007             : 
    2008             : /* }}} */
    2009             : 
    2010             : #endif /* HAVE_PCRE || HAVE_BUNDLED_PCRE */
    2011             : 
    2012             : /*
    2013             :  * Local variables:
    2014             :  * tab-width: 4
    2015             :  * c-basic-offset: 4
    2016             :  * End:
    2017             :  * vim600: sw=4 ts=4 fdm=marker
    2018             :  * vim<600: sw=4 ts=4
    2019             :  */

Generated by: LCOV version 1.10

Generated at Sun, 29 Mar 2015 03:45:54 +0000 (22 hours ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.