/src/php-src/ext/spl/php_spl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | https://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Marcus Boerger <helly@php.net> | |
14 | | +----------------------------------------------------------------------+ |
15 | | */ |
16 | | |
17 | | #ifdef HAVE_CONFIG_H |
18 | | #include <config.h> |
19 | | #endif |
20 | | |
21 | | #include "php.h" |
22 | | #include "php_main.h" |
23 | | #include "ext/standard/info.h" |
24 | | #include "php_spl.h" |
25 | | #include "php_spl_arginfo.h" |
26 | | #include "spl_functions.h" |
27 | | #include "spl_array.h" |
28 | | #include "spl_directory.h" |
29 | | #include "spl_iterators.h" |
30 | | #include "spl_exceptions.h" |
31 | | #include "spl_observer.h" |
32 | | #include "spl_dllist.h" |
33 | | #include "spl_fixedarray.h" |
34 | | #include "spl_heap.h" |
35 | | #include "zend_exceptions.h" |
36 | | #include "zend_interfaces.h" |
37 | | #include "main/snprintf.h" |
38 | | |
39 | | ZEND_TLS zend_string *spl_autoload_extensions; |
40 | | ZEND_TLS HashTable *spl_autoload_functions; |
41 | | |
42 | 0 | #define SPL_DEFAULT_FILE_EXTENSIONS ".inc,.php" |
43 | | |
44 | | static zend_class_entry * spl_find_ce_by_name(zend_string *name, bool autoload) |
45 | 0 | { |
46 | 0 | zend_class_entry *ce; |
47 | |
|
48 | 0 | if (!autoload) { |
49 | 0 | zend_string *lc_name = zend_string_tolower(name); |
50 | |
|
51 | 0 | ce = zend_hash_find_ptr(EG(class_table), lc_name); |
52 | 0 | zend_string_release(lc_name); |
53 | 0 | } else { |
54 | 0 | ce = zend_lookup_class(name); |
55 | 0 | } |
56 | 0 | if (ce == NULL) { |
57 | 0 | php_error_docref(NULL, E_WARNING, "Class %s does not exist%s", ZSTR_VAL(name), autoload ? " and could not be loaded" : ""); |
58 | 0 | return NULL; |
59 | 0 | } |
60 | | |
61 | 0 | return ce; |
62 | 0 | } |
63 | | |
64 | | /* {{{ Return an array containing the names of all parent classes */ |
65 | | PHP_FUNCTION(class_parents) |
66 | 0 | { |
67 | 0 | zval *obj; |
68 | 0 | zend_class_entry *parent_class, *ce; |
69 | 0 | bool autoload = 1; |
70 | | |
71 | | /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ |
72 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) { |
73 | 0 | RETURN_THROWS(); |
74 | 0 | } |
75 | | |
76 | 0 | if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) { |
77 | 0 | zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj)); |
78 | 0 | RETURN_THROWS(); |
79 | 0 | } |
80 | | |
81 | 0 | if (Z_TYPE_P(obj) == IS_STRING) { |
82 | 0 | if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) { |
83 | 0 | RETURN_FALSE; |
84 | 0 | } |
85 | 0 | } else { |
86 | 0 | ce = Z_OBJCE_P(obj); |
87 | 0 | } |
88 | | |
89 | 0 | array_init(return_value); |
90 | 0 | parent_class = ce->parent; |
91 | 0 | while (parent_class) { |
92 | 0 | spl_add_class_name(return_value, parent_class, 0, 0); |
93 | 0 | parent_class = parent_class->parent; |
94 | 0 | } |
95 | 0 | } |
96 | | /* }}} */ |
97 | | |
98 | | /* {{{ Return all classes and interfaces implemented by SPL */ |
99 | | PHP_FUNCTION(class_implements) |
100 | 0 | { |
101 | 0 | zval *obj; |
102 | 0 | bool autoload = 1; |
103 | 0 | zend_class_entry *ce; |
104 | | |
105 | | /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ |
106 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) { |
107 | 0 | RETURN_THROWS(); |
108 | 0 | } |
109 | 0 | if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) { |
110 | 0 | zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj)); |
111 | 0 | RETURN_THROWS(); |
112 | 0 | } |
113 | | |
114 | 0 | if (Z_TYPE_P(obj) == IS_STRING) { |
115 | 0 | if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) { |
116 | 0 | RETURN_FALSE; |
117 | 0 | } |
118 | 0 | } else { |
119 | 0 | ce = Z_OBJCE_P(obj); |
120 | 0 | } |
121 | | |
122 | 0 | array_init(return_value); |
123 | 0 | spl_add_interfaces(return_value, ce, 1, ZEND_ACC_INTERFACE); |
124 | 0 | } |
125 | | /* }}} */ |
126 | | |
127 | | /* {{{ Return all traits used by a class. */ |
128 | | PHP_FUNCTION(class_uses) |
129 | 0 | { |
130 | 0 | zval *obj; |
131 | 0 | bool autoload = 1; |
132 | 0 | zend_class_entry *ce; |
133 | | |
134 | | /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ |
135 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) { |
136 | 0 | RETURN_THROWS(); |
137 | 0 | } |
138 | 0 | if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) { |
139 | 0 | zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj)); |
140 | 0 | RETURN_THROWS(); |
141 | 0 | } |
142 | | |
143 | 0 | if (Z_TYPE_P(obj) == IS_STRING) { |
144 | 0 | if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) { |
145 | 0 | RETURN_FALSE; |
146 | 0 | } |
147 | 0 | } else { |
148 | 0 | ce = Z_OBJCE_P(obj); |
149 | 0 | } |
150 | | |
151 | 0 | array_init(return_value); |
152 | 0 | spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT); |
153 | 0 | } |
154 | | /* }}} */ |
155 | | |
156 | | #define SPL_ADD_CLASS(class_name, z_list, sub, allow, ce_flags) \ |
157 | 550 | spl_add_classes(spl_ce_ ## class_name, z_list, sub, allow, ce_flags) |
158 | | |
159 | | #define SPL_LIST_CLASSES(z_list, sub, allow, ce_flags) \ |
160 | 10 | SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \ |
161 | 10 | SPL_ADD_CLASS(ArrayIterator, z_list, sub, allow, ce_flags); \ |
162 | 10 | SPL_ADD_CLASS(ArrayObject, z_list, sub, allow, ce_flags); \ |
163 | 10 | SPL_ADD_CLASS(BadFunctionCallException, z_list, sub, allow, ce_flags); \ |
164 | 10 | SPL_ADD_CLASS(BadMethodCallException, z_list, sub, allow, ce_flags); \ |
165 | 10 | SPL_ADD_CLASS(CachingIterator, z_list, sub, allow, ce_flags); \ |
166 | 10 | SPL_ADD_CLASS(CallbackFilterIterator, z_list, sub, allow, ce_flags); \ |
167 | 10 | SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \ |
168 | 10 | SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \ |
169 | 10 | SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \ |
170 | 10 | SPL_ADD_CLASS(FilesystemIterator, z_list, sub, allow, ce_flags); \ |
171 | 10 | SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \ |
172 | 10 | SPL_ADD_CLASS(GlobIterator, z_list, sub, allow, ce_flags); \ |
173 | 10 | SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \ |
174 | 10 | SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \ |
175 | 10 | SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \ |
176 | 10 | SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \ |
177 | 10 | SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \ |
178 | 10 | SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \ |
179 | 10 | SPL_ADD_CLASS(MultipleIterator, z_list, sub, allow, ce_flags); \ |
180 | 10 | SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \ |
181 | 10 | SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \ |
182 | 10 | SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \ |
183 | 10 | SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \ |
184 | 10 | SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \ |
185 | 10 | SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \ |
186 | 10 | SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \ |
187 | 10 | SPL_ADD_CLASS(RecursiveArrayIterator, z_list, sub, allow, ce_flags); \ |
188 | 10 | SPL_ADD_CLASS(RecursiveCachingIterator, z_list, sub, allow, ce_flags); \ |
189 | 10 | SPL_ADD_CLASS(RecursiveCallbackFilterIterator, z_list, sub, allow, ce_flags); \ |
190 | 10 | SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \ |
191 | 10 | SPL_ADD_CLASS(RecursiveFilterIterator, z_list, sub, allow, ce_flags); \ |
192 | 10 | SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \ |
193 | 10 | SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \ |
194 | 10 | SPL_ADD_CLASS(RecursiveRegexIterator, z_list, sub, allow, ce_flags); \ |
195 | 10 | SPL_ADD_CLASS(RecursiveTreeIterator, z_list, sub, allow, ce_flags); \ |
196 | 10 | SPL_ADD_CLASS(RegexIterator, z_list, sub, allow, ce_flags); \ |
197 | 10 | SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \ |
198 | 10 | SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \ |
199 | 10 | SPL_ADD_CLASS(SplDoublyLinkedList, z_list, sub, allow, ce_flags); \ |
200 | 10 | SPL_ADD_CLASS(SplFileInfo, z_list, sub, allow, ce_flags); \ |
201 | 10 | SPL_ADD_CLASS(SplFileObject, z_list, sub, allow, ce_flags); \ |
202 | 10 | SPL_ADD_CLASS(SplFixedArray, z_list, sub, allow, ce_flags); \ |
203 | 10 | SPL_ADD_CLASS(SplHeap, z_list, sub, allow, ce_flags); \ |
204 | 10 | SPL_ADD_CLASS(SplMinHeap, z_list, sub, allow, ce_flags); \ |
205 | 10 | SPL_ADD_CLASS(SplMaxHeap, z_list, sub, allow, ce_flags); \ |
206 | 10 | SPL_ADD_CLASS(SplObjectStorage, z_list, sub, allow, ce_flags); \ |
207 | 10 | SPL_ADD_CLASS(SplObserver, z_list, sub, allow, ce_flags); \ |
208 | 10 | SPL_ADD_CLASS(SplPriorityQueue, z_list, sub, allow, ce_flags); \ |
209 | 10 | SPL_ADD_CLASS(SplQueue, z_list, sub, allow, ce_flags); \ |
210 | 10 | SPL_ADD_CLASS(SplStack, z_list, sub, allow, ce_flags); \ |
211 | 10 | SPL_ADD_CLASS(SplSubject, z_list, sub, allow, ce_flags); \ |
212 | 10 | SPL_ADD_CLASS(SplTempFileObject, z_list, sub, allow, ce_flags); \ |
213 | 10 | SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \ |
214 | 10 | SPL_ADD_CLASS(UnexpectedValueException, z_list, sub, allow, ce_flags); \ |
215 | | |
216 | | /* {{{ Return an array containing the names of all clsses and interfaces defined in SPL */ |
217 | | PHP_FUNCTION(spl_classes) |
218 | 0 | { |
219 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
220 | 0 | RETURN_THROWS(); |
221 | 0 | } |
222 | | |
223 | 0 | array_init(return_value); |
224 | |
|
225 | 0 | SPL_LIST_CLASSES(return_value, 0, 0, 0) |
226 | 0 | } |
227 | | /* }}} */ |
228 | | |
229 | | static int spl_autoload(zend_string *class_name, zend_string *lc_name, const char *ext, int ext_len) /* {{{ */ |
230 | 0 | { |
231 | 0 | zend_string *class_file; |
232 | 0 | zval dummy; |
233 | 0 | zend_file_handle file_handle; |
234 | 0 | zend_op_array *new_op_array; |
235 | 0 | zval result; |
236 | 0 | int ret; |
237 | |
|
238 | 0 | class_file = zend_strpprintf(0, "%s%.*s", ZSTR_VAL(lc_name), ext_len, ext); |
239 | |
|
240 | 0 | #if DEFAULT_SLASH != '\\' |
241 | 0 | { |
242 | 0 | char *ptr = ZSTR_VAL(class_file); |
243 | 0 | char *end = ptr + ZSTR_LEN(class_file); |
244 | |
|
245 | 0 | while ((ptr = memchr(ptr, '\\', (end - ptr))) != NULL) { |
246 | 0 | *ptr = DEFAULT_SLASH; |
247 | 0 | } |
248 | 0 | } |
249 | 0 | #endif |
250 | |
|
251 | 0 | zend_stream_init_filename_ex(&file_handle, class_file); |
252 | 0 | ret = php_stream_open_for_zend_ex(&file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE); |
253 | |
|
254 | 0 | if (ret == SUCCESS) { |
255 | 0 | zend_string *opened_path; |
256 | 0 | if (!file_handle.opened_path) { |
257 | 0 | file_handle.opened_path = zend_string_copy(class_file); |
258 | 0 | } |
259 | 0 | opened_path = zend_string_copy(file_handle.opened_path); |
260 | 0 | ZVAL_NULL(&dummy); |
261 | 0 | if (zend_hash_add(&EG(included_files), opened_path, &dummy)) { |
262 | 0 | new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE); |
263 | 0 | } else { |
264 | 0 | new_op_array = NULL; |
265 | 0 | } |
266 | 0 | zend_string_release_ex(opened_path, 0); |
267 | 0 | if (new_op_array) { |
268 | 0 | uint32_t orig_jit_trace_num = EG(jit_trace_num); |
269 | |
|
270 | 0 | ZVAL_UNDEF(&result); |
271 | 0 | zend_execute(new_op_array, &result); |
272 | 0 | EG(jit_trace_num) = orig_jit_trace_num; |
273 | |
|
274 | 0 | destroy_op_array(new_op_array); |
275 | 0 | efree(new_op_array); |
276 | 0 | if (!EG(exception)) { |
277 | 0 | zval_ptr_dtor(&result); |
278 | 0 | } |
279 | |
|
280 | 0 | zend_destroy_file_handle(&file_handle); |
281 | 0 | zend_string_release(class_file); |
282 | 0 | return zend_hash_exists(EG(class_table), lc_name); |
283 | 0 | } |
284 | 0 | } |
285 | 0 | zend_destroy_file_handle(&file_handle); |
286 | 0 | zend_string_release(class_file); |
287 | 0 | return 0; |
288 | 0 | } /* }}} */ |
289 | | |
290 | | /* {{{ Default autoloader implementation */ |
291 | | PHP_FUNCTION(spl_autoload) |
292 | 0 | { |
293 | 0 | int pos_len, pos1_len; |
294 | 0 | char *pos, *pos1; |
295 | 0 | zend_string *class_name, *lc_name, *file_exts = NULL; |
296 | |
|
297 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &class_name, &file_exts) == FAILURE) { |
298 | 0 | RETURN_THROWS(); |
299 | 0 | } |
300 | | |
301 | 0 | if (!file_exts) { |
302 | 0 | file_exts = spl_autoload_extensions; |
303 | 0 | } |
304 | |
|
305 | 0 | if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */ |
306 | 0 | pos = SPL_DEFAULT_FILE_EXTENSIONS; |
307 | 0 | pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1; |
308 | 0 | } else { |
309 | 0 | pos = ZSTR_VAL(file_exts); |
310 | 0 | pos_len = (int)ZSTR_LEN(file_exts); |
311 | 0 | } |
312 | |
|
313 | 0 | lc_name = zend_string_tolower(class_name); |
314 | 0 | while (pos && *pos && !EG(exception)) { |
315 | 0 | pos1 = strchr(pos, ','); |
316 | 0 | if (pos1) { |
317 | 0 | pos1_len = (int)(pos1 - pos); |
318 | 0 | } else { |
319 | 0 | pos1_len = pos_len; |
320 | 0 | } |
321 | 0 | if (spl_autoload(class_name, lc_name, pos, pos1_len)) { |
322 | 0 | break; /* loaded */ |
323 | 0 | } |
324 | 0 | pos = pos1 ? pos1 + 1 : NULL; |
325 | 0 | pos_len = pos1? pos_len - pos1_len - 1 : 0; |
326 | 0 | } |
327 | 0 | zend_string_release(lc_name); |
328 | 0 | } /* }}} */ |
329 | | |
330 | | /* {{{ Register and return default file extensions for spl_autoload */ |
331 | | PHP_FUNCTION(spl_autoload_extensions) |
332 | 0 | { |
333 | 0 | zend_string *file_exts = NULL; |
334 | |
|
335 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &file_exts) == FAILURE) { |
336 | 0 | RETURN_THROWS(); |
337 | 0 | } |
338 | | |
339 | 0 | if (file_exts) { |
340 | 0 | if (spl_autoload_extensions) { |
341 | 0 | zend_string_release_ex(spl_autoload_extensions, 0); |
342 | 0 | } |
343 | 0 | spl_autoload_extensions = zend_string_copy(file_exts); |
344 | 0 | } |
345 | |
|
346 | 0 | if (spl_autoload_extensions == NULL) { |
347 | 0 | RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1); |
348 | 0 | } else { |
349 | 0 | zend_string_addref(spl_autoload_extensions); |
350 | 0 | RETURN_STR(spl_autoload_extensions); |
351 | 0 | } |
352 | 0 | } /* }}} */ |
353 | | |
354 | | typedef struct { |
355 | | zend_function *func_ptr; |
356 | | zend_object *obj; |
357 | | zend_object *closure; |
358 | | zend_class_entry *ce; |
359 | | } autoload_func_info; |
360 | | |
361 | 443 | static void autoload_func_info_destroy(autoload_func_info *alfi) { |
362 | 443 | if (alfi->obj) { |
363 | 0 | zend_object_release(alfi->obj); |
364 | 0 | } |
365 | 443 | if (alfi->func_ptr && |
366 | 443 | UNEXPECTED(alfi->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) { |
367 | 0 | zend_string_release_ex(alfi->func_ptr->common.function_name, 0); |
368 | 0 | zend_free_trampoline(alfi->func_ptr); |
369 | 0 | } |
370 | 443 | if (alfi->closure) { |
371 | 420 | zend_object_release(alfi->closure); |
372 | 420 | } |
373 | 443 | efree(alfi); |
374 | 443 | } |
375 | | |
376 | | static void autoload_func_info_zval_dtor(zval *element) |
377 | 443 | { |
378 | 443 | autoload_func_info_destroy(Z_PTR_P(element)); |
379 | 443 | } |
380 | | |
381 | | static autoload_func_info *autoload_func_info_from_fci( |
382 | 443 | zend_fcall_info *fci, zend_fcall_info_cache *fcc) { |
383 | 443 | autoload_func_info *alfi = emalloc(sizeof(autoload_func_info)); |
384 | 443 | alfi->ce = fcc->calling_scope; |
385 | 443 | alfi->func_ptr = fcc->function_handler; |
386 | 443 | alfi->obj = fcc->object; |
387 | 443 | if (alfi->obj) { |
388 | 0 | GC_ADDREF(alfi->obj); |
389 | 0 | } |
390 | 443 | if (Z_TYPE(fci->function_name) == IS_OBJECT) { |
391 | 420 | alfi->closure = Z_OBJ(fci->function_name); |
392 | 420 | GC_ADDREF(alfi->closure); |
393 | 420 | } else { |
394 | 23 | alfi->closure = NULL; |
395 | 23 | } |
396 | 443 | return alfi; |
397 | 443 | } |
398 | | |
399 | | static bool autoload_func_info_equals( |
400 | 15 | const autoload_func_info *alfi1, const autoload_func_info *alfi2) { |
401 | 15 | if (UNEXPECTED( |
402 | 15 | (alfi1->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) && |
403 | 15 | (alfi2->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) |
404 | 15 | )) { |
405 | 0 | return alfi1->obj == alfi2->obj |
406 | 0 | && alfi1->ce == alfi2->ce |
407 | 0 | && alfi1->closure == alfi2->closure |
408 | 0 | && zend_string_equals(alfi1->func_ptr->common.function_name, alfi2->func_ptr->common.function_name) |
409 | 0 | ; |
410 | 0 | } |
411 | 15 | return alfi1->func_ptr == alfi2->func_ptr |
412 | 15 | && alfi1->obj == alfi2->obj |
413 | 15 | && alfi1->ce == alfi2->ce |
414 | 15 | && alfi1->closure == alfi2->closure; |
415 | 15 | } |
416 | | |
417 | 240k | static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_string *lc_name) { |
418 | 240k | if (!spl_autoload_functions) { |
419 | 239k | return NULL; |
420 | 239k | } |
421 | | |
422 | | /* We don't use ZEND_HASH_MAP_FOREACH here, |
423 | | * because autoloaders may be added/removed during autoloading. */ |
424 | 932 | HashPosition pos; |
425 | 932 | zend_hash_internal_pointer_reset_ex(spl_autoload_functions, &pos); |
426 | 1.15k | while (1) { |
427 | 1.02k | autoload_func_info *alfi = |
428 | 1.02k | zend_hash_get_current_data_ptr_ex(spl_autoload_functions, &pos); |
429 | 1.02k | if (!alfi) { |
430 | 75 | break; |
431 | 75 | } |
432 | | |
433 | 947 | zend_function *func = alfi->func_ptr; |
434 | 947 | if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) { |
435 | 0 | func = emalloc(sizeof(zend_op_array)); |
436 | 0 | memcpy(func, alfi->func_ptr, sizeof(zend_op_array)); |
437 | 0 | zend_string_addref(func->op_array.function_name); |
438 | 0 | } |
439 | | |
440 | 947 | zval param; |
441 | 947 | ZVAL_STR(¶m, class_name); |
442 | 947 | zend_call_known_function(func, alfi->obj, alfi->ce, NULL, 1, ¶m, NULL); |
443 | 947 | if (EG(exception)) { |
444 | 247 | break; |
445 | 247 | } |
446 | | |
447 | 700 | if (ZSTR_HAS_CE_CACHE(class_name) && ZSTR_GET_CE_CACHE(class_name)) { |
448 | 258 | return (zend_class_entry*)ZSTR_GET_CE_CACHE(class_name); |
449 | 442 | } else { |
450 | 442 | zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name); |
451 | 442 | if (ce) { |
452 | 223 | return ce; |
453 | 223 | } |
454 | 442 | } |
455 | | |
456 | 219 | zend_hash_move_forward_ex(spl_autoload_functions, &pos); |
457 | 219 | } |
458 | 451 | return NULL; |
459 | 932 | } |
460 | | |
461 | | /* {{{ Try all registered autoload function to load the requested class */ |
462 | | PHP_FUNCTION(spl_autoload_call) |
463 | 0 | { |
464 | 0 | zend_string *class_name; |
465 | |
|
466 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) { |
467 | 0 | RETURN_THROWS(); |
468 | 0 | } |
469 | | |
470 | 0 | zend_string *lc_name = zend_string_tolower(class_name); |
471 | 0 | spl_perform_autoload(class_name, lc_name); |
472 | 0 | zend_string_release(lc_name); |
473 | 0 | } /* }}} */ |
474 | | |
475 | | #define HT_MOVE_TAIL_TO_HEAD(ht) \ |
476 | 15 | ZEND_ASSERT(!HT_IS_PACKED(ht)); \ |
477 | 15 | do { \ |
478 | 15 | Bucket tmp = (ht)->arData[(ht)->nNumUsed-1]; \ |
479 | 15 | memmove((ht)->arData + 1, (ht)->arData, \ |
480 | 15 | sizeof(Bucket) * ((ht)->nNumUsed - 1)); \ |
481 | 15 | (ht)->arData[0] = tmp; \ |
482 | 15 | zend_hash_rehash(ht); \ |
483 | 15 | } while (0) |
484 | | |
485 | 443 | static Bucket *spl_find_registered_function(autoload_func_info *find_alfi) { |
486 | 443 | if (!spl_autoload_functions) { |
487 | 0 | return NULL; |
488 | 0 | } |
489 | | |
490 | 443 | autoload_func_info *alfi; |
491 | 916 | ZEND_HASH_MAP_FOREACH_PTR(spl_autoload_functions, alfi) { |
492 | 916 | if (autoload_func_info_equals(alfi, find_alfi)) { |
493 | 0 | return _p; |
494 | 0 | } |
495 | 916 | } ZEND_HASH_FOREACH_END(); |
496 | 443 | return NULL; |
497 | 443 | } |
498 | | |
499 | | /* {{{ Register given function as autoloader */ |
500 | | PHP_FUNCTION(spl_autoload_register) |
501 | 445 | { |
502 | 445 | bool do_throw = 1; |
503 | 445 | bool prepend = 0; |
504 | 445 | zend_fcall_info fci = {0}; |
505 | 445 | zend_fcall_info_cache fcc; |
506 | 445 | autoload_func_info *alfi; |
507 | | |
508 | 1.33k | ZEND_PARSE_PARAMETERS_START(0, 3) |
509 | 1.33k | Z_PARAM_OPTIONAL |
510 | 1.78k | Z_PARAM_FUNC_OR_NULL(fci, fcc) |
511 | 1.35k | Z_PARAM_BOOL(do_throw) |
512 | 75 | Z_PARAM_BOOL(prepend) |
513 | 445 | ZEND_PARSE_PARAMETERS_END(); |
514 | | |
515 | 443 | if (!do_throw) { |
516 | 0 | php_error_docref(NULL, E_NOTICE, "Argument #2 ($do_throw) has been ignored, " |
517 | 0 | "spl_autoload_register() will always throw"); |
518 | 0 | } |
519 | | |
520 | 443 | if (!spl_autoload_functions) { |
521 | 428 | ALLOC_HASHTABLE(spl_autoload_functions); |
522 | 428 | zend_hash_init(spl_autoload_functions, 1, NULL, autoload_func_info_zval_dtor, 0); |
523 | | /* Initialize as non-packed hash table for prepend functionality. */ |
524 | 428 | zend_hash_real_init_mixed(spl_autoload_functions); |
525 | 428 | } |
526 | | |
527 | | /* If first arg is not null */ |
528 | 443 | if (ZEND_FCI_INITIALIZED(fci)) { |
529 | 443 | if (!fcc.function_handler) { |
530 | | /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal |
531 | | * with it ourselves. It is important that it is not refetched on every call, |
532 | | * because calls may occur from different scopes. */ |
533 | 0 | zend_is_callable_ex(&fci.function_name, NULL, IS_CALLABLE_SUPPRESS_DEPRECATIONS, NULL, &fcc, NULL); |
534 | 0 | } |
535 | | |
536 | 443 | if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION && |
537 | 443 | fcc.function_handler->internal_function.handler == zif_spl_autoload_call) { |
538 | 0 | zend_argument_value_error(1, "must not be the spl_autoload_call() function"); |
539 | 0 | RETURN_THROWS(); |
540 | 0 | } |
541 | | |
542 | 443 | alfi = autoload_func_info_from_fci(&fci, &fcc); |
543 | 443 | if (UNEXPECTED(alfi->func_ptr == &EG(trampoline))) { |
544 | 0 | zend_function *copy = emalloc(sizeof(zend_op_array)); |
545 | |
|
546 | 0 | memcpy(copy, alfi->func_ptr, sizeof(zend_op_array)); |
547 | 0 | alfi->func_ptr->common.function_name = NULL; |
548 | 0 | alfi->func_ptr = copy; |
549 | 0 | } |
550 | 443 | } else { |
551 | 0 | alfi = emalloc(sizeof(autoload_func_info)); |
552 | 0 | alfi->func_ptr = zend_hash_str_find_ptr( |
553 | 0 | CG(function_table), "spl_autoload", sizeof("spl_autoload") - 1); |
554 | 0 | alfi->obj = NULL; |
555 | 0 | alfi->ce = NULL; |
556 | 0 | alfi->closure = NULL; |
557 | 0 | } |
558 | | |
559 | 443 | if (spl_find_registered_function(alfi)) { |
560 | 0 | autoload_func_info_destroy(alfi); |
561 | 0 | RETURN_TRUE; |
562 | 0 | } |
563 | | |
564 | 443 | zend_hash_next_index_insert_ptr(spl_autoload_functions, alfi); |
565 | 443 | if (prepend && spl_autoload_functions->nNumOfElements > 1) { |
566 | | /* Move the newly created element to the head of the hashtable */ |
567 | 15 | HT_MOVE_TAIL_TO_HEAD(spl_autoload_functions); |
568 | 15 | } |
569 | | |
570 | 443 | RETURN_TRUE; |
571 | 443 | } /* }}} */ |
572 | | |
573 | | /* {{{ Unregister given function as autoloader */ |
574 | | PHP_FUNCTION(spl_autoload_unregister) |
575 | 0 | { |
576 | 0 | zend_fcall_info fci; |
577 | 0 | zend_fcall_info_cache fcc; |
578 | |
|
579 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
580 | 0 | Z_PARAM_FUNC(fci, fcc) |
581 | 0 | ZEND_PARSE_PARAMETERS_END(); |
582 | | |
583 | 0 | if (fcc.function_handler && zend_string_equals_literal( |
584 | 0 | fcc.function_handler->common.function_name, "spl_autoload_call")) { |
585 | 0 | if (spl_autoload_functions) { |
586 | | /* Don't destroy the hash table, as we might be iterating over it right now. */ |
587 | 0 | zend_hash_clean(spl_autoload_functions); |
588 | 0 | } |
589 | 0 | RETURN_TRUE; |
590 | 0 | } |
591 | | |
592 | 0 | if (!fcc.function_handler) { |
593 | | /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal |
594 | | * with it ourselves. It is important that it is not refetched on every call, |
595 | | * because calls may occur from different scopes. */ |
596 | 0 | zend_is_callable_ex(&fci.function_name, NULL, 0, NULL, &fcc, NULL); |
597 | 0 | } |
598 | |
|
599 | 0 | autoload_func_info *alfi = autoload_func_info_from_fci(&fci, &fcc); |
600 | 0 | Bucket *p = spl_find_registered_function(alfi); |
601 | 0 | autoload_func_info_destroy(alfi); |
602 | 0 | if (p) { |
603 | 0 | zend_hash_del_bucket(spl_autoload_functions, p); |
604 | 0 | RETURN_TRUE; |
605 | 0 | } |
606 | | |
607 | 0 | RETURN_FALSE; |
608 | 0 | } /* }}} */ |
609 | | |
610 | | /* {{{ Return all registered autoloader functions */ |
611 | | PHP_FUNCTION(spl_autoload_functions) |
612 | 0 | { |
613 | 0 | autoload_func_info *alfi; |
614 | |
|
615 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
616 | 0 | RETURN_THROWS(); |
617 | 0 | } |
618 | | |
619 | 0 | array_init(return_value); |
620 | 0 | if (spl_autoload_functions) { |
621 | 0 | ZEND_HASH_MAP_FOREACH_PTR(spl_autoload_functions, alfi) { |
622 | 0 | if (alfi->closure) { |
623 | 0 | GC_ADDREF(alfi->closure); |
624 | 0 | add_next_index_object(return_value, alfi->closure); |
625 | 0 | } else if (alfi->func_ptr->common.scope) { |
626 | 0 | zval tmp; |
627 | |
|
628 | 0 | array_init(&tmp); |
629 | 0 | if (alfi->obj) { |
630 | 0 | GC_ADDREF(alfi->obj); |
631 | 0 | add_next_index_object(&tmp, alfi->obj); |
632 | 0 | } else { |
633 | 0 | add_next_index_str(&tmp, zend_string_copy(alfi->ce->name)); |
634 | 0 | } |
635 | 0 | add_next_index_str(&tmp, zend_string_copy(alfi->func_ptr->common.function_name)); |
636 | 0 | add_next_index_zval(return_value, &tmp); |
637 | 0 | } else { |
638 | 0 | add_next_index_str(return_value, zend_string_copy(alfi->func_ptr->common.function_name)); |
639 | 0 | } |
640 | 0 | } ZEND_HASH_FOREACH_END(); |
641 | 0 | } |
642 | 0 | } /* }}} */ |
643 | | |
644 | | /* {{{ Return hash id for given object */ |
645 | | PHP_FUNCTION(spl_object_hash) |
646 | 248 | { |
647 | 248 | zend_object *obj; |
648 | | |
649 | 744 | ZEND_PARSE_PARAMETERS_START(1, 1) |
650 | 992 | Z_PARAM_OBJ(obj) |
651 | 248 | ZEND_PARSE_PARAMETERS_END(); |
652 | | |
653 | 248 | RETURN_NEW_STR(php_spl_object_hash(obj)); |
654 | 248 | } |
655 | | /* }}} */ |
656 | | |
657 | | /* {{{ Returns the integer object handle for the given object */ |
658 | | PHP_FUNCTION(spl_object_id) |
659 | 323 | { |
660 | 323 | zend_object *obj; |
661 | | |
662 | 969 | ZEND_PARSE_PARAMETERS_START(1, 1) |
663 | 1.29k | Z_PARAM_OBJ(obj) |
664 | 323 | ZEND_PARSE_PARAMETERS_END(); |
665 | | |
666 | 318 | RETURN_LONG((zend_long)obj->handle); |
667 | 318 | } |
668 | | /* }}} */ |
669 | | |
670 | | PHPAPI zend_string *php_spl_object_hash(zend_object *obj) /* {{{*/ |
671 | 248 | { |
672 | 248 | return strpprintf(32, "%016zx0000000000000000", (intptr_t)obj->handle); |
673 | 248 | } |
674 | | /* }}} */ |
675 | | |
676 | | static void spl_build_class_list_string(zval *entry, char **list) /* {{{ */ |
677 | 275 | { |
678 | 275 | char *res; |
679 | | |
680 | 275 | spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry)); |
681 | 275 | efree(*list); |
682 | 275 | *list = res; |
683 | 275 | } /* }}} */ |
684 | | |
685 | | /* {{{ PHP_MINFO(spl) */ |
686 | | PHP_MINFO_FUNCTION(spl) |
687 | 5 | { |
688 | 5 | zval list, *zv; |
689 | 5 | char *strg; |
690 | | |
691 | 5 | php_info_print_table_start(); |
692 | 5 | php_info_print_table_row(2, "SPL support", "enabled"); |
693 | | |
694 | 5 | array_init(&list); |
695 | 5 | SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE) |
696 | 5 | strg = estrdup(""); |
697 | 60 | ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) { |
698 | 60 | spl_build_class_list_string(zv, &strg); |
699 | 60 | } ZEND_HASH_FOREACH_END(); |
700 | 5 | zend_array_destroy(Z_ARR(list)); |
701 | 5 | php_info_print_table_row(2, "Interfaces", strg + 2); |
702 | 5 | efree(strg); |
703 | | |
704 | 5 | array_init(&list); |
705 | 5 | SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE) |
706 | 5 | strg = estrdup(""); |
707 | 510 | ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) { |
708 | 510 | spl_build_class_list_string(zv, &strg); |
709 | 510 | } ZEND_HASH_FOREACH_END(); |
710 | 5 | zend_array_destroy(Z_ARR(list)); |
711 | 5 | php_info_print_table_row(2, "Classes", strg + 2); |
712 | 5 | efree(strg); |
713 | | |
714 | 5 | php_info_print_table_end(); |
715 | 5 | } |
716 | | /* }}} */ |
717 | | |
718 | | /* {{{ PHP_MINIT_FUNCTION(spl) */ |
719 | | PHP_MINIT_FUNCTION(spl) |
720 | 16 | { |
721 | 16 | zend_autoload = spl_perform_autoload; |
722 | | |
723 | 16 | PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU); |
724 | 16 | PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU); |
725 | 16 | PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU); |
726 | 16 | PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU); |
727 | 16 | PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU); |
728 | 16 | PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU); |
729 | 16 | PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU); |
730 | 16 | PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU); |
731 | | |
732 | 16 | return SUCCESS; |
733 | 16 | } |
734 | | /* }}} */ |
735 | | |
736 | | PHP_RINIT_FUNCTION(spl) /* {{{ */ |
737 | 300k | { |
738 | 300k | spl_autoload_extensions = NULL; |
739 | 300k | spl_autoload_functions = NULL; |
740 | 300k | return SUCCESS; |
741 | 300k | } /* }}} */ |
742 | | |
743 | | PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */ |
744 | 300k | { |
745 | 300k | if (spl_autoload_extensions) { |
746 | 0 | zend_string_release_ex(spl_autoload_extensions, 0); |
747 | 0 | spl_autoload_extensions = NULL; |
748 | 0 | } |
749 | 300k | if (spl_autoload_functions) { |
750 | 428 | zend_hash_destroy(spl_autoload_functions); |
751 | 428 | FREE_HASHTABLE(spl_autoload_functions); |
752 | 428 | spl_autoload_functions = NULL; |
753 | 428 | } |
754 | 300k | return SUCCESS; |
755 | 300k | } /* }}} */ |
756 | | |
757 | | static const zend_module_dep spl_deps[] = { |
758 | | ZEND_MOD_REQUIRED("json") |
759 | | ZEND_MOD_END |
760 | | }; |
761 | | |
762 | | zend_module_entry spl_module_entry = { |
763 | | STANDARD_MODULE_HEADER_EX, NULL, |
764 | | spl_deps, |
765 | | "SPL", |
766 | | ext_functions, |
767 | | PHP_MINIT(spl), |
768 | | NULL, |
769 | | PHP_RINIT(spl), |
770 | | PHP_RSHUTDOWN(spl), |
771 | | PHP_MINFO(spl), |
772 | | PHP_SPL_VERSION, |
773 | | STANDARD_MODULE_PROPERTIES |
774 | | }; |