2003-04-17 10:53:32 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-09 01:33:29 +08:00
|
|
|
| PHP Version 5 |
|
2003-04-17 10:53:32 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2013-01-01 16:28:54 +08:00
|
|
|
| Copyright (c) 1997-2013 The PHP Group |
|
2003-04-17 10:53:32 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:26:08 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2003-04-17 10:53:32 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:26:08 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2003-04-17 10:53:32 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Sascha Schumann <sascha@schumann.cx> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
#include "php.h"
|
|
|
|
#include "ext/standard/php_var.h"
|
|
|
|
#include "php_incomplete_class.h"
|
|
|
|
|
|
|
|
/* {{{ reference-handling for unserializer: var_* */
|
|
|
|
#define VAR_ENTRIES_MAX 1024
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
zval *data[VAR_ENTRIES_MAX];
|
2005-03-10 08:10:21 +08:00
|
|
|
long used_slots;
|
2001-11-11 05:18:34 +08:00
|
|
|
void *next;
|
|
|
|
} var_entries;
|
|
|
|
|
|
|
|
static inline void var_push(php_unserialize_data_t *var_hashx, zval **rval)
|
|
|
|
{
|
2010-09-19 00:09:28 +08:00
|
|
|
var_entries *var_hash = (*var_hashx)->last;
|
2010-05-26 15:24:37 +08:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "var_push(%ld): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_PP(rval));
|
|
|
|
#endif
|
2001-11-11 05:18:34 +08:00
|
|
|
|
2010-09-19 00:09:28 +08:00
|
|
|
if (!var_hash || var_hash->used_slots == VAR_ENTRIES_MAX) {
|
2001-11-11 05:18:34 +08:00
|
|
|
var_hash = emalloc(sizeof(var_entries));
|
|
|
|
var_hash->used_slots = 0;
|
|
|
|
var_hash->next = 0;
|
|
|
|
|
2010-09-19 00:09:28 +08:00
|
|
|
if (!(*var_hashx)->first) {
|
2010-05-26 15:24:37 +08:00
|
|
|
(*var_hashx)->first = var_hash;
|
2010-09-19 00:09:28 +08:00
|
|
|
} else {
|
|
|
|
((var_entries *) (*var_hashx)->last)->next = var_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*var_hashx)->last = var_hash;
|
2001-11-11 05:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var_hash->data[var_hash->used_slots++] = *rval;
|
|
|
|
}
|
|
|
|
|
2010-06-29 08:58:31 +08:00
|
|
|
PHPAPI void var_push_dtor(php_unserialize_data_t *var_hashx, zval **rval)
|
2005-01-16 02:18:08 +08:00
|
|
|
{
|
2010-09-19 00:09:28 +08:00
|
|
|
var_entries *var_hash = (*var_hashx)->last_dtor;
|
2010-05-26 15:24:37 +08:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "var_push_dtor(%ld): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_PP(rval));
|
|
|
|
#endif
|
2005-01-16 02:18:08 +08:00
|
|
|
|
2010-09-19 00:09:28 +08:00
|
|
|
if (!var_hash || var_hash->used_slots == VAR_ENTRIES_MAX) {
|
2005-01-16 02:18:08 +08:00
|
|
|
var_hash = emalloc(sizeof(var_entries));
|
|
|
|
var_hash->used_slots = 0;
|
|
|
|
var_hash->next = 0;
|
|
|
|
|
2010-09-19 00:09:28 +08:00
|
|
|
if (!(*var_hashx)->first_dtor) {
|
2010-05-26 15:24:37 +08:00
|
|
|
(*var_hashx)->first_dtor = var_hash;
|
2010-09-19 00:09:28 +08:00
|
|
|
} else {
|
|
|
|
((var_entries *) (*var_hashx)->last_dtor)->next = var_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*var_hashx)->last_dtor = var_hash;
|
2005-01-16 02:18:08 +08:00
|
|
|
}
|
|
|
|
|
2007-10-07 13:22:07 +08:00
|
|
|
Z_ADDREF_PP(rval);
|
2005-01-16 02:18:08 +08:00
|
|
|
var_hash->data[var_hash->used_slots++] = *rval;
|
|
|
|
}
|
|
|
|
|
2002-08-05 07:45:38 +08:00
|
|
|
PHPAPI void var_replace(php_unserialize_data_t *var_hashx, zval *ozval, zval **nzval)
|
2001-11-11 05:18:34 +08:00
|
|
|
{
|
2005-03-10 08:10:21 +08:00
|
|
|
long i;
|
2010-05-26 15:24:37 +08:00
|
|
|
var_entries *var_hash = (*var_hashx)->first;
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "var_replace(%ld): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_PP(nzval));
|
|
|
|
#endif
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
while (var_hash) {
|
|
|
|
for (i = 0; i < var_hash->used_slots; i++) {
|
|
|
|
if (var_hash->data[i] == ozval) {
|
|
|
|
var_hash->data[i] = *nzval;
|
2004-12-04 00:02:48 +08:00
|
|
|
/* do not break here */
|
2001-11-11 05:18:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var_hash = var_hash->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-10 08:10:21 +08:00
|
|
|
static int var_access(php_unserialize_data_t *var_hashx, long id, zval ***store)
|
2001-11-11 05:18:34 +08:00
|
|
|
{
|
2010-05-26 15:24:37 +08:00
|
|
|
var_entries *var_hash = (*var_hashx)->first;
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "var_access(%ld): %ld\n", var_hash?var_hash->used_slots:-1L, id);
|
|
|
|
#endif
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
while (id >= VAR_ENTRIES_MAX && var_hash && var_hash->used_slots == VAR_ENTRIES_MAX) {
|
|
|
|
var_hash = var_hash->next;
|
|
|
|
id -= VAR_ENTRIES_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!var_hash) return !SUCCESS;
|
|
|
|
|
2004-12-02 06:42:00 +08:00
|
|
|
if (id < 0 || id >= var_hash->used_slots) return !SUCCESS;
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
*store = &var_hash->data[id];
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2002-08-05 07:45:38 +08:00
|
|
|
PHPAPI void var_destroy(php_unserialize_data_t *var_hashx)
|
2001-11-11 05:18:34 +08:00
|
|
|
{
|
|
|
|
void *next;
|
2005-03-10 08:10:21 +08:00
|
|
|
long i;
|
2010-05-26 15:24:37 +08:00
|
|
|
var_entries *var_hash = (*var_hashx)->first;
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "var_destroy(%ld)\n", var_hash?var_hash->used_slots:-1L);
|
|
|
|
#endif
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
while (var_hash) {
|
|
|
|
next = var_hash->next;
|
|
|
|
efree(var_hash);
|
|
|
|
var_hash = next;
|
|
|
|
}
|
2005-01-16 02:18:08 +08:00
|
|
|
|
2010-05-26 15:24:37 +08:00
|
|
|
var_hash = (*var_hashx)->first_dtor;
|
2005-01-16 02:18:08 +08:00
|
|
|
|
|
|
|
while (var_hash) {
|
|
|
|
for (i = 0; i < var_hash->used_slots; i++) {
|
|
|
|
zval_ptr_dtor(&var_hash->data[i]);
|
|
|
|
}
|
|
|
|
next = var_hash->next;
|
|
|
|
efree(var_hash);
|
|
|
|
var_hash = next;
|
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
2007-07-09 22:31:56 +08:00
|
|
|
static char *unserialize_str(const unsigned char **p, size_t *len, size_t maxlen)
|
2006-12-15 08:58:08 +08:00
|
|
|
{
|
2007-03-24 04:15:22 +08:00
|
|
|
size_t i, j;
|
|
|
|
char *str = safe_emalloc(*len, 1, 1);
|
2007-07-09 22:31:56 +08:00
|
|
|
unsigned char *end = *(unsigned char **)p+maxlen;
|
2006-12-15 08:58:08 +08:00
|
|
|
|
2007-08-07 02:23:16 +08:00
|
|
|
if (end < *p) {
|
2007-03-24 04:15:22 +08:00
|
|
|
efree(str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-09 22:31:56 +08:00
|
|
|
for (i = 0; i < *len; i++) {
|
|
|
|
if (*p >= end) {
|
|
|
|
efree(str);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-12-15 08:58:08 +08:00
|
|
|
if (**p != '\\') {
|
|
|
|
str[i] = (char)**p;
|
|
|
|
} else {
|
|
|
|
unsigned char ch = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
(*p)++;
|
|
|
|
if (**p >= '0' && **p <= '9') {
|
|
|
|
ch = (ch << 4) + (**p -'0');
|
|
|
|
} else if (**p >= 'a' && **p <= 'f') {
|
|
|
|
ch = (ch << 4) + (**p -'a'+10);
|
|
|
|
} else if (**p >= 'A' && **p <= 'F') {
|
|
|
|
ch = (ch << 4) + (**p -'A'+10);
|
|
|
|
} else {
|
|
|
|
efree(str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str[i] = (char)ch;
|
|
|
|
}
|
|
|
|
(*p)++;
|
|
|
|
}
|
|
|
|
str[i] = 0;
|
2007-03-24 04:15:22 +08:00
|
|
|
*len = i;
|
2006-12-15 08:58:08 +08:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
#define YYFILL(n) do { } while (0)
|
|
|
|
#define YYCTYPE unsigned char
|
|
|
|
#define YYCURSOR cursor
|
|
|
|
#define YYLIMIT limit
|
|
|
|
#define YYMARKER marker
|
|
|
|
|
|
|
|
|
|
|
|
/*!re2c
|
2003-09-04 00:24:15 +08:00
|
|
|
uiv = [+]? [0-9]+;
|
2001-11-17 00:34:14 +08:00
|
|
|
iv = [+-]? [0-9]+;
|
2003-05-18 20:08:51 +08:00
|
|
|
nv = [+-]? ([0-9]* "." [0-9]+|[0-9]+ "." [0-9]*);
|
2002-08-20 03:45:46 +08:00
|
|
|
nvexp = (iv | nv) [eE] [+-]? iv;
|
2005-09-06 00:22:19 +08:00
|
|
|
any = [\000-\377];
|
2005-02-23 19:17:52 +08:00
|
|
|
object = [OC];
|
2001-11-11 05:18:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-03-10 08:10:21 +08:00
|
|
|
static inline long parse_iv2(const unsigned char *p, const unsigned char **q)
|
2001-11-11 05:18:34 +08:00
|
|
|
{
|
|
|
|
char cursor;
|
2005-03-10 08:10:21 +08:00
|
|
|
long result = 0;
|
2001-11-17 00:34:14 +08:00
|
|
|
int neg = 0;
|
|
|
|
|
|
|
|
switch (*p) {
|
|
|
|
case '-':
|
|
|
|
neg++;
|
|
|
|
/* fall-through */
|
|
|
|
case '+':
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
while (1) {
|
2004-09-06 01:16:02 +08:00
|
|
|
cursor = (char)*p;
|
2001-11-11 05:18:34 +08:00
|
|
|
if (cursor >= '0' && cursor <= '9') {
|
2010-08-07 02:19:23 +08:00
|
|
|
result = result * 10 + (size_t)(cursor - (unsigned char)'0');
|
2001-11-11 05:18:34 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (q) *q = p;
|
2001-11-17 00:34:14 +08:00
|
|
|
if (neg) return -result;
|
2001-11-11 05:18:34 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-03-10 08:10:21 +08:00
|
|
|
static inline long parse_iv(const unsigned char *p)
|
2001-11-11 05:18:34 +08:00
|
|
|
{
|
|
|
|
return parse_iv2(p, NULL);
|
|
|
|
}
|
|
|
|
|
2004-09-06 01:16:02 +08:00
|
|
|
/* no need to check for length - re2c already did */
|
|
|
|
static inline size_t parse_uiv(const unsigned char *p)
|
|
|
|
{
|
|
|
|
unsigned char cursor;
|
|
|
|
size_t result = 0;
|
|
|
|
|
|
|
|
if (*p == '+') {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
cursor = *p;
|
|
|
|
if (cursor >= '0' && cursor <= '9') {
|
|
|
|
result = result * 10 + (size_t)(cursor - (unsigned char)'0');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UNSERIALIZE_PARAMETER zval **rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash TSRMLS_DC
|
2001-11-11 05:18:34 +08:00
|
|
|
#define UNSERIALIZE_PASSTHRU rval, p, max, var_hash TSRMLS_CC
|
|
|
|
|
2011-09-28 22:47:42 +08:00
|
|
|
static inline int process_nested_data(UNSERIALIZE_PARAMETER, HashTable *ht, long elements, int objprops)
|
2001-11-11 05:18:34 +08:00
|
|
|
{
|
|
|
|
while (elements-- > 0) {
|
2005-01-17 19:45:09 +08:00
|
|
|
zval *key, *data, **old_data;
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
ALLOC_INIT_ZVAL(key);
|
|
|
|
|
|
|
|
if (!php_var_unserialize(&key, p, max, NULL TSRMLS_CC)) {
|
|
|
|
zval_dtor(key);
|
|
|
|
FREE_ZVAL(key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-27 05:47:27 +08:00
|
|
|
if (Z_TYPE_P(key) != IS_LONG && Z_TYPE_P(key) != IS_STRING) {
|
|
|
|
zval_dtor(key);
|
|
|
|
FREE_ZVAL(key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
ALLOC_INIT_ZVAL(data);
|
|
|
|
|
|
|
|
if (!php_var_unserialize(&data, p, max, var_hash TSRMLS_CC)) {
|
|
|
|
zval_dtor(key);
|
|
|
|
FREE_ZVAL(key);
|
|
|
|
zval_dtor(data);
|
|
|
|
FREE_ZVAL(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-28 22:47:42 +08:00
|
|
|
if (!objprops) {
|
|
|
|
switch (Z_TYPE_P(key)) {
|
2001-11-11 05:18:34 +08:00
|
|
|
case IS_LONG:
|
2005-01-16 02:30:16 +08:00
|
|
|
if (zend_hash_index_find(ht, Z_LVAL_P(key), (void **)&old_data)==SUCCESS) {
|
|
|
|
var_push_dtor(var_hash, old_data);
|
2004-12-04 00:02:48 +08:00
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
zend_hash_index_update(ht, Z_LVAL_P(key), &data, sizeof(data), NULL);
|
|
|
|
break;
|
|
|
|
case IS_STRING:
|
2008-03-19 11:05:35 +08:00
|
|
|
if (zend_symtable_find(ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void **)&old_data)==SUCCESS) {
|
2005-01-16 02:30:16 +08:00
|
|
|
var_push_dtor(var_hash, old_data);
|
2004-12-04 00:02:48 +08:00
|
|
|
}
|
2008-03-19 11:05:35 +08:00
|
|
|
zend_symtable_update(ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &data, sizeof(data), NULL);
|
2001-11-11 05:18:34 +08:00
|
|
|
break;
|
2011-09-28 22:47:42 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* object properties should include no integers */
|
|
|
|
convert_to_string(key);
|
|
|
|
zend_hash_update(ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &data,
|
|
|
|
sizeof data, NULL);
|
2001-11-11 05:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
zval_dtor(key);
|
|
|
|
FREE_ZVAL(key);
|
2004-09-12 20:43:05 +08:00
|
|
|
|
2007-08-07 02:23:16 +08:00
|
|
|
if (elements && *(*p-1) != ';' && *(*p-1) != '}') {
|
2004-09-12 20:43:05 +08:00
|
|
|
(*p)--;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int finish_nested_data(UNSERIALIZE_PARAMETER)
|
|
|
|
{
|
2007-08-07 02:23:16 +08:00
|
|
|
if (*((*p)++) == '}')
|
2001-11-11 05:18:34 +08:00
|
|
|
return 1;
|
|
|
|
|
2001-11-17 00:34:14 +08:00
|
|
|
#if SOMETHING_NEW_MIGHT_LEAD_TO_CRASH_ENABLE_IF_YOU_ARE_BRAVE
|
2001-11-11 05:18:34 +08:00
|
|
|
zval_ptr_dtor(rval);
|
2001-11-17 00:34:14 +08:00
|
|
|
#endif
|
2001-11-11 05:18:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-02-23 19:17:52 +08:00
|
|
|
static inline int object_custom(UNSERIALIZE_PARAMETER, zend_class_entry *ce)
|
|
|
|
{
|
2005-03-10 08:10:21 +08:00
|
|
|
long datalen;
|
2005-02-23 19:17:52 +08:00
|
|
|
|
|
|
|
datalen = parse_iv2((*p) + 2, p);
|
|
|
|
|
|
|
|
(*p) += 2;
|
|
|
|
|
2007-08-07 02:23:16 +08:00
|
|
|
if (datalen < 0 || (*p) + datalen >= max) {
|
2007-09-29 19:18:42 +08:00
|
|
|
zend_error(E_WARNING, "Insufficient data for unserializing - %ld required, %ld present", datalen, (long)(max - (*p)));
|
2005-02-23 19:17:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2005-03-08 06:27:29 +08:00
|
|
|
|
2008-08-29 22:59:20 +08:00
|
|
|
if (ce->unserialize == NULL) {
|
|
|
|
zend_error(E_WARNING, "Class %s has no unserializer", ce->name);
|
|
|
|
object_init_ex(*rval, ce);
|
|
|
|
} else if (ce->unserialize(rval, ce, (const unsigned char*)*p, datalen, (zend_unserialize_data *)var_hash TSRMLS_CC) != SUCCESS) {
|
2005-02-23 19:17:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*p) += datalen;
|
|
|
|
|
|
|
|
return finish_nested_data(UNSERIALIZE_PASSTHRU);
|
|
|
|
}
|
|
|
|
|
2005-03-10 08:10:21 +08:00
|
|
|
static inline long object_common1(UNSERIALIZE_PARAMETER, zend_class_entry *ce)
|
2001-11-11 05:18:34 +08:00
|
|
|
{
|
2005-03-10 08:10:21 +08:00
|
|
|
long elements;
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
elements = parse_iv2((*p) + 2, p);
|
|
|
|
|
|
|
|
(*p) += 2;
|
|
|
|
|
|
|
|
object_init_ex(*rval, ce);
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
|
2012-02-29 02:36:10 +08:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
# pragma optimize("", off)
|
|
|
|
#endif
|
2005-03-10 08:10:21 +08:00
|
|
|
static inline int object_common2(UNSERIALIZE_PARAMETER, long elements)
|
2001-11-11 05:18:34 +08:00
|
|
|
{
|
|
|
|
zval *retval_ptr = NULL;
|
|
|
|
zval fname;
|
|
|
|
|
2011-09-28 22:47:42 +08:00
|
|
|
if (!process_nested_data(UNSERIALIZE_PASSTHRU, Z_OBJPROP_PP(rval), elements, 1)) {
|
2001-11-11 05:18:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-01 19:03:58 +08:00
|
|
|
if (Z_OBJCE_PP(rval) != PHP_IC_ENTRY &&
|
2007-08-07 02:23:16 +08:00
|
|
|
zend_hash_exists(&Z_OBJCE_PP(rval)->function_table, "__wakeup", sizeof("__wakeup"))) {
|
2003-08-05 17:15:31 +08:00
|
|
|
INIT_PZVAL(&fname);
|
|
|
|
ZVAL_STRINGL(&fname, "__wakeup", sizeof("__wakeup") - 1, 0);
|
2011-10-19 18:09:24 +08:00
|
|
|
BG(serialize_lock)++;
|
2003-08-05 17:15:31 +08:00
|
|
|
call_user_function_ex(CG(function_table), rval, &fname, &retval_ptr, 0, 0, 1, NULL TSRMLS_CC);
|
2011-10-19 18:09:24 +08:00
|
|
|
BG(serialize_lock)--;
|
2003-08-05 17:15:31 +08:00
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
if (retval_ptr)
|
|
|
|
zval_ptr_dtor(&retval_ptr);
|
|
|
|
|
|
|
|
return finish_nested_data(UNSERIALIZE_PASSTHRU);
|
|
|
|
|
|
|
|
}
|
2012-02-29 02:36:10 +08:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
# pragma optimize("", on)
|
|
|
|
#endif
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
PHPAPI int php_var_unserialize(UNSERIALIZE_PARAMETER)
|
|
|
|
{
|
|
|
|
const unsigned char *cursor, *limit, *marker, *start;
|
|
|
|
zval **rval_ref;
|
|
|
|
|
2011-11-10 07:50:01 +08:00
|
|
|
limit = max;
|
|
|
|
cursor = *p;
|
|
|
|
|
|
|
|
if (YYCURSOR >= YYLIMIT) {
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
if (var_hash && cursor[0] != 'R') {
|
|
|
|
var_push(var_hash, rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
start = cursor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!re2c
|
2002-04-29 00:56:33 +08:00
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
"R:" iv ";" {
|
2005-03-10 08:10:21 +08:00
|
|
|
long id;
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
*p = YYCURSOR;
|
|
|
|
if (!var_hash) return 0;
|
|
|
|
|
|
|
|
id = parse_iv(start + 2) - 1;
|
|
|
|
if (id == -1 || var_access(var_hash, id, &rval_ref) != SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-07-03 00:19:57 +08:00
|
|
|
if (*rval != NULL) {
|
|
|
|
zval_ptr_dtor(rval);
|
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
*rval = *rval_ref;
|
2007-10-07 13:22:07 +08:00
|
|
|
Z_ADDREF_PP(rval);
|
|
|
|
Z_SET_ISREF_PP(rval);
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-02-03 22:12:59 +08:00
|
|
|
"r:" iv ";" {
|
2005-03-10 08:10:21 +08:00
|
|
|
long id;
|
2004-02-03 22:12:59 +08:00
|
|
|
|
|
|
|
*p = YYCURSOR;
|
|
|
|
if (!var_hash) return 0;
|
|
|
|
|
|
|
|
id = parse_iv(start + 2) - 1;
|
|
|
|
if (id == -1 || var_access(var_hash, id, &rval_ref) != SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-04 00:02:48 +08:00
|
|
|
if (*rval == *rval_ref) return 0;
|
|
|
|
|
2004-02-03 22:12:59 +08:00
|
|
|
if (*rval != NULL) {
|
|
|
|
zval_ptr_dtor(rval);
|
|
|
|
}
|
|
|
|
*rval = *rval_ref;
|
2007-10-07 13:22:07 +08:00
|
|
|
Z_ADDREF_PP(rval);
|
|
|
|
Z_UNSET_ISREF_PP(rval);
|
2004-02-03 22:12:59 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
"N;" {
|
|
|
|
*p = YYCURSOR;
|
|
|
|
INIT_PZVAL(*rval);
|
|
|
|
ZVAL_NULL(*rval);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-09-06 01:16:02 +08:00
|
|
|
"b:" [01] ";" {
|
2001-11-11 05:18:34 +08:00
|
|
|
*p = YYCURSOR;
|
|
|
|
INIT_PZVAL(*rval);
|
|
|
|
ZVAL_BOOL(*rval, parse_iv(start + 2));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
"i:" iv ";" {
|
2009-03-18 06:04:10 +08:00
|
|
|
#if SIZEOF_LONG == 4
|
|
|
|
int digits = YYCURSOR - start - 3;
|
|
|
|
|
|
|
|
if (start[2] == '-' || start[2] == '+') {
|
|
|
|
digits--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use double for large long values that were serialized on a 64-bit system */
|
|
|
|
if (digits >= MAX_LENGTH_OF_LONG - 1) {
|
|
|
|
if (digits == MAX_LENGTH_OF_LONG - 1) {
|
|
|
|
int cmp = strncmp(YYCURSOR - MAX_LENGTH_OF_LONG, long_min_digits, MAX_LENGTH_OF_LONG - 1);
|
|
|
|
|
|
|
|
if (!(cmp < 0 || (cmp == 0 && start[2] == '-'))) {
|
|
|
|
goto use_double;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
goto use_double;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2001-11-11 05:18:34 +08:00
|
|
|
*p = YYCURSOR;
|
|
|
|
INIT_PZVAL(*rval);
|
|
|
|
ZVAL_LONG(*rval, parse_iv(start + 2));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-27 08:50:41 +08:00
|
|
|
"d:" ("NAN" | "-"? "INF") ";" {
|
|
|
|
*p = YYCURSOR;
|
|
|
|
INIT_PZVAL(*rval);
|
2005-01-31 00:38:53 +08:00
|
|
|
|
2004-03-27 08:50:41 +08:00
|
|
|
if (!strncmp(start + 2, "NAN", 3)) {
|
|
|
|
ZVAL_DOUBLE(*rval, php_get_nan());
|
|
|
|
} else if (!strncmp(start + 2, "INF", 3)) {
|
|
|
|
ZVAL_DOUBLE(*rval, php_get_inf());
|
|
|
|
} else if (!strncmp(start + 2, "-INF", 4)) {
|
|
|
|
ZVAL_DOUBLE(*rval, -php_get_inf());
|
|
|
|
}
|
2005-01-31 00:38:53 +08:00
|
|
|
|
2004-03-27 08:50:41 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
"d:" (iv | nv | nvexp) ";" {
|
2009-03-18 06:04:10 +08:00
|
|
|
#if SIZEOF_LONG == 4
|
|
|
|
use_double:
|
|
|
|
#endif
|
2001-11-11 05:18:34 +08:00
|
|
|
*p = YYCURSOR;
|
|
|
|
INIT_PZVAL(*rval);
|
2005-03-10 08:10:21 +08:00
|
|
|
ZVAL_DOUBLE(*rval, zend_strtod((const char *)start + 2, NULL));
|
2001-11-11 05:18:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-09-04 00:24:15 +08:00
|
|
|
"s:" uiv ":" ["] {
|
2004-09-06 01:16:02 +08:00
|
|
|
size_t len, maxlen;
|
2001-11-11 05:18:34 +08:00
|
|
|
char *str;
|
|
|
|
|
2004-09-06 01:16:02 +08:00
|
|
|
len = parse_uiv(start + 2);
|
|
|
|
maxlen = max - YYCURSOR;
|
|
|
|
if (maxlen < len) {
|
|
|
|
*p = start + 2;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
|
2004-09-06 01:16:02 +08:00
|
|
|
str = (char*)YYCURSOR;
|
|
|
|
|
|
|
|
YYCURSOR += len;
|
|
|
|
|
|
|
|
if (*(YYCURSOR) != '"') {
|
|
|
|
*p = YYCURSOR;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
|
2004-09-06 01:16:02 +08:00
|
|
|
YYCURSOR += 2;
|
2001-11-11 05:18:34 +08:00
|
|
|
*p = YYCURSOR;
|
|
|
|
|
|
|
|
INIT_PZVAL(*rval);
|
2004-09-06 01:16:02 +08:00
|
|
|
ZVAL_STRINGL(*rval, str, len, 1);
|
2001-11-11 05:18:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-12-15 08:58:08 +08:00
|
|
|
"S:" uiv ":" ["] {
|
|
|
|
size_t len, maxlen;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
len = parse_uiv(start + 2);
|
|
|
|
maxlen = max - YYCURSOR;
|
|
|
|
if (maxlen < len) {
|
|
|
|
*p = start + 2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-09 22:31:56 +08:00
|
|
|
if ((str = unserialize_str(&YYCURSOR, &len, maxlen)) == NULL) {
|
2006-12-15 08:58:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*(YYCURSOR) != '"') {
|
|
|
|
efree(str);
|
|
|
|
*p = YYCURSOR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
YYCURSOR += 2;
|
|
|
|
*p = YYCURSOR;
|
|
|
|
|
|
|
|
INIT_PZVAL(*rval);
|
|
|
|
ZVAL_STRINGL(*rval, str, len, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-09-04 00:24:15 +08:00
|
|
|
"a:" uiv ":" "{" {
|
2005-03-10 08:10:21 +08:00
|
|
|
long elements = parse_iv(start + 2);
|
|
|
|
/* use iv() not uiv() in order to check data range */
|
2005-03-01 00:32:49 +08:00
|
|
|
*p = YYCURSOR;
|
|
|
|
|
2005-03-01 00:22:39 +08:00
|
|
|
if (elements < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
INIT_PZVAL(*rval);
|
|
|
|
|
2008-05-27 18:29:33 +08:00
|
|
|
array_init_size(*rval, elements);
|
2001-11-11 05:18:34 +08:00
|
|
|
|
2011-09-28 22:47:42 +08:00
|
|
|
if (!process_nested_data(UNSERIALIZE_PASSTHRU, Z_ARRVAL_PP(rval), elements, 0)) {
|
2001-11-11 05:18:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return finish_nested_data(UNSERIALIZE_PASSTHRU);
|
|
|
|
}
|
|
|
|
|
|
|
|
"o:" iv ":" ["] {
|
|
|
|
|
|
|
|
INIT_PZVAL(*rval);
|
|
|
|
|
|
|
|
return object_common2(UNSERIALIZE_PASSTHRU,
|
2002-03-13 04:18:01 +08:00
|
|
|
object_common1(UNSERIALIZE_PASSTHRU, ZEND_STANDARD_CLASS_DEF_PTR));
|
2001-11-11 05:18:34 +08:00
|
|
|
}
|
|
|
|
|
2005-02-23 19:17:52 +08:00
|
|
|
object ":" uiv ":" ["] {
|
2005-02-15 04:58:22 +08:00
|
|
|
size_t len, len2, len3, maxlen;
|
2005-03-10 08:10:21 +08:00
|
|
|
long elements;
|
2001-11-11 05:18:34 +08:00
|
|
|
char *class_name;
|
|
|
|
zend_class_entry *ce;
|
2003-05-25 01:10:03 +08:00
|
|
|
zend_class_entry **pce;
|
2001-11-11 05:18:34 +08:00
|
|
|
int incomplete_class = 0;
|
2005-02-23 19:17:52 +08:00
|
|
|
|
|
|
|
int custom_object = 0;
|
|
|
|
|
2001-11-27 06:33:44 +08:00
|
|
|
zval *user_func;
|
|
|
|
zval *retval_ptr;
|
|
|
|
zval **args[1];
|
|
|
|
zval *arg_func_name;
|
2005-02-23 19:17:52 +08:00
|
|
|
|
2007-08-07 02:23:16 +08:00
|
|
|
if (*start == 'C') {
|
2005-02-23 19:17:52 +08:00
|
|
|
custom_object = 1;
|
|
|
|
}
|
2001-11-27 06:33:44 +08:00
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
INIT_PZVAL(*rval);
|
2004-09-06 01:16:02 +08:00
|
|
|
len2 = len = parse_uiv(start + 2);
|
|
|
|
maxlen = max - YYCURSOR;
|
|
|
|
if (maxlen < len || len == 0) {
|
|
|
|
*p = start + 2;
|
2001-11-11 05:18:34 +08:00
|
|
|
return 0;
|
2004-09-06 01:16:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class_name = (char*)YYCURSOR;
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
YYCURSOR += len;
|
|
|
|
|
2004-09-06 01:16:02 +08:00
|
|
|
if (*(YYCURSOR) != '"') {
|
|
|
|
*p = YYCURSOR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (*(YYCURSOR+1) != ':') {
|
|
|
|
*p = YYCURSOR+1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-04 23:58:55 +08:00
|
|
|
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\\");
|
2005-02-15 04:58:22 +08:00
|
|
|
if (len3 != len)
|
|
|
|
{
|
|
|
|
*p = YYCURSOR + len3 - len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-06 01:16:02 +08:00
|
|
|
class_name = estrndup(class_name, len);
|
|
|
|
|
2003-05-25 01:10:03 +08:00
|
|
|
do {
|
|
|
|
/* Try to find class directly */
|
2013-01-21 11:35:22 +08:00
|
|
|
BG(serialize_lock) = 1;
|
2003-06-02 23:53:09 +08:00
|
|
|
if (zend_lookup_class(class_name, len2, &pce TSRMLS_CC) == SUCCESS) {
|
2013-01-21 11:35:22 +08:00
|
|
|
BG(serialize_lock) = 0;
|
2003-05-25 01:10:03 +08:00
|
|
|
ce = *pce;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-21 11:35:22 +08:00
|
|
|
BG(serialize_lock) = 0;
|
2003-05-25 01:10:03 +08:00
|
|
|
|
|
|
|
/* Check for unserialize callback */
|
2001-12-03 01:31:11 +08:00
|
|
|
if ((PG(unserialize_callback_func) == NULL) || (PG(unserialize_callback_func)[0] == '\0')) {
|
2001-11-27 06:33:44 +08:00
|
|
|
incomplete_class = 1;
|
|
|
|
ce = PHP_IC_ENTRY;
|
2003-05-25 01:10:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call unserialize callback */
|
|
|
|
MAKE_STD_ZVAL(user_func);
|
|
|
|
ZVAL_STRING(user_func, PG(unserialize_callback_func), 1);
|
|
|
|
args[0] = &arg_func_name;
|
|
|
|
MAKE_STD_ZVAL(arg_func_name);
|
|
|
|
ZVAL_STRING(arg_func_name, class_name, 1);
|
2013-01-21 11:35:22 +08:00
|
|
|
BG(serialize_lock) = 1;
|
2003-05-25 01:10:03 +08:00
|
|
|
if (call_user_function_ex(CG(function_table), NULL, user_func, &retval_ptr, 1, args, 0, NULL TSRMLS_CC) != SUCCESS) {
|
2013-01-21 11:35:22 +08:00
|
|
|
BG(serialize_lock) = 0;
|
2003-05-25 01:10:03 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "defined (%s) but not found", user_func->value.str.val);
|
|
|
|
incomplete_class = 1;
|
|
|
|
ce = PHP_IC_ENTRY;
|
|
|
|
zval_ptr_dtor(&user_func);
|
|
|
|
zval_ptr_dtor(&arg_func_name);
|
|
|
|
break;
|
|
|
|
}
|
2013-01-21 11:35:22 +08:00
|
|
|
BG(serialize_lock) = 0;
|
2003-05-25 01:10:03 +08:00
|
|
|
if (retval_ptr) {
|
|
|
|
zval_ptr_dtor(&retval_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The callback function may have defined the class */
|
2003-06-02 23:53:09 +08:00
|
|
|
if (zend_lookup_class(class_name, len2, &pce TSRMLS_CC) == SUCCESS) {
|
2003-05-25 01:10:03 +08:00
|
|
|
ce = *pce;
|
2001-11-27 06:33:44 +08:00
|
|
|
} else {
|
2003-05-25 01:10:03 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Function %s() hasn't defined the class it was called for", user_func->value.str.val);
|
|
|
|
incomplete_class = 1;
|
|
|
|
ce = PHP_IC_ENTRY;
|
2001-11-27 06:33:44 +08:00
|
|
|
}
|
2001-11-11 05:18:34 +08:00
|
|
|
|
2003-05-25 01:10:03 +08:00
|
|
|
zval_ptr_dtor(&user_func);
|
|
|
|
zval_ptr_dtor(&arg_func_name);
|
|
|
|
break;
|
|
|
|
} while (1);
|
2005-02-23 19:17:52 +08:00
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
*p = YYCURSOR;
|
2005-02-23 19:17:52 +08:00
|
|
|
|
2007-08-07 02:23:16 +08:00
|
|
|
if (custom_object) {
|
2008-08-29 22:59:20 +08:00
|
|
|
int ret = object_custom(UNSERIALIZE_PASSTHRU, ce);
|
|
|
|
|
|
|
|
if (ret && incomplete_class) {
|
|
|
|
php_store_class_name(*rval, class_name, len2);
|
|
|
|
}
|
2005-02-23 19:17:52 +08:00
|
|
|
efree(class_name);
|
2008-08-29 22:59:20 +08:00
|
|
|
return ret;
|
2005-02-23 19:17:52 +08:00
|
|
|
}
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
elements = object_common1(UNSERIALIZE_PASSTHRU, ce);
|
|
|
|
|
|
|
|
if (incomplete_class) {
|
|
|
|
php_store_class_name(*rval, class_name, len2);
|
|
|
|
}
|
2003-05-25 01:10:03 +08:00
|
|
|
efree(class_name);
|
2001-11-11 05:18:34 +08:00
|
|
|
|
|
|
|
return object_common2(UNSERIALIZE_PASSTHRU, elements);
|
|
|
|
}
|
|
|
|
|
2002-04-29 00:56:33 +08:00
|
|
|
"}" {
|
|
|
|
/* this is the case where we have less data than planned */
|
2003-01-25 00:29:40 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unexpected end of serialized data");
|
2002-04-29 00:56:33 +08:00
|
|
|
return 0; /* not sure if it should be 0 or 1 here? */
|
|
|
|
}
|
|
|
|
|
2001-11-11 05:18:34 +08:00
|
|
|
any { return 0; }
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|