xref: /PHP-7.0/ext/standard/php_var.h (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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    | Author: Jani Lehtimäki <jkl@njet.net>                                |
16    +----------------------------------------------------------------------+
17 */
18 
19 /* $Id$ */
20 
21 #ifndef PHP_VAR_H
22 #define PHP_VAR_H
23 
24 #include "ext/standard/basic_functions.h"
25 #include "zend_smart_str_public.h"
26 
27 PHP_FUNCTION(var_dump);
28 PHP_FUNCTION(var_export);
29 PHP_FUNCTION(debug_zval_dump);
30 PHP_FUNCTION(serialize);
31 PHP_FUNCTION(unserialize);
32 PHP_FUNCTION(memory_get_usage);
33 PHP_FUNCTION(memory_get_peak_usage);
34 
35 PHPAPI void php_var_dump(zval *struc, int level);
36 PHPAPI void php_var_export(zval *struc, int level);
37 PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf);
38 
39 PHPAPI void php_debug_zval_dump(zval *struc, int level);
40 
41 struct php_serialize_data {
42 	HashTable ht;
43 	uint32_t n;
44 };
45 
46 struct php_unserialize_data {
47 	void *first;
48 	void *last;
49 	void *first_dtor;
50 	void *last_dtor;
51 };
52 
53 typedef struct php_serialize_data *php_serialize_data_t;
54 typedef struct php_unserialize_data *php_unserialize_data_t;
55 
56 PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data);
57 PHPAPI int php_var_unserialize(zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash);
58 PHPAPI int php_var_unserialize_ref(zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash);
59 PHPAPI int php_var_unserialize_intern(zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash);
60 PHPAPI int php_var_unserialize_ex(zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash, HashTable *classes);
61 
62 #define PHP_VAR_SERIALIZE_INIT(d) \
63 do  { \
64 	/* fprintf(stderr, "SERIALIZE_INIT      == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ \
65 	if (BG(serialize_lock) || !BG(serialize).level) { \
66 		(d) = (php_serialize_data_t) emalloc(sizeof(struct php_serialize_data)); \
67 		zend_hash_init(&(d)->ht, 16, NULL, ZVAL_PTR_DTOR, 0); \
68 		(d)->n = 0; \
69 		if (!BG(serialize_lock)) { \
70 			BG(serialize).data = d; \
71 			BG(serialize).level = 1; \
72 		} \
73 	} else { \
74 		(d) = BG(serialize).data; \
75 		++BG(serialize).level; \
76 	} \
77 } while(0)
78 
79 #define PHP_VAR_SERIALIZE_DESTROY(d) \
80 do { \
81 	/* fprintf(stderr, "SERIALIZE_DESTROY   == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ \
82 	if (BG(serialize_lock) || BG(serialize).level == 1) { \
83 		zend_hash_destroy(&(d)->ht); \
84 		efree((d)); \
85 	} \
86 	if (!BG(serialize_lock) && !--BG(serialize).level) { \
87 		BG(serialize).data = NULL; \
88 	} \
89 } while (0)
90 
91 #define PHP_VAR_UNSERIALIZE_INIT(d) \
92 do { \
93 	/* fprintf(stderr, "UNSERIALIZE_INIT    == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */ \
94 	if (BG(serialize_lock) || !BG(unserialize).level) { \
95 		(d) = (php_unserialize_data_t)ecalloc(1, sizeof(struct php_unserialize_data)); \
96 		if (!BG(serialize_lock)) { \
97 			BG(unserialize).data = (d); \
98 			BG(unserialize).level = 1; \
99 		} \
100 	} else { \
101 		(d) = BG(unserialize).data; \
102 		++BG(unserialize).level; \
103 	} \
104 } while (0)
105 
106 #define PHP_VAR_UNSERIALIZE_DESTROY(d) \
107 do { \
108 	/* fprintf(stderr, "UNSERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */ \
109 	if (BG(serialize_lock) || BG(unserialize).level == 1) { \
110 		var_destroy(&(d)); \
111 		efree((d)); \
112 	} \
113 	if (!BG(serialize_lock) && !--BG(unserialize).level) { \
114 		BG(unserialize).data = NULL; \
115 	} \
116 } while (0)
117 
118 PHPAPI void var_replace(php_unserialize_data_t *var_hash, zval *ozval, zval *nzval);
119 PHPAPI void var_push_dtor(php_unserialize_data_t *var_hash, zval *val);
120 PHPAPI zval *var_tmp_var(php_unserialize_data_t *var_hashx);
121 PHPAPI void var_destroy(php_unserialize_data_t *var_hash);
122 
123 #endif /* PHP_VAR_H */
124