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

Generated by: LCOV version 1.10

Generated at Sun, 21 Dec 2014 04:58:52 +0000 (17 hours ago)

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