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