PageRenderTime 47ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/standard/var.c

http://github.com/php/php-src
C | 1337 lines | 1256 code | 38 blank | 43 comment | 78 complexity | 33031e84eebf5544dacd2d276a74bb93 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Jani Lehtimäki <jkl@njet.net> |
  14. | Thies C. Arntzen <thies@thieso.net> |
  15. | Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* {{{ includes
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include "php.h"
  24. #include "php_string.h"
  25. #include "php_var.h"
  26. #include "zend_smart_str.h"
  27. #include "basic_functions.h"
  28. #include "php_incomplete_class.h"
  29. /* }}} */
  30. struct php_serialize_data {
  31. HashTable ht;
  32. uint32_t n;
  33. };
  34. #define COMMON (is_ref ? "&" : "")
  35. static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
  36. {
  37. if (key == NULL) { /* numeric key */
  38. php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
  39. } else { /* string key */
  40. php_printf("%*c[\"", level + 1, ' ');
  41. PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
  42. php_printf("\"]=>\n");
  43. }
  44. php_var_dump(zv, level + 2);
  45. }
  46. /* }}} */
  47. static void php_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
  48. {
  49. const char *prop_name, *class_name;
  50. if (key == NULL) { /* numeric key */
  51. php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
  52. } else { /* string key */
  53. int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
  54. php_printf("%*c[", level + 1, ' ');
  55. if (class_name && unmangle == SUCCESS) {
  56. if (class_name[0] == '*') {
  57. php_printf("\"%s\":protected", prop_name);
  58. } else {
  59. php_printf("\"%s\":\"%s\":private", prop_name, class_name);
  60. }
  61. } else {
  62. php_printf("\"");
  63. PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
  64. php_printf("\"");
  65. }
  66. ZEND_PUTS("]=>\n");
  67. }
  68. if (Z_TYPE_P(zv) == IS_UNDEF) {
  69. ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type));
  70. zend_string *type_str = zend_type_to_string(prop_info->type);
  71. php_printf("%*cuninitialized(%s)\n",
  72. level + 1, ' ', ZSTR_VAL(type_str));
  73. zend_string_release(type_str);
  74. } else {
  75. php_var_dump(zv, level + 2);
  76. }
  77. }
  78. /* }}} */
  79. PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
  80. {
  81. HashTable *myht;
  82. zend_string *class_name;
  83. int is_ref = 0;
  84. zend_ulong num;
  85. zend_string *key;
  86. zval *val;
  87. uint32_t count;
  88. if (level > 1) {
  89. php_printf("%*c", level - 1, ' ');
  90. }
  91. again:
  92. switch (Z_TYPE_P(struc)) {
  93. case IS_FALSE:
  94. php_printf("%sbool(false)\n", COMMON);
  95. break;
  96. case IS_TRUE:
  97. php_printf("%sbool(true)\n", COMMON);
  98. break;
  99. case IS_NULL:
  100. php_printf("%sNULL\n", COMMON);
  101. break;
  102. case IS_LONG:
  103. php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
  104. break;
  105. case IS_DOUBLE:
  106. php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
  107. break;
  108. case IS_STRING:
  109. php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));
  110. PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
  111. PUTS("\"\n");
  112. break;
  113. case IS_ARRAY:
  114. myht = Z_ARRVAL_P(struc);
  115. if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
  116. if (GC_IS_RECURSIVE(myht)) {
  117. PUTS("*RECURSION*\n");
  118. return;
  119. }
  120. GC_PROTECT_RECURSION(myht);
  121. }
  122. count = zend_array_count(myht);
  123. php_printf("%sarray(%d) {\n", COMMON, count);
  124. ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) {
  125. php_array_element_dump(val, num, key, level);
  126. } ZEND_HASH_FOREACH_END();
  127. if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
  128. GC_UNPROTECT_RECURSION(myht);
  129. }
  130. if (level > 1) {
  131. php_printf("%*c", level-1, ' ');
  132. }
  133. PUTS("}\n");
  134. break;
  135. case IS_OBJECT:
  136. if (Z_IS_RECURSIVE_P(struc)) {
  137. PUTS("*RECURSION*\n");
  138. return;
  139. }
  140. Z_PROTECT_RECURSION_P(struc);
  141. myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
  142. class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
  143. php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0);
  144. zend_string_release_ex(class_name, 0);
  145. if (myht) {
  146. zend_ulong num;
  147. zend_string *key;
  148. zval *val;
  149. ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) {
  150. zend_property_info *prop_info = NULL;
  151. if (Z_TYPE_P(val) == IS_INDIRECT) {
  152. val = Z_INDIRECT_P(val);
  153. if (key) {
  154. prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
  155. }
  156. }
  157. if (!Z_ISUNDEF_P(val) || prop_info) {
  158. php_object_property_dump(prop_info, val, num, key, level);
  159. }
  160. } ZEND_HASH_FOREACH_END();
  161. zend_release_properties(myht);
  162. }
  163. if (level > 1) {
  164. php_printf("%*c", level-1, ' ');
  165. }
  166. PUTS("}\n");
  167. Z_UNPROTECT_RECURSION_P(struc);
  168. break;
  169. case IS_RESOURCE: {
  170. const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
  171. php_printf("%sresource(%d) of type (%s)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown");
  172. break;
  173. }
  174. case IS_REFERENCE:
  175. //??? hide references with refcount==1 (for compatibility)
  176. if (Z_REFCOUNT_P(struc) > 1) {
  177. is_ref = 1;
  178. }
  179. struc = Z_REFVAL_P(struc);
  180. goto again;
  181. break;
  182. default:
  183. php_printf("%sUNKNOWN:0\n", COMMON);
  184. break;
  185. }
  186. }
  187. /* }}} */
  188. /* {{{ proto void var_dump(mixed var)
  189. Dumps a string representation of variable to output */
  190. PHP_FUNCTION(var_dump)
  191. {
  192. zval *args;
  193. int argc;
  194. int i;
  195. ZEND_PARSE_PARAMETERS_START(1, -1)
  196. Z_PARAM_VARIADIC('+', args, argc)
  197. ZEND_PARSE_PARAMETERS_END();
  198. for (i = 0; i < argc; i++) {
  199. php_var_dump(&args[i], 1);
  200. }
  201. }
  202. /* }}} */
  203. static void zval_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
  204. {
  205. if (key == NULL) { /* numeric key */
  206. php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
  207. } else { /* string key */
  208. php_printf("%*c[\"", level + 1, ' ');
  209. PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
  210. php_printf("\"]=>\n");
  211. }
  212. php_debug_zval_dump(zv, level + 2);
  213. }
  214. /* }}} */
  215. static void zval_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
  216. {
  217. const char *prop_name, *class_name;
  218. if (key == NULL) { /* numeric key */
  219. php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
  220. } else { /* string key */
  221. zend_unmangle_property_name(key, &class_name, &prop_name);
  222. php_printf("%*c[", level + 1, ' ');
  223. if (class_name) {
  224. if (class_name[0] == '*') {
  225. php_printf("\"%s\":protected", prop_name);
  226. } else {
  227. php_printf("\"%s\":\"%s\":private", prop_name, class_name);
  228. }
  229. } else {
  230. php_printf("\"%s\"", prop_name);
  231. }
  232. ZEND_PUTS("]=>\n");
  233. }
  234. if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) {
  235. zend_string *type_str = zend_type_to_string(prop_info->type);
  236. php_printf("%*cuninitialized(%s)\n",
  237. level + 1, ' ', ZSTR_VAL(type_str));
  238. zend_string_release(type_str);
  239. } else {
  240. php_debug_zval_dump(zv, level + 2);
  241. }
  242. }
  243. /* }}} */
  244. PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */
  245. {
  246. HashTable *myht = NULL;
  247. zend_string *class_name;
  248. int is_ref = 0;
  249. zend_ulong index;
  250. zend_string *key;
  251. zval *val;
  252. uint32_t count;
  253. if (level > 1) {
  254. php_printf("%*c", level - 1, ' ');
  255. }
  256. again:
  257. switch (Z_TYPE_P(struc)) {
  258. case IS_FALSE:
  259. php_printf("%sbool(false)\n", COMMON);
  260. break;
  261. case IS_TRUE:
  262. php_printf("%sbool(true)\n", COMMON);
  263. break;
  264. case IS_NULL:
  265. php_printf("%sNULL\n", COMMON);
  266. break;
  267. case IS_LONG:
  268. php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
  269. break;
  270. case IS_DOUBLE:
  271. php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
  272. break;
  273. case IS_STRING:
  274. php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));
  275. PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
  276. php_printf("\" refcount(%u)\n", Z_REFCOUNTED_P(struc) ? Z_REFCOUNT_P(struc) : 1);
  277. break;
  278. case IS_ARRAY:
  279. myht = Z_ARRVAL_P(struc);
  280. if (level > 1 && !(GC_FLAGS(myht) & GC_IMMUTABLE)) {
  281. if (GC_IS_RECURSIVE(myht)) {
  282. PUTS("*RECURSION*\n");
  283. return;
  284. }
  285. GC_PROTECT_RECURSION(myht);
  286. }
  287. count = zend_array_count(myht);
  288. php_printf("%sarray(%d) refcount(%u){\n", COMMON, count, Z_REFCOUNTED_P(struc) ? Z_REFCOUNT_P(struc) : 1);
  289. ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
  290. zval_array_element_dump(val, index, key, level);
  291. } ZEND_HASH_FOREACH_END();
  292. if (level > 1 && !(GC_FLAGS(myht) & GC_IMMUTABLE)) {
  293. GC_UNPROTECT_RECURSION(myht);
  294. }
  295. if (level > 1) {
  296. php_printf("%*c", level - 1, ' ');
  297. }
  298. PUTS("}\n");
  299. break;
  300. case IS_OBJECT:
  301. myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
  302. if (myht) {
  303. if (GC_IS_RECURSIVE(myht)) {
  304. PUTS("*RECURSION*\n");
  305. zend_release_properties(myht);
  306. return;
  307. }
  308. GC_PROTECT_RECURSION(myht);
  309. }
  310. class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
  311. php_printf("%sobject(%s)#%d (%d) refcount(%u){\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0, Z_REFCOUNT_P(struc));
  312. zend_string_release_ex(class_name, 0);
  313. if (myht) {
  314. ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
  315. zend_property_info *prop_info = NULL;
  316. if (Z_TYPE_P(val) == IS_INDIRECT) {
  317. val = Z_INDIRECT_P(val);
  318. if (key) {
  319. prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
  320. }
  321. }
  322. if (!Z_ISUNDEF_P(val) || prop_info) {
  323. zval_object_property_dump(prop_info, val, index, key, level);
  324. }
  325. } ZEND_HASH_FOREACH_END();
  326. GC_UNPROTECT_RECURSION(myht);
  327. zend_release_properties(myht);
  328. }
  329. if (level > 1) {
  330. php_printf("%*c", level - 1, ' ');
  331. }
  332. PUTS("}\n");
  333. break;
  334. case IS_RESOURCE: {
  335. const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
  336. php_printf("%sresource(%d) of type (%s) refcount(%u)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown", Z_REFCOUNT_P(struc));
  337. break;
  338. }
  339. case IS_REFERENCE:
  340. //??? hide references with refcount==1 (for compatibility)
  341. if (Z_REFCOUNT_P(struc) > 1) {
  342. is_ref = 1;
  343. }
  344. struc = Z_REFVAL_P(struc);
  345. goto again;
  346. default:
  347. php_printf("%sUNKNOWN:0\n", COMMON);
  348. break;
  349. }
  350. }
  351. /* }}} */
  352. /* {{{ proto void debug_zval_dump(mixed var)
  353. Dumps a string representation of an internal zend value to output. */
  354. PHP_FUNCTION(debug_zval_dump)
  355. {
  356. zval *args;
  357. int argc;
  358. int i;
  359. ZEND_PARSE_PARAMETERS_START(1, -1)
  360. Z_PARAM_VARIADIC('+', args, argc)
  361. ZEND_PARSE_PARAMETERS_END();
  362. for (i = 0; i < argc; i++) {
  363. php_debug_zval_dump(&args[i], 1);
  364. }
  365. }
  366. /* }}} */
  367. #define buffer_append_spaces(buf, num_spaces) \
  368. do { \
  369. char *tmp_spaces; \
  370. size_t tmp_spaces_len; \
  371. tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \
  372. smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \
  373. efree(tmp_spaces); \
  374. } while(0);
  375. static void php_array_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
  376. {
  377. if (key == NULL) { /* numeric key */
  378. buffer_append_spaces(buf, level+1);
  379. smart_str_append_long(buf, (zend_long) index);
  380. smart_str_appendl(buf, " => ", 4);
  381. } else { /* string key */
  382. zend_string *tmp_str;
  383. zend_string *ckey = php_addcslashes(key, "'\\", 2);
  384. tmp_str = php_str_to_str(ZSTR_VAL(ckey), ZSTR_LEN(ckey), "\0", 1, "' . \"\\0\" . '", 12);
  385. buffer_append_spaces(buf, level + 1);
  386. smart_str_appendc(buf, '\'');
  387. smart_str_append(buf, tmp_str);
  388. smart_str_appendl(buf, "' => ", 5);
  389. zend_string_free(ckey);
  390. zend_string_free(tmp_str);
  391. }
  392. php_var_export_ex(zv, level + 2, buf);
  393. smart_str_appendc(buf, ',');
  394. smart_str_appendc(buf, '\n');
  395. }
  396. /* }}} */
  397. static void php_object_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
  398. {
  399. buffer_append_spaces(buf, level + 2);
  400. if (key != NULL) {
  401. const char *class_name, *prop_name;
  402. size_t prop_name_len;
  403. zend_string *pname_esc;
  404. zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len);
  405. pname_esc = php_addcslashes_str(prop_name, prop_name_len, "'\\", 2);
  406. smart_str_appendc(buf, '\'');
  407. smart_str_append(buf, pname_esc);
  408. smart_str_appendc(buf, '\'');
  409. zend_string_release_ex(pname_esc, 0);
  410. } else {
  411. smart_str_append_long(buf, (zend_long) index);
  412. }
  413. smart_str_appendl(buf, " => ", 4);
  414. php_var_export_ex(zv, level + 2, buf);
  415. smart_str_appendc(buf, ',');
  416. smart_str_appendc(buf, '\n');
  417. }
  418. /* }}} */
  419. PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
  420. {
  421. HashTable *myht;
  422. char tmp_str[PHP_DOUBLE_MAX_LENGTH];
  423. zend_string *ztmp, *ztmp2;
  424. zend_ulong index;
  425. zend_string *key;
  426. zval *val;
  427. again:
  428. switch (Z_TYPE_P(struc)) {
  429. case IS_FALSE:
  430. smart_str_appendl(buf, "false", 5);
  431. break;
  432. case IS_TRUE:
  433. smart_str_appendl(buf, "true", 4);
  434. break;
  435. case IS_NULL:
  436. smart_str_appendl(buf, "NULL", 4);
  437. break;
  438. case IS_LONG:
  439. /* INT_MIN as a literal will be parsed as a float. Emit something like
  440. * -9223372036854775807-1 to avoid this. */
  441. if (Z_LVAL_P(struc) == ZEND_LONG_MIN) {
  442. smart_str_append_long(buf, ZEND_LONG_MIN+1);
  443. smart_str_appends(buf, "-1");
  444. break;
  445. }
  446. smart_str_append_long(buf, Z_LVAL_P(struc));
  447. break;
  448. case IS_DOUBLE:
  449. php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
  450. smart_str_appends(buf, tmp_str);
  451. /* Without a decimal point, PHP treats a number literal as an int.
  452. * This check even works for scientific notation, because the
  453. * mantissa always contains a decimal point.
  454. * We need to check for finiteness, because INF, -INF and NAN
  455. * must not have a decimal point added.
  456. */
  457. if (zend_finite(Z_DVAL_P(struc)) && NULL == strchr(tmp_str, '.')) {
  458. smart_str_appendl(buf, ".0", 2);
  459. }
  460. break;
  461. case IS_STRING:
  462. ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2);
  463. ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12);
  464. smart_str_appendc(buf, '\'');
  465. smart_str_append(buf, ztmp2);
  466. smart_str_appendc(buf, '\'');
  467. zend_string_free(ztmp);
  468. zend_string_free(ztmp2);
  469. break;
  470. case IS_ARRAY:
  471. myht = Z_ARRVAL_P(struc);
  472. if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
  473. if (GC_IS_RECURSIVE(myht)) {
  474. smart_str_appendl(buf, "NULL", 4);
  475. zend_error(E_WARNING, "var_export does not handle circular references");
  476. return;
  477. }
  478. GC_PROTECT_RECURSION(myht);
  479. }
  480. if (level > 1) {
  481. smart_str_appendc(buf, '\n');
  482. buffer_append_spaces(buf, level - 1);
  483. }
  484. smart_str_appendl(buf, "array (\n", 8);
  485. ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
  486. php_array_element_export(val, index, key, level, buf);
  487. } ZEND_HASH_FOREACH_END();
  488. if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
  489. GC_UNPROTECT_RECURSION(myht);
  490. }
  491. if (level > 1) {
  492. buffer_append_spaces(buf, level - 1);
  493. }
  494. smart_str_appendc(buf, ')');
  495. break;
  496. case IS_OBJECT:
  497. myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT);
  498. if (myht) {
  499. if (GC_IS_RECURSIVE(myht)) {
  500. smart_str_appendl(buf, "NULL", 4);
  501. zend_error(E_WARNING, "var_export does not handle circular references");
  502. zend_release_properties(myht);
  503. return;
  504. } else {
  505. GC_TRY_PROTECT_RECURSION(myht);
  506. }
  507. }
  508. if (level > 1) {
  509. smart_str_appendc(buf, '\n');
  510. buffer_append_spaces(buf, level - 1);
  511. }
  512. /* stdClass has no __set_state method, but can be casted to */
  513. if (Z_OBJCE_P(struc) == zend_standard_class_def) {
  514. smart_str_appendl(buf, "(object) array(\n", 16);
  515. } else {
  516. smart_str_append(buf, Z_OBJCE_P(struc)->name);
  517. smart_str_appendl(buf, "::__set_state(array(\n", 21);
  518. }
  519. if (myht) {
  520. ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
  521. php_object_element_export(val, index, key, level, buf);
  522. } ZEND_HASH_FOREACH_END();
  523. GC_TRY_UNPROTECT_RECURSION(myht);
  524. zend_release_properties(myht);
  525. }
  526. if (level > 1) {
  527. buffer_append_spaces(buf, level - 1);
  528. }
  529. if (Z_OBJCE_P(struc) == zend_standard_class_def) {
  530. smart_str_appendc(buf, ')');
  531. } else {
  532. smart_str_appendl(buf, "))", 2);
  533. }
  534. break;
  535. case IS_REFERENCE:
  536. struc = Z_REFVAL_P(struc);
  537. goto again;
  538. break;
  539. default:
  540. smart_str_appendl(buf, "NULL", 4);
  541. break;
  542. }
  543. }
  544. /* }}} */
  545. /* FOR BC reasons, this will always perform and then print */
  546. PHPAPI void php_var_export(zval *struc, int level) /* {{{ */
  547. {
  548. smart_str buf = {0};
  549. php_var_export_ex(struc, level, &buf);
  550. smart_str_0(&buf);
  551. PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
  552. smart_str_free(&buf);
  553. }
  554. /* }}} */
  555. /* {{{ proto mixed var_export(mixed var [, bool return])
  556. Outputs or returns a string representation of a variable */
  557. PHP_FUNCTION(var_export)
  558. {
  559. zval *var;
  560. zend_bool return_output = 0;
  561. smart_str buf = {0};
  562. ZEND_PARSE_PARAMETERS_START(1, 2)
  563. Z_PARAM_ZVAL(var)
  564. Z_PARAM_OPTIONAL
  565. Z_PARAM_BOOL(return_output)
  566. ZEND_PARSE_PARAMETERS_END();
  567. php_var_export_ex(var, 1, &buf);
  568. smart_str_0 (&buf);
  569. if (return_output) {
  570. RETURN_NEW_STR(buf.s);
  571. } else {
  572. PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
  573. smart_str_free(&buf);
  574. }
  575. }
  576. /* }}} */
  577. static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash);
  578. static inline zend_long php_add_var_hash(php_serialize_data_t data, zval *var) /* {{{ */
  579. {
  580. zval *zv;
  581. zend_ulong key;
  582. zend_bool is_ref = Z_ISREF_P(var);
  583. data->n += 1;
  584. if (!is_ref && Z_TYPE_P(var) != IS_OBJECT) {
  585. return 0;
  586. }
  587. /* References to objects are treated as if the reference didn't exist */
  588. if (is_ref && Z_TYPE_P(Z_REFVAL_P(var)) == IS_OBJECT) {
  589. var = Z_REFVAL_P(var);
  590. }
  591. /* Index for the variable is stored using the numeric value of the pointer to
  592. * the zend_refcounted struct */
  593. key = (zend_ulong) (zend_uintptr_t) Z_COUNTED_P(var);
  594. zv = zend_hash_index_find(&data->ht, key);
  595. if (zv) {
  596. /* References are only counted once, undo the data->n increment above */
  597. if (is_ref) {
  598. data->n -= 1;
  599. }
  600. return Z_LVAL_P(zv);
  601. } else {
  602. zval zv_n;
  603. ZVAL_LONG(&zv_n, data->n);
  604. zend_hash_index_add_new(&data->ht, key, &zv_n);
  605. /* Additionally to the index, we also store the variable, to ensure that it is
  606. * not destroyed during serialization and its pointer reused. The variable is
  607. * stored at the numeric value of the pointer + 1, which cannot be the location
  608. * of another zend_refcounted structure. */
  609. zend_hash_index_add_new(&data->ht, key + 1, var);
  610. Z_ADDREF_P(var);
  611. return 0;
  612. }
  613. }
  614. /* }}} */
  615. static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */
  616. {
  617. smart_str_appendl(buf, "i:", 2);
  618. smart_str_append_long(buf, val);
  619. smart_str_appendc(buf, ';');
  620. }
  621. /* }}} */
  622. static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */
  623. {
  624. smart_str_appendl(buf, "s:", 2);
  625. smart_str_append_unsigned(buf, len);
  626. smart_str_appendl(buf, ":\"", 2);
  627. smart_str_appendl(buf, str, len);
  628. smart_str_appendl(buf, "\";", 2);
  629. }
  630. /* }}} */
  631. static inline zend_bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */
  632. {
  633. PHP_CLASS_ATTRIBUTES;
  634. PHP_SET_CLASS_ATTRIBUTES(struc);
  635. smart_str_appendl(buf, "O:", 2);
  636. smart_str_append_unsigned(buf, ZSTR_LEN(class_name));
  637. smart_str_appendl(buf, ":\"", 2);
  638. smart_str_append(buf, class_name);
  639. smart_str_appendl(buf, "\":", 2);
  640. PHP_CLEANUP_CLASS_ATTRIBUTES();
  641. return incomplete_class;
  642. }
  643. /* }}} */
  644. static int php_var_serialize_call_sleep(zval *retval, zval *struc) /* {{{ */
  645. {
  646. zval fname;
  647. int res;
  648. ZVAL_STRINGL(&fname, "__sleep", sizeof("__sleep") - 1);
  649. BG(serialize_lock)++;
  650. res = call_user_function(NULL, struc, &fname, retval, 0, 0);
  651. BG(serialize_lock)--;
  652. zval_ptr_dtor_str(&fname);
  653. if (res == FAILURE || Z_ISUNDEF_P(retval)) {
  654. zval_ptr_dtor(retval);
  655. return FAILURE;
  656. }
  657. if (!HASH_OF(retval)) {
  658. zend_class_entry *ce;
  659. ZEND_ASSERT(Z_TYPE_P(struc) == IS_OBJECT);
  660. ce = Z_OBJCE_P(struc);
  661. zval_ptr_dtor(retval);
  662. php_error_docref(NULL, E_NOTICE, "%s::__sleep should return an array only containing the names of instance-variables to serialize", ZSTR_VAL(ce->name));
  663. return FAILURE;
  664. }
  665. return SUCCESS;
  666. }
  667. /* }}} */
  668. static int php_var_serialize_call_magic_serialize(zval *retval, zval *obj) /* {{{ */
  669. {
  670. zval fname;
  671. int res;
  672. ZVAL_STRINGL(&fname, "__serialize", sizeof("__serialize") - 1);
  673. BG(serialize_lock)++;
  674. res = call_user_function(CG(function_table), obj, &fname, retval, 0, 0);
  675. BG(serialize_lock)--;
  676. zval_ptr_dtor_str(&fname);
  677. if (res == FAILURE || Z_ISUNDEF_P(retval)) {
  678. zval_ptr_dtor(retval);
  679. return FAILURE;
  680. }
  681. if (Z_TYPE_P(retval) != IS_ARRAY) {
  682. zval_ptr_dtor(retval);
  683. zend_type_error("%s::__serialize() must return an array", ZSTR_VAL(Z_OBJCE_P(obj)->name));
  684. return FAILURE;
  685. }
  686. return SUCCESS;
  687. }
  688. /* }}} */
  689. static int php_var_serialize_try_add_sleep_prop(
  690. HashTable *ht, HashTable *props, zend_string *name, zend_string *error_name, zval *struc) /* {{{ */
  691. {
  692. zval *val = zend_hash_find(props, name);
  693. if (val == NULL) {
  694. return FAILURE;
  695. }
  696. if (Z_TYPE_P(val) == IS_INDIRECT) {
  697. val = Z_INDIRECT_P(val);
  698. if (Z_TYPE_P(val) == IS_UNDEF) {
  699. zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
  700. if (info) {
  701. return SUCCESS;
  702. }
  703. return FAILURE;
  704. }
  705. }
  706. if (!zend_hash_add(ht, name, val)) {
  707. php_error_docref(NULL, E_NOTICE,
  708. "\"%s\" is returned from __sleep multiple times", ZSTR_VAL(error_name));
  709. return SUCCESS;
  710. }
  711. Z_TRY_ADDREF_P(val);
  712. return SUCCESS;
  713. }
  714. /* }}} */
  715. static int php_var_serialize_get_sleep_props(
  716. HashTable *ht, zval *struc, HashTable *sleep_retval) /* {{{ */
  717. {
  718. zend_class_entry *ce = Z_OBJCE_P(struc);
  719. HashTable *props = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
  720. zval *name_val;
  721. int retval = SUCCESS;
  722. zend_hash_init(ht, zend_hash_num_elements(sleep_retval), NULL, ZVAL_PTR_DTOR, 0);
  723. /* TODO: Rewrite this by fetching the property info instead of trying out different
  724. * name manglings? */
  725. ZEND_HASH_FOREACH_VAL(sleep_retval, name_val) {
  726. zend_string *name, *tmp_name, *priv_name, *prot_name;
  727. ZVAL_DEREF(name_val);
  728. if (Z_TYPE_P(name_val) != IS_STRING) {
  729. php_error_docref(NULL, E_NOTICE,
  730. "%s::__sleep should return an array only containing the names of instance-variables to serialize",
  731. ZSTR_VAL(ce->name));
  732. }
  733. name = zval_get_tmp_string(name_val, &tmp_name);
  734. if (php_var_serialize_try_add_sleep_prop(ht, props, name, name, struc) == SUCCESS) {
  735. zend_tmp_string_release(tmp_name);
  736. continue;
  737. }
  738. if (EG(exception)) {
  739. zend_tmp_string_release(tmp_name);
  740. retval = FAILURE;
  741. break;
  742. }
  743. priv_name = zend_mangle_property_name(
  744. ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
  745. ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
  746. if (php_var_serialize_try_add_sleep_prop(ht, props, priv_name, name, struc) == SUCCESS) {
  747. zend_tmp_string_release(tmp_name);
  748. zend_string_release(priv_name);
  749. continue;
  750. }
  751. zend_string_release(priv_name);
  752. if (EG(exception)) {
  753. zend_tmp_string_release(tmp_name);
  754. retval = FAILURE;
  755. break;
  756. }
  757. prot_name = zend_mangle_property_name(
  758. "*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
  759. if (php_var_serialize_try_add_sleep_prop(ht, props, prot_name, name, struc) == SUCCESS) {
  760. zend_tmp_string_release(tmp_name);
  761. zend_string_release(prot_name);
  762. continue;
  763. }
  764. zend_string_release(prot_name);
  765. if (EG(exception)) {
  766. zend_tmp_string_release(tmp_name);
  767. retval = FAILURE;
  768. break;
  769. }
  770. php_error_docref(NULL, E_NOTICE,
  771. "\"%s\" returned as member variable from __sleep() but does not exist", ZSTR_VAL(name));
  772. zend_tmp_string_release(tmp_name);
  773. } ZEND_HASH_FOREACH_END();
  774. zend_release_properties(props);
  775. return retval;
  776. }
  777. /* }}} */
  778. static void php_var_serialize_nested_data(smart_str *buf, zval *struc, HashTable *ht, uint32_t count, zend_bool incomplete_class, php_serialize_data_t var_hash) /* {{{ */
  779. {
  780. smart_str_append_unsigned(buf, count);
  781. smart_str_appendl(buf, ":{", 2);
  782. if (count > 0) {
  783. zend_string *key;
  784. zval *data;
  785. zend_ulong index;
  786. ZEND_HASH_FOREACH_KEY_VAL_IND(ht, index, key, data) {
  787. if (incomplete_class && strcmp(ZSTR_VAL(key), MAGIC_MEMBER) == 0) {
  788. continue;
  789. }
  790. if (!key) {
  791. php_var_serialize_long(buf, index);
  792. } else {
  793. php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
  794. }
  795. if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
  796. data = Z_REFVAL_P(data);
  797. }
  798. /* we should still add element even if it's not OK,
  799. * since we already wrote the length of the array before */
  800. if (Z_TYPE_P(data) == IS_ARRAY) {
  801. if (UNEXPECTED(Z_IS_RECURSIVE_P(data))
  802. || UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) {
  803. php_add_var_hash(var_hash, struc);
  804. smart_str_appendl(buf, "N;", 2);
  805. } else {
  806. if (Z_REFCOUNTED_P(data)) {
  807. Z_PROTECT_RECURSION_P(data);
  808. }
  809. php_var_serialize_intern(buf, data, var_hash);
  810. if (Z_REFCOUNTED_P(data)) {
  811. Z_UNPROTECT_RECURSION_P(data);
  812. }
  813. }
  814. } else {
  815. php_var_serialize_intern(buf, data, var_hash);
  816. }
  817. } ZEND_HASH_FOREACH_END();
  818. }
  819. smart_str_appendc(buf, '}');
  820. }
  821. /* }}} */
  822. static void php_var_serialize_class(smart_str *buf, zval *struc, zval *retval_ptr, php_serialize_data_t var_hash) /* {{{ */
  823. {
  824. HashTable props;
  825. if (php_var_serialize_get_sleep_props(&props, struc, HASH_OF(retval_ptr)) == SUCCESS) {
  826. php_var_serialize_class_name(buf, struc);
  827. php_var_serialize_nested_data(
  828. buf, struc, &props, zend_hash_num_elements(&props), /* incomplete_class */ 0, var_hash);
  829. }
  830. zend_hash_destroy(&props);
  831. }
  832. /* }}} */
  833. static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash) /* {{{ */
  834. {
  835. zend_long var_already;
  836. HashTable *myht;
  837. if (EG(exception)) {
  838. return;
  839. }
  840. if (var_hash && (var_already = php_add_var_hash(var_hash, struc))) {
  841. if (var_already == -1) {
  842. /* Reference to an object that failed to serialize, replace with null. */
  843. smart_str_appendl(buf, "N;", 2);
  844. return;
  845. } else if (Z_ISREF_P(struc)) {
  846. smart_str_appendl(buf, "R:", 2);
  847. smart_str_append_long(buf, var_already);
  848. smart_str_appendc(buf, ';');
  849. return;
  850. } else if (Z_TYPE_P(struc) == IS_OBJECT) {
  851. smart_str_appendl(buf, "r:", 2);
  852. smart_str_append_long(buf, var_already);
  853. smart_str_appendc(buf, ';');
  854. return;
  855. }
  856. }
  857. again:
  858. switch (Z_TYPE_P(struc)) {
  859. case IS_FALSE:
  860. smart_str_appendl(buf, "b:0;", 4);
  861. return;
  862. case IS_TRUE:
  863. smart_str_appendl(buf, "b:1;", 4);
  864. return;
  865. case IS_NULL:
  866. smart_str_appendl(buf, "N;", 2);
  867. return;
  868. case IS_LONG:
  869. php_var_serialize_long(buf, Z_LVAL_P(struc));
  870. return;
  871. case IS_DOUBLE: {
  872. char tmp_str[PHP_DOUBLE_MAX_LENGTH];
  873. smart_str_appendl(buf, "d:", 2);
  874. php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
  875. smart_str_appends(buf, tmp_str);
  876. smart_str_appendc(buf, ';');
  877. return;
  878. }
  879. case IS_STRING:
  880. php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc));
  881. return;
  882. case IS_OBJECT: {
  883. zend_class_entry *ce = Z_OBJCE_P(struc);
  884. zend_bool incomplete_class;
  885. uint32_t count;
  886. if (zend_hash_str_exists(&ce->function_table, "__serialize", sizeof("__serialize")-1)) {
  887. zval retval, obj;
  888. zend_string *key;
  889. zval *data;
  890. zend_ulong index;
  891. Z_ADDREF_P(struc);
  892. ZVAL_OBJ(&obj, Z_OBJ_P(struc));
  893. if (php_var_serialize_call_magic_serialize(&retval, &obj) == FAILURE) {
  894. if (!EG(exception)) {
  895. smart_str_appendl(buf, "N;", 2);
  896. }
  897. zval_ptr_dtor(&obj);
  898. return;
  899. }
  900. php_var_serialize_class_name(buf, &obj);
  901. smart_str_append_unsigned(buf, zend_array_count(Z_ARRVAL(retval)));
  902. smart_str_appendl(buf, ":{", 2);
  903. ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL(retval), index, key, data) {
  904. if (!key) {
  905. php_var_serialize_long(buf, index);
  906. } else {
  907. php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
  908. }
  909. if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
  910. data = Z_REFVAL_P(data);
  911. }
  912. php_var_serialize_intern(buf, data, var_hash);
  913. } ZEND_HASH_FOREACH_END();
  914. smart_str_appendc(buf, '}');
  915. zval_ptr_dtor(&obj);
  916. zval_ptr_dtor(&retval);
  917. return;
  918. }
  919. if (ce->serialize != NULL) {
  920. /* has custom handler */
  921. unsigned char *serialized_data = NULL;
  922. size_t serialized_length;
  923. if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) {
  924. smart_str_appendl(buf, "C:", 2);
  925. smart_str_append_unsigned(buf, ZSTR_LEN(Z_OBJCE_P(struc)->name));
  926. smart_str_appendl(buf, ":\"", 2);
  927. smart_str_append(buf, Z_OBJCE_P(struc)->name);
  928. smart_str_appendl(buf, "\":", 2);
  929. smart_str_append_unsigned(buf, serialized_length);
  930. smart_str_appendl(buf, ":{", 2);
  931. smart_str_appendl(buf, (char *) serialized_data, serialized_length);
  932. smart_str_appendc(buf, '}');
  933. } else {
  934. /* Mark this value in the var_hash, to avoid creating references to it. */
  935. zval *var_idx = zend_hash_index_find(&var_hash->ht,
  936. (zend_ulong) (zend_uintptr_t) Z_COUNTED_P(struc));
  937. ZVAL_LONG(var_idx, -1);
  938. smart_str_appendl(buf, "N;", 2);
  939. }
  940. if (serialized_data) {
  941. efree(serialized_data);
  942. }
  943. return;
  944. }
  945. if (ce != PHP_IC_ENTRY && zend_hash_str_exists(&ce->function_table, "__sleep", sizeof("__sleep")-1)) {
  946. zval retval, tmp;
  947. Z_ADDREF_P(struc);
  948. ZVAL_OBJ(&tmp, Z_OBJ_P(struc));
  949. if (php_var_serialize_call_sleep(&retval, &tmp) == FAILURE) {
  950. if (!EG(exception)) {
  951. /* we should still add element even if it's not OK,
  952. * since we already wrote the length of the array before */
  953. smart_str_appendl(buf, "N;", 2);
  954. }
  955. zval_ptr_dtor(&tmp);
  956. return;
  957. }
  958. php_var_serialize_class(buf, &tmp, &retval, var_hash);
  959. zval_ptr_dtor(&retval);
  960. zval_ptr_dtor(&tmp);
  961. return;
  962. }
  963. incomplete_class = php_var_serialize_class_name(buf, struc);
  964. myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
  965. /* count after serializing name, since php_var_serialize_class_name
  966. * changes the count if the variable is incomplete class */
  967. count = zend_array_count(myht);
  968. if (count > 0 && incomplete_class) {
  969. --count;
  970. }
  971. php_var_serialize_nested_data(buf, struc, myht, count, incomplete_class, var_hash);
  972. zend_release_properties(myht);
  973. return;
  974. }
  975. case IS_ARRAY:
  976. smart_str_appendl(buf, "a:", 2);
  977. myht = Z_ARRVAL_P(struc);
  978. php_var_serialize_nested_data(
  979. buf, struc, myht, zend_array_count(myht), /* incomplete_class */ 0, var_hash);
  980. return;
  981. case IS_REFERENCE:
  982. struc = Z_REFVAL_P(struc);
  983. goto again;
  984. default:
  985. smart_str_appendl(buf, "i:0;", 4);
  986. return;
  987. }
  988. }
  989. /* }}} */
  990. PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */
  991. {
  992. php_var_serialize_intern(buf, struc, *data);
  993. smart_str_0(buf);
  994. }
  995. /* }}} */
  996. PHPAPI php_serialize_data_t php_var_serialize_init() {
  997. struct php_serialize_data *d;
  998. /* fprintf(stderr, "SERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
  999. if (BG(serialize_lock) || !BG(serialize).level) {
  1000. d = emalloc(sizeof(struct php_serialize_data));
  1001. zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0);
  1002. d->n = 0;
  1003. if (!BG(serialize_lock)) {
  1004. BG(serialize).data = d;
  1005. BG(serialize).level = 1;
  1006. }
  1007. } else {
  1008. d = BG(serialize).data;
  1009. ++BG(serialize).level;
  1010. }
  1011. return d;
  1012. }
  1013. PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) {
  1014. /* fprintf(stderr, "SERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
  1015. if (BG(serialize_lock) || BG(serialize).level == 1) {
  1016. zend_hash_destroy(&d->ht);
  1017. efree(d);
  1018. }
  1019. if (!BG(serialize_lock) && !--BG(serialize).level) {
  1020. BG(serialize).data = NULL;
  1021. }
  1022. }
  1023. /* {{{ proto string serialize(mixed variable)
  1024. Returns a string representation of variable (which can later be unserialized) */
  1025. PHP_FUNCTION(serialize)
  1026. {
  1027. zval *struc;
  1028. php_serialize_data_t var_hash;
  1029. smart_str buf = {0};
  1030. ZEND_PARSE_PARAMETERS_START(1, 1)
  1031. Z_PARAM_ZVAL(struc)
  1032. ZEND_PARSE_PARAMETERS_END();
  1033. PHP_VAR_SERIALIZE_INIT(var_hash);
  1034. php_var_serialize(&buf, struc, &var_hash);
  1035. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  1036. if (EG(exception)) {
  1037. smart_str_free(&buf);
  1038. RETURN_THROWS();
  1039. }
  1040. if (buf.s) {
  1041. RETURN_NEW_STR(buf.s);
  1042. } else {
  1043. RETURN_EMPTY_STRING();
  1044. }
  1045. }
  1046. /* }}} */
  1047. /* {{{ proto mixed unserialize(string variable_representation[, array options])
  1048. Takes a string representation of variable and recreates it */
  1049. PHP_FUNCTION(unserialize)
  1050. {
  1051. char *buf = NULL;
  1052. size_t buf_len;
  1053. const unsigned char *p;
  1054. php_unserialize_data_t var_hash;
  1055. zval *options = NULL;
  1056. zval *retval;
  1057. HashTable *class_hash = NULL, *prev_class_hash;
  1058. zend_long prev_max_depth, prev_cur_depth;
  1059. ZEND_PARSE_PARAMETERS_START(1, 2)
  1060. Z_PARAM_STRING(buf, buf_len)
  1061. Z_PARAM_OPTIONAL
  1062. Z_PARAM_ARRAY(options)
  1063. ZEND_PARSE_PARAMETERS_END();
  1064. if (buf_len == 0) {
  1065. RETURN_FALSE;
  1066. }
  1067. p = (const unsigned char*) buf;
  1068. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  1069. prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash);
  1070. prev_max_depth = php_var_unserialize_get_max_depth(var_hash);
  1071. prev_cur_depth = php_var_unserialize_get_cur_depth(var_hash);
  1072. if (options != NULL) {
  1073. zval *classes, *max_depth;
  1074. classes = zend_hash_str_find_deref(Z_ARRVAL_P(options), "allowed_classes", sizeof("allowed_classes")-1);
  1075. if (classes && Z_TYPE_P(classes) != IS_ARRAY && Z_TYPE_P(classes) != IS_TRUE && Z_TYPE_P(classes) != IS_FALSE) {
  1076. php_error_docref(NULL, E_WARNING, "allowed_classes option should be array or boolean");
  1077. RETVAL_FALSE;
  1078. goto cleanup;
  1079. }
  1080. if(classes && (Z_TYPE_P(classes) == IS_ARRAY || !zend_is_true(classes))) {
  1081. ALLOC_HASHTABLE(class_hash);
  1082. zend_hash_init(class_hash, (Z_TYPE_P(classes) == IS_ARRAY)?zend_hash_num_elements(Z_ARRVAL_P(classes)):0, NULL, NULL, 0);
  1083. }
  1084. if(class_hash && Z_TYPE_P(classes) == IS_ARRAY) {
  1085. zval *entry;
  1086. zend_string *lcname;
  1087. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(classes), entry) {
  1088. convert_to_string_ex(entry);
  1089. lcname = zend_string_tolower(Z_STR_P(entry));
  1090. zend_hash_add_empty_element(class_hash, lcname);
  1091. zend_string_release_ex(lcname, 0);
  1092. } ZEND_HASH_FOREACH_END();
  1093. /* Exception during string conversion. */
  1094. if (EG(exception)) {
  1095. goto cleanup;
  1096. }
  1097. }
  1098. php_var_unserialize_set_allowed_classes(var_hash, class_hash);
  1099. max_depth = zend_hash_str_find_deref(Z_ARRVAL_P(options), "max_depth", sizeof("max_depth") - 1);
  1100. if (max_depth) {
  1101. if (Z_TYPE_P(max_depth) != IS_LONG) {
  1102. zend_type_error("unserialize(): 'max_depth' option must be of type int, %s given", zend_zval_type_name(max_depth));
  1103. goto cleanup;
  1104. }
  1105. if (Z_LVAL_P(max_depth) < 0) {
  1106. zend_value_error("unserialize(): 'max_depth' option must be greater than or equal to 0");
  1107. goto cleanup;
  1108. }
  1109. php_var_unserialize_set_max_depth(var_hash, Z_LVAL_P(max_depth));
  1110. /* If the max_depth for a nested unserialize() call has been overridden,
  1111. * start counting from zero again (for the nested call only). */
  1112. php_var_unserialize_set_cur_depth(var_hash, 0);
  1113. }
  1114. }
  1115. if (BG(unserialize).level > 1) {
  1116. retval = var_tmp_var(&var_hash);
  1117. } else {
  1118. retval = return_value;
  1119. }
  1120. if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) {
  1121. if (!EG(exception)) {
  1122. php_error_docref(NULL, E_NOTICE, "Error at offset " ZEND_LONG_FMT " of %zd bytes",
  1123. (zend_long)((char*)p - buf), buf_len);
  1124. }
  1125. if (BG(unserialize).level <= 1) {
  1126. zval_ptr_dtor(return_value);
  1127. }
  1128. RETVAL_FALSE;
  1129. } else if (BG(unserialize).level > 1) {
  1130. ZVAL_COPY(return_value, retval);
  1131. } else if (Z_REFCOUNTED_P(return_value)) {
  1132. zend_refcounted *ref = Z_COUNTED_P(return_value);
  1133. gc_check_possible_root(ref);
  1134. }
  1135. cleanup:
  1136. if (class_hash) {
  1137. zend_hash_destroy(class_hash);
  1138. FREE_HASHTABLE(class_hash);
  1139. }
  1140. /* Reset to previous options in case this is a nested call */
  1141. php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash);
  1142. php_var_unserialize_set_max_depth(var_hash, prev_max_depth);
  1143. php_var_unserialize_set_cur_depth(var_hash, prev_cur_depth);
  1144. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  1145. /* Per calling convention we must not return a reference here, so unwrap. We're doing this at
  1146. * the very end, because __wakeup() calls performed during UNSERIALIZE_DESTROY might affect
  1147. * the value we unwrap here. This is compatible with behavior in PHP <=7.0. */
  1148. if (Z_ISREF_P(return_value)) {
  1149. zend_unwrap_reference(return_value);
  1150. }
  1151. }
  1152. /* }}} */
  1153. /* {{{ proto int memory_get_usage([bool real_usage])
  1154. Returns the allocated by PHP memory */
  1155. PHP_FUNCTION(memory_get_usage) {
  1156. zend_bool real_usage = 0;
  1157. ZEND_PARSE_PARAMETERS_START(0, 1)
  1158. Z_PARAM_OPTIONAL
  1159. Z_PARAM_BOOL(real_usage)
  1160. ZEND_PARSE_PARAMETERS_END();
  1161. RETURN_LONG(zend_memory_usage(real_usage));
  1162. }
  1163. /* }}} */
  1164. /* {{{ proto int memory_get_peak_usage([bool real_usage])
  1165. Returns the peak allocated by PHP memory */
  1166. PHP_FUNCTION(memory_get_peak_usage) {
  1167. zend_bool real_usage = 0;
  1168. ZEND_PARSE_PARAMETERS_START(0, 1)
  1169. Z_PARAM_OPTIONAL
  1170. Z_PARAM_BOOL(real_usage)
  1171. ZEND_PARSE_PARAMETERS_END();
  1172. RETURN_LONG(zend_memory_peak_usage(real_usage));
  1173. }
  1174. /* }}} */
  1175. PHP_INI_BEGIN()
  1176. STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals)
  1177. PHP_INI_END()
  1178. PHP_MINIT_FUNCTION(var)
  1179. {
  1180. REGISTER_INI_ENTRIES();
  1181. return SUCCESS;
  1182. }