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