PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/standard/var.c

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