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/mbstring - mb_gpc.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 165 202 81.7 %
Date: 2014-04-18 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 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: Rui Hirokawa <hirokawa@php.net>                              |
      16             :    |         Moriyoshi Koizumi <moriyoshi@php.net>                        |
      17             :    +----------------------------------------------------------------------+
      18             :  */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : /* {{{ includes */
      23             : #ifdef HAVE_CONFIG_H
      24             : #include "config.h"
      25             : #endif
      26             : 
      27             : #include "php.h"
      28             : #include "php_ini.h"
      29             : #include "php_variables.h"
      30             : #include "mbstring.h"
      31             : #include "ext/standard/php_string.h"
      32             : #include "ext/standard/php_mail.h"
      33             : #include "ext/standard/url.h"
      34             : #include "main/php_output.h"
      35             : #include "ext/standard/info.h"
      36             : 
      37             : #include "php_variables.h"
      38             : #include "php_globals.h"
      39             : #include "rfc1867.h"
      40             : #include "php_content_types.h"
      41             : #include "SAPI.h"
      42             : #include "TSRM.h"
      43             : 
      44             : #include "mb_gpc.h"
      45             : /* }}} */
      46             : 
      47             : #if HAVE_MBSTRING
      48             : 
      49             : ZEND_EXTERN_MODULE_GLOBALS(mbstring)
      50             : 
      51             : /* {{{ MBSTRING_API SAPI_TREAT_DATA_FUNC(mbstr_treat_data)
      52             :  * http input processing */
      53       38734 : MBSTRING_API SAPI_TREAT_DATA_FUNC(mbstr_treat_data)
      54             : {
      55       38734 :         char *res = NULL, *separator=NULL;
      56             :         const char *c_var;
      57             :         zval *array_ptr;
      58       38734 :         int free_buffer=0;
      59             :         enum mbfl_no_encoding detected;
      60             :         php_mb_encoding_handler_info_t info;
      61             : 
      62       38734 :         if (arg != PARSE_STRING) {
      63       38704 :                 char *value = zend_ini_string("mbstring.internal_encoding", sizeof("mbstring.internal_encoding"), 0);
      64       38704 :                 _php_mb_ini_mbstring_internal_encoding_set(value, value ? strlen(value): 0 TSRMLS_CC);
      65             :         }
      66             : 
      67       38734 :         if (!MBSTRG(encoding_translation)) {
      68       38709 :                 php_default_treat_data(arg, str, destArray TSRMLS_CC);
      69       38709 :                 return;
      70             :         }
      71             : 
      72          25 :         switch (arg) {
      73             :                 case PARSE_POST:
      74             :                 case PARSE_GET:
      75             :                 case PARSE_COOKIE:
      76          25 :                         ALLOC_ZVAL(array_ptr);
      77          25 :                         array_init(array_ptr);
      78          25 :                         INIT_PZVAL(array_ptr);
      79          25 :                         switch (arg) {
      80             :                                 case PARSE_POST:
      81           3 :                                         PG(http_globals)[TRACK_VARS_POST] = array_ptr;
      82           3 :                                         break;
      83             :                                 case PARSE_GET:
      84          11 :                                         PG(http_globals)[TRACK_VARS_GET] = array_ptr;
      85          11 :                                         break;
      86             :                                 case PARSE_COOKIE:
      87          11 :                                         PG(http_globals)[TRACK_VARS_COOKIE] = array_ptr;
      88             :                                         break;
      89             :                         }
      90          25 :                         break;
      91             :                 default:
      92           0 :                         array_ptr=destArray;
      93             :                         break;
      94             :         }
      95             : 
      96          25 :         if (arg==PARSE_POST) { 
      97           3 :                 sapi_handle_post(array_ptr TSRMLS_CC);
      98           3 :                 return;
      99             :         }
     100             : 
     101          22 :         if (arg == PARSE_GET) {         /* GET data */
     102          11 :                 c_var = SG(request_info).query_string;
     103          14 :                 if (c_var && *c_var) {
     104           3 :                         res = (char *) estrdup(c_var);
     105           3 :                         free_buffer = 1;
     106             :                 } else {
     107           8 :                         free_buffer = 0;
     108             :                 }
     109          11 :         } else if (arg == PARSE_COOKIE) {               /* Cookie data */
     110          11 :                 c_var = SG(request_info).cookie_data;
     111          11 :                 if (c_var && *c_var) {
     112           0 :                         res = (char *) estrdup(c_var);
     113           0 :                         free_buffer = 1;
     114             :                 } else {
     115          11 :                         free_buffer = 0;
     116             :                 }
     117           0 :         } else if (arg == PARSE_STRING) {               /* String data */
     118           0 :                 res = str;
     119           0 :                 free_buffer = 1;
     120             :         }
     121             : 
     122          22 :         if (!res) {
     123          19 :                 return;
     124             :         }
     125             : 
     126           3 :         switch (arg) {
     127             :         case PARSE_POST:
     128             :         case PARSE_GET:
     129             :         case PARSE_STRING:
     130           3 :                 separator = (char *) estrdup(PG(arg_separator).input);
     131           3 :                 break;
     132             :         case PARSE_COOKIE:
     133           0 :                 separator = ";\0";
     134             :                 break;
     135             :         }
     136             :         
     137           3 :         switch(arg) {
     138             :         case PARSE_POST:
     139           0 :                 MBSTRG(http_input_identify_post) = mbfl_no_encoding_invalid;
     140           0 :                 break;
     141             :         case PARSE_GET:
     142           3 :                 MBSTRG(http_input_identify_get) = mbfl_no_encoding_invalid;
     143           3 :                 break;
     144             :         case PARSE_COOKIE:
     145           0 :                 MBSTRG(http_input_identify_cookie) = mbfl_no_encoding_invalid;
     146           0 :                 break;
     147             :         case PARSE_STRING:
     148           0 :                 MBSTRG(http_input_identify_string) = mbfl_no_encoding_invalid;
     149             :                 break;
     150             :         }
     151             : 
     152           3 :         info.data_type              = arg;
     153           3 :         info.separator              = separator; 
     154           3 :         info.force_register_globals = 0;
     155           3 :         info.report_errors          = 0;
     156           3 :         info.to_encoding            = MBSTRG(internal_encoding);
     157           3 :         info.to_language            = MBSTRG(language);
     158           3 :         info.from_encodings         = MBSTRG(http_input_list);
     159           3 :         info.num_from_encodings     = MBSTRG(http_input_list_size); 
     160           3 :         info.from_language          = MBSTRG(language);
     161             : 
     162           3 :         MBSTRG(illegalchars) = 0;
     163             : 
     164           3 :         detected = _php_mb_encoding_handler_ex(&info, array_ptr, res TSRMLS_CC);
     165           3 :         MBSTRG(http_input_identify) = detected;
     166             : 
     167           3 :         if (detected != mbfl_no_encoding_invalid) {
     168           3 :                 switch(arg){
     169             :                 case PARSE_POST:
     170           0 :                         MBSTRG(http_input_identify_post) = detected;
     171           0 :                         break;
     172             :                 case PARSE_GET:
     173           3 :                         MBSTRG(http_input_identify_get) = detected;
     174           3 :                         break;
     175             :                 case PARSE_COOKIE:
     176           0 :                         MBSTRG(http_input_identify_cookie) = detected;
     177           0 :                         break;
     178             :                 case PARSE_STRING:
     179           0 :                         MBSTRG(http_input_identify_string) = detected;
     180             :                         break;
     181             :                 }
     182             :         }
     183             : 
     184           3 :         if (arg != PARSE_COOKIE) {
     185           3 :                 efree(separator);
     186             :         }
     187             : 
     188           3 :         if (free_buffer) {
     189           3 :                 efree(res);
     190             :         }
     191             : }
     192             : /* }}} */
     193             : 
     194             : /* {{{ mbfl_no_encoding _php_mb_encoding_handler_ex() */
     195          23 : enum mbfl_no_encoding _php_mb_encoding_handler_ex(const php_mb_encoding_handler_info_t *info, zval *arg, char *res TSRMLS_DC)
     196             : {
     197             :         char *var, *val;
     198             :         const char *s1, *s2;
     199          23 :         char *strtok_buf = NULL, **val_list = NULL;
     200          23 :         zval *array_ptr = (zval *) arg;
     201          23 :         int n, num, *len_list = NULL;
     202             :         unsigned int val_len, new_val_len;
     203             :         mbfl_string string, resvar, resval;
     204          23 :         enum mbfl_no_encoding from_encoding = mbfl_no_encoding_invalid;
     205          23 :         mbfl_encoding_detector *identd = NULL; 
     206          23 :         mbfl_buffer_converter *convd = NULL;
     207          23 :         int prev_rg_state = 0;
     208             : 
     209          23 :         mbfl_string_init_set(&string, info->to_language, info->to_encoding);
     210          23 :         mbfl_string_init_set(&resvar, info->to_language, info->to_encoding);
     211          23 :         mbfl_string_init_set(&resval, info->to_language, info->to_encoding);
     212             : 
     213             :         /* register_globals stuff
     214             :          * XXX: this feature is going to be deprecated? */
     215             : 
     216          23 :         if (info->force_register_globals && !(prev_rg_state = PG(register_globals))) {
     217           8 :                 zend_alter_ini_entry("register_globals", sizeof("register_globals"), "1", sizeof("1")-1, PHP_INI_PERDIR, PHP_INI_STAGE_RUNTIME);
     218             :         }
     219             : 
     220          23 :         if (!res || *res == '\0') {
     221             :                 goto out;
     222             :         }
     223             :         
     224             :         /* count the variables(separators) contained in the "res".
     225             :          * separator may contain multiple separator chars.
     226             :          */
     227          23 :         num = 1;
     228         742 :         for (s1=res; *s1 != '\0'; s1++) {
     229        1648 :                 for (s2=info->separator; *s2 != '\0'; s2++) {
     230         929 :                         if (*s1 == *s2) {
     231          39 :                                 num++;
     232             :                         }       
     233             :                 }
     234             :         }
     235          23 :         num *= 2; /* need space for variable name and value */
     236             :         
     237          23 :         val_list = (char **)ecalloc(num, sizeof(char *));
     238          23 :         len_list = (int *)ecalloc(num, sizeof(int));
     239             : 
     240             :         /* split and decode the query */
     241          23 :         n = 0;
     242          23 :         strtok_buf = NULL;
     243          23 :         var = php_strtok_r(res, info->separator, &strtok_buf);
     244         108 :         while (var)  {
     245          62 :                 val = strchr(var, '=');
     246          62 :                 if (val) { /* have a value */
     247          54 :                         len_list[n] = php_url_decode(var, val-var);
     248          54 :                         val_list[n] = var;
     249          54 :                         n++;
     250             :                         
     251          54 :                         *val++ = '\0';
     252          54 :                         val_list[n] = val;
     253          54 :                         len_list[n] = php_url_decode(val, strlen(val));
     254             :                 } else {
     255           8 :                         len_list[n] = php_url_decode(var, strlen(var));
     256           8 :                         val_list[n] = var;
     257           8 :                         n++;
     258             :                         
     259           8 :                         val_list[n] = "";
     260           8 :                         len_list[n] = 0;
     261             :                 }
     262          62 :                 n++;
     263          62 :                 var = php_strtok_r(NULL, info->separator, &strtok_buf);
     264             :         } 
     265             : 
     266          23 :         if (n > (PG(max_input_vars) * 2)) {
     267           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
     268           2 :                 goto out;
     269             :         }
     270             : 
     271          21 :         num = n; /* make sure to process initilized vars only */
     272             :         
     273             :         /* initialize converter */
     274          21 :         if (info->num_from_encodings <= 0) {
     275           0 :                 from_encoding = mbfl_no_encoding_pass;
     276          21 :         } else if (info->num_from_encodings == 1) {
     277          21 :                 from_encoding = info->from_encodings[0];
     278             :         } else {
     279             :                 /* auto detect */
     280           0 :                 from_encoding = mbfl_no_encoding_invalid;
     281           0 :                 identd = mbfl_encoding_detector_new((enum mbfl_no_encoding *)info->from_encodings, info->num_from_encodings, MBSTRG(strict_detection));
     282           0 :                 if (identd) {
     283           0 :                         n = 0;
     284           0 :                         while (n < num) {
     285           0 :                                 string.val = (unsigned char *)val_list[n];
     286           0 :                                 string.len = len_list[n];
     287           0 :                                 if (mbfl_encoding_detector_feed(identd, &string)) {
     288           0 :                                         break;
     289             :                                 }
     290           0 :                                 n++;
     291             :                         }
     292           0 :                         from_encoding = mbfl_encoding_detector_judge(identd);
     293           0 :                         mbfl_encoding_detector_delete(identd);
     294             :                 }
     295           0 :                 if (from_encoding == mbfl_no_encoding_invalid) {
     296           0 :                         if (info->report_errors) {
     297           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to detect encoding");
     298             :                         }
     299           0 :                         from_encoding = mbfl_no_encoding_pass;
     300             :                 }
     301             :         }
     302             : 
     303          21 :         convd = NULL;
     304          21 :         if (from_encoding != mbfl_no_encoding_pass) {
     305           3 :                 convd = mbfl_buffer_converter_new(from_encoding, info->to_encoding, 0);
     306           3 :                 if (convd != NULL) {
     307           3 :                         mbfl_buffer_converter_illegal_mode(convd, MBSTRG(current_filter_illegal_mode));
     308           3 :                         mbfl_buffer_converter_illegal_substchar(convd, MBSTRG(current_filter_illegal_substchar));
     309             :                 } else {
     310           0 :                         if (info->report_errors) {
     311           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create converter");
     312             :                         }
     313           0 :                         goto out;
     314             :                 }
     315             :         }
     316             : 
     317             :         /* convert encoding */
     318          21 :         string.no_encoding = from_encoding;
     319             : 
     320          21 :         n = 0;
     321          93 :         while (n < num) {
     322          51 :                 string.val = (unsigned char *)val_list[n];
     323          51 :                 string.len = len_list[n];
     324          54 :                 if (convd != NULL && mbfl_buffer_converter_feed_result(convd, &string, &resvar) != NULL) {
     325           3 :                         var = (char *)resvar.val;
     326             :                 } else {
     327          48 :                         var = val_list[n];
     328             :                 }
     329          51 :                 n++;
     330          51 :                 string.val = val_list[n];
     331          51 :                 string.len = len_list[n];
     332          54 :                 if (convd != NULL && mbfl_buffer_converter_feed_result(convd, &string, &resval) != NULL) {
     333           3 :                         val = resval.val;
     334           3 :                         val_len = resval.len;
     335             :                 } else {
     336          48 :                         val = val_list[n];
     337          48 :                         val_len = len_list[n];
     338             :                 }
     339          51 :                 n++;
     340             :                 /* we need val to be emalloc()ed */
     341          51 :                 val = estrndup(val, val_len);
     342          51 :                 if (sapi_module.input_filter(info->data_type, var, &val, val_len, &new_val_len TSRMLS_CC)) {
     343             :                         /* add variable to symbol table */
     344          45 :                         php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
     345             :                 }
     346          51 :                 efree(val);
     347             :                 
     348          51 :                 if (convd != NULL){
     349           3 :                         mbfl_string_clear(&resvar);
     350           3 :                         mbfl_string_clear(&resval);
     351             :                 }
     352             :         }
     353             : 
     354             : out:
     355             :         /* register_global stuff */
     356          23 :         if (info->force_register_globals && !prev_rg_state) {
     357           8 :                 zend_alter_ini_entry("register_globals", sizeof("register_globals"), "0", sizeof("0")-1, PHP_INI_PERDIR, PHP_INI_STAGE_RUNTIME);
     358             :         }
     359             : 
     360          23 :         if (convd != NULL) {
     361           3 :                 MBSTRG(illegalchars) += mbfl_buffer_illegalchars(convd);
     362           3 :                 mbfl_buffer_converter_delete(convd);
     363             :         }
     364          23 :         if (val_list != NULL) {
     365          23 :                 efree((void *)val_list);
     366             :         }
     367          23 :         if (len_list != NULL) {
     368          23 :                 efree((void *)len_list);
     369             :         }
     370             : 
     371          23 :         return from_encoding;
     372             : }
     373             : /* }}} */
     374             : 
     375             : /* {{{ SAPI_POST_HANDLER_FUNC(php_mb_post_handler) */
     376           3 : SAPI_POST_HANDLER_FUNC(php_mb_post_handler)
     377             : {
     378             :         enum mbfl_no_encoding detected;
     379             :         php_mb_encoding_handler_info_t info;
     380             : 
     381           3 :         MBSTRG(http_input_identify_post) = mbfl_no_encoding_invalid;
     382             : 
     383           3 :         info.data_type              = PARSE_POST;
     384           3 :         info.separator              = "&";
     385           3 :         info.force_register_globals = 0;
     386           3 :         info.report_errors          = 0;
     387           3 :         info.to_encoding            = MBSTRG(internal_encoding);
     388           3 :         info.to_language            = MBSTRG(language);
     389           3 :         info.from_encodings         = MBSTRG(http_input_list);
     390           3 :         info.num_from_encodings     = MBSTRG(http_input_list_size); 
     391           3 :         info.from_language          = MBSTRG(language);
     392             : 
     393           3 :         detected = _php_mb_encoding_handler_ex(&info, arg, SG(request_info).post_data TSRMLS_CC);
     394             : 
     395           3 :         MBSTRG(http_input_identify) = detected;
     396           3 :         if (detected != mbfl_no_encoding_invalid) {
     397           1 :                 MBSTRG(http_input_identify_post) = detected;
     398             :         }
     399           3 : }
     400             : /* }}} */
     401             : 
     402             : #endif /* HAVE_MBSTRING */
     403             : 
     404             : /*
     405             :  * Local variables:
     406             :  * tab-width: 4
     407             :  * c-basic-offset: 4
     408             :  * End:
     409             :  * vim600: fdm=marker
     410             :  * vim: noet sw=4 ts=4
     411             :  */
     412             : 

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:29 +0000 (5 days ago)

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