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

Generated by: LCOV version 1.10

Generated at Fri, 19 Sep 2014 17:11:10 +0000 (3 days ago)

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