var.c 41 KB

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