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   |          Andrei Zmievski <andrei@php.net>                            |
18   +----------------------------------------------------------------------+
19*/
20
21/* $Id$ */
22
23#include "zend.h"
24#include "zend_execute.h"
25#include "zend_API.h"
26#include "zend_modules.h"
27#include "zend_constants.h"
28#include "zend_exceptions.h"
29#include "zend_closures.h"
30#include "zend_inheritance.h"
31
32#ifdef HAVE_STDARG_H
33#include <stdarg.h>
34#endif
35
36/* these variables are true statics/globals, and have to be mutex'ed on every access */
37ZEND_API HashTable module_registry;
38
39static zend_module_entry **module_request_startup_handlers;
40static zend_module_entry **module_request_shutdown_handlers;
41static zend_module_entry **module_post_deactivate_handlers;
42
43static zend_class_entry  **class_cleanup_handlers;
44
45/* this function doesn't check for too many parameters */
46ZEND_API int zend_get_parameters(int ht, int param_count, ...) /* {{{ */
47{
48    int arg_count;
49    va_list ptr;
50    zval **param, *param_ptr;
51
52    param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
53    arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
54
55    if (param_count>arg_count) {
56        return FAILURE;
57    }
58
59    va_start(ptr, param_count);
60
61    while (param_count-->0) {
62        param = va_arg(ptr, zval **);
63        if (!Z_ISREF_P(param_ptr) && Z_REFCOUNT_P(param_ptr) > 1) {
64            zval new_tmp;
65
66            ZVAL_DUP(&new_tmp, param_ptr);
67            Z_DELREF_P(param_ptr);
68            ZVAL_COPY_VALUE(param_ptr, &new_tmp);
69        }
70        *param = param_ptr;
71        param_ptr++;
72    }
73    va_end(ptr);
74
75    return SUCCESS;
76}
77/* }}} */
78
79/* Zend-optimized Extended functions */
80/* this function doesn't check for too many parameters */
81ZEND_API int zend_get_parameters_ex(int param_count, ...) /* {{{ */
82{
83    int arg_count;
84    va_list ptr;
85    zval **param, *param_ptr;
86
87    param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
88    arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
89
90    if (param_count>arg_count) {
91        return FAILURE;
92    }
93
94    va_start(ptr, param_count);
95    while (param_count-->0) {
96        param = va_arg(ptr, zval **);
97        *param = param_ptr;
98        param_ptr++;
99    }
100    va_end(ptr);
101
102    return SUCCESS;
103}
104/* }}} */
105
106ZEND_API int _zend_get_parameters_array_ex(int param_count, zval *argument_array) /* {{{ */
107{
108    zval *param_ptr;
109    int arg_count;
110
111    param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
112    arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
113
114    if (param_count>arg_count) {
115        return FAILURE;
116    }
117
118    while (param_count-->0) {
119        ZVAL_COPY_VALUE(argument_array, param_ptr);
120        argument_array++;
121        param_ptr++;
122    }
123
124    return SUCCESS;
125}
126/* }}} */
127
128ZEND_API int zend_copy_parameters_array(int param_count, zval *argument_array) /* {{{ */
129{
130    zval *param_ptr;
131    int arg_count;
132
133    param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
134    arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
135
136    if (param_count>arg_count) {
137        return FAILURE;
138    }
139
140    while (param_count-->0) {
141        if (Z_REFCOUNTED_P(param_ptr)) {
142            Z_ADDREF_P(param_ptr);
143        }
144        zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr);
145        param_ptr++;
146    }
147
148    return SUCCESS;
149}
150/* }}} */
151
152ZEND_API void zend_wrong_param_count(void) /* {{{ */
153{
154    const char *space;
155    const char *class_name = get_active_class_name(&space);
156
157    zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
158}
159/* }}} */
160
161/* Argument parsing API -- andrei */
162ZEND_API char *zend_get_type_by_const(int type) /* {{{ */
163{
164    switch(type) {
165        case IS_FALSE:
166        case IS_TRUE:
167        case _IS_BOOL:
168            return "boolean";
169        case IS_LONG:
170            return "integer";
171        case IS_DOUBLE:
172            return "float";
173        case IS_STRING:
174            return "string";
175        case IS_OBJECT:
176            return "object";
177        case IS_RESOURCE:
178            return "resource";
179        case IS_NULL:
180            return "null";
181        case IS_CALLABLE:
182            return "callable";
183        case IS_ARRAY:
184            return "array";
185        default:
186            return "unknown";
187    }
188}
189/* }}} */
190
191ZEND_API char *zend_zval_type_name(const zval *arg) /* {{{ */
192{
193    ZVAL_DEREF(arg);
194    return zend_get_type_by_const(Z_TYPE_P(arg));
195}
196/* }}} */
197
198#ifdef FAST_ZPP
199ZEND_API void ZEND_FASTCALL zend_wrong_paramers_count_error(int num_args, int min_num_args, int max_num_args) /* {{{ */
200{
201    zend_function *active_function = EG(current_execute_data)->func;
202    const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
203
204    zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects %s %d parameter%s, %d given",
205        class_name, \
206        class_name[0] ? "::" : "", \
207        ZSTR_VAL(active_function->common.function_name),
208        min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
209        num_args < min_num_args ? min_num_args : max_num_args,
210        (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
211        num_args);
212}
213/* }}} */
214
215ZEND_API void ZEND_FASTCALL zend_wrong_paramer_type_error(int num, zend_expected_type expected_type, zval *arg) /* {{{ */
216{
217    const char *space;
218    const char *class_name = get_active_class_name(&space);
219    static const char * const expected_error[] = {
220        Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
221        NULL
222    };
223
224    zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be %s, %s given",
225        class_name, space, get_active_function_name(), num, expected_error[expected_type], zend_zval_type_name(arg));
226}
227/* }}} */
228
229ZEND_API void ZEND_FASTCALL zend_wrong_paramer_class_error(int num, char *name, zval *arg) /* {{{ */
230{
231    const char *space;
232    const char *class_name = get_active_class_name(&space);
233
234    zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be %s, %s given",
235        class_name, space, get_active_function_name(), num, name, zend_zval_type_name(arg));
236}
237/* }}} */
238
239ZEND_API void ZEND_FASTCALL zend_wrong_callback_error(int severity, int num, char *error) /* {{{ */
240{
241    const char *space;
242    const char *class_name = get_active_class_name(&space);
243
244    if (severity == E_WARNING) {
245        zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a valid callback, %s",
246            class_name, space, get_active_function_name(), num, error);
247    } else if (severity == E_ERROR) {
248        zend_throw_error(zend_ce_type_error, "%s%s%s() expects parameter %d to be a valid callback, %s",
249            class_name, space, get_active_function_name(), num, error);
250    } else {
251        zend_error(severity, "%s%s%s() expects parameter %d to be a valid callback, %s",
252            class_name, space, get_active_function_name(), num, error);
253    }
254    efree(error);
255}
256/* }}} */
257
258ZEND_API int ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, int num, int check_null) /* {{{ */
259{
260    zend_class_entry *ce_base = *pce;
261
262    if (check_null && Z_TYPE_P(arg) == IS_NULL) {
263        *pce = NULL;
264        return 1;
265    }
266    convert_to_string_ex(arg);
267    *pce = zend_lookup_class(Z_STR_P(arg));
268    if (ce_base) {
269        if ((!*pce || !instanceof_function(*pce, ce_base))) {
270            const char *space;
271            const char *class_name = get_active_class_name(&space);
272
273            zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a class name derived from %s, '%s' given",
274                class_name, space, get_active_function_name(), num,
275                ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
276            *pce = NULL;
277            return 0;
278        }
279    }
280    if (!*pce) {
281        const char *space;
282        const char *class_name = get_active_class_name(&space);
283
284        zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a valid class name, '%s' given",
285            class_name, space, get_active_function_name(), num,
286            Z_STRVAL_P(arg));
287        return 0;
288    }
289    return 1;
290}
291/* }}} */
292#endif
293
294ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_weak(zval *arg, zend_bool *dest) /* {{{ */
295{
296    if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
297        *dest = zend_is_true(arg);
298    } else {
299        return 0;
300    }
301    return 1;
302}
303/* }}} */
304
305ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_slow(zval *arg, zend_bool *dest) /* {{{ */
306{
307    if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
308        return 0;
309    }
310    return zend_parse_arg_bool_weak(arg, dest);
311}
312/* }}} */
313
314ZEND_API int ZEND_FASTCALL zend_parse_arg_long_weak(zval *arg, zend_long *dest) /* {{{ */
315{
316    if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
317        if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
318            return 0;
319        }
320        if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
321            return 0;
322        } else {
323            *dest = zend_dval_to_lval(Z_DVAL_P(arg));
324        }
325    } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
326        double d;
327        int type;
328
329        if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
330            if (EXPECTED(type != 0)) {
331                if (UNEXPECTED(zend_isnan(d))) {
332                    return 0;
333                }
334                if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
335                    return 0;
336                } else {
337                    *dest = zend_dval_to_lval(d);
338                }
339            } else {
340                return 0;
341            }
342        }
343    } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
344        *dest = 0;
345    } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
346        *dest = 1;
347    } else {
348        return 0;
349    }
350    return 1;
351}
352/* }}} */
353
354ZEND_API int ZEND_FASTCALL zend_parse_arg_long_slow(zval *arg, zend_long *dest) /* {{{ */
355{
356    if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
357        return 0;
358    }
359    return zend_parse_arg_long_weak(arg, dest);
360}
361/* }}} */
362
363ZEND_API int ZEND_FASTCALL zend_parse_arg_long_cap_weak(zval *arg, zend_long *dest) /* {{{ */
364{
365    if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
366        if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
367            return 0;
368        }
369        if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
370            *dest = (Z_DVAL_P(arg) > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
371        } else {
372            *dest = zend_dval_to_lval(Z_DVAL_P(arg));
373        }
374    } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
375        double d;
376        int type;
377
378        if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
379            if (EXPECTED(type != 0)) {
380                if (UNEXPECTED(zend_isnan(d))) {
381                    return 0;
382                }
383                if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
384                    *dest = (d > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
385                } else {
386                    *dest = zend_dval_to_lval(d);
387                }
388            } else {
389                return 0;
390            }
391        }
392    } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
393        *dest = 0;
394    } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
395        *dest = 1;
396    } else {
397        return 0;
398    }
399    return 1;
400}
401/* }}} */
402
403ZEND_API int ZEND_FASTCALL zend_parse_arg_long_cap_slow(zval *arg, zend_long *dest) /* {{{ */
404{
405    if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
406        return 0;
407    }
408    return zend_parse_arg_long_cap_weak(arg, dest);
409}
410/* }}} */
411
412ZEND_API int ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest) /* {{{ */
413{
414    if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
415        *dest = (double)Z_LVAL_P(arg);
416    } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
417        zend_long l;
418        int type;
419
420        if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
421            if (EXPECTED(type != 0)) {
422                *dest = (double)(l);
423            } else {
424                return 0;
425            }
426        }
427    } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
428        *dest = 0.0;
429    } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
430        *dest = 1.0;
431    } else {
432        return 0;
433    }
434    return 1;
435}
436/* }}} */
437
438ZEND_API int ZEND_FASTCALL zend_parse_arg_double_slow(zval *arg, double *dest) /* {{{ */
439{
440    if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
441        /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
442        *dest = (double)Z_LVAL_P(arg);
443    } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
444        return 0;
445    }
446    return zend_parse_arg_double_weak(arg, dest);
447}
448/* }}} */
449
450ZEND_API int ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest) /* {{{ */
451{
452    if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
453        convert_to_string(arg);
454        *dest = Z_STR_P(arg);
455    } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
456        if (Z_OBJ_HANDLER_P(arg, cast_object)) {
457            zval obj;
458            if (Z_OBJ_HANDLER_P(arg, cast_object)(arg, &obj, IS_STRING) == SUCCESS) {
459                zval_ptr_dtor(arg);
460                ZVAL_COPY_VALUE(arg, &obj);
461                *dest = Z_STR_P(arg);
462                return 1;
463            }
464        } else if (Z_OBJ_HANDLER_P(arg, get)) {
465            zval rv;
466            zval *z = Z_OBJ_HANDLER_P(arg, get)(arg, &rv);
467
468            Z_ADDREF_P(z);
469            if (Z_TYPE_P(z) != IS_OBJECT) {
470                zval_dtor(arg);
471                ZVAL_NULL(arg);
472                if (!zend_make_printable_zval(z, arg)) {
473                    ZVAL_COPY_VALUE(arg, z);
474                }
475                *dest = Z_STR_P(arg);
476                return 1;
477            }
478            zval_ptr_dtor(z);
479        }
480        return 0;
481    } else {
482        return 0;
483    }
484    return 1;
485}
486/* }}} */
487
488ZEND_API int ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest) /* {{{ */
489{
490    if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
491        return 0;
492    }
493    return zend_parse_arg_str_weak(arg, dest);
494}
495/* }}} */
496
497static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, const char **spec, char **error, int *severity) /* {{{ */
498{
499    const char *spec_walk = *spec;
500    char c = *spec_walk++;
501    int check_null = 0;
502    zval *real_arg = arg;
503
504    /* scan through modifiers */
505    ZVAL_DEREF(arg);
506    while (1) {
507        if (*spec_walk == '/') {
508            SEPARATE_ZVAL(arg);
509            real_arg = arg;
510        } else if (*spec_walk == '!') {
511            check_null = 1;
512        } else {
513            break;
514        }
515        spec_walk++;
516    }
517
518    switch (c) {
519        case 'l':
520        case 'L':
521            {
522                zend_long *p = va_arg(*va, zend_long *);
523                zend_bool *is_null = NULL;
524
525                if (check_null) {
526                    is_null = va_arg(*va, zend_bool *);
527                }
528
529                if (!zend_parse_arg_long(arg, p, is_null, check_null, c == 'L')) {
530                    return "integer";
531                }
532            }
533            break;
534
535        case 'd':
536            {
537                double *p = va_arg(*va, double *);
538                zend_bool *is_null = NULL;
539
540                if (check_null) {
541                    is_null = va_arg(*va, zend_bool *);
542                }
543
544                if (!zend_parse_arg_double(arg, p, is_null, check_null)) {
545                    return "float";
546                }
547            }
548            break;
549
550        case 's':
551            {
552                char **p = va_arg(*va, char **);
553                size_t *pl = va_arg(*va, size_t *);
554                if (!zend_parse_arg_string(arg, p, pl, check_null)) {
555                    return "string";
556                }
557            }
558            break;
559
560        case 'p':
561            {
562                char **p = va_arg(*va, char **);
563                size_t *pl = va_arg(*va, size_t *);
564                if (!zend_parse_arg_path(arg, p, pl, check_null)) {
565                    return "a valid path";
566                }
567            }
568            break;
569
570        case 'P':
571            {
572                zend_string **str = va_arg(*va, zend_string **);
573                if (!zend_parse_arg_path_str(arg, str, check_null)) {
574                    return "a valid path";
575                }
576            }
577            break;
578
579        case 'S':
580            {
581                zend_string **str = va_arg(*va, zend_string **);
582                if (!zend_parse_arg_str(arg, str, check_null)) {
583                    return "string";
584                }
585            }
586            break;
587
588        case 'b':
589            {
590                zend_bool *p = va_arg(*va, zend_bool *);
591                zend_bool *is_null = NULL;
592
593                if (check_null) {
594                    is_null = va_arg(*va, zend_bool *);
595                }
596
597                if (!zend_parse_arg_bool(arg, p, is_null, check_null)) {
598                    return "boolean";
599                }
600            }
601            break;
602
603        case 'r':
604            {
605                zval **p = va_arg(*va, zval **);
606
607                if (!zend_parse_arg_resource(arg, p, check_null)) {
608                    return "resource";
609                }
610            }
611            break;
612
613        case 'A':
614        case 'a':
615            {
616                zval **p = va_arg(*va, zval **);
617
618                if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
619                    return "array";
620                }
621            }
622            break;
623
624        case 'H':
625        case 'h':
626            {
627                HashTable **p = va_arg(*va, HashTable **);
628
629                if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H')) {
630                    return "array";
631                }
632            }
633            break;
634
635        case 'o':
636            {
637                zval **p = va_arg(*va, zval **);
638
639                if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
640                    return "object";
641                }
642            }
643            break;
644
645        case 'O':
646            {
647                zval **p = va_arg(*va, zval **);
648                zend_class_entry *ce = va_arg(*va, zend_class_entry *);
649
650                if (!zend_parse_arg_object(arg, p, ce, check_null)) {
651                    if (ce) {
652                        return ZSTR_VAL(ce->name);
653                    } else {
654                        return "object";
655                    }
656                }
657            }
658            break;
659
660        case 'C':
661            {
662                zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
663                zend_class_entry *ce_base = *pce;
664
665                if (check_null && Z_TYPE_P(arg) == IS_NULL) {
666                    *pce = NULL;
667                    break;
668                }
669                convert_to_string_ex(arg);
670                if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
671                    *pce = NULL;
672                } else {
673                    *pce = lookup;
674                }
675                if (ce_base) {
676                    if ((!*pce || !instanceof_function(*pce, ce_base))) {
677                        zend_spprintf(error, 0, "to be a class name derived from %s, '%s' given",
678                            ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
679                        *pce = NULL;
680                        return "";
681                    }
682                }
683                if (!*pce) {
684                    zend_spprintf(error, 0, "to be a valid class name, '%s' given",
685                        Z_STRVAL_P(arg));
686                    return "";
687                }
688                break;
689
690            }
691            break;
692
693        case 'f':
694            {
695                zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
696                zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
697                char *is_callable_error = NULL;
698
699                if (check_null && Z_TYPE_P(arg) == IS_NULL) {
700                    fci->size = 0;
701                    fcc->initialized = 0;
702                    break;
703                }
704
705                if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
706                    if (is_callable_error) {
707                        *severity = E_DEPRECATED;
708                        zend_spprintf(error, 0, "to be a valid callback, %s", is_callable_error);
709                        efree(is_callable_error);
710                        *spec = spec_walk;
711                        return "";
712                    }
713                    break;
714                } else {
715                    if (is_callable_error) {
716                        *severity = E_ERROR;
717                        zend_spprintf(error, 0, "to be a valid callback, %s", is_callable_error);
718                        efree(is_callable_error);
719                        return "";
720                    } else {
721                        return "valid callback";
722                    }
723                }
724            }
725
726        case 'z':
727            {
728                zval **p = va_arg(*va, zval **);
729
730                zend_parse_arg_zval_deref(real_arg, p, check_null);
731            }
732            break;
733
734        case 'Z':
735            /* 'Z' iz not supported anymore and should be replaced with 'z' */
736            ZEND_ASSERT(c != 'Z');
737        default:
738            return "unknown";
739    }
740
741    *spec = spec_walk;
742
743    return NULL;
744}
745/* }}} */
746
747static int zend_parse_arg(int arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
748{
749    const char *expected_type = NULL;
750    char *error = NULL;
751    int severity = 0;
752
753    expected_type = zend_parse_arg_impl(arg_num, arg, va, spec, &error, &severity);
754    if (expected_type) {
755        if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
756            const char *space;
757            const char *class_name = get_active_class_name(&space);
758            zend_bool throw_exception =
759                ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW);
760
761            if (error) {
762                zend_internal_type_error(throw_exception, "%s%s%s() expects parameter %d %s",
763                        class_name, space, get_active_function_name(), arg_num, error);
764                efree(error);
765            } else {
766                zend_internal_type_error(throw_exception,
767                        "%s%s%s() expects parameter %d to be %s, %s given",
768                        class_name, space, get_active_function_name(), arg_num, expected_type,
769                        zend_zval_type_name(arg));
770            }
771        }
772        if (severity != E_DEPRECATED) {
773            return FAILURE;
774        }
775    }
776
777    return SUCCESS;
778}
779/* }}} */
780
781ZEND_API int zend_parse_parameter(int flags, int arg_num, zval *arg, const char *spec, ...)
782{
783    va_list va;
784    int ret;
785
786    va_start(va, spec);
787    ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
788    va_end(va);
789
790    return ret;
791}
792
793static void zend_parse_parameters_debug_error(const char *msg) {
794    zend_function *active_function = EG(current_execute_data)->func;
795    const char *class_name = active_function->common.scope
796        ? ZSTR_VAL(active_function->common.scope->name) : "";
797    zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
798        class_name, class_name[0] ? "::" : "",
799        ZSTR_VAL(active_function->common.function_name), msg);
800}
801
802static int zend_parse_va_args(int num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
803{
804    const  char *spec_walk;
805    int c, i;
806    int min_num_args = -1;
807    int max_num_args = 0;
808    int post_varargs = 0;
809    zval *arg;
810    int arg_count;
811    zend_bool have_varargs = 0;
812    zval **varargs = NULL;
813    int *n_varargs = NULL;
814
815    for (spec_walk = type_spec; *spec_walk; spec_walk++) {
816        c = *spec_walk;
817        switch (c) {
818            case 'l': case 'd':
819            case 's': case 'b':
820            case 'r': case 'a':
821            case 'o': case 'O':
822            case 'z': case 'Z':
823            case 'C': case 'h':
824            case 'f': case 'A':
825            case 'H': case 'p':
826            case 'S': case 'P':
827            case 'L':
828                max_num_args++;
829                break;
830
831            case '|':
832                min_num_args = max_num_args;
833                break;
834
835            case '/':
836            case '!':
837                /* Pass */
838                break;
839
840            case '*':
841            case '+':
842                if (have_varargs) {
843                    zend_parse_parameters_debug_error(
844                        "only one varargs specifier (* or +) is permitted");
845                    return FAILURE;
846                }
847                have_varargs = 1;
848                /* we expect at least one parameter in varargs */
849                if (c == '+') {
850                    max_num_args++;
851                }
852                /* mark the beginning of varargs */
853                post_varargs = max_num_args;
854                break;
855
856            default:
857                zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
858                return FAILURE;
859        }
860    }
861
862    if (min_num_args < 0) {
863        min_num_args = max_num_args;
864    }
865
866    if (have_varargs) {
867        /* calculate how many required args are at the end of the specifier list */
868        post_varargs = max_num_args - post_varargs;
869        max_num_args = -1;
870    }
871
872    if (num_args < min_num_args || (num_args > max_num_args && max_num_args > 0)) {
873        if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
874            zend_function *active_function = EG(current_execute_data)->func;
875            const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
876            zend_bool throw_exception =
877                ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW);
878            zend_internal_type_error(throw_exception, "%s%s%s() expects %s %d parameter%s, %d given",
879                    class_name,
880                    class_name[0] ? "::" : "",
881                    ZSTR_VAL(active_function->common.function_name),
882                    min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
883                    num_args < min_num_args ? min_num_args : max_num_args,
884                    (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
885                    num_args);
886        }
887        return FAILURE;
888    }
889
890    arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
891
892    if (num_args > arg_count) {
893        zend_parse_parameters_debug_error("could not obtain parameters for parsing");
894        return FAILURE;
895    }
896
897    i = 0;
898    while (num_args-- > 0) {
899        if (*type_spec == '|') {
900            type_spec++;
901        }
902
903        if (*type_spec == '*' || *type_spec == '+') {
904            int num_varargs = num_args + 1 - post_varargs;
905
906            /* eat up the passed in storage even if it won't be filled in with varargs */
907            varargs = va_arg(*va, zval **);
908            n_varargs = va_arg(*va, int *);
909            type_spec++;
910
911            if (num_varargs > 0) {
912                *n_varargs = num_varargs;
913                *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
914                /* adjust how many args we have left and restart loop */
915                num_args += 1 - num_varargs;
916                i += num_varargs;
917                continue;
918            } else {
919                *varargs = NULL;
920                *n_varargs = 0;
921            }
922        }
923
924        arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
925
926        if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
927            /* clean up varargs array if it was used */
928            if (varargs && *varargs) {
929                *varargs = NULL;
930            }
931            return FAILURE;
932        }
933        i++;
934    }
935
936    return SUCCESS;
937}
938/* }}} */
939
940#define RETURN_IF_ZERO_ARGS(num_args, type_spec, flags) do { \
941    int __num_args = (num_args); \
942    if (0 == (type_spec)[0] && 0 != __num_args && !(flags & ZEND_PARSE_PARAMS_QUIET)) { \
943        const char *__space; \
944        const char * __class_name = get_active_class_name(&__space); \
945        zend_bool throw_exception = \
946            ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW); \
947        zend_internal_type_error(throw_exception, \
948            "%s%s%s() expects exactly 0 parameters, %d given", \
949            __class_name, __space, get_active_function_name(), __num_args); \
950        return FAILURE; \
951    } \
952} while(0)
953
954ZEND_API int zend_parse_parameters_ex(int flags, int num_args, const char *type_spec, ...) /* {{{ */
955{
956    va_list va;
957    int retval;
958
959    RETURN_IF_ZERO_ARGS(num_args, type_spec, flags);
960
961    va_start(va, type_spec);
962    retval = zend_parse_va_args(num_args, type_spec, &va, flags);
963    va_end(va);
964
965    return retval;
966}
967/* }}} */
968
969ZEND_API int zend_parse_parameters(int num_args, const char *type_spec, ...) /* {{{ */
970{
971    va_list va;
972    int retval;
973    int flags = 0;
974
975    RETURN_IF_ZERO_ARGS(num_args, type_spec, flags);
976
977    va_start(va, type_spec);
978    retval = zend_parse_va_args(num_args, type_spec, &va, flags);
979    va_end(va);
980
981    return retval;
982}
983/* }}} */
984
985ZEND_API int zend_parse_parameters_throw(int num_args, const char *type_spec, ...) /* {{{ */
986{
987    va_list va;
988    int retval;
989    int flags = ZEND_PARSE_PARAMS_THROW;
990
991    RETURN_IF_ZERO_ARGS(num_args, type_spec, flags);
992
993    va_start(va, type_spec);
994    retval = zend_parse_va_args(num_args, type_spec, &va, flags);
995    va_end(va);
996
997    return retval;
998}
999/* }}} */
1000
1001ZEND_API int zend_parse_method_parameters(int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1002{
1003    va_list va;
1004    int retval;
1005    int flags = 0;
1006    const char *p = type_spec;
1007    zval **object;
1008    zend_class_entry *ce;
1009
1010    /* Just checking this_ptr is not enough, because fcall_common_helper does not set
1011     * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
1012     * In that case EG(This) would still be the $this from the calling code and we'd take the
1013     * wrong branch here. */
1014    zend_bool is_method = EG(current_execute_data)->func->common.scope != NULL;
1015
1016    if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
1017        RETURN_IF_ZERO_ARGS(num_args, p, flags);
1018
1019        va_start(va, type_spec);
1020        retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1021        va_end(va);
1022    } else {
1023        p++;
1024        RETURN_IF_ZERO_ARGS(num_args, p, flags);
1025
1026        va_start(va, type_spec);
1027
1028        object = va_arg(va, zval **);
1029        ce = va_arg(va, zend_class_entry *);
1030        *object = this_ptr;
1031
1032        if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1033            zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
1034                ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
1035        }
1036
1037        retval = zend_parse_va_args(num_args, p, &va, flags);
1038        va_end(va);
1039    }
1040    return retval;
1041}
1042/* }}} */
1043
1044ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1045{
1046    va_list va;
1047    int retval;
1048    const char *p = type_spec;
1049    zval **object;
1050    zend_class_entry *ce;
1051
1052    if (!this_ptr) {
1053        RETURN_IF_ZERO_ARGS(num_args, p, flags);
1054
1055        va_start(va, type_spec);
1056        retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1057        va_end(va);
1058    } else {
1059        p++;
1060        RETURN_IF_ZERO_ARGS(num_args, p, flags);
1061
1062        va_start(va, type_spec);
1063
1064        object = va_arg(va, zval **);
1065        ce = va_arg(va, zend_class_entry *);
1066        *object = this_ptr;
1067
1068        if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1069            if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
1070                zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
1071                    ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
1072            }
1073            va_end(va);
1074            return FAILURE;
1075        }
1076
1077        retval = zend_parse_va_args(num_args, p, &va, flags);
1078        va_end(va);
1079    }
1080    return retval;
1081}
1082/* }}} */
1083
1084/* Argument parsing API -- andrei */
1085ZEND_API int _array_init(zval *arg, uint32_t size ZEND_FILE_LINE_DC) /* {{{ */
1086{
1087    ZVAL_NEW_ARR(arg);
1088    _zend_hash_init(Z_ARRVAL_P(arg), size, ZVAL_PTR_DTOR, 0 ZEND_FILE_LINE_RELAY_CC);
1089    return SUCCESS;
1090}
1091/* }}} */
1092
1093/* This function should be called after the constructor has been called
1094 * because it may call __set from the uninitialized object otherwise. */
1095ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
1096{
1097    const zend_object_handlers *obj_ht = Z_OBJ_HT_P(obj);
1098    zend_class_entry *old_scope = EG(scope);
1099    zend_string *key;
1100    zval *value;
1101
1102    EG(scope) = Z_OBJCE_P(obj);
1103    ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, value) {
1104        if (key) {
1105            zval member;
1106
1107            ZVAL_STR(&member, key);
1108            obj_ht->write_property(obj, &member, value, NULL);
1109        }
1110    } ZEND_HASH_FOREACH_END();
1111    EG(scope) = old_scope;
1112}
1113/* }}} */
1114
1115ZEND_API int zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
1116{
1117    if (!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
1118        class_type->ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
1119
1120        if (class_type->parent) {
1121            if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
1122                return FAILURE;
1123            }
1124        }
1125
1126        if (!CE_STATIC_MEMBERS(class_type) && class_type->default_static_members_count) {
1127            /* initialize static members of internal class */
1128            int i;
1129            zval *p;
1130
1131#if ZTS
1132            CG(static_members_table)[(zend_intptr_t)(class_type->static_members_table)] = emalloc(sizeof(zval) * class_type->default_static_members_count);
1133#else
1134            class_type->static_members_table = emalloc(sizeof(zval) * class_type->default_static_members_count);
1135#endif
1136            for (i = 0; i < class_type->default_static_members_count; i++) {
1137                p = &class_type->default_static_members_table[i];
1138                if (Z_ISREF_P(p) &&
1139                    class_type->parent &&
1140                    i < class_type->parent->default_static_members_count &&
1141                    p == &class_type->parent->default_static_members_table[i] &&
1142                    Z_TYPE(CE_STATIC_MEMBERS(class_type->parent)[i]) != IS_UNDEF
1143                ) {
1144                    zval *q = &CE_STATIC_MEMBERS(class_type->parent)[i];
1145
1146                    ZVAL_NEW_REF(q, q);
1147                    ZVAL_COPY_VALUE(&CE_STATIC_MEMBERS(class_type)[i], q);
1148                    Z_ADDREF_P(q);
1149                } else {
1150                    ZVAL_DUP(&CE_STATIC_MEMBERS(class_type)[i], p);
1151                }
1152            }
1153        } else {
1154            zend_class_entry **scope = EG(current_execute_data) ? &EG(scope) : &CG(active_class_entry);
1155            zend_class_entry *old_scope = *scope;
1156            zend_class_entry *ce;
1157            zval *val;
1158            zend_property_info *prop_info;
1159
1160            *scope = class_type;
1161            ZEND_HASH_FOREACH_VAL(&class_type->constants_table, val) {
1162                ZVAL_DEREF(val);
1163                if (Z_CONSTANT_P(val)) {
1164                    if (UNEXPECTED(zval_update_constant_ex(val, 1, class_type) != SUCCESS)) {
1165                        return FAILURE;
1166                    }
1167                }
1168            } ZEND_HASH_FOREACH_END();
1169
1170            ce = class_type;
1171            while (ce) {
1172                ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
1173                    if (prop_info->ce == ce) {
1174                        if (prop_info->flags & ZEND_ACC_STATIC) {
1175                            val = CE_STATIC_MEMBERS(class_type) + prop_info->offset;
1176                        } else {
1177                            val = (zval*)((char*)class_type->default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
1178                        }
1179                        ZVAL_DEREF(val);
1180                        if (Z_CONSTANT_P(val)) {
1181                            *scope = ce;
1182                            if (UNEXPECTED(zval_update_constant_ex(val, 1, NULL) != SUCCESS)) {
1183                                return FAILURE;
1184                            }
1185                        }
1186                    }
1187                } ZEND_HASH_FOREACH_END();
1188                ce = ce->parent;
1189            }
1190
1191            *scope = old_scope;
1192        }
1193    }
1194    return SUCCESS;
1195}
1196/* }}} */
1197
1198ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1199{
1200    if (class_type->default_properties_count) {
1201        zval *src = class_type->default_properties_table;
1202        zval *dst = object->properties_table;
1203        zval *end = src + class_type->default_properties_count;
1204
1205        do {
1206#if ZTS
1207            ZVAL_DUP(dst, src);
1208#else
1209            ZVAL_COPY(dst, src);
1210#endif
1211            src++;
1212            dst++;
1213        } while (src != end);
1214        object->properties = NULL;
1215    }
1216}
1217/* }}} */
1218
1219ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
1220{
1221    object->properties = properties;
1222    if (object->ce->default_properties_count) {
1223        zval *prop;
1224        zend_string *key;
1225        zend_property_info *property_info;
1226
1227        ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
1228            property_info = zend_get_property_info(object->ce, key, 1);
1229            if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1230                property_info &&
1231                (property_info->flags & ZEND_ACC_STATIC) == 0) {
1232                zval *slot = OBJ_PROP(object, property_info->offset);
1233                ZVAL_COPY_VALUE(slot, prop);
1234                ZVAL_INDIRECT(prop, slot);
1235            }
1236        } ZEND_HASH_FOREACH_END();
1237    }
1238}
1239/* }}} */
1240
1241ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */
1242{
1243    zval *prop, tmp;
1244    zend_string *key;
1245    zend_property_info *property_info;
1246
1247    ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
1248        property_info = zend_get_property_info(object->ce, key, 1);
1249        if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1250            property_info &&
1251            (property_info->flags & ZEND_ACC_STATIC) == 0) {
1252            zval *slot = OBJ_PROP(object, property_info->offset);
1253            zval_ptr_dtor(slot);
1254            ZVAL_COPY_VALUE(slot, prop);
1255            zval_add_ref(slot);
1256            if (object->properties) {
1257                ZVAL_INDIRECT(&tmp, slot);
1258                zend_hash_update(object->properties, key, &tmp);
1259            }
1260        } else {
1261            if (!object->properties) {
1262                rebuild_object_properties(object);
1263            }
1264            prop = zend_hash_update(object->properties, key, prop);
1265            zval_add_ref(prop);
1266        }
1267    } ZEND_HASH_FOREACH_END();
1268}
1269/* }}} */
1270
1271/* This function requires 'properties' to contain all props declared in the
1272 * class and all props being public. If only a subset is given or the class
1273 * has protected members then you need to merge the properties separately by
1274 * calling zend_merge_properties(). */
1275ZEND_API int _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties ZEND_FILE_LINE_DC) /* {{{ */
1276{
1277    if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
1278        if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
1279            zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
1280        } else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
1281            zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
1282        } else {
1283            zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
1284        }
1285        ZVAL_NULL(arg);
1286        Z_OBJ_P(arg) = NULL;
1287        return FAILURE;
1288    }
1289
1290    if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1291        if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
1292            ZVAL_NULL(arg);
1293            Z_OBJ_P(arg) = NULL;
1294            return FAILURE;
1295        }
1296    }
1297
1298    if (class_type->create_object == NULL) {
1299        ZVAL_OBJ(arg, zend_objects_new(class_type));
1300        if (properties) {
1301            object_properties_init_ex(Z_OBJ_P(arg), properties);
1302        } else {
1303            object_properties_init(Z_OBJ_P(arg), class_type);
1304        }
1305    } else {
1306        ZVAL_OBJ(arg, class_type->create_object(class_type));
1307    }
1308    return SUCCESS;
1309}
1310/* }}} */
1311
1312ZEND_API int _object_init_ex(zval *arg, zend_class_entry *class_type ZEND_FILE_LINE_DC) /* {{{ */
1313{
1314    return _object_and_properties_init(arg, class_type, 0 ZEND_FILE_LINE_RELAY_CC);
1315}
1316/* }}} */
1317
1318ZEND_API int _object_init(zval *arg ZEND_FILE_LINE_DC) /* {{{ */
1319{
1320    return _object_init_ex(arg, zend_standard_class_def ZEND_FILE_LINE_RELAY_CC);
1321}
1322/* }}} */
1323
1324ZEND_API int add_assoc_function(zval *arg, const char *key, void (*function_ptr)(INTERNAL_FUNCTION_PARAMETERS)) /* {{{ */
1325{
1326    zend_error(E_WARNING, "add_assoc_function() is no longer supported");
1327    return FAILURE;
1328}
1329/* }}} */
1330
1331ZEND_API int add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1332{
1333    zval *ret, tmp;
1334
1335    ZVAL_LONG(&tmp, n);
1336    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1337    return ret ? SUCCESS : FAILURE;
1338}
1339/* }}} */
1340
1341ZEND_API int add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1342{
1343    zval *ret, tmp;
1344
1345    ZVAL_NULL(&tmp);
1346    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1347    return ret ? SUCCESS : FAILURE;
1348}
1349/* }}} */
1350
1351ZEND_API int add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, int b) /* {{{ */
1352{
1353    zval *ret, tmp;
1354
1355    ZVAL_BOOL(&tmp, b);
1356    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1357    return ret ? SUCCESS : FAILURE;
1358}
1359/* }}} */
1360
1361ZEND_API int add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1362{
1363    zval *ret, tmp;
1364
1365    ZVAL_RES(&tmp, r);
1366    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1367    return ret ? SUCCESS : FAILURE;
1368}
1369/* }}} */
1370
1371ZEND_API int add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1372{
1373    zval *ret, tmp;
1374
1375    ZVAL_DOUBLE(&tmp, d);
1376    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1377    return ret ? SUCCESS : FAILURE;
1378}
1379/* }}} */
1380
1381ZEND_API int add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1382{
1383    zval *ret, tmp;
1384
1385    ZVAL_STR(&tmp, str);
1386    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1387    return ret ? SUCCESS : FAILURE;
1388}
1389/* }}} */
1390
1391ZEND_API int add_assoc_string_ex(zval *arg, const char *key, size_t key_len, char *str) /* {{{ */
1392{
1393    zval *ret, tmp;
1394
1395    ZVAL_STRING(&tmp, str);
1396    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1397    return ret ? SUCCESS : FAILURE;
1398}
1399/* }}} */
1400
1401ZEND_API int add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, char *str, size_t length) /* {{{ */
1402{
1403    zval *ret, tmp;
1404
1405    ZVAL_STRINGL(&tmp, str, length);
1406    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1407    return ret ? SUCCESS : FAILURE;
1408}
1409/* }}} */
1410
1411ZEND_API int add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
1412{
1413    zval *ret;
1414
1415    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
1416    return ret ? SUCCESS : FAILURE;
1417}
1418/* }}} */
1419
1420ZEND_API int add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
1421{
1422    zval tmp;
1423
1424    ZVAL_LONG(&tmp, n);
1425    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1426}
1427/* }}} */
1428
1429ZEND_API int add_index_null(zval *arg, zend_ulong index) /* {{{ */
1430{
1431    zval tmp;
1432
1433    ZVAL_NULL(&tmp);
1434    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1435}
1436/* }}} */
1437
1438ZEND_API int add_index_bool(zval *arg, zend_ulong index, int b) /* {{{ */
1439{
1440    zval tmp;
1441
1442    ZVAL_BOOL(&tmp, b);
1443    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1444}
1445/* }}} */
1446
1447ZEND_API int add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
1448{
1449    zval tmp;
1450
1451    ZVAL_RES(&tmp, r);
1452    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1453}
1454/* }}} */
1455
1456ZEND_API int add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
1457{
1458    zval tmp;
1459
1460    ZVAL_DOUBLE(&tmp, d);
1461    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1462}
1463/* }}} */
1464
1465ZEND_API int add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
1466{
1467    zval tmp;
1468
1469    ZVAL_STR(&tmp, str);
1470    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1471}
1472/* }}} */
1473
1474ZEND_API int add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
1475{
1476    zval tmp;
1477
1478    ZVAL_STRING(&tmp, str);
1479    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1480}
1481/* }}} */
1482
1483ZEND_API int add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
1484{
1485    zval tmp;
1486
1487    ZVAL_STRINGL(&tmp, str, length);
1488    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1489}
1490/* }}} */
1491
1492ZEND_API int add_index_zval(zval *arg, zend_ulong index, zval *value) /* {{{ */
1493{
1494    return zend_hash_index_update(Z_ARRVAL_P(arg), index, value) ? SUCCESS : FAILURE;
1495}
1496/* }}} */
1497
1498ZEND_API int add_next_index_long(zval *arg, zend_long n) /* {{{ */
1499{
1500    zval tmp;
1501
1502    ZVAL_LONG(&tmp, n);
1503    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1504}
1505/* }}} */
1506
1507ZEND_API int add_next_index_null(zval *arg) /* {{{ */
1508{
1509    zval tmp;
1510
1511    ZVAL_NULL(&tmp);
1512    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1513}
1514/* }}} */
1515
1516ZEND_API int add_next_index_bool(zval *arg, int b) /* {{{ */
1517{
1518    zval tmp;
1519
1520    ZVAL_BOOL(&tmp, b);
1521    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1522}
1523/* }}} */
1524
1525ZEND_API int add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
1526{
1527    zval tmp;
1528
1529    ZVAL_RES(&tmp, r);
1530    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1531}
1532/* }}} */
1533
1534ZEND_API int add_next_index_double(zval *arg, double d) /* {{{ */
1535{
1536    zval tmp;
1537
1538    ZVAL_DOUBLE(&tmp, d);
1539    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1540}
1541/* }}} */
1542
1543ZEND_API int add_next_index_str(zval *arg, zend_string *str) /* {{{ */
1544{
1545    zval tmp;
1546
1547    ZVAL_STR(&tmp, str);
1548    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1549}
1550/* }}} */
1551
1552ZEND_API int add_next_index_string(zval *arg, const char *str) /* {{{ */
1553{
1554    zval tmp;
1555
1556    ZVAL_STRING(&tmp, str);
1557    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1558}
1559/* }}} */
1560
1561ZEND_API int add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
1562{
1563    zval tmp;
1564
1565    ZVAL_STRINGL(&tmp, str, length);
1566    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1567}
1568/* }}} */
1569
1570ZEND_API int add_next_index_zval(zval *arg, zval *value) /* {{{ */
1571{
1572    return zend_hash_next_index_insert(Z_ARRVAL_P(arg), value) ? SUCCESS : FAILURE;
1573}
1574/* }}} */
1575
1576ZEND_API zval *add_get_assoc_string_ex(zval *arg, const char *key, uint key_len, const char *str) /* {{{ */
1577{
1578    zval tmp, *ret;
1579
1580    ZVAL_STRING(&tmp, str);
1581    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1582    return ret;
1583}
1584/* }}} */
1585
1586ZEND_API zval *add_get_assoc_stringl_ex(zval *arg, const char *key, uint key_len, const char *str, size_t length) /* {{{ */
1587{
1588    zval tmp, *ret;
1589
1590    ZVAL_STRINGL(&tmp, str, length);
1591    ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1592    return ret;
1593}
1594/* }}} */
1595
1596ZEND_API zval *add_get_index_long(zval *arg, zend_ulong index, zend_long l) /* {{{ */
1597{
1598    zval tmp;
1599
1600    ZVAL_LONG(&tmp, l);
1601    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1602}
1603/* }}} */
1604
1605ZEND_API zval *add_get_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
1606{
1607    zval tmp;
1608
1609    ZVAL_DOUBLE(&tmp, d);
1610    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1611}
1612/* }}} */
1613
1614ZEND_API zval *add_get_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
1615{
1616    zval tmp;
1617
1618    ZVAL_STR(&tmp, str);
1619    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1620}
1621/* }}} */
1622
1623ZEND_API zval *add_get_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
1624{
1625    zval tmp;
1626
1627    ZVAL_STRING(&tmp, str);
1628    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1629}
1630/* }}} */
1631
1632ZEND_API zval *add_get_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
1633{
1634    zval tmp;
1635
1636    ZVAL_STRINGL(&tmp, str, length);
1637    return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1638}
1639/* }}} */
1640
1641ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
1642{
1643    zval *result;
1644
1645    switch (Z_TYPE_P(key)) {
1646        case IS_STRING:
1647            result = zend_symtable_update(ht, Z_STR_P(key), value);
1648            break;
1649        case IS_NULL:
1650            result = zend_symtable_update(ht, ZSTR_EMPTY_ALLOC(), value);
1651            break;
1652        case IS_RESOURCE:
1653            zend_error(E_NOTICE, "Resource ID#" ZEND_LONG_FMT " used as offset, casting to integer (%pd)", Z_RES_HANDLE_P(key), Z_RES_HANDLE_P(key));
1654            result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
1655            break;
1656        case IS_FALSE:
1657            result = zend_hash_index_update(ht, 0, value);
1658            break;
1659        case IS_TRUE:
1660            result = zend_hash_index_update(ht, 1, value);
1661            break;
1662        case IS_LONG:
1663            result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
1664            break;
1665        case IS_DOUBLE:
1666            result = zend_hash_index_update(ht, zend_dval_to_lval(Z_DVAL_P(key)), value);
1667            break;
1668        default:
1669            zend_error(E_WARNING, "Illegal offset type");
1670            result = NULL;
1671    }
1672
1673    if (result) {
1674        if (Z_REFCOUNTED_P(result)) {
1675            Z_ADDREF_P(result);
1676        }
1677        return SUCCESS;
1678    } else {
1679        return FAILURE;
1680    }
1681}
1682/* }}} */
1683
1684ZEND_API int add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1685{
1686    zval tmp;
1687    zval z_key;
1688
1689    ZVAL_LONG(&tmp, n);
1690    ZVAL_STRINGL(&z_key, key, key_len);
1691    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1692    zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1693    zval_ptr_dtor(&z_key);
1694    return SUCCESS;
1695}
1696/* }}} */
1697
1698ZEND_API int add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
1699{
1700    zval tmp;
1701    zval z_key;
1702
1703    ZVAL_BOOL(&tmp, b);
1704    ZVAL_STRINGL(&z_key, key, key_len);
1705    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1706    zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1707    zval_ptr_dtor(&z_key);
1708    return SUCCESS;
1709}
1710/* }}} */
1711
1712ZEND_API int add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1713{
1714    zval tmp;
1715    zval z_key;
1716
1717    ZVAL_NULL(&tmp);
1718    ZVAL_STRINGL(&z_key, key, key_len);
1719    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1720    zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1721    zval_ptr_dtor(&z_key);
1722    return SUCCESS;
1723}
1724/* }}} */
1725
1726ZEND_API int add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1727{
1728    zval tmp;
1729    zval z_key;
1730
1731    ZVAL_RES(&tmp, r);
1732    ZVAL_STRINGL(&z_key, key, key_len);
1733    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1734    zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1735    zval_ptr_dtor(&z_key);
1736    return SUCCESS;
1737}
1738/* }}} */
1739
1740ZEND_API int add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1741{
1742    zval tmp;
1743    zval z_key;
1744
1745    ZVAL_DOUBLE(&tmp, d);
1746    ZVAL_STRINGL(&z_key, key, key_len);
1747    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1748    zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1749    zval_ptr_dtor(&z_key);
1750    return SUCCESS;
1751}
1752/* }}} */
1753
1754ZEND_API int add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1755{
1756    zval tmp;
1757    zval z_key;
1758
1759    ZVAL_STR(&tmp, str);
1760    ZVAL_STRINGL(&z_key, key, key_len);
1761    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1762    zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1763    zval_ptr_dtor(&z_key);
1764    return SUCCESS;
1765}
1766/* }}} */
1767
1768ZEND_API int add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
1769{
1770    zval tmp;
1771    zval z_key;
1772
1773    ZVAL_STRING(&tmp, str);
1774    ZVAL_STRINGL(&z_key, key, key_len);
1775    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1776    zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1777    zval_ptr_dtor(&z_key);
1778    return SUCCESS;
1779}
1780/* }}} */
1781
1782ZEND_API int add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
1783{
1784    zval tmp;
1785    zval z_key;
1786
1787    ZVAL_STRINGL(&tmp, str, length);
1788    ZVAL_STRINGL(&z_key, key, key_len);
1789    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1790    zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1791    zval_ptr_dtor(&z_key);
1792    return SUCCESS;
1793}
1794/* }}} */
1795
1796ZEND_API int add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
1797{
1798    zval z_key;
1799
1800    ZVAL_STRINGL(&z_key, key, key_len);
1801    Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, value, NULL);
1802    zval_ptr_dtor(&z_key);
1803    return SUCCESS;
1804}
1805/* }}} */
1806
1807ZEND_API int zend_startup_module_ex(zend_module_entry *module) /* {{{ */
1808{
1809    size_t name_len;
1810    zend_string *lcname;
1811
1812    if (module->module_started) {
1813        return SUCCESS;
1814    }
1815    module->module_started = 1;
1816
1817    /* Check module dependencies */
1818    if (module->deps) {
1819        const zend_module_dep *dep = module->deps;
1820
1821        while (dep->name) {
1822            if (dep->type == MODULE_DEP_REQUIRED) {
1823                zend_module_entry *req_mod;
1824
1825                name_len = strlen(dep->name);
1826                lcname = zend_string_alloc(name_len, 0);
1827                zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
1828
1829                if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
1830                    zend_string_free(lcname);
1831                    /* TODO: Check version relationship */
1832                    zend_error(E_CORE_WARNING, "Cannot load module '%s' because required module '%s' is not loaded", module->name, dep->name);
1833                    module->module_started = 0;
1834                    return FAILURE;
1835                }
1836                zend_string_free(lcname);
1837            }
1838            ++dep;
1839        }
1840    }
1841
1842    /* Initialize module globals */
1843    if (module->globals_size) {
1844#ifdef ZTS
1845        ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
1846#else
1847        if (module->globals_ctor) {
1848            module->globals_ctor(module->globals_ptr);
1849        }
1850#endif
1851    }
1852    if (module->module_startup_func) {
1853        EG(current_module) = module;
1854        if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
1855            zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
1856            EG(current_module) = NULL;
1857            return FAILURE;
1858        }
1859        EG(current_module) = NULL;
1860    }
1861    return SUCCESS;
1862}
1863/* }}} */
1864
1865static int zend_startup_module_zval(zval *zv) /* {{{ */
1866{
1867    zend_module_entry *module = Z_PTR_P(zv);
1868
1869    return zend_startup_module_ex(module);
1870}
1871/* }}} */
1872
1873static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
1874{
1875    Bucket *b1 = base;
1876    Bucket *b2;
1877    Bucket *end = b1 + count;
1878    Bucket tmp;
1879    zend_module_entry *m, *r;
1880
1881    while (b1 < end) {
1882try_again:
1883        m = (zend_module_entry*)Z_PTR(b1->val);
1884        if (!m->module_started && m->deps) {
1885            const zend_module_dep *dep = m->deps;
1886            while (dep->name) {
1887                if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
1888                    b2 = b1 + 1;
1889                    while (b2 < end) {
1890                        r = (zend_module_entry*)Z_PTR(b2->val);
1891                        if (strcasecmp(dep->name, r->name) == 0) {
1892                            tmp = *b1;
1893                            *b1 = *b2;
1894                            *b2 = tmp;
1895                            goto try_again;
1896                        }
1897                        b2++;
1898                    }
1899                }
1900                dep++;
1901            }
1902        }
1903        b1++;
1904    }
1905}
1906/* }}} */
1907
1908ZEND_API void zend_collect_module_handlers(void) /* {{{ */
1909{
1910    zend_module_entry *module;
1911    int startup_count = 0;
1912    int shutdown_count = 0;
1913    int post_deactivate_count = 0;
1914    zend_class_entry *ce;
1915    int class_count = 0;
1916
1917    /* Collect extensions with request startup/shutdown handlers */
1918    ZEND_HASH_FOREACH_PTR(&module_registry, module) {
1919        if (module->request_startup_func) {
1920            startup_count++;
1921        }
1922        if (module->request_shutdown_func) {
1923            shutdown_count++;
1924        }
1925        if (module->post_deactivate_func) {
1926            post_deactivate_count++;
1927        }
1928    } ZEND_HASH_FOREACH_END();
1929    module_request_startup_handlers = (zend_module_entry**)malloc(
1930        sizeof(zend_module_entry*) *
1931        (startup_count + 1 +
1932         shutdown_count + 1 +
1933         post_deactivate_count + 1));
1934    module_request_startup_handlers[startup_count] = NULL;
1935    module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
1936    module_request_shutdown_handlers[shutdown_count] = NULL;
1937    module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
1938    module_post_deactivate_handlers[post_deactivate_count] = NULL;
1939    startup_count = 0;
1940
1941    ZEND_HASH_FOREACH_PTR(&module_registry, module) {
1942        if (module->request_startup_func) {
1943            module_request_startup_handlers[startup_count++] = module;
1944        }
1945        if (module->request_shutdown_func) {
1946            module_request_shutdown_handlers[--shutdown_count] = module;
1947        }
1948        if (module->post_deactivate_func) {
1949            module_post_deactivate_handlers[--post_deactivate_count] = module;
1950        }
1951    } ZEND_HASH_FOREACH_END();
1952
1953    /* Collect internal classes with static members */
1954    ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
1955        if (ce->type == ZEND_INTERNAL_CLASS &&
1956            ce->default_static_members_count > 0) {
1957            class_count++;
1958        }
1959    } ZEND_HASH_FOREACH_END();
1960
1961    class_cleanup_handlers = (zend_class_entry**)malloc(
1962        sizeof(zend_class_entry*) *
1963        (class_count + 1));
1964    class_cleanup_handlers[class_count] = NULL;
1965
1966    if (class_count) {
1967        ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
1968            if (ce->type == ZEND_INTERNAL_CLASS &&
1969                ce->default_static_members_count > 0) {
1970                class_cleanup_handlers[--class_count] = ce;
1971            }
1972        } ZEND_HASH_FOREACH_END();
1973    }
1974}
1975/* }}} */
1976
1977ZEND_API int zend_startup_modules(void) /* {{{ */
1978{
1979    zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
1980    zend_hash_apply(&module_registry, zend_startup_module_zval);
1981    return SUCCESS;
1982}
1983/* }}} */
1984
1985ZEND_API void zend_destroy_modules(void) /* {{{ */
1986{
1987    free(class_cleanup_handlers);
1988    free(module_request_startup_handlers);
1989    zend_hash_graceful_reverse_destroy(&module_registry);
1990}
1991/* }}} */
1992
1993ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module) /* {{{ */
1994{
1995    size_t name_len;
1996    zend_string *lcname;
1997    zend_module_entry *module_ptr;
1998
1999    if (!module) {
2000        return NULL;
2001    }
2002
2003#if 0
2004    zend_printf("%s: Registering module %d\n", module->name, module->module_number);
2005#endif
2006
2007    /* Check module dependencies */
2008    if (module->deps) {
2009        const zend_module_dep *dep = module->deps;
2010
2011        while (dep->name) {
2012            if (dep->type == MODULE_DEP_CONFLICTS) {
2013                name_len = strlen(dep->name);
2014                lcname = zend_string_alloc(name_len, 0);
2015                zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2016
2017                if (zend_hash_exists(&module_registry, lcname)) {
2018                    zend_string_free(lcname);
2019                    /* TODO: Check version relationship */
2020                    zend_error(E_CORE_WARNING, "Cannot load module '%s' because conflicting module '%s' is already loaded", module->name, dep->name);
2021                    return NULL;
2022                }
2023                zend_string_free(lcname);
2024            }
2025            ++dep;
2026        }
2027    }
2028
2029    name_len = strlen(module->name);
2030    lcname = zend_string_alloc(name_len, 1);
2031    zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
2032
2033    if ((module_ptr = zend_hash_add_mem(&module_registry, lcname, module, sizeof(zend_module_entry))) == NULL) {
2034        zend_error(E_CORE_WARNING, "Module '%s' already loaded", module->name);
2035        zend_string_release(lcname);
2036        return NULL;
2037    }
2038    zend_string_release(lcname);
2039    module = module_ptr;
2040    EG(current_module) = module;
2041
2042    if (module->functions && zend_register_functions(NULL, module->functions, NULL, module->type)==FAILURE) {
2043        EG(current_module) = NULL;
2044        zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
2045        return NULL;
2046    }
2047
2048    EG(current_module) = NULL;
2049    return module;
2050}
2051/* }}} */
2052
2053ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
2054{
2055    module->module_number = zend_next_free_module();
2056    module->type = MODULE_PERSISTENT;
2057    return zend_register_module_ex(module);
2058}
2059/* }}} */
2060
2061ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, int error_type) /* {{{ */
2062{
2063    char lcname[16];
2064    size_t name_len;
2065
2066    /* we don't care if the function name is longer, in fact lowercasing only
2067     * the beginning of the name speeds up the check process */
2068    name_len = ZSTR_LEN(fptr->common.function_name);
2069    zend_str_tolower_copy(lcname, ZSTR_VAL(fptr->common.function_name), MIN(name_len, sizeof(lcname)-1));
2070    lcname[sizeof(lcname)-1] = '\0'; /* zend_str_tolower_copy won't necessarily set the zero byte */
2071
2072    if (name_len == sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_DESTRUCTOR_FUNC_NAME, sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1) && fptr->common.num_args != 0) {
2073        zend_error(error_type, "Destructor %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_DESTRUCTOR_FUNC_NAME);
2074    } else if (name_len == sizeof(ZEND_CLONE_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME) - 1) && fptr->common.num_args != 0) {
2075        zend_error(error_type, "Method %s::%s() cannot accept any arguments", ZSTR_VAL(ce->name), ZEND_CLONE_FUNC_NAME);
2076    } else if (name_len == sizeof(ZEND_GET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME) - 1)) {
2077        if (fptr->common.num_args != 1) {
2078            zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_GET_FUNC_NAME);
2079        } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2080            zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_GET_FUNC_NAME);
2081        }
2082    } else if (name_len == sizeof(ZEND_SET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME) - 1)) {
2083        if (fptr->common.num_args != 2) {
2084            zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ZSTR_VAL(ce->name), ZEND_SET_FUNC_NAME);
2085        } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2086            zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_SET_FUNC_NAME);
2087        }
2088    } else if (name_len == sizeof(ZEND_UNSET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_UNSET_FUNC_NAME, sizeof(ZEND_UNSET_FUNC_NAME) - 1)) {
2089        if (fptr->common.num_args != 1) {
2090            zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_UNSET_FUNC_NAME);
2091        } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2092            zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_UNSET_FUNC_NAME);
2093        }
2094    } else if (name_len == sizeof(ZEND_ISSET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_ISSET_FUNC_NAME, sizeof(ZEND_ISSET_FUNC_NAME) - 1)) {
2095        if (fptr->common.num_args != 1) {
2096            zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_ISSET_FUNC_NAME);
2097        } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2098            zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_ISSET_FUNC_NAME);
2099        }
2100    } else if (name_len == sizeof(ZEND_CALL_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME) - 1)) {
2101        if (fptr->common.num_args != 2) {
2102            zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ZSTR_VAL(ce->name), ZEND_CALL_FUNC_NAME);
2103        } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2104            zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_CALL_FUNC_NAME);
2105        }
2106    } else if (name_len == sizeof(ZEND_CALLSTATIC_FUNC_NAME) - 1 &&
2107        !memcmp(lcname, ZEND_CALLSTATIC_FUNC_NAME, sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1)
2108    ) {
2109        if (fptr->common.num_args != 2) {
2110            zend_error(error_type, "Method %s::__callStatic() must take exactly 2 arguments", ZSTR_VAL(ce->name));
2111        } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2112            zend_error(error_type, "Method %s::__callStatic() cannot take arguments by reference", ZSTR_VAL(ce->name));
2113        }
2114    } else if (name_len == sizeof(ZEND_TOSTRING_FUNC_NAME) - 1 &&
2115        !memcmp(lcname, ZEND_TOSTRING_FUNC_NAME, sizeof(ZEND_TOSTRING_FUNC_NAME)-1) && fptr->common.num_args != 0
2116    ) {
2117        zend_error(error_type, "Method %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_TOSTRING_FUNC_NAME);
2118    } else if (name_len == sizeof(ZEND_DEBUGINFO_FUNC_NAME) - 1 &&
2119        !memcmp(lcname, ZEND_DEBUGINFO_FUNC_NAME, sizeof(ZEND_DEBUGINFO_FUNC_NAME)-1) && fptr->common.num_args != 0) {
2120        zend_error(error_type, "Method %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_DEBUGINFO_FUNC_NAME);
2121    }
2122}
2123/* }}} */
2124
2125/* registers all functions in *library_functions in the function hash */
2126ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
2127{
2128    const zend_function_entry *ptr = functions;
2129    zend_function function, *reg_function;
2130    zend_internal_function *internal_function = (zend_internal_function *)&function;
2131    int count=0, unload=0;
2132    HashTable *target_function_table = function_table;
2133    int error_type;
2134    zend_function *ctor = NULL, *dtor = NULL, *clone = NULL, *__get = NULL, *__set = NULL, *__unset = NULL, *__isset = NULL, *__call = NULL, *__callstatic = NULL, *__tostring = NULL, *__debugInfo = NULL;
2135    zend_string *lowercase_name;
2136    size_t fname_len;
2137    const char *lc_class_name = NULL;
2138    size_t class_name_len = 0;
2139
2140    if (type==MODULE_PERSISTENT) {
2141        error_type = E_CORE_WARNING;
2142    } else {
2143        error_type = E_WARNING;
2144    }
2145
2146    if (!target_function_table) {
2147        target_function_table = CG(function_table);
2148    }
2149    internal_function->type = ZEND_INTERNAL_FUNCTION;
2150    internal_function->module = EG(current_module);
2151
2152    if (scope) {
2153        class_name_len = ZSTR_LEN(scope->name);
2154        if ((lc_class_name = zend_memrchr(ZSTR_VAL(scope->name), '\\', class_name_len))) {
2155            ++lc_class_name;
2156            class_name_len -= (lc_class_name - ZSTR_VAL(scope->name));
2157            lc_class_name = zend_str_tolower_dup(lc_class_name, class_name_len);
2158        } else {
2159            lc_class_name = zend_str_tolower_dup(ZSTR_VAL(scope->name), class_name_len);
2160        }
2161    }
2162
2163    while (ptr->fname) {
2164        fname_len = strlen(ptr->fname);
2165        internal_function->handler = ptr->handler;
2166        internal_function->function_name = zend_new_interned_string(zend_string_init(ptr->fname, fname_len, 1));
2167        internal_function->scope = scope;
2168        internal_function->prototype = NULL;
2169        if (ptr->flags) {
2170            if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
2171                if (ptr->flags != ZEND_ACC_DEPRECATED || scope) {
2172                    zend_error(error_type, "Invalid access level for %s%s%s() - access must be exactly one of public, protected or private", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2173                }
2174                internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
2175            } else {
2176                internal_function->fn_flags = ptr->flags;
2177            }
2178        } else {
2179            internal_function->fn_flags = ZEND_ACC_PUBLIC;
2180        }
2181        if (ptr->arg_info) {
2182            zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
2183
2184            internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
2185            internal_function->num_args = ptr->num_args;
2186            /* Currently you cannot denote that the function can accept less arguments than num_args */
2187            if (info->required_num_args == (zend_uintptr_t)-1) {
2188                internal_function->required_num_args = ptr->num_args;
2189            } else {
2190                internal_function->required_num_args = info->required_num_args;
2191            }
2192            if (info->return_reference) {
2193                internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
2194            }
2195            if (ptr->arg_info[ptr->num_args].is_variadic) {
2196                internal_function->fn_flags |= ZEND_ACC_VARIADIC;
2197                /* Don't count the variadic argument */
2198                internal_function->num_args--;
2199            }
2200            if (info->type_hint) {
2201                if (info->class_name) {
2202                    ZEND_ASSERT(info->type_hint == IS_OBJECT);
2203                    if (!scope && (!strcasecmp(info->class_name, "self") || !strcasecmp(info->class_name, "parent"))) {
2204                        zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", info->class_name);
2205                    }
2206                }
2207
2208                internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
2209            }
2210        } else {
2211            internal_function->arg_info = NULL;
2212            internal_function->num_args = 0;
2213            internal_function->required_num_args = 0;
2214        }
2215        zend_set_function_arg_flags((zend_function*)internal_function);
2216        if (ptr->flags & ZEND_ACC_ABSTRACT) {
2217            if (scope) {
2218                /* This is a class that must be abstract itself. Here we set the check info. */
2219                scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
2220                if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
2221                    /* Since the class is not an interface it needs to be declared as a abstract class. */
2222                    /* Since here we are handling internal functions only we can add the keyword flag. */
2223                    /* This time we set the flag for the keyword 'abstract'. */
2224                    scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
2225                }
2226            }
2227            if (ptr->flags & ZEND_ACC_STATIC && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
2228                zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2229            }
2230        } else {
2231            if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
2232                efree((char*)lc_class_name);
2233                zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
2234                return FAILURE;
2235            }
2236            if (!internal_function->handler) {
2237                if (scope) {
2238                    efree((char*)lc_class_name);
2239                }
2240                zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2241                zend_unregister_functions(functions, count, target_function_table);
2242                return FAILURE;
2243            }
2244        }
2245        lowercase_name = zend_string_alloc(fname_len, 1);
2246        zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
2247        lowercase_name = zend_new_interned_string(lowercase_name);
2248        reg_function = malloc(sizeof(zend_internal_function));
2249        memcpy(reg_function, &function, sizeof(zend_internal_function));
2250        if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
2251            unload=1;
2252            free(reg_function);
2253            zend_string_release(lowercase_name);
2254            break;
2255        }
2256
2257        /* If types of arguments have to be checked */
2258        if (reg_function->common.arg_info && reg_function->common.num_args) {
2259            uint32_t i;
2260            for (i = 0; i < reg_function->common.num_args; i++) {
2261                if (reg_function->common.arg_info[i].class_name ||
2262                    reg_function->common.arg_info[i].type_hint) {
2263                    reg_function->common.fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
2264                    break;
2265                }
2266            }
2267        }
2268
2269        if (scope) {
2270            /* Look for ctor, dtor, clone
2271             * If it's an old-style constructor, store it only if we don't have
2272             * a constructor already.
2273             */
2274            if ((fname_len == class_name_len) && !ctor && !memcmp(ZSTR_VAL(lowercase_name), lc_class_name, class_name_len+1)) {
2275                ctor = reg_function;
2276            } else if (zend_string_equals_literal(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2277                ctor = reg_function;
2278            } else if (zend_string_equals_literal(lowercase_name, ZEND_DESTRUCTOR_FUNC_NAME)) {
2279                dtor = reg_function;
2280                if (internal_function->num_args) {
2281                    zend_error(error_type, "Destructor %s::%s() cannot take arguments", ZSTR_VAL(scope->name), ptr->fname);
2282                }
2283            } else if (zend_string_equals_literal(lowercase_name, ZEND_CLONE_FUNC_NAME)) {
2284                clone = reg_function;
2285            } else if (zend_string_equals_literal(lowercase_name, ZEND_CALL_FUNC_NAME)) {
2286                __call = reg_function;
2287            } else if (zend_string_equals_literal(lowercase_name, ZEND_CALLSTATIC_FUNC_NAME)) {
2288                __callstatic = reg_function;
2289            } else if (zend_string_equals_literal(lowercase_name, ZEND_TOSTRING_FUNC_NAME)) {
2290                __tostring = reg_function;
2291            } else if (zend_string_equals_literal(lowercase_name, ZEND_GET_FUNC_NAME)) {
2292                __get = reg_function;
2293                scope->ce_flags |= ZEND_ACC_USE_GUARDS;
2294            } else if (zend_string_equals_literal(lowercase_name, ZEND_SET_FUNC_NAME)) {
2295                __set = reg_function;
2296                scope->ce_flags |= ZEND_ACC_USE_GUARDS;
2297            } else if (zend_string_equals_literal(lowercase_name, ZEND_UNSET_FUNC_NAME)) {
2298                __unset = reg_function;
2299                scope->ce_flags |= ZEND_ACC_USE_GUARDS;
2300            } else if (zend_string_equals_literal(lowercase_name, ZEND_ISSET_FUNC_NAME)) {
2301                __isset = reg_function;
2302                scope->ce_flags |= ZEND_ACC_USE_GUARDS;
2303            } else if (zend_string_equals_literal(lowercase_name, ZEND_DEBUGINFO_FUNC_NAME)) {
2304                __debugInfo = reg_function;
2305            } else {
2306                reg_function = NULL;
2307            }
2308            if (reg_function) {
2309                zend_check_magic_method_implementation(scope, reg_function, error_type);
2310            }
2311        }
2312        ptr++;
2313        count++;
2314        zend_string_release(lowercase_name);
2315    }
2316    if (unload) { /* before unloading, display all remaining bad function in the module */
2317        if (scope) {
2318            efree((char*)lc_class_name);
2319        }
2320        while (ptr->fname) {
2321            fname_len = strlen(ptr->fname);
2322            lowercase_name = zend_string_alloc(fname_len, 0);
2323            zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
2324            if (zend_hash_exists(target_function_table, lowercase_name)) {
2325                zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2326            }
2327            zend_string_free(lowercase_name);
2328            ptr++;
2329        }
2330        zend_unregister_functions(functions, count, target_function_table);
2331        return FAILURE;
2332    }
2333    if (scope) {
2334        scope->constructor = ctor;
2335        scope->destructor = dtor;
2336        scope->clone = clone;
2337        scope->__call = __call;
2338        scope->__callstatic = __callstatic;
2339        scope->__tostring = __tostring;
2340        scope->__get = __get;
2341        scope->__set = __set;
2342        scope->__unset = __unset;
2343        scope->__isset = __isset;
2344        scope->__debugInfo = __debugInfo;
2345        if (ctor) {
2346            ctor->common.fn_flags |= ZEND_ACC_CTOR;
2347            if (ctor->common.fn_flags & ZEND_ACC_STATIC) {
2348                zend_error(error_type, "Constructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
2349            }
2350            ctor->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2351        }
2352        if (dtor) {
2353            dtor->common.fn_flags |= ZEND_ACC_DTOR;
2354            if (dtor->common.fn_flags & ZEND_ACC_STATIC) {
2355                zend_error(error_type, "Destructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
2356            }
2357            dtor->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2358        }
2359        if (clone) {
2360            clone->common.fn_flags |= ZEND_ACC_CLONE;
2361            if (clone->common.fn_flags & ZEND_ACC_STATIC) {
2362                zend_error(error_type, "Constructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
2363            }
2364            clone->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2365        }
2366        if (__call) {
2367            if (__call->common.fn_flags & ZEND_ACC_STATIC) {
2368                zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__call->common.function_name));
2369            }
2370            __call->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2371        }
2372        if (__callstatic) {
2373            if (!(__callstatic->common.fn_flags & ZEND_ACC_STATIC)) {
2374                zend_error(error_type, "Method %s::%s() must be static", ZSTR_VAL(scope->name), ZSTR_VAL(__callstatic->common.function_name));
2375            }
2376            __callstatic->common.fn_flags |= ZEND_ACC_STATIC;
2377        }
2378        if (__tostring) {
2379            if (__tostring->common.fn_flags & ZEND_ACC_STATIC) {
2380                zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__tostring->common.function_name));
2381            }
2382            __tostring->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2383        }
2384        if (__get) {
2385            if (__get->common.fn_flags & ZEND_ACC_STATIC) {
2386                zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__get->common.function_name));
2387            }
2388            __get->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2389        }
2390        if (__set) {
2391            if (__set->common.fn_flags & ZEND_ACC_STATIC) {
2392                zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__set->common.function_name));
2393            }
2394            __set->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2395        }
2396        if (__unset) {
2397            if (__unset->common.fn_flags & ZEND_ACC_STATIC) {
2398                zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__unset->common.function_name));
2399            }
2400            __unset->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2401        }
2402        if (__isset) {
2403            if (__isset->common.fn_flags & ZEND_ACC_STATIC) {
2404                zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__isset->common.function_name));
2405            }
2406            __isset->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2407        }
2408        if (__debugInfo) {
2409            if (__debugInfo->common.fn_flags & ZEND_ACC_STATIC) {
2410                zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__debugInfo->common.function_name));
2411            }
2412        }
2413
2414        if (ctor && ctor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && ctor->common.fn_flags & ZEND_ACC_CTOR) {
2415            zend_error_noreturn(E_CORE_ERROR, "Constructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
2416        }
2417
2418        if (dtor && dtor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && dtor->common.fn_flags & ZEND_ACC_DTOR) {
2419            zend_error_noreturn(E_CORE_ERROR, "Destructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
2420        }
2421
2422        if (clone && clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && dtor->common.fn_flags & ZEND_ACC_DTOR) {
2423            zend_error_noreturn(E_CORE_ERROR, "%s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
2424        }
2425        efree((char*)lc_class_name);
2426    }
2427    return SUCCESS;
2428}
2429/* }}} */
2430
2431/* count=-1 means erase all functions, otherwise,
2432 * erase the first count functions
2433 */
2434ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
2435{
2436    const zend_function_entry *ptr = functions;
2437    int i=0;
2438    HashTable *target_function_table = function_table;
2439    zend_string *lowercase_name;
2440    size_t fname_len;
2441
2442    if (!target_function_table) {
2443        target_function_table = CG(function_table);
2444    }
2445    while (ptr->fname) {
2446        if (count!=-1 && i>=count) {
2447            break;
2448        }
2449        fname_len = strlen(ptr->fname);
2450        lowercase_name = zend_string_alloc(fname_len, 0);
2451        zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
2452        zend_hash_del(target_function_table, lowercase_name);
2453        zend_string_free(lowercase_name);
2454        ptr++;
2455        i++;
2456    }
2457}
2458/* }}} */
2459
2460ZEND_API int zend_startup_module(zend_module_entry *module) /* {{{ */
2461{
2462    if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
2463        return SUCCESS;
2464    }
2465    return FAILURE;
2466}
2467/* }}} */
2468
2469ZEND_API int zend_get_module_started(const char *module_name) /* {{{ */
2470{
2471    zend_module_entry *module;
2472
2473    module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
2474    return (module && module->module_started) ? SUCCESS : FAILURE;
2475}
2476/* }}} */
2477
2478static int clean_module_class(zval *el, void *arg) /* {{{ */
2479{
2480    zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
2481    int module_number = *(int *)arg;
2482    if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
2483        return ZEND_HASH_APPLY_REMOVE;
2484    } else {
2485        return ZEND_HASH_APPLY_KEEP;
2486    }
2487}
2488/* }}} */
2489
2490static void clean_module_classes(int module_number) /* {{{ */
2491{
2492    zend_hash_apply_with_argument(EG(class_table), clean_module_class, (void *) &module_number);
2493}
2494/* }}} */
2495
2496void module_destructor(zend_module_entry *module) /* {{{ */
2497{
2498
2499    if (module->type == MODULE_TEMPORARY) {
2500        zend_clean_module_rsrc_dtors(module->module_number);
2501        clean_module_constants(module->module_number);
2502        clean_module_classes(module->module_number);
2503    }
2504
2505    if (module->module_started && module->module_shutdown_func) {
2506#if 0
2507        zend_printf("%s: Module shutdown\n", module->name);
2508#endif
2509        module->module_shutdown_func(module->type, module->module_number);
2510    }
2511
2512    /* Deinitilaise module globals */
2513    if (module->globals_size) {
2514#ifdef ZTS
2515        if (*module->globals_id_ptr) {
2516            ts_free_id(*module->globals_id_ptr);
2517        }
2518#else
2519        if (module->globals_dtor) {
2520            module->globals_dtor(module->globals_ptr);
2521        }
2522#endif
2523    }
2524
2525    module->module_started=0;
2526    if (module->functions) {
2527        zend_unregister_functions(module->functions, -1, NULL);
2528    }
2529
2530#if HAVE_LIBDL
2531#if !(defined(NETWARE) && defined(APACHE_1_BUILD))
2532    if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
2533        DL_UNLOAD(module->handle);
2534    }
2535#endif
2536#endif
2537}
2538/* }}} */
2539
2540ZEND_API void zend_activate_modules(void) /* {{{ */
2541{
2542    zend_module_entry **p = module_request_startup_handlers;
2543
2544    while (*p) {
2545        zend_module_entry *module = *p;
2546
2547        if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
2548            zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
2549            exit(1);
2550        }
2551        p++;
2552    }
2553}
2554/* }}} */
2555
2556/* call request shutdown for all modules */
2557static int module_registry_cleanup(zval *zv) /* {{{ */
2558{
2559    zend_module_entry *module = Z_PTR_P(zv);
2560
2561    if (module->request_shutdown_func) {
2562#if 0
2563        zend_printf("%s: Request shutdown\n", module->name);
2564#endif
2565        module->request_shutdown_func(module->type, module->module_number);
2566    }
2567    return 0;
2568}
2569/* }}} */
2570
2571ZEND_API void zend_deactivate_modules(void) /* {{{ */
2572{
2573    EG(current_execute_data) = NULL; /* we're no longer executing anything */
2574
2575    zend_try {
2576        if (EG(full_tables_cleanup)) {
2577            zend_hash_reverse_apply(&module_registry, module_registry_cleanup);
2578        } else {
2579            zend_module_entry **p = module_request_shutdown_handlers;
2580
2581            while (*p) {
2582                zend_module_entry *module = *p;
2583
2584                module->request_shutdown_func(module->type, module->module_number);
2585                p++;
2586            }
2587        }
2588    } zend_end_try();
2589}
2590/* }}} */
2591
2592ZEND_API void zend_cleanup_internal_classes(void) /* {{{ */
2593{
2594    zend_class_entry **p = class_cleanup_handlers;
2595
2596    while (*p) {
2597        zend_cleanup_internal_class_data(*p);
2598        p++;
2599    }
2600}
2601/* }}} */
2602
2603int module_registry_unload_temp(const zend_module_entry *module) /* {{{ */
2604{
2605    return (module->type == MODULE_TEMPORARY) ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_STOP;
2606}
2607/* }}} */
2608
2609static int module_registry_unload_temp_wrapper(zval *el) /* {{{ */
2610{
2611    zend_module_entry *module = (zend_module_entry *)Z_PTR_P(el);
2612    return module_registry_unload_temp((const zend_module_entry *)module);
2613}
2614/* }}} */
2615
2616static int exec_done_cb(zval *el) /* {{{ */
2617{
2618    zend_module_entry *module = (zend_module_entry *)Z_PTR_P(el);
2619    if (module->post_deactivate_func) {
2620        module->post_deactivate_func();
2621    }
2622    return 0;
2623}
2624/* }}} */
2625
2626ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
2627{
2628    if (EG(full_tables_cleanup)) {
2629        zend_hash_apply(&module_registry, exec_done_cb);
2630        zend_hash_reverse_apply(&module_registry, module_registry_unload_temp_wrapper);
2631    } else {
2632        zend_module_entry **p = module_post_deactivate_handlers;
2633
2634        while (*p) {
2635            zend_module_entry *module = *p;
2636
2637            module->post_deactivate_func();
2638            p++;
2639        }
2640    }
2641}
2642/* }}} */
2643
2644/* return the next free module number */
2645ZEND_API int zend_next_free_module(void) /* {{{ */
2646{
2647    return zend_hash_num_elements(&module_registry) + 1;
2648}
2649/* }}} */
2650
2651static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
2652{
2653    zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
2654    zend_string *lowercase_name = zend_string_alloc(ZSTR_LEN(orig_class_entry->name), 1);
2655    *class_entry = *orig_class_entry;
2656
2657    class_entry->type = ZEND_INTERNAL_CLASS;
2658    zend_initialize_class_data(class_entry, 0);
2659    class_entry->ce_flags = ce_flags | ZEND_ACC_CONSTANTS_UPDATED;
2660    class_entry->info.internal.module = EG(current_module);
2661
2662    if (class_entry->info.internal.builtin_functions) {
2663        zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, MODULE_PERSISTENT);
2664    }
2665
2666    zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ZSTR_VAL(orig_class_entry->name), ZSTR_LEN(class_entry->name));
2667    lowercase_name = zend_new_interned_string(lowercase_name);
2668    zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
2669    zend_string_release(lowercase_name);
2670    return class_entry;
2671}
2672/* }}} */
2673
2674/* If parent_ce is not NULL then it inherits from parent_ce
2675 * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
2676 * If both parent_ce and parent_name are NULL it does a regular class registration
2677 * If parent_name is specified but not found NULL is returned
2678 */
2679ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
2680{
2681    zend_class_entry *register_class;
2682
2683    register_class = zend_register_internal_class(class_entry);
2684
2685    if (parent_ce) {
2686        zend_do_inheritance(register_class, parent_ce);
2687    }
2688    return register_class;
2689}
2690/* }}} */
2691
2692ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
2693{
2694    zend_class_entry *interface_entry;
2695    va_list interface_list;
2696    va_start(interface_list, num_interfaces);
2697
2698    while (num_interfaces--) {
2699        interface_entry = va_arg(interface_list, zend_class_entry *);
2700        zend_do_implement_interface(class_entry, interface_entry);
2701    }
2702
2703    va_end(interface_list);
2704}
2705/* }}} */
2706
2707/* A class that contains at least one abstract method automatically becomes an abstract class.
2708 */
2709ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */
2710{
2711    return do_register_internal_class(orig_class_entry, 0);
2712}
2713/* }}} */
2714
2715ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
2716{
2717    return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
2718}
2719/* }}} */
2720
2721ZEND_API int zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce) /* {{{ */
2722{
2723    zend_string *lcname;
2724
2725    if (name[0] == '\\') {
2726        lcname = zend_string_alloc(name_len-1, 1);
2727        zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
2728    } else {
2729        lcname = zend_string_alloc(name_len, 1);
2730        zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
2731    }
2732
2733    zend_assert_valid_class_name(lcname);
2734
2735    ce = zend_hash_add_ptr(CG(class_table), lcname, ce);
2736    zend_string_release(lcname);
2737    if (ce) {
2738        ce->refcount++;
2739        return SUCCESS;
2740    }
2741    return FAILURE;
2742}
2743/* }}} */
2744
2745ZEND_API int zend_set_hash_symbol(zval *symbol, const char *name, int name_length, zend_bool is_ref, int num_symbol_tables, ...) /* {{{ */
2746{
2747    HashTable *symbol_table;
2748    va_list symbol_table_list;
2749
2750    if (num_symbol_tables <= 0) return FAILURE;
2751
2752    if (is_ref) {
2753        ZVAL_MAKE_REF(symbol);
2754    }
2755
2756    va_start(symbol_table_list, num_symbol_tables);
2757    while (num_symbol_tables-- > 0) {
2758        symbol_table = va_arg(symbol_table_list, HashTable *);
2759        zend_hash_str_update(symbol_table, name, name_length, symbol);
2760        if (Z_REFCOUNTED_P(symbol)) {
2761            Z_ADDREF_P(symbol);
2762        }
2763    }
2764    va_end(symbol_table_list);
2765    return SUCCESS;
2766}
2767/* }}} */
2768
2769/* Disabled functions support */
2770
2771/* {{{ proto void display_disabled_function(void)
2772Dummy function which displays an error when a disabled function is called. */
2773ZEND_API ZEND_FUNCTION(display_disabled_function)
2774{
2775    zend_error(E_WARNING, "%s() has been disabled for security reasons", get_active_function_name());
2776}
2777/* }}} */
2778
2779ZEND_API int zend_disable_function(char *function_name, size_t function_name_length) /* {{{ */
2780{
2781    zend_internal_function *func;
2782    if ((func = zend_hash_str_find_ptr(CG(function_table), function_name, function_name_length))) {
2783        func->fn_flags &= ~(ZEND_ACC_VARIADIC | ZEND_ACC_HAS_TYPE_HINTS);
2784        func->num_args = 0;
2785        func->arg_info = NULL;
2786        func->handler = ZEND_FN(display_disabled_function);
2787        return SUCCESS;
2788    }
2789    return FAILURE;
2790}
2791/* }}} */
2792
2793#ifdef ZEND_WIN32
2794#pragma optimize("", off)
2795#endif
2796static zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */
2797{
2798    zend_object *intern;
2799
2800    intern = zend_objects_new(class_type);
2801    zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name));
2802    return intern;
2803}
2804#ifdef ZEND_WIN32
2805#pragma optimize("", on)
2806#endif
2807/* }}} */
2808
2809static const zend_function_entry disabled_class_new[] = {
2810    ZEND_FE_END
2811};
2812
2813ZEND_API int zend_disable_class(char *class_name, size_t class_name_length) /* {{{ */
2814{
2815    zend_class_entry *disabled_class;
2816    zend_string *key;
2817
2818    key = zend_string_alloc(class_name_length, 0);
2819    zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
2820    disabled_class = zend_hash_find_ptr(CG(class_table), key);
2821    if (!disabled_class) {
2822        return FAILURE;
2823    }
2824    INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new, NULL, NULL, NULL, NULL, NULL);
2825    disabled_class->create_object = display_disabled_class;
2826    zend_hash_clean(&disabled_class->function_table);
2827    return SUCCESS;
2828}
2829/* }}} */
2830
2831static int zend_is_callable_check_class(zend_string *name, zend_fcall_info_cache *fcc, int *strict_class, char **error) /* {{{ */
2832{
2833    int ret = 0;
2834    zend_class_entry *ce;
2835    size_t name_len = ZSTR_LEN(name);
2836    zend_string *lcname;
2837    ALLOCA_FLAG(use_heap);
2838
2839    ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
2840    zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
2841
2842    *strict_class = 0;
2843    if (zend_string_equals_literal(lcname, "self")) {
2844        if (!EG(scope)) {
2845            if (error) *error = estrdup("cannot access self:: when no class scope is active");
2846        } else {
2847            fcc->called_scope = zend_get_called_scope(EG(current_execute_data));
2848            fcc->calling_scope = EG(scope);
2849            if (!fcc->object) {
2850                fcc->object = zend_get_this_object(EG(current_execute_data));
2851            }
2852            ret = 1;
2853        }
2854    } else if (zend_string_equals_literal(lcname, "parent")) {
2855        if (!EG(scope)) {
2856            if (error) *error = estrdup("cannot access parent:: when no class scope is active");
2857        } else if (!EG(scope)->parent) {
2858            if (error) *error = estrdup("cannot access parent:: when current class scope has no parent");
2859        } else {
2860            fcc->called_scope = zend_get_called_scope(EG(current_execute_data));
2861            fcc->calling_scope = EG(scope)->parent;
2862            if (!fcc->object) {
2863                fcc->object = zend_get_this_object(EG(current_execute_data));
2864            }
2865            *strict_class = 1;
2866            ret = 1;
2867        }
2868    } else if (zend_string_equals_literal(lcname, "static")) {
2869        zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
2870
2871        if (!called_scope) {
2872            if (error) *error = estrdup("cannot access static:: when no class scope is active");
2873        } else {
2874            fcc->called_scope = called_scope;
2875            fcc->calling_scope = called_scope;
2876            if (!fcc->object) {
2877                fcc->object = zend_get_this_object(EG(current_execute_data));
2878            }
2879            *strict_class = 1;
2880            ret = 1;
2881        }
2882    } else if ((ce = zend_lookup_class_ex(name, NULL, 1)) != NULL) {
2883        zend_class_entry *scope;
2884        zend_execute_data *ex = EG(current_execute_data);
2885
2886        while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
2887            ex = ex->prev_execute_data;
2888        }
2889        scope = ex ? ex->func->common.scope : NULL;
2890        fcc->calling_scope = ce;
2891        if (scope && !fcc->object) {
2892            zend_object *object = zend_get_this_object(EG(current_execute_data));
2893
2894            if (object &&
2895                instanceof_function(object->ce, scope) &&
2896                instanceof_function(scope, fcc->calling_scope)) {
2897                fcc->object = object;
2898                fcc->called_scope = object->ce;
2899            } else {
2900                fcc->called_scope = fcc->calling_scope;
2901            }
2902        } else {
2903            fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope;
2904        }
2905        *strict_class = 1;
2906        ret = 1;
2907    } else {
2908        if (error) zend_spprintf(error, 0, "class '%.*s' not found", name_len, ZSTR_VAL(name));
2909    }
2910    ZSTR_ALLOCA_FREE(lcname, use_heap);
2911    return ret;
2912}
2913/* }}} */
2914
2915static int zend_is_callable_check_func(int check_flags, zval *callable, zend_fcall_info_cache *fcc, int strict_class, char **error) /* {{{ */
2916{
2917    zend_class_entry *ce_org = fcc->calling_scope;
2918    int retval = 0;
2919    zend_string *mname, *cname;
2920    zend_string *lmname;
2921    const char *colon;
2922    size_t clen, mlen;
2923    zend_class_entry *last_scope;
2924    HashTable *ftable;
2925    int call_via_handler = 0;
2926    ALLOCA_FLAG(use_heap)
2927
2928    if (error) {
2929        *error = NULL;
2930    }
2931
2932    fcc->calling_scope = NULL;
2933    fcc->function_handler = NULL;
2934
2935    if (!ce_org) {
2936        zend_string *lmname;
2937
2938        /* Skip leading \ */
2939        if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
2940            ZSTR_ALLOCA_INIT(lmname, Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1, use_heap);
2941        } else {
2942            lmname = Z_STR_P(callable);
2943        }
2944        /* Check if function with given name exists.
2945         * This may be a compound name that includes namespace name */
2946        if (EXPECTED((fcc->function_handler = zend_hash_find_ptr(EG(function_table), lmname)) != NULL)) {
2947            if (lmname != Z_STR_P(callable)) {
2948                ZSTR_ALLOCA_FREE(lmname, use_heap);
2949            }
2950            return 1;
2951        } else {
2952            if (lmname == Z_STR_P(callable)) {
2953                ZSTR_ALLOCA_INIT(lmname, Z_STRVAL_P(callable), Z_STRLEN_P(callable), use_heap);
2954            } else {
2955                zend_string_forget_hash_val(lmname);
2956            }
2957            zend_str_tolower(ZSTR_VAL(lmname), ZSTR_LEN(lmname));
2958            if ((fcc->function_handler = zend_hash_find_ptr(EG(function_table), lmname)) != NULL) {
2959                ZSTR_ALLOCA_FREE(lmname, use_heap);
2960                return 1;
2961            }
2962        }
2963        if (lmname != Z_STR_P(callable)) {
2964            ZSTR_ALLOCA_FREE(lmname, use_heap);
2965        }
2966    }
2967
2968    /* Split name into class/namespace and method/function names */
2969    if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
2970        colon > Z_STRVAL_P(callable) &&
2971        *(colon-1) == ':'
2972    ) {
2973        colon--;
2974        clen = colon - Z_STRVAL_P(callable);
2975        mlen = Z_STRLEN_P(callable) - clen - 2;
2976
2977        if (colon == Z_STRVAL_P(callable)) {
2978            if (error) zend_spprintf(error, 0, "invalid function name");
2979            return 0;
2980        }
2981
2982        /* This is a compound name.
2983         * Try to fetch class and then find static method. */
2984        last_scope = EG(scope);
2985        if (ce_org) {
2986            EG(scope) = ce_org;
2987        }
2988
2989        cname = zend_string_init(Z_STRVAL_P(callable), clen, 0);
2990        if (!zend_is_callable_check_class(cname, fcc, &strict_class, error)) {
2991            zend_string_release(cname);
2992            EG(scope) = last_scope;
2993            return 0;
2994        }
2995        zend_string_release(cname);
2996        EG(scope) = last_scope;
2997
2998        ftable = &fcc->calling_scope->function_table;
2999        if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
3000            if (error) zend_spprintf(error, 0, "class '%s' is not a subclass of '%s'", ZSTR_VAL(ce_org->name), ZSTR_VAL(fcc->calling_scope->name));
3001            return 0;
3002        }
3003        mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
3004    } else if (ce_org) {
3005        /* Try to fetch find static method of given class. */
3006        mlen = Z_STRLEN_P(callable);
3007        mname = Z_STR_P(callable);
3008        zend_string_addref(mname);
3009        ftable = &ce_org->function_table;
3010        fcc->calling_scope = ce_org;
3011    } else {
3012        /* We already checked for plain function before. */
3013        if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3014            zend_spprintf(error, 0, "function '%s' not found or invalid function name", Z_STRVAL_P(callable));
3015        }
3016        return 0;
3017    }
3018
3019    lmname = zend_string_tolower(mname);
3020    if (strict_class &&
3021        fcc->calling_scope &&
3022        zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
3023        fcc->function_handler = fcc->calling_scope->constructor;
3024        if (fcc->function_handler) {
3025            retval = 1;
3026        }
3027    } else if ((fcc->function_handler = zend_hash_find_ptr(ftable, lmname)) != NULL) {
3028        retval = 1;
3029        if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3030            !strict_class && EG(scope) &&
3031            instanceof_function(fcc->function_handler->common.scope, EG(scope))) {
3032            zend_function *priv_fbc;
3033
3034            if ((priv_fbc = zend_hash_find_ptr(&EG(scope)->function_table, lmname)) != NULL
3035                && priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
3036                && priv_fbc->common.scope == EG(scope)) {
3037                fcc->function_handler = priv_fbc;
3038            }
3039        }
3040        if ((check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0 &&
3041            (fcc->calling_scope &&
3042             ((fcc->object && fcc->calling_scope->__call) ||
3043              (!fcc->object && fcc->calling_scope->__callstatic)))) {
3044            if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
3045                if (!zend_check_private(fcc->function_handler, fcc->object ? fcc->object->ce : EG(scope), lmname)) {
3046                    retval = 0;
3047                    fcc->function_handler = NULL;
3048                    goto get_function_via_handler;
3049                }
3050            } else if (fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED) {
3051                if (!zend_check_protected(fcc->function_handler->common.scope, EG(scope))) {
3052                    retval = 0;
3053                    fcc->function_handler = NULL;
3054                    goto get_function_via_handler;
3055                }
3056            }
3057        }
3058    } else {
3059get_function_via_handler:
3060        if (fcc->object && fcc->calling_scope == ce_org) {
3061            if (strict_class && ce_org->__call) {
3062                fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
3063                call_via_handler = 1;
3064                retval = 1;
3065            } else if (fcc->object->handlers->get_method) {
3066                fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
3067                if (fcc->function_handler) {
3068                    if (strict_class &&
3069                        (!fcc->function_handler->common.scope ||
3070                         !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
3071                        if (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
3072                            if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3073                                zend_string_release(fcc->function_handler->common.function_name);
3074                            }
3075                            zend_free_trampoline(fcc->function_handler);
3076                        }
3077                    } else {
3078                        retval = 1;
3079                        call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3080                    }
3081                }
3082            }
3083        } else if (fcc->calling_scope) {
3084            if (fcc->calling_scope->get_static_method) {
3085                fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
3086            } else {
3087                fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
3088            }
3089            if (fcc->function_handler) {
3090                retval = 1;
3091                call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3092                if (call_via_handler && !fcc->object) {
3093                    zend_object *object = zend_get_this_object(EG(current_execute_data));
3094                    if (object &&
3095                        instanceof_function(object->ce, fcc->calling_scope)) {
3096                        fcc->object = object;
3097                    }
3098                }
3099            }
3100        }
3101    }
3102
3103    if (retval) {
3104        if (fcc->calling_scope && !call_via_handler) {
3105            if (!fcc->object && (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT)) {
3106                if (error) {
3107                    zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3108                    retval = 0;
3109                } else {
3110                    zend_throw_error(NULL, "Cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3111                    return 0;
3112                }
3113            } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3114                int severity;
3115                char *verb;
3116                if (fcc->function_handler->common.fn_flags & ZEND_ACC_ALLOW_STATIC) {
3117                    severity = E_DEPRECATED;
3118                    verb = "should not";
3119                } else {
3120                    /* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */
3121                    severity = E_ERROR;
3122                    verb = "cannot";
3123                }
3124                if ((check_flags & IS_CALLABLE_CHECK_IS_STATIC) != 0) {
3125                    retval = 0;
3126                }
3127                if (error) {
3128                    zend_spprintf(error, 0, "non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
3129                    if (severity != E_DEPRECATED) {
3130                        retval = 0;
3131                    }
3132                } else if (retval) {
3133                    if (severity == E_ERROR) {
3134                        zend_throw_error(NULL, "Non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
3135                    } else {
3136                        zend_error(severity, "Non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
3137                    }
3138                }
3139            }
3140            if (retval && (check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0) {
3141                if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
3142                    if (!zend_check_private(fcc->function_handler, fcc->object ? fcc->object->ce : EG(scope), lmname)) {
3143                        if (error) {
3144                            if (*error) {
3145                                efree(*error);
3146                            }
3147                            zend_spprintf(error, 0, "cannot access private method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3148                        }
3149                        retval = 0;
3150                    }
3151                } else if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED)) {
3152                    if (!zend_check_protected(fcc->function_handler->common.scope, EG(scope))) {
3153                        if (error) {
3154                            if (*error) {
3155                                efree(*error);
3156                            }
3157                            zend_spprintf(error, 0, "cannot access protected method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3158                        }
3159                        retval = 0;
3160                    }
3161                }
3162            }
3163        }
3164    } else if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3165        if (fcc->calling_scope) {
3166            if (error) zend_spprintf(error, 0, "class '%s' does not have a method '%s'", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
3167        } else {
3168            if (error) zend_spprintf(error, 0, "function '%s' does not exist", ZSTR_VAL(mname));
3169        }
3170    }
3171    zend_string_release(lmname);
3172    zend_string_release(mname);
3173
3174    if (fcc->object) {
3175        fcc->called_scope = fcc->object->ce;
3176    }
3177    if (retval) {
3178        fcc->initialized = 1;
3179    }
3180    return retval;
3181}
3182/* }}} */
3183
3184ZEND_API zend_bool zend_is_callable_ex(zval *callable, zend_object *object, uint check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
3185{
3186    zend_bool ret;
3187    zend_fcall_info_cache fcc_local;
3188
3189    if (callable_name) {
3190        *callable_name = NULL;
3191    }
3192    if (fcc == NULL) {
3193        fcc = &fcc_local;
3194    }
3195    if (error) {
3196        *error = NULL;
3197    }
3198
3199    fcc->initialized = 0;
3200    fcc->calling_scope = NULL;
3201    fcc->called_scope = NULL;
3202    fcc->function_handler = NULL;
3203    fcc->object = NULL;
3204
3205    if (object &&
3206        (!EG(objects_store).object_buckets ||
3207         !IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle]))) {
3208        return 0;
3209    }
3210
3211again:
3212    switch (Z_TYPE_P(callable)) {
3213        case IS_STRING:
3214            if (object) {
3215                fcc->object = object;
3216                fcc->calling_scope = object->ce;
3217                if (callable_name) {
3218                    char *ptr;
3219
3220                    *callable_name = zend_string_alloc(ZSTR_LEN(fcc->calling_scope->name) + Z_STRLEN_P(callable) + sizeof("::") - 1, 0);
3221                    ptr = ZSTR_VAL(*callable_name);
3222                    memcpy(ptr, ZSTR_VAL(fcc->calling_scope->name), ZSTR_LEN(fcc->calling_scope->name));
3223                    ptr += ZSTR_LEN(fcc->calling_scope->name);
3224                    memcpy(ptr, "::", sizeof("::") - 1);
3225                    ptr += sizeof("::") - 1;
3226                    memcpy(ptr, Z_STRVAL_P(callable), Z_STRLEN_P(callable) + 1);
3227                }
3228            } else if (callable_name) {
3229                *callable_name = zend_string_copy(Z_STR_P(callable));
3230            }
3231            if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3232                fcc->called_scope = fcc->calling_scope;
3233                return 1;
3234            }
3235
3236            ret = zend_is_callable_check_func(check_flags, callable, fcc, 0, error);
3237            if (fcc == &fcc_local &&
3238                fcc->function_handler &&
3239                ((fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) ||
3240                 fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3241                 fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3242                if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3243                    zend_string_release(fcc->function_handler->common.function_name);
3244                }
3245                zend_free_trampoline(fcc->function_handler);
3246            }
3247            return ret;
3248
3249        case IS_ARRAY:
3250            {
3251                zval *method = NULL;
3252                zval *obj = NULL;
3253                int strict_class = 0;
3254
3255                if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3256                    obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
3257                    method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
3258                }
3259
3260                do {
3261                    if (obj == NULL || method == NULL) {
3262                        break;
3263                    }
3264
3265                    ZVAL_DEREF(method);
3266                    if (Z_TYPE_P(method) != IS_STRING) {
3267                        break;
3268                    }
3269
3270                    ZVAL_DEREF(obj);
3271                    if (Z_TYPE_P(obj) == IS_STRING) {
3272                        if (callable_name) {
3273                            char *ptr;
3274
3275
3276                            *callable_name = zend_string_alloc(Z_STRLEN_P(obj) + Z_STRLEN_P(method) + sizeof("::") - 1, 0);
3277                            ptr = ZSTR_VAL(*callable_name);
3278                            memcpy(ptr, Z_STRVAL_P(obj), Z_STRLEN_P(obj));
3279                            ptr += Z_STRLEN_P(obj);
3280                            memcpy(ptr, "::", sizeof("::") - 1);
3281                            ptr += sizeof("::") - 1;
3282                            memcpy(ptr, Z_STRVAL_P(method), Z_STRLEN_P(method) + 1);
3283                        }
3284
3285                        if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3286                            return 1;
3287                        }
3288
3289                        if (!zend_is_callable_check_class(Z_STR_P(obj), fcc, &strict_class, error)) {
3290                            return 0;
3291                        }
3292
3293                    } else if (Z_TYPE_P(obj) == IS_OBJECT) {
3294                        if (!EG(objects_store).object_buckets ||
3295                            !IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(obj)])) {
3296                            return 0;
3297                        }
3298
3299                        fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
3300
3301                        fcc->object = Z_OBJ_P(obj);
3302
3303                        if (callable_name) {
3304                            char *ptr;
3305
3306                            *callable_name = zend_string_alloc(ZSTR_LEN(fcc->calling_scope->name) + Z_STRLEN_P(method) + sizeof("::") - 1, 0);
3307                            ptr = ZSTR_VAL(*callable_name);
3308                            memcpy(ptr, ZSTR_VAL(fcc->calling_scope->name), ZSTR_LEN(fcc->calling_scope->name));
3309                            ptr += ZSTR_LEN(fcc->calling_scope->name);
3310                            memcpy(ptr, "::", sizeof("::") - 1);
3311                            ptr += sizeof("::") - 1;
3312                            memcpy(ptr, Z_STRVAL_P(method), Z_STRLEN_P(method) + 1);
3313                        }
3314
3315                        if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3316                            fcc->called_scope = fcc->calling_scope;
3317                            return 1;
3318                        }
3319                    } else {
3320                        break;
3321                    }
3322
3323                    ret = zend_is_callable_check_func(check_flags, method, fcc, strict_class, error);
3324                    if (fcc == &fcc_local &&
3325                        fcc->function_handler &&
3326                        ((fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) ||
3327                         fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3328                         fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3329                        if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3330                            zend_string_release(fcc->function_handler->common.function_name);
3331                        }
3332                        zend_free_trampoline(fcc->function_handler);
3333                    }
3334                    return ret;
3335
3336                } while (0);
3337                if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3338                    if (!obj || (!Z_ISREF_P(obj)?
3339                                (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) :
3340                                (Z_TYPE_P(Z_REFVAL_P(obj)) != IS_STRING && Z_TYPE_P(Z_REFVAL_P(obj)) != IS_OBJECT))) {
3341                        if (error) zend_spprintf(error, 0, "first array member is not a valid class name or object");
3342                    } else {
3343                        if (error) zend_spprintf(error, 0, "second array member is not a valid method");
3344                    }
3345                } else {
3346                    if (error) zend_spprintf(error, 0, "array must have exactly two members");
3347                }
3348                if (callable_name) {
3349                    *callable_name = zend_string_init("Array", sizeof("Array")-1, 0);
3350                }
3351            }
3352            return 0;
3353        case IS_OBJECT:
3354            if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(callable, &fcc->calling_scope, &fcc->function_handler, &fcc->object) == SUCCESS) {
3355                fcc->called_scope = fcc->calling_scope;
3356                if (callable_name) {
3357                    zend_class_entry *ce = Z_OBJCE_P(callable); /* TBFixed: what if it's overloaded? */
3358
3359                    *callable_name = zend_string_alloc(ZSTR_LEN(ce->name) + sizeof("::__invoke") - 1, 0);
3360                    memcpy(ZSTR_VAL(*callable_name), ZSTR_VAL(ce->name), ZSTR_LEN(ce->name));
3361                    memcpy(ZSTR_VAL(*callable_name) + ZSTR_LEN(ce->name), "::__invoke", sizeof("::__invoke"));
3362                }
3363                return 1;
3364            }
3365            if (callable_name) {
3366                *callable_name = zval_get_string(callable);
3367            }
3368            if (error) zend_spprintf(error, 0, "no array or string given");
3369            return 0;
3370        case IS_REFERENCE:
3371            callable = Z_REFVAL_P(callable);
3372            goto again;
3373        default:
3374            if (callable_name) {
3375                *callable_name = zval_get_string(callable);
3376            }
3377            if (error) zend_spprintf(error, 0, "no array or string given");
3378            return 0;
3379    }
3380}
3381/* }}} */
3382
3383ZEND_API zend_bool zend_is_callable(zval *callable, uint check_flags, zend_string **callable_name) /* {{{ */
3384{
3385    return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
3386}
3387/* }}} */
3388
3389ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */
3390{
3391    zend_fcall_info_cache fcc;
3392
3393    if (zend_is_callable_ex(callable, NULL, IS_CALLABLE_STRICT, callable_name, &fcc, NULL)) {
3394        if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
3395            zval_dtor(callable);
3396            array_init(callable);
3397            add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
3398            add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
3399        }
3400        if (fcc.function_handler &&
3401            ((fcc.function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) ||
3402             fcc.function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3403             fcc.function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3404            if (fcc.function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3405                zend_string_release(fcc.function_handler->common.function_name);
3406            }
3407            zend_free_trampoline(fcc.function_handler);
3408        }
3409        return 1;
3410    }
3411    return 0;
3412}
3413/* }}} */
3414
3415ZEND_API int zend_fcall_info_init(zval *callable, uint check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */
3416{
3417    if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
3418        return FAILURE;
3419    }
3420
3421    fci->size = sizeof(*fci);
3422    fci->function_table = fcc->calling_scope ? &fcc->calling_scope->function_table : EG(function_table);
3423    fci->object = fcc->object;
3424    ZVAL_COPY_VALUE(&fci->function_name, callable);
3425    fci->retval = NULL;
3426    fci->param_count = 0;
3427    fci->params = NULL;
3428    fci->no_separation = 1;
3429    fci->symbol_table = NULL;
3430
3431    return SUCCESS;
3432}
3433/* }}} */
3434
3435ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, int free_mem) /* {{{ */
3436{
3437    if (fci->params) {
3438        zval *p = fci->params;
3439        zval *end = p + fci->param_count;
3440
3441        while (p != end) {
3442            i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
3443            p++;
3444        }
3445        if (free_mem) {
3446            efree(fci->params);
3447            fci->params = NULL;
3448        }
3449    }
3450    fci->param_count = 0;
3451}
3452/* }}} */
3453
3454ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, int *param_count, zval **params) /* {{{ */
3455{
3456    *param_count = fci->param_count;
3457    *params = fci->params;
3458    fci->param_count = 0;
3459    fci->params = NULL;
3460}
3461/* }}} */
3462
3463ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, int param_count, zval *params) /* {{{ */
3464{
3465    zend_fcall_info_args_clear(fci, 1);
3466    fci->param_count = param_count;
3467    fci->params = params;
3468}
3469/* }}} */
3470
3471ZEND_API int zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
3472{
3473    zval *arg, *params;
3474    uint32_t n = 1;
3475
3476    zend_fcall_info_args_clear(fci, !args);
3477
3478    if (!args) {
3479        return SUCCESS;
3480    }
3481
3482    if (Z_TYPE_P(args) != IS_ARRAY) {
3483        return FAILURE;
3484    }
3485
3486    fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
3487    fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3488
3489    ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
3490        if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
3491            ZVAL_NEW_REF(params, arg);
3492            if (Z_REFCOUNTED_P(arg)) {
3493                Z_ADDREF_P(arg);
3494            }
3495        } else {
3496            ZVAL_COPY(params, arg);
3497        }
3498        params++;
3499        n++;
3500    } ZEND_HASH_FOREACH_END();
3501
3502    return SUCCESS;
3503}
3504/* }}} */
3505
3506ZEND_API int zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
3507{
3508    return zend_fcall_info_args_ex(fci, NULL, args);
3509}
3510/* }}} */
3511
3512ZEND_API int zend_fcall_info_argp(zend_fcall_info *fci, int argc, zval *argv) /* {{{ */
3513{
3514    int i;
3515
3516    if (argc < 0) {
3517        return FAILURE;
3518    }
3519
3520    zend_fcall_info_args_clear(fci, !argc);
3521
3522    if (argc) {
3523        fci->param_count = argc;
3524        fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3525
3526        for (i = 0; i < argc; ++i) {
3527            ZVAL_COPY(&fci->params[i], &argv[i]);
3528        }
3529    }
3530
3531    return SUCCESS;
3532}
3533/* }}} */
3534
3535ZEND_API int zend_fcall_info_argv(zend_fcall_info *fci, int argc, va_list *argv) /* {{{ */
3536{
3537    int i;
3538    zval *arg;
3539
3540    if (argc < 0) {
3541        return FAILURE;
3542    }
3543
3544    zend_fcall_info_args_clear(fci, !argc);
3545
3546    if (argc) {
3547        fci->param_count = argc;
3548        fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3549
3550        for (i = 0; i < argc; ++i) {
3551            arg = va_arg(*argv, zval *);
3552            ZVAL_COPY(&fci->params[i], arg);
3553        }
3554    }
3555
3556    return SUCCESS;
3557}
3558/* }}} */
3559
3560ZEND_API int zend_fcall_info_argn(zend_fcall_info *fci, int argc, ...) /* {{{ */
3561{
3562    int ret;
3563    va_list argv;
3564
3565    va_start(argv, argc);
3566    ret = zend_fcall_info_argv(fci, argc, &argv);
3567    va_end(argv);
3568
3569    return ret;
3570}
3571/* }}} */
3572
3573ZEND_API int zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
3574{
3575    zval retval, *org_params = NULL;
3576    int result, org_count = 0;
3577
3578    fci->retval = retval_ptr ? retval_ptr : &retval;
3579    if (args) {
3580        zend_fcall_info_args_save(fci, &org_count, &org_params);
3581        zend_fcall_info_args(fci, args);
3582    }
3583    result = zend_call_function(fci, fcc);
3584
3585    if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
3586        zval_ptr_dtor(&retval);
3587    }
3588    if (args) {
3589        zend_fcall_info_args_restore(fci, org_count, org_params);
3590    }
3591    return result;
3592}
3593/* }}} */
3594
3595ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
3596{
3597    zend_string *lname;
3598    size_t name_len = strlen(module_name);
3599    zend_module_entry *module;
3600
3601    lname = zend_string_alloc(name_len, 0);
3602    zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len);
3603    module = zend_hash_find_ptr(&module_registry, lname);
3604    zend_string_free(lname);
3605    return module ? module->version : NULL;
3606}
3607/* }}} */
3608
3609ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
3610{
3611    zend_property_info *property_info, *property_info_ptr;
3612
3613    if (ce->type == ZEND_INTERNAL_CLASS) {
3614        property_info = pemalloc(sizeof(zend_property_info), 1);
3615        if ((access_type & ZEND_ACC_STATIC) || Z_CONSTANT_P(property)) {
3616            ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3617        }
3618    } else {
3619        property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
3620        if (Z_CONSTANT_P(property)) {
3621            ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3622        }
3623    }
3624
3625    if (!(access_type & ZEND_ACC_PPP_MASK)) {
3626        access_type |= ZEND_ACC_PUBLIC;
3627    }
3628    if (access_type & ZEND_ACC_STATIC) {
3629        if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
3630            (property_info_ptr->flags & ZEND_ACC_STATIC) != 0) {
3631            property_info->offset = property_info_ptr->offset;
3632            zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
3633            zend_hash_del(&ce->properties_info, name);
3634        } else {
3635            property_info->offset = ce->default_static_members_count++;
3636            ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
3637        }
3638        ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
3639        if (ce->type == ZEND_USER_CLASS) {
3640            ce->static_members_table = ce->default_static_members_table;
3641        }
3642    } else {
3643        if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
3644            (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
3645            property_info->offset = property_info_ptr->offset;
3646            zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
3647            zend_hash_del(&ce->properties_info, name);
3648        } else {
3649            property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
3650            ce->default_properties_count++;
3651            ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
3652        }
3653        ZVAL_COPY_VALUE(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)], property);
3654    }
3655    if (ce->type & ZEND_INTERNAL_CLASS) {
3656        switch(Z_TYPE_P(property)) {
3657            case IS_ARRAY:
3658            case IS_OBJECT:
3659            case IS_RESOURCE:
3660                zend_error_noreturn(E_CORE_ERROR, "Internal zval's can't be arrays, objects or resources");
3661                break;
3662            default:
3663                break;
3664        }
3665    }
3666    if (access_type & ZEND_ACC_PUBLIC) {
3667        property_info->name = zend_string_copy(name);
3668    } else if (access_type & ZEND_ACC_PRIVATE) {
3669        property_info->name = zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
3670    } else {
3671        ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
3672        property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
3673    }
3674
3675    property_info->name = zend_new_interned_string(property_info->name);
3676    property_info->flags = access_type;
3677    property_info->doc_comment = doc_comment;
3678    property_info->ce = ce;
3679    zend_hash_update_ptr(&ce->properties_info, name, property_info);
3680
3681    return SUCCESS;
3682}
3683/* }}} */
3684
3685ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
3686{
3687    zend_string *key = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3688    int ret = zend_declare_property_ex(ce, key, property, access_type, NULL);
3689    zend_string_release(key);
3690    return ret;
3691}
3692/* }}} */
3693
3694ZEND_API int zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
3695{
3696    zval property;
3697
3698    ZVAL_NULL(&property);
3699    return zend_declare_property(ce, name, name_length, &property, access_type);
3700}
3701/* }}} */
3702
3703ZEND_API int zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
3704{
3705    zval property;
3706
3707    ZVAL_BOOL(&property, value);
3708    return zend_declare_property(ce, name, name_length, &property, access_type);
3709}
3710/* }}} */
3711
3712ZEND_API int zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
3713{
3714    zval property;
3715
3716    ZVAL_LONG(&property, value);
3717    return zend_declare_property(ce, name, name_length, &property, access_type);
3718}
3719/* }}} */
3720
3721ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
3722{
3723    zval property;
3724
3725    ZVAL_DOUBLE(&property, value);
3726    return zend_declare_property(ce, name, name_length, &property, access_type);
3727}
3728/* }}} */
3729
3730ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
3731{
3732    zval property;
3733
3734    ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
3735    return zend_declare_property(ce, name, name_length, &property, access_type);
3736}
3737/* }}} */
3738
3739ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type) /* {{{ */
3740{
3741    zval property;
3742
3743    ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
3744    return zend_declare_property(ce, name, name_length, &property, access_type);
3745}
3746/* }}} */
3747
3748ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
3749{
3750    if (Z_CONSTANT_P(value)) {
3751        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3752    }
3753    return zend_hash_str_update(&ce->constants_table, name, name_length, value) ?
3754        SUCCESS : FAILURE;
3755}
3756/* }}} */
3757
3758ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
3759{
3760    zval constant;
3761
3762    ZVAL_NULL(&constant);
3763    return zend_declare_class_constant(ce, name, name_length, &constant);
3764}
3765/* }}} */
3766
3767ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
3768{
3769    zval constant;
3770
3771    ZVAL_LONG(&constant, value);
3772    return zend_declare_class_constant(ce, name, name_length, &constant);
3773}
3774/* }}} */
3775
3776ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value) /* {{{ */
3777{
3778    zval constant;
3779
3780    ZVAL_BOOL(&constant, value);
3781    return zend_declare_class_constant(ce, name, name_length, &constant);
3782}
3783/* }}} */
3784
3785ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
3786{
3787    zval constant;
3788
3789    ZVAL_DOUBLE(&constant, value);
3790    return zend_declare_class_constant(ce, name, name_length, &constant);
3791}
3792/* }}} */
3793
3794ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length) /* {{{ */
3795{
3796    zval constant;
3797
3798    ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
3799    return zend_declare_class_constant(ce, name, name_length, &constant);
3800}
3801/* }}} */
3802
3803ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
3804{
3805    return zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
3806}
3807/* }}} */
3808
3809ZEND_API void zend_update_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zval *value) /* {{{ */
3810{
3811    zval property;
3812    zend_class_entry *old_scope = EG(scope);
3813
3814    EG(scope) = scope;
3815
3816    if (!Z_OBJ_HT_P(object)->write_property) {
3817        zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, ZSTR_VAL(Z_OBJCE_P(object)->name));
3818    }
3819    ZVAL_STR(&property, name);
3820    Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
3821
3822    EG(scope) = old_scope;
3823}
3824/* }}} */
3825
3826ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value) /* {{{ */
3827{
3828    zval property;
3829    zend_class_entry *old_scope = EG(scope);
3830
3831    EG(scope) = scope;
3832
3833    if (!Z_OBJ_HT_P(object)->write_property) {
3834        zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, ZSTR_VAL(Z_OBJCE_P(object)->name));
3835    }
3836    ZVAL_STRINGL(&property, name, name_length);
3837    Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
3838    zval_ptr_dtor(&property);
3839
3840    EG(scope) = old_scope;
3841}
3842/* }}} */
3843
3844ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, const char *name, size_t name_length) /* {{{ */
3845{
3846    zval tmp;
3847
3848    ZVAL_NULL(&tmp);
3849    zend_update_property(scope, object, name, name_length, &tmp);
3850}
3851/* }}} */
3852
3853ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value) /* {{{ */
3854{
3855    zval tmp;
3856
3857    ZVAL_BOOL(&tmp, value);
3858    zend_update_property(scope, object, name, name_length, &tmp);
3859}
3860/* }}} */
3861
3862ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value) /* {{{ */
3863{
3864    zval tmp;
3865
3866    ZVAL_LONG(&tmp, value);
3867    zend_update_property(scope, object, name, name_length, &tmp);
3868}
3869/* }}} */
3870
3871ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, const char *name, size_t name_length, double value) /* {{{ */
3872{
3873    zval tmp;
3874
3875    ZVAL_DOUBLE(&tmp, value);
3876    zend_update_property(scope, object, name, name_length, &tmp);
3877}
3878/* }}} */
3879
3880ZEND_API void zend_update_property_str(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
3881{
3882    zval tmp;
3883
3884    ZVAL_STR(&tmp, value);
3885    zend_update_property(scope, object, name, name_length, &tmp);
3886}
3887/* }}} */
3888
3889ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value) /* {{{ */
3890{
3891    zval tmp;
3892
3893    ZVAL_STRING(&tmp, value);
3894    Z_SET_REFCOUNT(tmp, 0);
3895    zend_update_property(scope, object, name, name_length, &tmp);
3896}
3897/* }}} */
3898
3899ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
3900{
3901    zval tmp;
3902
3903    ZVAL_STRINGL(&tmp, value, value_len);
3904    Z_SET_REFCOUNT(tmp, 0);
3905    zend_update_property(scope, object, name, name_length, &tmp);
3906}
3907/* }}} */
3908
3909ZEND_API int zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
3910{
3911    zval *property;
3912    zend_class_entry *old_scope = EG(scope);
3913    zend_string *key = zend_string_init(name, name_length, 0);
3914
3915    EG(scope) = scope;
3916    property = zend_std_get_static_property(scope, key, 0);
3917    EG(scope) = old_scope;
3918    zend_string_free(key);
3919    if (!property) {
3920        return FAILURE;
3921    } else {
3922        if (property != value) {
3923            if (Z_ISREF_P(property)) {
3924                zval_dtor(property);
3925                ZVAL_COPY_VALUE(property, value);
3926                if (Z_REFCOUNTED_P(value) && Z_REFCOUNT_P(value) > 0) {
3927                    zval_opt_copy_ctor(property);
3928                }
3929            } else {
3930                zval garbage;
3931
3932                ZVAL_COPY_VALUE(&garbage, property);
3933                if (Z_REFCOUNTED_P(value)) {
3934                    Z_ADDREF_P(value);
3935                    if (Z_ISREF_P(value)) {
3936                        SEPARATE_ZVAL(value);
3937                    }
3938                }
3939                ZVAL_COPY_VALUE(property, value);
3940                zval_ptr_dtor(&garbage);
3941            }
3942        }
3943        return SUCCESS;
3944    }
3945}
3946/* }}} */
3947
3948ZEND_API int zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
3949{
3950    zval tmp;
3951
3952    ZVAL_NULL(&tmp);
3953    return zend_update_static_property(scope, name, name_length, &tmp);
3954}
3955/* }}} */
3956
3957ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
3958{
3959    zval tmp;
3960
3961    ZVAL_BOOL(&tmp, value);
3962    return zend_update_static_property(scope, name, name_length, &tmp);
3963}
3964/* }}} */
3965
3966ZEND_API int zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
3967{
3968    zval tmp;
3969
3970    ZVAL_LONG(&tmp, value);
3971    return zend_update_static_property(scope, name, name_length, &tmp);
3972}
3973/* }}} */
3974
3975ZEND_API int zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
3976{
3977    zval tmp;
3978
3979    ZVAL_DOUBLE(&tmp, value);
3980    return zend_update_static_property(scope, name, name_length, &tmp);
3981}
3982/* }}} */
3983
3984ZEND_API int zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
3985{
3986    zval tmp;
3987
3988    ZVAL_STRING(&tmp, value);
3989    Z_SET_REFCOUNT(tmp, 0);
3990    return zend_update_static_property(scope, name, name_length, &tmp);
3991}
3992/* }}} */
3993
3994ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
3995{
3996    zval tmp;
3997
3998    ZVAL_STRINGL(&tmp, value, value_len);
3999    Z_SET_REFCOUNT(tmp, 0);
4000    return zend_update_static_property(scope, name, name_length, &tmp);
4001}
4002/* }}} */
4003
4004ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_bool silent, zval *rv) /* {{{ */
4005{
4006    zval property, *value;
4007    zend_class_entry *old_scope = EG(scope);
4008
4009    EG(scope) = scope;
4010
4011    if (!Z_OBJ_HT_P(object)->read_property) {
4012        zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be read", name, ZSTR_VAL(Z_OBJCE_P(object)->name));
4013    }
4014
4015    ZVAL_STRINGL(&property, name, name_length);
4016    value = Z_OBJ_HT_P(object)->read_property(object, &property, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
4017    zval_ptr_dtor(&property);
4018
4019    EG(scope) = old_scope;
4020    return value;
4021}
4022/* }}} */
4023
4024ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, zend_bool silent) /* {{{ */
4025{
4026    zval *property;
4027    zend_class_entry *old_scope = EG(scope);
4028    zend_string *key = zend_string_init(name, name_length, 0);
4029
4030    EG(scope) = scope;
4031    property = zend_std_get_static_property(scope, key, silent);
4032    EG(scope) = old_scope;
4033    zend_string_free(key);
4034
4035    return property;
4036}
4037/* }}} */
4038
4039ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
4040{
4041    current->handling = EG(error_handling);
4042    current->exception = EG(exception_class);
4043    ZVAL_COPY(&current->user_handler, &EG(user_error_handler));
4044}
4045/* }}} */
4046
4047ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
4048{
4049    if (current) {
4050        zend_save_error_handling(current);
4051        if (error_handling != EH_NORMAL && Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
4052            zval_ptr_dtor(&EG(user_error_handler));
4053            ZVAL_UNDEF(&EG(user_error_handler));
4054        }
4055    }
4056    EG(error_handling) = error_handling;
4057    EG(exception_class) = error_handling == EH_THROW ? exception_class : NULL;
4058}
4059/* }}} */
4060
4061static int same_zval(zval *zv1, zval *zv2)  /* {{{ */
4062{
4063    if (Z_TYPE_P(zv1) != Z_TYPE_P(zv2)) {
4064        return 0;
4065    }
4066    switch (Z_TYPE_P(zv1)) {
4067        case IS_UNDEF:
4068        case IS_NULL:
4069        case IS_FALSE:
4070        case IS_TRUE:
4071            return 1;
4072        case IS_LONG:
4073            return Z_LVAL_P(zv1) == Z_LVAL_P(zv2);
4074        case IS_DOUBLE:
4075            return Z_LVAL_P(zv1) == Z_LVAL_P(zv2);
4076        case IS_STRING:
4077        case IS_ARRAY:
4078        case IS_OBJECT:
4079        case IS_RESOURCE:
4080            return Z_COUNTED_P(zv1) == Z_COUNTED_P(zv2);
4081        default:
4082            return 0;
4083    }
4084}
4085/* }}} */
4086
4087ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
4088{
4089    EG(error_handling) = saved->handling;
4090    EG(exception_class) = saved->handling == EH_THROW ? saved->exception : NULL;
4091    if (Z_TYPE(saved->user_handler) != IS_UNDEF
4092        && !same_zval(&saved->user_handler, &EG(user_error_handler))) {
4093        zval_ptr_dtor(&EG(user_error_handler));
4094        ZVAL_COPY_VALUE(&EG(user_error_handler), &saved->user_handler);
4095    } else if (Z_TYPE(saved->user_handler)) {
4096        zval_ptr_dtor(&saved->user_handler);
4097    }
4098    ZVAL_UNDEF(&saved->user_handler);
4099}
4100/* }}} */
4101
4102ZEND_API zend_string* zend_find_alias_name(zend_class_entry *ce, zend_string *name) /* {{{ */
4103{
4104    zend_trait_alias *alias, **alias_ptr;
4105
4106    if ((alias_ptr = ce->trait_aliases)) {
4107        alias = *alias_ptr;
4108        while (alias) {
4109            if (alias->alias && zend_string_equals_ci(alias->alias, name)) {
4110                return alias->alias;
4111            }
4112            alias_ptr++;
4113            alias = *alias_ptr;
4114        }
4115    }
4116
4117    return name;
4118}
4119/* }}} */
4120
4121ZEND_API zend_string *zend_resolve_method_name(zend_class_entry *ce, zend_function *f) /* {{{ */
4122{
4123    zend_function *func;
4124    HashTable *function_table;
4125    zend_string *name;
4126
4127    if (f->common.type != ZEND_USER_FUNCTION ||
4128        (f->op_array.refcount && *(f->op_array.refcount) < 2) ||
4129        !f->common.scope ||
4130        !f->common.scope->trait_aliases) {
4131        return f->common.function_name;
4132    }
4133
4134    function_table = &ce->function_table;
4135    ZEND_HASH_FOREACH_STR_KEY_PTR(function_table, name, func) {
4136        if (func == f) {
4137            if (!name) {
4138                return f->common.function_name;
4139            }
4140            if (ZSTR_LEN(name) == ZSTR_LEN(f->common.function_name) &&
4141                !strncasecmp(ZSTR_VAL(name), ZSTR_VAL(f->common.function_name), ZSTR_LEN(f->common.function_name))) {
4142                return f->common.function_name;
4143            }
4144            return zend_find_alias_name(f->common.scope, name);
4145        }
4146    } ZEND_HASH_FOREACH_END();
4147    return f->common.function_name;
4148}
4149/* }}} */
4150
4151ZEND_API const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */
4152{
4153    if(ce->ce_flags & ZEND_ACC_TRAIT) {
4154        return "trait";
4155    } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4156        return "interface";
4157    } else {
4158        return "class";
4159    }
4160}
4161/* }}} */
4162
4163/*
4164 * Local variables:
4165 * tab-width: 4
4166 * c-basic-offset: 4
4167 * indent-tabs-mode: t
4168 * End:
4169 */
4170