php-src/ext/wddx/wddx.c

1345 lines
33 KiB
C
Raw Normal View History

1999-07-20 04:24:03 +08:00
/*
+----------------------------------------------------------------------+
2014-09-20 00:33:14 +08:00
| PHP Version 7 |
1999-07-20 04:24:03 +08:00
+----------------------------------------------------------------------+
2018-01-02 12:55:14 +08:00
| Copyright (c) 1997-2018 The PHP Group |
1999-07-20 04:24:03 +08:00
+----------------------------------------------------------------------+
2006-01-01 20:51:34 +08:00
| This source file is subject to version 3.01 of the PHP license, |
2001-06-20 05:25:53 +08:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 20:51:34 +08:00
| http://www.php.net/license/3_01.txt |
2001-06-20 05:25:53 +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. |
1999-07-20 04:24:03 +08:00
+----------------------------------------------------------------------+
2002-05-14 01:28:38 +08:00
| Author: Andrei Zmievski <andrei@php.net> |
1999-07-20 04:24:03 +08:00
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-07-20 04:24:03 +08:00
#include "php.h"
#if HAVE_WDDX
2003-05-20 07:45:20 +08:00
#include "ext/xml/expat_compat.h"
#include "php_wddx.h"
#include "php_wddx_api.h"
#define PHP_XML_INTERNAL
#include "ext/xml/php_xml.h"
2000-06-24 01:00:47 +08:00
#include "ext/standard/php_incomplete_class.h"
2000-08-08 01:25:57 +08:00
#include "ext/standard/base64.h"
#include "ext/standard/info.h"
#include "zend_smart_str.h"
#include "ext/standard/html.h"
#include "ext/standard/php_string.h"
#include "ext/date/php_date.h"
2006-01-28 14:15:25 +08:00
#include "zend_globals.h"
1999-07-20 04:24:03 +08:00
#define WDDX_BUF_LEN 256
#define PHP_CLASS_NAME_VAR "php_class_name"
1999-07-20 04:24:03 +08:00
#define EL_ARRAY "array"
2000-08-08 01:25:57 +08:00
#define EL_BINARY "binary"
#define EL_BOOLEAN "boolean"
1999-07-20 04:24:03 +08:00
#define EL_CHAR "char"
#define EL_CHAR_CODE "code"
#define EL_NULL "null"
1999-07-20 04:24:03 +08:00
#define EL_NUMBER "number"
#define EL_PACKET "wddxPacket"
#define EL_STRING "string"
1999-07-20 04:24:03 +08:00
#define EL_STRUCT "struct"
#define EL_VALUE "value"
1999-07-20 04:24:03 +08:00
#define EL_VAR "var"
#define EL_NAME "name"
1999-07-20 04:24:03 +08:00
#define EL_VERSION "version"
#define EL_RECORDSET "recordset"
#define EL_FIELD "field"
#define EL_DATETIME "dateTime"
1999-07-20 04:24:03 +08:00
#define php_wddx_deserialize(a,b) \
php_wddx_deserialize_ex(Z_STRVAL_P(a), Z_STRLEN_P(a), (b))
#define SET_STACK_VARNAME \
if (stack->varname) { \
ent.varname = estrdup(stack->varname); \
efree(stack->varname); \
stack->varname = NULL; \
} else \
ent.varname = NULL; \
2015-01-03 17:22:58 +08:00
1999-07-20 04:24:03 +08:00
static int le_wddx;
typedef struct {
zval data;
1999-07-20 04:24:03 +08:00
enum {
ST_ARRAY,
ST_BOOLEAN,
ST_NULL,
ST_NUMBER,
ST_STRING,
2000-08-08 01:25:57 +08:00
ST_BINARY,
ST_STRUCT,
ST_RECORDSET,
ST_FIELD,
ST_DATETIME
1999-07-20 04:24:03 +08:00
} type;
char *varname;
} st_entry;
typedef struct {
int top, max;
char *varname;
zend_bool done;
1999-07-20 04:24:03 +08:00
void **elements;
} wddx_stack;
2003-10-20 23:41:53 +08:00
static void php_wddx_process_data(void *user_data, const XML_Char *s, int len);
1999-07-20 04:24:03 +08:00
2008-06-27 22:33:12 +08:00
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_wddx_serialize_value, 0, 0, 1)
ZEND_ARG_INFO(0, var)
ZEND_ARG_INFO(0, comment)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wddx_serialize_vars, 0, 0, 1)
ZEND_ARG_VARIADIC_INFO(0, var_names)
2008-06-27 22:33:12 +08:00
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wddx_serialize_start, 0, 0, 0)
ZEND_ARG_INFO(0, comment)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wddx_packet_end, 0, 0, 1)
ZEND_ARG_INFO(0, packet_id)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wddx_add_vars, 0, 0, 2)
ZEND_ARG_INFO(0, packet_id)
ZEND_ARG_VARIADIC_INFO(0, var_names)
2008-06-27 22:33:12 +08:00
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wddx_deserialize, 0, 0, 1)
ZEND_ARG_INFO(0, packet)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ wddx_functions[]
*/
const zend_function_entry wddx_functions[] = {
2008-06-27 22:33:12 +08:00
PHP_FE(wddx_serialize_value, arginfo_wddx_serialize_value)
PHP_FE(wddx_serialize_vars, arginfo_wddx_serialize_vars)
PHP_FE(wddx_packet_start, arginfo_wddx_serialize_start)
PHP_FE(wddx_packet_end, arginfo_wddx_packet_end)
PHP_FE(wddx_add_vars, arginfo_wddx_add_vars)
PHP_FE(wddx_deserialize, arginfo_wddx_deserialize)
2011-07-25 19:35:02 +08:00
PHP_FE_END
1999-07-20 04:24:03 +08:00
};
/* }}} */
1999-07-20 04:24:03 +08:00
2000-09-14 04:58:19 +08:00
PHP_MINIT_FUNCTION(wddx);
PHP_MINFO_FUNCTION(wddx);
2002-07-18 03:27:36 +08:00
/* {{{ dynamically loadable module stuff */
#ifdef COMPILE_DL_WDDX
ZEND_GET_MODULE(wddx)
#endif /* COMPILE_DL_WDDX */
/* }}} */
/* {{{ wddx_module_entry
*/
1999-07-20 04:24:03 +08:00
zend_module_entry wddx_module_entry = {
STANDARD_MODULE_HEADER,
2000-09-14 04:58:19 +08:00
"wddx",
wddx_functions,
PHP_MINIT(wddx),
NULL,
NULL,
NULL,
PHP_MINFO(wddx),
PHP_WDDX_VERSION,
2000-09-14 04:58:19 +08:00
STANDARD_MODULE_PROPERTIES
1999-07-20 04:24:03 +08:00
};
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ wddx_stack_init
2015-01-03 17:22:58 +08:00
*/
1999-07-20 04:24:03 +08:00
static int wddx_stack_init(wddx_stack *stack)
{
stack->top = 0;
2003-08-12 08:58:52 +08:00
stack->elements = (void **) safe_emalloc(sizeof(void **), STACK_BLOCK_SIZE, 0);
stack->max = STACK_BLOCK_SIZE;
stack->varname = NULL;
stack->done = 0;
return SUCCESS;
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ wddx_stack_push
*/
1999-07-20 04:24:03 +08:00
static int wddx_stack_push(wddx_stack *stack, void *element, int size)
{
if (stack->top >= stack->max) { /* we need to allocate more memory */
stack->elements = (void **) erealloc(stack->elements,
(sizeof(void **) * (stack->max += STACK_BLOCK_SIZE)));
}
stack->elements[stack->top] = (void *) emalloc(size);
memcpy(stack->elements[stack->top], element, size);
return stack->top++;
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ wddx_stack_top
*/
1999-07-20 04:24:03 +08:00
static int wddx_stack_top(wddx_stack *stack, void **element)
{
if (stack->top > 0) {
*element = stack->elements[stack->top - 1];
return SUCCESS;
} else {
*element = NULL;
return FAILURE;
}
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ wddx_stack_is_empty
*/
1999-07-20 04:24:03 +08:00
static int wddx_stack_is_empty(wddx_stack *stack)
{
if (stack->top == 0) {
return 1;
} else {
return 0;
}
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ wddx_stack_destroy
*/
1999-07-20 04:24:03 +08:00
static int wddx_stack_destroy(wddx_stack *stack)
{
register int i;
if (stack->elements) {
for (i = 0; i < stack->top; i++) {
if (Z_TYPE(((st_entry *)stack->elements[i])->data) != IS_UNDEF
&& ((st_entry *)stack->elements[i])->type != ST_FIELD) {
zval_ptr_dtor(&((st_entry *)stack->elements[i])->data);
}
2008-08-24 12:02:49 +08:00
if (((st_entry *)stack->elements[i])->varname) {
efree(((st_entry *)stack->elements[i])->varname);
2008-08-24 12:02:49 +08:00
}
1999-07-20 04:24:03 +08:00
efree(stack->elements[i]);
2015-01-03 17:22:58 +08:00
}
1999-07-20 04:24:03 +08:00
efree(stack->elements);
}
if (stack->varname) {
efree(stack->varname);
}
1999-07-20 04:24:03 +08:00
return SUCCESS;
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ release_wddx_packet_rsrc
*/
2014-12-14 06:06:14 +08:00
static void release_wddx_packet_rsrc(zend_resource *rsrc)
1999-07-20 04:24:03 +08:00
{
smart_str *str = (smart_str *)rsrc->ptr;
smart_str_free(str);
efree(str);
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
#include "ext/session/php_session.h"
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
/* {{{ PS_SERIALIZER_ENCODE_FUNC
*/
PS_SERIALIZER_ENCODE_FUNC(wddx)
{
wddx_packet *packet;
zend_string *str;
PS_ENCODE_VARS;
packet = php_wddx_constructor();
php_wddx_packet_start(packet, NULL, 0);
php_wddx_add_chunk_static(packet, WDDX_STRUCT_S);
2015-01-03 17:22:58 +08:00
PS_ENCODE_LOOP(
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, struc, key);
);
2015-01-03 17:22:58 +08:00
php_wddx_add_chunk_static(packet, WDDX_STRUCT_E);
php_wddx_packet_end(packet);
smart_str_0(packet);
2014-08-26 01:24:55 +08:00
str = zend_string_copy(packet->s);
php_wddx_destructor(packet);
return str;
}
/* }}} */
/* {{{ PS_SERIALIZER_DECODE_FUNC
*/
PS_SERIALIZER_DECODE_FUNC(wddx)
{
zval retval;
zval *ent;
zend_string *key;
2014-09-12 22:37:55 +08:00
zend_ulong idx;
int ret;
2008-08-24 12:02:49 +08:00
if (vallen == 0) {
return SUCCESS;
2008-08-24 12:02:49 +08:00
}
2015-01-03 17:22:58 +08:00
ZVAL_UNDEF(&retval);
if ((ret = php_wddx_deserialize_ex(val, vallen, &retval)) == SUCCESS) {
if (Z_TYPE(retval) != IS_ARRAY) {
zval_dtor(&retval);
return FAILURE;
}
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(retval), idx, key, ent) {
if (key == NULL) {
2014-08-26 04:47:04 +08:00
key = zend_long_to_str(idx);
} else {
2014-08-26 01:24:55 +08:00
zend_string_addref(key);
}
2014-12-14 06:06:14 +08:00
if (php_set_session_var(key, ent, NULL)) {
if (Z_REFCOUNTED_P(ent)) Z_ADDREF_P(ent);
}
PS_ADD_VAR(key);
2014-08-26 01:24:55 +08:00
zend_string_release(key);
} ZEND_HASH_FOREACH_END();
}
zval_ptr_dtor(&retval);
return ret;
}
/* }}} */
2001-10-27 14:26:02 +08:00
#endif
1999-07-20 04:24:03 +08:00
/* {{{ PHP_MINIT_FUNCTION
*/
2000-09-14 04:58:19 +08:00
PHP_MINIT_FUNCTION(wddx)
1999-07-20 04:24:03 +08:00
{
le_wddx = zend_register_list_destructors_ex(release_wddx_packet_rsrc, NULL, "wddx", module_number);
2001-10-27 14:26:02 +08:00
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
php_session_register_serializer("wddx",
PS_SERIALIZER_ENCODE_NAME(wddx),
PS_SERIALIZER_DECODE_NAME(wddx));
2015-01-03 17:22:58 +08:00
#endif
2001-10-27 14:26:02 +08:00
1999-07-20 04:24:03 +08:00
return SUCCESS;
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ PHP_MINFO_FUNCTION
*/
2000-09-14 04:58:19 +08:00
PHP_MINFO_FUNCTION(wddx)
{
php_info_print_table_start();
#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
2002-02-24 15:54:04 +08:00
php_info_print_table_header(2, "WDDX Support", "enabled" );
2001-10-27 14:26:02 +08:00
php_info_print_table_row(2, "WDDX Session Serializer", "enabled" );
2002-02-24 15:54:04 +08:00
#else
php_info_print_table_row(2, "WDDX Support", "enabled" );
2001-10-27 14:26:02 +08:00
#endif
2000-09-14 04:58:19 +08:00
php_info_print_table_end();
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_packet_start
*/
void php_wddx_packet_start(wddx_packet *packet, char *comment, size_t comment_len)
1999-07-20 04:24:03 +08:00
{
php_wddx_add_chunk_static(packet, WDDX_PACKET_S);
if (comment) {
zend_string *escaped = php_escape_html_entities(
comment, comment_len, 0, ENT_QUOTES, NULL);
2016-07-30 21:10:54 +08:00
php_wddx_add_chunk_static(packet, WDDX_HEADER_S);
php_wddx_add_chunk_static(packet, WDDX_COMMENT_S);
php_wddx_add_chunk_ex(packet, ZSTR_VAL(escaped), ZSTR_LEN(escaped));
php_wddx_add_chunk_static(packet, WDDX_COMMENT_E);
php_wddx_add_chunk_static(packet, WDDX_HEADER_E);
2016-07-30 21:10:54 +08:00
zend_string_release(escaped);
2008-08-24 12:02:49 +08:00
} else {
php_wddx_add_chunk_static(packet, WDDX_HEADER);
2008-08-24 12:02:49 +08:00
}
php_wddx_add_chunk_static(packet, WDDX_DATA_S);
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_packet_end
*/
void php_wddx_packet_end(wddx_packet *packet)
1999-07-20 04:24:03 +08:00
{
php_wddx_add_chunk_static(packet, WDDX_DATA_E);
2015-01-03 17:22:58 +08:00
php_wddx_add_chunk_static(packet, WDDX_PACKET_E);
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
#define FLUSH_BUF() \
if (l > 0) { \
php_wddx_add_chunk_ex(packet, buf, l); \
l = 0; \
}
/* {{{ php_wddx_serialize_string
*/
2014-12-14 06:06:14 +08:00
static void php_wddx_serialize_string(wddx_packet *packet, zval *var)
1999-07-20 04:24:03 +08:00
{
php_wddx_add_chunk_static(packet, WDDX_STRING_S);
1999-07-20 04:24:03 +08:00
2014-08-26 01:24:55 +08:00
if (Z_STRLEN_P(var) > 0) {
zend_string *buf = php_escape_html_entities(
(unsigned char *) Z_STRVAL_P(var), Z_STRLEN_P(var), 0, ENT_QUOTES, NULL);
php_wddx_add_chunk_ex(packet, ZSTR_VAL(buf), ZSTR_LEN(buf));
2014-08-26 01:24:55 +08:00
zend_string_release(buf);
1999-07-20 04:24:03 +08:00
}
php_wddx_add_chunk_static(packet, WDDX_STRING_E);
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_serialize_number
*/
static void php_wddx_serialize_number(wddx_packet *packet, zval *var)
1999-07-20 04:24:03 +08:00
{
char tmp_buf[WDDX_BUF_LEN], *dec_point;
zend_string *str = zval_get_string(var);
snprintf(tmp_buf, sizeof(tmp_buf), WDDX_NUMBER, ZSTR_VAL(str));
zend_string_release(str);
dec_point = strchr(tmp_buf, ',');
if (dec_point) {
*dec_point = '.';
}
php_wddx_add_chunk(packet, tmp_buf);
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_serialize_boolean
*/
static void php_wddx_serialize_boolean(wddx_packet *packet, zval *var)
{
php_wddx_add_chunk(packet, Z_TYPE_P(var) == IS_TRUE ? WDDX_BOOLEAN_TRUE : WDDX_BOOLEAN_FALSE);
}
/* }}} */
/* {{{ php_wddx_serialize_unset
*/
static void php_wddx_serialize_unset(wddx_packet *packet)
{
php_wddx_add_chunk_static(packet, WDDX_NULL);
}
/* }}} */
/* {{{ php_wddx_serialize_object
*/
static void php_wddx_serialize_object(wddx_packet *packet, zval *obj)
1999-07-20 04:24:03 +08:00
{
2001-07-29 03:11:05 +08:00
/* OBJECTS_FIXME */
zval *ent, fname, *varname;
zval retval;
zend_string *key;
2014-09-12 22:37:55 +08:00
zend_ulong idx;
char tmp_buf[WDDX_BUF_LEN];
HashTable *objhash, *sleephash;
zend_class_entry *ce;
PHP_CLASS_ATTRIBUTES;
PHP_SET_CLASS_ATTRIBUTES(obj);
ce = Z_OBJCE_P(obj);
if (!ce || ce->serialize || ce->unserialize) {
php_error_docref(NULL, E_WARNING, "Class %s can not be serialized", ZSTR_VAL(class_name));
PHP_CLEANUP_CLASS_ATTRIBUTES();
return;
}
1999-07-20 04:24:03 +08:00
ZVAL_STRING(&fname, "__sleep");
/*
* We try to call __sleep() method on object. It's supposed to return an
* array of property names to be serialized.
*/
2014-12-14 06:06:14 +08:00
if (call_user_function_ex(CG(function_table), obj, &fname, &retval, 0, 0, 1, NULL) == SUCCESS) {
if (!Z_ISUNDEF(retval) && (sleephash = HASH_OF(&retval))) {
php_wddx_add_chunk_static(packet, WDDX_STRUCT_S);
2007-01-19 00:21:32 +08:00
snprintf(tmp_buf, WDDX_BUF_LEN, WDDX_VAR_S, PHP_CLASS_NAME_VAR);
php_wddx_add_chunk(packet, tmp_buf);
php_wddx_add_chunk_static(packet, WDDX_STRING_S);
php_wddx_add_chunk_ex(packet, ZSTR_VAL(class_name), ZSTR_LEN(class_name));
php_wddx_add_chunk_static(packet, WDDX_STRING_E);
php_wddx_add_chunk_static(packet, WDDX_VAR_E);
2000-06-24 01:00:47 +08:00
objhash = Z_OBJPROP_P(obj);
2015-01-03 17:22:58 +08:00
ZEND_HASH_FOREACH_VAL(sleephash, varname) {
if (Z_TYPE_P(varname) != IS_STRING) {
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_NOTICE, "__sleep should return an array only containing the names of instance-variables to serialize.");
continue;
}
if ((ent = zend_hash_find(objhash, Z_STR_P(varname))) != NULL) {
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, ent, Z_STR_P(varname));
}
} ZEND_HASH_FOREACH_END();
2015-01-03 17:22:58 +08:00
php_wddx_add_chunk_static(packet, WDDX_STRUCT_E);
}
} else {
php_wddx_add_chunk_static(packet, WDDX_STRUCT_S);
2007-01-19 00:21:32 +08:00
snprintf(tmp_buf, WDDX_BUF_LEN, WDDX_VAR_S, PHP_CLASS_NAME_VAR);
php_wddx_add_chunk(packet, tmp_buf);
php_wddx_add_chunk_static(packet, WDDX_STRING_S);
php_wddx_add_chunk_ex(packet, ZSTR_VAL(class_name), ZSTR_LEN(class_name));
php_wddx_add_chunk_static(packet, WDDX_STRING_E);
php_wddx_add_chunk_static(packet, WDDX_VAR_E);
2000-06-24 01:00:47 +08:00
objhash = Z_OBJPROP_P(obj);
ZEND_HASH_FOREACH_KEY_VAL(objhash, idx, key, ent) {
if (ent == obj) {
continue;
2008-08-24 12:02:49 +08:00
}
if (key) {
const char *class_name, *prop_name;
size_t prop_name_len;
zend_string *tmp;
2015-01-03 17:22:58 +08:00
zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len);
tmp = zend_string_init(prop_name, prop_name_len, 0);
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, ent, tmp);
2014-08-26 01:24:55 +08:00
zend_string_release(tmp);
} else {
2014-08-26 04:47:04 +08:00
key = zend_long_to_str(idx);
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, ent, key);
2014-08-26 01:24:55 +08:00
zend_string_release(key);
}
} ZEND_HASH_FOREACH_END();
php_wddx_add_chunk_static(packet, WDDX_STRUCT_E);
}
PHP_CLEANUP_CLASS_ATTRIBUTES();
zval_ptr_dtor(&fname);
zval_ptr_dtor(&retval);
}
/* }}} */
/* {{{ php_wddx_serialize_array
*/
static void php_wddx_serialize_array(wddx_packet *packet, zval *arr)
{
zval *ent;
zend_string *key;
int is_struct = 0;
2014-09-12 22:37:55 +08:00
zend_ulong idx;
HashTable *target_hash;
char tmp_buf[WDDX_BUF_LEN];
2014-09-12 22:37:55 +08:00
zend_ulong ind = 0;
target_hash = Z_ARRVAL_P(arr);
ZEND_HASH_FOREACH_KEY(target_hash, idx, key) {
if (key) {
is_struct = 1;
break;
}
if (idx != ind) {
is_struct = 1;
break;
}
ind++;
} ZEND_HASH_FOREACH_END();
if (is_struct) {
php_wddx_add_chunk_static(packet, WDDX_STRUCT_S);
} else {
snprintf(tmp_buf, sizeof(tmp_buf), WDDX_ARRAY_S, zend_hash_num_elements(target_hash));
php_wddx_add_chunk(packet, tmp_buf);
}
ZEND_HASH_FOREACH_KEY_VAL(target_hash, idx, key, ent) {
if (ent == arr) {
continue;
2008-08-24 12:02:49 +08:00
}
if (is_struct) {
if (key) {
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, ent, key);
} else {
2014-08-26 04:47:04 +08:00
key = zend_long_to_str(idx);
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, ent, key);
2014-08-26 01:24:55 +08:00
zend_string_release(key);
}
2008-08-24 12:02:49 +08:00
} else {
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, ent, NULL);
2008-08-24 12:02:49 +08:00
}
} ZEND_HASH_FOREACH_END();
2015-01-03 17:22:58 +08:00
if (is_struct) {
php_wddx_add_chunk_static(packet, WDDX_STRUCT_E);
} else {
php_wddx_add_chunk_static(packet, WDDX_ARRAY_E);
}
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_serialize_var
*/
2014-12-14 06:06:14 +08:00
void php_wddx_serialize_var(wddx_packet *packet, zval *var, zend_string *name)
1999-07-20 04:24:03 +08:00
{
HashTable *ht;
1999-07-20 04:24:03 +08:00
if (name) {
char *tmp_buf;
zend_string *name_esc = php_escape_html_entities((unsigned char *) ZSTR_VAL(name), ZSTR_LEN(name), 0, ENT_QUOTES, NULL);
tmp_buf = emalloc(ZSTR_LEN(name_esc) + sizeof(WDDX_VAR_S));
snprintf(tmp_buf, ZSTR_LEN(name_esc) + sizeof(WDDX_VAR_S), WDDX_VAR_S, ZSTR_VAL(name_esc));
php_wddx_add_chunk(packet, tmp_buf);
efree(tmp_buf);
2014-08-26 01:24:55 +08:00
zend_string_release(name_esc);
1999-07-20 04:24:03 +08:00
}
2015-01-03 17:22:58 +08:00
if (Z_TYPE_P(var) == IS_INDIRECT) {
var = Z_INDIRECT_P(var);
}
ZVAL_DEREF(var);
switch (Z_TYPE_P(var)) {
1999-07-20 04:24:03 +08:00
case IS_STRING:
2014-12-14 06:06:14 +08:00
php_wddx_serialize_string(packet, var);
1999-07-20 04:24:03 +08:00
break;
2015-01-03 17:22:58 +08:00
2014-08-26 01:24:55 +08:00
case IS_LONG:
1999-07-20 04:24:03 +08:00
case IS_DOUBLE:
php_wddx_serialize_number(packet, var);
break;
case IS_TRUE:
case IS_FALSE:
php_wddx_serialize_boolean(packet, var);
break;
2000-01-04 22:41:56 +08:00
case IS_NULL:
php_wddx_serialize_unset(packet);
1999-07-20 04:24:03 +08:00
break;
2015-01-03 17:22:58 +08:00
1999-07-20 04:24:03 +08:00
case IS_ARRAY:
ht = Z_ARRVAL_P(var);
if (ht->u.v.nApplyCount > 1) {
2016-06-15 00:32:33 +08:00
zend_throw_error(NULL, "WDDX doesn't support circular references");
return;
}
if (ZEND_HASH_APPLY_PROTECTION(ht)) {
ht->u.v.nApplyCount++;
}
php_wddx_serialize_array(packet, var);
if (ZEND_HASH_APPLY_PROTECTION(ht)) {
ht->u.v.nApplyCount--;
}
break;
1999-07-20 04:24:03 +08:00
case IS_OBJECT:
ht = Z_OBJPROP_P(var);
if (ht->u.v.nApplyCount > 1) {
2016-06-15 00:32:33 +08:00
zend_throw_error(NULL, "WDDX doesn't support circular references");
return;
}
ht->u.v.nApplyCount++;
php_wddx_serialize_object(packet, var);
ht->u.v.nApplyCount--;
1999-07-20 04:24:03 +08:00
break;
}
2015-01-03 17:22:58 +08:00
1999-07-20 04:24:03 +08:00
if (name) {
php_wddx_add_chunk_static(packet, WDDX_VAR_E);
1999-07-20 04:24:03 +08:00
}
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_add_var
*/
static void php_wddx_add_var(wddx_packet *packet, zval *name_var)
1999-07-20 04:24:03 +08:00
{
zval *val;
HashTable *target_hash;
2008-08-24 12:02:49 +08:00
if (Z_TYPE_P(name_var) == IS_STRING) {
2014-12-14 06:06:14 +08:00
zend_array *symbol_table = zend_rebuild_symbol_table();
if ((val = zend_hash_find(symbol_table, Z_STR_P(name_var))) != NULL) {
2014-05-12 10:52:37 +08:00
if (Z_TYPE_P(val) == IS_INDIRECT) {
val = Z_INDIRECT_P(val);
}
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, val, Z_STR_P(name_var));
2015-01-03 17:22:58 +08:00
}
2008-08-24 12:02:49 +08:00
} else if (Z_TYPE_P(name_var) == IS_ARRAY || Z_TYPE_P(name_var) == IS_OBJECT) {
int is_array = Z_TYPE_P(name_var) == IS_ARRAY;
2015-01-03 17:22:58 +08:00
target_hash = HASH_OF(name_var);
2015-01-03 17:22:58 +08:00
if (is_array && target_hash->u.v.nApplyCount > 1) {
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_WARNING, "recursion detected");
return;
}
if (!Z_REFCOUNTED_P(name_var)) {
ZEND_HASH_FOREACH_VAL(target_hash, val) {
php_wddx_add_var(packet, val);
} ZEND_HASH_FOREACH_END();
} else {
ZEND_HASH_FOREACH_VAL(target_hash, val) {
if (is_array) {
target_hash->u.v.nApplyCount++;
}
ZVAL_DEREF(val);
php_wddx_add_var(packet, val);
if (is_array) {
target_hash->u.v.nApplyCount--;
}
} ZEND_HASH_FOREACH_END();
}
1999-07-20 04:24:03 +08:00
}
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_push_element
*/
2003-10-20 23:41:53 +08:00
static void php_wddx_push_element(void *user_data, const XML_Char *name, const XML_Char **atts)
1999-07-20 04:24:03 +08:00
{
st_entry ent;
wddx_stack *stack = (wddx_stack *)user_data;
if (!strcmp((char *)name, EL_PACKET)) {
1999-07-20 04:24:03 +08:00
int i;
2015-01-03 17:22:58 +08:00
2006-05-25 17:59:25 +08:00
if (atts) for (i=0; atts[i]; i++) {
if (!strcmp((char *)atts[i], EL_VERSION)) {
/* nothing for now */
1999-07-20 04:24:03 +08:00
}
}
} else if (!strcmp((char *)name, EL_STRING)) {
2001-09-28 03:43:06 +08:00
ent.type = ST_STRING;
SET_STACK_VARNAME;
2015-01-03 17:22:58 +08:00
ZVAL_STR(&ent.data, ZSTR_EMPTY_ALLOC());
2000-08-08 01:25:57 +08:00
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp((char *)name, EL_BINARY)) {
2001-09-28 03:43:06 +08:00
ent.type = ST_BINARY;
2000-08-08 01:25:57 +08:00
SET_STACK_VARNAME;
2015-01-03 17:22:58 +08:00
ZVAL_STR(&ent.data, ZSTR_EMPTY_ALLOC());
1999-07-20 04:24:03 +08:00
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp((char *)name, EL_CHAR)) {
1999-07-20 04:24:03 +08:00
int i;
2015-01-03 17:22:58 +08:00
2006-05-25 17:59:25 +08:00
if (atts) for (i = 0; atts[i]; i++) {
if (!strcmp((char *)atts[i], EL_CHAR_CODE) && atts[i+1] && atts[i+1][0]) {
char tmp_buf[2];
snprintf(tmp_buf, sizeof(tmp_buf), "%c", (char)strtol((char *)atts[i+1], NULL, 16));
php_wddx_process_data(user_data, (XML_Char *) tmp_buf, strlen(tmp_buf));
break;
1999-07-20 04:24:03 +08:00
}
}
} else if (!strcmp((char *)name, EL_NUMBER)) {
2001-09-28 03:43:06 +08:00
ent.type = ST_NUMBER;
SET_STACK_VARNAME;
2015-01-03 17:22:58 +08:00
2014-08-26 01:24:55 +08:00
ZVAL_LONG(&ent.data, 0);
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp((char *)name, EL_BOOLEAN)) {
int i;
ent.type = ST_BOOLEAN;
SET_STACK_VARNAME;
2006-05-25 17:59:25 +08:00
if (atts) for (i = 0; atts[i]; i++) {
if (!strcmp((char *)atts[i], EL_VALUE) && atts[i+1] && atts[i+1][0]) {
ZVAL_TRUE(&ent.data);
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
php_wddx_process_data(user_data, atts[i+1], strlen((char *)atts[i+1]));
break;
}
} else {
ZVAL_FALSE(&ent.data);
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
}
} else if (!strcmp((char *)name, EL_NULL)) {
2001-09-28 03:43:06 +08:00
ent.type = ST_NULL;
SET_STACK_VARNAME;
ZVAL_NULL(&ent.data);
2015-01-03 17:22:58 +08:00
1999-07-20 04:24:03 +08:00
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp((char *)name, EL_ARRAY)) {
2001-09-28 03:43:06 +08:00
ent.type = ST_ARRAY;
SET_STACK_VARNAME;
2015-01-03 17:22:58 +08:00
array_init(&ent.data);
1999-07-20 04:24:03 +08:00
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp((char *)name, EL_STRUCT)) {
2001-09-28 03:43:06 +08:00
ent.type = ST_STRUCT;
SET_STACK_VARNAME;
array_init(&ent.data);
1999-07-20 04:24:03 +08:00
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp((char *)name, EL_VAR)) {
1999-07-20 04:24:03 +08:00
int i;
2015-01-03 17:22:58 +08:00
2006-05-25 17:59:25 +08:00
if (atts) for (i = 0; atts[i]; i++) {
if (!strcmp((char *)atts[i], EL_NAME) && atts[i+1] && atts[i+1][0]) {
2016-06-21 21:21:58 +08:00
if (stack->varname) efree(stack->varname);
stack->varname = estrdup((char *)atts[i+1]);
break;
}
}
} else if (!strcmp((char *)name, EL_RECORDSET)) {
int i;
ent.type = ST_RECORDSET;
SET_STACK_VARNAME;
array_init(&ent.data);
2006-05-25 17:59:25 +08:00
if (atts) for (i = 0; atts[i]; i++) {
if (!strcmp((char *)atts[i], "fieldNames") && atts[i+1] && atts[i+1][0]) {
zval tmp;
char *key;
const char *p1, *p2, *endp;
i++;
endp = (char *)atts[i] + strlen((char *)atts[i]);
p1 = (char *)atts[i];
while ((p2 = php_memnstr(p1, ",", sizeof(",")-1, endp)) != NULL) {
key = estrndup(p1, p2 - p1);
array_init(&tmp);
2014-05-12 10:46:19 +08:00
add_assoc_zval_ex(&ent.data, key, p2 - p1, &tmp);
p1 = p2 + sizeof(",")-1;
efree(key);
}
if (p1 <= endp) {
array_init(&tmp);
2014-05-12 10:46:19 +08:00
add_assoc_zval_ex(&ent.data, p1, endp - p1, &tmp);
}
break;
}
}
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp((char *)name, EL_FIELD)) {
int i;
st_entry ent;
ent.type = ST_FIELD;
ent.varname = NULL;
ZVAL_UNDEF(&ent.data);
2006-05-25 17:59:25 +08:00
if (atts) for (i = 0; atts[i]; i++) {
if (!strcmp((char *)atts[i], EL_NAME) && atts[i+1] && atts[i+1][0]) {
st_entry *recordset;
zval *field;
2015-01-03 17:22:58 +08:00
if (wddx_stack_top(stack, (void**)&recordset) == SUCCESS &&
recordset->type == ST_RECORDSET &&
(field = zend_hash_str_find(Z_ARRVAL(recordset->data), (char*)atts[i+1], strlen((char *)atts[i+1]))) != NULL) {
ZVAL_COPY_VALUE(&ent.data, field);
}
2015-01-03 17:22:58 +08:00
break;
1999-07-20 04:24:03 +08:00
}
}
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp((char *)name, EL_DATETIME)) {
ent.type = ST_DATETIME;
SET_STACK_VARNAME;
2015-01-03 17:22:58 +08:00
2014-08-26 01:24:55 +08:00
ZVAL_LONG(&ent.data, 0);
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
1999-07-20 04:24:03 +08:00
}
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_pop_element
*/
2003-10-20 23:41:53 +08:00
static void php_wddx_pop_element(void *user_data, const XML_Char *name)
1999-07-20 04:24:03 +08:00
{
st_entry *ent1, *ent2;
wddx_stack *stack = (wddx_stack *)user_data;
HashTable *target_hash;
zend_class_entry *pce;
zval obj;
2001-07-29 03:11:05 +08:00
/* OBJECTS_FIXME */
2008-08-24 12:02:49 +08:00
if (stack->top == 0) {
return;
2008-08-24 12:02:49 +08:00
}
2000-08-08 01:25:57 +08:00
if (!strcmp((char *)name, EL_STRING) || !strcmp((char *)name, EL_NUMBER) ||
!strcmp((char *)name, EL_BOOLEAN) || !strcmp((char *)name, EL_NULL) ||
!strcmp((char *)name, EL_ARRAY) || !strcmp((char *)name, EL_STRUCT) ||
!strcmp((char *)name, EL_RECORDSET) || !strcmp((char *)name, EL_BINARY) ||
!strcmp((char *)name, EL_DATETIME)) {
wddx_stack_top(stack, (void**)&ent1);
2016-03-02 16:02:33 +08:00
if (Z_TYPE(ent1->data) == IS_UNDEF) {
if (stack->top > 1) {
stack->top--;
efree(ent1);
} else {
stack->done = 1;
}
return;
}
if (!strcmp((char *)name, EL_BINARY)) {
2016-12-06 21:42:59 +08:00
zend_string *new_str = NULL;
2016-12-06 21:42:59 +08:00
if (ZSTR_EMPTY_ALLOC() != Z_STR(ent1->data)) {
new_str = php_base64_decode(
(unsigned char *)Z_STRVAL(ent1->data), Z_STRLEN(ent1->data));
}
zval_ptr_dtor(&ent1->data);
if (new_str) {
ZVAL_STR(&ent1->data, new_str);
} else {
ZVAL_EMPTY_STRING(&ent1->data);
}
2000-08-08 01:25:57 +08:00
}
/* Call __wakeup() method on the object. */
if (Z_TYPE(ent1->data) == IS_OBJECT) {
zval fname, retval;
ZVAL_STRING(&fname, "__wakeup");
2014-12-14 06:06:14 +08:00
call_user_function_ex(NULL, &ent1->data, &fname, &retval, 0, 0, 0, NULL);
zval_ptr_dtor(&fname);
zval_ptr_dtor(&retval);
}
1999-07-20 04:24:03 +08:00
if (stack->top > 1) {
stack->top--;
wddx_stack_top(stack, (void**)&ent2);
2015-01-03 17:22:58 +08:00
/* if non-existent field */
Merge branch 'PHP-5.6' into PHP-7.0 * PHP-5.6: (24 commits) Update NEWS BLock test with memory leak fix tests Fix TSRM build Fix bug #72850 - integer overflow in uuencode Fixed bug #72849 - integer overflow in urlencode Fix bug #72848 - integer overflow in quoted_printable_encode caused heap corruption Fix bug #72838 - Integer overflow lead to heap corruption in sql_regcase Fix bug #72837 - integer overflow in bzdecompress caused heap corruption Fix bug #72836 - integer overflow in base64_decode caused heap corruption Fix for bug #72807 - do not produce strings with negative length Fix for bug #72790 and bug #72799 Fix bug #72730 - imagegammacorrect allows arbitrary write access Fix bug#72697 - select_colors write out-of-bounds Fixed bug #72627: Memory Leakage In exif_process_IFD_in_TIFF Fix bug #72750: wddx_deserialize null dereference Fix bug #72771: ftps:// opendir wrapper is vulnerable to protocol downgrade attack Improve fix for #72663 Fix bug #70436: Use After Free Vulnerability in unserialize() Fix bug #72749: wddx_deserialize allows illegal memory access ... Conflicts: Zend/zend_API.h ext/bz2/bz2.c ext/curl/interface.c ext/ereg/ereg.c ext/exif/exif.c ext/gd/gd.c ext/gd/tests/imagetruecolortopalette_error3.phpt ext/gd/tests/imagetruecolortopalette_error4.phpt ext/session/session.c ext/snmp/snmp.c ext/standard/base64.c ext/standard/ftp_fopen_wrapper.c ext/standard/quot_print.c ext/standard/url.c ext/standard/uuencode.c ext/standard/var.c ext/standard/var_unserializer.c ext/standard/var_unserializer.re ext/wddx/tests/bug72790.phpt ext/wddx/tests/bug72799.phpt ext/wddx/wddx.c sapi/cli/generate_mime_type_map.php
2016-08-17 15:23:51 +08:00
if (Z_ISUNDEF(ent2->data)) {
zval_ptr_dtor(&ent1->data);
efree(ent1);
return;
}
2015-01-03 17:22:58 +08:00
if (Z_TYPE(ent2->data) == IS_ARRAY || Z_TYPE(ent2->data) == IS_OBJECT) {
target_hash = HASH_OF(&ent2->data);
1999-07-20 04:24:03 +08:00
if (ent1->varname) {
if (!strcmp(ent1->varname, PHP_CLASS_NAME_VAR) &&
Z_TYPE(ent1->data) == IS_STRING && Z_STRLEN(ent1->data) &&
ent2->type == ST_STRUCT && Z_TYPE(ent2->data) == IS_ARRAY) {
2000-06-24 01:00:47 +08:00
zend_bool incomplete_class = 0;
2014-08-26 01:24:55 +08:00
zend_str_tolower(Z_STRVAL(ent1->data), Z_STRLEN(ent1->data));
zend_string_forget_hash_val(Z_STR(ent1->data));
if ((pce = zend_hash_find_ptr(EG(class_table), Z_STR(ent1->data))) == NULL) {
2000-06-24 01:00:47 +08:00
incomplete_class = 1;
pce = PHP_IC_ENTRY;
}
if (pce != PHP_IC_ENTRY && (pce->serialize || pce->unserialize)) {
2016-11-08 19:12:58 +08:00
zval_ptr_dtor(&ent2->data);
ZVAL_UNDEF(&ent2->data);
php_error_docref(NULL, E_WARNING, "Class %s can not be unserialized", Z_STRVAL(ent1->data));
} else {
/* Initialize target object */
if (object_init_ex(&obj, pce) != SUCCESS || EG(exception)) {
zval_ptr_dtor(&ent2->data);
ZVAL_UNDEF(&ent2->data);
php_error_docref(NULL, E_WARNING, "Class %s can not be instantiated", Z_STRVAL(ent1->data));
} else {
/* Merge current hashtable with object's default properties */
zend_hash_merge(Z_OBJPROP(obj),
Z_ARRVAL(ent2->data),
zval_add_ref, 0);
if (incomplete_class) {
php_store_class_name(&obj, Z_STRVAL(ent1->data), Z_STRLEN(ent1->data));
}
/* Clean up old array entry */
zval_ptr_dtor(&ent2->data);
/* Set stack entry to point to the newly created object */
ZVAL_COPY_VALUE(&ent2->data, &obj);
}
}
2015-01-03 17:22:58 +08:00
/* Clean up class name var entry */
2001-08-15 04:33:19 +08:00
zval_ptr_dtor(&ent1->data);
} else if (Z_TYPE(ent2->data) == IS_OBJECT) {
zend_update_property(Z_OBJCE(ent2->data), &ent2->data, ent1->varname, strlen(ent1->varname), &ent1->data);
if Z_REFCOUNTED(ent1->data) Z_DELREF(ent1->data);
} else {
zend_symtable_str_update(target_hash, ent1->varname, strlen(ent1->varname), &ent1->data);
}
1999-07-20 04:24:03 +08:00
efree(ent1->varname);
1999-08-19 00:43:57 +08:00
} else {
zend_hash_next_index_insert(target_hash, &ent1->data);
1999-07-20 04:24:03 +08:00
}
}
efree(ent1);
2008-08-24 12:02:49 +08:00
} else {
stack->done = 1;
2008-08-24 12:02:49 +08:00
}
} else if (!strcmp((char *)name, EL_VAR) && stack->varname) {
1999-08-19 00:43:57 +08:00
efree(stack->varname);
stack->varname = NULL;
} else if (!strcmp((char *)name, EL_FIELD)) {
st_entry *ent;
wddx_stack_top(stack, (void **)&ent);
efree(ent);
stack->top--;
2001-07-29 03:11:05 +08:00
}
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_process_data
*/
2003-10-20 23:41:53 +08:00
static void php_wddx_process_data(void *user_data, const XML_Char *s, int len)
1999-07-20 04:24:03 +08:00
{
st_entry *ent;
wddx_stack *stack = (wddx_stack *)user_data;
if (!wddx_stack_is_empty(stack) && !stack->done) {
1999-07-20 04:24:03 +08:00
wddx_stack_top(stack, (void**)&ent);
switch (ent->type) {
2000-08-08 01:25:57 +08:00
case ST_BINARY:
2015-01-03 17:22:58 +08:00
case ST_STRING:
2014-08-26 01:24:55 +08:00
if (Z_STRLEN(ent->data) == 0) {
zval_ptr_dtor(&ent->data);
ZVAL_STRINGL(&ent->data, (char *)s, len);
2000-08-08 01:25:57 +08:00
} else {
Z_STR(ent->data) = zend_string_extend(Z_STR(ent->data), Z_STRLEN(ent->data) + len, 0);
2014-08-26 01:24:55 +08:00
memcpy(Z_STRVAL(ent->data) + Z_STRLEN(ent->data) - len, (char *)s, len);
Z_STRVAL(ent->data)[Z_STRLEN(ent->data)] = '\0';
2000-08-08 01:25:57 +08:00
}
break;
1999-07-20 04:24:03 +08:00
case ST_NUMBER:
ZVAL_STRINGL(&ent->data, (char *)s, len);
2014-12-14 06:06:14 +08:00
convert_scalar_to_number(&ent->data);
1999-07-20 04:24:03 +08:00
break;
case ST_BOOLEAN:
if (!strcmp((char *)s, "true")) {
ZVAL_TRUE(&ent->data);
} else if (!strcmp((char *)s, "false")) {
ZVAL_FALSE(&ent->data);
2008-08-24 12:02:49 +08:00
} else {
2001-08-15 04:33:19 +08:00
zval_ptr_dtor(&ent->data);
if (ent->varname) {
efree(ent->varname);
ent->varname = NULL;
}
2016-03-02 16:02:33 +08:00
ZVAL_UNDEF(&ent->data);
}
break;
case ST_DATETIME: {
2016-08-17 18:19:14 +08:00
zend_string *str;
if (Z_TYPE(ent->data) == IS_STRING) {
2016-08-17 18:19:14 +08:00
str = zend_string_safe_alloc(Z_STRLEN(ent->data), 1, len, 0);
memcpy(ZSTR_VAL(str), Z_STRVAL(ent->data), Z_STRLEN(ent->data));
memcpy(ZSTR_VAL(str) + Z_STRLEN(ent->data), s, len);
ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
zval_dtor(&ent->data);
} else {
2016-08-17 18:19:14 +08:00
str = zend_string_init((char *)s, len, 0);
}
2016-08-17 19:22:02 +08:00
ZVAL_LONG(&ent->data, php_parse_date(ZSTR_VAL(str), NULL));
/* date out of range < 1969 or > 2038 */
2014-08-26 01:24:55 +08:00
if (Z_LVAL(ent->data) == -1) {
2016-08-17 18:19:14 +08:00
ZVAL_STR_COPY(&ent->data, str);
}
2016-08-17 17:51:56 +08:00
2016-08-17 18:19:14 +08:00
zend_string_release(str);
}
break;
1999-07-20 04:24:03 +08:00
default:
break;
}
}
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_deserialize_ex
*/
2014-11-18 20:13:23 +08:00
int php_wddx_deserialize_ex(const char *value, size_t vallen, zval *return_value)
1999-07-20 04:24:03 +08:00
{
wddx_stack stack;
XML_Parser parser;
st_entry *ent;
int retval;
2015-01-03 17:22:58 +08:00
1999-07-20 04:24:03 +08:00
wddx_stack_init(&stack);
parser = XML_ParserCreate((XML_Char *) "UTF-8");
1999-07-20 04:24:03 +08:00
XML_SetUserData(parser, &stack);
XML_SetElementHandler(parser, php_wddx_push_element, php_wddx_pop_element);
XML_SetCharacterDataHandler(parser, php_wddx_process_data);
2015-01-03 17:22:58 +08:00
2014-11-18 20:13:23 +08:00
/* XXX value should be parsed in the loop to exhaust size_t */
XML_Parse(parser, (const XML_Char *) value, (int)vallen, 1);
2015-01-03 17:22:58 +08:00
1999-07-20 04:24:03 +08:00
XML_ParserFree(parser);
if (stack.top == 1) {
1999-08-26 21:25:53 +08:00
wddx_stack_top(&stack, (void**)&ent);
if (Z_ISUNDEF(ent->data)) {
2016-08-11 14:43:56 +08:00
retval = FAILURE;
} else {
Merge branch 'PHP-5.6' into PHP-7.0 * PHP-5.6: (24 commits) Update NEWS BLock test with memory leak fix tests Fix TSRM build Fix bug #72850 - integer overflow in uuencode Fixed bug #72849 - integer overflow in urlencode Fix bug #72848 - integer overflow in quoted_printable_encode caused heap corruption Fix bug #72838 - Integer overflow lead to heap corruption in sql_regcase Fix bug #72837 - integer overflow in bzdecompress caused heap corruption Fix bug #72836 - integer overflow in base64_decode caused heap corruption Fix for bug #72807 - do not produce strings with negative length Fix for bug #72790 and bug #72799 Fix bug #72730 - imagegammacorrect allows arbitrary write access Fix bug#72697 - select_colors write out-of-bounds Fixed bug #72627: Memory Leakage In exif_process_IFD_in_TIFF Fix bug #72750: wddx_deserialize null dereference Fix bug #72771: ftps:// opendir wrapper is vulnerable to protocol downgrade attack Improve fix for #72663 Fix bug #70436: Use After Free Vulnerability in unserialize() Fix bug #72749: wddx_deserialize allows illegal memory access ... Conflicts: Zend/zend_API.h ext/bz2/bz2.c ext/curl/interface.c ext/ereg/ereg.c ext/exif/exif.c ext/gd/gd.c ext/gd/tests/imagetruecolortopalette_error3.phpt ext/gd/tests/imagetruecolortopalette_error4.phpt ext/session/session.c ext/snmp/snmp.c ext/standard/base64.c ext/standard/ftp_fopen_wrapper.c ext/standard/quot_print.c ext/standard/url.c ext/standard/uuencode.c ext/standard/var.c ext/standard/var_unserializer.c ext/standard/var_unserializer.re ext/wddx/tests/bug72790.phpt ext/wddx/tests/bug72799.phpt ext/wddx/wddx.c sapi/cli/generate_mime_type_map.php
2016-08-17 15:23:51 +08:00
ZVAL_COPY(return_value, &ent->data);
2016-08-11 14:43:56 +08:00
retval = SUCCESS;
}
2008-08-24 12:02:49 +08:00
} else {
retval = FAILURE;
2008-08-24 12:02:49 +08:00
}
2015-01-03 17:22:58 +08:00
1999-07-20 04:24:03 +08:00
wddx_stack_destroy(&stack);
return retval;
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ proto string wddx_serialize_value(mixed var [, string comment])
1999-07-20 04:24:03 +08:00
Creates a new packet and serializes the given value */
PHP_FUNCTION(wddx_serialize_value)
{
zval *var;
char *comment = NULL;
2014-08-28 01:25:28 +08:00
size_t comment_len = 0;
1999-07-20 04:24:03 +08:00
wddx_packet *packet;
2015-01-03 17:22:58 +08:00
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|s", &var, &comment, &comment_len) == FAILURE) {
return;
2008-08-24 12:02:49 +08:00
}
2015-01-03 17:22:58 +08:00
packet = php_wddx_constructor();
1999-07-20 04:24:03 +08:00
php_wddx_packet_start(packet, comment, comment_len);
2014-12-14 06:06:14 +08:00
php_wddx_serialize_var(packet, var, NULL);
php_wddx_packet_end(packet);
smart_str_0(packet);
RETVAL_STR_COPY(packet->s);
php_wddx_destructor(packet);
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ proto string wddx_serialize_vars(mixed var_name [, mixed ...])
1999-07-20 04:24:03 +08:00
Creates a new packet and serializes given variables into a struct */
PHP_FUNCTION(wddx_serialize_vars)
{
2008-06-21 03:57:25 +08:00
int num_args, i;
1999-07-20 04:24:03 +08:00
wddx_packet *packet;
zval *args = NULL;
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &num_args) == FAILURE) {
2008-06-21 03:57:25 +08:00
return;
1999-07-20 04:24:03 +08:00
}
2015-01-03 17:22:58 +08:00
packet = php_wddx_constructor();
1999-07-20 04:24:03 +08:00
php_wddx_packet_start(packet, NULL, 0);
php_wddx_add_chunk_static(packet, WDDX_STRUCT_S);
2015-01-03 17:22:58 +08:00
2008-06-21 03:57:25 +08:00
for (i=0; i<num_args; i++) {
2014-05-12 11:39:55 +08:00
zval *arg;
if (!Z_ISREF(args[i])) {
arg = &args[i];
} else {
arg = Z_REFVAL(args[i]);
}
if (Z_TYPE_P(arg) != IS_ARRAY && Z_TYPE_P(arg) != IS_OBJECT) {
convert_to_string_ex(arg);
2008-06-21 03:57:25 +08:00
}
2014-05-12 11:39:55 +08:00
php_wddx_add_var(packet, arg);
2015-01-03 17:22:58 +08:00
}
php_wddx_add_chunk_static(packet, WDDX_STRUCT_E);
php_wddx_packet_end(packet);
2014-05-12 11:39:55 +08:00
smart_str_0(packet);
1999-07-20 04:24:03 +08:00
RETVAL_STR_COPY(packet->s);
php_wddx_destructor(packet);
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_constructor
*/
wddx_packet *php_wddx_constructor(void)
{
smart_str *packet;
packet = ecalloc(1, sizeof(smart_str));
return packet;
}
/* }}} */
1999-07-20 04:24:03 +08:00
/* {{{ php_wddx_destructor
*/
void php_wddx_destructor(wddx_packet *packet)
{
smart_str_free(packet);
efree(packet);
}
/* }}} */
2008-07-01 19:26:10 +08:00
/* {{{ proto resource wddx_packet_start([string comment])
1999-07-20 04:24:03 +08:00
Starts a WDDX packet with optional comment and returns the packet id */
PHP_FUNCTION(wddx_packet_start)
{
char *comment = NULL;
2014-08-28 01:25:28 +08:00
size_t comment_len = 0;
1999-07-20 04:24:03 +08:00
wddx_packet *packet;
comment = NULL;
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &comment, &comment_len) == FAILURE) {
return;
2008-08-24 12:02:49 +08:00
}
1999-07-20 04:24:03 +08:00
packet = php_wddx_constructor();
2015-01-03 17:22:58 +08:00
php_wddx_packet_start(packet, comment, comment_len);
php_wddx_add_chunk_static(packet, WDDX_STRUCT_S);
2015-02-03 12:02:28 +08:00
RETURN_RES(zend_register_resource(packet, le_wddx));
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
2008-07-01 19:26:10 +08:00
/* {{{ proto string wddx_packet_end(resource packet_id)
1999-07-20 04:24:03 +08:00
Ends specified WDDX packet and returns the string containing the packet */
PHP_FUNCTION(wddx_packet_end)
{
zval *packet_id;
wddx_packet *packet = NULL;
2015-01-03 17:22:58 +08:00
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &packet_id) == FAILURE) {
return;
2008-07-01 19:26:10 +08:00
}
1999-07-20 04:24:03 +08:00
2015-02-03 12:02:28 +08:00
if ((packet = (wddx_packet *)zend_fetch_resource(Z_RES_P(packet_id), "WDDX packet ID", le_wddx)) == NULL) {
RETURN_FALSE;
}
2015-01-03 17:22:58 +08:00
php_wddx_add_chunk_static(packet, WDDX_STRUCT_E);
php_wddx_packet_end(packet);
smart_str_0(packet);
1999-07-20 04:24:03 +08:00
RETVAL_STR_COPY(packet->s);
zend_list_close(Z_RES_P(packet_id));
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
2015-07-19 22:04:40 +08:00
/* {{{ proto bool wddx_add_vars(resource packet_id, mixed var_names [, mixed ...])
1999-07-20 04:24:03 +08:00
Serializes given variables and adds them to packet given by packet_id */
PHP_FUNCTION(wddx_add_vars)
{
2008-06-21 03:57:25 +08:00
int num_args, i;
zval *args = NULL;
2008-07-01 19:26:10 +08:00
zval *packet_id;
wddx_packet *packet = NULL;
2015-01-03 17:22:58 +08:00
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r+", &packet_id, &args, &num_args) == FAILURE) {
2008-06-21 03:57:25 +08:00
return;
1999-07-20 04:24:03 +08:00
}
2015-02-03 12:02:28 +08:00
if ((packet = (wddx_packet *)zend_fetch_resource(Z_RES_P(packet_id), "WDDX packet ID", le_wddx)) == NULL) {
RETURN_FALSE;
}
2015-01-03 17:22:58 +08:00
for (i=0; i<num_args; i++) {
2014-05-12 11:39:55 +08:00
zval *arg;
if (!Z_ISREF(args[i])) {
arg = &args[i];
} else {
arg = Z_REFVAL(args[i]);
}
if (Z_TYPE_P(arg) != IS_ARRAY && Z_TYPE_P(arg) != IS_OBJECT) {
convert_to_string_ex(arg);
2008-06-21 03:57:25 +08:00
}
2014-05-12 11:39:55 +08:00
php_wddx_add_var(packet, arg);
1999-07-20 04:24:03 +08:00
}
RETURN_TRUE;
}
/* }}} */
1999-07-20 04:24:03 +08:00
2015-01-03 17:22:58 +08:00
/* {{{ proto mixed wddx_deserialize(mixed packet)
1999-07-20 04:24:03 +08:00
Deserializes given packet and returns a PHP value */
PHP_FUNCTION(wddx_deserialize)
{
zval *packet;
php_stream *stream = NULL;
zend_string *payload = NULL;
2015-01-03 17:22:58 +08:00
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &packet) == FAILURE) {
return;
2008-08-24 12:02:49 +08:00
}
if (Z_TYPE_P(packet) == IS_STRING) {
payload = Z_STR_P(packet);
2008-08-24 12:02:49 +08:00
} else if (Z_TYPE_P(packet) == IS_RESOURCE) {
php_stream_from_zval(stream, packet);
if (stream) {
payload = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
}
} else {
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_WARNING, "Expecting parameter 1 to be a string or a stream");
return;
}
1999-08-19 00:43:57 +08:00
if (payload == NULL) {
1999-08-19 00:43:57 +08:00
return;
2008-08-24 12:02:49 +08:00
}
php_wddx_deserialize_ex(ZSTR_VAL(payload), ZSTR_LEN(payload), return_value);
2015-01-03 17:22:58 +08:00
2008-08-24 12:02:49 +08:00
if (stream) {
efree(payload);
2008-08-24 12:02:49 +08:00
}
1999-07-20 04:24:03 +08:00
}
/* }}} */
1999-07-20 04:24:03 +08:00
#endif /* HAVE_LIBEXPAT */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/