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