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

LTP GCOV extension - code coverage report
Current view: directory - mbstring - mb_gpc.c
Test: PHP Code Coverage
Date: 2009-11-23 Instrumented lines: 193
Code covered: 54.4 % Executed lines: 105
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5

Generated at Mon, 23 Nov 2009 17:39:32 +0000 (33 hours ago)

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