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/filter - logical_filters.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 375 389 96.4 %
Date: 2014-10-22 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Derick Rethans <derick@php.net>                             |
      16             :   |          Pierre-A. Joye <pierre@php.net>                             |
      17             :   +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "php_filter.h"
      23             : #include "filter_private.h"
      24             : #include "ext/standard/url.h"
      25             : #include "ext/pcre/php_pcre.h"
      26             : 
      27             : #include "zend_multiply.h"
      28             : 
      29             : #if HAVE_ARPA_INET_H
      30             : # include <arpa/inet.h>
      31             : #endif
      32             : 
      33             : #ifndef INADDR_NONE
      34             : # define INADDR_NONE ((unsigned long int) -1)
      35             : #endif
      36             : 
      37             : 
      38             : /* {{{ FETCH_LONG_OPTION(var_name, option_name) */
      39             : #define FETCH_LONG_OPTION(var_name, option_name) \
      40             :         var_name = 0; \
      41             :         var_name##_set = 0; \
      42             :         if (option_array) { \
      43             :                 if ((option_val = zend_hash_str_find(HASH_OF(option_array), option_name, sizeof(option_name) - 1)) != NULL) {   \
      44             :                         var_name = zval_get_long(option_val); \
      45             :                         var_name##_set = 1; \
      46             :                 } \
      47             :         }
      48             : /* }}} */
      49             : 
      50             : /* {{{ FETCH_STRING_OPTION(var_name, option_name) */
      51             : #define FETCH_STRING_OPTION(var_name, option_name) \
      52             :         var_name = NULL; \
      53             :         var_name##_set = 0; \
      54             :         var_name##_len = 0; \
      55             :         if (option_array) { \
      56             :                 if ((option_val = zend_hash_str_find(HASH_OF(option_array), option_name, sizeof(option_name) - 1)) != NULL) { \
      57             :                         if (Z_TYPE_P(option_val) == IS_STRING) { \
      58             :                                 var_name = Z_STRVAL_P(option_val); \
      59             :                                 var_name##_len = Z_STRLEN_P(option_val); \
      60             :                                 var_name##_set = 1; \
      61             :                         } \
      62             :                 } \
      63             :         }
      64             : /* }}} */
      65             : 
      66             : /* {{{ FETCH_STR_OPTION(var_name, option_name) */
      67             : #define FETCH_STR_OPTION(var_name, option_name) \
      68             :         var_name = NULL; \
      69             :         var_name##_set = 0; \
      70             :         if (option_array) { \
      71             :                 if ((option_val = zend_hash_str_find(HASH_OF(option_array), option_name, sizeof(option_name) - 1)) != NULL) { \
      72             :                         if (Z_TYPE_P(option_val) == IS_STRING) { \
      73             :                                 var_name = Z_STR_P(option_val); \
      74             :                                 var_name##_set = 1; \
      75             :                         } \
      76             :                 } \
      77             :         }
      78             : /* }}} */
      79             : 
      80             : #define FORMAT_IPV4    4
      81             : #define FORMAT_IPV6    6
      82             : 
      83          59 : static int php_filter_parse_int(const char *str, unsigned int str_len, zend_long *ret TSRMLS_DC) { /* {{{ */
      84             :         zend_long ctx_value;
      85          59 :         int sign = 0, digit = 0;
      86          59 :         const char *end = str + str_len;
      87             : 
      88          59 :         switch (*str) {
      89             :                 case '-':
      90          16 :                         sign = 1;
      91             :                 case '+':
      92          17 :                         str++;
      93             :                 default:
      94             :                         break;
      95             :         }
      96             : 
      97          59 :         if (*str == '0' && str + 1 == end) {
      98             :                 /* Special cases: +0 and -0 */
      99           2 :                 return 1;
     100             :         }
     101             : 
     102             :         /* must start with 1..9*/
     103          99 :         if (str < end && *str >= '1' && *str <= '9') {
     104          42 :                 ctx_value = ((sign)?-1:1) * ((*(str++)) - '0');
     105             :         } else {
     106          15 :                 return -1;
     107             :         }
     108             : 
     109          42 :         if ((end - str > MAX_LENGTH_OF_LONG - 1) /* number too long */
     110             :          || (SIZEOF_LONG == 4 && (end - str == MAX_LENGTH_OF_LONG - 1) && *str > '2')) {
     111             :                 /* overflow */
     112           0 :                 return -1;
     113             :         }
     114             : 
     115         217 :         while (str < end) {
     116         274 :                 if (*str >= '0' && *str <= '9') {
     117         135 :                         digit = (*(str++) - '0');
     118         207 :                         if ( (!sign) && ctx_value <= (ZEND_LONG_MAX-digit)/10 ) {
     119          72 :                                 ctx_value = (ctx_value * 10) + digit;
     120         124 :                         } else if ( sign && ctx_value >= (ZEND_LONG_MIN+digit)/10) {
     121          61 :                                 ctx_value = (ctx_value * 10) - digit;
     122             :                         } else {
     123           2 :                                 return -1;
     124             :                         }
     125             :                 } else {
     126           6 :                         return -1;
     127             :                 }
     128             :         }
     129             : 
     130          34 :         *ret = ctx_value;
     131          34 :         return 1;
     132             : }
     133             : /* }}} */
     134             : 
     135          11 : static int php_filter_parse_octal(const char *str, unsigned int str_len, zend_long *ret TSRMLS_DC) { /* {{{ */
     136          11 :         zend_ulong ctx_value = 0;
     137          11 :         const char *end = str + str_len;
     138             : 
     139         121 :         while (str < end) {
     140         200 :                 if (*str >= '0' && *str <= '7') {
     141         100 :                         zend_ulong n = ((*(str++)) - '0');
     142             : 
     143         199 :                         if ((ctx_value > ((zend_ulong)(~(zend_long)0)) / 8) ||
     144          99 :                                 ((ctx_value = ctx_value * 8) > ((zend_ulong)(~(zend_long)0)) - n)) {
     145           1 :                                 return -1;
     146             :                         }
     147          99 :                         ctx_value += n;
     148             :                 } else {
     149           1 :                         return -1;
     150             :                 }
     151             :         }
     152             :         
     153           9 :         *ret = (zend_long)ctx_value;
     154           9 :         return 1;
     155             : }
     156             : /* }}} */
     157             : 
     158          66 : static int php_filter_parse_hex(const char *str, unsigned int str_len, zend_long *ret TSRMLS_DC) { /* {{{ */
     159          66 :         zend_ulong ctx_value = 0;
     160          66 :         const char *end = str + str_len;
     161             :         zend_ulong n;
     162             : 
     163         328 :         while (str < end) {
     164         320 :                 if (*str >= '0' && *str <= '9') {
     165         122 :                         n = ((*(str++)) - '0');
     166         141 :                 } else if (*str >= 'a' && *str <= 'f') {
     167          65 :                         n = ((*(str++)) - ('a' - 10));
     168          21 :                 } else if (*str >= 'A' && *str <= 'F') {
     169          10 :                         n = ((*(str++)) - ('A' - 10));
     170             :                 } else {
     171           1 :                         return -1;
     172             :                 }
     173         393 :                 if ((ctx_value > ((zend_ulong)(~(zend_long)0)) / 16) ||
     174         196 :                         ((ctx_value = ctx_value * 16) > ((zend_ulong)(~(zend_long)0)) - n)) {
     175           1 :                         return -1;
     176             :                 }
     177         196 :                 ctx_value += n;
     178             :         }
     179             : 
     180          64 :         *ret = (zend_long)ctx_value;
     181          64 :         return 1;
     182             : }
     183             : /* }}} */
     184             : 
     185          95 : void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     186             : {
     187             :         zval *option_val;
     188             :         zend_long  min_range, max_range, option_flags;
     189             :         int   min_range_set, max_range_set;
     190          95 :         int   allow_octal = 0, allow_hex = 0;
     191          95 :         int       len, error = 0;
     192             :         zend_long  ctx_value;
     193             :         char *p;
     194             : 
     195             :         /* Parse options */
     196         114 :         FETCH_LONG_OPTION(min_range,    "min_range");
     197         114 :         FETCH_LONG_OPTION(max_range,    "max_range");
     198          95 :         option_flags = flags;
     199             : 
     200          95 :         len = Z_STRLEN_P(value);
     201             : 
     202          95 :         if (len == 0) {
     203           5 :                 RETURN_VALIDATION_FAILED
     204             :         }
     205             : 
     206          90 :         if (option_flags & FILTER_FLAG_ALLOW_OCTAL) {
     207          19 :                 allow_octal = 1;
     208             :         }
     209             : 
     210          90 :         if (option_flags & FILTER_FLAG_ALLOW_HEX) {
     211          17 :                 allow_hex = 1;
     212             :         }
     213             : 
     214             :         /* Start the validating loop */
     215          90 :         p = Z_STRVAL_P(value);
     216          90 :         ctx_value = 0;
     217             : 
     218          90 :         PHP_FILTER_TRIM_DEFAULT(p, len);
     219             : 
     220          90 :         if (*p == '0') {
     221          31 :                 p++; len--;
     222          43 :                 if (allow_hex && (*p == 'x' || *p == 'X')) {
     223          12 :                         p++; len--;
     224          12 :                         if (php_filter_parse_hex(p, len, &ctx_value TSRMLS_CC) < 0) {
     225           1 :                                 error = 1;
     226             :                         }
     227          19 :                 } else if (allow_octal) {
     228          11 :                         if (php_filter_parse_octal(p, len, &ctx_value TSRMLS_CC) < 0) {
     229           2 :                                 error = 1;
     230             :                         }
     231           8 :                 } else if (len != 0) {
     232           4 :                         error = 1;
     233             :                 }
     234             :         } else {
     235          59 :                 if (php_filter_parse_int(p, len, &ctx_value TSRMLS_CC) < 0) {
     236          23 :                         error = 1;
     237             :                 }
     238             :         }
     239             : 
     240          90 :         if (error > 0 || (min_range_set && (ctx_value < min_range)) || (max_range_set && (ctx_value > max_range))) {
     241          33 :                 RETURN_VALIDATION_FAILED
     242             :         } else {
     243          57 :                 zval_ptr_dtor(value);
     244          57 :                 ZVAL_LONG(value, ctx_value);
     245          57 :                 return;
     246             :         }
     247             : }
     248             : /* }}} */
     249             : 
     250          68 : void php_filter_boolean(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     251             : {
     252          68 :         char *str = Z_STRVAL_P(value);
     253          68 :         int len = Z_STRLEN_P(value);
     254             :         int ret;
     255             : 
     256          68 :         PHP_FILTER_TRIM_DEFAULT_EX(str, len, 0);
     257             : 
     258             :         /* returns true for "1", "true", "on" and "yes"
     259             :          * returns false for "0", "false", "off", "no", and ""
     260             :          * null otherwise. */
     261          68 :         switch (len) {
     262             :                 case 0:
     263           8 :                         ret = 0;
     264           8 :                         break;
     265             :                 case 1:
     266          19 :                         if (*str == '1') {
     267          10 :                                 ret = 1;
     268           9 :                         } else if (*str == '0') {
     269           7 :                                 ret = 0;
     270             :                         } else {
     271           2 :                                 ret = -1;
     272             :                         }
     273          19 :                         break;
     274             :                 case 2:
     275           9 :                         if (strncasecmp(str, "on", 2) == 0) {
     276           5 :                                 ret = 1;
     277           4 :                         } else if (strncasecmp(str, "no", 2) == 0) {
     278           3 :                                 ret = 0;
     279             :                         } else {
     280           1 :                                 ret = -1;
     281             :                         }
     282           9 :                         break;
     283             :                 case 3:
     284          11 :                         if (strncasecmp(str, "yes", 3) == 0) {
     285           3 :                                 ret = 1;
     286           8 :                         } else if (strncasecmp(str, "off", 3) == 0) {
     287           5 :                                 ret = 0;
     288             :                         } else {
     289           3 :                                 ret = -1;
     290             :                         }
     291          11 :                         break;
     292             :                 case 4:
     293           6 :                         if (strncasecmp(str, "true", 4) == 0) {
     294           4 :                                 ret = 1;
     295             :                         } else {
     296           2 :                                 ret = -1;
     297             :                         }
     298           6 :                         break;
     299             :                 case 5:
     300           5 :                         if (strncasecmp(str, "false", 5) == 0) {
     301           5 :                                 ret = 0;
     302             :                         } else {
     303           0 :                                 ret = -1;
     304             :                         }
     305           5 :                         break;
     306             :                 default:
     307          10 :                         ret = -1;
     308             :         }
     309             : 
     310          68 :         if (ret == -1) {
     311          18 :                 RETURN_VALIDATION_FAILED
     312             :         } else {
     313             :                 zval_dtor(value);
     314          50 :                 ZVAL_BOOL(value, ret);
     315             :         }
     316             : }
     317             : /* }}} */
     318             : 
     319          69 : void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     320             : {
     321             :         int len;
     322             :         char *str, *end;
     323             :         char *num, *p;
     324             :         zval *option_val;
     325             :         char *decimal;
     326             :         int decimal_set, decimal_len;
     327          69 :         char dec_sep = '.';
     328          69 :         char tsd_sep[3] = "',.";
     329             : 
     330             :         zend_long lval;
     331             :         double dval;
     332             : 
     333             :         int first, n;
     334             : 
     335          69 :         len = Z_STRLEN_P(value);
     336          69 :         str = Z_STRVAL_P(value);
     337             : 
     338          73 :         PHP_FILTER_TRIM_DEFAULT(str, len);
     339          65 :         end = str + len;
     340             : 
     341          75 :         FETCH_STRING_OPTION(decimal, "decimal");
     342             : 
     343          65 :         if (decimal_set) {
     344           5 :                 if (decimal_len != 1) {
     345           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "decimal separator must be one char");
     346           1 :                         RETURN_VALIDATION_FAILED
     347             :                 } else {
     348           4 :                         dec_sep = *decimal;
     349             :                 }
     350             :         }
     351             : 
     352          64 :         num = p = emalloc(len+1);
     353          64 :         if (str < end && (*str == '+' || *str == '-')) {
     354           9 :                 *p++ = *str++;
     355             :         }
     356          64 :         first = 1;
     357             :         while (1) {
     358          79 :                 n = 0;
     359         310 :                 while (str < end && *str >= '0' && *str <= '9') {
     360         152 :                         ++n;
     361         152 :                         *p++ = *str++;
     362             :                 }
     363          79 :                 if (str == end || *str == dec_sep || *str == 'e' || *str == 'E') {
     364          50 :                         if (!first && n != 3) {
     365           3 :                                 goto error;
     366             :                         }
     367          47 :                         if (*str == dec_sep) {
     368          25 :                                 *p++ = '.';
     369          25 :                                 str++;
     370         126 :                                 while (str < end && *str >= '0' && *str <= '9') {
     371          76 :                                         *p++ = *str++;
     372             :                                 }
     373             :                         }
     374          47 :                         if (*str == 'e' || *str == 'E') {
     375          14 :                                 *p++ = *str++;
     376          14 :                                 if (str < end && (*str == '+' || *str == '-')) {
     377           7 :                                         *p++ = *str++;
     378             :                                 }
     379          48 :                                 while (str < end && *str >= '0' && *str <= '9') {
     380          20 :                                         *p++ = *str++;
     381             :                                 }
     382             :                         }
     383          47 :                         break;
     384             :                 }
     385          29 :                 if ((flags & FILTER_FLAG_ALLOW_THOUSAND) && (*str == tsd_sep[0] || *str == tsd_sep[1] || *str == tsd_sep[2])) {
     386          16 :                         if (first?(n < 1 || n > 3):(n != 3)) {
     387           1 :                                 goto error;
     388             :                         }
     389          15 :                         first = 0;
     390          15 :                         str++;
     391             :                 } else {
     392             :                         goto error;
     393             :                 }
     394          15 :         }
     395          47 :         if (str != end) {
     396           2 :                 goto error;
     397             :         }
     398          45 :         *p = 0;
     399             : 
     400          90 :         switch (is_numeric_string(num, p - num, &lval, &dval, 0)) {
     401             :                 case IS_LONG:
     402           9 :                         zval_ptr_dtor(value);
     403           9 :                         ZVAL_DOUBLE(value, lval);
     404           9 :                         break;
     405             :                 case IS_DOUBLE:
     406          31 :                         if ((!dval && p - num > 1 && strpbrk(num, "123456789")) || !zend_finite(dval)) {
     407             :                                 goto error;
     408             :                         }
     409          29 :                         zval_ptr_dtor(value);
     410          29 :                         ZVAL_DOUBLE(value, dval);
     411          29 :                         break;
     412             :                 default:
     413             : error:
     414          26 :                         efree(num);
     415          26 :                         RETURN_VALIDATION_FAILED
     416             :         }
     417          38 :         efree(num);     
     418             : }
     419             : /* }}} */
     420             : 
     421          16 : void php_filter_validate_regexp(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     422             : {
     423             :         zval *option_val;
     424             :         zend_string *regexp;
     425             :         zend_long option_flags;
     426             :         int regexp_set, option_flags_set;
     427          16 :         pcre *re = NULL;
     428          16 :         pcre_extra *pcre_extra = NULL;
     429          16 :         int preg_options = 0;
     430             :         int ovector[3];
     431             :         int matches;
     432             : 
     433             :         /* Parse options */
     434          46 :         FETCH_STR_OPTION(regexp, "regexp");
     435          31 :         FETCH_LONG_OPTION(option_flags, "flags");
     436             : 
     437          16 :         if (!regexp_set) {
     438           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "'regexp' option missing");
     439           1 :                 RETURN_VALIDATION_FAILED
     440             :         }
     441             : 
     442          15 :         re = pcre_get_compiled_regex(regexp, &pcre_extra, &preg_options TSRMLS_CC);
     443          15 :         if (!re) {
     444           0 :                 RETURN_VALIDATION_FAILED
     445             :         }
     446          15 :         matches = pcre_exec(re, NULL, Z_STRVAL_P(value), Z_STRLEN_P(value), 0, 0, ovector, 3);
     447             : 
     448             :         /* 0 means that the vector is too small to hold all the captured substring offsets */
     449          15 :         if (matches < 0) {
     450          12 :                 RETURN_VALIDATION_FAILED
     451             :         }
     452             : }
     453             : /* }}} */
     454             : 
     455          49 : void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     456             : {
     457             :         php_url *url;
     458          49 :         int old_len = Z_STRLEN_P(value);
     459             :         
     460          49 :         php_filter_url(value, flags, option_array, charset TSRMLS_CC);
     461             : 
     462          49 :         if (Z_TYPE_P(value) != IS_STRING || old_len != Z_STRLEN_P(value)) {
     463           2 :                 RETURN_VALIDATION_FAILED
     464             :         }
     465             : 
     466             :         /* Use parse_url - if it returns false, we return NULL */
     467          47 :         url = php_url_parse_ex(Z_STRVAL_P(value), Z_STRLEN_P(value));
     468             : 
     469          47 :         if (url == NULL) {
     470           6 :                 RETURN_VALIDATION_FAILED
     471             :         }
     472             : 
     473          41 :         if (url->scheme != NULL && (!strcasecmp(url->scheme, "http") || !strcasecmp(url->scheme, "https"))) {
     474             :                 char *e, *s;
     475             : 
     476          20 :                 if (url->host == NULL) {
     477           3 :                         goto bad_url;
     478             :                 }
     479             : 
     480          17 :                 e = url->host + strlen(url->host);
     481          17 :                 s = url->host;
     482             : 
     483             :                 /* First char of hostname must be alphanumeric */
     484          17 :                 if(!isalnum((int)*(unsigned char *)s)) { 
     485           0 :                         goto bad_url;
     486             :                 }
     487             : 
     488         225 :                 while (s < e) {
     489         192 :                         if (!isalnum((int)*(unsigned char *)s) && *s != '-' && *s != '.') {
     490           1 :                                 goto bad_url;
     491             :                         }
     492         191 :                         s++;
     493             :                 }
     494             :         }
     495             : 
     496         113 :         if (
     497          37 :                 url->scheme == NULL || 
     498             :                 /* some schemas allow the host to be empty */
     499          31 :                 (url->host == NULL && (strcmp(url->scheme, "mailto") && strcmp(url->scheme, "news") && strcmp(url->scheme, "file"))) ||
     500          45 :                 ((flags & FILTER_FLAG_PATH_REQUIRED) && url->path == NULL) || ((flags & FILTER_FLAG_QUERY_REQUIRED) && url->query == NULL)
     501             :         ) {
     502             : bad_url:
     503          22 :                 php_url_free(url);
     504          22 :                 RETURN_VALIDATION_FAILED
     505             :         }
     506          19 :         php_url_free(url);
     507             : }
     508             : /* }}} */
     509             : 
     510          36 : void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     511             : {
     512             :         /*
     513             :          * The regex below is based on a regex by Michael Rushton.
     514             :          * However, it is not identical.  I changed it to only consider routeable
     515             :          * addresses as valid.  Michael's regex considers a@b a valid address
     516             :          * which conflicts with section 2.3.5 of RFC 5321 which states that:
     517             :          *
     518             :          *   Only resolvable, fully-qualified domain names (FQDNs) are permitted
     519             :          *   when domain names are used in SMTP.  In other words, names that can
     520             :          *   be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
     521             :          *   in Section 5) are permitted, as are CNAME RRs whose targets can be
     522             :          *   resolved, in turn, to MX or address RRs.  Local nicknames or
     523             :          *   unqualified names MUST NOT be used.
     524             :          *
     525             :          * This regex does not handle comments and folding whitespace.  While
     526             :          * this is technically valid in an email address, these parts aren't
     527             :          * actually part of the address itself.
     528             :          *
     529             :          * Michael's regex carries this copyright:
     530             :          *
     531             :          * Copyright © Michael Rushton 2009-10
     532             :          * http://squiloople.com/
     533             :          * Feel free to use and redistribute this code. But please keep this copyright notice.
     534             :          *
     535             :          */
     536          36 :         const char regexp[] = "/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD";
     537          36 :         pcre       *re = NULL;
     538          36 :         pcre_extra *pcre_extra = NULL;
     539          36 :         int preg_options = 0;
     540             :         int         ovector[150]; /* Needs to be a multiple of 3 */
     541             :         int         matches;
     542             :         zend_string *sregexp;
     543             : 
     544             : 
     545             :         /* The maximum length of an e-mail address is 320 octets, per RFC 2821. */
     546          36 :         if (Z_STRLEN_P(value) > 320) {
     547           2 :                 RETURN_VALIDATION_FAILED
     548             :         }
     549             : 
     550          34 :         sregexp = zend_string_init(regexp, sizeof(regexp) - 1, 0);
     551          34 :         re = pcre_get_compiled_regex(sregexp, &pcre_extra, &preg_options TSRMLS_CC);
     552          34 :         if (!re) {
     553             :                 zend_string_release(sregexp);
     554           0 :                 RETURN_VALIDATION_FAILED
     555             :         }
     556             :         zend_string_release(sregexp);
     557          34 :         matches = pcre_exec(re, NULL, Z_STRVAL_P(value), Z_STRLEN_P(value), 0, 0, ovector, 3);
     558             : 
     559             :         /* 0 means that the vector is too small to hold all the captured substring offsets */
     560          34 :         if (matches < 0) {
     561          22 :                 RETURN_VALIDATION_FAILED
     562             :         }
     563             : 
     564             : }
     565             : /* }}} */
     566             : 
     567          55 : static int _php_filter_validate_ipv4(char *str, int str_len, int *ip) /* {{{ */
     568             : {
     569          55 :         const char *end = str + str_len;
     570             :         int num, m;
     571          55 :         int n = 0;
     572             : 
     573         248 :         while (str < end) {
     574             :                 int leading_zero;
     575         193 :                 if (*str < '0' || *str > '9') {
     576           6 :                         return 0;
     577             :                 }
     578         187 :                 leading_zero = (*str == '0');
     579         187 :                 m = 1;
     580         187 :                 num = ((*(str++)) - '0');
     581         546 :                 while (str < end && (*str >= '0' && *str <= '9')) {
     582         173 :                         num = num * 10 + ((*(str++)) - '0');
     583         173 :                         if (num > 255 || ++m > 3) {
     584           1 :                                 return 0;
     585             :                         }
     586             :                 }
     587             :                 /* don't allow a leading 0; that introduces octal numbers,
     588             :                  * which we don't support */
     589         186 :                 if (leading_zero && (num != 0 || m > 1))
     590           2 :                         return 0;
     591         184 :                 ip[n++] = num;
     592         184 :                 if (n == 4) {
     593          45 :                         return str == end;
     594         139 :                 } else if (str >= end || *(str++) != '.') {
     595           1 :                         return 0;
     596             :                 }
     597             :         }
     598           0 :         return 0;               
     599             : }
     600             : /* }}} */
     601             : 
     602          68 : static int _php_filter_validate_ipv6(char *str, int str_len TSRMLS_DC) /* {{{ */
     603             : {
     604          68 :         int compressed = 0;
     605          68 :         int blocks = 0;
     606             :         int n;
     607             :         char *ipv4;
     608             :         char *end;
     609             :         int ip4elm[4];
     610          68 :         char *s = str;
     611             : 
     612          68 :         if (!memchr(str, ':', str_len)) {
     613           0 :                 return 0;
     614             :         }
     615             : 
     616             :         /* check for bundled IPv4 */
     617          68 :         ipv4 = memchr(str, '.', str_len);
     618          68 :         if (ipv4) {
     619         114 :                 while (ipv4 > str && *(ipv4-1) != ':') {
     620          62 :                         ipv4--;
     621             :                 }
     622             : 
     623          26 :                 if (!_php_filter_validate_ipv4(ipv4, (str_len - (ipv4 - str)), ip4elm)) {
     624           4 :                         return 0;
     625             :                 }
     626             : 
     627          22 :                 str_len = ipv4 - str; /* length excluding ipv4 */
     628          22 :                 if (str_len < 2) {
     629           0 :                         return 0;
     630             :                 }
     631             : 
     632          22 :                 if (ipv4[-2] != ':') {
     633             :                         /* don't include : before ipv4 unless it's a :: */
     634          13 :                         str_len--;
     635             :                 }
     636             : 
     637          22 :                 blocks = 2;
     638             :         }
     639             : 
     640          64 :         end = str + str_len;
     641             : 
     642         354 :         while (str < end) {
     643         254 :                 if (*str == ':') {
     644         209 :                         if (++str >= end) {
     645             :                                 /* cannot end in : without previous : */
     646           0 :                                 return 0;
     647             :                         }
     648         209 :                         if (*str == ':') {
     649          54 :                                 if (compressed) {
     650           4 :                                         return 0;
     651             :                                 }
     652          50 :                                 blocks++; /* :: means 1 or more 16-bit 0 blocks */
     653          50 :                                 compressed = 1;
     654             : 
     655          50 :                                 if (++str == end) {
     656          10 :                                         return (blocks <= 8);
     657             :                                 }
     658         155 :                         } else if ((str - 1) == s) {
     659             :                                 /* dont allow leading : without another : following */
     660           0 :                                 return 0;
     661             :                         }                               
     662             :                 }
     663         240 :                 n = 0;
     664        2704 :                 while ((str < end) &&
     665        1186 :                        ((*str >= '0' && *str <= '9') ||
     666         381 :                         (*str >= 'a' && *str <= 'f') ||
     667         261 :                         (*str >= 'A' && *str <= 'F'))) {
     668         396 :                         n++;
     669         396 :                         str++;
     670             :                 }
     671         240 :                 if (n < 1 || n > 4) {
     672           8 :                         return 0;
     673             :                 }
     674         232 :                 if (++blocks > 8)
     675           6 :                         return 0;
     676             :         }
     677          36 :         return ((compressed && blocks <= 8) || blocks == 8);
     678             : }
     679             : /* }}} */
     680             : 
     681         107 : void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     682             : {
     683             :         /* validates an ipv4 or ipv6 IP, based on the flag (4, 6, or both) add a
     684             :          * flag to throw out reserved ranges; multicast ranges... etc. If both
     685             :          * allow_ipv4 and allow_ipv6 flags flag are used, then the first dot or
     686             :          * colon determine the format */
     687             : 
     688             :         int            ip[4];
     689             :         int            mode;
     690             : 
     691         107 :         if (memchr(Z_STRVAL_P(value), ':', Z_STRLEN_P(value))) {
     692          69 :                 mode = FORMAT_IPV6;
     693          38 :         } else if (memchr(Z_STRVAL_P(value), '.', Z_STRLEN_P(value))) {
     694          30 :                 mode = FORMAT_IPV4;
     695             :         } else {
     696           8 :                 RETURN_VALIDATION_FAILED
     697             :         }
     698             : 
     699          99 :         if ((flags & FILTER_FLAG_IPV4) && (flags & FILTER_FLAG_IPV6)) {
     700             :                 /* Both formats are cool */
     701          99 :         } else if ((flags & FILTER_FLAG_IPV4) && mode == FORMAT_IPV6) {
     702           1 :                 RETURN_VALIDATION_FAILED
     703          98 :         } else if ((flags & FILTER_FLAG_IPV6) && mode == FORMAT_IPV4) {
     704           1 :                 RETURN_VALIDATION_FAILED
     705             :         }
     706             : 
     707          97 :         switch (mode) {
     708             :                 case FORMAT_IPV4:
     709          29 :                         if (!_php_filter_validate_ipv4(Z_STRVAL_P(value), Z_STRLEN_P(value), ip)) {
     710           9 :                                 RETURN_VALIDATION_FAILED
     711             :                         }
     712             : 
     713             :                         /* Check flags */
     714          20 :                         if (flags & FILTER_FLAG_NO_PRIV_RANGE) {
     715           8 :                                 if (
     716           2 :                                         (ip[0] == 10) ||
     717           2 :                                         (ip[0] == 172 && (ip[1] >= 16 && ip[1] <= 31)) ||
     718           4 :                                         (ip[0] == 192 && ip[1] == 168)
     719             :                                 ) {
     720           1 :                                         RETURN_VALIDATION_FAILED
     721             :                                 }
     722             :                         }
     723             : 
     724          19 :                         if (flags & FILTER_FLAG_NO_RES_RANGE) {
     725          51 :                                 if (
     726           8 :                                         (ip[0] == 0) ||
     727          12 :                                         (ip[0] == 100 && (ip[1] >= 64 && ip[1] <= 127)) ||
     728           6 :                                         (ip[0] == 169 && ip[1] == 254) ||
     729           8 :                                         (ip[0] == 192 && ip[1] == 0 && ip[2] == 2) ||
     730          12 :                                         (ip[0] == 127 && ip[1] == 0 && ip[2] == 0 && ip[3] == 1) ||
     731           5 :                                         (ip[0] >= 224 && ip[0] <= 255)
     732             :                                 ) {
     733           5 :                                         RETURN_VALIDATION_FAILED
     734             :                                 }
     735             :                         }
     736          14 :                         break;
     737             : 
     738             :                 case FORMAT_IPV6:
     739             :                         {
     740          68 :                                 int res = 0;
     741          68 :                                 res = _php_filter_validate_ipv6(Z_STRVAL_P(value), Z_STRLEN_P(value) TSRMLS_CC);
     742          68 :                                 if (res < 1) {
     743          27 :                                         RETURN_VALIDATION_FAILED
     744             :                                 }
     745             :                                 /* Check flags */
     746          41 :                                 if (flags & FILTER_FLAG_NO_PRIV_RANGE) {
     747           1 :                                         if (Z_STRLEN_P(value) >=2 && (!strncasecmp("FC", Z_STRVAL_P(value), 2) || !strncasecmp("FD", Z_STRVAL_P(value), 2))) {
     748           1 :                                                 RETURN_VALIDATION_FAILED
     749             :                                         }
     750             :                                 }
     751          40 :                                 if (flags & FILTER_FLAG_NO_RES_RANGE) {
     752           8 :                                         switch (Z_STRLEN_P(value)) {
     753             :                                                 case 1: case 0:
     754           0 :                                                         break;
     755             :                                                 case 2:
     756           1 :                                                         if (!strcmp("::", Z_STRVAL_P(value))) {
     757           1 :                                                                 RETURN_VALIDATION_FAILED
     758             :                                                         }
     759           0 :                                                         break;
     760             :                                                 case 3:
     761           3 :                                                         if (!strcmp("::1", Z_STRVAL_P(value)) || !strcmp("5f:", Z_STRVAL_P(value))) {
     762           3 :                                                                 RETURN_VALIDATION_FAILED
     763             :                                                         }
     764           0 :                                                         break;
     765             :                                                 default:
     766           4 :                                                         if (Z_STRLEN_P(value) >= 5) {
     767          13 :                                                                 if (
     768           4 :                                                                         !strncasecmp("fe8", Z_STRVAL_P(value), 3) ||
     769           3 :                                                                         !strncasecmp("fe9", Z_STRVAL_P(value), 3) ||
     770           3 :                                                                         !strncasecmp("fea", Z_STRVAL_P(value), 3) ||
     771           3 :                                                                         !strncasecmp("feb", Z_STRVAL_P(value), 3)
     772             :                                                                 ) {
     773           1 :                                                                         RETURN_VALIDATION_FAILED
     774             :                                                                 }
     775             :                                                         }
     776          10 :                                                         if (
     777           4 :                                                                 (Z_STRLEN_P(value) >= 9 &&  !strncasecmp("2001:0db8", Z_STRVAL_P(value), 9)) ||
     778           4 :                                                                 (Z_STRLEN_P(value) >= 2 &&  !strncasecmp("5f", Z_STRVAL_P(value), 2)) ||
     779           2 :                                                                 (Z_STRLEN_P(value) >= 4 &&  !strncasecmp("3ff3", Z_STRVAL_P(value), 4)) ||
     780           0 :                                                                 (Z_STRLEN_P(value) >= 8 &&  !strncasecmp("2001:001", Z_STRVAL_P(value), 8))
     781             :                                                         ) {
     782           3 :                                                                 RETURN_VALIDATION_FAILED
     783             :                                                         }
     784             :                                         }
     785             :                                 }
     786             :                         }
     787             :                         break;
     788             :         }
     789             : }
     790             : /* }}} */
     791             : 
     792          24 : void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     793             : {
     794          24 :         char *input = Z_STRVAL_P(value);
     795          24 :         int input_len = Z_STRLEN_P(value);
     796             :         int tokens, length, i, offset, exp_separator_set, exp_separator_len;
     797             :         char separator;
     798             :         char *exp_separator;
     799          24 :         zend_long ret = 0;
     800             :         zval *option_val;
     801             : 
     802          34 :         FETCH_STRING_OPTION(exp_separator, "separator");
     803             : 
     804          24 :         if (exp_separator_set && exp_separator_len != 1) {
     805           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Separator must be exactly one character long");
     806           2 :                 RETURN_VALIDATION_FAILED;
     807             :         }
     808             : 
     809          22 :         if (14 == input_len) {
     810             :                 /* EUI-64 format: Four hexadecimal digits separated by dots. Less
     811             :                  * commonly used but valid nonetheless.
     812             :                  */
     813           1 :                 tokens = 3;
     814           1 :                 length = 4;
     815           1 :                 separator = '.';
     816          27 :         } else if (17 == input_len && input[2] == '-') {
     817             :                 /* IEEE 802 format: Six hexadecimal digits separated by hyphens. */
     818           6 :                 tokens = 6;
     819           6 :                 length = 2;
     820           6 :                 separator = '-';
     821          21 :         } else if (17 == input_len && input[2] == ':') {
     822             :                 /* IEEE 802 format: Six hexadecimal digits separated by colons. */
     823           6 :                 tokens = 6;
     824           6 :                 length = 2;
     825           6 :                 separator = ':';
     826             :         } else {
     827           9 :                 RETURN_VALIDATION_FAILED;
     828             :         }
     829             : 
     830          13 :         if (exp_separator_set && separator != exp_separator[0]) {
     831           2 :                 RETURN_VALIDATION_FAILED;
     832             :         }
     833             : 
     834             :         /* Essentially what we now have is a set of tokens each consisting of
     835             :          * a hexadecimal number followed by a separator character. (With the
     836             :          * exception of the last token which does not have the separator.)
     837             :          */
     838          64 :         for (i = 0; i < tokens; i++) {
     839          55 :                 offset = i * (length + 1);
     840             : 
     841          55 :                 if (i < tokens - 1 && input[offset + length] != separator) {
     842             :                         /* The current token did not end with e.g. a "." */
     843           1 :                         RETURN_VALIDATION_FAILED
     844             :                 }
     845          54 :                 if (php_filter_parse_hex(input + offset, length, &ret TSRMLS_CC) < 0) {
     846             :                         /* The current token is no valid hexadecimal digit */
     847           1 :                         RETURN_VALIDATION_FAILED
     848             :                 }
     849             :         }
     850             : }
     851             : /* }}} */
     852             : 
     853             : /*
     854             :  * Local variables:
     855             :  * tab-width: 4
     856             :  * c-basic-offset: 4
     857             :  * End:
     858             :  * vim600: noet sw=4 ts=4 fdm=marker
     859             :  * vim<600: noet sw=4 ts=4
     860             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:48 +0000 (2 days ago)

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