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