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