1/*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2015 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 "ext/standard/php_smart_str_public.h"
26
27PHP_FUNCTION(var_dump);
28PHP_FUNCTION(var_export);
29PHP_FUNCTION(debug_zval_dump);
30PHP_FUNCTION(serialize);
31PHP_FUNCTION(unserialize);
32PHP_FUNCTION(memory_get_usage);
33PHP_FUNCTION(memory_get_peak_usage);
34
35PHPAPI void php_var_dump(zval **struc, int level TSRMLS_DC);
36PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC);
37PHPAPI void php_var_export_ex(zval **struc, int level, smart_str *buf TSRMLS_DC);
38
39PHPAPI void php_debug_zval_dump(zval **struc, int level TSRMLS_DC);
40
41typedef HashTable* php_serialize_data_t;
42
43struct php_unserialize_data {
44 void *first;
45 void *last;
46 void *first_dtor;
47 void *last_dtor;
48};
49
50typedef struct php_unserialize_data* php_unserialize_data_t;
51
52PHPAPI void php_var_serialize(smart_str *buf, zval **struc, php_serialize_data_t *var_hash TSRMLS_DC);
53PHPAPI int php_var_unserialize(zval **rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash TSRMLS_DC);
54
55#define PHP_VAR_SERIALIZE_INIT(var_hash_ptr) \
56do { \
57 /* fprintf(stderr, "SERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ \
58 if (BG(serialize_lock) || !BG(serialize).level) { \
59 ALLOC_HASHTABLE(var_hash_ptr); \
60 zend_hash_init((var_hash_ptr), 10, NULL, NULL, 0); \
61 if (!BG(serialize_lock)) { \
62 BG(serialize).var_hash = (void *)(var_hash_ptr); \
63 BG(serialize).level = 1; \
64 } \
65 } else { \
66 (var_hash_ptr) = (php_serialize_data_t)BG(serialize).var_hash; \
67 ++BG(serialize).level; \
68 } \
69} while(0)
70
71#define PHP_VAR_SERIALIZE_DESTROY(var_hash_ptr) \
72do { \
73 /* fprintf(stderr, "SERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ \
74 if (BG(serialize_lock) || !BG(serialize).level) { \
75 zend_hash_destroy((var_hash_ptr)); \
76 FREE_HASHTABLE(var_hash_ptr); \
77 } else { \
78 if (!--BG(serialize).level) { \
79 zend_hash_destroy((php_serialize_data_t)BG(serialize).var_hash); \
80 FREE_HASHTABLE((php_serialize_data_t)BG(serialize).var_hash); \
81 BG(serialize).var_hash = NULL; \
82 } \
83 } \
84} while (0)
85
86#define PHP_VAR_UNSERIALIZE_INIT(var_hash_ptr) \
87do { \
88 /* fprintf(stderr, "UNSERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */ \
89 if (BG(serialize_lock) || !BG(unserialize).level) { \
90 (var_hash_ptr) = (php_unserialize_data_t)ecalloc(1, sizeof(struct php_unserialize_data)); \
91 if (!BG(serialize_lock)) { \
92 BG(unserialize).var_hash = (void *)(var_hash_ptr); \
93 BG(unserialize).level = 1; \
94 } \
95 } else { \
96 (var_hash_ptr) = (php_unserialize_data_t)BG(unserialize).var_hash; \
97 ++BG(unserialize).level; \
98 } \
99} while (0)
100
101#define PHP_VAR_UNSERIALIZE_DESTROY(var_hash_ptr) \
102do { \
103 /* fprintf(stderr, "UNSERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */ \
104 if (BG(serialize_lock) || !BG(unserialize).level) { \
105 var_destroy(&(var_hash_ptr)); \
106 efree(var_hash_ptr); \
107 } else { \
108 if (!--BG(unserialize).level) { \
109 var_destroy(&(var_hash_ptr)); \
110 efree((var_hash_ptr)); \
111 BG(unserialize).var_hash = NULL; \
112 } \
113 } \
114} while (0)
115
116PHPAPI void var_replace(php_unserialize_data_t *var_hash, zval *ozval, zval **nzval);
117PHPAPI void var_push_dtor(php_unserialize_data_t *var_hash, zval **val);
118PHPAPI void var_push_dtor_no_addref(php_unserialize_data_t *var_hashx, zval **rval);
119PHPAPI void var_destroy(php_unserialize_data_t *var_hash);
120
121#define PHP_VAR_UNSERIALIZE_ZVAL_CHANGED(var_hash, ozval, nzval) \
122 var_replace((var_hash), (ozval), &(nzval))
123
124PHPAPI zend_class_entry *php_create_empty_class(char *class_name, int len);
125
126static inline int php_varname_check(char *name, int name_len, zend_bool silent TSRMLS_DC) /* {{{ */
127{
128 if (name_len == sizeof("GLOBALS") - 1 && !memcmp(name, "GLOBALS", sizeof("GLOBALS") - 1)) {
129 if (!silent) {
130 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempted GLOBALS variable overwrite");
131 }
132 return FAILURE;
133 } else if (name[0] == '_' &&
134 (
135 (name_len == sizeof("_GET") - 1 && !memcmp(name, "_GET", sizeof("_GET") - 1)) ||
136 (name_len == sizeof("_POST") - 1 && !memcmp(name, "_POST", sizeof("_POST") - 1)) ||
137 (name_len == sizeof("_COOKIE") - 1 && !memcmp(name, "_COOKIE", sizeof("_COOKIE") - 1)) ||
138 (name_len == sizeof("_ENV") - 1 && !memcmp(name, "_ENV", sizeof("_ENV") - 1)) ||
139 (name_len == sizeof("_SERVER") - 1 && !memcmp(name, "_SERVER", sizeof("_SERVER") - 1)) ||
140 (name_len == sizeof("_SESSION") - 1 && !memcmp(name, "_SESSION", sizeof("_SESSION") - 1)) ||
141 (name_len == sizeof("_FILES") - 1 && !memcmp(name, "_FILES", sizeof("_FILES") - 1)) ||
142 (name_len == sizeof("_REQUEST") -1 && !memcmp(name, "_REQUEST", sizeof("_REQUEST") - 1))
143 )
144 ) {
145 if (!silent) {
146 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempted super-global (%s) variable overwrite", name);
147 }
148 return FAILURE;
149 } else if (name[0] == 'H' &&
150 (
151 (name_len == sizeof("HTTP_POST_VARS") - 1 && !memcmp(name, "HTTP_POST_VARS", sizeof("HTTP_POST_VARS") - 1)) ||
152 (name_len == sizeof("HTTP_GET_VARS") - 1 && !memcmp(name, "HTTP_GET_VARS", sizeof("HTTP_GET_VARS") - 1)) ||
153 (name_len == sizeof("HTTP_COOKIE_VARS") - 1 && !memcmp(name, "HTTP_COOKIE_VARS", sizeof("HTTP_COOKIE_VARS") - 1)) ||
154 (name_len == sizeof("HTTP_ENV_VARS") - 1 && !memcmp(name, "HTTP_ENV_VARS", sizeof("HTTP_ENV_VARS") - 1)) ||
155 (name_len == sizeof("HTTP_SERVER_VARS") - 1 && !memcmp(name, "HTTP_SERVER_VARS", sizeof("HTTP_SERVER_VARS") - 1)) ||
156 (name_len == sizeof("HTTP_SESSION_VARS") - 1 && !memcmp(name, "HTTP_SESSION_VARS", sizeof("HTTP_SESSION_VARS") - 1)) ||
157 (name_len == sizeof("HTTP_RAW_POST_DATA") - 1 && !memcmp(name, "HTTP_RAW_POST_DATA", sizeof("HTTP_RAW_POST_DATA") - 1)) ||
158 (name_len == sizeof("HTTP_POST_FILES") - 1 && !memcmp(name, "HTTP_POST_FILES", sizeof("HTTP_POST_FILES") - 1))
159 )
160 ) {
161 if (!silent) {
162 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempted long input array (%s) overwrite", name);
163 }
164 return FAILURE;
165 }
166 return SUCCESS;
167}
168/* }}} */
169
170#endif /* PHP_VAR_H */
171