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