"Fossies" - the Fresh Open Source Software Archive

Member "dbg-2.15.5/dbg_ser.c" (28 Apr 2007, 8704 Bytes) of package /linux/www/old/dbg-2.15.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dbg_ser.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2                           dbg_ser.c  -  description
    3                              -------------------
    4     begin                : Sun Sep 24 2000
    5     copyright            : (C) 2001 by Dmitri Dmitrienko
    6                          : (C) 2002, 2007 NuSphere Corp.
    7     www                  : http://dd.cron.ru
    8                          : http://www.nusphere.com/
    9     www                  : http://dd.cron.ru
   10     license              : This source file is subject to version 1.01 of 
   11                            the License,  that is bundled with this package 
   12                            in the file LICENSE, and is available at through 
   13                            the world-wide-web at http://dd.cron.ru/license
   14  ***************************************************************************/
   15 
   16 
   17 #include "php.h"
   18 
   19 #if PHP_WIN32 || defined WIN32
   20 #include "config.w32.h"
   21 #endif
   22 
   23 #ifdef HAVE_CONFIG_H
   24 #include "config.h"
   25 #endif
   26 
   27 #include "php_dbg.h"
   28 #include "dbg_ser.h"
   29 
   30 
   31 /*         S E R I A L I Z A T I O N    E F F O R T        */
   32 
   33 #define STR_CAT(P,S,I) {\
   34     pval *__p = (P);\
   35     ulong __i = Z_STRLEN_P(__p);\
   36     Z_STRLEN_P(__p) += (I);\
   37     if (Z_STRVAL_P(__p)) {\
   38         Z_STRVAL_P(__p) = (char *)erealloc(Z_STRVAL_P(__p), Z_STRLEN_P(__p) + 1);\
   39     } else {\
   40         Z_STRVAL_P(__p) = emalloc(Z_STRLEN_P(__p) + 1);\
   41         if (Z_STRVAL_P(__p)) *Z_STRVAL_P(__p) = 0;\
   42     }\
   43     if (Z_STRVAL_P(__p) && (I)) {\
   44         memcpy(Z_STRVAL_P(__p) + __i, (S), (I));\
   45         Z_STRVAL_P(__p)[__i + (I)] = 0;\
   46     }\
   47 }
   48 
   49 static inline void serialize_zval(HashTable *var_hash, zval *data, zval *buf, unsigned int Flags TSRMLS_DC2(DBG, E));
   50 
   51 static inline void serialize_hash(HashTable *var_hash, HashTable *ht, zval *buf, unsigned int Flags TSRMLS_DC2(DBG, E)) {
   52     HashPosition pos;
   53     zval **tmp, *d;
   54     ulong index;
   55     char *key;
   56     int cnt = 0;
   57     uint len;
   58 
   59     zend_hash_internal_pointer_reset_ex(ht, &pos);
   60     while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &pos) == SUCCESS && *tmp) {
   61         cnt++;
   62         len = 1;
   63         switch (ZEND_HASH_GET_CURRENT_KEY_EX(ht, &key, &len, &index, &pos)) {
   64             case HASH_KEY_IS_LONG:
   65                 MAKE_STD_ZVAL(d);   
   66                 d->type = IS_LONG;
   67                 d->value.lval = index;
   68                 DBG_TRACE(("[%d]\n",index));
   69                 serialize_zval(NULL, d, buf, 0 TSRMLS_CC2(DBG, E));
   70                 FREE_ZVAL(d);
   71                 break;
   72             case HASH_KEY_IS_STRING:
   73                 MAKE_STD_ZVAL(d);
   74                 d->type = IS_STRING;
   75 #if defined(ZEND_ENGINE_2)
   76                 if (Flags & DSER_DEMANGLE) {
   77                     char *propname, *classname;
   78 #if defined(ZEND_ENGINE_2_2)
   79                     zend_unmangle_property_name(key, len - 1, &classname, &propname);
   80 #else
   81                     zend_unmangle_property_name(key, &classname, &propname);
   82 #endif
   83                     Z_STRVAL_P(d) = propname;
   84                     Z_STRLEN_P(d) = strlen(propname);
   85                 } else {
   86                     Z_STRVAL_P(d) = key;
   87                     Z_STRLEN_P(d) = len - 1;
   88                 }
   89 #else
   90                 Z_STRVAL_P(d) = key;
   91                 Z_STRLEN_P(d) = len - 1;
   92 #endif
   93                 DBG_TRACE(("[%s]\n", SON(key)));
   94                 serialize_zval(NULL, d, buf, Flags & DSER_ADD_DSIGN TSRMLS_CC2(DBG, E));                
   95                 Z_STRVAL_P(d) = NULL;
   96                 Z_STRLEN_P(d) = 0;
   97                 FREE_ZVAL(d);
   98                 efree(key);
   99                 break;
  100         }
  101         serialize_zval(var_hash, *tmp, buf, Flags & DSER_CORR_REF TSRMLS_CC2(DBG, E));
  102         zend_hash_move_forward_ex(ht, &pos);
  103     }   
  104 }
  105 
  106 
  107 static inline int add_var_hash(HashTable *var_hash, zval *var, unsigned long *var_idx, int corrRef) {
  108     unsigned long var_new_idx;
  109     unsigned long *pidx;
  110     char id[64];
  111 
  112     snprintf(id,sizeof(id)-1, "%p", var);
  113     id[sizeof(id)-1]=0;
  114     
  115     DBG_TRACE(("<ADDR=%s>\n",id));
  116 
  117     if(zend_hash_find(var_hash, id, strlen(id), (void*) &pidx) == SUCCESS) {
  118         DBG_TRACE(("<FOUND ID=%ld>\n",*pidx));
  119 /*      if(!var->is_ref) {
  120             var_new_idx = -1;
  121             zend_hash_next_index_insert(var_hash, &var_new_idx, sizeof(var_new_idx), NULL);
  122         }*/
  123         *var_idx = *pidx;
  124         return FAILURE;
  125     }
  126     
  127     var_new_idx = zend_hash_num_elements(var_hash) + 1 + corrRef;
  128 #if TRACE_EVAL
  129     DBG_TRACE(("<ASSIGNED ID=%ld>\n",var_new_idx));
  130 #endif
  131     *var_idx = var_new_idx;
  132     zend_hash_add(var_hash, id, strlen(id), &var_new_idx, sizeof(var_new_idx), NULL);
  133     return SUCCESS;
  134 }
  135 
  136 static inline void serialize_zval(HashTable *var_hash, zval *data, zval *buf, unsigned int Flags TSRMLS_DC2(DBG, E)) {
  137     ulong slen, len;
  138     int num_elements;
  139     char s[64];
  140     HashTable *ht;
  141     unsigned long var_idx;
  142     char * type_name;
  143 
  144     if (var_hash && add_var_hash(var_hash, data, &var_idx, (Flags & DSER_CORR_REF) != 0) == FAILURE) {
  145         if (data->is_ref) {
  146             slen = snprintf(s, sizeof(s)-1, "R:%ld;", var_idx);
  147         } else {
  148             slen = snprintf(s, sizeof(s)-1, "r:%ld;", var_idx);
  149         }
  150         STR_CAT(buf, s, slen);
  151         return;
  152     }
  153 
  154     switch (data->type) {
  155         case IS_NULL:
  156             STR_CAT(buf, "N;", 2);
  157             return;
  158         case IS_LONG:
  159             slen = snprintf(s, sizeof(s) - 1, "i:%ld;", data->value.lval);
  160             STR_CAT(buf, s, slen);
  161             return;
  162         case IS_DOUBLE: {               
  163                 slen = snprintf(s, sizeof(s)-1, "d:%.*G;",(int) EG(precision), data->value.dval);
  164                 STR_CAT(buf, s, slen);
  165             }
  166             return;
  167         case IS_CONSTANT:
  168         case IS_STRING: 
  169             if ((Flags & DSER_ADD_DSIGN) != 0) {
  170                 slen = snprintf(s, sizeof(s)-1, "s:%d:\"", data->value.str.len+1);
  171                 STR_CAT(buf, s, slen);
  172                 STR_CAT(buf, "$", 1);
  173                 if (data->value.str.len != 0) {
  174                     STR_CAT(buf, Z_STRVAL_P(data), Z_STRLEN_P(data));
  175                 }
  176                 STR_CAT(buf, "\";", 2);
  177             } else {
  178                 slen = snprintf(s, sizeof(s)-1, "s:%d:\"", data->value.str.len);
  179                 STR_CAT(buf, s, slen);
  180                 if (data->value.str.len != 0) {
  181                     STR_CAT(buf, Z_STRVAL_P(data), Z_STRLEN_P(data));
  182                 }
  183                 STR_CAT(buf, "\";", 2);
  184             }
  185             return;
  186         case IS_CONSTANT_ARRAY:
  187         case IS_ARRAY: {
  188                 if  (DBG(eval_nest) > 64) {
  189                     return;
  190                 }
  191                 DBG(eval_nest)++;
  192                 ht = HASH_OF(data);
  193                 num_elements = ht ? zend_hash_num_elements(ht) : 0;
  194                 slen = snprintf(s, sizeof(s)-1, "a:%d:{", num_elements);
  195                 STR_CAT(buf, s, slen);
  196                 if (ht)
  197                     serialize_hash(var_hash, ht, buf, Flags & DSER_CORR_REF TSRMLS_CC2(DBG, E));
  198                 STR_CAT(buf, "}", 1);
  199                 DBG(eval_nest)--;
  200                 return;
  201             }
  202         case IS_OBJECT: {           
  203                 if  (DBG(eval_nest) > 64) {
  204                     return;
  205                 }
  206                 DBG(eval_nest)++;
  207 
  208                 ht = HASH_OF(data);
  209                 num_elements = ht ? zend_hash_num_elements(ht) : 0;
  210 
  211                 
  212                 slen = snprintf(s, sizeof(s), "O:%d:\"", Z_OBJCE_P(data)->name_length);
  213                 STR_CAT(buf, s, slen);
  214                 STR_CAT(buf, Z_OBJCE_P(data)->name, Z_OBJCE_P(data)->name_length);
  215                 slen = snprintf(s, sizeof(s), "\":%d:{", num_elements);
  216                 STR_CAT(buf, s, slen);
  217 #if 0
  218                 slen = snprintf(s, sizeof(s), "O:%d:\"", data->value.obj.ce->name_length);
  219                 STR_CAT(buf, s, slen);
  220                 STR_CAT(buf, data->value.obj.ce->name, data->value.obj.ce->name_length);
  221                 slen = snprintf(s, sizeof(s), "\":%d:{", num_elements);
  222                 STR_CAT(buf, s, slen);
  223 #endif
  224                 if (ht) {
  225                     serialize_hash(var_hash, ht, buf, (Flags & DSER_CORR_REF) | DSER_DEMANGLE TSRMLS_CC2(DBG, E));
  226                 }
  227                 STR_CAT(buf, "}", 1);
  228                 DBG(eval_nest)--;
  229                 return;
  230             }
  231         case IS_BOOL:
  232             slen = snprintf(s, sizeof(s)-1, "b:%ld;", data->value.lval);
  233             STR_CAT(buf, s, slen);
  234             return;
  235         case IS_RESOURCE:
  236             type_name = zend_rsrc_list_get_rsrc_type(data->value.lval TSRMLS_CC0);
  237             if (!type_name) type_name = "";
  238             len = strlen(type_name);
  239             slen = snprintf(s, sizeof(s), "z:%ld:\"", len);
  240             STR_CAT(buf, s, slen);
  241             STR_CAT(buf, type_name, len);
  242             slen = snprintf(s, sizeof(s), "\":%ld;", data->value.lval);
  243             STR_CAT(buf, s, slen);
  244             return;
  245         default:            
  246             STR_CAT(buf, "?;", 2);
  247             return;
  248     }
  249 }
  250 
  251 
  252 void dbg_serialize_zval(zval *data, zval *buf, unsigned int Flags TSRMLS_DC2(DBG, E)) {
  253     HashTable var_hash;
  254 
  255 #if TRACE_EVAL
  256     DBG_TRACE((">>START OF ZVAL EVAL\n"));
  257 #endif
  258     zend_hash_init(&var_hash,16,NULL,NULL,0);
  259     serialize_zval(&var_hash, data, buf, Flags & DSER_ADD_DSIGN TSRMLS_CC2(DBG, E));
  260     zend_hash_destroy(&var_hash);
  261 #if TRACE_EVAL
  262     DBG_TRACE((">>END OF ZVAL EVAL\n"));
  263 #endif
  264 }
  265 
  266 void dbg_serialize_hash(HashTable *ht, zval *zthis, zval *buf, unsigned int Flags TSRMLS_DC2(DBG, E)) {
  267     int num_elements;
  268     ulong slen;
  269     char s[64];
  270     HashTable var_hash;
  271     zval **v;
  272     
  273 #if TRACE_EVAL
  274     DBG_TRACE((">>START OF HASH EVAL\n"));
  275 #endif
  276     num_elements = zend_hash_num_elements(ht);
  277 
  278     if (zthis) {
  279         if (zend_hash_find(ht, "this", sizeof("this"), (void **) &v) != FAILURE) {
  280             zthis = NULL;
  281         } else {
  282             num_elements++;
  283         }
  284     }
  285     slen = snprintf(s, sizeof(s)-1, "a:%d:{", num_elements);
  286     STR_CAT(buf, s, slen);
  287     zend_hash_init(&var_hash,16,NULL,NULL,0);
  288     if (zthis) {
  289         STR_CAT(buf, "s:5:\"$this\";", sizeof("s:5:\"$this\";") - 1);
  290         serialize_zval(&var_hash, zthis, buf, (Flags & DSER_ADD_DSIGN) | DSER_CORR_REF TSRMLS_CC2(DBG, E));
  291     }
  292     serialize_hash(&var_hash, ht, buf, (Flags & DSER_ADD_DSIGN) | DSER_CORR_REF TSRMLS_CC2(DBG, E));
  293     zend_hash_destroy(&var_hash);
  294     STR_CAT(buf, "}", 1);
  295 #if TRACE_EVAL
  296     DBG_TRACE((">>END OF HASH EVAL\n"));
  297 #endif
  298 }