xref: /PHP-8.0/Zend/zend_hash.h (revision 87104292)
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.h"
25 
26 #define HASH_KEY_IS_STRING 1
27 #define HASH_KEY_IS_LONG 2
28 #define HASH_KEY_NON_EXISTENT 3
29 
30 #define HASH_UPDATE 			(1<<0)
31 #define HASH_ADD				(1<<1)
32 #define HASH_UPDATE_INDIRECT	(1<<2)
33 #define HASH_ADD_NEW			(1<<3)
34 #define HASH_ADD_NEXT			(1<<4)
35 
36 #define HASH_FLAG_CONSISTENCY      ((1<<0) | (1<<1))
37 #define HASH_FLAG_PACKED           (1<<2)
38 #define HASH_FLAG_UNINITIALIZED    (1<<3)
39 #define HASH_FLAG_STATIC_KEYS      (1<<4) /* long and interned strings */
40 #define HASH_FLAG_HAS_EMPTY_IND    (1<<5)
41 #define HASH_FLAG_ALLOW_COW_VIOLATION (1<<6)
42 
43 /* Only the low byte are real flags */
44 #define HASH_FLAG_MASK 0xff
45 
46 #define HT_FLAGS(ht) (ht)->u.flags
47 
48 #define HT_INVALIDATE(ht) do { \
49 		HT_FLAGS(ht) = HASH_FLAG_UNINITIALIZED; \
50 	} while (0)
51 
52 #define HT_IS_INITIALIZED(ht) \
53 	((HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) == 0)
54 
55 #define HT_IS_PACKED(ht) \
56 	((HT_FLAGS(ht) & HASH_FLAG_PACKED) != 0)
57 
58 #define HT_IS_WITHOUT_HOLES(ht) \
59 	((ht)->nNumUsed == (ht)->nNumOfElements)
60 
61 #define HT_HAS_STATIC_KEYS_ONLY(ht) \
62 	((HT_FLAGS(ht) & (HASH_FLAG_PACKED|HASH_FLAG_STATIC_KEYS)) != 0)
63 
64 #if ZEND_DEBUG
65 # define HT_ALLOW_COW_VIOLATION(ht) HT_FLAGS(ht) |= HASH_FLAG_ALLOW_COW_VIOLATION
66 #else
67 # define HT_ALLOW_COW_VIOLATION(ht)
68 #endif
69 
70 #define HT_ITERATORS_COUNT(ht) (ht)->u.v.nIteratorsCount
71 #define HT_ITERATORS_OVERFLOW(ht) (HT_ITERATORS_COUNT(ht) == 0xff)
72 #define HT_HAS_ITERATORS(ht) (HT_ITERATORS_COUNT(ht) != 0)
73 
74 #define HT_SET_ITERATORS_COUNT(ht, iters) \
75 	do { HT_ITERATORS_COUNT(ht) = (iters); } while (0)
76 #define HT_INC_ITERATORS_COUNT(ht) \
77 	HT_SET_ITERATORS_COUNT(ht, HT_ITERATORS_COUNT(ht) + 1)
78 #define HT_DEC_ITERATORS_COUNT(ht) \
79 	HT_SET_ITERATORS_COUNT(ht, HT_ITERATORS_COUNT(ht) - 1)
80 
81 extern ZEND_API const HashTable zend_empty_array;
82 
83 #define ZVAL_EMPTY_ARRAY(z) do {						\
84 		zval *__z = (z);								\
85 		Z_ARR_P(__z) = (zend_array*)&zend_empty_array;	\
86 		Z_TYPE_INFO_P(__z) = IS_ARRAY; \
87 	} while (0)
88 
89 
90 typedef struct _zend_hash_key {
91 	zend_ulong h;
92 	zend_string *key;
93 } zend_hash_key;
94 
95 typedef zend_bool (*merge_checker_func_t)(HashTable *target_ht, zval *source_data, zend_hash_key *hash_key, void *pParam);
96 
97 BEGIN_EXTERN_C()
98 
99 /* startup/shutdown */
100 ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent);
101 ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht);
102 ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht);
103 
104 #define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent) \
105 	_zend_hash_init((ht), (nSize), (pDestructor), (persistent))
106 
107 ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, zend_bool packed);
108 ZEND_API void ZEND_FASTCALL zend_hash_real_init_packed(HashTable *ht);
109 ZEND_API void ZEND_FASTCALL zend_hash_real_init_mixed(HashTable *ht);
110 ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht);
111 ZEND_API void ZEND_FASTCALL zend_hash_to_packed(HashTable *ht);
112 ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, zend_bool packed);
113 ZEND_API void ZEND_FASTCALL zend_hash_discard(HashTable *ht, uint32_t nNumUsed);
114 
115 /* additions/updates/changes */
116 ZEND_API zval* ZEND_FASTCALL zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag);
117 ZEND_API zval* ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key,zval *pData);
118 ZEND_API zval* ZEND_FASTCALL zend_hash_update_ind(HashTable *ht, zend_string *key,zval *pData);
119 ZEND_API zval* ZEND_FASTCALL zend_hash_add(HashTable *ht, zend_string *key,zval *pData);
120 ZEND_API zval* ZEND_FASTCALL zend_hash_add_new(HashTable *ht, zend_string *key,zval *pData);
121 
122 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_or_update(HashTable *ht, const char *key, size_t len, zval *pData, uint32_t flag);
123 ZEND_API zval* ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData);
124 ZEND_API zval* ZEND_FASTCALL zend_hash_str_update_ind(HashTable *ht, const char *key, size_t len, zval *pData);
125 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add(HashTable *ht, const char *key, size_t len, zval *pData);
126 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *key, size_t len, zval *pData);
127 
128 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag);
129 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData);
130 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData);
131 ZEND_API zval* ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData);
132 ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData);
133 ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert_new(HashTable *ht, zval *pData);
134 
135 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h);
136 ZEND_API zval* ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key);
137 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_empty_element(HashTable *ht, const char *key, size_t len);
138 
139 ZEND_API zval* ZEND_FASTCALL zend_hash_set_bucket_key(HashTable *ht, Bucket *p, zend_string *key);
140 
141 #define ZEND_HASH_APPLY_KEEP				0
142 #define ZEND_HASH_APPLY_REMOVE				1<<0
143 #define ZEND_HASH_APPLY_STOP				1<<1
144 
145 typedef int (*apply_func_t)(zval *pDest);
146 typedef int (*apply_func_arg_t)(zval *pDest, void *argument);
147 typedef int (*apply_func_args_t)(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
148 
149 ZEND_API void ZEND_FASTCALL zend_hash_graceful_destroy(HashTable *ht);
150 ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht);
151 ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func);
152 ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *);
153 ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
154 
155 /* This function should be used with special care (in other words,
156  * it should usually not be used).  When used with the ZEND_HASH_APPLY_STOP
157  * return value, it assumes things about the order of the elements in the hash.
158  * Also, it does not provide the same kind of reentrancy protection that
159  * the standard apply functions do.
160  */
161 ZEND_API void ZEND_FASTCALL zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func);
162 
163 
164 /* Deletes */
165 ZEND_API zend_result ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key);
166 ZEND_API zend_result ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key);
167 ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *key, size_t len);
168 ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const char *key, size_t len);
169 ZEND_API zend_result ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h);
170 ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p);
171 
172 /* Data retrieval */
173 ZEND_API zval* ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key);
174 ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *key, size_t len);
175 ZEND_API zval* ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h);
176 ZEND_API zval* ZEND_FASTCALL _zend_hash_index_find(const HashTable *ht, zend_ulong h);
177 
178 /* The same as zend_hash_find(), but hash value of the key must be already calculated */
179 ZEND_API zval* ZEND_FASTCALL _zend_hash_find_known_hash(const HashTable *ht, zend_string *key);
180 
zend_hash_find_ex(const HashTable * ht,zend_string * key,zend_bool known_hash)181 static zend_always_inline zval *zend_hash_find_ex(const HashTable *ht, zend_string *key, zend_bool known_hash)
182 {
183 	if (known_hash) {
184 		return _zend_hash_find_known_hash(ht, key);
185 	} else {
186 		return zend_hash_find(ht, key);
187 	}
188 }
189 
190 #define ZEND_HASH_INDEX_FIND(_ht, _h, _ret, _not_found) do { \
191 		if (EXPECTED(HT_FLAGS(_ht) & HASH_FLAG_PACKED)) { \
192 			if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed)) { \
193 				_ret = &_ht->arData[_h].val; \
194 				if (UNEXPECTED(Z_TYPE_P(_ret) == IS_UNDEF)) { \
195 					goto _not_found; \
196 				} \
197 			} else { \
198 				goto _not_found; \
199 			} \
200 		} else { \
201 			_ret = _zend_hash_index_find(_ht, _h); \
202 			if (UNEXPECTED(_ret == NULL)) { \
203 				goto _not_found; \
204 			} \
205 		} \
206 	} while (0)
207 
208 
209 /* Misc */
zend_hash_exists(const HashTable * ht,zend_string * key)210 static zend_always_inline zend_bool zend_hash_exists(const HashTable *ht, zend_string *key)
211 {
212 	return zend_hash_find(ht, key) != NULL;
213 }
214 
zend_hash_str_exists(const HashTable * ht,const char * str,size_t len)215 static zend_always_inline zend_bool zend_hash_str_exists(const HashTable *ht, const char *str, size_t len)
216 {
217 	return zend_hash_str_find(ht, str, len) != NULL;
218 }
219 
zend_hash_index_exists(const HashTable * ht,zend_ulong h)220 static zend_always_inline zend_bool zend_hash_index_exists(const HashTable *ht, zend_ulong h)
221 {
222 	return zend_hash_index_find(ht, h) != NULL;
223 }
224 
225 /* traversing */
226 ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos(const HashTable *ht);
227 
228 #define zend_hash_has_more_elements_ex(ht, pos) \
229 	(zend_hash_get_current_key_type_ex(ht, pos) == HASH_KEY_NON_EXISTENT ? FAILURE : SUCCESS)
230 ZEND_API zend_result   ZEND_FASTCALL zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
231 ZEND_API zend_result   ZEND_FASTCALL zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
232 ZEND_API int   ZEND_FASTCALL zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, HashPosition *pos);
233 ZEND_API void  ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos);
234 ZEND_API int   ZEND_FASTCALL zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
235 ZEND_API zval* ZEND_FASTCALL zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos);
236 ZEND_API void  ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
237 ZEND_API void  ZEND_FASTCALL zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
238 
239 #define zend_hash_has_more_elements(ht) \
240 	zend_hash_has_more_elements_ex(ht, &(ht)->nInternalPointer)
241 #define zend_hash_move_forward(ht) \
242 	zend_hash_move_forward_ex(ht, &(ht)->nInternalPointer)
243 #define zend_hash_move_backwards(ht) \
244 	zend_hash_move_backwards_ex(ht, &(ht)->nInternalPointer)
245 #define zend_hash_get_current_key(ht, str_index, num_index) \
246 	zend_hash_get_current_key_ex(ht, str_index, num_index, &(ht)->nInternalPointer)
247 #define zend_hash_get_current_key_zval(ht, key) \
248 	zend_hash_get_current_key_zval_ex(ht, key, &(ht)->nInternalPointer)
249 #define zend_hash_get_current_key_type(ht) \
250 	zend_hash_get_current_key_type_ex(ht, &(ht)->nInternalPointer)
251 #define zend_hash_get_current_data(ht) \
252 	zend_hash_get_current_data_ex(ht, &(ht)->nInternalPointer)
253 #define zend_hash_internal_pointer_reset(ht) \
254 	zend_hash_internal_pointer_reset_ex(ht, &(ht)->nInternalPointer)
255 #define zend_hash_internal_pointer_end(ht) \
256 	zend_hash_internal_pointer_end_ex(ht, &(ht)->nInternalPointer)
257 
258 /* Copying, merging and sorting */
259 ZEND_API void  ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor);
260 ZEND_API void  ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, zend_bool overwrite);
261 ZEND_API void  ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
262 ZEND_API void  zend_hash_bucket_swap(Bucket *p, Bucket *q);
263 ZEND_API void  zend_hash_bucket_renum_swap(Bucket *p, Bucket *q);
264 ZEND_API void  zend_hash_bucket_packed_swap(Bucket *p, Bucket *q);
265 
266 typedef int (*bucket_compare_func_t)(Bucket *a, Bucket *b);
267 ZEND_API int   zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered);
268 ZEND_API void  ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, zend_bool renumber);
269 ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, bucket_compare_func_t compar, uint32_t flag);
270 
271 #define zend_hash_sort(ht, compare_func, renumber) \
272 	zend_hash_sort_ex(ht, zend_sort, compare_func, renumber)
273 
274 #define zend_hash_num_elements(ht) \
275 	(ht)->nNumOfElements
276 
277 #define zend_hash_next_free_element(ht) \
278 	(ht)->nNextFreeElement
279 
280 ZEND_API void ZEND_FASTCALL zend_hash_rehash(HashTable *ht);
281 
282 #if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
283 # define zend_new_array(size) \
284 	(__builtin_constant_p(size) ? \
285 		((((uint32_t)(size)) <= HT_MIN_SIZE) ? \
286 			_zend_new_array_0() \
287 		: \
288 			_zend_new_array((size)) \
289 		) \
290 	: \
291 		_zend_new_array((size)) \
292 	)
293 #else
294 # define zend_new_array(size) \
295 	_zend_new_array(size)
296 #endif
297 
298 ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(void);
299 ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t size);
300 ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(zval *val1, zval *val2);
301 ZEND_API uint32_t zend_array_count(HashTable *ht);
302 ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source);
303 ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht);
304 ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht);
305 ZEND_API HashTable* ZEND_FASTCALL zend_symtable_to_proptable(HashTable *ht);
306 ZEND_API HashTable* ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, zend_bool always_duplicate);
307 
308 ZEND_API bool ZEND_FASTCALL _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx);
309 
310 ZEND_API uint32_t     ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos);
311 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht);
312 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval *array);
313 ZEND_API void         ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx);
314 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(HashTable *ht, HashPosition start);
315 ZEND_API void         ZEND_FASTCALL _zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to);
316 ZEND_API void         ZEND_FASTCALL zend_hash_iterators_advance(HashTable *ht, HashPosition step);
317 
zend_hash_iterators_update(HashTable * ht,HashPosition from,HashPosition to)318 static zend_always_inline void zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to)
319 {
320 	if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
321 		_zend_hash_iterators_update(ht, from, to);
322 	}
323 }
324 
325 /* For regular arrays (non-persistent, storing zvals). */
zend_array_release(zend_array * array)326 static zend_always_inline void zend_array_release(zend_array *array)
327 {
328 	if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
329 		if (GC_DELREF(array) == 0) {
330 			zend_array_destroy(array);
331 		}
332 	}
333 }
334 
335 /* For general hashes (possibly persistent, storing any kind of value). */
zend_hash_release(zend_array * array)336 static zend_always_inline void zend_hash_release(zend_array *array)
337 {
338 	if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
339 		if (GC_DELREF(array) == 0) {
340 			zend_hash_destroy(array);
341 			pefree(array, GC_FLAGS(array) & IS_ARRAY_PERSISTENT);
342 		}
343 	}
344 }
345 
END_EXTERN_C()346 END_EXTERN_C()
347 
348 #define ZEND_INIT_SYMTABLE(ht)								\
349 	ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
350 
351 #define ZEND_INIT_SYMTABLE_EX(ht, n, persistent)			\
352 	zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
353 
354 static zend_always_inline bool _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
355 {
356 	const char *tmp = key;
357 
358 	if (EXPECTED(*tmp > '9')) {
359 		return 0;
360 	} else if (*tmp < '0') {
361 		if (*tmp != '-') {
362 			return 0;
363 		}
364 		tmp++;
365 		if (*tmp > '9' || *tmp < '0') {
366 			return 0;
367 		}
368 	}
369 	return _zend_handle_numeric_str_ex(key, length, idx);
370 }
371 
372 #define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
373 	_zend_handle_numeric_str(key, length, &idx)
374 
375 #define ZEND_HANDLE_NUMERIC(key, idx) \
376 	ZEND_HANDLE_NUMERIC_STR(ZSTR_VAL(key), ZSTR_LEN(key), idx)
377 
378 
zend_hash_find_ind(const HashTable * ht,zend_string * key)379 static zend_always_inline zval *zend_hash_find_ind(const HashTable *ht, zend_string *key)
380 {
381 	zval *zv;
382 
383 	zv = zend_hash_find(ht, key);
384 	return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
385 		((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
386 }
387 
388 
zend_hash_find_ex_ind(const HashTable * ht,zend_string * key,zend_bool known_hash)389 static zend_always_inline zval *zend_hash_find_ex_ind(const HashTable *ht, zend_string *key, zend_bool known_hash)
390 {
391 	zval *zv;
392 
393 	zv = zend_hash_find_ex(ht, key, known_hash);
394 	return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
395 		((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
396 }
397 
398 
zend_hash_exists_ind(const HashTable * ht,zend_string * key)399 static zend_always_inline bool zend_hash_exists_ind(const HashTable *ht, zend_string *key)
400 {
401 	zval *zv;
402 
403 	zv = zend_hash_find(ht, key);
404 	return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
405 			Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
406 }
407 
408 
zend_hash_str_find_ind(const HashTable * ht,const char * str,size_t len)409 static zend_always_inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, size_t len)
410 {
411 	zval *zv;
412 
413 	zv = zend_hash_str_find(ht, str, len);
414 	return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
415 		((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
416 }
417 
418 
zend_hash_str_exists_ind(const HashTable * ht,const char * str,size_t len)419 static zend_always_inline bool zend_hash_str_exists_ind(const HashTable *ht, const char *str, size_t len)
420 {
421 	zval *zv;
422 
423 	zv = zend_hash_str_find(ht, str, len);
424 	return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
425 			Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
426 }
427 
zend_symtable_add_new(HashTable * ht,zend_string * key,zval * pData)428 static zend_always_inline zval *zend_symtable_add_new(HashTable *ht, zend_string *key, zval *pData)
429 {
430 	zend_ulong idx;
431 
432 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
433 		return zend_hash_index_add_new(ht, idx, pData);
434 	} else {
435 		return zend_hash_add_new(ht, key, pData);
436 	}
437 }
438 
zend_symtable_update(HashTable * ht,zend_string * key,zval * pData)439 static zend_always_inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
440 {
441 	zend_ulong idx;
442 
443 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
444 		return zend_hash_index_update(ht, idx, pData);
445 	} else {
446 		return zend_hash_update(ht, key, pData);
447 	}
448 }
449 
450 
zend_symtable_update_ind(HashTable * ht,zend_string * key,zval * pData)451 static zend_always_inline zval *zend_symtable_update_ind(HashTable *ht, zend_string *key, zval *pData)
452 {
453 	zend_ulong idx;
454 
455 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
456 		return zend_hash_index_update(ht, idx, pData);
457 	} else {
458 		return zend_hash_update_ind(ht, key, pData);
459 	}
460 }
461 
462 
zend_symtable_del(HashTable * ht,zend_string * key)463 static zend_always_inline zend_result zend_symtable_del(HashTable *ht, zend_string *key)
464 {
465 	zend_ulong idx;
466 
467 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
468 		return zend_hash_index_del(ht, idx);
469 	} else {
470 		return zend_hash_del(ht, key);
471 	}
472 }
473 
474 
zend_symtable_del_ind(HashTable * ht,zend_string * key)475 static zend_always_inline zend_result zend_symtable_del_ind(HashTable *ht, zend_string *key)
476 {
477 	zend_ulong idx;
478 
479 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
480 		return zend_hash_index_del(ht, idx);
481 	} else {
482 		return zend_hash_del_ind(ht, key);
483 	}
484 }
485 
486 
zend_symtable_find(const HashTable * ht,zend_string * key)487 static zend_always_inline zval *zend_symtable_find(const HashTable *ht, zend_string *key)
488 {
489 	zend_ulong idx;
490 
491 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
492 		return zend_hash_index_find(ht, idx);
493 	} else {
494 		return zend_hash_find(ht, key);
495 	}
496 }
497 
498 
zend_symtable_find_ind(const HashTable * ht,zend_string * key)499 static zend_always_inline zval *zend_symtable_find_ind(const HashTable *ht, zend_string *key)
500 {
501 	zend_ulong idx;
502 
503 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
504 		return zend_hash_index_find(ht, idx);
505 	} else {
506 		return zend_hash_find_ind(ht, key);
507 	}
508 }
509 
510 
zend_symtable_exists(HashTable * ht,zend_string * key)511 static zend_always_inline bool zend_symtable_exists(HashTable *ht, zend_string *key)
512 {
513 	zend_ulong idx;
514 
515 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
516 		return zend_hash_index_exists(ht, idx);
517 	} else {
518 		return zend_hash_exists(ht, key);
519 	}
520 }
521 
522 
zend_symtable_exists_ind(HashTable * ht,zend_string * key)523 static zend_always_inline bool zend_symtable_exists_ind(HashTable *ht, zend_string *key)
524 {
525 	zend_ulong idx;
526 
527 	if (ZEND_HANDLE_NUMERIC(key, idx)) {
528 		return zend_hash_index_exists(ht, idx);
529 	} else {
530 		return zend_hash_exists_ind(ht, key);
531 	}
532 }
533 
534 
zend_symtable_str_update(HashTable * ht,const char * str,size_t len,zval * pData)535 static zend_always_inline zval *zend_symtable_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
536 {
537 	zend_ulong idx;
538 
539 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
540 		return zend_hash_index_update(ht, idx, pData);
541 	} else {
542 		return zend_hash_str_update(ht, str, len, pData);
543 	}
544 }
545 
546 
zend_symtable_str_update_ind(HashTable * ht,const char * str,size_t len,zval * pData)547 static zend_always_inline zval *zend_symtable_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
548 {
549 	zend_ulong idx;
550 
551 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
552 		return zend_hash_index_update(ht, idx, pData);
553 	} else {
554 		return zend_hash_str_update_ind(ht, str, len, pData);
555 	}
556 }
557 
558 
zend_symtable_str_del(HashTable * ht,const char * str,size_t len)559 static zend_always_inline zend_result zend_symtable_str_del(HashTable *ht, const char *str, size_t len)
560 {
561 	zend_ulong idx;
562 
563 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
564 		return zend_hash_index_del(ht, idx);
565 	} else {
566 		return zend_hash_str_del(ht, str, len);
567 	}
568 }
569 
570 
zend_symtable_str_del_ind(HashTable * ht,const char * str,size_t len)571 static zend_always_inline zend_result zend_symtable_str_del_ind(HashTable *ht, const char *str, size_t len)
572 {
573 	zend_ulong idx;
574 
575 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
576 		return zend_hash_index_del(ht, idx);
577 	} else {
578 		return zend_hash_str_del_ind(ht, str, len);
579 	}
580 }
581 
582 
zend_symtable_str_find(HashTable * ht,const char * str,size_t len)583 static zend_always_inline zval *zend_symtable_str_find(HashTable *ht, const char *str, size_t len)
584 {
585 	zend_ulong idx;
586 
587 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
588 		return zend_hash_index_find(ht, idx);
589 	} else {
590 		return zend_hash_str_find(ht, str, len);
591 	}
592 }
593 
594 
zend_symtable_str_exists(HashTable * ht,const char * str,size_t len)595 static zend_always_inline bool zend_symtable_str_exists(HashTable *ht, const char *str, size_t len)
596 {
597 	zend_ulong idx;
598 
599 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
600 		return zend_hash_index_exists(ht, idx);
601 	} else {
602 		return zend_hash_str_exists(ht, str, len);
603 	}
604 }
605 
zend_hash_add_ptr(HashTable * ht,zend_string * key,void * pData)606 static zend_always_inline void *zend_hash_add_ptr(HashTable *ht, zend_string *key, void *pData)
607 {
608 	zval tmp, *zv;
609 
610 	ZVAL_PTR(&tmp, pData);
611 	zv = zend_hash_add(ht, key, &tmp);
612 	if (zv) {
613 		ZEND_ASSUME(Z_PTR_P(zv));
614 		return Z_PTR_P(zv);
615 	} else {
616 		return NULL;
617 	}
618 }
619 
zend_hash_add_new_ptr(HashTable * ht,zend_string * key,void * pData)620 static zend_always_inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
621 {
622 	zval tmp, *zv;
623 
624 	ZVAL_PTR(&tmp, pData);
625 	zv = zend_hash_add_new(ht, key, &tmp);
626 	if (zv) {
627 		ZEND_ASSUME(Z_PTR_P(zv));
628 		return Z_PTR_P(zv);
629 	} else {
630 		return NULL;
631 	}
632 }
633 
zend_hash_str_add_ptr(HashTable * ht,const char * str,size_t len,void * pData)634 static zend_always_inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, size_t len, void *pData)
635 {
636 	zval tmp, *zv;
637 
638 	ZVAL_PTR(&tmp, pData);
639 	zv = zend_hash_str_add(ht, str, len, &tmp);
640 	if (zv) {
641 		ZEND_ASSUME(Z_PTR_P(zv));
642 		return Z_PTR_P(zv);
643 	} else {
644 		return NULL;
645 	}
646 }
647 
zend_hash_str_add_new_ptr(HashTable * ht,const char * str,size_t len,void * pData)648 static zend_always_inline void *zend_hash_str_add_new_ptr(HashTable *ht, const char *str, size_t len, void *pData)
649 {
650 	zval tmp, *zv;
651 
652 	ZVAL_PTR(&tmp, pData);
653 	zv = zend_hash_str_add_new(ht, str, len, &tmp);
654 	if (zv) {
655 		ZEND_ASSUME(Z_PTR_P(zv));
656 		return Z_PTR_P(zv);
657 	} else {
658 		return NULL;
659 	}
660 }
661 
zend_hash_update_ptr(HashTable * ht,zend_string * key,void * pData)662 static zend_always_inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
663 {
664 	zval tmp, *zv;
665 
666 	ZVAL_PTR(&tmp, pData);
667 	zv = zend_hash_update(ht, key, &tmp);
668 	ZEND_ASSUME(Z_PTR_P(zv));
669 	return Z_PTR_P(zv);
670 }
671 
zend_hash_str_update_ptr(HashTable * ht,const char * str,size_t len,void * pData)672 static zend_always_inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, size_t len, void *pData)
673 {
674 	zval tmp, *zv;
675 
676 	ZVAL_PTR(&tmp, pData);
677 	zv = zend_hash_str_update(ht, str, len, &tmp);
678 	ZEND_ASSUME(Z_PTR_P(zv));
679 	return Z_PTR_P(zv);
680 }
681 
zend_hash_add_mem(HashTable * ht,zend_string * key,void * pData,size_t size)682 static zend_always_inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
683 {
684 	zval tmp, *zv;
685 
686 	ZVAL_PTR(&tmp, NULL);
687 	if ((zv = zend_hash_add(ht, key, &tmp))) {
688 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
689 		memcpy(Z_PTR_P(zv), pData, size);
690 		return Z_PTR_P(zv);
691 	}
692 	return NULL;
693 }
694 
zend_hash_add_new_mem(HashTable * ht,zend_string * key,void * pData,size_t size)695 static zend_always_inline void *zend_hash_add_new_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
696 {
697 	zval tmp, *zv;
698 
699 	ZVAL_PTR(&tmp, NULL);
700 	if ((zv = zend_hash_add_new(ht, key, &tmp))) {
701 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
702 		memcpy(Z_PTR_P(zv), pData, size);
703 		return Z_PTR_P(zv);
704 	}
705 	return NULL;
706 }
707 
zend_hash_str_add_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)708 static zend_always_inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
709 {
710 	zval tmp, *zv;
711 
712 	ZVAL_PTR(&tmp, NULL);
713 	if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
714 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
715 		memcpy(Z_PTR_P(zv), pData, size);
716 		return Z_PTR_P(zv);
717 	}
718 	return NULL;
719 }
720 
zend_hash_str_add_new_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)721 static zend_always_inline void *zend_hash_str_add_new_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
722 {
723 	zval tmp, *zv;
724 
725 	ZVAL_PTR(&tmp, NULL);
726 	if ((zv = zend_hash_str_add_new(ht, str, len, &tmp))) {
727 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
728 		memcpy(Z_PTR_P(zv), pData, size);
729 		return Z_PTR_P(zv);
730 	}
731 	return NULL;
732 }
733 
zend_hash_update_mem(HashTable * ht,zend_string * key,void * pData,size_t size)734 static zend_always_inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
735 {
736 	void *p;
737 
738 	p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
739 	memcpy(p, pData, size);
740 	return zend_hash_update_ptr(ht, key, p);
741 }
742 
zend_hash_str_update_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)743 static zend_always_inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
744 {
745 	void *p;
746 
747 	p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
748 	memcpy(p, pData, size);
749 	return zend_hash_str_update_ptr(ht, str, len, p);
750 }
751 
zend_hash_index_add_ptr(HashTable * ht,zend_ulong h,void * pData)752 static zend_always_inline void *zend_hash_index_add_ptr(HashTable *ht, zend_ulong h, void *pData)
753 {
754 	zval tmp, *zv;
755 
756 	ZVAL_PTR(&tmp, pData);
757 	zv = zend_hash_index_add(ht, h, &tmp);
758 	return zv ? Z_PTR_P(zv) : NULL;
759 }
760 
zend_hash_index_add_new_ptr(HashTable * ht,zend_ulong h,void * pData)761 static zend_always_inline void *zend_hash_index_add_new_ptr(HashTable *ht, zend_ulong h, void *pData)
762 {
763 	zval tmp, *zv;
764 
765 	ZVAL_PTR(&tmp, pData);
766 	zv = zend_hash_index_add_new(ht, h, &tmp);
767 	return zv ? Z_PTR_P(zv) : NULL;
768 }
769 
zend_hash_index_update_ptr(HashTable * ht,zend_ulong h,void * pData)770 static zend_always_inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
771 {
772 	zval tmp, *zv;
773 
774 	ZVAL_PTR(&tmp, pData);
775 	zv = zend_hash_index_update(ht, h, &tmp);
776 	ZEND_ASSUME(Z_PTR_P(zv));
777 	return Z_PTR_P(zv);
778 }
779 
zend_hash_index_add_mem(HashTable * ht,zend_ulong h,void * pData,size_t size)780 static zend_always_inline void *zend_hash_index_add_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
781 {
782 	zval tmp, *zv;
783 
784 	ZVAL_PTR(&tmp, NULL);
785 	if ((zv = zend_hash_index_add(ht, h, &tmp))) {
786 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
787 		memcpy(Z_PTR_P(zv), pData, size);
788 		return Z_PTR_P(zv);
789 	}
790 	return NULL;
791 }
792 
zend_hash_next_index_insert_ptr(HashTable * ht,void * pData)793 static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
794 {
795 	zval tmp, *zv;
796 
797 	ZVAL_PTR(&tmp, pData);
798 	zv = zend_hash_next_index_insert(ht, &tmp);
799 	if (zv) {
800 		ZEND_ASSUME(Z_PTR_P(zv));
801 		return Z_PTR_P(zv);
802 	} else {
803 		return NULL;
804 	}
805 }
806 
zend_hash_index_update_mem(HashTable * ht,zend_ulong h,void * pData,size_t size)807 static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
808 {
809 	void *p;
810 
811 	p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
812 	memcpy(p, pData, size);
813 	return zend_hash_index_update_ptr(ht, h, p);
814 }
815 
zend_hash_next_index_insert_mem(HashTable * ht,void * pData,size_t size)816 static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
817 {
818 	zval tmp, *zv;
819 
820 	ZVAL_PTR(&tmp, NULL);
821 	if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
822 		Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
823 		memcpy(Z_PTR_P(zv), pData, size);
824 		return Z_PTR_P(zv);
825 	}
826 	return NULL;
827 }
828 
zend_hash_find_ptr(const HashTable * ht,zend_string * key)829 static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
830 {
831 	zval *zv;
832 
833 	zv = zend_hash_find(ht, key);
834 	if (zv) {
835 		ZEND_ASSUME(Z_PTR_P(zv));
836 		return Z_PTR_P(zv);
837 	} else {
838 		return NULL;
839 	}
840 }
841 
zend_hash_find_ex_ptr(const HashTable * ht,zend_string * key,zend_bool known_hash)842 static zend_always_inline void *zend_hash_find_ex_ptr(const HashTable *ht, zend_string *key, zend_bool known_hash)
843 {
844 	zval *zv;
845 
846 	zv = zend_hash_find_ex(ht, key, known_hash);
847 	if (zv) {
848 		ZEND_ASSUME(Z_PTR_P(zv));
849 		return Z_PTR_P(zv);
850 	} else {
851 		return NULL;
852 	}
853 }
854 
zend_hash_str_find_ptr(const HashTable * ht,const char * str,size_t len)855 static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
856 {
857 	zval *zv;
858 
859 	zv = zend_hash_str_find(ht, str, len);
860 	if (zv) {
861 		ZEND_ASSUME(Z_PTR_P(zv));
862 		return Z_PTR_P(zv);
863 	} else {
864 		return NULL;
865 	}
866 }
867 
868 /* Will lowercase the str; use only if you don't need the lowercased string for
869  * anything else. If you have a lowered string, use zend_hash_str_find_ptr. */
870 ZEND_API void *zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len);
871 
872 /* Will lowercase the str; use only if you don't need the lowercased string for
873  * anything else. If you have a lowered string, use zend_hash_find_ptr. */
874 ZEND_API void *zend_hash_find_ptr_lc(const HashTable *ht, zend_string *key);
875 
zend_hash_index_find_ptr(const HashTable * ht,zend_ulong h)876 static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
877 {
878 	zval *zv;
879 
880 	zv = zend_hash_index_find(ht, h);
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_index_find_deref(HashTable * ht,zend_ulong h)889 static zend_always_inline zval *zend_hash_index_find_deref(HashTable *ht, zend_ulong h)
890 {
891 	zval *zv = zend_hash_index_find(ht, h);
892 	if (zv) {
893 		ZVAL_DEREF(zv);
894 	}
895 	return zv;
896 }
897 
zend_hash_find_deref(HashTable * ht,zend_string * str)898 static zend_always_inline zval *zend_hash_find_deref(HashTable *ht, zend_string *str)
899 {
900 	zval *zv = zend_hash_find(ht, str);
901 	if (zv) {
902 		ZVAL_DEREF(zv);
903 	}
904 	return zv;
905 }
906 
zend_hash_str_find_deref(HashTable * ht,const char * str,size_t len)907 static zend_always_inline zval *zend_hash_str_find_deref(HashTable *ht, const char *str, size_t len)
908 {
909 	zval *zv = zend_hash_str_find(ht, str, len);
910 	if (zv) {
911 		ZVAL_DEREF(zv);
912 	}
913 	return zv;
914 }
915 
zend_symtable_str_find_ptr(HashTable * ht,const char * str,size_t len)916 static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
917 {
918 	zend_ulong idx;
919 
920 	if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
921 		return zend_hash_index_find_ptr(ht, idx);
922 	} else {
923 		return zend_hash_str_find_ptr(ht, str, len);
924 	}
925 }
926 
zend_hash_get_current_data_ptr_ex(HashTable * ht,HashPosition * pos)927 static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
928 {
929 	zval *zv;
930 
931 	zv = zend_hash_get_current_data_ex(ht, pos);
932 	if (zv) {
933 		ZEND_ASSUME(Z_PTR_P(zv));
934 		return Z_PTR_P(zv);
935 	} else {
936 		return NULL;
937 	}
938 }
939 
940 #define zend_hash_get_current_data_ptr(ht) \
941 	zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
942 
943 #define ZEND_HASH_FOREACH(_ht, indirect) do { \
944 		HashTable *__ht = (_ht); \
945 		Bucket *_p = __ht->arData; \
946 		Bucket *_end = _p + __ht->nNumUsed; \
947 		for (; _p != _end; _p++) { \
948 			zval *_z = &_p->val; \
949 			if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
950 				_z = Z_INDIRECT_P(_z); \
951 			} \
952 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
953 
954 #define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
955 		HashTable *__ht = (_ht); \
956 		uint32_t _idx = __ht->nNumUsed; \
957 		Bucket *_p = __ht->arData + _idx; \
958 		zval *_z; \
959 		for (_idx = __ht->nNumUsed; _idx > 0; _idx--) { \
960 			_p--; \
961 			_z = &_p->val; \
962 			if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
963 				_z = Z_INDIRECT_P(_z); \
964 			} \
965 			if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
966 
967 #define ZEND_HASH_FOREACH_END() \
968 		} \
969 	} while (0)
970 
971 #define ZEND_HASH_FOREACH_END_DEL() \
972 			__ht->nNumOfElements--; \
973 			do { \
974 				uint32_t j = HT_IDX_TO_HASH(_idx - 1); \
975 				uint32_t nIndex = _p->h | __ht->nTableMask; \
976 				uint32_t i = HT_HASH(__ht, nIndex); \
977 				if (UNEXPECTED(j != i)) { \
978 					Bucket *prev = HT_HASH_TO_BUCKET(__ht, i); \
979 					while (Z_NEXT(prev->val) != j) { \
980 						i = Z_NEXT(prev->val); \
981 						prev = HT_HASH_TO_BUCKET(__ht, i); \
982 					} \
983 					Z_NEXT(prev->val) = Z_NEXT(_p->val); \
984 				} else { \
985 					HT_HASH(__ht, nIndex) = Z_NEXT(_p->val); \
986 				} \
987 			} while (0); \
988 		} \
989 		__ht->nNumUsed = _idx; \
990 	} while (0)
991 
992 #define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
993 	ZEND_HASH_FOREACH(ht, 0); \
994 	_bucket = _p;
995 
996 #define ZEND_HASH_REVERSE_FOREACH_BUCKET(ht, _bucket) \
997 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
998 	_bucket = _p;
999 
1000 #define ZEND_HASH_FOREACH_VAL(ht, _val) \
1001 	ZEND_HASH_FOREACH(ht, 0); \
1002 	_val = _z;
1003 
1004 #define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
1005 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1006 	_val = _z;
1007 
1008 #define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
1009 	ZEND_HASH_FOREACH(ht, 1); \
1010 	_val = _z;
1011 
1012 #define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
1013 	ZEND_HASH_REVERSE_FOREACH(ht, 1); \
1014 	_val = _z;
1015 
1016 #define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
1017 	ZEND_HASH_FOREACH(ht, 0); \
1018 	_ptr = Z_PTR_P(_z);
1019 
1020 #define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
1021 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1022 	_ptr = Z_PTR_P(_z);
1023 
1024 #define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
1025 	ZEND_HASH_FOREACH(ht, 0); \
1026 	_h = _p->h;
1027 
1028 #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY(ht, _h) \
1029 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1030 	_h = _p->h;
1031 
1032 #define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
1033 	ZEND_HASH_FOREACH(ht, 0); \
1034 	_key = _p->key;
1035 
1036 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY(ht, _key) \
1037 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1038 	_key = _p->key;
1039 
1040 #define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
1041 	ZEND_HASH_FOREACH(ht, 0); \
1042 	_h = _p->h; \
1043 	_key = _p->key;
1044 
1045 #define ZEND_HASH_REVERSE_FOREACH_KEY(ht, _h, _key) \
1046 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1047 	_h = _p->h; \
1048 	_key = _p->key;
1049 
1050 #define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
1051 	ZEND_HASH_FOREACH(ht, 0); \
1052 	_h = _p->h; \
1053 	_val = _z;
1054 
1055 #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
1056 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1057 	_h = _p->h; \
1058 	_val = _z;
1059 
1060 #define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
1061 	ZEND_HASH_FOREACH(ht, 0); \
1062 	_key = _p->key; \
1063 	_val = _z;
1064 
1065 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(ht, _key, _val) \
1066 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1067 	_key = _p->key; \
1068 	_val = _z;
1069 
1070 #define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
1071 	ZEND_HASH_FOREACH(ht, 0); \
1072 	_h = _p->h; \
1073 	_key = _p->key; \
1074 	_val = _z;
1075 
1076 #define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
1077 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1078 	_h = _p->h; \
1079 	_key = _p->key; \
1080 	_val = _z;
1081 
1082 #define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
1083 	ZEND_HASH_FOREACH(ht, 1); \
1084 	_key = _p->key; \
1085 	_val = _z;
1086 
1087 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
1088 	ZEND_HASH_REVERSE_FOREACH(ht, 1); \
1089 	_key = _p->key; \
1090 	_val = _z;
1091 
1092 #define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
1093 	ZEND_HASH_FOREACH(ht, 1); \
1094 	_h = _p->h; \
1095 	_key = _p->key; \
1096 	_val = _z;
1097 
1098 #define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
1099 	ZEND_HASH_REVERSE_FOREACH(ht, 1); \
1100 	_h = _p->h; \
1101 	_key = _p->key; \
1102 	_val = _z;
1103 
1104 #define ZEND_HASH_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
1105 	ZEND_HASH_FOREACH(ht, 0); \
1106 	_h = _p->h; \
1107 	_ptr = Z_PTR_P(_z);
1108 
1109 #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
1110 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1111 	_h = _p->h; \
1112 	_ptr = Z_PTR_P(_z);
1113 
1114 #define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
1115 	ZEND_HASH_FOREACH(ht, 0); \
1116 	_key = _p->key; \
1117 	_ptr = Z_PTR_P(_z);
1118 
1119 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
1120 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1121 	_key = _p->key; \
1122 	_ptr = Z_PTR_P(_z);
1123 
1124 #define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
1125 	ZEND_HASH_FOREACH(ht, 0); \
1126 	_h = _p->h; \
1127 	_key = _p->key; \
1128 	_ptr = Z_PTR_P(_z);
1129 
1130 #define ZEND_HASH_REVERSE_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
1131 	ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1132 	_h = _p->h; \
1133 	_key = _p->key; \
1134 	_ptr = Z_PTR_P(_z);
1135 
1136 /* The following macros are useful to insert a sequence of new elements
1137  * of packed array. They may be used instead of series of
1138  * zend_hash_next_index_insert_new()
1139  * (HashTable must have enough free buckets).
1140  */
1141 #define ZEND_HASH_FILL_PACKED(ht) do { \
1142 		HashTable *__fill_ht = (ht); \
1143 		Bucket *__fill_bkt = __fill_ht->arData + __fill_ht->nNumUsed; \
1144 		uint32_t __fill_idx = __fill_ht->nNumUsed; \
1145 		ZEND_ASSERT(HT_FLAGS(__fill_ht) & HASH_FLAG_PACKED);
1146 
1147 #define ZEND_HASH_FILL_SET(_val) \
1148 		ZVAL_COPY_VALUE(&__fill_bkt->val, _val)
1149 
1150 #define ZEND_HASH_FILL_SET_NULL() \
1151 		ZVAL_NULL(&__fill_bkt->val)
1152 
1153 #define ZEND_HASH_FILL_SET_LONG(_val) \
1154 		ZVAL_LONG(&__fill_bkt->val, _val)
1155 
1156 #define ZEND_HASH_FILL_SET_DOUBLE(_val) \
1157 		ZVAL_DOUBLE(&__fill_bkt->val, _val)
1158 
1159 #define ZEND_HASH_FILL_SET_STR(_val) \
1160 		ZVAL_STR(&__fill_bkt->val, _val)
1161 
1162 #define ZEND_HASH_FILL_SET_STR_COPY(_val) \
1163 		ZVAL_STR_COPY(&__fill_bkt->val, _val)
1164 
1165 #define ZEND_HASH_FILL_SET_INTERNED_STR(_val) \
1166 		ZVAL_INTERNED_STR(&__fill_bkt->val, _val)
1167 
1168 #define ZEND_HASH_FILL_NEXT() do {\
1169 		__fill_bkt->h = (__fill_idx); \
1170 		__fill_bkt->key = NULL; \
1171 		__fill_bkt++; \
1172 		__fill_idx++; \
1173 	} while (0)
1174 
1175 #define ZEND_HASH_FILL_ADD(_val) do { \
1176 		ZEND_HASH_FILL_SET(_val); \
1177 		ZEND_HASH_FILL_NEXT(); \
1178 	} while (0)
1179 
1180 #define ZEND_HASH_FILL_FINISH() do { \
1181 		__fill_ht->nNumUsed = __fill_idx; \
1182 		__fill_ht->nNumOfElements = __fill_idx; \
1183 		__fill_ht->nNextFreeElement = __fill_idx; \
1184 		__fill_ht->nInternalPointer = 0; \
1185 	} while (0)
1186 
1187 #define ZEND_HASH_FILL_END() \
1188 		ZEND_HASH_FILL_FINISH(); \
1189 	} while (0)
1190 
_zend_hash_append_ex(HashTable * ht,zend_string * key,zval * zv,bool interned)1191 static zend_always_inline zval *_zend_hash_append_ex(HashTable *ht, zend_string *key, zval *zv, bool interned)
1192 {
1193 	uint32_t idx = ht->nNumUsed++;
1194 	uint32_t nIndex;
1195 	Bucket *p = ht->arData + idx;
1196 
1197 	ZVAL_COPY_VALUE(&p->val, zv);
1198 	if (!interned && !ZSTR_IS_INTERNED(key)) {
1199 		HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1200 		zend_string_addref(key);
1201 		zend_string_hash_val(key);
1202 	}
1203 	p->key = key;
1204 	p->h = ZSTR_H(key);
1205 	nIndex = (uint32_t)p->h | ht->nTableMask;
1206 	Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1207 	HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1208 	ht->nNumOfElements++;
1209 	return &p->val;
1210 }
1211 
_zend_hash_append(HashTable * ht,zend_string * key,zval * zv)1212 static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *key, zval *zv)
1213 {
1214 	return _zend_hash_append_ex(ht, key, zv, 0);
1215 }
1216 
_zend_hash_append_ptr_ex(HashTable * ht,zend_string * key,void * ptr,bool interned)1217 static zend_always_inline zval *_zend_hash_append_ptr_ex(HashTable *ht, zend_string *key, void *ptr, bool interned)
1218 {
1219 	uint32_t idx = ht->nNumUsed++;
1220 	uint32_t nIndex;
1221 	Bucket *p = ht->arData + idx;
1222 
1223 	ZVAL_PTR(&p->val, ptr);
1224 	if (!interned && !ZSTR_IS_INTERNED(key)) {
1225 		HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1226 		zend_string_addref(key);
1227 		zend_string_hash_val(key);
1228 	}
1229 	p->key = key;
1230 	p->h = ZSTR_H(key);
1231 	nIndex = (uint32_t)p->h | ht->nTableMask;
1232 	Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1233 	HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1234 	ht->nNumOfElements++;
1235 	return &p->val;
1236 }
1237 
_zend_hash_append_ptr(HashTable * ht,zend_string * key,void * ptr)1238 static zend_always_inline zval *_zend_hash_append_ptr(HashTable *ht, zend_string *key, void *ptr)
1239 {
1240 	return _zend_hash_append_ptr_ex(ht, key, ptr, 0);
1241 }
1242 
_zend_hash_append_ind(HashTable * ht,zend_string * key,zval * ptr)1243 static zend_always_inline void _zend_hash_append_ind(HashTable *ht, zend_string *key, zval *ptr)
1244 {
1245 	uint32_t idx = ht->nNumUsed++;
1246 	uint32_t nIndex;
1247 	Bucket *p = ht->arData + idx;
1248 
1249 	ZVAL_INDIRECT(&p->val, ptr);
1250 	if (!ZSTR_IS_INTERNED(key)) {
1251 		HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1252 		zend_string_addref(key);
1253 		zend_string_hash_val(key);
1254 	}
1255 	p->key = key;
1256 	p->h = ZSTR_H(key);
1257 	nIndex = (uint32_t)p->h | ht->nTableMask;
1258 	Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1259 	HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1260 	ht->nNumOfElements++;
1261 }
1262 
1263 #endif							/* ZEND_HASH_H */
1264