Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 374 lines (308 sloc) 8.963 kb
5c2d8d5 initial import of php-judy in pecl
nicolas authored
1 /*
2 +----------------------------------------------------------------------+
3 | PHP Judy |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2010 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Nicolas Brousse <nicolas@brousse.info> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include "php_judy.h"
20 #include "judy_iterator.h"
21
22 /* {{{ judy iterator handlers table
68f4e8a spaces -> tabs, that's the standard
nicolas authored
23 */
5c2d8d5 initial import of php-judy in pecl
nicolas authored
24 zend_object_iterator_funcs judy_iterator_funcs = {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
25 judy_iterator_dtor,
26 judy_iterator_valid,
27 judy_iterator_current_data,
28 judy_iterator_current_key,
29 judy_iterator_move_forward,
30 judy_iterator_rewind,
31 NULL /* invalidate current */
5c2d8d5 initial import of php-judy in pecl
nicolas authored
32 };
33 /* }}} */
34
35 /* {{{ judy_get_iterator
68f4e8a spaces -> tabs, that's the standard
nicolas authored
36 */
5c2d8d5 initial import of php-judy in pecl
nicolas authored
37 zend_object_iterator *judy_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
38 {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
39 judy_iterator *it = emalloc(sizeof(judy_iterator));
5c2d8d5 initial import of php-judy in pecl
nicolas authored
40
68f4e8a spaces -> tabs, that's the standard
nicolas authored
41 if (by_ref) {
42 zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
43 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
44
68f4e8a spaces -> tabs, that's the standard
nicolas authored
45 Z_ADDREF_P(object);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
46
68f4e8a spaces -> tabs, that's the standard
nicolas authored
47 it->intern.data = (void*) object;
48 it->intern.funcs = &judy_iterator_funcs;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
49
68f4e8a spaces -> tabs, that's the standard
nicolas authored
50 MAKE_STD_ZVAL(it->key);
51 ZVAL_NULL(it->key);
52 it->data = NULL;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
53
68f4e8a spaces -> tabs, that's the standard
nicolas authored
54 return (zend_object_iterator*) it;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
55 }
56 /* }}} */
57
58 /* {{{ judy_iterator_data_dtor
68f4e8a spaces -> tabs, that's the standard
nicolas authored
59 */
fa5ed45 fix some segfault in iterator. relates to github issue #2
nicolas authored
60 void judy_iterator_data_dtor(judy_iterator *it)
5c2d8d5 initial import of php-judy in pecl
nicolas authored
61 {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
62 if (it->key) {
63 zval_ptr_dtor(&it->key);
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
64 it->key = NULL;
68f4e8a spaces -> tabs, that's the standard
nicolas authored
65 }
fa5ed45 fix some segfault in iterator. relates to github issue #2
nicolas authored
66
68f4e8a spaces -> tabs, that's the standard
nicolas authored
67 if (it->data) {
68 zval_ptr_dtor(&it->data);
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
69 it->data = NULL;
68f4e8a spaces -> tabs, that's the standard
nicolas authored
70 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
71 }
72 /* }}} */
73
74 /* {{{ judy_iterator_dtor
68f4e8a spaces -> tabs, that's the standard
nicolas authored
75 */
5c2d8d5 initial import of php-judy in pecl
nicolas authored
76 void judy_iterator_dtor(zend_object_iterator *iterator TSRMLS_DC)
77 {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
78 judy_iterator *it = (judy_iterator*) iterator;
79 zval *intern = (zval*) it->intern.data;
fa5ed45 fix some segfault in iterator. relates to github issue #2
nicolas authored
80
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
81 judy_iterator_data_dtor(it);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
82
68f4e8a spaces -> tabs, that's the standard
nicolas authored
83 Z_DELREF_P(intern);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
84
68f4e8a spaces -> tabs, that's the standard
nicolas authored
85 efree(it);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
86 }
87 /* }}} */
88
89 /* {{{ judy_iterator_valid
68f4e8a spaces -> tabs, that's the standard
nicolas authored
90 */
5c2d8d5 initial import of php-judy in pecl
nicolas authored
91 int judy_iterator_valid(zend_object_iterator *iterator TSRMLS_DC)
92 {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
93 JUDY_ITERATOR_GET_OBJECT
94
95 if (it->key == NULL && it->data == NULL) {
96 return FAILURE;
97 }
98
99 if (object->type == TYPE_BITSET) {
100 int Rc_int;
101
102 J1T(Rc_int, object->array, (Word_t) Z_LVAL_P(it->key));
103 if (Rc_int == 1) {
104 return SUCCESS;
105 }
106 } else if (object->type == TYPE_INT_TO_INT || object->type == TYPE_INT_TO_MIXED) {
107 Word_t *PValue;
108
109 JLG(PValue, object->array, (Word_t) Z_LVAL_P(it->key));
110 if (PValue != NULL && PValue != PJERR) {
111 return SUCCESS;
112 }
113 } else if (object->type == TYPE_STRING_TO_INT || object->type == TYPE_STRING_TO_MIXED) {
114 uint8_t key[PHP_JUDY_MAX_LENGTH];
115 Word_t *PValue;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
116
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
117 if (Z_TYPE_P(it->key) == IS_STRING) {
118 int key_len;
119 key_len = Z_STRLEN_P(it->key) >= PHP_JUDY_MAX_LENGTH ? PHP_JUDY_MAX_LENGTH - 1 : Z_STRLEN_P(it->key);
120 memcpy(key, Z_STRVAL_P(it->key), key_len);
121 key[key_len] = '\0';
122 } else if (Z_TYPE_P(it->key) == IS_NULL) {
123 key[0] = '\0';
124 } else {
125 return FAILURE;
126 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
127
68f4e8a spaces -> tabs, that's the standard
nicolas authored
128 JSLG(PValue, object->array, key);
129 if (PValue != NULL && PValue != PJERR) {
130 return SUCCESS;
131 }
132 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
133
68f4e8a spaces -> tabs, that's the standard
nicolas authored
134 return FAILURE;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
135 }
136 /* }}} */
137
138 /* {{{ judy_iterator_current_data
68f4e8a spaces -> tabs, that's the standard
nicolas authored
139 */
5c2d8d5 initial import of php-judy in pecl
nicolas authored
140 void judy_iterator_current_data(zend_object_iterator *iterator,
68f4e8a spaces -> tabs, that's the standard
nicolas authored
141 zval ***data TSRMLS_DC)
5c2d8d5 initial import of php-judy in pecl
nicolas authored
142 {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
143 judy_iterator *it = (judy_iterator*) iterator;
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
144 if (it->data) {
145 *data = &it->data;
146 } else {
147 *data = NULL;
148 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
149 }
150 /* }}} */
151
152 /* {{{ judy_iterator_current_key
68f4e8a spaces -> tabs, that's the standard
nicolas authored
153 */
bc4ba58 fix build with PHP 5.5 (https://wiki.php.net/rfc/foreach-non-scalar-keys...
remi authored
154 #if ZEND_MODULE_API_NO >= 20121212
155 void judy_iterator_current_key(zend_object_iterator *iterator, zval *key TSRMLS_DC)
156 {
157 judy_iterator *it = (judy_iterator*) iterator;
158
159 ZVAL_ZVAL(key, it->key, 1, 0);
160
161 #else
5c2d8d5 initial import of php-judy in pecl
nicolas authored
162 int judy_iterator_current_key(zend_object_iterator *iterator,
68f4e8a spaces -> tabs, that's the standard
nicolas authored
163 char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC)
5c2d8d5 initial import of php-judy in pecl
nicolas authored
164 {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
165 JUDY_ITERATOR_GET_OBJECT
166
167 *str_key = NULL;
168 *str_key_len = 0;
169 *int_key = 0;
170
171 if (Z_TYPE_P(it->key) == IS_LONG) {
172 *int_key = Z_LVAL_P(it->key);
173 return HASH_KEY_IS_LONG;
174 } else if(Z_TYPE_P(it->key) != IS_STRING) {
175 convert_to_string(it->key);
176 }
177
178 *str_key = estrndup(Z_STRVAL_P(it->key), Z_STRLEN_P(it->key));
179 *str_key_len = Z_STRLEN_P(it->key)+1;
180
181 return HASH_KEY_IS_STRING;
bc4ba58 fix build with PHP 5.5 (https://wiki.php.net/rfc/foreach-non-scalar-keys...
remi authored
182 #endif
5c2d8d5 initial import of php-judy in pecl
nicolas authored
183 }
184 /* }}} */
185
186 /* {{{ judy_iterator_move_forward
68f4e8a spaces -> tabs, that's the standard
nicolas authored
187 */
5c2d8d5 initial import of php-judy in pecl
nicolas authored
188 void judy_iterator_move_forward(zend_object_iterator *iterator TSRMLS_DC)
189 {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
190 JUDY_ITERATOR_GET_OBJECT
5c2d8d5 initial import of php-judy in pecl
nicolas authored
191
68f4e8a spaces -> tabs, that's the standard
nicolas authored
192 if (it->data) {
193 zval_ptr_dtor(&it->data);
194 it->data = NULL;
195 }
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
196
68f4e8a spaces -> tabs, that's the standard
nicolas authored
197 if (object->type == TYPE_BITSET) {
5c2d8d5 initial import of php-judy in pecl
nicolas authored
198
68f4e8a spaces -> tabs, that's the standard
nicolas authored
199 Word_t index;
200 int Rc_int;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
201
68f4e8a spaces -> tabs, that's the standard
nicolas authored
202 if (Z_TYPE_P(it->key) == IS_NULL) {
203 index = 0;
204 J1F(Rc_int, object->array, index);
205 } else {
206 index = Z_LVAL_P(it->key);
207 J1N(Rc_int, object->array, index);
208 }
fa5ed45 fix some segfault in iterator. relates to github issue #2
nicolas authored
209
68f4e8a spaces -> tabs, that's the standard
nicolas authored
210 if (Rc_int) {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
211 zval_dtor(it->key);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
212 ZVAL_LONG(it->key, index);
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
213 MAKE_STD_ZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
214 ZVAL_BOOL(it->data, 1);
215 } else {
216 judy_iterator_data_dtor(it);
217 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
218
68f4e8a spaces -> tabs, that's the standard
nicolas authored
219 } else if (object->type == TYPE_INT_TO_INT || object->type == TYPE_INT_TO_MIXED) {
5c2d8d5 initial import of php-judy in pecl
nicolas authored
220
68f4e8a spaces -> tabs, that's the standard
nicolas authored
221 Word_t index;
222 Pvoid_t *PValue = NULL;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
223
68f4e8a spaces -> tabs, that's the standard
nicolas authored
224 if (Z_TYPE_P(it->key) == IS_NULL) {
225 index = 0;
226 JLF(*PValue, object->array, index);
227 } else {
228 index = Z_LVAL_P(it->key);
229 JLN(PValue, object->array, index);
230 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
231
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
232 zval_dtor(it->key);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
233 ZVAL_LONG(it->key, index);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
234
68f4e8a spaces -> tabs, that's the standard
nicolas authored
235 JLG(PValue, object->array, index);
236 if (PValue != NULL && PValue != PJERR) {
237 if (object->type == TYPE_INT_TO_INT) {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
238 MAKE_STD_ZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
239 ZVAL_LONG(it->data, (long)*PValue);
240 } else {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
241 zval *value = *(zval **)PValue;
242
243 MAKE_STD_ZVAL(it->data);
244 *it->data = *value;
245 zval_copy_ctor(it->data);
246 INIT_PZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
247 }
248 } else {
249 judy_iterator_data_dtor(it);
250 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
251
68f4e8a spaces -> tabs, that's the standard
nicolas authored
252 } else if (object->type == TYPE_STRING_TO_INT || object->type == TYPE_STRING_TO_MIXED) {
5c2d8d5 initial import of php-judy in pecl
nicolas authored
253
68f4e8a spaces -> tabs, that's the standard
nicolas authored
254 uint8_t key[PHP_JUDY_MAX_LENGTH];
255 Pvoid_t *PValue;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
256
68f4e8a spaces -> tabs, that's the standard
nicolas authored
257 /* JudySL require null temrinated strings */
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
258 if (Z_TYPE_P(it->key) == IS_STRING) {
259 int key_len;
260 key_len = Z_STRLEN_P(it->key) >= PHP_JUDY_MAX_LENGTH ? PHP_JUDY_MAX_LENGTH - 1 : Z_STRLEN_P(it->key);
261 memcpy(key, Z_STRVAL_P(it->key), key_len);
262 key[key_len] = '\0';
5c2d8d5 initial import of php-judy in pecl
nicolas authored
263
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
264 JSLN(PValue, object->array, key);
265 } else {
266 key[0] = '\0';
267 JSLF(PValue, object->array, key);
268 }
269
68f4e8a spaces -> tabs, that's the standard
nicolas authored
270 if ((PValue != NULL && PValue != PJERR)) {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
271
272 zval_dtor(it->key);
273 ZVAL_STRING(it->key, (char *)key, 1);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
274
68f4e8a spaces -> tabs, that's the standard
nicolas authored
275 if (object->type == TYPE_STRING_TO_INT) {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
276 MAKE_STD_ZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
277 ZVAL_LONG(it->data, (long)*PValue);
278 } else {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
279 zval *value = *(zval **)PValue;
280
281 MAKE_STD_ZVAL(it->data);
282 *it->data = *value;
283 zval_copy_ctor(it->data);
284 INIT_PZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
285 }
286 } else {
287 judy_iterator_data_dtor(it);
288 }
289 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
290 }
291 /* }}} */
292
293 /* {{{ judy_iterator_rewind
68f4e8a spaces -> tabs, that's the standard
nicolas authored
294 */
5c2d8d5 initial import of php-judy in pecl
nicolas authored
295 void judy_iterator_rewind(zend_object_iterator *iterator TSRMLS_DC)
296 {
68f4e8a spaces -> tabs, that's the standard
nicolas authored
297 JUDY_ITERATOR_GET_OBJECT
5c2d8d5 initial import of php-judy in pecl
nicolas authored
298
68f4e8a spaces -> tabs, that's the standard
nicolas authored
299 if (it->data) {
300 zval_ptr_dtor(&it->data);
301 it->data = NULL;
302 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
303
68f4e8a spaces -> tabs, that's the standard
nicolas authored
304 if (object->type == TYPE_BITSET) {
305 Word_t index = 0;
306 int Rc_int;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
307
68f4e8a spaces -> tabs, that's the standard
nicolas authored
308 J1F(Rc_int, object->array, index);
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
309 zval_dtor(it->key);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
310 ZVAL_LONG(it->key, index);
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
311 MAKE_STD_ZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
312 ZVAL_BOOL(it->data, 1);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
313
68f4e8a spaces -> tabs, that's the standard
nicolas authored
314 } else if (object->type == TYPE_INT_TO_INT || object->type == TYPE_INT_TO_MIXED) {
5c2d8d5 initial import of php-judy in pecl
nicolas authored
315
68f4e8a spaces -> tabs, that's the standard
nicolas authored
316 Word_t index = 0;
317 Pvoid_t *PValue = NULL;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
318
68f4e8a spaces -> tabs, that's the standard
nicolas authored
319 JLF(PValue, object->array, index);
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
320 zval_dtor(it->key);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
321 ZVAL_LONG(it->key, index);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
322
68f4e8a spaces -> tabs, that's the standard
nicolas authored
323 JLG(PValue, object->array, index);
324 if (PValue != NULL && PValue != PJERR) {
325 if (object->type == TYPE_INT_TO_INT) {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
326 MAKE_STD_ZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
327 ZVAL_LONG(it->data, (long)*PValue);
328 } else {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
329 zval *value = *(zval **)PValue;
330
331 MAKE_STD_ZVAL(it->data);
332 *it->data = *value;
333 zval_copy_ctor(it->data);
334 INIT_PZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
335 }
336 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
337
68f4e8a spaces -> tabs, that's the standard
nicolas authored
338 } else if (object->type == TYPE_STRING_TO_INT || object->type == TYPE_STRING_TO_MIXED) {
5c2d8d5 initial import of php-judy in pecl
nicolas authored
339
68f4e8a spaces -> tabs, that's the standard
nicolas authored
340 uint8_t key[PHP_JUDY_MAX_LENGTH];
341 Pvoid_t *PValue;
5c2d8d5 initial import of php-judy in pecl
nicolas authored
342
68f4e8a spaces -> tabs, that's the standard
nicolas authored
343 /* JudySL require null temrinated strings */
344 key[0] = '\0';
345 JSLF(PValue, object->array, key);
5c2d8d5 initial import of php-judy in pecl
nicolas authored
346
68f4e8a spaces -> tabs, that's the standard
nicolas authored
347 if (PValue != NULL && PValue != PJERR) {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
348 zval_dtor(it->key);
349 ZVAL_STRING(it->key, (const char *) key, 1);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
350 if (object->type == TYPE_STRING_TO_INT) {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
351 MAKE_STD_ZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
352 ZVAL_LONG(it->data, (long)*PValue);
353 } else {
3a687c2 massive patch fixing memory leaks and segfaults
nicolas authored
354 zval *value = *(zval **)PValue;
355
356 MAKE_STD_ZVAL(it->data);
357 *it->data = *value;
358 zval_copy_ctor(it->data);
359 INIT_PZVAL(it->data);
68f4e8a spaces -> tabs, that's the standard
nicolas authored
360 }
361 }
362 }
5c2d8d5 initial import of php-judy in pecl
nicolas authored
363 }
364 /* }}} */
365
366 /*
367 * Local variables:
368 * tab-width: 4
369 * c-basic-offset: 4
370 * End:
371 * vim600: noet sw=4 ts=4 fdm=marker
372 * vim<600: noet sw=4 ts=4
373 */
Something went wrong with that request. Please try again.