xref: /php-src/Zend/zend_hash.h (revision 4b517a96)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Dmitry Stogov <dmitry@php.net>                              |
18    +----------------------------------------------------------------------+
19 */
20 
21 #ifndef ZEND_HASH_H
22 #define ZEND_HASH_H
23 
24 #include "zend_types.h"
25 #include "zend_gc.h"
26 #include "zend_string.h"
27 #include "zend_sort.h"
28 
29 #define HASH_KEY_IS_STRING 1
30 #define HASH_KEY_IS_LONG 2
31 #define HASH_KEY_NON_EXISTENT 3
32 
33 #define HASH_UPDATE 			(1<<0) /* Create new entry, or update the existing one. */
34 #define HASH_ADD				(1<<1) /* Create new entry, or fail if it exists. */
35 #define HASH_UPDATE_INDIRECT	(1<<2) /* If the given ht entry is an indirect zval, unwrap it before writing to it. \
36 										* When used with HASH_ADD, writing is allowed if the target zval is IS_UNDEF. */
37 #define HASH_ADD_NEW			(1<<3) /* Used when the offset is known not to exist. */
38 #define HASH_ADD_NEXT			(1<<4) /* Append to an array. (e.g. $array[] = 42;) */
39 #define HASH_LOOKUP				(1<<5) /* Look up an existing entry, or create one with a NULL value. */
40 
41 #define HASH_FLAG_CONSISTENCY      ((1<<0) | (1<<1))
42 #define HASH_FLAG_PACKED           (1<<2)
43 #define HASH_FLAG_UNINITIALIZED    (1<<3)
44 #define HASH_FLAG_STATIC_KEYS      (1<<4) /* long and interned strings */
45 #define HASH_FLAG_HAS_EMPTY_IND    (1<<5)
46 #define HASH_FLAG_ALLOW_COW_VIOLATION (1<<6)
47 
48 /* Only the low byte are real flags */
49 #define HASH_FLAG_MASK 0xff
50 
51 #define HT_FLAGS(ht) (ht)->u.flags
52 
53 #define HT_INVALIDATE(ht) do { \
54 		HT_FLAGS(ht) = HASH_FLAG_UNINITIALIZED; \
55 	} while (0)
56 
57 #define HT_IS_INITIALIZED(ht) \
58 	((HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) == 0)
59 
60 #define HT_IS_PACKED(ht) \
61 	((HT_FLAGS(ht) & HASH_FLAG_PACKED) != 0)
62 
63 #define HT_IS_WITHOUT_HOLES(ht) \
64 	((ht)->nNumUsed == (ht)->nNumOfElements)
65 
66 #define HT_HAS_STATIC_KEYS_ONLY(ht) \
67 	((HT_FLAGS(ht) & (HASH_FLAG_PACKED|HASH_FLAG_STATIC_KEYS)) != 0)
68 
69 #if ZEND_DEBUG
70 # define HT_ALLOW_COW_VIOLATION(ht) HT_FLAGS(ht) |= HASH_FLAG_ALLOW_COW_VIOLATION
71 #else
72 # define HT_ALLOW_COW_VIOLATION(ht)
73 #endif
74 
75 #define HT_ITERATORS_COUNT(ht) (ht)->u.v.nIteratorsCount
76 #define HT_ITERATORS_OVERFLOW(ht) (HT_ITERATORS_COUNT(ht) == 0xff)
77 #define HT_HAS_ITERATORS(ht) (HT_ITERATORS_COUNT(ht) != 0)
78 
79 #define HT_SET_ITERATORS_COUNT(ht, iters) \
80 	do { HT_ITERATORS_COUNT(ht) = (iters); } while (0)
81 #define HT_INC_ITERATORS_COUNT(ht) \
82 	HT_SET_ITERATORS_COUNT(ht, HT_ITERATORS_COUNT(ht) + 1)
83 #define HT_DEC_ITERATORS_COUNT(ht) \
84 	HT_SET_ITERATORS_COUNT(ht, HT_ITERATORS_COUNT(ht) - 1)
85 
86 extern ZEND_API const HashTable zend_empty_array;
87 
88 #define ZVAL_EMPTY_ARRAY(z) do {						\
89 		zval *__z = (z);								\
90 		Z_ARR_P(__z) = (zend_array*)&zend_empty_array;	\
91 		Z_TYPE_INFO_P(__z) = IS_ARRAY; \
92 	} while (0)
93 
94 
95 typedef struct _zend_hash_key {
96 	zend_ulong h;
97 	zend_string *key;
98 } zend_hash_key;
99 
100 typedef bool (*merge_checker_func_t)(HashTable *target_ht, zval *source_data, zend_hash_key *hash_key, void *pParam);
101 
102 BEGIN_EXTERN_C()
103 
104 /* startup/shutdown */
105 ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, bool persistent);
106 ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht);
107 ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht);
108 
109 #define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent) \
110 	_zend_hash_init((ht), (nSize), (pDestructor), (persistent))
111 
112 ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, bool packed);
113 ZEND_API void ZEND_FASTCALL zend_hash_real_init_packed(HashTable *ht);
114 ZEND_API void ZEND_FASTCALL zend_hash_real_init_mixed(HashTable *ht);
115 ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht);
116 ZEND_API void ZEND_FASTCALL zend_hash_to_packed(HashTable *ht);
117 ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, bool packed);
118 ZEND_API void ZEND_FASTCALL zend_hash_discard(HashTable *ht, uint32_t nNumUsed);
119 ZEND_API void ZEND_FASTCALL zend_hash_packed_grow(HashTable *ht);
120 
121 /* additions/updates/changes */
122 ZEND_API zval* ZEND_FASTCALL zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag);
123 ZEND_API zval* ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key,zval *pData);
124 ZEND_API zval* ZEND_FASTCALL zend_hash_update_ind(HashTable *ht, zend_string *key,zval *pData);
125 ZEND_API zval* ZEND_FASTCALL zend_hash_add(HashTable *ht, zend_string *key,zval *pData);
126 ZEND_API zval* ZEND_FASTCALL zend_hash_add_new(HashTable *ht, zend_string *key,zval *pData);
127 
128 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_or_update(HashTable *ht, const char *key, size_t len, zval *pData, uint32_t flag);
129 ZEND_API zval* ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData);
130 ZEND_API zval* ZEND_FASTCALL zend_hash_str_update_ind(HashTable *ht, const char *key, size_t len, zval *pData);
131 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add(HashTable *ht, const char *key, size_t len, zval *pData);
132 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *key, size_t len, zval *pData);
133 
134 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag);
135 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData);
136 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData);
137 ZEND_API zval* ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData);
138 ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData);
139 ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert_new(HashTable *ht, zval *pData);
140 
141 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h);
142 ZEND_API zval* ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key);
143 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_empty_element(HashTable *ht, const char *key, size_t len);
144 
145 ZEND_API zval* ZEND_FASTCALL zend_hash_set_bucket_key(HashTable *ht, Bucket *p, zend_string *key);
146 
147 #define ZEND_HASH_APPLY_KEEP				0
148 #define ZEND_HASH_APPLY_REMOVE				1<<0
149 #define ZEND_HASH_APPLY_STOP				1<<1
150 
151 typedef int (*apply_func_t)(zval *pDest);
152 typedef int (*apply_func_arg_t)(zval *pDest, void *argument);
153 typedef int (*apply_func_args_t)(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
154 
155 ZEND_API void ZEND_FASTCALL zend_hash_graceful_destroy(HashTable *ht);
156 ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht);
157 ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func);
158 ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *);
159 ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
160 
161 /* This function should be used with special care (in other words,
162  * it should usually not be used).  When used with the ZEND_HASH_APPLY_STOP
163  * return value, it assumes things about the order of the elements in the hash.
164  * Also, it does not provide the same kind of reentrancy protection that
165  * the standard apply functions do.
166  */
167 ZEND_API void ZEND_FASTCALL zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func);
168 
169 
170 /* Deletes */
171 ZEND_API zend_result ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key);
172 ZEND_API zend_result ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key);
173 ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *key, size_t len);
174 ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const char *key, size_t len);
175 ZEND_API zend_result ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h);
176 ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p);
177 ZEND_API void ZEND_FASTCALL zend_hash_packed_del_val(HashTable *ht, zval *zv);
178 
179 /* Data retrieval */
180 ZEND_API zval* ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key);
181 ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *key, size_t len);
182 ZEND_API zval* ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h);
183 ZEND_API zval* ZEND_FASTCALL _zend_hash_index_find(const HashTable *ht, zend_ulong h);
184 
185 /* The same as zend_hash_find(), but hash value of the key must be already calculated. */
186 ZEND_API zval* ZEND_FASTCALL zend_hash_find_known_hash(const HashTable *ht, const zend_string *key);
187 
zend_hash_find_ex(const HashTable * ht,zend_string * key,bool known_hash)188 static zend_always_inline zval *zend_hash_find_ex(const HashTable *ht, zend_string *key, bool known_hash)
189 {
190 	if (known_hash) {
191 		return zend_hash_find_known_hash(ht, key);
192 	} else {
193 		return zend_hash_find(ht, key);
194 	}
195 }
196 
197 #define ZEND_HASH_INDEX_FIND(_ht, _h, _ret, _not_found) do { \
198 		if (EXPECTED(HT_IS_PACKED(_ht))) { \
199 			if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed)) { \
200 				_ret = &_ht->arPacked[_h]; \
201 				if (UNEXPECTED(Z_TYPE_P(_ret) == IS_UNDEF)) { \
202 					goto _not_found; \
203 				} \
204 			} else { \
205 				goto _not_found; \
206 			} \
207 		} else { \
208 			_ret = _zend_hash_index_find(_ht, _h); \
209 			if (UNEXPECTED(_ret == NULL)) { \
210 				goto _not_found; \
211 			} \
212 		} \
213 	} while (0)
214 
215 
216 /* Find or add NULL, if doesn't exist */
217 ZEND_API zval* ZEND_FASTCALL zend_hash_lookup(HashTable *ht, zend_string *key);
218 ZEND_API zval* ZEND_FASTCALL zend_hash_index_lookup(HashTable *ht, zend_ulong h);
219 
220 #define ZEND_HASH_INDEX_LOOKUP(_ht, _h, _ret) do { \
221 		if (EXPECTED(HT_IS_PACKED(_ht))) { \
222 			if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed)) { \
223 				_ret = &_ht->arPacked[_h]; \
224 				if (EXPECTED(Z_TYPE_P(_ret) != IS_UNDEF)) { \
225 					break; \
226 				} \
227 			} \
228 		} \
229 		_ret = zend_hash_index_lookup(_ht, _h); \
230 	} while (0)
231 
232 /* Misc */
zend_hash_exists(const HashTable * ht,zend_string * key)233 static zend_always_inline bool zend_hash_exists(const HashTable *ht, zend_string *key)
234 {
235 	return zend_hash_find(ht, key) != NULL;
236 }
237 
zend_hash_str_exists(const HashTable * ht,const char * str,size_t len)238 static zend_always_inline bool zend_hash_str_exists(const HashTable *ht, const char *str, size_t len)
239 {
240 	return zend_hash_str_find(ht, str, len) != NULL;
241 }
242 
zend_hash_index_exists(const HashTable * ht,zend_ulong h)243 static zend_always_inline bool zend_hash_index_exists(const HashTable *ht, zend_ulong h)
244 {
245 	return zend_hash_index_find(ht, h) != NULL;
246 }
247 
248 /* traversing */
249 ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos_ex(const HashTable *ht, HashPosition pos);
250 ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos(const HashTable *ht);
251 
252 ZEND_API zend_result   ZEND_FASTCALL zend_hash_move_forward_ex(const HashTable *ht, HashPosition *pos);
253 ZEND_API zend_result   ZEND_FASTCALL zend_hash_move_backwards_ex(const HashTable *ht, HashPosition *pos);
254 ZEND_API int   ZEND_FASTCALL zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, const HashPosition *pos);
255 ZEND_API void  ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, const HashPosition *pos);
256 ZEND_API int   ZEND_FASTCALL zend_hash_get_current_key_type_ex(const HashTable *ht, const HashPosition *pos);
257 ZEND_API zval* ZEND_FASTCALL zend_hash_get_current_data_ex(const HashTable *ht, const HashPosition *pos);
258 ZEND_API void  ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(const HashTable *ht, HashPosition *pos);
259 ZEND_API void  ZEND_FASTCALL zend_hash_internal_pointer_end_ex(const HashTable *ht, HashPosition *pos);
260 
zend_hash_has_more_elements_ex(const HashTable * ht,const HashPosition * pos)261 static zend_always_inline zend_result zend_hash_has_more_elements_ex(const HashTable *ht, const HashPosition *pos) {
262 	return (zend_hash_get_current_key_type_ex(ht, pos) == HASH_KEY_NON_EXISTENT ? FAILURE : SUCCESS);
263 }
zend_hash_has_more_elements(HashTable * ht)264 static zend_always_inline zend_result zend_hash_has_more_elements(HashTable *ht) {
265 	return zend_hash_has_more_elements_ex(ht, &ht->nInternalPointer);
266 }
zend_hash_move_forward(HashTable * ht)267 static zend_always_inline zend_result zend_hash_move_forward(HashTable *ht) {
268 	return zend_hash_move_forward_ex(ht, &ht->nInternalPointer);
269 }
zend_hash_move_backwards(HashTable * ht)270 static zend_always_inline zend_result zend_hash_move_backwards(HashTable *ht) {
271 	return zend_hash_move_backwards_ex(ht, &ht->nInternalPointer);
272 }
zend_hash_get_current_key(const HashTable * ht,zend_string ** str_index,zend_ulong * num_index)273 static zend_always_inline int zend_hash_get_current_key(const HashTable *ht, zend_string **str_index, zend_ulong *num_index) {
274 	return zend_hash_get_current_key_ex(ht, str_index, num_index, &ht->nInternalPointer);
275 }
zend_hash_get_current_key_zval(const HashTable * ht,zval * key)276 static zend_always_inline void zend_hash_get_current_key_zval(const HashTable *ht, zval *key) {
277 	zend_hash_get_current_key_zval_ex(ht, key, &ht->nInternalPointer);
278 }
zend_hash_get_current_key_type(const HashTable * ht)279 static zend_always_inline int zend_hash_get_current_key_type(const HashTable *ht) {
280 	return zend_hash_get_current_key_type_ex(ht, &ht->nInternalPointer);
281 }
zend_hash_get_current_data(const HashTable * ht)282 static zend_always_inline zval* zend_hash_get_current_data(const HashTable *ht) {
283 	return zend_hash_get_current_data_ex(ht, &ht->nInternalPointer);
284 }
zend_hash_internal_pointer_reset(HashTable * ht)285 static zend_always_inline void zend_hash_internal_pointer_reset(HashTable *ht) {
286 	zend_hash_internal_pointer_reset_ex(ht, &ht->nInternalPointer);
287 }
zend_hash_internal_pointer_end(HashTable * ht)288 static zend_always_inline void zend_hash_internal_pointer_end(HashTable *ht) {
289 	zend_hash_internal_pointer_end_ex(ht, &ht->nInternalPointer);
290 }
291 
292 /* Copying, merging and sorting */
293 ZEND_API void  ZEND_FASTCALL zend_hash_copy(HashTable *target, const HashTable *source, copy_ctor_func_t pCopyConstructor);
294 ZEND_API void  ZEND_FASTCALL zend_hash_merge(HashTable *target, const HashTable *source, copy_ctor_func_t pCopyConstructor, bool overwrite);
295 ZEND_API void  ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, const HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
296 ZEND_API void  zend_hash_bucket_swap(Bucket *p, Bucket *q);
297 ZEND_API void  zend_hash_bucket_renum_swap(Bucket *p, Bucket *q);
298 ZEND_API void  zend_hash_bucket_packed_swap(Bucket *p, Bucket *q);
299 
300 typedef int (*bucket_compare_func_t)(Bucket *a, Bucket *b);
301 ZEND_API int   zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, bool ordered);
302 ZEND_API void  ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, bool renumber);
303 ZEND_API void  ZEND_FASTCALL zend_array_sort_ex(HashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, bool renumber);
304 ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag);
305 
zend_hash_sort(HashTable * ht,bucket_compare_func_t compare_func,bool renumber)306 static zend_always_inline void ZEND_FASTCALL zend_hash_sort(HashTable *ht, bucket_compare_func_t compare_func, bool renumber) {
307 	zend_hash_sort_ex(ht, zend_sort, compare_func, renumber);
308 }
309 
310 /* Use this variant over zend_hash_sort() when sorting user arrays that may
311  * trigger user code. It will ensure the user code cannot free the array during
312  * sorting. */
zend_array_sort(HashTable * ht,bucket_compare_func_t compare_func,bool renumber)313 static zend_always_inline void zend_array_sort(HashTable *ht, bucket_compare_func_t compare_func, bool renumber) {
314 	zend_array_sort_ex(ht, zend_sort, compare_func, renumber);
315 }
316 
zend_hash_num_elements(const HashTable * ht)317 static zend_always_inline uint32_t zend_hash_num_elements(const HashTable *ht) {
318 	return ht->nNumOfElements;
319 }
320 
zend_hash_next_free_element(const HashTable * ht)321 static zend_always_inline zend_long zend_hash_next_free_element(const HashTable *ht) {
322 	return ht->nNextFreeElement;
323 }
324 
325 ZEND_API void ZEND_FASTCALL zend_hash_rehash(HashTable *ht);
326 
327 #if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
328 # define zend_new_array(size) \
329 	(__builtin_constant_p(size) ? \
330 		((((uint32_t)(size)) <= HT_MIN_SIZE) ? \
331 			_zend_new_array_0() \
332 		: \
333 			_zend_new_array((size)) \
334 		) \
335 	: \
336 		_zend_new_array((size)) \
337 	)
338 #else
339 # define zend_new_array(size) \
340 	_zend_new_array(size)
341 #endif
342 
343 ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(void);
344 ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t size);
345 ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(const zval *val1, const zval *val2);
346 ZEND_API uint32_t zend_array_count(HashTable *ht);
347 ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(const HashTable *source);
348 ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht);
349 ZEND_API HashTable* zend_array_to_list(const HashTable *source);
350 ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht);
351 ZEND_API HashTable* ZEND_FASTCALL zend_symtable_to_proptable(HashTable *ht);
352 ZEND_API HashTable* ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, bool always_duplicate);
353 
354 ZEND_API bool ZEND_FASTCALL _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx);
355 
356 ZEND_API uint32_t     ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos);
357 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht);
358 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval *array);
359 ZEND_API void         ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx);
360 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(const HashTable *ht, HashPosition start);
361 ZEND_API void         ZEND_FASTCALL _zend_hash_iterators_update(const HashTable *ht, HashPosition from, HashPosition to);
362 ZEND_API void         ZEND_FASTCALL zend_hash_iterators_advance(const HashTable *ht, HashPosition step);
363 
zend_hash_iterators_update(const HashTable * ht,HashPosition from,HashPosition to)364 static zend_always_inline void zend_hash_iterators_update(const HashTable *ht, HashPosition from, HashPosition to)
365 {
366 	if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
367 		_zend_hash_iterators_update(ht, from, to);
368 	}
369 }
370 
371 /* For regular arrays (non-persistent, storing zvals). */
zend_array_release(zend_array * array)372 static zend_always_inline void zend_array_release(zend_array *array)
373 {
374 	if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
375 		if (GC_DELREF(array) == 0) {
376 			zend_array_destroy(array);
377 		}
378 	}
379 }
380 
381 /* For general hashes (possibly persistent, storing any kind of value). */
zend_hash_release(zend_array * array)382 static zend_always_inline void zend_hash_release(zend_array *array)
383 {
384 	if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
385 		if (GC_DELREF(array) == 0) {
386 			zend_hash_destroy(array);
387 			pefree(array, GC_FLAGS(array) & IS_ARRAY_PERSISTENT);
388 		}
389 	}
390 }
391 
END_EXTERN_C()392 END_EXTERN_C()
393 
394 #define ZEND_INIT_SYMTABLE(ht)								\
395 	ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
396 
397 #define ZEND_INIT_SYMTABLE_EX(ht, n, persistent)			\
398 	zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
399 
400 static zend_always_inline bool _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
401 {
402 	const char *tmp = key;
403 
404 	if (EXPECTED(*tmp > '9')) {
405 		return 0;
406 	} else if (*tmp < '0') {
407 		if (*tmp != '-') {
408 			return 0;
409 		}
410 		tmp++;
411 		if (*tmp > '9' || *tmp < '0') {
412 			return 0;
413 		}
414 	}
415 	return _zend_handle_numeric_str_ex(key, length, idx);
416 }
417 
418 #define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
419 	_zend_handle_numeric_str(key, length, &idx)
420 
421 #define ZEND_HANDLE_NUMERIC(key, idx) \
422 	ZEND_HANDLE_NUMERIC_STR(ZSTR_VAL(key), ZSTR_LEN(key), idx)
423 
424 
zend_hash_find_ind(const HashTable * ht,zend_string * key)425 static zend_always_inline zval *zend_hash_find_ind(const HashTable *ht, zend_string *key)
426 {
427 	zval *zv;
428 
429 	zv = zend_hash_find(ht, key);
430 	return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
431 		((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
432 }
433 
434 
zend_hash_find_ex_ind(const HashTable * ht,zend_string * key,bool known_hash)435 static zend_always_inline zval *zend_hash_find_ex_ind(const HashTable *ht, zend_string *key, bool known_hash)
436 {
437 	zval *zv;
438 
439 	zv = zend_hash_find_ex(ht, key, known_hash);
440 	return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
441 		((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
442 }
443 
444 
zend_hash_exists_ind(const HashTable * ht,zend_string * key)445 static zend_always_inline bool zend_hash_exists_ind(const HashTable *ht, zend_string *key)
446 {
447 	zval *zv;
448 
449 	zv = zend_hash_find(ht, key);
450 	return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
451 			Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
452 }
453 
454 
zend_hash_str_find_ind(const HashTable * ht,const char * str,size_t len)455 static zend_always_inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, size_t len)
456 {
457 	zval *zv;
458 
459 	zv = zend_hash_str_find(ht, str, len);
460 	return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
461 		((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
462 }
463 
464 
zend_hash_str_exists_ind(const HashTable * ht,const char * str,size_t len)465 static zend_always_inline bool zend_hash_str_exists_ind(const HashTable *ht, const char *str, size_t len)
466 {
467 	zval *zv;
468 
469 	zv = zend_hash_str_find(ht, str, len);
470 	return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
471 			Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
472 }
473 
zend_symtable_add_new(HashTable * ht,zend_string * key,zval * pData)474 static zend_always_inline zval *zend_symtable_add_new(HashTable *ht, zend_string *key, zval *pData)
475 {
476 	zend_ulong idx;
477 
478 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
479 		return zend_hash_index_add_new(ht, idx, pData);
480 	} else {
481 		return zend_hash_add_new(ht, key, pData);
482 	}
483 }
484 
zend_symtable_update(HashTable * ht,zend_string * key,zval * pData)485 static zend_always_inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
486 {
487 	zend_ulong idx;
488 
489 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
490 		return zend_hash_index_update(ht, idx, pData);
491 	} else {
492 		return zend_hash_update(ht, key, pData);
493 	}
494 }
495 
496 
zend_symtable_update_ind(HashTable * ht,zend_string * key,zval * pData)497 static zend_always_inline zval *zend_symtable_update_ind(HashTable *ht, zend_string *key, zval *pData)
498 {
499 	zend_ulong idx;
500 
501 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
502 		return zend_hash_index_update(ht, idx, pData);
503 	} else {
504 		return zend_hash_update_ind(ht, key, pData);
505 	}
506 }
507 
508 
zend_symtable_del(HashTable * ht,zend_string * key)509 static zend_always_inline zend_result zend_symtable_del(HashTable *ht, zend_string *key)
510 {
511 	zend_ulong idx;
512 
513 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
514 		return zend_hash_index_del(ht, idx);
515 	} else {
516 		return zend_hash_del(ht, key);
517 	}
518 }
519 
520 
zend_symtable_del_ind(HashTable * ht,zend_string * key)521 static zend_always_inline zend_result zend_symtable_del_ind(HashTable *ht, zend_string *key)
522 {
523 	zend_ulong idx;
524 
525 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
526 		return zend_hash_index_del(ht, idx);
527 	} else {
528 		return zend_hash_del_ind(ht, key);
529 	}
530 }
531 
532 
zend_symtable_find(const HashTable * ht,zend_string * key)533 static zend_always_inline zval *zend_symtable_find(const HashTable *ht, zend_string *key)
534 {
535 	zend_ulong idx;
536 
537 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
538 		return zend_hash_index_find(ht, idx);
539 	} else {
540 		return zend_hash_find(ht, key);
541 	}
542 }
543 
544 
zend_symtable_find_ind(const HashTable * ht,zend_string * key)545 static zend_always_inline zval *zend_symtable_find_ind(const HashTable *ht, zend_string *key)
546 {
547 	zend_ulong idx;
548 
549 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
550 		return zend_hash_index_find(ht, idx);
551 	} else {
552 		return zend_hash_find_ind(ht, key);
553 	}
554 }
555 
556 
zend_symtable_exists(HashTable * ht,zend_string * key)557 static zend_always_inline bool zend_symtable_exists(HashTable *ht, zend_string *key)
558 {
559 	zend_ulong idx;
560 
561 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
562 		return zend_hash_index_exists(ht, idx);
563 	} else {
564 		return zend_hash_exists(ht, key);
565 	}
566 }
567 
568 
zend_symtable_exists_ind(HashTable * ht,zend_string * key)569 static zend_always_inline bool zend_symtable_exists_ind(HashTable *ht, zend_string *key)
570 {
571 	zend_ulong idx;
572 
573 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
574 		return zend_hash_index_exists(ht, idx);
575 	} else {
576 		return zend_hash_exists_ind(ht, key);
577 	}
578 }
579 
580 
zend_symtable_str_update(HashTable * ht,const char * str,size_t len,zval * pData)581 static zend_always_inline zval *zend_symtable_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
582 {
583 	zend_ulong idx;
584 
585 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
586 		return zend_hash_index_update(ht, idx, pData);
587 	} else {
588 		return zend_hash_str_update(ht, str, len, pData);
589 	}
590 }
591 
592 
zend_symtable_str_update_ind(HashTable * ht,const char * str,size_t len,zval * pData)593 static zend_always_inline zval *zend_symtable_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
594 {
595 	zend_ulong idx;
596 
597 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
598 		return zend_hash_index_update(ht, idx, pData);
599 	} else {
600 		return zend_hash_str_update_ind(ht, str, len, pData);
601 	}
602 }
603 
604 
zend_symtable_str_del(HashTable * ht,const char * str,size_t len)605 static zend_always_inline zend_result zend_symtable_str_del(HashTable *ht, const char *str, size_t len)
606 {
607 	zend_ulong idx;
608 
609 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
610 		return zend_hash_index_del(ht, idx);
611 	} else {
612 		return zend_hash_str_del(ht, str, len);
613 	}
614 }
615 
616 
zend_symtable_str_del_ind(HashTable * ht,const char * str,size_t len)617 static zend_always_inline zend_result zend_symtable_str_del_ind(HashTable *ht, const char *str, size_t len)
618 {
619 	zend_ulong idx;
620 
621 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
622 		return zend_hash_index_del(ht, idx);
623 	} else {
624 		return zend_hash_str_del_ind(ht, str, len);
625 	}
626 }
627 
628 
zend_symtable_str_find(HashTable * ht,const char * str,size_t len)629 static zend_always_inline zval *zend_symtable_str_find(HashTable *ht, const char *str, size_t len)
630 {
631 	zend_ulong idx;
632 
633 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
634 		return zend_hash_index_find(ht, idx);
635 	} else {
636 		return zend_hash_str_find(ht, str, len);
637 	}
638 }
639 
640 
zend_symtable_str_exists(HashTable * ht,const char * str,size_t len)641 static zend_always_inline bool zend_symtable_str_exists(HashTable *ht, const char *str, size_t len)
642 {
643 	zend_ulong idx;
644 
645 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
646 		return zend_hash_index_exists(ht, idx);
647 	} else {
648 		return zend_hash_str_exists(ht, str, len);
649 	}
650 }
651 
zend_hash_add_ptr(HashTable * ht,zend_string * key,void * pData)652 static zend_always_inline void *zend_hash_add_ptr(HashTable *ht, zend_string *key, void *pData)
653 {
654 	zval tmp, *zv;
655 
656 	ZVAL_PTR(&tmp, pData);
657 	zv = zend_hash_add(ht, key, &tmp);
658 	if (zv) {
659 		ZEND_ASSERT(Z_PTR_P(zv));
660 		return Z_PTR_P(zv);
661 	} else {
662 		return NULL;
663 	}
664 }
665 
zend_hash_add_new_ptr(HashTable * ht,zend_string * key,void * pData)666 static zend_always_inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
667 {
668 	zval tmp, *zv;
669 
670 	ZVAL_PTR(&tmp, pData);
671 	zv = zend_hash_add_new(ht, key, &tmp);
672 	if (zv) {
673 		ZEND_ASSERT(Z_PTR_P(zv));
674 		return Z_PTR_P(zv);
675 	} else {
676 		return NULL;
677 	}
678 }
679 
zend_hash_str_add_ptr(HashTable * ht,const char * str,size_t len,void * pData)680 static zend_always_inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, size_t len, void *pData)
681 {
682 	zval tmp, *zv;
683 
684 	ZVAL_PTR(&tmp, pData);
685 	zv = zend_hash_str_add(ht, str, len, &tmp);
686 	if (zv) {
687 		ZEND_ASSERT(Z_PTR_P(zv));
688 		return Z_PTR_P(zv);
689 	} else {
690 		return NULL;
691 	}
692 }
693 
zend_hash_str_add_new_ptr(HashTable * ht,const char * str,size_t len,void * pData)694 static zend_always_inline void *zend_hash_str_add_new_ptr(HashTable *ht, const char *str, size_t len, void *pData)
695 {
696 	zval tmp, *zv;
697 
698 	ZVAL_PTR(&tmp, pData);
699 	zv = zend_hash_str_add_new(ht, str, len, &tmp);
700 	if (zv) {
701 		ZEND_ASSERT(Z_PTR_P(zv));
702 		return Z_PTR_P(zv);
703 	} else {
704 		return NULL;
705 	}
706 }
707 
zend_hash_update_ptr(HashTable * ht,zend_string * key,void * pData)708 static zend_always_inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
709 {
710 	zval tmp, *zv;
711 
712 	ZVAL_PTR(&tmp, pData);
713 	zv = zend_hash_update(ht, key, &tmp);
714 	ZEND_ASSERT(Z_PTR_P(zv));
715 	return Z_PTR_P(zv);
716 }
717 
zend_hash_str_update_ptr(HashTable * ht,const char * str,size_t len,void * pData)718 static zend_always_inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, size_t len, void *pData)
719 {
720 	zval tmp, *zv;
721 
722 	ZVAL_PTR(&tmp, pData);
723 	zv = zend_hash_str_update(ht, str, len, &tmp);
724 	ZEND_ASSERT(Z_PTR_P(zv));
725 	return Z_PTR_P(zv);
726 }
727 
zend_hash_add_mem(HashTable * ht,zend_string * key,void * pData,size_t size)728 static zend_always_inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
729 {
730 	zval tmp, *zv;
731 
732 	ZVAL_PTR(&tmp, NULL);
733 	if ((zv = zend_hash_add(ht, key, &tmp))) {
734 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
735 		memcpy(Z_PTR_P(zv), pData, size);
736 		return Z_PTR_P(zv);
737 	}
738 	return NULL;
739 }
740 
zend_hash_add_new_mem(HashTable * ht,zend_string * key,void * pData,size_t size)741 static zend_always_inline void *zend_hash_add_new_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
742 {
743 	zval tmp, *zv;
744 
745 	ZVAL_PTR(&tmp, NULL);
746 	if ((zv = zend_hash_add_new(ht, key, &tmp))) {
747 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
748 		memcpy(Z_PTR_P(zv), pData, size);
749 		return Z_PTR_P(zv);
750 	}
751 	return NULL;
752 }
753 
zend_hash_str_add_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)754 static zend_always_inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
755 {
756 	zval tmp, *zv;
757 
758 	ZVAL_PTR(&tmp, NULL);
759 	if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
760 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
761 		memcpy(Z_PTR_P(zv), pData, size);
762 		return Z_PTR_P(zv);
763 	}
764 	return NULL;
765 }
766 
zend_hash_str_add_new_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)767 static zend_always_inline void *zend_hash_str_add_new_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
768 {
769 	zval tmp, *zv;
770 
771 	ZVAL_PTR(&tmp, NULL);
772 	if ((zv = zend_hash_str_add_new(ht, str, len, &tmp))) {
773 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
774 		memcpy(Z_PTR_P(zv), pData, size);
775 		return Z_PTR_P(zv);
776 	}
777 	return NULL;
778 }
779 
zend_hash_update_mem(HashTable * ht,zend_string * key,void * pData,size_t size)780 static zend_always_inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
781 {
782 	void *p;
783 
784 	p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
785 	memcpy(p, pData, size);
786 	return zend_hash_update_ptr(ht, key, p);
787 }
788 
zend_hash_str_update_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)789 static zend_always_inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
790 {
791 	void *p;
792 
793 	p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
794 	memcpy(p, pData, size);
795 	return zend_hash_str_update_ptr(ht, str, len, p);
796 }
797 
zend_hash_index_add_ptr(HashTable * ht,zend_ulong h,void * pData)798 static zend_always_inline void *zend_hash_index_add_ptr(HashTable *ht, zend_ulong h, void *pData)
799 {
800 	zval tmp, *zv;
801 
802 	ZVAL_PTR(&tmp, pData);
803 	zv = zend_hash_index_add(ht, h, &tmp);
804 	return zv ? Z_PTR_P(zv) : NULL;
805 }
806 
zend_hash_index_add_new_ptr(HashTable * ht,zend_ulong h,void * pData)807 static zend_always_inline void *zend_hash_index_add_new_ptr(HashTable *ht, zend_ulong h, void *pData)
808 {
809 	zval tmp, *zv;
810 
811 	ZVAL_PTR(&tmp, pData);
812 	zv = zend_hash_index_add_new(ht, h, &tmp);
813 	return zv ? Z_PTR_P(zv) : NULL;
814 }
815 
zend_hash_index_update_ptr(HashTable * ht,zend_ulong h,void * pData)816 static zend_always_inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
817 {
818 	zval tmp, *zv;
819 
820 	ZVAL_PTR(&tmp, pData);
821 	zv = zend_hash_index_update(ht, h, &tmp);
822 	ZEND_ASSERT(Z_PTR_P(zv));
823 	return Z_PTR_P(zv);
824 }
825 
zend_hash_index_add_mem(HashTable * ht,zend_ulong h,void * pData,size_t size)826 static zend_always_inline void *zend_hash_index_add_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
827 {
828 	zval tmp, *zv;
829 
830 	ZVAL_PTR(&tmp, NULL);
831 	if ((zv = zend_hash_index_add(ht, h, &tmp))) {
832 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
833 		memcpy(Z_PTR_P(zv), pData, size);
834 		return Z_PTR_P(zv);
835 	}
836 	return NULL;
837 }
838 
zend_hash_next_index_insert_ptr(HashTable * ht,void * pData)839 static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
840 {
841 	zval tmp, *zv;
842 
843 	ZVAL_PTR(&tmp, pData);
844 	zv = zend_hash_next_index_insert(ht, &tmp);
845 	if (zv) {
846 		ZEND_ASSERT(Z_PTR_P(zv));
847 		return Z_PTR_P(zv);
848 	} else {
849 		return NULL;
850 	}
851 }
852 
zend_hash_index_update_mem(HashTable * ht,zend_ulong h,void * pData,size_t size)853 static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
854 {
855 	void *p;
856 
857 	p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
858 	memcpy(p, pData, size);
859 	return zend_hash_index_update_ptr(ht, h, p);
860 }
861 
zend_hash_next_index_insert_mem(HashTable * ht,void * pData,size_t size)862 static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
863 {
864 	zval tmp;
865 
866 	void *p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
867 	memcpy(p, pData, size);
868 	ZVAL_PTR(&tmp, p);
869 	if (!zend_hash_next_index_insert(ht, &tmp)) {
870 		pefree(p, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
871 		return NULL;
872 	}
873 	return p;
874 }
875 
zend_hash_find_ptr(const HashTable * ht,zend_string * key)876 static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
877 {
878 	zval *zv;
879 
880 	zv = zend_hash_find(ht, key);
881 	if (zv) {
882 		ZEND_ASSUME(Z_PTR_P(zv));
883 		return Z_PTR_P(zv);
884 	} else {
885 		return NULL;
886 	}
887 }
888 
zend_hash_find_ex_ptr(const HashTable * ht,zend_string * key,bool known_hash)889 static zend_always_inline void *zend_hash_find_ex_ptr(const HashTable *ht, zend_string *key, bool known_hash)
890 {
891 	zval *zv;
892 
893 	zv = zend_hash_find_ex(ht, key, known_hash);
894 	if (zv) {
895 		ZEND_ASSUME(Z_PTR_P(zv));
896 		return Z_PTR_P(zv);
897 	} else {
898 		return NULL;
899 	}
900 }
901 
zend_hash_str_find_ptr(const HashTable * ht,const char * str,size_t len)902 static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
903 {
904 	zval *zv;
905 
906 	zv = zend_hash_str_find(ht, str, len);
907 	if (zv) {
908 		ZEND_ASSUME(Z_PTR_P(zv));
909 		return Z_PTR_P(zv);
910 	} else {
911 		return NULL;
912 	}
913 }
914 
915 BEGIN_EXTERN_C()
916 
917 /* Will lowercase the str; use only if you don't need the lowercased string for
918  * anything else. If you have a lowered string, use zend_hash_str_find_ptr. */
919 ZEND_API void *zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len);
920 
921 /* Will lowercase the str; use only if you don't need the lowercased string for
922  * anything else. If you have a lowered string, use zend_hash_find_ptr. */
923 ZEND_API void *zend_hash_find_ptr_lc(const HashTable *ht, zend_string *key);
924 
END_EXTERN_C()925 END_EXTERN_C()
926 
927 static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
928 {
929 	zval *zv;
930 
931 	zv = zend_hash_index_find(ht, h);
932 	if (zv) {
933 		ZEND_ASSUME(Z_PTR_P(zv));
934 		return Z_PTR_P(zv);
935 	} else {
936 		return NULL;
937 	}
938 }
939 
zend_hash_index_find_deref(HashTable * ht,zend_ulong h)940 static zend_always_inline zval *zend_hash_index_find_deref(HashTable *ht, zend_ulong h)
941 {
942 	zval *zv = zend_hash_index_find(ht, h);
943 	if (zv) {
944 		ZVAL_DEREF(zv);
945 	}
946 	return zv;
947 }
948 
zend_hash_find_deref(HashTable * ht,zend_string * str)949 static zend_always_inline zval *zend_hash_find_deref(HashTable *ht, zend_string *str)
950 {
951 	zval *zv = zend_hash_find(ht, str);
952 	if (zv) {
953 		ZVAL_DEREF(zv);
954 	}
955 	return zv;
956 }
957 
zend_hash_str_find_deref(HashTable * ht,const char * str,size_t len)958 static zend_always_inline zval *zend_hash_str_find_deref(HashTable *ht, const char *str, size_t len)
959 {
960 	zval *zv = zend_hash_str_find(ht, str, len);
961 	if (zv) {
962 		ZVAL_DEREF(zv);
963 	}
964 	return zv;
965 }
966 
zend_symtable_str_find_ptr(HashTable * ht,const char * str,size_t len)967 static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
968 {
969 	zend_ulong idx;
970 
971 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
972 		return zend_hash_index_find_ptr(ht, idx);
973 	} else {
974 		return zend_hash_str_find_ptr(ht, str, len);
975 	}
976 }
977 
zend_hash_get_current_data_ptr_ex(HashTable * ht,HashPosition * pos)978 static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
979 {
980 	zval *zv;
981 
982 	zv = zend_hash_get_current_data_ex(ht, pos);
983 	if (zv) {
984 		ZEND_ASSUME(Z_PTR_P(zv));
985 		return Z_PTR_P(zv);
986 	} else {
987 		return NULL;
988 	}
989 }
990 
991 #define zend_hash_get_current_data_ptr(ht) \
992 	zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
993 
994 /* Common hash/packed array iterators */
995 #if 0
996 # define ZEND_HASH_ELEMENT_SIZE(__ht) \
997 	(HT_IS_PACKED(__ht) ? sizeof(zval) : sizeof(Bucket))
998 #else /* optimized version */
999 # define ZEND_HASH_ELEMENT_SIZE(__ht) \
1000 	(sizeof(zval) + (~HT_FLAGS(__ht) & HASH_FLAG_PACKED) * ((sizeof(Bucket)-sizeof(zval))/HASH_FLAG_PACKED))
1001 #endif
1002 
1003 #define ZEND_HASH_ELEMENT_EX(__ht, _idx, _size) \
1004 	((zval*)(((char*)(__ht)->arPacked) + ((_idx) * (_size))))
1005 
1006 #define ZEND_HASH_ELEMENT(__ht, _idx) \
1007 	ZEND_HASH_ELEMENT_EX(__ht, _idx, ZEND_HASH_ELEMENT_SIZE(__ht))
1008 
1009 #define ZEND_HASH_NEXT_ELEMENT(_el, _size) \
1010 	((zval*)(((char*)(_el)) + (_size)))
1011 
1012 #define ZEND_HASH_PREV_ELEMENT(_el, _size) \
1013 	((zval*)(((char*)(_el)) - (_size)))
1014 
1015 #define _ZEND_HASH_FOREACH_VAL(_ht) do { \
1016 		const HashTable *__ht = (_ht); \
1017 		uint32_t _count = __ht->nNumUsed; \
1018 		size_t _size = ZEND_HASH_ELEMENT_SIZE(__ht); \
1019 		zval *_z = __ht->arPacked; \
1020 		for (; _count > 0; _z = ZEND_HASH_NEXT_ELEMENT(_z, _size), _count--) { \
1021 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
1022 
1023 #define _ZEND_HASH_REVERSE_FOREACH_VAL(_ht) do { \
1024 		const HashTable *__ht = (_ht); \
1025 		uint32_t _idx = __ht->nNumUsed; \
1026 		size_t _size = ZEND_HASH_ELEMENT_SIZE(__ht); \
1027 		zval *_z = ZEND_HASH_ELEMENT_EX(__ht, _idx, _size); \
1028 		for (;_idx > 0; _idx--) { \
1029 			_z = ZEND_HASH_PREV_ELEMENT(_z, _size); \
1030 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
1031 
1032 #define ZEND_HASH_FOREACH_FROM(_ht, indirect, _from) do { \
1033 		const HashTable *__ht = (_ht); \
1034 		zend_ulong __h; \
1035 		zend_string *__key = NULL; \
1036 		uint32_t _idx = (_from); \
1037 		size_t _size = ZEND_HASH_ELEMENT_SIZE(__ht); \
1038 		zval *__z = ZEND_HASH_ELEMENT_EX(__ht, _idx, _size); \
1039 		uint32_t _count = __ht->nNumUsed - _idx; \
1040 		for (;_count > 0; _count--) { \
1041 			zval *_z = __z; \
1042 			if (HT_IS_PACKED(__ht)) { \
1043 				__z++; \
1044 				__h = _idx; \
1045 				_idx++; \
1046 			} else { \
1047 				Bucket *_p = (Bucket*)__z; \
1048 				__z = &(_p + 1)->val; \
1049 				__h = _p->h; \
1050 				__key = _p->key; \
1051 				if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
1052 					_z = Z_INDIRECT_P(_z); \
1053 				} \
1054 			} \
1055 			(void) __h; (void) __key; (void) _idx; \
1056 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
1057 
1058 #define ZEND_HASH_FOREACH(_ht, indirect) ZEND_HASH_FOREACH_FROM(_ht, indirect, 0)
1059 
1060 #define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
1061 		const HashTable *__ht = (_ht); \
1062 		uint32_t _idx = __ht->nNumUsed; \
1063 		zval *_z; \
1064 		zend_ulong __h; \
1065 		zend_string *__key = NULL; \
1066 		size_t _size = ZEND_HASH_ELEMENT_SIZE(__ht); \
1067 		zval *__z = ZEND_HASH_ELEMENT_EX(__ht, _idx, _size); \
1068 		for (;_idx > 0; _idx--) { \
1069 			if (HT_IS_PACKED(__ht)) { \
1070 				__z--; \
1071 				_z = __z; \
1072 				__h = _idx - 1; \
1073 			} else { \
1074 				Bucket *_p = (Bucket*)__z; \
1075 				_p--; \
1076 				__z = &_p->val; \
1077 				_z = __z; \
1078 				__h = _p->h; \
1079 				__key = _p->key; \
1080 				if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
1081 					_z = Z_INDIRECT_P(_z); \
1082 				} \
1083 			} \
1084 			(void) __h; (void) __key; (void) __z; \
1085 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
1086 
1087 #define ZEND_HASH_FOREACH_END() \
1088 		} \
1089 	} while (0)
1090 
1091 #define ZEND_HASH_FOREACH_END_DEL() \
1092 	ZEND_HASH_MAP_FOREACH_END_DEL()
1093 
1094 #define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
1095 	ZEND_HASH_MAP_FOREACH_BUCKET(ht, _bucket)
1096 
1097 #define ZEND_HASH_FOREACH_BUCKET_FROM(ht, _bucket, _from) \
1098 	ZEND_HASH_MAP_FOREACH_BUCKET_FROM(ht, _bucket, _from)
1099 
1100 #define ZEND_HASH_REVERSE_FOREACH_BUCKET(ht, _bucket) \
1101 	ZEND_HASH_MAP_REVERSE_FOREACH_BUCKET(ht, _bucket)
1102 
1103 #define ZEND_HASH_FOREACH_VAL(ht, _val) \
1104 	_ZEND_HASH_FOREACH_VAL(ht); \
1105 	_val = _z;
1106 
1107 #define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
1108 	_ZEND_HASH_REVERSE_FOREACH_VAL(ht); \
1109 	_val = _z;
1110 
1111 #define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
1112 	ZEND_HASH_FOREACH(ht, 1); \
1113 	_val = _z;
1114 
1115 #define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
1116 	ZEND_HASH_REVERSE_FOREACH(ht, 1); \
1117 	_val = _z;
1118 
1119 #define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
1120 	_ZEND_HASH_FOREACH_VAL(ht); \
1121 	_ptr = Z_PTR_P(_z);
1122 
1123 #define ZEND_HASH_FOREACH_PTR_FROM(ht, _ptr, _from) \
1124 	ZEND_HASH_FOREACH_FROM(ht, 0, _from); \
1125 	_ptr = Z_PTR_P(_z);
1126 
1127 #define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
1128 	_ZEND_HASH_REVERSE_FOREACH_VAL(ht); \
1129 	_ptr = Z_PTR_P(_z);
1130 
1131 #define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
1132 	ZEND_HASH_FOREACH(ht, 0); \
1133 	_h = __h;
1134 
1135 #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY(ht, _h) \
1136 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1137 	_h = __h;
1138 
1139 #define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
1140 	ZEND_HASH_FOREACH(ht, 0); \
1141 	_key = __key;
1142 
1143 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY(ht, _key) \
1144 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1145 	_key = __key;
1146 
1147 #define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
1148 	ZEND_HASH_FOREACH(ht, 0); \
1149 	_h = __h; \
1150 	_key = __key;
1151 
1152 #define ZEND_HASH_REVERSE_FOREACH_KEY(ht, _h, _key) \
1153 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1154 	_h = __h; \
1155 	_key = __key;
1156 
1157 #define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
1158 	ZEND_HASH_FOREACH(ht, 0); \
1159 	_h = __h; \
1160 	_val = _z;
1161 
1162 #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
1163 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1164 	_h = __h; \
1165 	_val = _z;
1166 
1167 #define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
1168 	ZEND_HASH_FOREACH(ht, 0); \
1169 	_key = __key; \
1170 	_val = _z;
1171 
1172 #define ZEND_HASH_FOREACH_STR_KEY_VAL_FROM(ht, _key, _val, _from) \
1173 	ZEND_HASH_FOREACH_FROM(ht, 0, _from); \
1174 	_key = __key; \
1175 	_val = _z;
1176 
1177 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(ht, _key, _val) \
1178 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1179 	_key = __key; \
1180 	_val = _z;
1181 
1182 #define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
1183 	ZEND_HASH_FOREACH(ht, 0); \
1184 	_h = __h; \
1185 	_key = __key; \
1186 	_val = _z;
1187 
1188 #define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
1189 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1190 	_h = __h; \
1191 	_key = __key; \
1192 	_val = _z;
1193 
1194 #define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
1195 	ZEND_HASH_FOREACH(ht, 1); \
1196 	_key = __key; \
1197 	_val = _z;
1198 
1199 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
1200 	ZEND_HASH_REVERSE_FOREACH(ht, 1); \
1201 	_key = __key; \
1202 	_val = _z;
1203 
1204 #define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
1205 	ZEND_HASH_FOREACH(ht, 1); \
1206 	_h = __h; \
1207 	_key = __key; \
1208 	_val = _z;
1209 
1210 #define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
1211 	ZEND_HASH_REVERSE_FOREACH(ht, 1); \
1212 	_h = __h; \
1213 	_key = __key; \
1214 	_val = _z;
1215 
1216 #define ZEND_HASH_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
1217 	ZEND_HASH_FOREACH(ht, 0); \
1218 	_h = __h; \
1219 	_ptr = Z_PTR_P(_z);
1220 
1221 #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
1222 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1223 	_h = __h; \
1224 	_ptr = Z_PTR_P(_z);
1225 
1226 #define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
1227 	ZEND_HASH_FOREACH(ht, 0); \
1228 	_key = __key; \
1229 	_ptr = Z_PTR_P(_z);
1230 
1231 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
1232 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1233 	_key = __key; \
1234 	_ptr = Z_PTR_P(_z);
1235 
1236 #define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
1237 	ZEND_HASH_FOREACH(ht, 0); \
1238 	_h = __h; \
1239 	_key = __key; \
1240 	_ptr = Z_PTR_P(_z);
1241 
1242 #define ZEND_HASH_REVERSE_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
1243 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1244 	_h = __h; \
1245 	_key = __key; \
1246 	_ptr = Z_PTR_P(_z);
1247 
1248 /* Hash array iterators */
1249 #define ZEND_HASH_MAP_FOREACH_FROM(_ht, indirect, _from) do { \
1250 		const HashTable *__ht = (_ht); \
1251 		Bucket *_p = __ht->arData + (_from); \
1252 		const Bucket *_end = __ht->arData + __ht->nNumUsed; \
1253 		ZEND_ASSERT(!HT_IS_PACKED(__ht)); \
1254 		for (; _p != _end; _p++) { \
1255 			zval *_z = &_p->val; \
1256 			if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
1257 				_z = Z_INDIRECT_P(_z); \
1258 			} \
1259 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
1260 
1261 #define ZEND_HASH_MAP_FOREACH(_ht, indirect) ZEND_HASH_MAP_FOREACH_FROM(_ht, indirect, 0)
1262 
1263 #define ZEND_HASH_MAP_REVERSE_FOREACH(_ht, indirect) do { \
1264 		/* const */ HashTable *__ht = (_ht); \
1265 		uint32_t _idx = __ht->nNumUsed; \
1266 		Bucket *_p = __ht->arData + _idx; \
1267 		zval *_z; \
1268 		ZEND_ASSERT(!HT_IS_PACKED(__ht)); \
1269 		for (_idx = __ht->nNumUsed; _idx > 0; _idx--) { \
1270 			_p--; \
1271 			_z = &_p->val; \
1272 			if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
1273 				_z = Z_INDIRECT_P(_z); \
1274 			} \
1275 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
1276 
1277 #define ZEND_HASH_MAP_FOREACH_END_DEL() \
1278 			ZEND_ASSERT(!HT_IS_PACKED(__ht)); \
1279 			__ht->nNumOfElements--; \
1280 			do { \
1281 				uint32_t j = HT_IDX_TO_HASH(_idx - 1); \
1282 				uint32_t nIndex = _p->h | __ht->nTableMask; \
1283 				uint32_t i = HT_HASH(__ht, nIndex); \
1284 				if (UNEXPECTED(j != i)) { \
1285 					Bucket *prev = HT_HASH_TO_BUCKET(__ht, i); \
1286 					while (Z_NEXT(prev->val) != j) { \
1287 						i = Z_NEXT(prev->val); \
1288 						prev = HT_HASH_TO_BUCKET(__ht, i); \
1289 					} \
1290 					Z_NEXT(prev->val) = Z_NEXT(_p->val); \
1291 				} else { \
1292 					HT_HASH(__ht, nIndex) = Z_NEXT(_p->val); \
1293 				} \
1294 			} while (0); \
1295 		} \
1296 		__ht->nNumUsed = _idx; \
1297 	} while (0)
1298 
1299 #define ZEND_HASH_MAP_FOREACH_BUCKET(ht, _bucket) \
1300 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1301 	_bucket = _p;
1302 
1303 #define ZEND_HASH_MAP_FOREACH_BUCKET_FROM(ht, _bucket, _from) \
1304 	ZEND_HASH_MAP_FOREACH_FROM(ht, 0, _from); \
1305 	_bucket = _p;
1306 
1307 #define ZEND_HASH_MAP_REVERSE_FOREACH_BUCKET(ht, _bucket) \
1308 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1309 	_bucket = _p;
1310 
1311 #define ZEND_HASH_MAP_FOREACH_VAL(ht, _val) \
1312 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1313 	_val = _z;
1314 
1315 #define ZEND_HASH_MAP_REVERSE_FOREACH_VAL(ht, _val) \
1316 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1317 	_val = _z;
1318 
1319 #define ZEND_HASH_MAP_FOREACH_VAL_IND(ht, _val) \
1320 	ZEND_HASH_MAP_FOREACH(ht, 1); \
1321 	_val = _z;
1322 
1323 #define ZEND_HASH_MAP_REVERSE_FOREACH_VAL_IND(ht, _val) \
1324 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 1); \
1325 	_val = _z;
1326 
1327 #define ZEND_HASH_MAP_FOREACH_PTR(ht, _ptr) \
1328 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1329 	_ptr = Z_PTR_P(_z);
1330 
1331 #define ZEND_HASH_MAP_FOREACH_PTR_FROM(ht, _ptr, _from) \
1332 	ZEND_HASH_MAP_FOREACH_FROM(ht, 0, _from); \
1333 	_ptr = Z_PTR_P(_z);
1334 
1335 #define ZEND_HASH_MAP_REVERSE_FOREACH_PTR(ht, _ptr) \
1336 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1337 	_ptr = Z_PTR_P(_z);
1338 
1339 #define ZEND_HASH_MAP_FOREACH_NUM_KEY(ht, _h) \
1340 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1341 	_h = _p->h;
1342 
1343 #define ZEND_HASH_MAP_REVERSE_FOREACH_NUM_KEY(ht, _h) \
1344 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1345 	_h = _p->h;
1346 
1347 #define ZEND_HASH_MAP_FOREACH_STR_KEY(ht, _key) \
1348 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1349 	_key = _p->key;
1350 
1351 #define ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY(ht, _key) \
1352 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1353 	_key = _p->key;
1354 
1355 #define ZEND_HASH_MAP_FOREACH_KEY(ht, _h, _key) \
1356 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1357 	_h = _p->h; \
1358 	_key = _p->key;
1359 
1360 #define ZEND_HASH_MAP_REVERSE_FOREACH_KEY(ht, _h, _key) \
1361 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1362 	_h = _p->h; \
1363 	_key = _p->key;
1364 
1365 #define ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
1366 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1367 	_h = _p->h; \
1368 	_val = _z;
1369 
1370 #define ZEND_HASH_MAP_REVERSE_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
1371 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1372 	_h = _p->h; \
1373 	_val = _z;
1374 
1375 #define ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(ht, _key, _val) \
1376 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1377 	_key = _p->key; \
1378 	_val = _z;
1379 
1380 #define ZEND_HASH_MAP_FOREACH_STR_KEY_VAL_FROM(ht, _key, _val, _from) \
1381 	ZEND_HASH_MAP_FOREACH_FROM(ht, 0, _from); \
1382 	_key = _p->key; \
1383 	_val = _z;
1384 
1385 #define ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_VAL(ht, _key, _val) \
1386 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1387 	_key = _p->key; \
1388 	_val = _z;
1389 
1390 #define ZEND_HASH_MAP_FOREACH_KEY_VAL(ht, _h, _key, _val) \
1391 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1392 	_h = _p->h; \
1393 	_key = _p->key; \
1394 	_val = _z;
1395 
1396 #define ZEND_HASH_MAP_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
1397 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1398 	_h = _p->h; \
1399 	_key = _p->key; \
1400 	_val = _z;
1401 
1402 #define ZEND_HASH_MAP_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
1403 	ZEND_HASH_MAP_FOREACH(ht, 1); \
1404 	_key = _p->key; \
1405 	_val = _z;
1406 
1407 #define ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
1408 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 1); \
1409 	_key = _p->key; \
1410 	_val = _z;
1411 
1412 #define ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
1413 	ZEND_HASH_MAP_FOREACH(ht, 1); \
1414 	_h = _p->h; \
1415 	_key = _p->key; \
1416 	_val = _z;
1417 
1418 #define ZEND_HASH_MAP_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
1419 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 1); \
1420 	_h = _p->h; \
1421 	_key = _p->key; \
1422 	_val = _z;
1423 
1424 #define ZEND_HASH_MAP_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
1425 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1426 	_h = _p->h; \
1427 	_ptr = Z_PTR_P(_z);
1428 
1429 #define ZEND_HASH_MAP_REVERSE_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
1430 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1431 	_h = _p->h; \
1432 	_ptr = Z_PTR_P(_z);
1433 
1434 #define ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
1435 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1436 	_key = _p->key; \
1437 	_ptr = Z_PTR_P(_z);
1438 
1439 #define ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
1440 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1441 	_key = _p->key; \
1442 	_ptr = Z_PTR_P(_z);
1443 
1444 #define ZEND_HASH_MAP_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
1445 	ZEND_HASH_MAP_FOREACH(ht, 0); \
1446 	_h = _p->h; \
1447 	_key = _p->key; \
1448 	_ptr = Z_PTR_P(_z);
1449 
1450 #define ZEND_HASH_MAP_REVERSE_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
1451 	ZEND_HASH_MAP_REVERSE_FOREACH(ht, 0); \
1452 	_h = _p->h; \
1453 	_key = _p->key; \
1454 	_ptr = Z_PTR_P(_z);
1455 
1456 /* Packed array iterators */
1457 #define ZEND_HASH_PACKED_FOREACH_FROM(_ht, _from) do { \
1458 		const HashTable *__ht = (_ht); \
1459 		zend_ulong _idx = (_from); \
1460 		zval *_z = __ht->arPacked + (_from); \
1461 		zval *_end = __ht->arPacked + __ht->nNumUsed; \
1462 		ZEND_ASSERT(HT_IS_PACKED(__ht)); \
1463 		for (;_z != _end; _z++, _idx++) { \
1464 			(void) _idx; \
1465 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
1466 
1467 #define ZEND_HASH_PACKED_FOREACH(_ht) ZEND_HASH_PACKED_FOREACH_FROM(_ht, 0)
1468 
1469 #define ZEND_HASH_PACKED_REVERSE_FOREACH(_ht) do { \
1470 		const HashTable *__ht = (_ht); \
1471 		zend_ulong _idx = __ht->nNumUsed; \
1472 		zval *_z = __ht->arPacked + _idx; \
1473 		ZEND_ASSERT(HT_IS_PACKED(__ht)); \
1474 		while (_idx > 0) { \
1475 			_z--; \
1476 			_idx--; \
1477 			(void) _idx; \
1478 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
1479 
1480 #define ZEND_HASH_PACKED_FOREACH_VAL(ht, _val) \
1481 	ZEND_HASH_PACKED_FOREACH(ht); \
1482 	_val = _z;
1483 
1484 #define ZEND_HASH_PACKED_REVERSE_FOREACH_VAL(ht, _val) \
1485 	ZEND_HASH_PACKED_REVERSE_FOREACH(ht); \
1486 	_val = _z;
1487 
1488 #define ZEND_HASH_PACKED_FOREACH_PTR(ht, _ptr) \
1489 	ZEND_HASH_PACKED_FOREACH(ht); \
1490 	_ptr = Z_PTR_P(_z);
1491 
1492 #define ZEND_HASH_PACKED_REVERSE_FOREACH_PTR(ht, _ptr) \
1493 	ZEND_HASH_PACKED_REVERSE_FOREACH(ht); \
1494 	_ptr = Z_PTR_P(_z);
1495 
1496 #define ZEND_HASH_PACKED_FOREACH_KEY(ht, _h) \
1497 	ZEND_HASH_PACKED_FOREACH(ht); \
1498 	_h = _idx;
1499 
1500 #define ZEND_HASH_PACKED_REVERSE_FOREACH_KEY(ht, _h) \
1501 	ZEND_HASH_PACKED_REVERSE_FOREACH(ht); \
1502 	_h = _idx;
1503 
1504 #define ZEND_HASH_PACKED_FOREACH_KEY_VAL(ht, _h, _val) \
1505 	ZEND_HASH_PACKED_FOREACH(ht); \
1506 	_h = _idx; \
1507 	_val = _z;
1508 
1509 #define ZEND_HASH_PACKED_REVERSE_FOREACH_KEY_VAL(ht, _h, _val) \
1510 	ZEND_HASH_PACKED_REVERSE_FOREACH(ht); \
1511 	_h = _idx; \
1512 	_val = _z;
1513 
1514 #define ZEND_HASH_PACKED_FOREACH_KEY_PTR(ht, _h, _ptr) \
1515 	ZEND_HASH_PACKED_FOREACH(ht); \
1516 	_h = _idx; \
1517 	_ptr = Z_PTR_P(_z);
1518 
1519 #define ZEND_HASH_PACKED_REVERSE_FOREACH_KEY_PTR(ht, _h, _ptr) \
1520 	ZEND_HASH_PACKED_REVERSE_FOREACH(ht); \
1521 	_h = _idx; \
1522 	_ptr = Z_PTR_P(_z);
1523 
1524 /* The following macros are useful to insert a sequence of new elements
1525  * of packed array. They may be used instead of series of
1526  * zend_hash_next_index_insert_new()
1527  * (HashTable must have enough free buckets).
1528  */
1529 #define ZEND_HASH_FILL_PACKED(ht) do { \
1530 		HashTable *__fill_ht = (ht); \
1531 		zval *__fill_val = __fill_ht->arPacked + __fill_ht->nNumUsed; \
1532 		uint32_t __fill_idx = __fill_ht->nNumUsed; \
1533 		ZEND_ASSERT(HT_IS_PACKED(__fill_ht));
1534 
1535 #define ZEND_HASH_FILL_GROW() do { \
1536 		if (UNEXPECTED(__fill_idx >= __fill_ht->nTableSize)) { \
1537 			__fill_ht->nNumOfElements += __fill_idx - __fill_ht->nNumUsed; \
1538 			__fill_ht->nNumUsed = __fill_idx; \
1539 			__fill_ht->nNextFreeElement = __fill_idx; \
1540 			zend_hash_packed_grow(__fill_ht); \
1541 			__fill_val = __fill_ht->arPacked + __fill_idx; \
1542 		} \
1543 	} while (0);
1544 
1545 #define ZEND_HASH_FILL_SET(_val) \
1546 		ZVAL_COPY_VALUE(__fill_val, _val)
1547 
1548 #define ZEND_HASH_FILL_SET_NULL() \
1549 		ZVAL_NULL(__fill_val)
1550 
1551 #define ZEND_HASH_FILL_SET_LONG(_val) \
1552 		ZVAL_LONG(__fill_val, _val)
1553 
1554 #define ZEND_HASH_FILL_SET_DOUBLE(_val) \
1555 		ZVAL_DOUBLE(__fill_val, _val)
1556 
1557 #define ZEND_HASH_FILL_SET_STR(_val) \
1558 		ZVAL_STR(__fill_val, _val)
1559 
1560 #define ZEND_HASH_FILL_SET_STR_COPY(_val) \
1561 		ZVAL_STR_COPY(__fill_val, _val)
1562 
1563 #define ZEND_HASH_FILL_SET_INTERNED_STR(_val) \
1564 		ZVAL_INTERNED_STR(__fill_val, _val)
1565 
1566 #define ZEND_HASH_FILL_NEXT() do {\
1567 		__fill_val++; \
1568 		__fill_idx++; \
1569 	} while (0)
1570 
1571 #define ZEND_HASH_FILL_ADD(_val) do { \
1572 		ZEND_HASH_FILL_SET(_val); \
1573 		ZEND_HASH_FILL_NEXT(); \
1574 	} while (0)
1575 
1576 #define ZEND_HASH_FILL_FINISH() do { \
1577 		__fill_ht->nNumOfElements += __fill_idx - __fill_ht->nNumUsed; \
1578 		__fill_ht->nNumUsed = __fill_idx; \
1579 		__fill_ht->nNextFreeElement = __fill_idx; \
1580 		__fill_ht->nInternalPointer = 0; \
1581 	} while (0)
1582 
1583 #define ZEND_HASH_FILL_END() \
1584 		ZEND_HASH_FILL_FINISH(); \
1585 	} while (0)
1586 
1587 /* Check if an array is a list */
zend_array_is_list(const zend_array * array)1588 static zend_always_inline bool zend_array_is_list(const zend_array *array)
1589 {
1590 	zend_ulong expected_idx = 0;
1591 	zend_ulong num_idx;
1592 	zend_string* str_idx;
1593 	/* Empty arrays are lists */
1594 	if (zend_hash_num_elements(array) == 0) {
1595 		return 1;
1596 	}
1597 
1598 	/* Packed arrays are lists */
1599 	if (HT_IS_PACKED(array)) {
1600 		if (HT_IS_WITHOUT_HOLES(array)) {
1601 			return 1;
1602 		}
1603 		/* Check if the list could theoretically be repacked */
1604 		ZEND_HASH_PACKED_FOREACH_KEY(array, num_idx) {
1605 			if (num_idx != expected_idx++) {
1606 				return 0;
1607 			}
1608 		} ZEND_HASH_FOREACH_END();
1609 	} else {
1610 		/* Check if the list could theoretically be repacked */
1611 		ZEND_HASH_MAP_FOREACH_KEY(array, num_idx, str_idx) {
1612 			if (str_idx != NULL || num_idx != expected_idx++) {
1613 				return 0;
1614 			}
1615 		} ZEND_HASH_FOREACH_END();
1616 	}
1617 
1618 	return 1;
1619 }
1620 
1621 
_zend_hash_append_ex(HashTable * ht,zend_string * key,zval * zv,bool interned)1622 static zend_always_inline zval *_zend_hash_append_ex(HashTable *ht, zend_string *key, zval *zv, bool interned)
1623 {
1624 	uint32_t idx = ht->nNumUsed++;
1625 	uint32_t nIndex;
1626 	Bucket *p = ht->arData + idx;
1627 
1628 	ZVAL_COPY_VALUE(&p->val, zv);
1629 	if (!interned && !ZSTR_IS_INTERNED(key)) {
1630 		HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1631 		zend_string_addref(key);
1632 		zend_string_hash_val(key);
1633 	}
1634 	p->key = key;
1635 	p->h = ZSTR_H(key);
1636 	nIndex = (uint32_t)p->h | ht->nTableMask;
1637 	Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1638 	HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1639 	ht->nNumOfElements++;
1640 	return &p->val;
1641 }
1642 
_zend_hash_append(HashTable * ht,zend_string * key,zval * zv)1643 static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *key, zval *zv)
1644 {
1645 	return _zend_hash_append_ex(ht, key, zv, 0);
1646 }
1647 
_zend_hash_append_ptr_ex(HashTable * ht,zend_string * key,void * ptr,bool interned)1648 static zend_always_inline zval *_zend_hash_append_ptr_ex(HashTable *ht, zend_string *key, void *ptr, bool interned)
1649 {
1650 	uint32_t idx = ht->nNumUsed++;
1651 	uint32_t nIndex;
1652 	Bucket *p = ht->arData + idx;
1653 
1654 	ZVAL_PTR(&p->val, ptr);
1655 	if (!interned && !ZSTR_IS_INTERNED(key)) {
1656 		HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1657 		zend_string_addref(key);
1658 		zend_string_hash_val(key);
1659 	}
1660 	p->key = key;
1661 	p->h = ZSTR_H(key);
1662 	nIndex = (uint32_t)p->h | ht->nTableMask;
1663 	Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1664 	HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1665 	ht->nNumOfElements++;
1666 	return &p->val;
1667 }
1668 
_zend_hash_append_ptr(HashTable * ht,zend_string * key,void * ptr)1669 static zend_always_inline zval *_zend_hash_append_ptr(HashTable *ht, zend_string *key, void *ptr)
1670 {
1671 	return _zend_hash_append_ptr_ex(ht, key, ptr, 0);
1672 }
1673 
_zend_hash_append_ind(HashTable * ht,zend_string * key,zval * ptr)1674 static zend_always_inline void _zend_hash_append_ind(HashTable *ht, zend_string *key, zval *ptr)
1675 {
1676 	uint32_t idx = ht->nNumUsed++;
1677 	uint32_t nIndex;
1678 	Bucket *p = ht->arData + idx;
1679 
1680 	ZVAL_INDIRECT(&p->val, ptr);
1681 	if (!ZSTR_IS_INTERNED(key)) {
1682 		HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1683 		zend_string_addref(key);
1684 		zend_string_hash_val(key);
1685 	}
1686 	p->key = key;
1687 	p->h = ZSTR_H(key);
1688 	nIndex = (uint32_t)p->h | ht->nTableMask;
1689 	Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1690 	HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1691 	ht->nNumOfElements++;
1692 }
1693 
1694 #endif							/* ZEND_HASH_H */
1695