LTP GCOV extension - code coverage report
Current view: directory - ext/standard - var.c
Test: PHP Code Coverage
Date: 2007-04-10 Instrumented lines: 449
Code covered: 29.8 % Executed lines: 134
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2007 The PHP Group                                |
       6                 :    +----------------------------------------------------------------------+
       7                 :    | This source file is subject to version 3.01 of the PHP license,      |
       8                 :    | that is bundled with this package in the file LICENSE, and is        |
       9                 :    | available through the world-wide-web at the following url:           |
      10                 :    | http://www.php.net/license/3_01.txt                                  |
      11                 :    | If you did not receive a copy of the PHP license and are unable to   |
      12                 :    | obtain it through the world-wide-web, please send a note to          |
      13                 :    | license@php.net so we can mail you a copy immediately.               |
      14                 :    +----------------------------------------------------------------------+
      15                 :    | Authors: Jani Lehtimäki <jkl@njet.net>                               |
      16                 :    |          Thies C. Arntzen <thies@thieso.net>                         |
      17                 :    |          Sascha Schumann <sascha@schumann.cx>                        |
      18                 :    +----------------------------------------------------------------------+
      19                 : */
      20                 : 
      21                 : /* $Id: var.c,v 1.203.2.7.2.15 2007/02/16 03:41:56 iliaa Exp $ */
      22                 : 
      23                 : 
      24                 : 
      25                 : /* {{{ includes 
      26                 : */
      27                 : 
      28                 : #include <stdio.h>
      29                 : #include <stdlib.h>
      30                 : #include <errno.h>
      31                 : #include "php.h"
      32                 : #include "php_string.h"
      33                 : #include "php_var.h"
      34                 : #include "php_smart_str.h"
      35                 : #include "basic_functions.h"
      36                 : #include "php_incomplete_class.h"
      37                 : 
      38                 : #define COMMON ((*struc)->is_ref ? "&" : "")
      39                 : #define Z_REFCOUNT_PP(a) ((*a)->refcount)
      40                 : 
      41                 : /* }}} */
      42                 : /* {{{ php_var_dump */
      43                 : 
      44                 : static int php_array_element_dump(zval **zv, int num_args, va_list args, zend_hash_key *hash_key)
      45             110 : {
      46                 :         int level;
      47                 :         TSRMLS_FETCH();
      48                 : 
      49             110 :         level = va_arg(args, int);
      50                 : 
      51             110 :         if (hash_key->nKeyLength==0) { /* numeric key */
      52              44 :                 php_printf("%*c[%ld]=>\n", level + 1, ' ', hash_key->h);
      53                 :         } else { /* string key */
      54              66 :                 php_printf("%*c[\"", level + 1, ' ');
      55              66 :                 PHPWRITE(hash_key->arKey, hash_key->nKeyLength - 1);
      56              66 :                 php_printf("\"]=>\n");
      57                 :         }
      58             110 :         php_var_dump(zv, level + 2 TSRMLS_CC);
      59             110 :         return 0;
      60                 : }
      61                 : 
      62                 : static int php_object_property_dump(zval **zv, int num_args, va_list args, zend_hash_key *hash_key)
      63              72 : {
      64                 :         int level;
      65                 :         char *prop_name, *class_name;
      66                 :         TSRMLS_FETCH();
      67                 : 
      68              72 :         level = va_arg(args, int);
      69                 : 
      70              72 :         if (hash_key->nKeyLength ==0 ) { /* numeric key */
      71               6 :                 php_printf("%*c[%ld]=>\n", level + 1, ' ', hash_key->h);
      72                 :         } else { /* string key */
      73              66 :                 int unmangle = zend_unmangle_property_name(hash_key->arKey, hash_key->nKeyLength-1, &class_name, &prop_name);
      74              83 :                 if (class_name && unmangle == SUCCESS) {
      75              17 :                         php_printf("%*c[\"%s", level + 1, ' ', prop_name);
      76              17 :                         if (class_name[0]=='*') {
      77               9 :                                 ZEND_PUTS(":protected");
      78                 :                         } else {
      79               8 :                                 ZEND_PUTS(":private");
      80                 :                         }
      81                 :                 } else {
      82              49 :                         php_printf("%*c[\"", level + 1, ' ');
      83              49 :                         PHPWRITE(hash_key->arKey, hash_key->nKeyLength - 1);
      84                 : #ifdef ANDREY_0
      85                 :                         ZEND_PUTS(":public");
      86                 : #endif
      87                 :                 }
      88              66 :                 ZEND_PUTS("\"]=>\n");
      89                 :         }
      90              72 :         php_var_dump(zv, level + 2 TSRMLS_CC);
      91              72 :         return 0;
      92                 : }
      93                 : 
      94                 : 
      95                 : PHPAPI void php_var_dump(zval **struc, int level TSRMLS_DC)
      96             411 : {
      97             411 :         HashTable *myht = NULL;
      98                 :         char *class_name;
      99                 :         zend_uint class_name_len;
     100                 :         int (*php_element_dump_func)(zval**, int, va_list, zend_hash_key*);
     101                 : 
     102             411 :         if (level > 1) {
     103             182 :                 php_printf("%*c", level - 1, ' ');
     104                 :         }
     105                 : 
     106             411 :         switch (Z_TYPE_PP(struc)) {
     107                 :         case IS_BOOL:
     108              35 :                 php_printf("%sbool(%s)\n", COMMON, Z_LVAL_PP(struc)?"true":"false");
     109              35 :                 break;
     110                 :         case IS_NULL:
     111               9 :                 php_printf("%sNULL\n", COMMON);
     112               9 :                 break;
     113                 :         case IS_LONG:
     114             139 :                 php_printf("%sint(%ld)\n", COMMON, Z_LVAL_PP(struc));
     115             139 :                 break;
     116                 :         case IS_DOUBLE:
     117              14 :                 php_printf("%sfloat(%.*G)\n", COMMON, (int) EG(precision), Z_DVAL_PP(struc));
     118              14 :                 break;
     119                 :         case IS_STRING:
     120             143 :                 php_printf("%sstring(%d) \"", COMMON, Z_STRLEN_PP(struc));
     121             143 :                 PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc));
     122             143 :                 PUTS("\"\n");
     123             143 :                 break;
     124                 :         case IS_ARRAY:
     125              53 :                 myht = Z_ARRVAL_PP(struc);
     126              53 :                 if (myht->nApplyCount > 1) {
     127               0 :                         PUTS("*RECURSION*\n");
     128               0 :                         return;
     129                 :                 }
     130              53 :                 php_printf("%sarray(%d) {\n", COMMON, zend_hash_num_elements(myht));
     131              53 :                 php_element_dump_func = php_array_element_dump;
     132              53 :                 goto head_done;
     133                 :         case IS_OBJECT:
     134              18 :                 myht = Z_OBJPROP_PP(struc);
     135              18 :                 if (myht && myht->nApplyCount > 1) {
     136               0 :                         PUTS("*RECURSION*\n");
     137               0 :                         return;
     138                 :                 }
     139                 : 
     140              18 :                 Z_OBJ_HANDLER(**struc, get_class_name)(*struc, &class_name, &class_name_len, 0 TSRMLS_CC);
     141              18 :                 php_printf("%sobject(%s)#%d (%d) {\n", COMMON, class_name, Z_OBJ_HANDLE_PP(struc), myht ? zend_hash_num_elements(myht) : 0);
     142              18 :                 efree(class_name);
     143              18 :                 php_element_dump_func = php_object_property_dump;
     144              71 : head_done:
     145              71 :                 if (myht) {
     146              71 :                         zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_element_dump_func, 1, level);
     147                 :                 }
     148              71 :                 if (level > 1) {
     149              42 :                         php_printf("%*c", level-1, ' ');
     150                 :                 }
     151              71 :                 PUTS("}\n");
     152              71 :                 break;
     153                 :         case IS_RESOURCE: {
     154                 :                 char *type_name;
     155                 : 
     156               0 :                 type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(struc) TSRMLS_CC);
     157               0 :                 php_printf("%sresource(%ld) of type (%s)\n", COMMON, Z_LVAL_PP(struc), type_name ? type_name : "Unknown");
     158               0 :                 break;
     159                 :         }
     160                 :         default:
     161               0 :                 php_printf("%sUNKNOWN:0\n", COMMON);
     162                 :                 break;
     163                 :         }
     164                 : }
     165                 : 
     166                 : /* }}} */
     167                 : 
     168                 : 
     169                 : 
     170                 : /* {{{ proto void var_dump(mixed var)
     171                 :    Dumps a string representation of variable to output */
     172                 : PHP_FUNCTION(var_dump)
     173             227 : {
     174                 :         zval ***args;
     175                 :         int argc;
     176                 :         int     i;
     177                 :         
     178             227 :         argc = ZEND_NUM_ARGS();
     179                 :         
     180             227 :         args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
     181             227 :         if (ZEND_NUM_ARGS() == 0 || zend_get_parameters_array_ex(argc, args) == FAILURE) {
     182               0 :                 efree(args);
     183               0 :                 WRONG_PARAM_COUNT;
     184                 :         }
     185                 :         
     186             456 :         for (i=0; i<argc; i++)
     187             229 :                 php_var_dump(args[i], 1 TSRMLS_CC);
     188                 :         
     189             227 :         efree(args);
     190                 : }
     191                 : /* }}} */
     192                 : 
     193                 : /* {{{ debug_zval_dump */
     194                 : 
     195                 : static int zval_array_element_dump(zval **zv, int num_args, va_list args, zend_hash_key *hash_key)
     196               0 : {
     197                 :         int level;
     198                 :         TSRMLS_FETCH();
     199                 : 
     200               0 :         level = va_arg(args, int);
     201                 : 
     202               0 :         if (hash_key->nKeyLength==0) { /* numeric key */
     203               0 :                 php_printf("%*c[%ld]=>\n", level + 1, ' ', hash_key->h);
     204                 :         } else { /* string key */
     205                 :                 /* XXX: perphaps when we are inside the class we should permit access to 
     206                 :                  * private & protected values
     207                 :                  */
     208               0 :                 if (va_arg(args, int) && hash_key->arKey[0] == '\0') {
     209               0 :                         return 0;
     210                 :                 }
     211               0 :                 php_printf("%*c[\"", level + 1, ' ');
     212               0 :                 PHPWRITE(hash_key->arKey, hash_key->nKeyLength - 1);
     213               0 :                 php_printf("\"]=>\n");
     214                 :         }
     215               0 :         php_debug_zval_dump(zv, level + 2 TSRMLS_CC);
     216               0 :         return 0;
     217                 : }
     218                 : 
     219                 : static int zval_object_property_dump(zval **zv, int num_args, va_list args, zend_hash_key *hash_key)
     220               0 : {
     221                 :         int level;
     222                 :         char *prop_name, *class_name;
     223                 :         TSRMLS_FETCH();
     224                 : 
     225               0 :         level = va_arg(args, int);
     226                 : 
     227               0 :         if (hash_key->nKeyLength ==0 ) { /* numeric key */
     228               0 :                 php_printf("%*c[%ld]=>\n", level + 1, ' ', hash_key->h);
     229                 :         } else { /* string key */
     230               0 :                 zend_unmangle_property_name(hash_key->arKey, hash_key->nKeyLength-1, &class_name, &prop_name);
     231               0 :                 if (class_name) {
     232               0 :                         php_printf("%*c[\"%s", level + 1, ' ', prop_name);
     233               0 :                         if (class_name[0]=='*') {
     234               0 :                                 ZEND_PUTS(":protected");
     235                 :                         } else {
     236               0 :                                 ZEND_PUTS(":private");
     237                 :                         }
     238                 :                 } else {
     239               0 :                         php_printf("%*c[\"%s", level + 1, ' ', hash_key->arKey);
     240                 : #ifdef ANDREY_0
     241                 :                         ZEND_PUTS(":public");
     242                 : #endif
     243                 :                 }
     244               0 :                 ZEND_PUTS("\"]=>\n");
     245                 :         }
     246               0 :         php_debug_zval_dump(zv, level + 2 TSRMLS_CC);
     247               0 :         return 0;
     248                 : }
     249                 : 
     250                 : PHPAPI void php_debug_zval_dump(zval **struc, int level TSRMLS_DC)
     251               0 : {
     252               0 :         HashTable *myht = NULL;
     253                 :         char *class_name;
     254                 :         zend_uint class_name_len;
     255                 :         zend_class_entry *ce;
     256                 :         int (*zval_element_dump_func)(zval**, int, va_list, zend_hash_key*);
     257                 : 
     258               0 :         if (level > 1) {
     259               0 :                 php_printf("%*c", level - 1, ' ');
     260                 :         }
     261                 : 
     262               0 :         switch (Z_TYPE_PP(struc)) {
     263                 :         case IS_BOOL:
     264               0 :                 php_printf("%sbool(%s) refcount(%u)\n", COMMON, Z_LVAL_PP(struc)?"true":"false", Z_REFCOUNT_PP(struc));
     265               0 :                 break;
     266                 :         case IS_NULL:
     267               0 :                 php_printf("%sNULL refcount(%u)\n", COMMON, Z_REFCOUNT_PP(struc));
     268               0 :                 break;
     269                 :         case IS_LONG:
     270               0 :                 php_printf("%slong(%ld) refcount(%u)\n", COMMON, Z_LVAL_PP(struc), Z_REFCOUNT_PP(struc));
     271               0 :                 break;
     272                 :         case IS_DOUBLE:
     273               0 :                 php_printf("%sdouble(%.*G) refcount(%u)\n", COMMON, (int) EG(precision), Z_DVAL_PP(struc), Z_REFCOUNT_PP(struc));
     274               0 :                 break;
     275                 :         case IS_STRING:
     276               0 :                 php_printf("%sstring(%d) \"", COMMON, Z_STRLEN_PP(struc));
     277               0 :                 PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc));
     278               0 :                 php_printf("\" refcount(%u)\n", Z_REFCOUNT_PP(struc));
     279               0 :                 break;
     280                 :         case IS_ARRAY:
     281               0 :                 myht = Z_ARRVAL_PP(struc);
     282               0 :                 if (myht->nApplyCount > 1) {
     283               0 :                         PUTS("*RECURSION*\n");
     284               0 :                         return;
     285                 :                 }
     286               0 :                 php_printf("%sarray(%d) refcount(%u){\n", COMMON, zend_hash_num_elements(myht), Z_REFCOUNT_PP(struc));
     287               0 :                 zval_element_dump_func = zval_array_element_dump;
     288               0 :                 goto head_done;
     289                 :         case IS_OBJECT:
     290               0 :                 myht = Z_OBJPROP_PP(struc);
     291               0 :                 if (myht && myht->nApplyCount > 1) {
     292               0 :                         PUTS("*RECURSION*\n");
     293               0 :                         return;
     294                 :                 }
     295               0 :                 ce = Z_OBJCE(**struc);
     296               0 :                 Z_OBJ_HANDLER(**struc, get_class_name)(*struc, &class_name, &class_name_len, 0 TSRMLS_CC);
     297               0 :                 php_printf("%sobject(%s)#%d (%d) refcount(%u){\n", COMMON, class_name, Z_OBJ_HANDLE_PP(struc), myht ? zend_hash_num_elements(myht) : 0, Z_REFCOUNT_PP(struc));
     298               0 :                 efree(class_name);
     299               0 :                 zval_element_dump_func = zval_object_property_dump;
     300               0 : head_done:
     301               0 :                 if (myht) {
     302               0 :                         zend_hash_apply_with_arguments(myht, (apply_func_args_t) zval_element_dump_func, 1, level, (Z_TYPE_PP(struc) == IS_ARRAY ? 0 : 1));
     303                 :                 }
     304               0 :                 if (level > 1) {
     305               0 :                         php_printf("%*c", level-1, ' ');
     306                 :                 }
     307               0 :                 PUTS("}\n");
     308               0 :                 break;
     309                 :         case IS_RESOURCE: {
     310                 :                 char *type_name;
     311                 : 
     312               0 :                 type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(struc) TSRMLS_CC);
     313               0 :                 php_printf("%sresource(%ld) of type (%s) refcount(%u)\n", COMMON, Z_LVAL_PP(struc), type_name ? type_name : "Unknown", Z_REFCOUNT_PP(struc));
     314               0 :                 break;
     315                 :         }
     316                 :         default:
     317               0 :                 php_printf("%sUNKNOWN:0\n", COMMON);
     318                 :                 break;
     319                 :         }
     320                 : }
     321                 : 
     322                 : /* }}} */
     323                 : 
     324                 : /* {{{ proto void debug_zval_dump(mixed var)
     325                 :    Dumps a string representation of an internal zend value to output. */
     326                 : PHP_FUNCTION(debug_zval_dump)
     327               0 : {
     328                 :         zval ***args;
     329                 :         int argc;
     330                 :         int     i;
     331                 :         
     332               0 :         argc = ZEND_NUM_ARGS();
     333                 :         
     334               0 :         args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
     335               0 :         if (ZEND_NUM_ARGS() == 0 || zend_get_parameters_array_ex(argc, args) == FAILURE) {
     336               0 :                 efree(args);
     337               0 :                 WRONG_PARAM_COUNT;
     338                 :         }
     339                 :         
     340               0 :         for (i=0; i<argc; i++)
     341               0 :                 php_debug_zval_dump(args[i], 1 TSRMLS_CC);
     342                 :         
     343               0 :         efree(args);
     344                 : }
     345                 : /* }}} */
     346                 : 
     347                 : 
     348                 : /* {{{ php_var_export */
     349                 : 
     350                 : static int php_array_element_export(zval **zv, int num_args, va_list args, zend_hash_key *hash_key)
     351               0 : {
     352                 :         int level;
     353                 :         TSRMLS_FETCH();
     354                 : 
     355               0 :         level = va_arg(args, int);
     356                 : 
     357               0 :         if (hash_key->nKeyLength==0) { /* numeric key */
     358               0 :                 php_printf("%*c%ld => ", level + 1, ' ', hash_key->h);
     359                 :         } else { /* string key */
     360                 :                 char *key;
     361                 :                 int key_len;
     362               0 :                 key = php_addcslashes(hash_key->arKey, hash_key->nKeyLength - 1, &key_len, 0, "'\\", 2 TSRMLS_CC);
     363               0 :                 php_printf("%*c'", level + 1, ' ');
     364               0 :                 PHPWRITE(key, key_len);
     365               0 :                 php_printf("' => ");
     366               0 :                 efree(key);
     367                 :         }
     368               0 :         php_var_export(zv, level + 2 TSRMLS_CC);
     369               0 :         PUTS (",\n");
     370               0 :         return 0;
     371                 : }
     372                 : 
     373                 : static int php_object_element_export(zval **zv, int num_args, va_list args, zend_hash_key *hash_key)
     374               0 : {
     375                 :         int level;
     376                 :         char *prop_name, *class_name;
     377                 :         TSRMLS_FETCH();
     378                 : 
     379               0 :         level = va_arg(args, int);
     380                 : 
     381               0 :         if (hash_key->nKeyLength != 0) {
     382               0 :                 php_printf("%*c", level + 1, ' ');
     383               0 :                 zend_unmangle_property_name(hash_key->arKey, hash_key->nKeyLength-1, &class_name, &prop_name);
     384               0 :                 php_printf(" '%s' => ", prop_name);
     385               0 :                 php_var_export(zv, level + 2 TSRMLS_CC);
     386               0 :                 PUTS (",\n");
     387                 :         }
     388               0 :         return 0;
     389                 : }
     390                 : 
     391                 : PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC)
     392               0 : {
     393                 :         HashTable *myht;
     394                 :         char*     tmp_str;
     395                 :         int       tmp_len;
     396                 :         char *class_name;
     397                 :         zend_uint class_name_len;
     398                 : 
     399               0 :         switch (Z_TYPE_PP(struc)) {
     400                 :         case IS_BOOL:
     401               0 :                 php_printf("%s", Z_LVAL_PP(struc) ? "true" : "false");
     402               0 :                 break;
     403                 :         case IS_NULL:
     404               0 :                 php_printf("NULL");
     405               0 :                 break;
     406                 :         case IS_LONG:
     407               0 :                 php_printf("%ld", Z_LVAL_PP(struc));
     408               0 :                 break;
     409                 :         case IS_DOUBLE:
     410               0 :                 php_printf("%.*G", (int) EG(precision), Z_DVAL_PP(struc));
     411               0 :                 break;
     412                 :         case IS_STRING:
     413               0 :                 tmp_str = php_addcslashes(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc), &tmp_len, 0, "'\\\0", 3 TSRMLS_CC);
     414               0 :                 PUTS ("'");
     415               0 :                 PHPWRITE(tmp_str, tmp_len);
     416               0 :                 PUTS ("'");
     417               0 :                 efree (tmp_str);
     418               0 :                 break;
     419                 :         case IS_ARRAY:
     420               0 :                 myht = Z_ARRVAL_PP(struc);
     421               0 :                 if (level > 1) {
     422               0 :                         php_printf("\n%*c", level - 1, ' ');
     423                 :                 }
     424               0 :                 PUTS ("array (\n");
     425               0 :                 zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_array_element_export, 1, level, (Z_TYPE_PP(struc) == IS_ARRAY ? 0 : 1));
     426               0 :                 if (level > 1) {
     427               0 :                         php_printf("%*c", level - 1, ' ');
     428                 :                 }
     429               0 :                 PUTS(")");
     430               0 :                 break;
     431                 :         case IS_OBJECT:
     432               0 :                 myht = Z_OBJPROP_PP(struc);
     433               0 :                 if (level > 1) {
     434               0 :                         php_printf("\n%*c", level - 1, ' ');
     435                 :                 }
     436               0 :                 Z_OBJ_HANDLER(**struc, get_class_name)(*struc, &class_name, &class_name_len, 0 TSRMLS_CC);
     437               0 :                 php_printf ("%s::__set_state(array(\n", class_name);
     438               0 :                 efree(class_name);
     439               0 :                 if (myht) {
     440               0 :                         zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_object_element_export, 1, level);
     441                 :                 }
     442               0 :                 if (level > 1) {
     443               0 :                         php_printf("%*c", level - 1, ' ');
     444                 :                 }
     445               0 :                 php_printf ("))");
     446               0 :                 break;
     447                 :         default:
     448               0 :                 PUTS ("NULL");
     449                 :                 break;
     450                 :         }
     451               0 : }
     452                 : 
     453                 : /* }}} */
     454                 : 
     455                 : 
     456                 : /* {{{ proto mixed var_export(mixed var [, bool return])
     457                 :    Outputs or returns a string representation of a variable */
     458                 : PHP_FUNCTION(var_export)
     459               0 : {
     460                 :         zval *var;
     461               0 :         zend_bool return_output = 0;
     462                 :         
     463               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &var, &return_output) == FAILURE) {
     464               0 :                 return;
     465                 :         }
     466                 :         
     467               0 :         if (return_output) {
     468               0 :                 php_start_ob_buffer (NULL, 0, 1 TSRMLS_CC);
     469                 :         }
     470                 :         
     471               0 :         php_var_export(&var, 1 TSRMLS_CC);
     472                 : 
     473               0 :         if (return_output) {
     474               0 :                 php_ob_get_buffer (return_value TSRMLS_CC);
     475               0 :                 php_end_ob_buffer (0, 0 TSRMLS_CC);
     476                 :         }
     477                 : }
     478                 : /* }}} */
     479                 : 
     480                 : 
     481                 : 
     482                 : /* {{{ php_var_serialize */
     483                 : 
     484                 : static void php_var_serialize_intern(smart_str *buf, zval *struc, HashTable *var_hash TSRMLS_DC);
     485                 : 
     486                 : static inline int php_add_var_hash(HashTable *var_hash, zval *var, void *var_old TSRMLS_DC)
     487              10 : {
     488                 :         ulong var_no;
     489                 :         char id[32], *p;
     490                 :         register int len;
     491                 : 
     492                 :         /* relies on "(long)" being a perfect hash function for data pointers,
     493                 :            however the actual identity of an object has had to be determined
     494                 :            by its object handle and the class entry since 5.0. */
     495              10 :         if ((Z_TYPE_P(var) == IS_OBJECT) && Z_OBJ_HT_P(var)->get_class_entry) {
     496               0 :                 p = smart_str_print_long(id + sizeof(id) - 1,
     497                 :                                 (((unsigned long)Z_OBJCE_P(var) << 5)
     498                 :                                 | ((unsigned long)Z_OBJCE_P(var) >> (sizeof(long) * 8 - 5)))
     499                 :                                 + (long) Z_OBJ_HANDLE_P(var));
     500               0 :                 *(--p) = 'O';
     501               0 :                 len = id + sizeof(id) - 1 - p;
     502                 :         } else {
     503              10 :                 p = smart_str_print_long(id + sizeof(id) - 1, (long) var);
     504              10 :                 len = id + sizeof(id) - 1 - p;
     505                 :         }
     506                 : 
     507              10 :         if (var_old && zend_hash_find(var_hash, p, len, var_old) == SUCCESS) {
     508               0 :                 if (!var->is_ref) {
     509                 :                         /* we still need to bump up the counter, since non-refs will
     510                 :                            be counted separately by unserializer */
     511               0 :                         var_no = -1;
     512               0 :                         zend_hash_next_index_insert(var_hash, &var_no, sizeof(var_no), NULL);
     513                 :                 }
     514               0 :                 return FAILURE;
     515                 :         }
     516                 :         
     517                 :         /* +1 because otherwise hash will think we are trying to store NULL pointer */
     518              10 :         var_no = zend_hash_num_elements(var_hash) + 1;
     519              10 :         zend_hash_add(var_hash, p, len, &var_no, sizeof(var_no), NULL);
     520              10 :         return SUCCESS;
     521                 : }
     522                 : 
     523                 : static inline void php_var_serialize_long(smart_str *buf, long val)
     524               2 : {
     525               2 :         smart_str_appendl(buf, "i:", 2);
     526               2 :         smart_str_append_long(buf, val);
     527               2 :         smart_str_appendc(buf, ';');
     528               2 : }
     529                 : 
     530                 : static inline void php_var_serialize_string(smart_str *buf, char *str, int len)
     531              12 : {
     532              12 :         smart_str_appendl(buf, "s:", 2);
     533              12 :         smart_str_append_long(buf, len);
     534              12 :         smart_str_appendl(buf, ":\"", 2);
     535              12 :         smart_str_appendl(buf, str, len);
     536              12 :         smart_str_appendl(buf, "\";", 2);
     537              12 : }
     538                 : 
     539                 : static inline zend_bool php_var_serialize_class_name(smart_str *buf, zval *struc TSRMLS_DC)
     540               0 : {
     541               0 :         PHP_CLASS_ATTRIBUTES;
     542                 : 
     543               0 :         PHP_SET_CLASS_ATTRIBUTES(struc);
     544               0 :         smart_str_appendl(buf, "O:", 2);
     545               0 :         smart_str_append_long(buf, name_len);
     546               0 :         smart_str_appendl(buf, ":\"", 2);
     547               0 :         smart_str_appendl(buf, class_name, name_len);
     548               0 :         smart_str_appendl(buf, "\":", 2);
     549               0 :         PHP_CLEANUP_CLASS_ATTRIBUTES();
     550               0 :         return incomplete_class;
     551                 : }
     552                 : 
     553                 : static void php_var_serialize_class(smart_str *buf, zval *struc, zval *retval_ptr, HashTable *var_hash TSRMLS_DC)
     554               0 : {
     555                 :         int count;
     556                 :         zend_bool  incomplete_class;
     557                 : 
     558               0 :         incomplete_class = php_var_serialize_class_name(buf, struc TSRMLS_CC);
     559                 :         /* count after serializing name, since php_var_serialize_class_name
     560                 :            changes the count if the variable is incomplete class */
     561               0 :         count = zend_hash_num_elements(HASH_OF(retval_ptr));
     562               0 :         if (incomplete_class) {
     563               0 :                 --count;
     564                 :         }
     565               0 :         smart_str_append_long(buf, count);
     566               0 :         smart_str_appendl(buf, ":{", 2);
     567                 : 
     568               0 :         if (count > 0) {
     569                 :                 char *key;
     570                 :                 zval **d, **name;
     571                 :                 ulong index;
     572                 :                 HashPosition pos;
     573                 :                 int i;
     574                 :                 zval nval, *nvalp;
     575                 : 
     576               0 :                 ZVAL_NULL(&nval);
     577               0 :                 nvalp = &nval;
     578                 : 
     579               0 :                 zend_hash_internal_pointer_reset_ex(HASH_OF(retval_ptr), &pos);
     580                 :         
     581               0 :                 for (;; zend_hash_move_forward_ex(HASH_OF(retval_ptr), &pos)) {
     582               0 :                         i = zend_hash_get_current_key_ex(HASH_OF(retval_ptr), &key, NULL, 
     583                 :                                         &index, 0, &pos);
     584                 :                         
     585               0 :                         if (i == HASH_KEY_NON_EXISTANT)
     586               0 :                                 break;
     587                 : 
     588               0 :                         if (incomplete_class && strcmp(key, MAGIC_MEMBER) == 0) {
     589               0 :                                 continue;
     590                 :                         }
     591               0 :                         zend_hash_get_current_data_ex(HASH_OF(retval_ptr), 
     592                 :                                         (void **) &name, &pos);
     593                 : 
     594               0 :                         if (Z_TYPE_PP(name) != IS_STRING) {
     595               0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "__sleep should return an array only "
     596                 :                                                 "containing the names of instance-variables to "
     597                 :                                                 "serialize.");
     598                 :                                 /* we should still add element even if it's not OK,
     599                 :                                    since we already wrote the length of the array before */
     600               0 :                                 smart_str_appendl(buf,"N;", 2);
     601               0 :                                 continue;
     602                 :                         }
     603               0 :                         if (zend_hash_find(Z_OBJPROP_P(struc), Z_STRVAL_PP(name), 
     604                 :                                                 Z_STRLEN_PP(name) + 1, (void *) &d) == SUCCESS) {
     605               0 :                                 php_var_serialize_string(buf, Z_STRVAL_PP(name), Z_STRLEN_PP(name));
     606               0 :                                 php_var_serialize_intern(buf, *d, var_hash TSRMLS_CC);
     607                 :                         } else {
     608                 :                                 zend_class_entry *ce;
     609               0 :                                 ce = zend_get_class_entry(struc TSRMLS_CC);
     610               0 :                                 if (ce) {
     611                 :                                         char *prot_name, *priv_name;
     612                 :                                         int prop_name_length;
     613                 :                                         
     614                 :                                         do {
     615               0 :                                                 zend_mangle_property_name(&priv_name, &prop_name_length, ce->name, ce->name_length, 
     616                 :                                                                         Z_STRVAL_PP(name), Z_STRLEN_PP(name), ce->type & ZEND_INTERNAL_CLASS);
     617               0 :                                                 if (zend_hash_find(Z_OBJPROP_P(struc), priv_name, prop_name_length+1, (void *) &d) == SUCCESS) {
     618               0 :                                                         php_var_serialize_string(buf, priv_name, prop_name_length);
     619               0 :                                                         efree(priv_name);
     620               0 :                                                         php_var_serialize_intern(buf, *d, var_hash TSRMLS_CC);
     621               0 :                                                         break;
     622                 :                                                 }
     623               0 :                                                 efree(priv_name);
     624               0 :                                                 zend_mangle_property_name(&prot_name, &prop_name_length,  "*", 1, 
     625                 :                                                                         Z_STRVAL_PP(name), Z_STRLEN_PP(name), ce->type & ZEND_INTERNAL_CLASS);
     626               0 :                                                 if (zend_hash_find(Z_OBJPROP_P(struc), prot_name, prop_name_length+1, (void *) &d) == SUCCESS) {
     627               0 :                                                         php_var_serialize_string(buf, prot_name, prop_name_length);
     628               0 :                                                         efree(prot_name);
     629               0 :                                                         php_var_serialize_intern(buf, *d, var_hash TSRMLS_CC);
     630               0 :                                                         break;
     631                 :                                                 }
     632               0 :                                                 efree(prot_name);
     633               0 :                                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "\"%s\" returned as member variable from __sleep() but does not exist", Z_STRVAL_PP(name));
     634               0 :                                                 php_var_serialize_string(buf, Z_STRVAL_PP(name), Z_STRLEN_PP(name));
     635               0 :                                                 php_var_serialize_intern(buf, nvalp, var_hash TSRMLS_CC);
     636                 :                                         } while (0);
     637                 :                                 } else {
     638               0 :                                         php_var_serialize_string(buf, Z_STRVAL_PP(name), Z_STRLEN_PP(name));
     639               0 :                                         php_var_serialize_intern(buf, nvalp, var_hash TSRMLS_CC);
     640                 :                                 }
     641                 :                         }
     642               0 :                 }
     643                 :         }
     644               0 :         smart_str_appendc(buf, '}');
     645               0 : }
     646                 : 
     647                 : 
     648                 : static void php_var_serialize_intern(smart_str *buf, zval *struc, HashTable *var_hash TSRMLS_DC)
     649              10 : {
     650                 :         int i;
     651                 :         ulong *var_already;
     652                 :         HashTable *myht;
     653                 : 
     654              10 :         if (var_hash 
     655                 :             && php_add_var_hash(var_hash, struc, (void *) &var_already TSRMLS_CC) == FAILURE) {
     656               0 :                 if(struc->is_ref) {
     657               0 :                         smart_str_appendl(buf, "R:", 2);
     658               0 :                         smart_str_append_long(buf, *var_already);
     659               0 :                         smart_str_appendc(buf, ';');
     660               0 :                         return;
     661               0 :                 } else if(Z_TYPE_P(struc) == IS_OBJECT) {
     662               0 :                         smart_str_appendl(buf, "r:", 2);
     663               0 :                         smart_str_append_long(buf, *var_already);
     664               0 :                         smart_str_appendc(buf, ';');
     665               0 :                         return;
     666                 :                 }
     667                 :         }
     668                 : 
     669              10 :         switch (Z_TYPE_P(struc)) {
     670                 :                 case IS_BOOL:
     671               0 :                         smart_str_appendl(buf, "b:", 2);
     672               0 :                         smart_str_append_long(buf, Z_LVAL_P(struc));
     673               0 :                         smart_str_appendc(buf, ';');
     674               0 :                         return;
     675                 : 
     676                 :                 case IS_NULL:
     677               0 :                         smart_str_appendl(buf, "N;", 2);
     678               0 :                         return;
     679                 : 
     680                 :                 case IS_LONG:
     681               2 :                         php_var_serialize_long(buf, Z_LVAL_P(struc));
     682               2 :                         return;
     683                 : 
     684                 :                 case IS_DOUBLE: {
     685                 :                                 char *s;
     686                 : 
     687               0 :                                 smart_str_appendl(buf, "d:", 2);
     688               0 :                                 s = (char *) safe_emalloc(PG(serialize_precision), 1, MAX_LENGTH_OF_DOUBLE + 1);
     689               0 :                                 php_gcvt(Z_DVAL_P(struc), PG(serialize_precision), '.', 'E', s);
     690               0 :                                 smart_str_appends(buf, s);
     691               0 :                                 smart_str_appendc(buf, ';');
     692               0 :                                 efree(s);
     693               0 :                                 return;
     694                 :                         }
     695                 : 
     696                 :                 case IS_STRING:
     697               4 :                         php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc));
     698               4 :                         return;
     699                 : 
     700                 :                 case IS_OBJECT: {
     701               0 :                                 zval *retval_ptr = NULL;
     702                 :                                 zval fname;
     703                 :                                 int res;
     704               0 :                                 zend_class_entry *ce = NULL;
     705                 : 
     706               0 :                                 if(Z_OBJ_HT_P(struc)->get_class_entry) {
     707               0 :                                         ce = Z_OBJCE_P(struc);
     708                 :                                 } 
     709                 : 
     710               0 :                                 if(ce && ce->serialize != NULL) {
     711                 :                                         /* has custom handler */
     712               0 :                                         unsigned char *serialized_data = NULL;
     713                 :                                         zend_uint serialized_length;
     714                 : 
     715               0 :                                         if(ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash TSRMLS_CC) == SUCCESS) {
     716               0 :                                                 smart_str_appendl(buf, "C:", 2);
     717               0 :                                                 smart_str_append_long(buf, Z_OBJCE_P(struc)->name_length);
     718               0 :                                                 smart_str_appendl(buf, ":\"", 2);
     719               0 :                                                 smart_str_appendl(buf, Z_OBJCE_P(struc)->name, Z_OBJCE_P(struc)->name_length);
     720               0 :                                                 smart_str_appendl(buf, "\":", 2);
     721                 :                                         
     722               0 :                                                 smart_str_append_long(buf, serialized_length);
     723               0 :                                                 smart_str_appendl(buf, ":{", 2);
     724               0 :                                                 smart_str_appendl(buf, serialized_data, serialized_length);
     725               0 :                                                 smart_str_appendc(buf, '}'); 
     726                 :                                         } else {
     727               0 :                                                 smart_str_appendl(buf, "N;", 2);
     728                 :                                         }
     729               0 :                                         if(serialized_data) {
     730               0 :                                                 efree(serialized_data);
     731                 :                                         }
     732               0 :                                         return;
     733                 :                                 }
     734                 :                                 
     735               0 :                                 if (ce && ce != PHP_IC_ENTRY &&
     736                 :                                                 zend_hash_exists(&ce->function_table, "__sleep", sizeof("__sleep"))) {
     737               0 :                                         INIT_PZVAL(&fname);
     738               0 :                                         ZVAL_STRINGL(&fname, "__sleep", sizeof("__sleep") - 1, 0);
     739               0 :                                         res = call_user_function_ex(CG(function_table), &struc, &fname, 
     740                 :                                                                                                 &retval_ptr, 0, 0, 1, NULL TSRMLS_CC);
     741                 : 
     742               0 :                                         if (res == SUCCESS && !EG(exception)) {
     743               0 :                                                 if (retval_ptr) {
     744               0 :                                                         if (HASH_OF(retval_ptr)) {
     745               0 :                                                                 php_var_serialize_class(buf, struc, retval_ptr, 
     746                 :                                                                                                                 var_hash TSRMLS_CC);
     747                 :                                                         } else {
     748               0 :                                                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "__sleep should return an array only "
     749                 :                                                                                                  "containing the names of instance-variables to "
     750                 :                                                                                                  "serialize.");
     751                 :                                                                 /* we should still add element even if it's not OK,
     752                 :                                                                 since we already wrote the length of the array before */
     753               0 :                                                                 smart_str_appendl(buf,"N;", 2);
     754                 :                                                         }
     755                 :                                                         
     756               0 :                                                         zval_ptr_dtor(&retval_ptr);
     757                 :                                                 }
     758               0 :                                                 return; 
     759                 :                                         }
     760                 :                                 }
     761                 :                                 
     762               0 :                                 if (retval_ptr)
     763               0 :                                         zval_ptr_dtor(&retval_ptr);
     764                 :                                 /* fall-through */
     765                 :                         }
     766                 :                 case IS_ARRAY: {
     767               4 :                         zend_bool incomplete_class = 0;
     768               4 :                         if (Z_TYPE_P(struc) == IS_ARRAY) {
     769               4 :                                 smart_str_appendl(buf, "a:", 2);
     770               4 :                                 myht = HASH_OF(struc);
     771                 :                         } else {
     772               0 :                                 incomplete_class = php_var_serialize_class_name(buf, struc TSRMLS_CC);
     773               0 :                                 myht = Z_OBJPROP_P(struc);
     774                 :                         }
     775                 :                         /* count after serializing name, since php_var_serialize_class_name
     776                 :                            changes the count if the variable is incomplete class */
     777               4 :                         i = myht ? zend_hash_num_elements(myht) : 0;
     778               4 :                         if (i > 0 && incomplete_class) {
     779               0 :                                 --i;
     780                 :                         }
     781               4 :                         smart_str_append_long(buf, i);
     782               4 :                         smart_str_appendl(buf, ":{", 2);
     783               4 :                         if (i > 0) {
     784                 :                                 char *key;
     785                 :                                 zval **data;
     786                 :                                 ulong index;
     787                 :                                 uint key_len;
     788                 :                                 HashPosition pos;
     789                 :                                 
     790               4 :                                 zend_hash_internal_pointer_reset_ex(myht, &pos);
     791               8 :                                 for (;; zend_hash_move_forward_ex(myht, &pos)) {
     792              12 :                                         i = zend_hash_get_current_key_ex(myht, &key, &key_len, 
     793                 :                                                         &index, 0, &pos);
     794              12 :                                         if (i == HASH_KEY_NON_EXISTANT)
     795               4 :                                                 break;
     796                 :                                         
     797               8 :                                         if (incomplete_class && strcmp(key, MAGIC_MEMBER) == 0) {
     798               0 :                                                 continue;
     799                 :                                         }               
     800                 :                                         
     801               8 :                                         switch (i) {
     802                 :                                                 case HASH_KEY_IS_LONG:
     803               0 :                                                         php_var_serialize_long(buf, index);
     804               0 :                                                         break;
     805                 :                                                 case HASH_KEY_IS_STRING:
     806               8 :                                                         php_var_serialize_string(buf, key, key_len - 1);
     807                 :                                                         break;
     808                 :                                         }
     809                 : 
     810                 :                                         /* we should still add element even if it's not OK,
     811                 :                                            since we already wrote the length of the array before */
     812               8 :                                         if (zend_hash_get_current_data_ex(myht, 
     813                 :                                                 (void **) &data, &pos) != SUCCESS 
     814                 :                                                 || !data 
     815                 :                                                 || data == &struc
     816                 :                                                 || (Z_TYPE_PP(data) == IS_ARRAY && Z_ARRVAL_PP(data)->nApplyCount > 1)
     817                 :                                         ) {
     818               0 :                                                 smart_str_appendl(buf, "N;", 2);
     819                 :                                         } else {
     820               8 :                                                 if (Z_TYPE_PP(data) == IS_ARRAY) {
     821               2 :                                                         Z_ARRVAL_PP(data)->nApplyCount++;
     822                 :                                                 }
     823               8 :                                                 php_var_serialize_intern(buf, *data, var_hash TSRMLS_CC);
     824               8 :                                                 if (Z_TYPE_PP(data) == IS_ARRAY) {
     825               2 :                                                         Z_ARRVAL_PP(data)->nApplyCount--;
     826                 :                                                 }
     827                 :                                         }
     828               8 :                                 }
     829                 :                         }
     830               4 :                         smart_str_appendc(buf, '}');
     831               4 :                         return;
     832                 :                 }
     833                 :                 default:
     834               0 :                         smart_str_appendl(buf, "i:0;", 4);
     835               0 :                         return;
     836                 :         } 
     837                 : }
     838                 : 
     839                 : PHPAPI void php_var_serialize(smart_str *buf, zval **struc, HashTable *var_hash TSRMLS_DC)
     840               2 : {
     841               2 :         php_var_serialize_intern(buf, *struc, var_hash TSRMLS_CC);
     842               2 :         smart_str_0(buf);
     843               2 : }
     844                 :         
     845                 : /* }}} */
     846                 : 
     847                 : /* {{{ proto string serialize(mixed variable)
     848                 :    Returns a string representation of variable (which can later be unserialized) */
     849                 : PHP_FUNCTION(serialize)
     850               2 : {
     851                 :         zval **struc;
     852                 :         php_serialize_data_t var_hash;
     853               2 :         smart_str buf = {0};
     854                 : 
     855               2 :         if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &struc) == FAILURE) {
     856               0 :                 WRONG_PARAM_COUNT;
     857                 :         }
     858                 : 
     859               2 :         Z_TYPE_P(return_value) = IS_STRING;
     860               2 :         Z_STRVAL_P(return_value) = NULL;
     861               2 :         Z_STRLEN_P(return_value) = 0;
     862                 : 
     863               2 :         PHP_VAR_SERIALIZE_INIT(var_hash);
     864               2 :         php_var_serialize(&buf, struc, &var_hash TSRMLS_CC);
     865               2 :         PHP_VAR_SERIALIZE_DESTROY(var_hash);
     866                 : 
     867               2 :         if (buf.c) {
     868               2 :                 RETURN_STRINGL(buf.c, buf.len, 0);
     869                 :         } else {
     870               0 :                 RETURN_NULL();
     871                 :         }
     872                 : }
     873                 : 
     874                 : /* }}} */
     875                 : /* {{{ proto mixed unserialize(string variable_representation)
     876                 :    Takes a string representation of variable and recreates it */
     877                 : 
     878                 : 
     879                 : PHP_FUNCTION(unserialize)
     880               0 : {
     881                 :         char *buf;
     882                 :         int buf_len;
     883                 :         const unsigned char *p;
     884                 :         php_unserialize_data_t var_hash;
     885                 :         
     886               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &buf_len) == FAILURE) {
     887               0 :                 RETURN_FALSE;
     888                 :         }
     889                 : 
     890               0 :         if (buf_len == 0) {
     891               0 :                 RETURN_FALSE;
     892                 :         }
     893                 : 
     894               0 :         p = (const unsigned char*)buf;
     895               0 :         PHP_VAR_UNSERIALIZE_INIT(var_hash);
     896               0 :         if (!php_var_unserialize(&return_value, &p, p + buf_len,  &var_hash TSRMLS_CC)) {
     897               0 :                 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
     898               0 :                 zval_dtor(return_value);
     899               0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Error at offset %ld of %d bytes", (long)((char*)p - buf), buf_len);
     900               0 :                 RETURN_FALSE;
     901                 :         }
     902               0 :         PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
     903                 : }
     904                 : 
     905                 : /* }}} */
     906                 : 
     907                 : /* {{{ proto int memory_get_usage([real_usage])
     908                 :     Returns the allocated by PHP memory */
     909               0 : PHP_FUNCTION(memory_get_usage) {
     910               0 :         zend_bool real_usage = 0;
     911                 : 
     912               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &real_usage) == FAILURE) {
     913               0 :                 RETURN_FALSE;
     914                 :         }
     915                 :         
     916               0 :         RETURN_LONG(zend_memory_usage(real_usage TSRMLS_CC));
     917                 : }
     918                 : /* }}} */
     919                 : 
     920                 : /* {{{ proto int memory_get_peak_usage([real_usage])
     921                 :     Returns the peak allocated by PHP memory */
     922               0 : PHP_FUNCTION(memory_get_peak_usage) {
     923               0 :         zend_bool real_usage = 0;
     924                 : 
     925               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &real_usage) == FAILURE) {
     926               0 :                 RETURN_FALSE;
     927                 :         }
     928                 : 
     929               0 :         RETURN_LONG(zend_memory_peak_usage(real_usage TSRMLS_CC));
     930                 : }
     931                 : /* }}} */
     932                 : 
     933                 : /*
     934                 :  * Local variables:
     935                 :  * tab-width: 4
     936                 :  * c-basic-offset: 4
     937                 :  * End:
     938                 :  * vim600: sw=4 ts=4 fdm=marker
     939                 :  * vim<600: sw=4 ts=4
     940                 :  */

Generated by: LTP GCOV extension version 1.5