1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 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   +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22#ifndef ZEND_HASH_H
23#define ZEND_HASH_H
24
25#include <sys/types.h>
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#define HASH_KEY_NON_EXISTANT HASH_KEY_NON_EXISTENT /* Keeping old define (with typo) for backward compatibility */
32
33#define HASH_UPDATE             (1<<0)
34#define HASH_ADD                (1<<1)
35#define HASH_NEXT_INSERT        (1<<2)
36#define HASH_UPDATE_INDIRECT    (1<<3)
37#define HASH_ADD_NEW            (1<<4)
38
39#define INVALID_IDX ((uint)-1)
40
41#define HASH_FLAG_PERSISTENT       (1<<0)
42#define HASH_FLAG_APPLY_PROTECTION (1<<1)
43#define HASH_FLAG_PACKED           (1<<2)
44
45#define HASH_MASK_CONSISTENCY      0x60
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
54typedef uint HashPosition;
55
56BEGIN_EXTERN_C()
57
58/* startup/shutdown */
59ZEND_API void _zend_hash_init(HashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC);
60ZEND_API void _zend_hash_init_ex(HashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent, zend_bool bApplyProtection ZEND_FILE_LINE_DC);
61ZEND_API void zend_hash_destroy(HashTable *ht);
62ZEND_API void zend_hash_clean(HashTable *ht);
63#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)                       _zend_hash_init((ht), (nSize), (pDestructor), (persistent) ZEND_FILE_LINE_CC)
64#define zend_hash_init_ex(ht, nSize, pHashFunction, pDestructor, persistent, bApplyProtection)      _zend_hash_init_ex((ht), (nSize), (pDestructor), (persistent), (bApplyProtection) ZEND_FILE_LINE_CC)
65
66ZEND_API void zend_hash_real_init(HashTable *ht, int packed);
67ZEND_API void zend_hash_packed_to_hash(HashTable *ht);
68ZEND_API void zend_hash_to_packed(HashTable *ht);
69
70/* additions/updates/changes */
71ZEND_API zval *_zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, int flag ZEND_FILE_LINE_DC);
72ZEND_API zval *_zend_hash_update(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
73ZEND_API zval *_zend_hash_update_ind(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
74ZEND_API zval *_zend_hash_add(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
75ZEND_API zval *_zend_hash_add_new(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
76
77#define zend_hash_update(ht, key, pData) \
78        _zend_hash_update(ht, key, pData ZEND_FILE_LINE_CC)
79#define zend_hash_update_ind(ht, key, pData) \
80        _zend_hash_update_ind(ht, key, pData ZEND_FILE_LINE_CC)
81#define zend_hash_add(ht, key, pData) \
82        _zend_hash_add(ht, key, pData ZEND_FILE_LINE_CC)
83#define zend_hash_add_new(ht, key, pData) \
84        _zend_hash_add_new(ht, key, pData ZEND_FILE_LINE_CC)
85
86ZEND_API zval *_zend_hash_str_add_or_update(HashTable *ht, const char *key, int len, zval *pData, int flag ZEND_FILE_LINE_DC);
87ZEND_API zval *_zend_hash_str_update(HashTable *ht, const char *key, int len, zval *pData ZEND_FILE_LINE_DC);
88ZEND_API zval *_zend_hash_str_update_ind(HashTable *ht, const char *key, int len, zval *pData ZEND_FILE_LINE_DC);
89ZEND_API zval *_zend_hash_str_add(HashTable *ht, const char *key, int len, zval *pData ZEND_FILE_LINE_DC);
90ZEND_API zval *_zend_hash_str_add_new(HashTable *ht, const char *key, int len, zval *pData ZEND_FILE_LINE_DC);
91
92#define zend_hash_str_update(ht, key, len, pData) \
93        _zend_hash_str_update(ht, key, len, pData ZEND_FILE_LINE_CC)
94#define zend_hash_str_update_ind(ht, key, len, pData) \
95        _zend_hash_str_update_ind(ht, key, len, pData ZEND_FILE_LINE_CC)
96#define zend_hash_str_add(ht, key, len, pData) \
97        _zend_hash_str_add(ht, key, len, pData ZEND_FILE_LINE_CC)
98#define zend_hash_str_add_new(ht, key, len, pData) \
99        _zend_hash_str_add_new(ht, key, len, pData ZEND_FILE_LINE_CC)
100
101ZEND_API zval *_zend_hash_index_update_or_next_insert(HashTable *ht, zend_ulong h, zval *pData, int flag ZEND_FILE_LINE_DC);
102ZEND_API zval *_zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
103ZEND_API zval *_zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
104ZEND_API zval *_zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
105ZEND_API zval *_zend_hash_next_index_insert(HashTable *ht, zval *pData ZEND_FILE_LINE_DC);
106ZEND_API zval *_zend_hash_next_index_insert_new(HashTable *ht, zval *pData ZEND_FILE_LINE_DC);
107
108#define zend_hash_index_add(ht, h, pData) \
109        _zend_hash_index_add(ht, h, pData ZEND_FILE_LINE_CC)
110#define zend_hash_index_add_new(ht, h, pData) \
111        _zend_hash_index_add_new(ht, h, pData ZEND_FILE_LINE_CC)
112#define zend_hash_index_update(ht, h, pData) \
113        _zend_hash_index_update(ht, h, pData ZEND_FILE_LINE_CC)
114#define zend_hash_next_index_insert(ht, pData) \
115        _zend_hash_next_index_insert(ht, pData ZEND_FILE_LINE_CC)
116#define zend_hash_next_index_insert_new(ht, pData) \
117        _zend_hash_next_index_insert_new(ht, pData ZEND_FILE_LINE_CC)
118
119ZEND_API zval *zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h);
120ZEND_API zval *zend_hash_add_empty_element(HashTable *ht, zend_string *key);
121ZEND_API zval *zend_hash_str_add_empty_element(HashTable *ht, const char *key, int len);
122
123#define ZEND_HASH_APPLY_KEEP                0
124#define ZEND_HASH_APPLY_REMOVE              1<<0
125#define ZEND_HASH_APPLY_STOP                1<<1
126
127typedef int (*apply_func_t)(zval *pDest TSRMLS_DC);
128typedef int (*apply_func_arg_t)(zval *pDest, void *argument TSRMLS_DC);
129typedef int (*apply_func_args_t)(zval *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
130
131ZEND_API void zend_hash_graceful_destroy(HashTable *ht);
132ZEND_API void zend_hash_graceful_reverse_destroy(HashTable *ht);
133ZEND_API void zend_hash_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
134ZEND_API void zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void * TSRMLS_DC);
135ZEND_API void zend_hash_apply_with_arguments(HashTable *ht TSRMLS_DC, apply_func_args_t apply_func, int, ...);
136
137/* This function should be used with special care (in other words,
138 * it should usually not be used).  When used with the ZEND_HASH_APPLY_STOP
139 * return value, it assumes things about the order of the elements in the hash.
140 * Also, it does not provide the same kind of reentrancy protection that
141 * the standard apply functions do.
142 */
143ZEND_API void zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
144
145
146/* Deletes */
147ZEND_API int zend_hash_del(HashTable *ht, zend_string *key);
148ZEND_API int zend_hash_del_ind(HashTable *ht, zend_string *key);
149ZEND_API int zend_hash_str_del(HashTable *ht, const char *key, int len);
150ZEND_API int zend_hash_str_del_ind(HashTable *ht, const char *key, int len);
151ZEND_API int zend_hash_index_del(HashTable *ht, zend_ulong h);
152
153/* Data retreival */
154ZEND_API zval *zend_hash_find(const HashTable *ht, zend_string *key);
155ZEND_API zval *zend_hash_str_find(const HashTable *ht, const char *key, int len);
156ZEND_API zval *zend_hash_index_find(const HashTable *ht, zend_ulong h);
157
158/* Misc */
159ZEND_API int zend_hash_exists(const HashTable *ht, zend_string *key);
160ZEND_API int zend_hash_str_exists(const HashTable *ht, const char *str, int len);
161ZEND_API int 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_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
167ZEND_API int zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
168ZEND_API int zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, zend_bool duplicate, HashPosition *pos);
169ZEND_API void zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos);
170ZEND_API int zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
171ZEND_API zval *zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos);
172ZEND_API void zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
173ZEND_API void zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
174
175typedef struct _HashPointer {
176    HashPosition pos;
177    HashTable *ht;
178    zend_ulong h;
179} HashPointer;
180
181ZEND_API int zend_hash_get_pointer(const HashTable *ht, HashPointer *ptr);
182ZEND_API int zend_hash_set_pointer(HashTable *ht, const HashPointer *ptr);
183
184#define zend_hash_has_more_elements(ht) \
185    zend_hash_has_more_elements_ex(ht, &(ht)->nInternalPointer)
186#define zend_hash_move_forward(ht) \
187    zend_hash_move_forward_ex(ht, &(ht)->nInternalPointer)
188#define zend_hash_move_backwards(ht) \
189    zend_hash_move_backwards_ex(ht, &(ht)->nInternalPointer)
190#define zend_hash_get_current_key(ht, str_index, num_index, duplicate) \
191    zend_hash_get_current_key_ex(ht, str_index, num_index, duplicate, &(ht)->nInternalPointer)
192#define zend_hash_get_current_key_zval(ht, key) \
193    zend_hash_get_current_key_zval_ex(ht, key, &(ht)->nInternalPointer)
194#define zend_hash_get_current_key_type(ht) \
195    zend_hash_get_current_key_type_ex(ht, &(ht)->nInternalPointer)
196#define zend_hash_get_current_data(ht) \
197    zend_hash_get_current_data_ex(ht, &(ht)->nInternalPointer)
198#define zend_hash_internal_pointer_reset(ht) \
199    zend_hash_internal_pointer_reset_ex(ht, &(ht)->nInternalPointer)
200#define zend_hash_internal_pointer_end(ht) \
201    zend_hash_internal_pointer_end_ex(ht, &(ht)->nInternalPointer)
202
203/* Copying, merging and sorting */
204ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor);
205ZEND_API void _zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite ZEND_FILE_LINE_DC);
206ZEND_API void zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
207ZEND_API int zend_hash_sort(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC);
208ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC);
209ZEND_API zval *zend_hash_minmax(const HashTable *ht, compare_func_t compar, int flag TSRMLS_DC);
210
211#define zend_hash_merge(target, source, pCopyConstructor, overwrite)                    \
212    _zend_hash_merge(target, source, pCopyConstructor, overwrite ZEND_FILE_LINE_CC)
213
214#define zend_hash_num_elements(ht) \
215    (ht)->nNumOfElements
216
217#define zend_hash_next_free_element(ht) \
218    (ht)->nNextFreeElement
219
220ZEND_API int zend_hash_rehash(HashTable *ht);
221
222ZEND_API void zend_array_dup(HashTable *target, HashTable *source);
223
224#if ZEND_DEBUG
225/* debug */
226void zend_hash_display_pListTail(const HashTable *ht);
227void zend_hash_display(const HashTable *ht);
228#endif
229
230END_EXTERN_C()
231
232#define ZEND_INIT_SYMTABLE(ht)                              \
233    ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
234
235#define ZEND_INIT_SYMTABLE_EX(ht, n, persistent)            \
236    zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
237
238static inline int _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
239{
240    register const char *tmp = key;
241    const char *end;
242
243    if (*tmp > '9') {
244        return 0;
245    } else if (*tmp < '0') {
246        if (*tmp != '-') {
247            return 0;
248        }
249        tmp++;
250        if (*tmp > '9' || *tmp < '0') {
251            return 0;
252        }
253    }
254
255    /* possibly a numeric index */
256    end = key + length;
257
258    if ((*end != '\0') /* not a null terminated string */
259     || (*tmp == '0' && length > 1) /* numbers with leading zeros */
260     || (end - tmp > MAX_LENGTH_OF_LONG - 1) /* number too long */
261     || (SIZEOF_ZEND_LONG == 4 &&
262         end - tmp == MAX_LENGTH_OF_LONG - 1 &&
263         *tmp > '2')) { /* overflow */
264        return 0;
265    }
266    *idx = (*tmp - '0');
267    while (1) {
268        ++tmp;
269        if (tmp == end) {
270            if (*key == '-') {
271                if (*idx-1 > ZEND_LONG_MAX) { /* overflow */
272                    return 0;
273                }
274                *idx = 0 - *idx;
275            } else if (*idx > ZEND_LONG_MAX) { /* overflow */
276                return 0;
277            }
278            return 1;
279        }
280        if (*tmp <= '9' && *tmp >= '0') {
281            *idx = (*idx * 10) + (*tmp - '0');
282        } else {
283            return 0;
284        }
285    }
286}
287
288#define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
289    _zend_handle_numeric_str(key, length, &idx)
290
291#define ZEND_HANDLE_NUMERIC(key, idx) \
292    _zend_handle_numeric_str((key)->val, (key)->len, &idx)
293
294
295static inline zval *zend_hash_find_ind(const HashTable *ht, zend_string *key)
296{
297    zval *zv;
298
299    zv = zend_hash_find(ht, key);
300    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ? Z_INDIRECT_P(zv) : zv;
301}
302
303
304static inline int zend_hash_exists_ind(const HashTable *ht, zend_string *key)
305{
306    zval *zv;
307
308    zv = zend_hash_find(ht, key);
309    return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
310            Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
311}
312
313
314static inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, int len)
315{
316    zval *zv;
317
318    zv = zend_hash_str_find(ht, str, len);
319    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ? Z_INDIRECT_P(zv) : zv;
320}
321
322
323static inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
324{
325    zend_ulong idx;
326
327    if (ZEND_HANDLE_NUMERIC(key, idx)) {
328        return zend_hash_index_update(ht, idx, pData);
329    } else {
330        return zend_hash_update(ht, key, pData);
331    }
332}
333
334
335static inline zval *zend_symtable_update_ind(HashTable *ht, zend_string *key, zval *pData)
336{
337    zend_ulong idx;
338
339    if (ZEND_HANDLE_NUMERIC(key, idx)) {
340        return zend_hash_index_update(ht, idx, pData);
341    } else {
342        return zend_hash_update_ind(ht, key, pData);
343    }
344}
345
346
347static inline int zend_symtable_del(HashTable *ht, zend_string *key)
348{
349    zend_ulong idx;
350
351    if (ZEND_HANDLE_NUMERIC(key, idx)) {
352        return zend_hash_index_del(ht, idx);
353    } else {
354        return zend_hash_del(ht, key);
355    }
356}
357
358
359static inline int zend_symtable_del_ind(HashTable *ht, zend_string *key)
360{
361    zend_ulong idx;
362
363    if (ZEND_HANDLE_NUMERIC(key, idx)) {
364        return zend_hash_index_del(ht, idx);
365    } else {
366        return zend_hash_del_ind(ht, key);
367    }
368}
369
370
371static inline zval *zend_symtable_find(const HashTable *ht, zend_string *key)
372{
373    zend_ulong idx;
374
375    if (ZEND_HANDLE_NUMERIC(key, idx)) {
376        return zend_hash_index_find(ht, idx);
377    } else {
378        return zend_hash_find(ht, key);
379    }
380}
381
382
383static inline zval *zend_symtable_find_ind(const HashTable *ht, zend_string *key)
384{
385    zend_ulong idx;
386
387    if (ZEND_HANDLE_NUMERIC(key, idx)) {
388        return zend_hash_index_find(ht, idx);
389    } else {
390        return zend_hash_find_ind(ht, key);
391    }
392}
393
394
395static inline int zend_symtable_exists(HashTable *ht, zend_string *key)
396{
397    zend_ulong idx;
398
399    if (ZEND_HANDLE_NUMERIC(key, idx)) {
400        return zend_hash_index_exists(ht, idx);
401    } else {
402        return zend_hash_exists(ht, key);
403    }
404}
405
406
407static inline zval *zend_symtable_str_update(HashTable *ht, const char *str, int len, zval *pData)
408{
409    zend_ulong idx;
410
411    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
412        return zend_hash_index_update(ht, idx, pData);
413    } else {
414        return zend_hash_str_update(ht, str, len, pData);
415    }
416}
417
418
419static inline zval *zend_symtable_str_update_ind(HashTable *ht, const char *str, int len, zval *pData)
420{
421    zend_ulong idx;
422
423    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
424        return zend_hash_index_update(ht, idx, pData);
425    } else {
426        return zend_hash_str_update_ind(ht, str, len, pData);
427    }
428}
429
430
431static inline int zend_symtable_str_del(HashTable *ht, const char *str, int len)
432{
433    zend_ulong idx;
434
435    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
436        return zend_hash_index_del(ht, idx);
437    } else {
438        return zend_hash_str_del(ht, str, len);
439    }
440}
441
442
443static inline int zend_symtable_str_del_ind(HashTable *ht, const char *str, int len)
444{
445    zend_ulong idx;
446
447    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
448        return zend_hash_index_del(ht, idx);
449    } else {
450        return zend_hash_str_del_ind(ht, str, len);
451    }
452}
453
454
455static inline zval *zend_symtable_str_find(HashTable *ht, const char *str, int len)
456{
457    zend_ulong idx;
458
459    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
460        return zend_hash_index_find(ht, idx);
461    } else {
462        return zend_hash_str_find(ht, str, len);
463    }
464}
465
466
467static inline int zend_symtable_str_exists(HashTable *ht, const char *str, int len)
468{
469    zend_ulong idx;
470
471    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
472        return zend_hash_index_exists(ht, idx);
473    } else {
474        return zend_hash_str_exists(ht, str, len);
475    }
476}
477
478static inline void *zend_hash_add_ptr(HashTable *ht, zend_string *key, void *pData)
479{
480    zval tmp, *zv;
481
482    ZVAL_PTR(&tmp, pData);
483    zv = zend_hash_add(ht, key, &tmp);
484    return zv ? Z_PTR_P(zv) : NULL;
485}
486
487static inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
488{
489    zval tmp, *zv;
490
491    ZVAL_PTR(&tmp, pData);
492    zv = zend_hash_add_new(ht, key, &tmp);
493    return zv ? Z_PTR_P(zv) : NULL;
494}
495
496static inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, int len, void *pData)
497{
498    zval tmp, *zv;
499
500    ZVAL_PTR(&tmp, pData);
501    zv = zend_hash_str_add(ht, str, len, &tmp);
502    return zv ? Z_PTR_P(zv) : NULL;
503}
504
505static inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
506{
507    zval tmp, *zv;
508
509    ZVAL_PTR(&tmp, pData);
510    zv = zend_hash_update(ht, key, &tmp);
511    return zv ? Z_PTR_P(zv) : NULL;
512}
513
514static inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, int len, void *pData)
515{
516    zval tmp, *zv;
517
518    ZVAL_PTR(&tmp, pData);
519    zv = zend_hash_str_update(ht, str, len, &tmp);
520    return zv ? Z_PTR_P(zv) : NULL;
521}
522
523static inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
524{
525    zval tmp, *zv;
526
527    ZVAL_PTR(&tmp, NULL);
528    if ((zv = zend_hash_add(ht, key, &tmp))) {
529        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
530        memcpy(Z_PTR_P(zv), pData, size);
531        return Z_PTR_P(zv);
532    }
533    return NULL;
534}
535
536static inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, int len, void *pData, size_t size)
537{
538    zval tmp, *zv;
539
540    ZVAL_PTR(&tmp, NULL);
541    if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
542        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
543        memcpy(Z_PTR_P(zv), pData, size);
544        return Z_PTR_P(zv);
545    }
546    return NULL;
547}
548
549static inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
550{
551    void *p;
552
553    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
554    memcpy(p, pData, size);
555    return zend_hash_update_ptr(ht, key, p);
556}
557
558static inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, int len, void *pData, size_t size)
559{
560    void *p;
561
562    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
563    memcpy(p, pData, size);
564    return zend_hash_str_update_ptr(ht, str, len, p);
565}
566
567static inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
568{
569    zval tmp, *zv;
570
571    ZVAL_PTR(&tmp, pData);
572    zv = zend_hash_index_update(ht, h, &tmp);
573    return zv ? Z_PTR_P(zv) : NULL;
574}
575
576static inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
577{
578    zval tmp, *zv;
579
580    ZVAL_PTR(&tmp, pData);
581    zv = zend_hash_next_index_insert(ht, &tmp);
582    return zv ? Z_PTR_P(zv) : NULL;
583}
584
585static inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
586{
587    void *p;
588
589    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
590    memcpy(p, pData, size);
591    return zend_hash_index_update_ptr(ht, h, p);
592}
593
594static inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
595{
596    zval tmp, *zv;
597
598    ZVAL_PTR(&tmp, NULL);
599    if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
600        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
601        memcpy(Z_PTR_P(zv), pData, size);
602        return Z_PTR_P(zv);
603    }
604    return NULL;
605}
606
607static inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
608{
609    zval *zv;
610
611    zv = zend_hash_find(ht, key);
612    return zv ? Z_PTR_P(zv) : NULL;
613}
614
615static inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, int len)
616{
617    zval *zv;
618
619    zv = zend_hash_str_find(ht, str, len);
620    return zv ? Z_PTR_P(zv) : NULL;
621}
622
623static inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
624{
625    zval *zv;
626
627    zv = zend_hash_index_find(ht, h);
628    return zv ? Z_PTR_P(zv) : NULL;
629}
630
631static inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, int len)
632{
633    zend_ulong idx;
634
635    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
636        return zend_hash_index_find_ptr(ht, idx);
637    } else {
638        return zend_hash_str_find_ptr(ht, str, len);
639    }
640}
641
642static inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
643{
644    zval *zv;
645
646    zv = zend_hash_get_current_data_ex(ht, pos);
647    return zv ? Z_PTR_P(zv) : NULL;
648}
649
650#define zend_hash_get_current_data_ptr(ht) \
651    zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
652
653#define ZEND_HASH_FOREACH(_ht, indirect) do { \
654        uint _idx; \
655        for (_idx = 0; _idx < (_ht)->nNumUsed; _idx++) { \
656            Bucket *_p = (_ht)->arData + _idx; \
657            zval *_z = &_p->val; \
658            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
659                _z = Z_INDIRECT_P(_z); \
660            } \
661            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
662
663#define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
664        uint _idx; \
665        for (_idx = (_ht)->nNumUsed; _idx > 0; _idx--) { \
666            Bucket *_p = (_ht)->arData + _idx - 1; \
667            zval *_z = &_p->val; \
668            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
669                _z = Z_INDIRECT_P(_z); \
670            } \
671            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
672
673#define ZEND_HASH_FOREACH_END() \
674        } \
675    } while (0)
676
677#define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
678    ZEND_HASH_FOREACH(ht, 0); \
679    _bucket = _p;
680
681#define ZEND_HASH_FOREACH_VAL(ht, _val) \
682    ZEND_HASH_FOREACH(ht, 0); \
683    _val = _z;
684
685#define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
686    ZEND_HASH_FOREACH(ht, 1); \
687    _val = _z;
688
689#define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
690    ZEND_HASH_FOREACH(ht, 0); \
691    _ptr = Z_PTR_P(_z);
692
693#define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
694    ZEND_HASH_FOREACH(ht, 0); \
695    _h = _p->h;
696
697#define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
698    ZEND_HASH_FOREACH(ht, 0); \
699    _key = _p->key;
700
701#define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
702    ZEND_HASH_FOREACH(ht, 0); \
703    _h = _p->h; \
704    _key = _p->key;
705
706#define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
707    ZEND_HASH_FOREACH(ht, 0); \
708    _h = _p->h; \
709    _val = _z;
710
711#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
712    ZEND_HASH_FOREACH(ht, 0); \
713    _key = _p->key; \
714    _val = _z;
715
716#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
717    ZEND_HASH_FOREACH(ht, 0); \
718    _h = _p->h; \
719    _key = _p->key; \
720    _val = _z;
721
722#define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
723    ZEND_HASH_FOREACH(ht, 1); \
724    _key = _p->key; \
725    _val = _z;
726
727#define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
728    ZEND_HASH_FOREACH(ht, 1); \
729    _h = _p->h; \
730    _key = _p->key; \
731    _val = _z;
732
733#define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
734    ZEND_HASH_FOREACH(ht, 0); \
735    _key = _p->key; \
736    _ptr = Z_PTR_P(_z);
737
738#define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
739    ZEND_HASH_FOREACH(ht, 0); \
740    _h = _p->h; \
741    _key = _p->key; \
742    _ptr = Z_PTR_P(_z);
743
744#define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
745    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
746    _val = _z;
747
748#define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
749    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
750    _ptr = Z_PTR_P(_z);
751
752#define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
753    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
754    _val = _z;
755
756#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
757    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
758    _h = _p->h; \
759    _key = _p->key; \
760    _val = _z;
761
762#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
763    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
764    _h = _p->h; \
765    _key = _p->key; \
766    _val = _z;
767
768#define ZEND_HASH_APPLY_PROTECTION(ht) \
769    ((ht)->u.flags & HASH_FLAG_APPLY_PROTECTION)
770
771#define ZEND_HASH_APPLY_SHIFT 8
772#define ZEND_HASH_GET_APPLY_COUNT(ht) ((ht)->u.flags >> ZEND_HASH_APPLY_SHIFT)
773#define ZEND_HASH_INC_APPLY_COUNT(ht) ((ht)->u.flags += (1 << ZEND_HASH_APPLY_SHIFT))
774#define ZEND_HASH_DEC_APPLY_COUNT(ht) ((ht)->u.flags -= (1 << ZEND_HASH_APPLY_SHIFT))
775
776#endif                          /* ZEND_HASH_H */
777
778/*
779 * Local variables:
780 * tab-width: 4
781 * c-basic-offset: 4
782 * indent-tabs-mode: t
783 * End:
784 */
785