var.c 29 KB

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