var.c 33 KB

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