1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 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@zend.com>                                |
16   |          Zeev Suraski <zeev@zend.com>                                |
17   |          Dmitry Stogov <dmitry@zend.com>                             |
18   +----------------------------------------------------------------------+
19*/
20
21/* $Id$ */
22
23#ifndef ZEND_HASH_H
24#define ZEND_HASH_H
25
26#include "zend.h"
27
28#define HASH_KEY_IS_STRING 1
29#define HASH_KEY_IS_LONG 2
30#define HASH_KEY_NON_EXISTENT 3
31
32#define HASH_UPDATE             (1<<0)
33#define HASH_ADD                (1<<1)
34#define HASH_UPDATE_INDIRECT    (1<<2)
35#define HASH_ADD_NEW            (1<<3)
36#define HASH_ADD_NEXT           (1<<4)
37
38#define HASH_FLAG_PERSISTENT       (1<<0)
39#define HASH_FLAG_APPLY_PROTECTION (1<<1)
40#define HASH_FLAG_PACKED           (1<<2)
41#define HASH_FLAG_INITIALIZED      (1<<3)
42#define HASH_FLAG_STATIC_KEYS      (1<<4)
43#define HASH_FLAG_HAS_EMPTY_IND    (1<<5)
44
45#define HASH_MASK_CONSISTENCY      0xc0
46
47typedef struct _zend_hash_key {
48    zend_ulong h;
49    zend_string *key;
50} zend_hash_key;
51
52typedef zend_bool (*merge_checker_func_t)(HashTable *target_ht, zval *source_data, zend_hash_key *hash_key, void *pParam);
53
54BEGIN_EXTERN_C()
55
56/* startup/shutdown */
57ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC);
58ZEND_API void ZEND_FASTCALL _zend_hash_init_ex(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent, zend_bool bApplyProtection ZEND_FILE_LINE_DC);
59ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht);
60ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht);
61#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)                       _zend_hash_init((ht), (nSize), (pDestructor), (persistent) ZEND_FILE_LINE_CC)
62#define zend_hash_init_ex(ht, nSize, pHashFunction, pDestructor, persistent, bApplyProtection)      _zend_hash_init_ex((ht), (nSize), (pDestructor), (persistent), (bApplyProtection) ZEND_FILE_LINE_CC)
63
64ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, zend_bool packed);
65ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht);
66ZEND_API void ZEND_FASTCALL zend_hash_to_packed(HashTable *ht);
67ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, zend_bool packed);
68
69/* additions/updates/changes */
70ZEND_API zval* ZEND_FASTCALL _zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag ZEND_FILE_LINE_DC);
71ZEND_API zval* ZEND_FASTCALL _zend_hash_update(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
72ZEND_API zval* ZEND_FASTCALL _zend_hash_update_ind(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
73ZEND_API zval* ZEND_FASTCALL _zend_hash_add(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
74ZEND_API zval* ZEND_FASTCALL _zend_hash_add_new(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
75
76#define zend_hash_update(ht, key, pData) \
77        _zend_hash_update(ht, key, pData ZEND_FILE_LINE_CC)
78#define zend_hash_update_ind(ht, key, pData) \
79        _zend_hash_update_ind(ht, key, pData ZEND_FILE_LINE_CC)
80#define zend_hash_add(ht, key, pData) \
81        _zend_hash_add(ht, key, pData ZEND_FILE_LINE_CC)
82#define zend_hash_add_new(ht, key, pData) \
83        _zend_hash_add_new(ht, key, pData ZEND_FILE_LINE_CC)
84
85ZEND_API zval* ZEND_FASTCALL _zend_hash_str_add_or_update(HashTable *ht, const char *key, size_t len, zval *pData, uint32_t flag ZEND_FILE_LINE_DC);
86ZEND_API zval* ZEND_FASTCALL _zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
87ZEND_API zval* ZEND_FASTCALL _zend_hash_str_update_ind(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
88ZEND_API zval* ZEND_FASTCALL _zend_hash_str_add(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
89ZEND_API zval* ZEND_FASTCALL _zend_hash_str_add_new(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
90
91#define zend_hash_str_update(ht, key, len, pData) \
92        _zend_hash_str_update(ht, key, len, pData ZEND_FILE_LINE_CC)
93#define zend_hash_str_update_ind(ht, key, len, pData) \
94        _zend_hash_str_update_ind(ht, key, len, pData ZEND_FILE_LINE_CC)
95#define zend_hash_str_add(ht, key, len, pData) \
96        _zend_hash_str_add(ht, key, len, pData ZEND_FILE_LINE_CC)
97#define zend_hash_str_add_new(ht, key, len, pData) \
98        _zend_hash_str_add_new(ht, key, len, pData ZEND_FILE_LINE_CC)
99
100ZEND_API zval* ZEND_FASTCALL _zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag ZEND_FILE_LINE_DC);
101ZEND_API zval* ZEND_FASTCALL _zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
102ZEND_API zval* ZEND_FASTCALL _zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
103ZEND_API zval* ZEND_FASTCALL _zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
104ZEND_API zval* ZEND_FASTCALL _zend_hash_next_index_insert(HashTable *ht, zval *pData ZEND_FILE_LINE_DC);
105ZEND_API zval* ZEND_FASTCALL _zend_hash_next_index_insert_new(HashTable *ht, zval *pData ZEND_FILE_LINE_DC);
106
107#define zend_hash_index_add(ht, h, pData) \
108        _zend_hash_index_add(ht, h, pData ZEND_FILE_LINE_CC)
109#define zend_hash_index_add_new(ht, h, pData) \
110        _zend_hash_index_add_new(ht, h, pData ZEND_FILE_LINE_CC)
111#define zend_hash_index_update(ht, h, pData) \
112        _zend_hash_index_update(ht, h, pData ZEND_FILE_LINE_CC)
113#define zend_hash_next_index_insert(ht, pData) \
114        _zend_hash_next_index_insert(ht, pData ZEND_FILE_LINE_CC)
115#define zend_hash_next_index_insert_new(ht, pData) \
116        _zend_hash_next_index_insert_new(ht, pData ZEND_FILE_LINE_CC)
117
118ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h);
119ZEND_API zval* ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key);
120ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_empty_element(HashTable *ht, const char *key, size_t len);
121
122#define ZEND_HASH_APPLY_KEEP                0
123#define ZEND_HASH_APPLY_REMOVE              1<<0
124#define ZEND_HASH_APPLY_STOP                1<<1
125
126typedef int (*apply_func_t)(zval *pDest);
127typedef int (*apply_func_arg_t)(zval *pDest, void *argument);
128typedef int (*apply_func_args_t)(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
129
130ZEND_API void ZEND_FASTCALL zend_hash_graceful_destroy(HashTable *ht);
131ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht);
132ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func);
133ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *);
134ZEND_API void ZEND_FASTCALL zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
135
136/* This function should be used with special care (in other words,
137 * it should usually not be used).  When used with the ZEND_HASH_APPLY_STOP
138 * return value, it assumes things about the order of the elements in the hash.
139 * Also, it does not provide the same kind of reentrancy protection that
140 * the standard apply functions do.
141 */
142ZEND_API void ZEND_FASTCALL zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func);
143
144
145/* Deletes */
146ZEND_API int ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key);
147ZEND_API int ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key);
148ZEND_API int ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *key, size_t len);
149ZEND_API int ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const char *key, size_t len);
150ZEND_API int ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h);
151ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p);
152
153/* Data retreival */
154ZEND_API zval* ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key);
155ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *key, size_t len);
156ZEND_API zval* ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h);
157
158/* Misc */
159ZEND_API zend_bool ZEND_FASTCALL zend_hash_exists(const HashTable *ht, zend_string *key);
160ZEND_API zend_bool ZEND_FASTCALL zend_hash_str_exists(const HashTable *ht, const char *str, size_t len);
161ZEND_API zend_bool ZEND_FASTCALL zend_hash_index_exists(const HashTable *ht, zend_ulong h);
162
163/* traversing */
164#define zend_hash_has_more_elements_ex(ht, pos) \
165    (zend_hash_get_current_key_type_ex(ht, pos) == HASH_KEY_NON_EXISTENT ? FAILURE : SUCCESS)
166ZEND_API int   ZEND_FASTCALL zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
167ZEND_API int   ZEND_FASTCALL zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
168ZEND_API int   ZEND_FASTCALL zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, HashPosition *pos);
169ZEND_API void  ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos);
170ZEND_API int   ZEND_FASTCALL zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
171ZEND_API zval* ZEND_FASTCALL zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos);
172ZEND_API void  ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
173ZEND_API void  ZEND_FASTCALL zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
174
175#define zend_hash_has_more_elements(ht) \
176    zend_hash_has_more_elements_ex(ht, &(ht)->nInternalPointer)
177#define zend_hash_move_forward(ht) \
178    zend_hash_move_forward_ex(ht, &(ht)->nInternalPointer)
179#define zend_hash_move_backwards(ht) \
180    zend_hash_move_backwards_ex(ht, &(ht)->nInternalPointer)
181#define zend_hash_get_current_key(ht, str_index, num_index) \
182    zend_hash_get_current_key_ex(ht, str_index, num_index, &(ht)->nInternalPointer)
183#define zend_hash_get_current_key_zval(ht, key) \
184    zend_hash_get_current_key_zval_ex(ht, key, &(ht)->nInternalPointer)
185#define zend_hash_get_current_key_type(ht) \
186    zend_hash_get_current_key_type_ex(ht, &(ht)->nInternalPointer)
187#define zend_hash_get_current_data(ht) \
188    zend_hash_get_current_data_ex(ht, &(ht)->nInternalPointer)
189#define zend_hash_internal_pointer_reset(ht) \
190    zend_hash_internal_pointer_reset_ex(ht, &(ht)->nInternalPointer)
191#define zend_hash_internal_pointer_end(ht) \
192    zend_hash_internal_pointer_end_ex(ht, &(ht)->nInternalPointer)
193
194/* Copying, merging and sorting */
195ZEND_API void  ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor);
196ZEND_API void  ZEND_FASTCALL _zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, zend_bool overwrite ZEND_FILE_LINE_DC);
197ZEND_API void  ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
198ZEND_API void  zend_hash_bucket_swap(Bucket *p, Bucket *q);
199ZEND_API void  zend_hash_bucket_renum_swap(Bucket *p, Bucket *q);
200ZEND_API void  zend_hash_bucket_packed_swap(Bucket *p, Bucket *q);
201ZEND_API int   zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered);
202ZEND_API int   ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, zend_bool renumber);
203ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag);
204
205#define zend_hash_merge(target, source, pCopyConstructor, overwrite)                    \
206    _zend_hash_merge(target, source, pCopyConstructor, overwrite ZEND_FILE_LINE_CC)
207
208#define zend_hash_sort(ht, compare_func, renumber) \
209    zend_hash_sort_ex(ht, zend_sort, compare_func, renumber)
210
211#define zend_hash_num_elements(ht) \
212    (ht)->nNumOfElements
213
214#define zend_hash_next_free_element(ht) \
215    (ht)->nNextFreeElement
216
217ZEND_API int ZEND_FASTCALL zend_hash_rehash(HashTable *ht);
218
219ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source);
220ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht);
221ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht);
222
223ZEND_API int ZEND_FASTCALL _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx);
224
225ZEND_API uint32_t     ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos);
226ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht);
227ZEND_API void         ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx);
228ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(HashTable *ht, HashPosition start);
229ZEND_API void         ZEND_FASTCALL _zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to);
230
231static zend_always_inline void zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to)
232{
233    if (UNEXPECTED(ht->u.v.nIteratorsCount)) {
234        _zend_hash_iterators_update(ht, from, to);
235    }
236}
237
238
239END_EXTERN_C()
240
241#define ZEND_INIT_SYMTABLE(ht)                              \
242    ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
243
244#define ZEND_INIT_SYMTABLE_EX(ht, n, persistent)            \
245    zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
246
247static zend_always_inline int _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
248{
249    register const char *tmp = key;
250
251    if (*tmp > '9') {
252        return 0;
253    } else if (*tmp < '0') {
254        if (*tmp != '-') {
255            return 0;
256        }
257        tmp++;
258        if (*tmp > '9' || *tmp < '0') {
259            return 0;
260        }
261    }
262    return _zend_handle_numeric_str_ex(key, length, idx);
263}
264
265#define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
266    _zend_handle_numeric_str(key, length, &idx)
267
268#define ZEND_HANDLE_NUMERIC(key, idx) \
269    ZEND_HANDLE_NUMERIC_STR(ZSTR_VAL(key), ZSTR_LEN(key), idx)
270
271
272static zend_always_inline zval *zend_hash_find_ind(const HashTable *ht, zend_string *key)
273{
274    zval *zv;
275
276    zv = zend_hash_find(ht, key);
277    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
278        ((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
279}
280
281
282static zend_always_inline int zend_hash_exists_ind(const HashTable *ht, zend_string *key)
283{
284    zval *zv;
285
286    zv = zend_hash_find(ht, key);
287    return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
288            Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
289}
290
291
292static zend_always_inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, size_t len)
293{
294    zval *zv;
295
296    zv = zend_hash_str_find(ht, str, len);
297    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
298        ((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
299}
300
301
302static zend_always_inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
303{
304    zend_ulong idx;
305
306    if (ZEND_HANDLE_NUMERIC(key, idx)) {
307        return zend_hash_index_update(ht, idx, pData);
308    } else {
309        return zend_hash_update(ht, key, pData);
310    }
311}
312
313
314static zend_always_inline zval *zend_symtable_update_ind(HashTable *ht, zend_string *key, zval *pData)
315{
316    zend_ulong idx;
317
318    if (ZEND_HANDLE_NUMERIC(key, idx)) {
319        return zend_hash_index_update(ht, idx, pData);
320    } else {
321        return zend_hash_update_ind(ht, key, pData);
322    }
323}
324
325
326static zend_always_inline int zend_symtable_del(HashTable *ht, zend_string *key)
327{
328    zend_ulong idx;
329
330    if (ZEND_HANDLE_NUMERIC(key, idx)) {
331        return zend_hash_index_del(ht, idx);
332    } else {
333        return zend_hash_del(ht, key);
334    }
335}
336
337
338static zend_always_inline int zend_symtable_del_ind(HashTable *ht, zend_string *key)
339{
340    zend_ulong idx;
341
342    if (ZEND_HANDLE_NUMERIC(key, idx)) {
343        return zend_hash_index_del(ht, idx);
344    } else {
345        return zend_hash_del_ind(ht, key);
346    }
347}
348
349
350static zend_always_inline zval *zend_symtable_find(const HashTable *ht, zend_string *key)
351{
352    zend_ulong idx;
353
354    if (ZEND_HANDLE_NUMERIC(key, idx)) {
355        return zend_hash_index_find(ht, idx);
356    } else {
357        return zend_hash_find(ht, key);
358    }
359}
360
361
362static zend_always_inline zval *zend_symtable_find_ind(const HashTable *ht, zend_string *key)
363{
364    zend_ulong idx;
365
366    if (ZEND_HANDLE_NUMERIC(key, idx)) {
367        return zend_hash_index_find(ht, idx);
368    } else {
369        return zend_hash_find_ind(ht, key);
370    }
371}
372
373
374static zend_always_inline int zend_symtable_exists(HashTable *ht, zend_string *key)
375{
376    zend_ulong idx;
377
378    if (ZEND_HANDLE_NUMERIC(key, idx)) {
379        return zend_hash_index_exists(ht, idx);
380    } else {
381        return zend_hash_exists(ht, key);
382    }
383}
384
385
386static zend_always_inline zval *zend_symtable_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
387{
388    zend_ulong idx;
389
390    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
391        return zend_hash_index_update(ht, idx, pData);
392    } else {
393        return zend_hash_str_update(ht, str, len, pData);
394    }
395}
396
397
398static zend_always_inline zval *zend_symtable_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
399{
400    zend_ulong idx;
401
402    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
403        return zend_hash_index_update(ht, idx, pData);
404    } else {
405        return zend_hash_str_update_ind(ht, str, len, pData);
406    }
407}
408
409
410static zend_always_inline int zend_symtable_str_del(HashTable *ht, const char *str, size_t len)
411{
412    zend_ulong idx;
413
414    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
415        return zend_hash_index_del(ht, idx);
416    } else {
417        return zend_hash_str_del(ht, str, len);
418    }
419}
420
421
422static zend_always_inline int zend_symtable_str_del_ind(HashTable *ht, const char *str, size_t len)
423{
424    zend_ulong idx;
425
426    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
427        return zend_hash_index_del(ht, idx);
428    } else {
429        return zend_hash_str_del_ind(ht, str, len);
430    }
431}
432
433
434static zend_always_inline zval *zend_symtable_str_find(HashTable *ht, const char *str, size_t len)
435{
436    zend_ulong idx;
437
438    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
439        return zend_hash_index_find(ht, idx);
440    } else {
441        return zend_hash_str_find(ht, str, len);
442    }
443}
444
445
446static zend_always_inline int zend_symtable_str_exists(HashTable *ht, const char *str, size_t len)
447{
448    zend_ulong idx;
449
450    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
451        return zend_hash_index_exists(ht, idx);
452    } else {
453        return zend_hash_str_exists(ht, str, len);
454    }
455}
456
457static zend_always_inline void *zend_hash_add_ptr(HashTable *ht, zend_string *key, void *pData)
458{
459    zval tmp, *zv;
460
461    ZVAL_PTR(&tmp, pData);
462    zv = zend_hash_add(ht, key, &tmp);
463    if (zv) {
464        ZEND_ASSUME(Z_PTR_P(zv));
465        return Z_PTR_P(zv);
466    } else {
467        return NULL;
468    }
469}
470
471static zend_always_inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
472{
473    zval tmp, *zv;
474
475    ZVAL_PTR(&tmp, pData);
476    zv = zend_hash_add_new(ht, key, &tmp);
477    if (zv) {
478        ZEND_ASSUME(Z_PTR_P(zv));
479        return Z_PTR_P(zv);
480    } else {
481        return NULL;
482    }
483}
484
485static zend_always_inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, size_t len, void *pData)
486{
487    zval tmp, *zv;
488
489    ZVAL_PTR(&tmp, pData);
490    zv = zend_hash_str_add(ht, str, len, &tmp);
491    if (zv) {
492        ZEND_ASSUME(Z_PTR_P(zv));
493        return Z_PTR_P(zv);
494    } else {
495        return NULL;
496    }
497}
498
499static zend_always_inline void *zend_hash_str_add_new_ptr(HashTable *ht, const char *str, size_t len, void *pData)
500{
501    zval tmp, *zv;
502
503    ZVAL_PTR(&tmp, pData);
504    zv = zend_hash_str_add_new(ht, str, len, &tmp);
505    if (zv) {
506        ZEND_ASSUME(Z_PTR_P(zv));
507        return Z_PTR_P(zv);
508    } else {
509        return NULL;
510    }
511}
512
513static zend_always_inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
514{
515    zval tmp, *zv;
516
517    ZVAL_PTR(&tmp, pData);
518    zv = zend_hash_update(ht, key, &tmp);
519    if (zv) {
520        ZEND_ASSUME(Z_PTR_P(zv));
521        return Z_PTR_P(zv);
522    } else {
523        return NULL;
524    }
525}
526
527static zend_always_inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, size_t len, void *pData)
528{
529    zval tmp, *zv;
530
531    ZVAL_PTR(&tmp, pData);
532    zv = zend_hash_str_update(ht, str, len, &tmp);
533    if (zv) {
534        ZEND_ASSUME(Z_PTR_P(zv));
535        return Z_PTR_P(zv);
536    } else {
537        return NULL;
538    }
539}
540
541static zend_always_inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
542{
543    zval tmp, *zv;
544
545    ZVAL_PTR(&tmp, NULL);
546    if ((zv = zend_hash_add(ht, key, &tmp))) {
547        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
548        memcpy(Z_PTR_P(zv), pData, size);
549        return Z_PTR_P(zv);
550    }
551    return NULL;
552}
553
554static zend_always_inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
555{
556    zval tmp, *zv;
557
558    ZVAL_PTR(&tmp, NULL);
559    if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
560        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
561        memcpy(Z_PTR_P(zv), pData, size);
562        return Z_PTR_P(zv);
563    }
564    return NULL;
565}
566
567static zend_always_inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
568{
569    void *p;
570
571    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
572    memcpy(p, pData, size);
573    return zend_hash_update_ptr(ht, key, p);
574}
575
576static zend_always_inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
577{
578    void *p;
579
580    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
581    memcpy(p, pData, size);
582    return zend_hash_str_update_ptr(ht, str, len, p);
583}
584
585static zend_always_inline void *zend_hash_index_add_ptr(HashTable *ht, zend_ulong h, void *pData)
586{
587    zval tmp, *zv;
588
589    ZVAL_PTR(&tmp, pData);
590    zv = zend_hash_index_add(ht, h, &tmp);
591    return zv ? Z_PTR_P(zv) : NULL;
592}
593
594static zend_always_inline void *zend_hash_index_add_new_ptr(HashTable *ht, zend_ulong h, void *pData)
595{
596    zval tmp, *zv;
597
598    ZVAL_PTR(&tmp, pData);
599    zv = zend_hash_index_add_new(ht, h, &tmp);
600    return zv ? Z_PTR_P(zv) : NULL;
601}
602
603static zend_always_inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
604{
605    zval tmp, *zv;
606
607    ZVAL_PTR(&tmp, pData);
608    zv = zend_hash_index_update(ht, h, &tmp);
609    if (zv) {
610        ZEND_ASSUME(Z_PTR_P(zv));
611        return Z_PTR_P(zv);
612    } else {
613        return NULL;
614    }
615}
616
617static zend_always_inline void *zend_hash_index_add_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
618{
619    zval tmp, *zv;
620
621    ZVAL_PTR(&tmp, NULL);
622    if ((zv = zend_hash_index_add(ht, h, &tmp))) {
623        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
624        memcpy(Z_PTR_P(zv), pData, size);
625        return Z_PTR_P(zv);
626    }
627    return NULL;
628}
629
630static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
631{
632    zval tmp, *zv;
633
634    ZVAL_PTR(&tmp, pData);
635    zv = zend_hash_next_index_insert(ht, &tmp);
636    if (zv) {
637        ZEND_ASSUME(Z_PTR_P(zv));
638        return Z_PTR_P(zv);
639    } else {
640        return NULL;
641    }
642}
643
644static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
645{
646    void *p;
647
648    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
649    memcpy(p, pData, size);
650    return zend_hash_index_update_ptr(ht, h, p);
651}
652
653static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
654{
655    zval tmp, *zv;
656
657    ZVAL_PTR(&tmp, NULL);
658    if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
659        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
660        memcpy(Z_PTR_P(zv), pData, size);
661        return Z_PTR_P(zv);
662    }
663    return NULL;
664}
665
666static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
667{
668    zval *zv;
669
670    zv = zend_hash_find(ht, key);
671    if (zv) {
672        ZEND_ASSUME(Z_PTR_P(zv));
673        return Z_PTR_P(zv);
674    } else {
675        return NULL;
676    }
677}
678
679static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
680{
681    zval *zv;
682
683    zv = zend_hash_str_find(ht, str, len);
684    if (zv) {
685        ZEND_ASSUME(Z_PTR_P(zv));
686        return Z_PTR_P(zv);
687    } else {
688        return NULL;
689    }
690}
691
692static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
693{
694    zval *zv;
695
696    zv = zend_hash_index_find(ht, h);
697    if (zv) {
698        ZEND_ASSUME(Z_PTR_P(zv));
699        return Z_PTR_P(zv);
700    } else {
701        return NULL;
702    }
703}
704
705static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
706{
707    zend_ulong idx;
708
709    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
710        return zend_hash_index_find_ptr(ht, idx);
711    } else {
712        return zend_hash_str_find_ptr(ht, str, len);
713    }
714}
715
716static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
717{
718    zval *zv;
719
720    zv = zend_hash_get_current_data_ex(ht, pos);
721    if (zv) {
722        ZEND_ASSUME(Z_PTR_P(zv));
723        return Z_PTR_P(zv);
724    } else {
725        return NULL;
726    }
727}
728
729#define zend_hash_get_current_data_ptr(ht) \
730    zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
731
732#define ZEND_HASH_FOREACH(_ht, indirect) do { \
733        Bucket *_p = (_ht)->arData; \
734        Bucket *_end = _p + (_ht)->nNumUsed; \
735        for (; _p != _end; _p++) { \
736            zval *_z = &_p->val; \
737            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
738                _z = Z_INDIRECT_P(_z); \
739            } \
740            if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
741
742#define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
743        uint _idx; \
744        for (_idx = (_ht)->nNumUsed; _idx > 0; _idx--) { \
745            Bucket *_p = (_ht)->arData + _idx - 1; \
746            zval *_z = &_p->val; \
747            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
748                _z = Z_INDIRECT_P(_z); \
749            } \
750            if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
751
752#define ZEND_HASH_FOREACH_END() \
753        } \
754    } while (0)
755
756#define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
757    ZEND_HASH_FOREACH(ht, 0); \
758    _bucket = _p;
759
760#define ZEND_HASH_FOREACH_VAL(ht, _val) \
761    ZEND_HASH_FOREACH(ht, 0); \
762    _val = _z;
763
764#define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
765    ZEND_HASH_FOREACH(ht, 1); \
766    _val = _z;
767
768#define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
769    ZEND_HASH_FOREACH(ht, 0); \
770    _ptr = Z_PTR_P(_z);
771
772#define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
773    ZEND_HASH_FOREACH(ht, 0); \
774    _h = _p->h;
775
776#define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
777    ZEND_HASH_FOREACH(ht, 0); \
778    _key = _p->key;
779
780#define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
781    ZEND_HASH_FOREACH(ht, 0); \
782    _h = _p->h; \
783    _key = _p->key;
784
785#define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
786    ZEND_HASH_FOREACH(ht, 0); \
787    _h = _p->h; \
788    _val = _z;
789
790#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
791    ZEND_HASH_FOREACH(ht, 0); \
792    _key = _p->key; \
793    _val = _z;
794
795#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
796    ZEND_HASH_FOREACH(ht, 0); \
797    _h = _p->h; \
798    _key = _p->key; \
799    _val = _z;
800
801#define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
802    ZEND_HASH_FOREACH(ht, 1); \
803    _key = _p->key; \
804    _val = _z;
805
806#define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
807    ZEND_HASH_FOREACH(ht, 1); \
808    _h = _p->h; \
809    _key = _p->key; \
810    _val = _z;
811
812#define ZEND_HASH_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
813    ZEND_HASH_FOREACH(ht, 0); \
814    _h = _p->h; \
815    _ptr = Z_PTR_P(_z);
816
817#define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
818    ZEND_HASH_FOREACH(ht, 0); \
819    _key = _p->key; \
820    _ptr = Z_PTR_P(_z);
821
822#define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
823    ZEND_HASH_FOREACH(ht, 0); \
824    _h = _p->h; \
825    _key = _p->key; \
826    _ptr = Z_PTR_P(_z);
827
828#define ZEND_HASH_REVERSE_FOREACH_BUCKET(ht, _bucket) \
829    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
830    _bucket = _p;
831
832#define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
833    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
834    _val = _z;
835
836#define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
837    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
838    _ptr = Z_PTR_P(_z);
839
840#define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
841    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
842    _val = _z;
843
844#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
845    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
846    _h = _p->h; \
847    _key = _p->key; \
848    _val = _z;
849
850#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
851    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
852    _h = _p->h; \
853    _key = _p->key; \
854    _val = _z;
855
856#define ZEND_HASH_APPLY_PROTECTION(ht) \
857    ((ht)->u.flags & HASH_FLAG_APPLY_PROTECTION)
858
859#define ZEND_HASH_APPLY_SHIFT         8
860#define ZEND_HASH_APPLY_COUNT_MASK    0xff00
861#define ZEND_HASH_GET_APPLY_COUNT(ht) (((ht)->u.flags & ZEND_HASH_APPLY_COUNT_MASK) >> ZEND_HASH_APPLY_SHIFT)
862#define ZEND_HASH_INC_APPLY_COUNT(ht) ((ht)->u.flags += (1 << ZEND_HASH_APPLY_SHIFT))
863#define ZEND_HASH_DEC_APPLY_COUNT(ht) ((ht)->u.flags -= (1 << ZEND_HASH_APPLY_SHIFT))
864
865
866/* The following macros are useful to insert a sequence of new elements
867 * of packed array. They may be use insted of series of
868 * zend_hash_next_index_insert_new()
869 * (HashTable must have enough free buckets).
870 */
871#define ZEND_HASH_FILL_PACKED(ht) do { \
872        HashTable *__fill_ht = (ht); \
873        Bucket *__fill_bkt = __fill_ht->arData + __fill_ht->nNumUsed; \
874        uint32_t __fill_idx = __fill_ht->nNumUsed; \
875        ZEND_ASSERT(__fill_ht->u.flags & HASH_FLAG_PACKED);
876
877#define ZEND_HASH_FILL_ADD(_val) do { \
878        ZVAL_COPY_VALUE(&__fill_bkt->val, _val); \
879        __fill_bkt->h = (__fill_idx); \
880        __fill_bkt->key = NULL; \
881        __fill_bkt++; \
882        __fill_idx++; \
883    } while (0)
884
885#define ZEND_HASH_FILL_END() \
886        __fill_ht->nNumUsed = __fill_idx; \
887        __fill_ht->nNumOfElements = __fill_idx; \
888        __fill_ht->nNextFreeElement = __fill_idx; \
889        __fill_ht->nInternalPointer = __fill_idx ? 0 : HT_INVALID_IDX; \
890    } while (0)
891
892static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *key, zval *zv)
893{
894    uint32_t idx = ht->nNumUsed++;
895    uint32_t nIndex;
896    Bucket *p = ht->arData + idx;
897
898    ZVAL_COPY_VALUE(&p->val, zv);
899    if (!ZSTR_IS_INTERNED(key)) {
900        ht->u.flags &= ~HASH_FLAG_STATIC_KEYS;
901        zend_string_addref(key);
902        zend_string_hash_val(key);
903    }
904    p->key = key;
905    p->h = ZSTR_H(key);
906    nIndex = (uint32_t)p->h | ht->nTableMask;
907    Z_NEXT(p->val) = HT_HASH(ht, nIndex);
908    HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
909    ht->nNumUsed = idx + 1;
910    ht->nNumOfElements++;
911    return &p->val;
912}
913
914static zend_always_inline zval *_zend_hash_append_ptr(HashTable *ht, zend_string *key, void *ptr)
915{
916    uint32_t idx = ht->nNumUsed++;
917    uint32_t nIndex;
918    Bucket *p = ht->arData + idx;
919
920    ZVAL_PTR(&p->val, ptr);
921    if (!ZSTR_IS_INTERNED(key)) {
922        ht->u.flags &= ~HASH_FLAG_STATIC_KEYS;
923        zend_string_addref(key);
924        zend_string_hash_val(key);
925    }
926    p->key = key;
927    p->h = ZSTR_H(key);
928    nIndex = (uint32_t)p->h | ht->nTableMask;
929    Z_NEXT(p->val) = HT_HASH(ht, nIndex);
930    HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
931    ht->nNumUsed = idx + 1;
932    ht->nNumOfElements++;
933    return &p->val;
934}
935
936static zend_always_inline void _zend_hash_append_ind(HashTable *ht, zend_string *key, zval *ptr)
937{
938    uint32_t idx = ht->nNumUsed++;
939    uint32_t nIndex;
940    Bucket *p = ht->arData + idx;
941
942    ZVAL_INDIRECT(&p->val, ptr);
943    if (!ZSTR_IS_INTERNED(key)) {
944        ht->u.flags &= ~HASH_FLAG_STATIC_KEYS;
945        zend_string_addref(key);
946        zend_string_hash_val(key);
947    }
948    p->key = key;
949    p->h = ZSTR_H(key);
950    nIndex = (uint32_t)p->h | ht->nTableMask;
951    Z_NEXT(p->val) = HT_HASH(ht, nIndex);
952    HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
953    ht->nNumUsed = idx + 1;
954    ht->nNumOfElements++;
955}
956
957#endif                          /* ZEND_HASH_H */
958
959/*
960 * Local variables:
961 * tab-width: 4
962 * c-basic-offset: 4
963 * indent-tabs-mode: t
964 * End:
965 */
966