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