1/*
2  +----------------------------------------------------------------------+
3  | PHP Version 5                                                        |
4  +----------------------------------------------------------------------+
5  | Copyright (c) 1997-2014 The PHP Group                                |
6  +----------------------------------------------------------------------+
7  | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
11  | If you did not receive a copy of the PHP license and are unable to   |
12  | obtain it through the world-wide-web, please send a note to          |
13  | license@php.net so we can mail you a copy immediately.               |
14  +----------------------------------------------------------------------+
15  | Author: Sascha Schumann <sascha@schumann.cx>                         |
16  +----------------------------------------------------------------------+
17*/
18
19/* $Id$ */
20
21#include "php.h"
22#include "ext/standard/php_var.h"
23#include "php_incomplete_class.h"
24#include "Zend/zend_interfaces.h"
25
26/* {{{ reference-handling for unserializer: var_* */
27#define VAR_ENTRIES_MAX 1024
28#define VAR_ENTRIES_DBG 0
29
30typedef struct {
31    zval *data[VAR_ENTRIES_MAX];
32    long used_slots;
33    void *next;
34} var_entries;
35
36static inline void var_push(php_unserialize_data_t *var_hashx, zval **rval)
37{
38    var_entries *var_hash = (*var_hashx)->last;
39#if VAR_ENTRIES_DBG
40    fprintf(stderr, "var_push(%ld): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_PP(rval));
41#endif
42
43    if (!var_hash || var_hash->used_slots == VAR_ENTRIES_MAX) {
44        var_hash = emalloc(sizeof(var_entries));
45        var_hash->used_slots = 0;
46        var_hash->next = 0;
47
48        if (!(*var_hashx)->first) {
49            (*var_hashx)->first = var_hash;
50        } else {
51            ((var_entries *) (*var_hashx)->last)->next = var_hash;
52        }
53
54        (*var_hashx)->last = var_hash;
55    }
56
57    var_hash->data[var_hash->used_slots++] = *rval;
58}
59
60PHPAPI void var_push_dtor(php_unserialize_data_t *var_hashx, zval **rval)
61{
62    var_entries *var_hash;
63
64    if (!var_hashx || !*var_hashx) {
65        return;
66    }
67
68    var_hash = (*var_hashx)->last_dtor;
69#if VAR_ENTRIES_DBG
70    fprintf(stderr, "var_push_dtor(%ld): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_PP(rval));
71#endif
72
73    if (!var_hash || var_hash->used_slots == VAR_ENTRIES_MAX) {
74        var_hash = emalloc(sizeof(var_entries));
75        var_hash->used_slots = 0;
76        var_hash->next = 0;
77
78        if (!(*var_hashx)->first_dtor) {
79            (*var_hashx)->first_dtor = var_hash;
80        } else {
81            ((var_entries *) (*var_hashx)->last_dtor)->next = var_hash;
82        }
83
84        (*var_hashx)->last_dtor = var_hash;
85    }
86
87    Z_ADDREF_PP(rval);
88    var_hash->data[var_hash->used_slots++] = *rval;
89}
90
91PHPAPI void var_push_dtor_no_addref(php_unserialize_data_t *var_hashx, zval **rval)
92{
93    var_entries *var_hash = (*var_hashx)->last_dtor;
94#if VAR_ENTRIES_DBG
95    fprintf(stderr, "var_push_dtor_no_addref(%ld): %d (%d)\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_PP(rval), Z_REFCOUNT_PP(rval));
96#endif
97
98    if (!var_hash || var_hash->used_slots == VAR_ENTRIES_MAX) {
99        var_hash = emalloc(sizeof(var_entries));
100        var_hash->used_slots = 0;
101        var_hash->next = 0;
102
103        if (!(*var_hashx)->first_dtor) {
104            (*var_hashx)->first_dtor = var_hash;
105        } else {
106            ((var_entries *) (*var_hashx)->last_dtor)->next = var_hash;
107        }
108
109        (*var_hashx)->last_dtor = var_hash;
110    }
111
112    var_hash->data[var_hash->used_slots++] = *rval;
113}
114
115PHPAPI void var_replace(php_unserialize_data_t *var_hashx, zval *ozval, zval **nzval)
116{
117    long i;
118    var_entries *var_hash = (*var_hashx)->first;
119#if VAR_ENTRIES_DBG
120    fprintf(stderr, "var_replace(%ld): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_PP(nzval));
121#endif
122
123    while (var_hash) {
124        for (i = 0; i < var_hash->used_slots; i++) {
125            if (var_hash->data[i] == ozval) {
126                var_hash->data[i] = *nzval;
127                /* do not break here */
128            }
129        }
130        var_hash = var_hash->next;
131    }
132}
133
134static int var_access(php_unserialize_data_t *var_hashx, long id, zval ***store)
135{
136    var_entries *var_hash = (*var_hashx)->first;
137#if VAR_ENTRIES_DBG
138    fprintf(stderr, "var_access(%ld): %ld\n", var_hash?var_hash->used_slots:-1L, id);
139#endif
140
141    while (id >= VAR_ENTRIES_MAX && var_hash && var_hash->used_slots == VAR_ENTRIES_MAX) {
142        var_hash = var_hash->next;
143        id -= VAR_ENTRIES_MAX;
144    }
145
146    if (!var_hash) return !SUCCESS;
147
148    if (id < 0 || id >= var_hash->used_slots) return !SUCCESS;
149
150    *store = &var_hash->data[id];
151
152    return SUCCESS;
153}
154
155PHPAPI void var_destroy(php_unserialize_data_t *var_hashx)
156{
157    void *next;
158    long i;
159    var_entries *var_hash = (*var_hashx)->first;
160#if VAR_ENTRIES_DBG
161    fprintf(stderr, "var_destroy(%ld)\n", var_hash?var_hash->used_slots:-1L);
162#endif
163
164    while (var_hash) {
165        next = var_hash->next;
166        efree(var_hash);
167        var_hash = next;
168    }
169
170    var_hash = (*var_hashx)->first_dtor;
171
172    while (var_hash) {
173        for (i = 0; i < var_hash->used_slots; i++) {
174            zval_ptr_dtor(&var_hash->data[i]);
175        }
176        next = var_hash->next;
177        efree(var_hash);
178        var_hash = next;
179    }
180}
181
182/* }}} */
183
184static char *unserialize_str(const unsigned char **p, size_t *len, size_t maxlen)
185{
186    size_t i, j;
187    char *str = safe_emalloc(*len, 1, 1);
188    unsigned char *end = *(unsigned char **)p+maxlen;
189
190    if (end < *p) {
191        efree(str);
192        return NULL;
193    }
194
195    for (i = 0; i < *len; i++) {
196        if (*p >= end) {
197            efree(str);
198            return NULL;
199        }
200        if (**p != '\\') {
201            str[i] = (char)**p;
202        } else {
203            unsigned char ch = 0;
204
205            for (j = 0; j < 2; j++) {
206                (*p)++;
207                if (**p >= '0' && **p <= '9') {
208                    ch = (ch << 4) + (**p -'0');
209                } else if (**p >= 'a' && **p <= 'f') {
210                    ch = (ch << 4) + (**p -'a'+10);
211                } else if (**p >= 'A' && **p <= 'F') {
212                    ch = (ch << 4) + (**p -'A'+10);
213                } else {
214                    efree(str);
215                    return NULL;
216                }
217            }
218            str[i] = (char)ch;
219        }
220        (*p)++;
221    }
222    str[i] = 0;
223    *len = i;
224    return str;
225}
226
227#define YYFILL(n) do { } while (0)
228#define YYCTYPE unsigned char
229#define YYCURSOR cursor
230#define YYLIMIT limit
231#define YYMARKER marker
232
233
234/*!re2c
235uiv = [+]? [0-9]+;
236iv = [+-]? [0-9]+;
237nv = [+-]? ([0-9]* "." [0-9]+|[0-9]+ "." [0-9]*);
238nvexp = (iv | nv) [eE] [+-]? iv;
239any = [\000-\377];
240object = [OC];
241*/
242
243
244
245static inline long parse_iv2(const unsigned char *p, const unsigned char **q)
246{
247    char cursor;
248    long result = 0;
249    int neg = 0;
250
251    switch (*p) {
252        case '-':
253            neg++;
254            /* fall-through */
255        case '+':
256            p++;
257    }
258
259    while (1) {
260        cursor = (char)*p;
261        if (cursor >= '0' && cursor <= '9') {
262            result = result * 10 + (size_t)(cursor - (unsigned char)'0');
263        } else {
264            break;
265        }
266        p++;
267    }
268    if (q) *q = p;
269    if (neg) return -result;
270    return result;
271}
272
273static inline long parse_iv(const unsigned char *p)
274{
275    return parse_iv2(p, NULL);
276}
277
278/* no need to check for length - re2c already did */
279static inline size_t parse_uiv(const unsigned char *p)
280{
281    unsigned char cursor;
282    size_t result = 0;
283
284    if (*p == '+') {
285        p++;
286    }
287
288    while (1) {
289        cursor = *p;
290        if (cursor >= '0' && cursor <= '9') {
291            result = result * 10 + (size_t)(cursor - (unsigned char)'0');
292        } else {
293            break;
294        }
295        p++;
296    }
297    return result;
298}
299
300#define UNSERIALIZE_PARAMETER zval **rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash TSRMLS_DC
301#define UNSERIALIZE_PASSTHRU rval, p, max, var_hash TSRMLS_CC
302
303static inline int process_nested_data(UNSERIALIZE_PARAMETER, HashTable *ht, long elements, int objprops)
304{
305    while (elements-- > 0) {
306        zval *key, *data, **old_data;
307
308        ALLOC_INIT_ZVAL(key);
309
310        if (!php_var_unserialize(&key, p, max, NULL TSRMLS_CC)) {
311            zval_dtor(key);
312            FREE_ZVAL(key);
313            return 0;
314        }
315
316        if (Z_TYPE_P(key) != IS_LONG && Z_TYPE_P(key) != IS_STRING) {
317            zval_dtor(key);
318            FREE_ZVAL(key);
319            return 0;
320        }
321
322        ALLOC_INIT_ZVAL(data);
323
324        if (!php_var_unserialize(&data, p, max, var_hash TSRMLS_CC)) {
325            zval_dtor(key);
326            FREE_ZVAL(key);
327            zval_dtor(data);
328            FREE_ZVAL(data);
329            return 0;
330        }
331
332        if (!objprops) {
333            switch (Z_TYPE_P(key)) {
334            case IS_LONG:
335                if (zend_hash_index_find(ht, Z_LVAL_P(key), (void **)&old_data)==SUCCESS) {
336                    var_push_dtor(var_hash, old_data);
337                }
338                zend_hash_index_update(ht, Z_LVAL_P(key), &data, sizeof(data), NULL);
339                break;
340            case IS_STRING:
341                if (zend_symtable_find(ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void **)&old_data)==SUCCESS) {
342                    var_push_dtor(var_hash, old_data);
343                }
344                zend_symtable_update(ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &data, sizeof(data), NULL);
345                break;
346            }
347        } else {
348            /* object properties should include no integers */
349            convert_to_string(key);
350            if (zend_symtable_find(ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void **)&old_data)==SUCCESS) {
351                var_push_dtor(var_hash, old_data);
352            }
353            zend_hash_update(ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &data,
354                    sizeof data, NULL);
355        }
356
357        zval_dtor(key);
358        FREE_ZVAL(key);
359
360        if (elements && *(*p-1) != ';' && *(*p-1) != '}') {
361            (*p)--;
362            return 0;
363        }
364    }
365
366    return 1;
367}
368
369static inline int finish_nested_data(UNSERIALIZE_PARAMETER)
370{
371    if (*((*p)++) == '}')
372        return 1;
373
374#if SOMETHING_NEW_MIGHT_LEAD_TO_CRASH_ENABLE_IF_YOU_ARE_BRAVE
375    zval_ptr_dtor(rval);
376#endif
377    return 0;
378}
379
380static inline int object_custom(UNSERIALIZE_PARAMETER, zend_class_entry *ce)
381{
382    long datalen;
383
384    datalen = parse_iv2((*p) + 2, p);
385
386    (*p) += 2;
387
388    if (datalen < 0 || (max - (*p)) <= datalen) {
389        zend_error(E_WARNING, "Insufficient data for unserializing - %ld required, %ld present", datalen, (long)(max - (*p)));
390        return 0;
391    }
392
393    if (ce->unserialize == NULL) {
394        zend_error(E_WARNING, "Class %s has no unserializer", ce->name);
395        object_init_ex(*rval, ce);
396    } else if (ce->unserialize(rval, ce, (const unsigned char*)*p, datalen, (zend_unserialize_data *)var_hash TSRMLS_CC) != SUCCESS) {
397        return 0;
398    }
399
400    (*p) += datalen;
401
402    return finish_nested_data(UNSERIALIZE_PASSTHRU);
403}
404
405static inline long object_common1(UNSERIALIZE_PARAMETER, zend_class_entry *ce)
406{
407    long elements;
408
409    elements = parse_iv2((*p) + 2, p);
410
411    (*p) += 2;
412
413    /* The internal class check here is a BC fix only, userspace classes implementing the
414    Serializable interface have eventually an inconsistent behavior at this place when
415    unserialized from a manipulated string. Additionaly the interal classes can possibly
416    crash PHP so they're still disabled here. */
417    if (ce->serialize == NULL || ce->unserialize == zend_user_unserialize || (ZEND_INTERNAL_CLASS != ce->type && ce->create_object == NULL)) {
418        object_init_ex(*rval, ce);
419    } else {
420        /* If this class implements Serializable, it should not land here but in object_custom(). The passed string
421        obviously doesn't descend from the regular serializer. */
422        zend_error(E_WARNING, "Erroneous data format for unserializing '%s'", ce->name);
423        return 0;
424    }
425
426    return elements;
427}
428
429#ifdef PHP_WIN32
430# pragma optimize("", off)
431#endif
432static inline int object_common2(UNSERIALIZE_PARAMETER, long elements)
433{
434    zval *retval_ptr = NULL;
435    zval fname;
436
437    if (Z_TYPE_PP(rval) != IS_OBJECT) {
438        return 0;
439    }
440
441    if (!process_nested_data(UNSERIALIZE_PASSTHRU, Z_OBJPROP_PP(rval), elements, 1)) {
442        return 0;
443    }
444
445    if (Z_OBJCE_PP(rval) != PHP_IC_ENTRY &&
446        zend_hash_exists(&Z_OBJCE_PP(rval)->function_table, "__wakeup", sizeof("__wakeup"))) {
447        INIT_PZVAL(&fname);
448        ZVAL_STRINGL(&fname, "__wakeup", sizeof("__wakeup") - 1, 0);
449        BG(serialize_lock)++;
450        call_user_function_ex(CG(function_table), rval, &fname, &retval_ptr, 0, 0, 1, NULL TSRMLS_CC);
451        BG(serialize_lock)--;
452    }
453
454    if (retval_ptr) {
455        zval_ptr_dtor(&retval_ptr);
456    }
457
458    if (EG(exception)) {
459        return 0;
460    }
461
462    return finish_nested_data(UNSERIALIZE_PASSTHRU);
463
464}
465#ifdef PHP_WIN32
466# pragma optimize("", on)
467#endif
468
469PHPAPI int php_var_unserialize(UNSERIALIZE_PARAMETER)
470{
471    const unsigned char *cursor, *limit, *marker, *start;
472    zval **rval_ref;
473
474    limit = max;
475    cursor = *p;
476
477    if (YYCURSOR >= YYLIMIT) {
478        return 0;
479    }
480
481    if (var_hash && cursor[0] != 'R') {
482        var_push(var_hash, rval);
483    }
484
485    start = cursor;
486
487
488
489/*!re2c
490
491"R:" iv ";"     {
492    long id;
493
494    *p = YYCURSOR;
495    if (!var_hash) return 0;
496
497    id = parse_iv(start + 2) - 1;
498    if (id == -1 || var_access(var_hash, id, &rval_ref) != SUCCESS) {
499        return 0;
500    }
501
502    if (*rval != NULL) {
503        zval_ptr_dtor(rval);
504    }
505    *rval = *rval_ref;
506    Z_ADDREF_PP(rval);
507    Z_SET_ISREF_PP(rval);
508
509    return 1;
510}
511
512"r:" iv ";"     {
513    long id;
514
515    *p = YYCURSOR;
516    if (!var_hash) return 0;
517
518    id = parse_iv(start + 2) - 1;
519    if (id == -1 || var_access(var_hash, id, &rval_ref) != SUCCESS) {
520        return 0;
521    }
522
523    if (*rval == *rval_ref) return 0;
524
525    if (*rval != NULL) {
526        var_push_dtor_no_addref(var_hash, rval);
527    }
528    *rval = *rval_ref;
529    Z_ADDREF_PP(rval);
530    Z_UNSET_ISREF_PP(rval);
531
532    return 1;
533}
534
535"N;"    {
536    *p = YYCURSOR;
537    INIT_PZVAL(*rval);
538    ZVAL_NULL(*rval);
539    return 1;
540}
541
542"b:" [01] ";"   {
543    *p = YYCURSOR;
544    INIT_PZVAL(*rval);
545    ZVAL_BOOL(*rval, parse_iv(start + 2));
546    return 1;
547}
548
549"i:" iv ";" {
550#if SIZEOF_LONG == 4
551    int digits = YYCURSOR - start - 3;
552
553    if (start[2] == '-' || start[2] == '+') {
554        digits--;
555    }
556
557    /* Use double for large long values that were serialized on a 64-bit system */
558    if (digits >= MAX_LENGTH_OF_LONG - 1) {
559        if (digits == MAX_LENGTH_OF_LONG - 1) {
560            int cmp = strncmp(YYCURSOR - MAX_LENGTH_OF_LONG, long_min_digits, MAX_LENGTH_OF_LONG - 1);
561
562            if (!(cmp < 0 || (cmp == 0 && start[2] == '-'))) {
563                goto use_double;
564            }
565        } else {
566            goto use_double;
567        }
568    }
569#endif
570    *p = YYCURSOR;
571    INIT_PZVAL(*rval);
572    ZVAL_LONG(*rval, parse_iv(start + 2));
573    return 1;
574}
575
576"d:" ("NAN" | "-"? "INF") ";"   {
577    *p = YYCURSOR;
578    INIT_PZVAL(*rval);
579
580    if (!strncmp(start + 2, "NAN", 3)) {
581        ZVAL_DOUBLE(*rval, php_get_nan());
582    } else if (!strncmp(start + 2, "INF", 3)) {
583        ZVAL_DOUBLE(*rval, php_get_inf());
584    } else if (!strncmp(start + 2, "-INF", 4)) {
585        ZVAL_DOUBLE(*rval, -php_get_inf());
586    }
587
588    return 1;
589}
590
591"d:" (iv | nv | nvexp) ";"  {
592#if SIZEOF_LONG == 4
593use_double:
594#endif
595    *p = YYCURSOR;
596    INIT_PZVAL(*rval);
597    ZVAL_DOUBLE(*rval, zend_strtod((const char *)start + 2, NULL));
598    return 1;
599}
600
601"s:" uiv ":" ["]    {
602    size_t len, maxlen;
603    char *str;
604
605    len = parse_uiv(start + 2);
606    maxlen = max - YYCURSOR;
607    if (maxlen < len) {
608        *p = start + 2;
609        return 0;
610    }
611
612    str = (char*)YYCURSOR;
613
614    YYCURSOR += len;
615
616    if (*(YYCURSOR) != '"') {
617        *p = YYCURSOR;
618        return 0;
619    }
620
621    YYCURSOR += 2;
622    *p = YYCURSOR;
623
624    INIT_PZVAL(*rval);
625    ZVAL_STRINGL(*rval, str, len, 1);
626    return 1;
627}
628
629"S:" uiv ":" ["]    {
630    size_t len, maxlen;
631    char *str;
632
633    len = parse_uiv(start + 2);
634    maxlen = max - YYCURSOR;
635    if (maxlen < len) {
636        *p = start + 2;
637        return 0;
638    }
639
640    if ((str = unserialize_str(&YYCURSOR, &len, maxlen)) == NULL) {
641        return 0;
642    }
643
644    if (*(YYCURSOR) != '"') {
645        efree(str);
646        *p = YYCURSOR;
647        return 0;
648    }
649
650    YYCURSOR += 2;
651    *p = YYCURSOR;
652
653    INIT_PZVAL(*rval);
654    ZVAL_STRINGL(*rval, str, len, 0);
655    return 1;
656}
657
658"a:" uiv ":" "{" {
659    long elements = parse_iv(start + 2);
660    /* use iv() not uiv() in order to check data range */
661    *p = YYCURSOR;
662
663    if (elements < 0) {
664        return 0;
665    }
666
667    INIT_PZVAL(*rval);
668
669    array_init_size(*rval, elements);
670
671    if (!process_nested_data(UNSERIALIZE_PASSTHRU, Z_ARRVAL_PP(rval), elements, 0)) {
672        return 0;
673    }
674
675    return finish_nested_data(UNSERIALIZE_PASSTHRU);
676}
677
678"o:" iv ":" ["] {
679
680    INIT_PZVAL(*rval);
681
682    return object_common2(UNSERIALIZE_PASSTHRU,
683            object_common1(UNSERIALIZE_PASSTHRU, ZEND_STANDARD_CLASS_DEF_PTR));
684}
685
686object ":" uiv ":" ["]  {
687    size_t len, len2, len3, maxlen;
688    long elements;
689    char *class_name;
690    zend_class_entry *ce;
691    zend_class_entry **pce;
692    int incomplete_class = 0;
693
694    int custom_object = 0;
695
696    zval *user_func;
697    zval *retval_ptr;
698    zval **args[1];
699    zval *arg_func_name;
700
701    if (*start == 'C') {
702        custom_object = 1;
703    }
704
705    INIT_PZVAL(*rval);
706    len2 = len = parse_uiv(start + 2);
707    maxlen = max - YYCURSOR;
708    if (maxlen < len || len == 0) {
709        *p = start + 2;
710        return 0;
711    }
712
713    class_name = (char*)YYCURSOR;
714
715    YYCURSOR += len;
716
717    if (*(YYCURSOR) != '"') {
718        *p = YYCURSOR;
719        return 0;
720    }
721    if (*(YYCURSOR+1) != ':') {
722        *p = YYCURSOR+1;
723        return 0;
724    }
725
726    len3 = strspn(class_name, "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\\");
727    if (len3 != len)
728    {
729        *p = YYCURSOR + len3 - len;
730        return 0;
731    }
732
733    class_name = estrndup(class_name, len);
734
735    do {
736        /* Try to find class directly */
737        BG(serialize_lock)++;
738        if (zend_lookup_class(class_name, len2, &pce TSRMLS_CC) == SUCCESS) {
739            BG(serialize_lock)--;
740            if (EG(exception)) {
741                efree(class_name);
742                return 0;
743            }
744            ce = *pce;
745            break;
746        }
747        BG(serialize_lock)--;
748
749        if (EG(exception)) {
750            efree(class_name);
751            return 0;
752        }
753
754        /* Check for unserialize callback */
755        if ((PG(unserialize_callback_func) == NULL) || (PG(unserialize_callback_func)[0] == '\0')) {
756            incomplete_class = 1;
757            ce = PHP_IC_ENTRY;
758            break;
759        }
760
761        /* Call unserialize callback */
762        MAKE_STD_ZVAL(user_func);
763        ZVAL_STRING(user_func, PG(unserialize_callback_func), 1);
764        args[0] = &arg_func_name;
765        MAKE_STD_ZVAL(arg_func_name);
766        ZVAL_STRING(arg_func_name, class_name, 1);
767        BG(serialize_lock)++;
768        if (call_user_function_ex(CG(function_table), NULL, user_func, &retval_ptr, 1, args, 0, NULL TSRMLS_CC) != SUCCESS) {
769            BG(serialize_lock)--;
770            if (EG(exception)) {
771                efree(class_name);
772                zval_ptr_dtor(&user_func);
773                zval_ptr_dtor(&arg_func_name);
774                return 0;
775            }
776            php_error_docref(NULL TSRMLS_CC, E_WARNING, "defined (%s) but not found", user_func->value.str.val);
777            incomplete_class = 1;
778            ce = PHP_IC_ENTRY;
779            zval_ptr_dtor(&user_func);
780            zval_ptr_dtor(&arg_func_name);
781            break;
782        }
783        BG(serialize_lock)--;
784        if (retval_ptr) {
785            zval_ptr_dtor(&retval_ptr);
786        }
787        if (EG(exception)) {
788            efree(class_name);
789            zval_ptr_dtor(&user_func);
790            zval_ptr_dtor(&arg_func_name);
791            return 0;
792        }
793
794        /* The callback function may have defined the class */
795        if (zend_lookup_class(class_name, len2, &pce TSRMLS_CC) == SUCCESS) {
796            ce = *pce;
797        } else {
798            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Function %s() hasn't defined the class it was called for", user_func->value.str.val);
799            incomplete_class = 1;
800            ce = PHP_IC_ENTRY;
801        }
802
803        zval_ptr_dtor(&user_func);
804        zval_ptr_dtor(&arg_func_name);
805        break;
806    } while (1);
807
808    *p = YYCURSOR;
809
810    if (custom_object) {
811        int ret;
812
813        ret = object_custom(UNSERIALIZE_PASSTHRU, ce);
814
815        if (ret && incomplete_class) {
816            php_store_class_name(*rval, class_name, len2);
817        }
818        efree(class_name);
819        return ret;
820    }
821
822    elements = object_common1(UNSERIALIZE_PASSTHRU, ce);
823
824    if (incomplete_class) {
825        php_store_class_name(*rval, class_name, len2);
826    }
827    efree(class_name);
828
829    return object_common2(UNSERIALIZE_PASSTHRU, elements);
830}
831
832"}" {
833    /* this is the case where we have less data than planned */
834    php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unexpected end of serialized data");
835    return 0; /* not sure if it should be 0 or 1 here? */
836}
837
838any { return 0; }
839
840*/
841
842    return 0;
843}
844