PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/soap - php_encoding.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 2035 0.0 %
Date: 2014-04-16 Functions: 0 87 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             :   | Authors: Brad Lafountain <rodif_bl@yahoo.com>                        |
      16             :   |          Shane Caraveo <shane@caraveo.com>                           |
      17             :   |          Dmitry Stogov <dmitry@zend.com>                             |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : /* $Id$ */
      21             : 
      22             : #include <time.h>
      23             : 
      24             : #include "php_soap.h"
      25             : #include "ext/libxml/php_libxml.h"
      26             : #include "ext/standard/base64.h"
      27             : #include <libxml/parserInternals.h>
      28             : #include "zend_strtod.h"
      29             : #include "zend_interfaces.h"
      30             : 
      31             : /* zval type decode */
      32             : static zval *to_zval_double(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      33             : static zval *to_zval_long(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      34             : static zval *to_zval_bool(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      35             : static zval *to_zval_string(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      36             : static zval *to_zval_stringr(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      37             : static zval *to_zval_stringc(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      38             : static zval *to_zval_map(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      39             : static zval *to_zval_null(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      40             : static zval *to_zval_base64(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      41             : static zval *to_zval_hexbin(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      42             : 
      43             : static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      44             : static xmlNodePtr to_xml_double(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      45             : static xmlNodePtr to_xml_bool(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      46             : 
      47             : /* String encode */
      48             : static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      49             : static xmlNodePtr to_xml_base64(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      50             : static xmlNodePtr to_xml_hexbin(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      51             : 
      52             : /* Null encode */
      53             : static xmlNodePtr to_xml_null(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      54             : 
      55             : /* Array encode */
      56             : static xmlNodePtr guess_array_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      57             : static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      58             : 
      59             : static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      60             : static xmlNodePtr to_xml_list1(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      61             : 
      62             : /* Datetime encode/decode */
      63             : static xmlNodePtr to_xml_datetime_ex(encodeTypePtr type, zval *data, char *format, int style, xmlNodePtr parent TSRMLS_DC);
      64             : static xmlNodePtr to_xml_datetime(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      65             : static xmlNodePtr to_xml_time(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      66             : static xmlNodePtr to_xml_date(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      67             : static xmlNodePtr to_xml_gyearmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      68             : static xmlNodePtr to_xml_gyear(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      69             : static xmlNodePtr to_xml_gmonthday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      70             : static xmlNodePtr to_xml_gday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      71             : static xmlNodePtr to_xml_gmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      72             : static xmlNodePtr to_xml_duration(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      73             : 
      74             : static zval *to_zval_object(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      75             : static zval *to_zval_array(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      76             : 
      77             : static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      78             : static xmlNodePtr to_xml_array(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      79             : 
      80             : static zval *to_zval_any(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      81             : static xmlNodePtr to_xml_any(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      82             : 
      83             : /* Try and guess for non-wsdl clients and servers */
      84             : static zval *guess_zval_convert(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
      85             : static xmlNodePtr guess_xml_convert(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
      86             : 
      87             : static int is_map(zval *array);
      88             : static encodePtr get_array_type(xmlNodePtr node, zval *array, smart_str *out_type TSRMLS_DC);
      89             : 
      90             : static xmlNodePtr check_and_resolve_href(xmlNodePtr data);
      91             : 
      92             : static void set_ns_prop(xmlNodePtr node, char *ns, char *name, char *val);
      93             : static void set_xsi_nil(xmlNodePtr node);
      94             : static void set_xsi_type(xmlNodePtr node, char *type);
      95             : 
      96             : static void get_type_str(xmlNodePtr node, const char* ns, const char* type, smart_str* ret);
      97             : static void set_ns_and_type_ex(xmlNodePtr node, char *ns, char *type);
      98             : 
      99             : static void set_ns_and_type(xmlNodePtr node, encodeTypePtr type);
     100             : 
     101             : #define FIND_XML_NULL(xml,zval) \
     102             :         { \
     103             :                 xmlAttrPtr null; \
     104             :                 if (!xml) { \
     105             :                         ZVAL_NULL(zval); \
     106             :                         return zval; \
     107             :                 } \
     108             :                 if (xml->properties) { \
     109             :                         null = get_attribute(xml->properties, "nil"); \
     110             :                         if (null) { \
     111             :                                 ZVAL_NULL(zval); \
     112             :                                 return zval; \
     113             :                         } \
     114             :                 } \
     115             :         }
     116             : 
     117             : #define CHECK_XML_NULL(xml) \
     118             :         { \
     119             :                 xmlAttrPtr null; \
     120             :                 if (!xml) { \
     121             :                         zval *ret; \
     122             :                         ALLOC_INIT_ZVAL(ret); \
     123             :                         ZVAL_NULL(ret); \
     124             :                         return ret; \
     125             :                 } \
     126             :                 if (xml->properties) { \
     127             :                         null = get_attribute(xml->properties, "nil"); \
     128             :                         if (null) { \
     129             :                                 zval *ret; \
     130             :                                 ALLOC_INIT_ZVAL(ret); \
     131             :                                 ZVAL_NULL(ret); \
     132             :                                 return ret; \
     133             :                         } \
     134             :                 } \
     135             :         }
     136             : 
     137             : #define FIND_ZVAL_NULL(zval, xml, style) \
     138             : { \
     139             :         if (!zval || Z_TYPE_P(zval) == IS_NULL) { \
     140             :           if (style == SOAP_ENCODED) {\
     141             :                         set_xsi_nil(xml); \
     142             :                 } \
     143             :                 return xml; \
     144             :         } \
     145             : }
     146             : 
     147             : encode defaultEncoding[] = {
     148             :         {{UNKNOWN_TYPE, NULL, NULL, NULL}, guess_zval_convert, guess_xml_convert},
     149             : 
     150             :         {{IS_NULL, "nil", XSI_NAMESPACE, NULL}, to_zval_null, to_xml_null},
     151             :         {{IS_STRING, XSD_STRING_STRING, XSD_NAMESPACE, NULL}, to_zval_string, to_xml_string},
     152             :         {{IS_LONG, XSD_INT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     153             :         {{IS_DOUBLE, XSD_FLOAT_STRING, XSD_NAMESPACE, NULL}, to_zval_double, to_xml_double},
     154             :         {{IS_BOOL, XSD_BOOLEAN_STRING, XSD_NAMESPACE, NULL}, to_zval_bool, to_xml_bool},
     155             :         {{IS_CONSTANT, XSD_STRING_STRING, XSD_NAMESPACE, NULL}, to_zval_string, to_xml_string},
     156             :         {{IS_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_array, guess_array_map},
     157             :         {{IS_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_object, to_xml_object},
     158             :         {{IS_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_array, guess_array_map},
     159             :         {{IS_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_object, to_xml_object},
     160             : 
     161             :         {{XSD_STRING, XSD_STRING_STRING, XSD_NAMESPACE, NULL}, to_zval_string, to_xml_string},
     162             :         {{XSD_BOOLEAN, XSD_BOOLEAN_STRING, XSD_NAMESPACE, NULL}, to_zval_bool, to_xml_bool},
     163             :         {{XSD_DECIMAL, XSD_DECIMAL_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     164             :         {{XSD_FLOAT, XSD_FLOAT_STRING, XSD_NAMESPACE, NULL}, to_zval_double, to_xml_double},
     165             :         {{XSD_DOUBLE, XSD_DOUBLE_STRING, XSD_NAMESPACE, NULL}, to_zval_double, to_xml_double},
     166             : 
     167             :         {{XSD_DATETIME, XSD_DATETIME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_datetime},
     168             :         {{XSD_TIME, XSD_TIME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_time},
     169             :         {{XSD_DATE, XSD_DATE_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_date},
     170             :         {{XSD_GYEARMONTH, XSD_GYEARMONTH_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gyearmonth},
     171             :         {{XSD_GYEAR, XSD_GYEAR_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gyear},
     172             :         {{XSD_GMONTHDAY, XSD_GMONTHDAY_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gmonthday},
     173             :         {{XSD_GDAY, XSD_GDAY_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gday},
     174             :         {{XSD_GMONTH, XSD_GMONTH_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gmonth},
     175             :         {{XSD_DURATION, XSD_DURATION_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_duration},
     176             : 
     177             :         {{XSD_HEXBINARY, XSD_HEXBINARY_STRING, XSD_NAMESPACE, NULL}, to_zval_hexbin, to_xml_hexbin},
     178             :         {{XSD_BASE64BINARY, XSD_BASE64BINARY_STRING, XSD_NAMESPACE, NULL}, to_zval_base64, to_xml_base64},
     179             : 
     180             :         {{XSD_LONG, XSD_LONG_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     181             :         {{XSD_INT, XSD_INT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     182             :         {{XSD_SHORT, XSD_SHORT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     183             :         {{XSD_BYTE, XSD_BYTE_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     184             :         {{XSD_NONPOSITIVEINTEGER, XSD_NONPOSITIVEINTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     185             :         {{XSD_POSITIVEINTEGER, XSD_POSITIVEINTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     186             :         {{XSD_NONNEGATIVEINTEGER, XSD_NONNEGATIVEINTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     187             :         {{XSD_NEGATIVEINTEGER, XSD_NEGATIVEINTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     188             :         {{XSD_UNSIGNEDBYTE, XSD_UNSIGNEDBYTE_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     189             :         {{XSD_UNSIGNEDSHORT, XSD_UNSIGNEDSHORT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     190             :         {{XSD_UNSIGNEDINT, XSD_UNSIGNEDINT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     191             :         {{XSD_UNSIGNEDLONG, XSD_UNSIGNEDLONG_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     192             :         {{XSD_INTEGER, XSD_INTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     193             : 
     194             :         {{XSD_ANYTYPE, XSD_ANYTYPE_STRING, XSD_NAMESPACE, NULL}, guess_zval_convert, guess_xml_convert},
     195             :         {{XSD_UR_TYPE, XSD_UR_TYPE_STRING, XSD_NAMESPACE, NULL}, guess_zval_convert, guess_xml_convert},
     196             :         {{XSD_ANYURI, XSD_ANYURI_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     197             :         {{XSD_QNAME, XSD_QNAME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     198             :         {{XSD_NOTATION, XSD_NOTATION_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     199             :         {{XSD_NORMALIZEDSTRING, XSD_NORMALIZEDSTRING_STRING, XSD_NAMESPACE, NULL}, to_zval_stringr, to_xml_string},
     200             :         {{XSD_TOKEN, XSD_TOKEN_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     201             :         {{XSD_LANGUAGE, XSD_LANGUAGE_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     202             :         {{XSD_NMTOKEN, XSD_NMTOKEN_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     203             :         {{XSD_NMTOKENS, XSD_NMTOKENS_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_list1},
     204             :         {{XSD_NAME, XSD_NAME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     205             :         {{XSD_NCNAME, XSD_NCNAME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     206             :         {{XSD_ID, XSD_ID_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     207             :         {{XSD_IDREF, XSD_IDREF_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     208             :         {{XSD_IDREFS, XSD_IDREFS_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_list1},
     209             :         {{XSD_ENTITY, XSD_ENTITY_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     210             :         {{XSD_ENTITIES, XSD_ENTITIES_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_list1},
     211             : 
     212             :         {{APACHE_MAP, APACHE_MAP_STRING, APACHE_NAMESPACE, NULL}, to_zval_map, to_xml_map},
     213             : 
     214             :         {{SOAP_ENC_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_object, to_xml_object},
     215             :         {{SOAP_ENC_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_array, to_xml_array},
     216             :         {{SOAP_ENC_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_object, to_xml_object},
     217             :         {{SOAP_ENC_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_array, to_xml_array},
     218             : 
     219             :         /* support some of the 1999 data types */
     220             :         {{XSD_STRING, XSD_STRING_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_string, to_xml_string},
     221             :         {{XSD_BOOLEAN, XSD_BOOLEAN_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_bool, to_xml_bool},
     222             :         {{XSD_DECIMAL, XSD_DECIMAL_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     223             :         {{XSD_FLOAT, XSD_FLOAT_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_double, to_xml_double},
     224             :         {{XSD_DOUBLE, XSD_DOUBLE_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_double, to_xml_double},
     225             : 
     226             :         {{XSD_LONG, XSD_LONG_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     227             :         {{XSD_INT, XSD_INT_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     228             :         {{XSD_SHORT, XSD_SHORT_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     229             :         {{XSD_BYTE, XSD_BYTE_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_long, to_xml_long},
     230             :         {{XSD_1999_TIMEINSTANT, XSD_1999_TIMEINSTANT_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
     231             : 
     232             :         {{XSD_ANYXML, "<anyXML>", "<anyXML>", NULL}, to_zval_any, to_xml_any},
     233             : 
     234             :         {{END_KNOWN_TYPES, NULL, NULL, NULL}, guess_zval_convert, guess_xml_convert}
     235             : };
     236             : 
     237             : int numDefaultEncodings = sizeof(defaultEncoding)/sizeof(encode);
     238             : 
     239             : 
     240           0 : void whiteSpace_replace(xmlChar* str)
     241             : {
     242           0 :         while (*str != '\0') {
     243           0 :                 if (*str == '\x9' || *str == '\xA' || *str == '\xD') {
     244           0 :                         *str = ' ';
     245             :                 }
     246           0 :                 str++;
     247             :         }
     248           0 : }
     249             : 
     250           0 : void whiteSpace_collapse(xmlChar* str)
     251             : {
     252             :         xmlChar *pos;
     253             :         xmlChar old;
     254             : 
     255           0 :         pos = str;
     256           0 :         whiteSpace_replace(str);
     257           0 :         while (*str == ' ') {
     258           0 :                 str++;
     259             :         }
     260           0 :         old = '\0';
     261           0 :         while (*str != '\0') {
     262           0 :                 if (*str != ' ' || old != ' ') {
     263           0 :                         *pos = *str;
     264           0 :                         pos++;
     265             :                 }
     266           0 :                 old = *str;
     267           0 :                 str++;
     268             :         }
     269           0 :         if (old == ' ') {
     270           0 :                 --pos;
     271             :         }
     272           0 :         *pos = '\0';
     273           0 : }
     274             : 
     275           0 : static encodePtr find_encoder_by_type_name(sdlPtr sdl, const char *type)
     276             : {
     277           0 :         if (sdl && sdl->encoders) {
     278             :                 HashPosition pos;
     279             :                 encodePtr *enc;
     280             : 
     281           0 :                 for (zend_hash_internal_pointer_reset_ex(sdl->encoders, &pos);
     282           0 :                      zend_hash_get_current_data_ex(sdl->encoders, (void **) &enc, &pos) == SUCCESS;
     283           0 :                      zend_hash_move_forward_ex(sdl->encoders, &pos)) {
     284           0 :                     if (strcmp((*enc)->details.type_str, type) == 0) {
     285           0 :                                 return *enc;
     286             :                         }
     287             :                 }
     288             :         }
     289           0 :         return NULL;
     290             : }
     291             : 
     292           0 : static zend_bool soap_check_zval_ref(zval *data, xmlNodePtr node TSRMLS_DC) {
     293             :         xmlNodePtr *node_ptr;
     294             : 
     295           0 :         if (SOAP_GLOBAL(ref_map)) {
     296           0 :                 if (Z_TYPE_P(data) == IS_OBJECT) {
     297           0 :                         data = (zval*)zend_objects_get_address(data TSRMLS_CC);
     298             :                 }
     299           0 :                 if (zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)data, (void**)&node_ptr) == SUCCESS) {
     300           0 :                         xmlAttrPtr attr = (*node_ptr)->properties;
     301             :                         char *id;
     302           0 :                         smart_str prefix = {0};
     303             : 
     304           0 :                         if (*node_ptr == node) {
     305           0 :                                 return 0;
     306             :                         }
     307           0 :                         xmlNodeSetName(node, (*node_ptr)->name);
     308           0 :                         xmlSetNs(node, (*node_ptr)->ns);
     309           0 :                         if (SOAP_GLOBAL(soap_version) == SOAP_1_1) {
     310             :                                 while (1) {
     311           0 :                                         attr = get_attribute(attr, "id");
     312           0 :                                         if (attr == NULL || attr->ns == NULL) {
     313             :                                                 break;
     314             :                                         }
     315           0 :                                         attr = attr->next;
     316           0 :                                 }
     317           0 :                                 if (attr) {
     318           0 :                                         id = (char*)attr->children->content;
     319           0 :                                         smart_str_appendc(&prefix, '#');
     320           0 :                                         smart_str_appends(&prefix, id);
     321           0 :                                         smart_str_0(&prefix);
     322           0 :                                         id = prefix.c;
     323             :                                 } else {
     324           0 :                                         SOAP_GLOBAL(cur_uniq_ref)++;
     325           0 :                                         smart_str_appendl(&prefix, "#ref", 4);
     326           0 :                                         smart_str_append_long(&prefix, SOAP_GLOBAL(cur_uniq_ref));
     327           0 :                                         smart_str_0(&prefix);
     328           0 :                                         id = prefix.c;
     329           0 :                                         xmlSetProp((*node_ptr), BAD_CAST("id"), BAD_CAST(id+1));
     330             :                                 }
     331           0 :                                 xmlSetProp(node, BAD_CAST("href"), BAD_CAST(id));
     332             :                         } else {
     333           0 :                                 attr = get_attribute_ex(attr, "id", SOAP_1_2_ENC_NAMESPACE);
     334           0 :                                 if (attr) {
     335           0 :                                         id = (char*)attr->children->content;
     336           0 :                                         smart_str_appendc(&prefix, '#');
     337           0 :                                         smart_str_appends(&prefix, id);
     338           0 :                                         smart_str_0(&prefix);
     339           0 :                                         id = prefix.c;
     340             :                                 } else {
     341           0 :                                         SOAP_GLOBAL(cur_uniq_ref)++;
     342           0 :                                         smart_str_appendl(&prefix, "#ref", 4);
     343           0 :                                         smart_str_append_long(&prefix, SOAP_GLOBAL(cur_uniq_ref));
     344           0 :                                         smart_str_0(&prefix);
     345           0 :                                         id = prefix.c;
     346           0 :                                         set_ns_prop((*node_ptr), SOAP_1_2_ENC_NAMESPACE, "id", id+1);
     347             :                                 }
     348           0 :                                 set_ns_prop(node, SOAP_1_2_ENC_NAMESPACE, "ref", id);
     349             :                         }
     350           0 :                         smart_str_free(&prefix);
     351           0 :                         return 1;
     352             :                 } else {
     353           0 :                         zend_hash_index_update(SOAP_GLOBAL(ref_map), (ulong)data, (void**)&node, sizeof(xmlNodePtr), NULL);
     354             :                 }
     355             :         }
     356           0 :         return 0;
     357             : }
     358             : 
     359           0 : static zval* soap_find_xml_ref(xmlNodePtr node TSRMLS_DC)
     360             : {
     361             :         zval **data_ptr;
     362             : 
     363           0 :         if (SOAP_GLOBAL(ref_map) && 
     364           0 :             zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)node, (void**)&data_ptr) == SUCCESS) {
     365           0 :                 Z_SET_ISREF_PP(data_ptr);
     366           0 :                 Z_ADDREF_PP(data_ptr);
     367           0 :                 return *data_ptr;
     368             :         }
     369           0 :         return NULL;
     370             : }
     371             : 
     372           0 : static zend_bool soap_check_xml_ref(zval **data, xmlNodePtr node TSRMLS_DC)
     373             : {
     374             :         zval **data_ptr;
     375             : 
     376           0 :         if (SOAP_GLOBAL(ref_map)) {
     377           0 :                 if (zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)node, (void**)&data_ptr) == SUCCESS) {
     378           0 :                         if (*data != *data_ptr) {
     379           0 :                                 zval_ptr_dtor(data);
     380           0 :                                 *data = *data_ptr;
     381           0 :                                 Z_SET_ISREF_PP(data);
     382           0 :                                 Z_ADDREF_PP(data);
     383           0 :                                 return 1;
     384             :                         }
     385             :                 } else {
     386           0 :                         zend_hash_index_update(SOAP_GLOBAL(ref_map), (ulong)node, (void**)data, sizeof(zval*), NULL);
     387             :                 }
     388             :         }
     389           0 :         return 0;
     390             : }
     391             : 
     392           0 : static xmlNodePtr master_to_xml_int(encodePtr encode, zval *data, int style, xmlNodePtr parent, int check_class_map TSRMLS_DC)
     393             : {
     394           0 :         xmlNodePtr node = NULL;
     395           0 :         int add_type = 0;
     396             : 
     397             :         /* Special handling of class SoapVar */
     398           0 :         if (data &&
     399           0 :             Z_TYPE_P(data) == IS_OBJECT &&
     400           0 :             Z_OBJCE_P(data) == soap_var_class_entry) {
     401             :                 zval **ztype, **zdata, **zns, **zstype, **zname, **znamens;
     402           0 :                 encodePtr enc = NULL;
     403           0 :                 HashTable *ht = Z_OBJPROP_P(data);
     404             : 
     405           0 :                 if (zend_hash_find(ht, "enc_type", sizeof("enc_type"), (void **)&ztype) == FAILURE) {
     406           0 :                         soap_error0(E_ERROR, "Encoding: SoapVar has no 'enc_type' property");
     407             :                 }
     408             : 
     409           0 :                 if (zend_hash_find(ht, "enc_stype", sizeof("enc_stype"), (void **)&zstype) == SUCCESS) {
     410           0 :                         if (zend_hash_find(ht, "enc_ns", sizeof("enc_ns"), (void **)&zns) == SUCCESS) {
     411           0 :                                 enc = get_encoder(SOAP_GLOBAL(sdl), Z_STRVAL_PP(zns), Z_STRVAL_PP(zstype));
     412             :                         } else {
     413           0 :                                 zns = NULL;
     414           0 :                                 enc = get_encoder_ex(SOAP_GLOBAL(sdl), Z_STRVAL_PP(zstype), Z_STRLEN_PP(zstype));
     415             :                         }
     416           0 :                         if (enc == NULL && SOAP_GLOBAL(typemap)) {
     417             :                                 encodePtr *new_enc;
     418           0 :                                 smart_str nscat = {0};
     419             : 
     420           0 :                                 if (zns != NULL) {
     421           0 :                                         smart_str_appendl(&nscat, Z_STRVAL_PP(zns), Z_STRLEN_PP(zns));
     422           0 :                                         smart_str_appendc(&nscat, ':');
     423             :                                 }
     424           0 :                                 smart_str_appendl(&nscat, Z_STRVAL_PP(zstype), Z_STRLEN_PP(zstype));
     425           0 :                                 smart_str_0(&nscat);
     426           0 :                                 if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
     427           0 :                                         enc = *new_enc;
     428             :                                 }
     429           0 :                                 smart_str_free(&nscat);                     
     430             :                         }
     431             :                 }
     432           0 :                 if (enc == NULL) {
     433           0 :                         enc = get_conversion(Z_LVAL_P(*ztype));
     434             :                 }
     435           0 :                 if (enc == NULL) {
     436           0 :                         enc = encode;
     437             :                 }
     438             : 
     439           0 :                 if (zend_hash_find(ht, "enc_value", sizeof("enc_value"), (void **)&zdata) == FAILURE) {
     440           0 :                         node = master_to_xml(enc, NULL, style, parent TSRMLS_CC);
     441             :                 } else {
     442           0 :                         node = master_to_xml(enc, *zdata, style, parent TSRMLS_CC);
     443             :                 }
     444             : 
     445           0 :                 if (style == SOAP_ENCODED || (SOAP_GLOBAL(sdl) && encode != enc)) {
     446           0 :                         if (zend_hash_find(ht, "enc_stype", sizeof("enc_stype"), (void **)&zstype) == SUCCESS) {
     447           0 :                                 if (zend_hash_find(ht, "enc_ns", sizeof("enc_ns"), (void **)&zns) == SUCCESS) {
     448           0 :                                         set_ns_and_type_ex(node, Z_STRVAL_PP(zns), Z_STRVAL_PP(zstype));
     449             :                                 } else {
     450           0 :                                         set_ns_and_type_ex(node, NULL, Z_STRVAL_PP(zstype));
     451             :                                 }
     452             :                         }
     453             :                 }
     454             : 
     455           0 :                 if (zend_hash_find(ht, "enc_name", sizeof("enc_name"), (void **)&zname) == SUCCESS) {
     456           0 :                         xmlNodeSetName(node, BAD_CAST(Z_STRVAL_PP(zname)));
     457             :                 }
     458           0 :                 if (zend_hash_find(ht, "enc_namens", sizeof("enc_namens"), (void **)&znamens) == SUCCESS) {
     459           0 :                         xmlNsPtr nsp = encode_add_ns(node, Z_STRVAL_PP(znamens));
     460           0 :                         xmlSetNs(node, nsp);
     461             :                 }
     462             :         } else {
     463           0 :                 if (check_class_map && SOAP_GLOBAL(class_map) && data &&
     464           0 :                     Z_TYPE_P(data) == IS_OBJECT &&
     465           0 :                     !Z_OBJPROP_P(data)->nApplyCount) {
     466           0 :                         zend_class_entry *ce = Z_OBJCE_P(data);
     467             :                         HashPosition pos;
     468             :                         zval **tmp;
     469           0 :                         char *type_name = NULL;
     470             :                         uint type_len;
     471             :                         ulong idx;
     472             : 
     473           0 :                         for (zend_hash_internal_pointer_reset_ex(SOAP_GLOBAL(class_map), &pos);
     474           0 :                              zend_hash_get_current_data_ex(SOAP_GLOBAL(class_map), (void **) &tmp, &pos) == SUCCESS;
     475           0 :                              zend_hash_move_forward_ex(SOAP_GLOBAL(class_map), &pos)) {
     476           0 :                                 if (Z_TYPE_PP(tmp) == IS_STRING &&
     477           0 :                                     ce->name_length == Z_STRLEN_PP(tmp) &&
     478           0 :                                     zend_binary_strncasecmp(ce->name, ce->name_length, Z_STRVAL_PP(tmp), ce->name_length, ce->name_length) == 0 &&
     479           0 :                                     zend_hash_get_current_key_ex(SOAP_GLOBAL(class_map), &type_name, &type_len, &idx, 0, &pos) == HASH_KEY_IS_STRING) {
     480             : 
     481             :                                         /* TODO: namespace isn't stored */
     482           0 :                                         encodePtr enc = NULL;
     483           0 :                                         if (SOAP_GLOBAL(sdl)) {
     484           0 :                                                 enc = get_encoder(SOAP_GLOBAL(sdl), SOAP_GLOBAL(sdl)->target_ns, type_name);
     485           0 :                                                 if (!enc) {
     486           0 :                                                         enc = find_encoder_by_type_name(SOAP_GLOBAL(sdl), type_name);
     487             :                                                 }
     488             :                                         }
     489           0 :                                         if (enc) {
     490           0 :                                                 if (encode != enc && style == SOAP_LITERAL) {
     491           0 :                                                         add_type = 1;                                           
     492             :                                                 }
     493           0 :                                                 encode = enc;
     494             :                                         }
     495           0 :                                         break;
     496             :                                 }
     497             :                         }
     498             :                 }
     499             : 
     500           0 :                 if (encode == NULL) {
     501           0 :                         encode = get_conversion(UNKNOWN_TYPE);
     502             :                 }
     503           0 :                 if (SOAP_GLOBAL(typemap) && encode->details.type_str) {
     504           0 :                         smart_str nscat = {0};
     505             :                         encodePtr *new_enc;
     506             : 
     507           0 :                         if (encode->details.ns) {
     508           0 :                                 smart_str_appends(&nscat, encode->details.ns);
     509           0 :                                 smart_str_appendc(&nscat, ':');
     510             :                         }
     511           0 :                         smart_str_appends(&nscat, encode->details.type_str);
     512           0 :                         smart_str_0(&nscat);
     513           0 :                         if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
     514           0 :                                 encode = *new_enc;
     515             :                         }
     516           0 :                         smart_str_free(&nscat);                     
     517             :                 }
     518           0 :                 if (encode->to_xml) {
     519           0 :                         node = encode->to_xml(&encode->details, data, style, parent TSRMLS_CC);
     520           0 :                         if (add_type) {
     521           0 :                                 set_ns_and_type(node, &encode->details);
     522             :                         }
     523             :                 }
     524             :         }
     525           0 :         return node;
     526             : }
     527             : 
     528           0 : xmlNodePtr master_to_xml(encodePtr encode, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
     529             : {
     530           0 :         return master_to_xml_int(encode, data, style, parent, 1 TSRMLS_CC);
     531             : }
     532             : 
     533           0 : static zval *master_to_zval_int(encodePtr encode, xmlNodePtr data TSRMLS_DC)
     534             : {
     535           0 :         zval *ret = NULL;
     536             : 
     537           0 :         if (SOAP_GLOBAL(typemap)) {
     538           0 :                 if (encode->details.type_str) {
     539           0 :                         smart_str nscat = {0};
     540             :                         encodePtr *new_enc;
     541             : 
     542           0 :                         if (encode->details.ns) {
     543           0 :                                 smart_str_appends(&nscat, encode->details.ns);
     544           0 :                                 smart_str_appendc(&nscat, ':');
     545             :                         }
     546           0 :                         smart_str_appends(&nscat, encode->details.type_str);
     547           0 :                         smart_str_0(&nscat);
     548           0 :                         if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
     549           0 :                                 encode = *new_enc;
     550             :                         }
     551           0 :                         smart_str_free(&nscat);                     
     552             :                 } else {
     553           0 :                         xmlAttrPtr type_attr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
     554             : 
     555           0 :                         if (type_attr != NULL) {
     556             :                                 encodePtr *new_enc;
     557             :                                 xmlNsPtr nsptr;
     558             :                                 char *ns, *cptype;
     559           0 :                                 smart_str nscat = {0};
     560             : 
     561           0 :                                 parse_namespace(type_attr->children->content, &cptype, &ns);
     562           0 :                                 nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
     563           0 :                                 if (nsptr != NULL) {
     564           0 :                                         smart_str_appends(&nscat, (char*)nsptr->href);
     565           0 :                                         smart_str_appendc(&nscat, ':');
     566             :                                 }
     567           0 :                                 smart_str_appends(&nscat, cptype);
     568           0 :                                 smart_str_0(&nscat);
     569           0 :                                 efree(cptype);
     570           0 :                                 if (ns) {efree(ns);}
     571           0 :                                 if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
     572           0 :                                         encode = *new_enc;
     573             :                                 }
     574           0 :                                 smart_str_free(&nscat);                     
     575             :                         }
     576             :                 }
     577             :         }
     578           0 :         if (encode->to_zval) {
     579           0 :                 ret = encode->to_zval(&encode->details, data TSRMLS_CC);
     580             :         }
     581           0 :         return ret;
     582             : }
     583             : 
     584           0 : zval *master_to_zval(encodePtr encode, xmlNodePtr data TSRMLS_DC)
     585             : {
     586           0 :         data = check_and_resolve_href(data);
     587             : 
     588           0 :         if (encode == NULL) {
     589           0 :                 encode = get_conversion(UNKNOWN_TYPE);
     590             :         } else {
     591             :                 /* Use xsi:type if it is defined */
     592           0 :                 xmlAttrPtr type_attr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
     593             : 
     594           0 :                 if (type_attr != NULL) {
     595           0 :                         encodePtr  enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, type_attr->children->content);
     596             : 
     597           0 :                         if (enc != NULL && enc != encode) {
     598           0 :                           encodePtr tmp = enc;
     599           0 :                           while (tmp &&
     600           0 :                                  tmp->details.sdl_type != NULL &&
     601           0 :                                  tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
     602           0 :                             if (enc == tmp->details.sdl_type->encode ||
     603           0 :                                 tmp == tmp->details.sdl_type->encode) {
     604           0 :                                 enc = NULL;
     605           0 :                                 break;
     606             :                             }
     607           0 :                             tmp = tmp->details.sdl_type->encode;
     608             :                           }
     609           0 :                           if (enc != NULL) {
     610           0 :                             encode = enc;
     611             :                           }
     612             :                         }
     613             :                 }
     614             :         }
     615           0 :         return master_to_zval_int(encode, data TSRMLS_CC);
     616             : }
     617             : 
     618           0 : xmlNodePtr to_xml_user(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
     619             : {
     620           0 :         xmlNodePtr ret = NULL;
     621             :         zval *return_value;
     622             : 
     623           0 :         if (type && type->map && type->map->to_xml) {
     624           0 :                 MAKE_STD_ZVAL(return_value);
     625             : 
     626           0 :                 if (call_user_function(EG(function_table), NULL, type->map->to_xml, return_value, 1, &data TSRMLS_CC) == FAILURE) {
     627           0 :                         soap_error0(E_ERROR, "Encoding: Error calling to_xml callback");
     628             :                 }
     629           0 :                 if (Z_TYPE_P(return_value) == IS_STRING) {              
     630           0 :                         xmlDocPtr doc = soap_xmlParseMemory(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
     631           0 :                         if (doc && doc->children) {                          
     632           0 :                                 ret = xmlDocCopyNode(doc->children, parent->doc, 1);
     633             :                         }
     634           0 :                         xmlFreeDoc(doc);
     635             :                 }
     636             : 
     637           0 :                 zval_ptr_dtor(&return_value);
     638             :         }
     639           0 :         if (!ret) {
     640           0 :                 ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     641             :         }
     642           0 :         xmlAddChild(parent, ret);
     643           0 :         if (style == SOAP_ENCODED) {
     644           0 :                 set_ns_and_type(ret, type);
     645             :         }
     646           0 :         return ret;
     647             : }
     648             : 
     649           0 : zval *to_zval_user(encodeTypePtr type, xmlNodePtr node TSRMLS_DC)
     650             : {
     651             :         zval *return_value;
     652             : 
     653           0 :         if (type && type->map && type->map->to_zval) {
     654             :                 xmlBufferPtr buf;
     655             :                 zval *data;
     656             :                 xmlNodePtr copy;
     657             : 
     658           0 :                 copy = xmlCopyNode(node, 1);
     659           0 :                 buf = xmlBufferCreate();
     660           0 :                 xmlNodeDump(buf, NULL, copy, 0, 0);
     661           0 :                 MAKE_STD_ZVAL(data);
     662           0 :                 ZVAL_STRING(data, (char*)xmlBufferContent(buf), 1);
     663           0 :                 xmlBufferFree(buf);
     664           0 :                 xmlFreeNode(copy);
     665             : 
     666           0 :                 ALLOC_INIT_ZVAL(return_value);
     667             :                 
     668           0 :                 if (call_user_function(EG(function_table), NULL, type->map->to_zval, return_value, 1, &data TSRMLS_CC) == FAILURE) {
     669           0 :                         soap_error0(E_ERROR, "Encoding: Error calling from_xml callback");
     670             :                 }
     671           0 :                 zval_ptr_dtor(&data);
     672             :         } else {
     673           0 :                 ALLOC_INIT_ZVAL(return_value);
     674             :         }
     675           0 :         return return_value;
     676             : }
     677             : 
     678             : /* TODO: get rid of "bogus".. ither by passing in the already created xmlnode or passing in the node name */
     679             : /* String encode/decode */
     680           0 : static zval *to_zval_string(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
     681             : {
     682             :         zval *ret;
     683           0 :         MAKE_STD_ZVAL(ret);
     684           0 :         FIND_XML_NULL(data, ret);
     685           0 :         if (data && data->children) {
     686           0 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     687           0 :                         if (SOAP_GLOBAL(encoding) != NULL) {
     688           0 :                                 xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
     689           0 :                                 xmlBufferPtr out = xmlBufferCreate();
     690           0 :                                 int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
     691             : 
     692           0 :                                 if (n >= 0) {
     693           0 :                                         ZVAL_STRING(ret, (char*)xmlBufferContent(out), 1);
     694             :                                 } else {
     695           0 :                                         ZVAL_STRING(ret, (char*)data->children->content, 1);
     696             :                                 }
     697           0 :                                 xmlBufferFree(out);
     698           0 :                                 xmlBufferFree(in);
     699             :                         } else {
     700           0 :                                 ZVAL_STRING(ret, (char*)data->children->content, 1);
     701             :                         }
     702           0 :                 } else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
     703           0 :                         ZVAL_STRING(ret, (char*)data->children->content, 1);
     704             :                 } else {
     705           0 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     706             :                 }
     707             :         } else {
     708           0 :                 ZVAL_EMPTY_STRING(ret);
     709             :         }
     710           0 :         return ret;
     711             : }
     712             : 
     713           0 : static zval *to_zval_stringr(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
     714             : {
     715             :         zval *ret;
     716           0 :         MAKE_STD_ZVAL(ret);
     717           0 :         FIND_XML_NULL(data, ret);
     718           0 :         if (data && data->children) {
     719           0 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     720           0 :                         whiteSpace_replace(data->children->content);
     721           0 :                         if (SOAP_GLOBAL(encoding) != NULL) {
     722           0 :                                 xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
     723           0 :                                 xmlBufferPtr out = xmlBufferCreate();
     724           0 :                                 int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
     725             : 
     726           0 :                                 if (n >= 0) {
     727           0 :                                         ZVAL_STRING(ret, (char*)xmlBufferContent(out), 1);
     728             :                                 } else {
     729           0 :                                         ZVAL_STRING(ret, (char*)data->children->content, 1);
     730             :                                 }
     731           0 :                                 xmlBufferFree(out);
     732           0 :                                 xmlBufferFree(in);
     733             :                         } else {
     734           0 :                                 ZVAL_STRING(ret, (char*)data->children->content, 1);
     735             :                         }
     736           0 :                 } else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
     737           0 :                         ZVAL_STRING(ret, (char*)data->children->content, 1);
     738             :                 } else {
     739           0 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     740             :                 }
     741             :         } else {
     742           0 :                 ZVAL_EMPTY_STRING(ret);
     743             :         }
     744           0 :         return ret;
     745             : }
     746             : 
     747           0 : static zval *to_zval_stringc(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
     748             : {
     749             :         zval *ret;
     750           0 :         MAKE_STD_ZVAL(ret);
     751           0 :         FIND_XML_NULL(data, ret);
     752           0 :         if (data && data->children) {
     753           0 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     754           0 :                         whiteSpace_collapse(data->children->content);
     755           0 :                         if (SOAP_GLOBAL(encoding) != NULL) {
     756           0 :                                 xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
     757           0 :                                 xmlBufferPtr out = xmlBufferCreate();
     758           0 :                                 int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
     759             : 
     760           0 :                                 if (n >= 0) {
     761           0 :                                         ZVAL_STRING(ret, (char*)xmlBufferContent(out), 1);
     762             :                                 } else {
     763           0 :                                         ZVAL_STRING(ret, (char*)data->children->content, 1);
     764             :                                 }
     765           0 :                                 xmlBufferFree(out);
     766           0 :                                 xmlBufferFree(in);
     767             :                         } else {
     768           0 :                                 ZVAL_STRING(ret, (char*)data->children->content, 1);
     769             :                         }
     770           0 :                 } else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
     771           0 :                         ZVAL_STRING(ret, (char*)data->children->content, 1);
     772             :                 } else {
     773           0 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     774             :                 }
     775             :         } else {
     776           0 :                 ZVAL_EMPTY_STRING(ret);
     777             :         }
     778           0 :         return ret;
     779             : }
     780             : 
     781           0 : static zval *to_zval_base64(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
     782             : {
     783             :         zval *ret;
     784             :         char *str;
     785             :         int str_len;
     786             : 
     787           0 :         MAKE_STD_ZVAL(ret);
     788           0 :         FIND_XML_NULL(data, ret);
     789           0 :         if (data && data->children) {
     790           0 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     791           0 :                         whiteSpace_collapse(data->children->content);
     792           0 :                         str = (char*)php_base64_decode(data->children->content, strlen((char*)data->children->content), &str_len);
     793           0 :                         if (!str) {
     794           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     795             :                         }
     796           0 :                         ZVAL_STRINGL(ret, str, str_len, 0);
     797           0 :                 } else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
     798           0 :                         str = (char*)php_base64_decode(data->children->content, strlen((char*)data->children->content), &str_len);
     799           0 :                         if (!str) {
     800           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     801             :                         }
     802           0 :                         ZVAL_STRINGL(ret, str, str_len, 0);
     803             :                 } else {
     804           0 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     805             :                 }
     806             :         } else {
     807           0 :                 ZVAL_EMPTY_STRING(ret);
     808             :         }
     809           0 :         return ret;
     810             : }
     811             : 
     812           0 : static zval *to_zval_hexbin(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
     813             : {
     814             :         zval *ret;
     815             :         unsigned char *str;
     816             :         int str_len, i, j;
     817             :         unsigned char c;
     818             : 
     819           0 :         MAKE_STD_ZVAL(ret);
     820           0 :         FIND_XML_NULL(data, ret);
     821           0 :         if (data && data->children) {
     822           0 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     823           0 :                         whiteSpace_collapse(data->children->content);
     824           0 :                 } else if (data->children->type != XML_CDATA_SECTION_NODE || data->children->next != NULL) {
     825           0 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     826           0 :                         return ret;
     827             :                 }
     828           0 :                 str_len = strlen((char*)data->children->content) / 2;
     829           0 :                 str = emalloc(str_len+1);
     830           0 :                 for (i = j = 0; i < str_len; i++) {
     831           0 :                         c = data->children->content[j++];
     832           0 :                         if (c >= '0' && c <= '9') {
     833           0 :                                 str[i] = (c - '0') << 4;
     834           0 :                         } else if (c >= 'a' && c <= 'f') {
     835           0 :                                 str[i] = (c - 'a' + 10) << 4;
     836           0 :                         } else if (c >= 'A' && c <= 'F') {
     837           0 :                                 str[i] = (c - 'A' + 10) << 4;
     838             :                         } else {
     839           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     840             :                         }
     841           0 :                         c = data->children->content[j++];
     842           0 :                         if (c >= '0' && c <= '9') {
     843           0 :                                 str[i] |= c - '0';
     844           0 :                         } else if (c >= 'a' && c <= 'f') {
     845           0 :                                 str[i] |= c - 'a' + 10;
     846           0 :                         } else if (c >= 'A' && c <= 'F') {
     847           0 :                                 str[i] |= c - 'A' + 10;
     848             :                         } else {
     849           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     850             :                         }
     851             :                 }
     852           0 :                 str[str_len] = '\0';
     853           0 :                 ZVAL_STRINGL(ret, (char*)str, str_len, 0);
     854             :         } else {
     855           0 :                 ZVAL_EMPTY_STRING(ret);
     856             :         }
     857           0 :         return ret;
     858             : }
     859             : 
     860           0 : static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
     861             : {
     862             :         xmlNodePtr ret, text;
     863             :         char *str;
     864             :         int new_len;
     865             : 
     866           0 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     867           0 :         xmlAddChild(parent, ret);
     868           0 :         FIND_ZVAL_NULL(data, ret, style);
     869             : 
     870           0 :         if (Z_TYPE_P(data) == IS_STRING) {
     871           0 :                 str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
     872           0 :                 new_len = Z_STRLEN_P(data);
     873             :         } else {
     874           0 :                 zval tmp = *data;
     875             : 
     876             :                 zval_copy_ctor(&tmp);
     877           0 :                 convert_to_string(&tmp);
     878           0 :                 str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
     879           0 :                 new_len = Z_STRLEN(tmp);
     880             :                 zval_dtor(&tmp);
     881             :         }
     882             : 
     883           0 :         if (SOAP_GLOBAL(encoding) != NULL) {
     884           0 :                 xmlBufferPtr in  = xmlBufferCreateStatic(str, new_len);
     885           0 :                 xmlBufferPtr out = xmlBufferCreate();
     886           0 :                 int n = xmlCharEncInFunc(SOAP_GLOBAL(encoding), out, in);
     887             : 
     888           0 :                 if (n >= 0) {
     889           0 :                         efree(str);
     890           0 :                         str = estrdup((char*)xmlBufferContent(out));
     891           0 :                         new_len = n;
     892             :                 }
     893           0 :                 xmlBufferFree(out);
     894           0 :                 xmlBufferFree(in);
     895             :         }
     896             : 
     897           0 :         if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) {
     898           0 :                 char *err = emalloc(new_len + 8);
     899             :                 char c;
     900             :                 int i;
     901             :                 
     902           0 :                 memcpy(err, str, new_len+1);
     903           0 :                 for (i = 0; (c = err[i++]);) {
     904           0 :                         if ((c & 0x80) == 0) {
     905           0 :                         } else if ((c & 0xe0) == 0xc0) {
     906           0 :                                 if ((err[i] & 0xc0) != 0x80) {
     907           0 :                                         break;
     908             :                                 }
     909           0 :                                 i++;
     910           0 :                         } else if ((c & 0xf0) == 0xe0) {
     911           0 :                                 if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80) {
     912             :                                         break;
     913             :                                 }
     914           0 :                                 i += 2;
     915           0 :                         } else if ((c & 0xf8) == 0xf0) {
     916           0 :                                 if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80 || (err[i+2] & 0xc0) != 0x80) {
     917             :                                         break;
     918             :                                 }
     919           0 :                                 i += 3;
     920             :                         } else {
     921           0 :                                 break;
     922             :                         }
     923             :                 }
     924           0 :                 if (c) {
     925           0 :                         err[i-1] = '\\';
     926           0 :                         err[i++] = 'x';
     927           0 :                         err[i++] = ((unsigned char)c >> 4) + ((((unsigned char)c >> 4) > 9) ? ('a' - 10) : '0');
     928           0 :                         err[i++] = (c & 15) + (((c & 15) > 9) ? ('a' - 10) : '0');
     929           0 :                         err[i++] = '.';
     930           0 :                         err[i++] = '.';
     931           0 :                         err[i++] = '.';
     932           0 :                         err[i++] = 0;
     933             :                 }
     934             : 
     935           0 :                 soap_error1(E_ERROR,  "Encoding: string '%s' is not a valid utf-8 string", err);
     936             :         }
     937             : 
     938           0 :         text = xmlNewTextLen(BAD_CAST(str), new_len);
     939           0 :         xmlAddChild(ret, text);
     940           0 :         efree(str);
     941             : 
     942           0 :         if (style == SOAP_ENCODED) {
     943           0 :                 set_ns_and_type(ret, type);
     944             :         }
     945           0 :         return ret;
     946             : }
     947             : 
     948           0 : static xmlNodePtr to_xml_base64(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
     949             : {
     950             :         xmlNodePtr ret, text;
     951             :         unsigned char *str;
     952             :         int str_len;
     953             : 
     954           0 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     955           0 :         xmlAddChild(parent, ret);
     956           0 :         FIND_ZVAL_NULL(data, ret, style);
     957             : 
     958           0 :         if (Z_TYPE_P(data) == IS_STRING) {
     959           0 :                 str = php_base64_encode((unsigned char*)Z_STRVAL_P(data), Z_STRLEN_P(data), &str_len);
     960           0 :                 text = xmlNewTextLen(str, str_len);
     961           0 :                 xmlAddChild(ret, text);
     962           0 :                 efree(str);
     963             :         } else {
     964           0 :                 zval tmp = *data;
     965             : 
     966             :                 zval_copy_ctor(&tmp);
     967           0 :                 convert_to_string(&tmp);
     968           0 :                 str = php_base64_encode((unsigned char*)Z_STRVAL(tmp), Z_STRLEN(tmp), &str_len);
     969           0 :                 text = xmlNewTextLen(str, str_len);
     970           0 :                 xmlAddChild(ret, text);
     971           0 :                 efree(str);
     972             :                 zval_dtor(&tmp);
     973             :         }
     974             : 
     975           0 :         if (style == SOAP_ENCODED) {
     976           0 :                 set_ns_and_type(ret, type);
     977             :         }
     978           0 :         return ret;
     979             : }
     980             : 
     981           0 : static xmlNodePtr to_xml_hexbin(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
     982             : {
     983             :         static char hexconvtab[] = "0123456789ABCDEF";
     984             :         xmlNodePtr ret, text;
     985             :         unsigned char *str;
     986             :         zval tmp;
     987             :         int i, j;
     988             : 
     989           0 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     990           0 :         xmlAddChild(parent, ret);
     991           0 :         FIND_ZVAL_NULL(data, ret, style);
     992             : 
     993           0 :         if (Z_TYPE_P(data) != IS_STRING) {
     994           0 :                 tmp = *data;
     995             :                 zval_copy_ctor(&tmp);
     996           0 :                 convert_to_string(&tmp);
     997           0 :                 data = &tmp;
     998             :         }
     999           0 :         str = (unsigned char *) safe_emalloc(Z_STRLEN_P(data) * 2, sizeof(char), 1);
    1000             : 
    1001           0 :         for (i = j = 0; i < Z_STRLEN_P(data); i++) {
    1002           0 :                 str[j++] = hexconvtab[((unsigned char)Z_STRVAL_P(data)[i]) >> 4];
    1003           0 :                 str[j++] = hexconvtab[((unsigned char)Z_STRVAL_P(data)[i]) & 15];
    1004             :         }
    1005           0 :         str[j] = '\0';
    1006             : 
    1007           0 :         text = xmlNewTextLen(str, Z_STRLEN_P(data) * 2 * sizeof(char));
    1008           0 :         xmlAddChild(ret, text);
    1009           0 :         efree(str);
    1010           0 :         if (data == &tmp) {
    1011             :                 zval_dtor(&tmp);
    1012             :         }
    1013             : 
    1014           0 :         if (style == SOAP_ENCODED) {
    1015           0 :                 set_ns_and_type(ret, type);
    1016             :         }
    1017           0 :         return ret;
    1018             : }
    1019             : 
    1020           0 : static zval *to_zval_double(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1021             : {
    1022             :         zval *ret;
    1023           0 :         MAKE_STD_ZVAL(ret);
    1024           0 :         FIND_XML_NULL(data, ret);
    1025             : 
    1026           0 :         if (data && data->children) {
    1027           0 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
    1028             :                         long lval;
    1029             :                         double dval;
    1030             : 
    1031           0 :                         whiteSpace_collapse(data->children->content);
    1032           0 :                         switch (is_numeric_string((char*)data->children->content, strlen((char*)data->children->content), &lval, &dval, 0)) {
    1033             :                                 case IS_LONG:
    1034           0 :                                         Z_TYPE_P(ret) = IS_DOUBLE;
    1035           0 :                                         Z_DVAL_P(ret) = lval;
    1036           0 :                                         break;
    1037             :                                 case IS_DOUBLE:
    1038           0 :                                         Z_TYPE_P(ret) = IS_DOUBLE;
    1039           0 :                                         Z_DVAL_P(ret) = dval;
    1040           0 :                                         break;
    1041             :                                 default:
    1042           0 :                                         if (strncasecmp((char*)data->children->content, "NaN", sizeof("NaN")-1) == 0) {
    1043           0 :                                                 ZVAL_DOUBLE(ret, php_get_nan());
    1044           0 :                                         } else if (strncasecmp((char*)data->children->content, "INF", sizeof("INF")-1) == 0) {
    1045           0 :                                                 ZVAL_DOUBLE(ret, php_get_inf());
    1046           0 :                                         } else if (strncasecmp((char*)data->children->content, "-INF", sizeof("-INF")-1) == 0) {
    1047           0 :                                                 ZVAL_DOUBLE(ret, -php_get_inf());
    1048             :                                         } else {
    1049           0 :                                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    1050             :                                         }
    1051             :                         }
    1052             :                 } else {
    1053           0 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    1054             :                 }
    1055             :         } else {
    1056           0 :                 ZVAL_NULL(ret);
    1057             :         }
    1058           0 :         return ret;
    1059             : }
    1060             : 
    1061           0 : static zval *to_zval_long(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1062             : {
    1063             :         zval *ret;
    1064           0 :         MAKE_STD_ZVAL(ret);
    1065           0 :         FIND_XML_NULL(data, ret);
    1066             : 
    1067           0 :         if (data && data->children) {
    1068           0 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
    1069             :                         long lval;
    1070             :                         double dval;
    1071             : 
    1072           0 :                         whiteSpace_collapse(data->children->content);
    1073           0 :                         errno = 0;
    1074             : 
    1075           0 :                         switch ((Z_TYPE_P(ret) = is_numeric_string((char*)data->children->content, strlen((char*)data->children->content), &lval, &dval, 0))) {
    1076             :                                 case IS_LONG:
    1077           0 :                                         Z_LVAL_P(ret) = lval;
    1078           0 :                                         break;
    1079             :                                 case IS_DOUBLE:
    1080           0 :                                         Z_DVAL_P(ret) = dval;
    1081           0 :                                         break;
    1082             :                                 default:
    1083           0 :                                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    1084             :                         }
    1085             :                 } else {
    1086           0 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    1087             :                 }
    1088             :         } else {
    1089           0 :                 ZVAL_NULL(ret);
    1090             :         }
    1091           0 :         return ret;
    1092             : }
    1093             : 
    1094           0 : static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1095             : {
    1096             :         xmlNodePtr ret;
    1097             : 
    1098           0 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1099           0 :         xmlAddChild(parent, ret);
    1100           0 :         FIND_ZVAL_NULL(data, ret, style);
    1101             : 
    1102           0 :         if (Z_TYPE_P(data) == IS_DOUBLE) {
    1103             :                 char s[256];
    1104             : 
    1105           0 :                 snprintf(s, sizeof(s), "%0.0F",floor(Z_DVAL_P(data)));
    1106           0 :                 xmlNodeSetContent(ret, BAD_CAST(s));
    1107             :         } else {
    1108           0 :                 zval tmp = *data;
    1109             : 
    1110             :                 zval_copy_ctor(&tmp);
    1111           0 :                 if (Z_TYPE(tmp) != IS_LONG) {
    1112           0 :                         convert_to_long(&tmp);
    1113             :                 }
    1114           0 :                 convert_to_string(&tmp);
    1115           0 :                 xmlNodeSetContentLen(ret, BAD_CAST(Z_STRVAL(tmp)), Z_STRLEN(tmp));
    1116             :                 zval_dtor(&tmp);
    1117             :         }
    1118             : 
    1119           0 :         if (style == SOAP_ENCODED) {
    1120           0 :                 set_ns_and_type(ret, type);
    1121             :         }
    1122           0 :         return ret;
    1123             : }
    1124             : 
    1125           0 : static xmlNodePtr to_xml_double(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1126             : {
    1127             :         xmlNodePtr ret;
    1128             :         zval tmp;
    1129             :         char *str;
    1130             : 
    1131           0 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1132           0 :         xmlAddChild(parent, ret);
    1133           0 :         FIND_ZVAL_NULL(data, ret, style);
    1134             : 
    1135           0 :         tmp = *data;
    1136           0 :         if (Z_TYPE(tmp) != IS_DOUBLE) {
    1137             :                 zval_copy_ctor(&tmp);
    1138           0 :                 convert_to_double(&tmp);
    1139             :         }
    1140             :         
    1141           0 :         str = (char *) safe_emalloc(EG(precision), 1, MAX_LENGTH_OF_DOUBLE + 1);
    1142           0 :         php_gcvt(Z_DVAL(tmp), EG(precision), '.', 'E', str);
    1143           0 :         xmlNodeSetContentLen(ret, BAD_CAST(str), strlen(str));
    1144           0 :         efree(str);
    1145             : 
    1146           0 :         if (style == SOAP_ENCODED) {
    1147           0 :                 set_ns_and_type(ret, type);
    1148             :         }
    1149           0 :         return ret;
    1150             : }
    1151             : 
    1152           0 : static zval *to_zval_bool(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1153             : {
    1154             :         zval *ret;
    1155           0 :         MAKE_STD_ZVAL(ret);
    1156           0 :         FIND_XML_NULL(data, ret);
    1157             : 
    1158           0 :         if (data && data->children) {
    1159           0 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
    1160           0 :                         whiteSpace_collapse(data->children->content);
    1161           0 :                         if (stricmp((char*)data->children->content, "true") == 0 ||
    1162           0 :                                 stricmp((char*)data->children->content, "t") == 0 ||
    1163           0 :                                 strcmp((char*)data->children->content, "1") == 0) {
    1164           0 :                                 ZVAL_BOOL(ret, 1);
    1165           0 :                         } else if (stricmp((char*)data->children->content, "false") == 0 ||
    1166           0 :                                 stricmp((char*)data->children->content, "f") == 0 ||
    1167           0 :                                 strcmp((char*)data->children->content, "0") == 0) {
    1168           0 :                                 ZVAL_BOOL(ret, 0);
    1169             :                         } else {
    1170           0 :                                 ZVAL_STRING(ret, (char*)data->children->content, 1);
    1171           0 :                                 convert_to_boolean(ret);
    1172             :                         }
    1173             :                 } else {
    1174           0 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    1175             :                 }
    1176             :         } else {
    1177           0 :                 ZVAL_NULL(ret);
    1178             :         }
    1179           0 :         return ret;
    1180             : }
    1181             : 
    1182           0 : static xmlNodePtr to_xml_bool(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1183             : {
    1184             :         xmlNodePtr ret;
    1185             : 
    1186           0 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1187           0 :         xmlAddChild(parent, ret);
    1188           0 :         FIND_ZVAL_NULL(data, ret, style);
    1189             : 
    1190           0 :         if (zend_is_true(data TSRMLS_CC)) {
    1191           0 :                 xmlNodeSetContent(ret, BAD_CAST("true"));
    1192             :         } else {
    1193           0 :                 xmlNodeSetContent(ret, BAD_CAST("false"));
    1194             :         }
    1195             : 
    1196           0 :         if (style == SOAP_ENCODED) {
    1197           0 :                 set_ns_and_type(ret, type);
    1198             :         }
    1199           0 :         return ret;
    1200             : }
    1201             : 
    1202             : /* Null encode/decode */
    1203           0 : static zval *to_zval_null(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1204             : {
    1205             :         zval *ret;
    1206           0 :         MAKE_STD_ZVAL(ret);
    1207           0 :         ZVAL_NULL(ret);
    1208           0 :         return ret;
    1209             : }
    1210             : 
    1211           0 : static xmlNodePtr to_xml_null(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1212             : {
    1213             :         xmlNodePtr ret;
    1214             : 
    1215           0 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1216           0 :         xmlAddChild(parent, ret);
    1217           0 :         if (style == SOAP_ENCODED) {
    1218           0 :                 set_xsi_nil(ret);
    1219             :         }
    1220           0 :         return ret;
    1221             : }
    1222             : 
    1223           0 : static void set_zval_property(zval* object, char* name, zval* val TSRMLS_DC)
    1224             : {
    1225             :         zend_class_entry *old_scope;
    1226             : 
    1227           0 :         old_scope = EG(scope);
    1228           0 :         EG(scope) = Z_OBJCE_P(object);
    1229             :         Z_DELREF_P(val);
    1230           0 :         add_property_zval(object, name, val);
    1231           0 :         EG(scope) = old_scope;
    1232           0 : }
    1233             : 
    1234           0 : static zval* get_zval_property(zval* object, char* name TSRMLS_DC)
    1235             : {
    1236           0 :         if (Z_TYPE_P(object) == IS_OBJECT) {
    1237             :                 zval member;
    1238             :                 zval *data;
    1239             :                 zend_class_entry *old_scope;
    1240             : 
    1241           0 :                 INIT_PZVAL(&member);
    1242           0 :                 ZVAL_STRING(&member, name, 0);
    1243           0 :                 old_scope = EG(scope);
    1244           0 :                 EG(scope) = Z_OBJCE_P(object);
    1245           0 :                 data = Z_OBJ_HT_P(object)->read_property(object, &member, BP_VAR_IS, 0 TSRMLS_CC);
    1246           0 :                 if (data == EG(uninitialized_zval_ptr)) {
    1247             :                         /* Hack for bug #32455 */
    1248             :                         zend_property_info *property_info;
    1249             : 
    1250           0 :                         property_info = zend_get_property_info(Z_OBJCE_P(object), &member, 1 TSRMLS_CC);
    1251           0 :                         EG(scope) = old_scope;
    1252           0 :                         if (property_info && zend_hash_quick_exists(Z_OBJPROP_P(object), property_info->name, property_info->name_length+1, property_info->h)) {
    1253           0 :                                 return data;
    1254             :                         }
    1255           0 :                         return NULL;
    1256             :                 }
    1257           0 :                 EG(scope) = old_scope;
    1258           0 :                 return data;
    1259           0 :         } else if (Z_TYPE_P(object) == IS_ARRAY) {
    1260             :                 zval **data_ptr;
    1261             : 
    1262           0 :                 if (zend_hash_find(Z_ARRVAL_P(object), name, strlen(name)+1, (void**)&data_ptr) == SUCCESS) {
    1263           0 :                   return *data_ptr;
    1264             :                 }
    1265             :         }
    1266           0 :   return NULL;
    1267             : }
    1268             : 
    1269           0 : static void unset_zval_property(zval* object, char* name TSRMLS_DC)
    1270             : {
    1271           0 :         if (Z_TYPE_P(object) == IS_OBJECT) {
    1272             :                 zval member;
    1273             :                 zend_class_entry *old_scope;
    1274             : 
    1275           0 :                 INIT_PZVAL(&member);
    1276           0 :                 ZVAL_STRING(&member, name, 0);
    1277           0 :                 old_scope = EG(scope);
    1278           0 :                 EG(scope) = Z_OBJCE_P(object);
    1279           0 :                 Z_OBJ_HT_P(object)->unset_property(object, &member, 0 TSRMLS_CC);
    1280           0 :                 EG(scope) = old_scope;
    1281           0 :         } else if (Z_TYPE_P(object) == IS_ARRAY) {
    1282           0 :                 zend_hash_del(Z_ARRVAL_P(object), name, strlen(name)+1);
    1283             :         }
    1284           0 : }
    1285             : 
    1286           0 : static void model_to_zval_any(zval *ret, xmlNodePtr node TSRMLS_DC)
    1287             : {
    1288           0 :         zval* any = NULL;
    1289           0 :         char* name = NULL;
    1290             : 
    1291           0 :         while (node != NULL) {
    1292           0 :                 if (get_zval_property(ret, (char*)node->name TSRMLS_CC) == NULL) {
    1293           0 :                         zval* val = master_to_zval(get_conversion(XSD_ANYXML), node TSRMLS_CC);
    1294             :                         
    1295           0 :                         if (any && Z_TYPE_P(any) != IS_ARRAY) {
    1296             :                                 /* Convert into array */
    1297             :                                 zval *arr;
    1298             : 
    1299           0 :                                 MAKE_STD_ZVAL(arr);
    1300           0 :                                 array_init(arr);
    1301           0 :                                 if (name) {
    1302           0 :                                         add_assoc_zval(arr, name, any);
    1303             :                                 } else {
    1304           0 :                                         add_next_index_zval(arr, any);
    1305             :                                 }
    1306           0 :                                 any = arr;
    1307             :                         }
    1308             : 
    1309           0 :                         if (Z_TYPE_P(val) == IS_STRING && *Z_STRVAL_P(val) == '<') {
    1310           0 :                                 name = NULL;
    1311           0 :                                 while (node->next != NULL) {
    1312           0 :                                         zval* val2 = master_to_zval(get_conversion(XSD_ANYXML), node->next TSRMLS_CC);
    1313           0 :                                         if (Z_TYPE_P(val2) != IS_STRING ||  *Z_STRVAL_P(val) != '<') {
    1314             :                                                 break;
    1315             :                                         }
    1316           0 :                                         add_string_to_string(val, val, val2);
    1317           0 :                                         zval_ptr_dtor(&val2);
    1318           0 :                                         node = node->next;
    1319             :                                 }
    1320             :                         } else {
    1321           0 :                                 name = (char*)node->name;
    1322             :                         }
    1323             : 
    1324           0 :                         if (any == NULL) {
    1325           0 :                                 if (name) {
    1326             :                                         /* Convert into array */
    1327             :                                         zval *arr;
    1328             : 
    1329           0 :                                         MAKE_STD_ZVAL(arr);
    1330           0 :                                         array_init(arr);
    1331           0 :                                         add_assoc_zval(arr, name, val);
    1332           0 :                                         any = arr;
    1333           0 :                                         name = NULL;
    1334             :                                 } else {
    1335           0 :                                         any = val;
    1336             :                                 }
    1337             :                         } else {
    1338             :                                 /* Add array element */
    1339           0 :                                 if (name) {
    1340             :                                         zval **el;
    1341           0 :                                         if (zend_hash_find(Z_ARRVAL_P(any), name, strlen(name)+1, (void**)&el) == SUCCESS) {
    1342           0 :                                                 if (Z_TYPE_PP(el) != IS_ARRAY) {
    1343             :                                                         /* Convert into array */
    1344             :                                                         zval *arr;
    1345             :         
    1346           0 :                                                         MAKE_STD_ZVAL(arr);
    1347           0 :                                                         array_init(arr);
    1348           0 :                                                         add_next_index_zval(arr, *el);
    1349           0 :                                                         *el = arr;
    1350             :                                                 }
    1351           0 :                                                 add_next_index_zval(*el, val);
    1352             :                                         } else {
    1353           0 :                                                 add_assoc_zval(any, name, val);
    1354             :                                         }
    1355             :                                 } else {
    1356           0 :                                         add_next_index_zval(any, val);
    1357             :                                 }
    1358           0 :                                 name = NULL;
    1359             :                         }
    1360             :                 }
    1361           0 :                 node = node->next;
    1362             :         }
    1363           0 :         if (any) {
    1364           0 :                 set_zval_property(ret, name ? name : "any", any TSRMLS_CC);
    1365             :         }
    1366           0 : }
    1367             : 
    1368           0 : static void model_to_zval_object(zval *ret, sdlContentModelPtr model, xmlNodePtr data, sdlPtr sdl TSRMLS_DC)
    1369             : {
    1370           0 :         switch (model->kind) {
    1371             :                 case XSD_CONTENT_ELEMENT:
    1372           0 :                         if (model->u.element->name) {
    1373           0 :                                 xmlNodePtr node = get_node(data->children, model->u.element->name);
    1374             : 
    1375           0 :                                 if (node) {
    1376             :                                         zval *val;
    1377             :                                         xmlNodePtr r_node;
    1378             : 
    1379           0 :                                         r_node = check_and_resolve_href(node);
    1380           0 :                                         if (r_node && r_node->children && r_node->children->content) {
    1381           0 :                                                 if (model->u.element->fixed && strcmp(model->u.element->fixed, (char*)r_node->children->content) != 0) {
    1382           0 :                                                         soap_error3(E_ERROR, "Encoding: Element '%s' has fixed value '%s' (value '%s' is not allowed)", model->u.element->name, model->u.element->fixed, r_node->children->content);
    1383             :                                                 }
    1384           0 :                                                 val = master_to_zval(model->u.element->encode, r_node TSRMLS_CC);
    1385           0 :                                         } else if (model->u.element->fixed) {
    1386           0 :                                                 xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1387           0 :                                                 xmlNodeSetContent(dummy, BAD_CAST(model->u.element->fixed));
    1388           0 :                                                 val = master_to_zval(model->u.element->encode, dummy TSRMLS_CC);
    1389           0 :                                                 xmlFreeNode(dummy);
    1390           0 :                                         } else if (model->u.element->def && !model->u.element->nillable) {
    1391           0 :                                                 xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1392           0 :                                                 xmlNodeSetContent(dummy, BAD_CAST(model->u.element->def));
    1393           0 :                                                 val = master_to_zval(model->u.element->encode, dummy TSRMLS_CC);
    1394           0 :                                                 xmlFreeNode(dummy);
    1395             :                                         } else {
    1396           0 :                                                 val = master_to_zval(model->u.element->encode, r_node TSRMLS_CC);
    1397             :                                         }
    1398           0 :                                         if ((node = get_node(node->next, model->u.element->name)) != NULL) {
    1399             :                                                 zval *array;
    1400             : 
    1401           0 :                                                 MAKE_STD_ZVAL(array);
    1402           0 :                                                 array_init(array);
    1403           0 :                                                 add_next_index_zval(array, val);
    1404             :                                                 do {
    1405           0 :                                                         if (node && node->children && node->children->content) {
    1406           0 :                                                                 if (model->u.element->fixed && strcmp(model->u.element->fixed, (char*)node->children->content) != 0) {
    1407           0 :                                                                         soap_error3(E_ERROR, "Encoding: Element '%s' has fixed value '%s' (value '%s' is not allowed)", model->u.element->name, model->u.element->fixed, node->children->content);
    1408             :                                                                 }
    1409           0 :                                                                 val = master_to_zval(model->u.element->encode, node TSRMLS_CC);
    1410           0 :                                                         } else if (model->u.element->fixed) {
    1411           0 :                                                                 xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1412           0 :                                                                 xmlNodeSetContent(dummy, BAD_CAST(model->u.element->fixed));
    1413           0 :                                                                 val = master_to_zval(model->u.element->encode, dummy TSRMLS_CC);
    1414           0 :                                                                 xmlFreeNode(dummy);
    1415           0 :                                                         } else if (model->u.element->def && !model->u.element->nillable) {
    1416           0 :                                                                 xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1417           0 :                                                                 xmlNodeSetContent(dummy, BAD_CAST(model->u.element->def));
    1418           0 :                                                                 val = master_to_zval(model->u.element->encode, dummy TSRMLS_CC);
    1419           0 :                                                                 xmlFreeNode(dummy);
    1420             :                                                         } else {
    1421           0 :                                                                 val = master_to_zval(model->u.element->encode, node TSRMLS_CC);
    1422             :                                                         }
    1423           0 :                                                         add_next_index_zval(array, val);
    1424           0 :                                                 } while ((node = get_node(node->next, model->u.element->name)) != NULL);
    1425           0 :                                                 val = array;
    1426           0 :                                         } else if ((Z_TYPE_P(val) != IS_NULL || !model->u.element->nillable) &&
    1427           0 :                                                    (SOAP_GLOBAL(features) & SOAP_SINGLE_ELEMENT_ARRAYS) &&
    1428           0 :                                                    (model->max_occurs == -1 || model->max_occurs > 1)) {
    1429             :                                                 zval *array;
    1430             : 
    1431           0 :                                                 MAKE_STD_ZVAL(array);
    1432           0 :                                                 array_init(array);
    1433           0 :                                                 add_next_index_zval(array, val);
    1434           0 :                                                 val = array;
    1435             :                                         }
    1436           0 :                                         set_zval_property(ret, model->u.element->name, val TSRMLS_CC);
    1437             :                                 }
    1438             :                         }
    1439           0 :                         break;
    1440             :                 case XSD_CONTENT_ALL:
    1441             :                 case XSD_CONTENT_SEQUENCE:
    1442             :                 case XSD_CONTENT_CHOICE: {
    1443             :                         sdlContentModelPtr *tmp;
    1444             :                         HashPosition pos;
    1445           0 :                         sdlContentModelPtr any = NULL;
    1446             : 
    1447           0 :                         zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
    1448           0 :                         while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
    1449           0 :                                 if ((*tmp)->kind == XSD_CONTENT_ANY) {
    1450           0 :                                         any = *tmp;
    1451             :                                 } else {
    1452           0 :                                         model_to_zval_object(ret, *tmp, data, sdl TSRMLS_CC);
    1453             :                                 }
    1454           0 :                                 zend_hash_move_forward_ex(model->u.content, &pos);
    1455             :                         }
    1456           0 :                         if (any) {
    1457           0 :                                 model_to_zval_any(ret, data->children TSRMLS_CC);
    1458             :                         }
    1459           0 :                         break;
    1460             :                 }
    1461             :                 case XSD_CONTENT_GROUP:
    1462           0 :                         model_to_zval_object(ret, model->u.group->model, data, sdl TSRMLS_CC);
    1463             :                         break;
    1464             :                 default:
    1465             :                   break;
    1466             :         }
    1467           0 : }
    1468             : 
    1469             : /* Struct encode/decode */
    1470           0 : static zval *to_zval_object_ex(encodeTypePtr type, xmlNodePtr data, zend_class_entry *pce TSRMLS_DC)
    1471             : {
    1472             :         zval *ret;
    1473             :         xmlNodePtr trav;
    1474             :         sdlPtr sdl;
    1475           0 :         sdlTypePtr sdlType = type->sdl_type;
    1476           0 :         zend_class_entry *ce = ZEND_STANDARD_CLASS_DEF_PTR;
    1477           0 :         zval *redo_any = NULL;
    1478             : 
    1479           0 :         if (pce) {
    1480           0 :                 ce = pce;
    1481           0 :         } else if (SOAP_GLOBAL(class_map) && type->type_str) {
    1482             :                 zval             **classname;
    1483             :                 zend_class_entry  *tmp;
    1484             : 
    1485           0 :                 if (zend_hash_find(SOAP_GLOBAL(class_map), type->type_str, strlen(type->type_str)+1, (void**)&classname) == SUCCESS &&
    1486           0 :                     Z_TYPE_PP(classname) == IS_STRING &&
    1487           0 :                     (tmp = zend_fetch_class(Z_STRVAL_PP(classname), Z_STRLEN_PP(classname), ZEND_FETCH_CLASS_AUTO TSRMLS_CC)) != NULL) {
    1488           0 :                         ce = tmp;
    1489             :                 }
    1490             :         }
    1491           0 :         sdl = SOAP_GLOBAL(sdl);
    1492           0 :         if (sdlType) {
    1493           0 :                 if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
    1494           0 :                     sdlType->encode && type != &sdlType->encode->details) {
    1495             :                         encodePtr enc;
    1496             : 
    1497           0 :                         enc = sdlType->encode;
    1498           0 :                         while (enc && enc->details.sdl_type &&
    1499           0 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
    1500           0 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
    1501           0 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
    1502           0 :                                 enc = enc->details.sdl_type->encode;
    1503             :                         }
    1504           0 :                         if (enc) {
    1505             :                                 zval *base;
    1506             : 
    1507           0 :                                 ALLOC_INIT_ZVAL(ret);
    1508           0 :                                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1509           0 :                                         return ret;
    1510             :                                 }
    1511             : 
    1512           0 :                                 object_init_ex(ret, ce);
    1513           0 :                                 base = master_to_zval_int(enc, data TSRMLS_CC);
    1514           0 :                                 set_zval_property(ret, "_", base TSRMLS_CC);
    1515             :                         } else {
    1516           0 :                                 ALLOC_INIT_ZVAL(ret);
    1517           0 :                                 FIND_XML_NULL(data, ret);
    1518           0 :                                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1519           0 :                                         return ret;
    1520             :                                 }
    1521           0 :                                 object_init_ex(ret, ce);
    1522             :                         }
    1523           0 :                 } else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
    1524           0 :                            sdlType->encode &&
    1525           0 :                            type != &sdlType->encode->details) {
    1526           0 :                         if (sdlType->encode->details.sdl_type &&
    1527           0 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
    1528           0 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
    1529           0 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
    1530             : 
    1531           0 :                                 CHECK_XML_NULL(data);
    1532           0 :                                 if ((ret = soap_find_xml_ref(data TSRMLS_CC)) != NULL) {
    1533           0 :                                         return ret;
    1534             :                                 }
    1535             : 
    1536           0 :                             if (ce != ZEND_STANDARD_CLASS_DEF_PTR &&
    1537           0 :                                 sdlType->encode->to_zval == sdl_guess_convert_zval &&
    1538           0 :                                 sdlType->encode->details.sdl_type != NULL &&
    1539           0 :                                 (sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_COMPLEX ||
    1540           0 :                                  sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_RESTRICTION ||
    1541           0 :                                  sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_EXTENSION) &&
    1542           0 :                                 (sdlType->encode->details.sdl_type->encode == NULL ||
    1543           0 :                                  (sdlType->encode->details.sdl_type->encode->details.type != IS_ARRAY &&
    1544           0 :                                   sdlType->encode->details.sdl_type->encode->details.type != SOAP_ENC_ARRAY))) {
    1545           0 :                                         ret = to_zval_object_ex(&sdlType->encode->details, data, ce TSRMLS_CC);
    1546             :                             } else {
    1547           0 :                                         ret = master_to_zval_int(sdlType->encode, data TSRMLS_CC);
    1548             :                                 }
    1549           0 :                                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1550           0 :                                         return ret;
    1551             :                                 }
    1552           0 :                                 redo_any = get_zval_property(ret, "any" TSRMLS_CC);
    1553           0 :                                 if (Z_TYPE_P(ret) == IS_OBJECT && ce != ZEND_STANDARD_CLASS_DEF_PTR) {
    1554           0 :                                         zend_object *zobj = zend_objects_get_address(ret TSRMLS_CC);
    1555           0 :                                         zobj->ce = ce;
    1556             :                                 }
    1557             :                         } else {
    1558             :                                 zval *base;
    1559             : 
    1560           0 :                                 ALLOC_INIT_ZVAL(ret);
    1561           0 :                                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1562           0 :                                         return ret;
    1563             :                                 }
    1564             : 
    1565           0 :                                 object_init_ex(ret, ce);
    1566           0 :                                 base = master_to_zval_int(sdlType->encode, data TSRMLS_CC);
    1567           0 :                                 set_zval_property(ret, "_", base TSRMLS_CC);
    1568             :                         }
    1569             :                 } else {
    1570           0 :                         ALLOC_INIT_ZVAL(ret);
    1571           0 :                         FIND_XML_NULL(data, ret);
    1572           0 :                         if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1573           0 :                                 return ret;
    1574             :                         }
    1575           0 :                         object_init_ex(ret, ce);
    1576             :                 }
    1577           0 :                 if (sdlType->model) {
    1578           0 :                         if (redo_any) {
    1579           0 :                                 Z_ADDREF_P(redo_any);
    1580           0 :                                 unset_zval_property(ret, "any" TSRMLS_CC);
    1581             :                         }
    1582           0 :                         model_to_zval_object(ret, sdlType->model, data, sdl TSRMLS_CC);
    1583           0 :                         if (redo_any) {
    1584           0 :                                 zval *tmp = get_zval_property(ret, "any" TSRMLS_CC);
    1585             : 
    1586           0 :                                 if (tmp == NULL) {
    1587           0 :                                         model_to_zval_any(ret, data->children TSRMLS_CC);
    1588           0 :                                 } else if (Z_REFCOUNT_P(tmp) == 0) {
    1589             :                                         zval_dtor(tmp);
    1590           0 :                                         efree(tmp);
    1591             :                                 }
    1592           0 :                                 zval_ptr_dtor(&redo_any);
    1593             :                         }
    1594             :                 }
    1595           0 :                 if (sdlType->attributes) {
    1596             :                         sdlAttributePtr *attr;
    1597             :                         HashPosition pos;
    1598             : 
    1599           0 :                         zend_hash_internal_pointer_reset_ex(sdlType->attributes, &pos);
    1600           0 :                         while (zend_hash_get_current_data_ex(sdlType->attributes, (void**)&attr, &pos) == SUCCESS) {
    1601           0 :                                 if ((*attr)->name) {
    1602           0 :                                         xmlAttrPtr val = get_attribute(data->properties, (*attr)->name);
    1603           0 :                                         char *str_val = NULL;
    1604             : 
    1605           0 :                                         if (val && val->children && val->children->content) {
    1606           0 :                                                 str_val = (char*)val->children->content;
    1607           0 :                                                 if ((*attr)->fixed && strcmp((*attr)->fixed, str_val) != 0) {
    1608           0 :                                                         soap_error3(E_ERROR, "Encoding: Attribute '%s' has fixed value '%s' (value '%s' is not allowed)", (*attr)->name, (*attr)->fixed, str_val);
    1609             :                                                 }
    1610           0 :                                         } else if ((*attr)->fixed) {
    1611           0 :                                                 str_val = (*attr)->fixed;
    1612           0 :                                         } else if ((*attr)->def) {
    1613           0 :                                                 str_val = (*attr)->def;
    1614             :                                         }
    1615           0 :                                         if (str_val) {
    1616             :                                                 xmlNodePtr dummy, text;
    1617             :                                                 zval *data;
    1618             : 
    1619           0 :                                                 dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1620           0 :                                                 text = xmlNewText(BAD_CAST(str_val));
    1621           0 :                                                 xmlAddChild(dummy, text);
    1622           0 :                                                 data = master_to_zval((*attr)->encode, dummy TSRMLS_CC);
    1623           0 :                                                 xmlFreeNode(dummy);
    1624           0 :                                                 set_zval_property(ret, (*attr)->name, data TSRMLS_CC);
    1625             :                                         }
    1626             :                                 }
    1627           0 :                                 zend_hash_move_forward_ex(sdlType->attributes, &pos);
    1628             :                         }
    1629             :                 }
    1630             :         } else {
    1631             : 
    1632           0 :                 ALLOC_INIT_ZVAL(ret);
    1633           0 :                 FIND_XML_NULL(data, ret);
    1634           0 :                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1635           0 :                         return ret;
    1636             :                 }
    1637             : 
    1638           0 :                 object_init_ex(ret, ce);
    1639           0 :                 trav = data->children;
    1640             : 
    1641           0 :                 while (trav != NULL) {
    1642           0 :                         if (trav->type == XML_ELEMENT_NODE) {
    1643             :                                 zval  *tmpVal;
    1644             :                                 zval *prop;
    1645             : 
    1646           0 :                                 tmpVal = master_to_zval(NULL, trav TSRMLS_CC);
    1647             : 
    1648           0 :                                 prop = get_zval_property(ret, (char*)trav->name TSRMLS_CC);
    1649           0 :                                 if (!prop) {
    1650           0 :                                         if (!trav->next || !get_node(trav->next, (char*)trav->name)) {
    1651           0 :                                                 set_zval_property(ret, (char*)trav->name, tmpVal TSRMLS_CC);
    1652             :                                         } else {
    1653             :                                                 zval *arr;
    1654             : 
    1655           0 :                                                 MAKE_STD_ZVAL(arr);
    1656           0 :                                                 array_init(arr);
    1657           0 :                                                 add_next_index_zval(arr, tmpVal);
    1658           0 :                                                 set_zval_property(ret, (char*)trav->name, arr TSRMLS_CC);
    1659             :                                         }
    1660             :                                 } else {
    1661             :                                         /* Property already exist - make array */
    1662           0 :                                         if (Z_TYPE_P(prop) != IS_ARRAY) {
    1663             :                                                 /* Convert into array */
    1664             :                                                 zval *arr;
    1665             : 
    1666           0 :                                                 MAKE_STD_ZVAL(arr);
    1667           0 :                                                 array_init(arr);
    1668             :                                                 Z_ADDREF_P(prop);
    1669           0 :                                                 add_next_index_zval(arr, prop);
    1670           0 :                                                 set_zval_property(ret, (char*)trav->name, arr TSRMLS_CC);
    1671           0 :                                                 prop = arr;
    1672             :                                         }
    1673             :                                         /* Add array element */
    1674           0 :                                         add_next_index_zval(prop, tmpVal);
    1675             :                                 }
    1676             :                         }
    1677           0 :                         trav = trav->next;
    1678             :                 }
    1679             :         }
    1680           0 :         return ret;
    1681             : }
    1682             : 
    1683           0 : static zval *to_zval_object(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1684             : {
    1685           0 :         return to_zval_object_ex(type, data, NULL TSRMLS_CC);
    1686             : }
    1687             : 
    1688             : 
    1689           0 : static int model_to_xml_object(xmlNodePtr node, sdlContentModelPtr model, zval *object, int style, int strict TSRMLS_DC)
    1690             : {
    1691           0 :         switch (model->kind) {
    1692             :                 case XSD_CONTENT_ELEMENT: {
    1693             :                         zval *data;
    1694             :                         xmlNodePtr property;
    1695             :                         encodePtr enc;
    1696             : 
    1697           0 :                         data = get_zval_property(object, model->u.element->name TSRMLS_CC);
    1698           0 :                         if (data &&
    1699           0 :                             Z_TYPE_P(data) == IS_NULL &&
    1700           0 :                             !model->u.element->nillable &&
    1701           0 :                             model->min_occurs > 0 &&
    1702             :                             !strict) {
    1703           0 :                                 return 0;
    1704             :                         }
    1705           0 :                         if (data) {
    1706           0 :                                 enc = model->u.element->encode;
    1707           0 :                                 if ((model->max_occurs == -1 || model->max_occurs > 1) &&
    1708           0 :                                     Z_TYPE_P(data) == IS_ARRAY &&
    1709           0 :                                     !is_map(data)) {
    1710           0 :                                         HashTable *ht = Z_ARRVAL_P(data);
    1711             :                                         zval **val;
    1712             : 
    1713           0 :                                         zend_hash_internal_pointer_reset(ht);
    1714           0 :                                         while (zend_hash_get_current_data(ht,(void**)&val) == SUCCESS) {
    1715           0 :                                                 if (Z_TYPE_PP(val) == IS_NULL && model->u.element->nillable) {
    1716           0 :                                                         property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1717           0 :                                                         xmlAddChild(node, property);
    1718           0 :                                                         set_xsi_nil(property);
    1719             :                                                 } else {
    1720           0 :                                                         property = master_to_xml(enc, *val, style, node TSRMLS_CC);
    1721           0 :                                                         if (property->children && property->children->content &&
    1722           0 :                                                             model->u.element->fixed && strcmp(model->u.element->fixed, (char*)property->children->content) != 0) {
    1723           0 :                                                                 soap_error3(E_ERROR, "Encoding: Element '%s' has fixed value '%s' (value '%s' is not allowed)", model->u.element->name, model->u.element->fixed, property->children->content);
    1724             :                                                         }
    1725             :                                                 }
    1726           0 :                                                 xmlNodeSetName(property, BAD_CAST(model->u.element->name));
    1727           0 :                                                 if (style == SOAP_LITERAL &&
    1728           0 :                                                     model->u.element->namens &&
    1729           0 :                                                     model->u.element->form == XSD_FORM_QUALIFIED) {
    1730           0 :                                                         xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
    1731           0 :                                                         xmlSetNs(property, nsp);
    1732             :                                                 }
    1733           0 :                                                 zend_hash_move_forward(ht);
    1734             :                                         }
    1735             :                                 } else {
    1736           0 :                                         if (Z_TYPE_P(data) == IS_NULL && model->u.element->nillable) {
    1737           0 :                                                 property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1738           0 :                                                 xmlAddChild(node, property);
    1739           0 :                                                 set_xsi_nil(property);
    1740           0 :                                         } else if (Z_TYPE_P(data) == IS_NULL && model->min_occurs == 0) {
    1741           0 :                                                 return 1;
    1742             :                                         } else {
    1743           0 :                                                 property = master_to_xml(enc, data, style, node TSRMLS_CC);
    1744           0 :                                                 if (property->children && property->children->content &&
    1745           0 :                                                     model->u.element->fixed && strcmp(model->u.element->fixed, (char*)property->children->content) != 0) {
    1746           0 :                                                         soap_error3(E_ERROR, "Encoding: Element '%s' has fixed value '%s' (value '%s' is not allowed)", model->u.element->name, model->u.element->fixed, property->children->content);
    1747             :                                                 }
    1748             :                                         }
    1749           0 :                                         xmlNodeSetName(property, BAD_CAST(model->u.element->name));
    1750           0 :                                         if (style == SOAP_LITERAL &&
    1751           0 :                                             model->u.element->namens &&
    1752           0 :                                             model->u.element->form == XSD_FORM_QUALIFIED) {
    1753           0 :                                                 xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
    1754           0 :                                                 xmlSetNs(property, nsp);
    1755             :                                         }
    1756             :                                 }
    1757           0 :                                 return 1;
    1758           0 :                         } else if (strict && model->u.element->nillable && model->min_occurs > 0) {
    1759           0 :                                 property = xmlNewNode(NULL, BAD_CAST(model->u.element->name));
    1760           0 :                                 xmlAddChild(node, property);
    1761           0 :                                 set_xsi_nil(property);
    1762           0 :                                 if (style == SOAP_LITERAL &&
    1763           0 :                                     model->u.element->namens &&
    1764           0 :                                     model->u.element->form == XSD_FORM_QUALIFIED) {
    1765           0 :                                         xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
    1766           0 :                                         xmlSetNs(property, nsp);
    1767             :                                 }
    1768           0 :                                 return 1;
    1769           0 :                         } else if (model->min_occurs == 0) {
    1770           0 :                                 return 2;
    1771             :                         } else {
    1772           0 :                                 if (strict) {
    1773           0 :                                         soap_error1(E_ERROR,  "Encoding: object has no '%s' property", model->u.element->name);
    1774             :                                 }
    1775           0 :                                 return 0;
    1776             :                         }
    1777             :                         break;
    1778             :                 }
    1779             :                 case XSD_CONTENT_ANY: {
    1780             :                         zval *data;
    1781             :                         xmlNodePtr property;
    1782             :                         encodePtr enc;
    1783             : 
    1784           0 :                         data = get_zval_property(object, "any" TSRMLS_CC);
    1785           0 :                         if (data) {
    1786           0 :                                 enc = get_conversion(XSD_ANYXML);
    1787           0 :                                 if ((model->max_occurs == -1 || model->max_occurs > 1) &&
    1788           0 :                                     Z_TYPE_P(data) == IS_ARRAY &&
    1789           0 :                                     !is_map(data)) {
    1790           0 :                                         HashTable *ht = Z_ARRVAL_P(data);
    1791             :                                         zval **val;
    1792             : 
    1793           0 :                                         zend_hash_internal_pointer_reset(ht);
    1794           0 :                                         while (zend_hash_get_current_data(ht,(void**)&val) == SUCCESS) {
    1795           0 :                                                 property = master_to_xml(enc, *val, style, node TSRMLS_CC);
    1796           0 :                                                 zend_hash_move_forward(ht);
    1797             :                                         }
    1798             :                                 } else {
    1799           0 :                                         property = master_to_xml(enc, data, style, node TSRMLS_CC);
    1800             :                                 }
    1801           0 :                                 return 1;
    1802           0 :                         } else if (model->min_occurs == 0) {
    1803           0 :                                 return 2;
    1804             :                         } else {
    1805           0 :                                 if (strict) {
    1806           0 :                                         soap_error0(E_ERROR,  "Encoding: object has no 'any' property");
    1807             :                                 }
    1808           0 :                                 return 0;
    1809             :                         }
    1810             :                         break;
    1811             :                 }
    1812             :                 case XSD_CONTENT_SEQUENCE:
    1813             :                 case XSD_CONTENT_ALL: {
    1814             :                         sdlContentModelPtr *tmp;
    1815             :                         HashPosition pos;
    1816             : 
    1817           0 :                         zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
    1818           0 :                         while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
    1819           0 :                                 if (!model_to_xml_object(node, *tmp, object, style, strict && ((*tmp)->min_occurs > 0) TSRMLS_CC)) {
    1820           0 :                                         if (!strict || (*tmp)->min_occurs > 0) {
    1821           0 :                                                 return 0;
    1822             :                                         }
    1823             :                                 }
    1824           0 :                                 strict = 1;
    1825           0 :                                 zend_hash_move_forward_ex(model->u.content, &pos);
    1826             :                         }
    1827           0 :                         return 1;
    1828             :                 }
    1829             :                 case XSD_CONTENT_CHOICE: {
    1830             :                         sdlContentModelPtr *tmp;
    1831             :                         HashPosition pos;
    1832           0 :                         int ret = 0;
    1833             : 
    1834           0 :                         zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
    1835           0 :                         while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
    1836           0 :                                 int tmp_ret = model_to_xml_object(node, *tmp, object, style, 0 TSRMLS_CC);
    1837           0 :                                 if (tmp_ret == 1) {
    1838           0 :                                         return 1;
    1839           0 :                                 } else if (tmp_ret != 0) {
    1840           0 :                                         ret = 1;
    1841             :                                 }
    1842           0 :                                 zend_hash_move_forward_ex(model->u.content, &pos);
    1843             :                         }
    1844           0 :                         return ret;
    1845             :                 }
    1846             :                 case XSD_CONTENT_GROUP: {
    1847           0 :                         return model_to_xml_object(node, model->u.group->model, object, style, strict && model->min_occurs > 0 TSRMLS_CC);
    1848             :                 }
    1849             :                 default:
    1850             :                   break;
    1851             :         }
    1852           0 :         return 1;
    1853             : }
    1854             : 
    1855           0 : static sdlTypePtr model_array_element(sdlContentModelPtr model)
    1856             : {
    1857           0 :         switch (model->kind) {
    1858             :                 case XSD_CONTENT_ELEMENT: {
    1859           0 :                         if (model->max_occurs == -1 || model->max_occurs > 1) {
    1860           0 :                           return model->u.element;
    1861             :                         } else {
    1862           0 :                           return NULL;
    1863             :                         }
    1864             :                 }
    1865             :                 case XSD_CONTENT_SEQUENCE:
    1866             :                 case XSD_CONTENT_ALL:
    1867             :                 case XSD_CONTENT_CHOICE: {
    1868             :                         sdlContentModelPtr *tmp;
    1869             :                         HashPosition pos;
    1870             : 
    1871           0 :                         if (zend_hash_num_elements(model->u.content) != 1) {
    1872           0 :                           return NULL;
    1873             :                         }
    1874           0 :                         zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
    1875           0 :                         zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos);
    1876           0 :                         return model_array_element(*tmp);
    1877             :                 }
    1878             :                 case XSD_CONTENT_GROUP: {
    1879           0 :                         return model_array_element(model->u.group->model);
    1880             :                 }
    1881             :                 default:
    1882             :                   break;
    1883             :         }
    1884           0 :         return NULL;
    1885             : }
    1886             : 
    1887           0 : static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1888             : {
    1889             :         xmlNodePtr xmlParam;
    1890           0 :         HashTable *prop = NULL;
    1891             :         int i;
    1892           0 :         sdlTypePtr sdlType = type->sdl_type;
    1893             : 
    1894           0 :         if (!data || Z_TYPE_P(data) == IS_NULL) {
    1895           0 :                 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1896           0 :                 xmlAddChild(parent, xmlParam);
    1897           0 :                 if (style == SOAP_ENCODED) {
    1898           0 :                         set_xsi_nil(xmlParam);
    1899           0 :                         set_ns_and_type(xmlParam, type);
    1900             :                 }
    1901           0 :                 return xmlParam;
    1902             :         }
    1903             : 
    1904           0 :         if (Z_TYPE_P(data) == IS_OBJECT) {
    1905           0 :                 prop = Z_OBJPROP_P(data);
    1906           0 :         } else if (Z_TYPE_P(data) == IS_ARRAY) {
    1907           0 :                 prop = Z_ARRVAL_P(data);
    1908             :         }
    1909             : 
    1910           0 :         if (sdlType) {
    1911           0 :                 if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
    1912           0 :                     sdlType->encode && type != &sdlType->encode->details) {
    1913             :                         encodePtr enc;
    1914             : 
    1915           0 :                         enc = sdlType->encode;
    1916           0 :                         while (enc && enc->details.sdl_type &&
    1917           0 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
    1918           0 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
    1919           0 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
    1920           0 :                                 enc = enc->details.sdl_type->encode;
    1921             :                         }
    1922           0 :                         if (enc) {
    1923           0 :                                 zval *tmp = get_zval_property(data, "_" TSRMLS_CC);
    1924           0 :                                 if (tmp) {
    1925           0 :                                         xmlParam = master_to_xml(enc, tmp, style, parent TSRMLS_CC);
    1926           0 :                                 } else if (prop == NULL) {
    1927           0 :                                         xmlParam = master_to_xml(enc, data, style, parent TSRMLS_CC);
    1928             :                                 } else {
    1929           0 :                                         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1930           0 :                                         xmlAddChild(parent, xmlParam);
    1931             :                                 }
    1932             :                         } else {
    1933           0 :                                 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1934           0 :                                 xmlAddChild(parent, xmlParam);
    1935             :                         }
    1936           0 :                 } else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
    1937           0 :                            sdlType->encode && type != &sdlType->encode->details) {
    1938           0 :                         if (sdlType->encode->details.sdl_type &&
    1939           0 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
    1940           0 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
    1941           0 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
    1942             : 
    1943           0 :                                 if (prop) prop->nApplyCount++;
    1944           0 :                                 xmlParam = master_to_xml(sdlType->encode, data, style, parent TSRMLS_CC);
    1945           0 :                                 if (prop) prop->nApplyCount--;
    1946             :                         } else {
    1947           0 :                                 zval *tmp = get_zval_property(data, "_" TSRMLS_CC);
    1948             : 
    1949           0 :                                 if (tmp) {
    1950           0 :                                         xmlParam = master_to_xml(sdlType->encode, tmp, style, parent TSRMLS_CC);
    1951           0 :                                 } else if (prop == NULL) {
    1952           0 :                                         xmlParam = master_to_xml(sdlType->encode, data, style, parent TSRMLS_CC);
    1953             :                                 } else {
    1954           0 :                                         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1955           0 :                                         xmlAddChild(parent, xmlParam);
    1956             :                                 }
    1957             :                         }
    1958             :                 } else {
    1959           0 :                         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1960           0 :                         xmlAddChild(parent, xmlParam);
    1961             :                 }
    1962             : 
    1963           0 :                 if (soap_check_zval_ref(data, xmlParam TSRMLS_CC)) {
    1964           0 :                         return xmlParam;
    1965             :                 }
    1966           0 :                 if (prop != NULL) {
    1967             :                         sdlTypePtr array_el;
    1968             : 
    1969           0 :                         if (Z_TYPE_P(data) == IS_ARRAY &&
    1970           0 :                       !is_map(data) &&
    1971           0 :                       sdlType->attributes == NULL &&
    1972           0 :                       sdlType->model != NULL &&
    1973           0 :                       (array_el = model_array_element(sdlType->model)) != NULL) {
    1974             :                                 zval **val;
    1975             : 
    1976           0 :                                 zend_hash_internal_pointer_reset(prop);
    1977           0 :                                 while (zend_hash_get_current_data(prop,(void**)&val) == SUCCESS) {
    1978             :                                         xmlNodePtr property;
    1979           0 :                                         if (Z_TYPE_PP(val) == IS_NULL && array_el->nillable) {
    1980           0 :                                                 property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1981           0 :                                                 xmlAddChild(xmlParam, property);
    1982           0 :                                                 set_xsi_nil(property);
    1983             :                                         } else {
    1984           0 :                                                 property = master_to_xml(array_el->encode, *val, style, xmlParam TSRMLS_CC);
    1985             :                                         }
    1986           0 :                                         xmlNodeSetName(property, BAD_CAST(array_el->name));
    1987           0 :                                         if (style == SOAP_LITERAL &&
    1988           0 :                                            array_el->namens &&
    1989           0 :                                            array_el->form == XSD_FORM_QUALIFIED) {
    1990           0 :                                                 xmlNsPtr nsp = encode_add_ns(property, array_el->namens);
    1991           0 :                                                 xmlSetNs(property, nsp);
    1992             :                                         }
    1993           0 :                                         zend_hash_move_forward(prop);
    1994             :                                 }
    1995           0 :                         } else if (sdlType->model) {
    1996           0 :                                 model_to_xml_object(xmlParam, sdlType->model, data, style, 1 TSRMLS_CC);
    1997             :                         }
    1998           0 :                         if (sdlType->attributes) {
    1999             :                                 sdlAttributePtr *attr;
    2000             :                                 zval *zattr;
    2001             :                                 HashPosition pos;
    2002             : 
    2003           0 :                                 zend_hash_internal_pointer_reset_ex(sdlType->attributes, &pos);
    2004           0 :                                 while (zend_hash_get_current_data_ex(sdlType->attributes, (void**)&attr, &pos) == SUCCESS) {
    2005           0 :                                         if ((*attr)->name) {
    2006           0 :                                                 zattr = get_zval_property(data, (*attr)->name TSRMLS_CC);
    2007           0 :                                                 if (zattr) {
    2008             :                                                         xmlNodePtr dummy;
    2009             : 
    2010           0 :                                                         dummy = master_to_xml((*attr)->encode, zattr, SOAP_LITERAL, xmlParam TSRMLS_CC);
    2011           0 :                                                         if (dummy->children && dummy->children->content) {
    2012           0 :                                                                 if ((*attr)->fixed && strcmp((*attr)->fixed, (char*)dummy->children->content) != 0) {
    2013           0 :                                                                         soap_error3(E_ERROR, "Encoding: Attribute '%s' has fixed value '%s' (value '%s' is not allowed)", (*attr)->name, (*attr)->fixed, dummy->children->content);
    2014             :                                                                 }
    2015             :                                                                 /* we need to handle xml: namespace specially, since it is
    2016             :                                                                    an implicit schema. Otherwise, use form.
    2017             :                                                                 */
    2018           0 :                                                                 if ((*attr)->namens &&
    2019           0 :                                                                     (!strncmp((*attr)->namens, XML_NAMESPACE, sizeof(XML_NAMESPACE)) ||
    2020           0 :                                                                      (*attr)->form == XSD_FORM_QUALIFIED)) {
    2021           0 :                                                                         xmlNsPtr nsp = encode_add_ns(xmlParam, (*attr)->namens);
    2022             : 
    2023           0 :                                                                         xmlSetNsProp(xmlParam, nsp, BAD_CAST((*attr)->name), dummy->children->content);
    2024             :                                                                 } else {
    2025           0 :                                                                         xmlSetProp(xmlParam, BAD_CAST((*attr)->name), dummy->children->content);
    2026             :                                                                 }
    2027             :                                                         }
    2028           0 :                                                         xmlUnlinkNode(dummy);
    2029           0 :                                                         xmlFreeNode(dummy);
    2030             :                                                 }
    2031             :                                         }
    2032           0 :                                         zend_hash_move_forward_ex(sdlType->attributes, &pos);
    2033             :                                 }
    2034             :                         }
    2035             :                 }
    2036           0 :                 if (style == SOAP_ENCODED) {
    2037           0 :                         set_ns_and_type(xmlParam, type);
    2038             :                 }
    2039             :         } else {
    2040           0 :                 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2041           0 :                 xmlAddChild(parent, xmlParam);
    2042             : 
    2043           0 :                 if (soap_check_zval_ref(data, xmlParam TSRMLS_CC)) {
    2044           0 :                         return xmlParam;
    2045             :                 }
    2046           0 :                 if (prop != NULL) {
    2047           0 :                         i = zend_hash_num_elements(prop);
    2048           0 :                         zend_hash_internal_pointer_reset(prop);
    2049             : 
    2050           0 :                         for (;i > 0;i--) {
    2051             :                                 xmlNodePtr property;
    2052             :                                 zval **zprop;
    2053             :                                 char *str_key;
    2054             :                                 ulong index;
    2055             :                                 int key_type;
    2056             :                                 unsigned int str_key_len;
    2057             : 
    2058           0 :                                 key_type = zend_hash_get_current_key_ex(prop, &str_key, &str_key_len, &index, FALSE, NULL);
    2059           0 :                                 zend_hash_get_current_data(prop, (void **)&zprop);
    2060             : 
    2061           0 :                                 property = master_to_xml(get_conversion((*zprop)->type), (*zprop), style, xmlParam TSRMLS_CC);
    2062             : 
    2063           0 :                                 if (key_type == HASH_KEY_IS_STRING) {
    2064             :                                         const char *prop_name;
    2065             : 
    2066           0 :                                         if (Z_TYPE_P(data) == IS_OBJECT) {
    2067             :                                                 const char *class_name;
    2068             : 
    2069           0 :                                                 zend_unmangle_property_name(str_key, str_key_len-1, &class_name, &prop_name);
    2070             :                                         } else {
    2071           0 :                                                 prop_name = str_key;
    2072             :                                         }
    2073           0 :                                         if (prop_name) {
    2074           0 :                                                 xmlNodeSetName(property, BAD_CAST(prop_name));
    2075             :                                         }
    2076             :                                 }
    2077           0 :                                 zend_hash_move_forward(prop);
    2078             :                         }
    2079             :                 }
    2080           0 :                 if (style == SOAP_ENCODED) {
    2081           0 :                         set_ns_and_type(xmlParam, type);
    2082             :                 }
    2083             :         }
    2084           0 :         return xmlParam;
    2085             : }
    2086             : 
    2087             : /* Array encode/decode */
    2088           0 : static xmlNodePtr guess_array_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    2089             : {
    2090           0 :         encodePtr enc = NULL;
    2091             : 
    2092           0 :         if (data && Z_TYPE_P(data) == IS_ARRAY) {
    2093           0 :                 if (is_map(data)) {
    2094           0 :                         enc = get_conversion(APACHE_MAP);
    2095             :                 } else {
    2096           0 :                         enc = get_conversion(SOAP_ENC_ARRAY);
    2097             :                 }
    2098             :         }
    2099           0 :         if (!enc) {
    2100           0 :                 enc = get_conversion(IS_NULL);
    2101             :         }
    2102             : 
    2103           0 :         return master_to_xml(enc, data, style, parent TSRMLS_CC);
    2104             : }
    2105             : 
    2106           0 : static int calc_dimension_12(const char* str)
    2107             : {
    2108           0 :         int i = 0, flag = 0;
    2109           0 :         while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
    2110           0 :                 str++;
    2111             :         }
    2112           0 :         if (*str == '*') {
    2113           0 :                 i++;
    2114           0 :                 str++;
    2115             :         }
    2116           0 :         while (*str != '\0') {
    2117           0 :                 if (*str >= '0' && *str <= '9') {
    2118           0 :                         if (flag == 0) {
    2119           0 :                         i++;
    2120           0 :                         flag = 1;
    2121             :                 }
    2122           0 :           } else if (*str == '*') {
    2123           0 :                         soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
    2124             :                 } else {
    2125           0 :                         flag = 0;
    2126             :                 }
    2127           0 :                 str++;
    2128             :         }
    2129           0 :         return i;
    2130             : }
    2131             : 
    2132           0 : static int* get_position_12(int dimension, const char* str)
    2133             : {
    2134             :         int *pos;
    2135           0 :         int i = -1, flag = 0;
    2136             : 
    2137           0 :         pos = safe_emalloc(sizeof(int), dimension, 0);
    2138           0 :         memset(pos,0,sizeof(int)*dimension);
    2139           0 :         while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
    2140           0 :                 str++;
    2141             :         }
    2142           0 :         if (*str == '*') {
    2143           0 :                 str++;
    2144           0 :                 i++;
    2145             :         }
    2146           0 :         while (*str != '\0') {
    2147           0 :                 if (*str >= '0' && *str <= '9') {
    2148           0 :                         if (flag == 0) {
    2149           0 :                                 i++;
    2150           0 :                                 flag = 1;
    2151             :                         }
    2152           0 :                         pos[i] = (pos[i]*10)+(*str-'0');
    2153           0 :                 } else if (*str == '*') {
    2154           0 :                         soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
    2155             :                 } else {
    2156           0 :                   flag = 0;
    2157             :                 }
    2158           0 :                 str++;
    2159             :         }
    2160           0 :         return pos;
    2161             : }
    2162             : 
    2163           0 : static int calc_dimension(const char* str)
    2164             : {
    2165           0 :         int i = 1;
    2166           0 :         while (*str != ']' && *str != '\0') {
    2167           0 :                 if (*str == ',') {
    2168           0 :                 i++;
    2169             :                 }
    2170           0 :                 str++;
    2171             :         }
    2172           0 :         return i;
    2173             : }
    2174             : 
    2175           0 : static void get_position_ex(int dimension, const char* str, int** pos)
    2176             : {
    2177           0 :         int i = 0;
    2178             : 
    2179           0 :         memset(*pos,0,sizeof(int)*dimension);
    2180           0 :         while (*str != ']' && *str != '\0' && i < dimension) {
    2181           0 :                 if (*str >= '0' && *str <= '9') {
    2182           0 :                         (*pos)[i] = ((*pos)[i]*10)+(*str-'0');
    2183           0 :                 } else if (*str == ',') {
    2184           0 :                         i++;
    2185             :                 }
    2186           0 :                 str++;
    2187             :         }
    2188           0 : }
    2189             : 
    2190           0 : static int* get_position(int dimension, const char* str)
    2191             : {
    2192             :         int *pos;
    2193             : 
    2194           0 :         pos = safe_emalloc(sizeof(int), dimension, 0);
    2195           0 :         get_position_ex(dimension, str, &pos);
    2196           0 :         return pos;
    2197             : }
    2198             : 
    2199           0 : static void add_xml_array_elements(xmlNodePtr xmlParam,
    2200             :                                    sdlTypePtr type,
    2201             :                                    encodePtr enc,
    2202             :                                    xmlNsPtr ns,
    2203             :                                    int dimension ,
    2204             :                                    int* dims,
    2205             :                                    zval* data,
    2206             :                                    int style
    2207             :                                    TSRMLS_DC)
    2208             : {
    2209             :         int j;
    2210             : 
    2211           0 :         if (data && Z_TYPE_P(data) == IS_ARRAY) {
    2212           0 :                 zend_hash_internal_pointer_reset(data->value.ht);
    2213           0 :                 for (j=0; j<dims[0]; j++) {
    2214             :                         zval **zdata;
    2215             : 
    2216           0 :                         if (zend_hash_get_current_data(data->value.ht, (void **)&zdata) != SUCCESS) {
    2217           0 :                                 zdata = NULL;
    2218             :                         }
    2219           0 :                         if (dimension == 1) {
    2220             :                                 xmlNodePtr xparam;
    2221             : 
    2222           0 :                                 if (zdata) {
    2223           0 :                                         if (enc == NULL) {
    2224           0 :                                                 xparam = master_to_xml(get_conversion((*zdata)->type), (*zdata), style, xmlParam TSRMLS_CC);
    2225             :                                         } else {
    2226           0 :                                                 xparam = master_to_xml(enc, (*zdata), style, xmlParam TSRMLS_CC);
    2227             :                                         }
    2228             :                                 } else {
    2229           0 :                                         xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2230           0 :                                         xmlAddChild(xmlParam, xparam);
    2231             :                                 }
    2232             : 
    2233           0 :                                 if (type) {
    2234           0 :                                         xmlNodeSetName(xparam, BAD_CAST(type->name));
    2235           0 :                                 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
    2236           0 :                                         xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
    2237           0 :                                         xmlSetNs(xparam, ns);
    2238             :                                 } else {
    2239           0 :                                         xmlNodeSetName(xparam, BAD_CAST("item"));
    2240             :                                 }
    2241             :                         } else {
    2242           0 :                                 if (zdata) {
    2243           0 :                                   add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, *zdata, style TSRMLS_CC);
    2244             :                                 } else {
    2245           0 :                                   add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style TSRMLS_CC);
    2246             :                                 }
    2247             :                         }
    2248           0 :                         zend_hash_move_forward(data->value.ht);
    2249             :                 }
    2250             :         } else {
    2251           0 :                 for (j=0; j<dims[0]; j++) {
    2252           0 :                         if (dimension == 1) {
    2253             :                                 xmlNodePtr xparam;
    2254             : 
    2255           0 :                                 xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2256           0 :                                 xmlAddChild(xmlParam, xparam);
    2257           0 :                                 if (type) {
    2258           0 :                                         xmlNodeSetName(xparam, BAD_CAST(type->name));
    2259           0 :                                 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
    2260           0 :                                         xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
    2261           0 :                                         xmlSetNs(xparam, ns);
    2262             :                                 } else {
    2263           0 :                                         xmlNodeSetName(xparam, BAD_CAST("item"));
    2264             :                                 }
    2265             :                         } else {
    2266           0 :                           add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style TSRMLS_CC);
    2267             :                         }
    2268             :                 }
    2269             :         }
    2270           0 : }
    2271             : 
    2272             : static inline int array_num_elements(HashTable* ht)
    2273             : {
    2274             :         if (ht->pListTail && ht->pListTail->nKeyLength == 0) {
    2275             :                 return ht->pListTail->h-1;
    2276             :         }
    2277             :         return 0;
    2278             : }
    2279             : 
    2280           0 : static xmlNodePtr to_xml_array(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    2281             : {
    2282           0 :         sdlTypePtr sdl_type = type->sdl_type;
    2283           0 :         sdlTypePtr element_type = NULL;
    2284           0 :         smart_str array_type = {0}, array_size = {0};
    2285             :         int i;
    2286             :         xmlNodePtr xmlParam;
    2287           0 :         encodePtr enc = NULL;
    2288           0 :         int dimension = 1;
    2289             :         int* dims;
    2290             :         int soap_version;
    2291           0 :         zval *array_copy = NULL;
    2292             : 
    2293           0 :         soap_version = SOAP_GLOBAL(soap_version);
    2294             : 
    2295           0 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2296           0 :         xmlAddChild(parent, xmlParam);
    2297             : 
    2298           0 :         if (!data || Z_TYPE_P(data) == IS_NULL) {
    2299           0 :                 if (style == SOAP_ENCODED) {
    2300           0 :                         set_xsi_nil(xmlParam);
    2301           0 :                         if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
    2302           0 :                                 set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
    2303             :                         } else {
    2304           0 :                                 set_ns_and_type(xmlParam, type);
    2305             :                         }
    2306             :                 }
    2307           0 :                 return xmlParam;
    2308             :         }
    2309             : 
    2310           0 :         if (Z_TYPE_P(data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(data), zend_ce_traversable TSRMLS_CC)) {
    2311             :                 zend_object_iterator   *iter;
    2312           0 :                 zend_class_entry       *ce = Z_OBJCE_P(data);
    2313             :                 zval                  **val;
    2314             : 
    2315           0 :                 ALLOC_ZVAL(array_copy);
    2316           0 :                 INIT_PZVAL(array_copy);
    2317           0 :                 array_init(array_copy);
    2318             : 
    2319           0 :                 iter = ce->get_iterator(ce, data, 0 TSRMLS_CC);
    2320             : 
    2321           0 :                 if (EG(exception)) {
    2322           0 :                         goto iterator_done;
    2323             :                 }
    2324             : 
    2325           0 :                 if (iter->funcs->rewind) {
    2326           0 :                         iter->funcs->rewind(iter TSRMLS_CC);
    2327           0 :                         if (EG(exception)) {
    2328           0 :                                 goto iterator_done;
    2329             :                         }
    2330             :                 }
    2331             : 
    2332           0 :                 while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
    2333           0 :                         if (EG(exception)) {
    2334           0 :                                 goto iterator_done;
    2335             :                         }
    2336             : 
    2337           0 :                         iter->funcs->get_current_data(iter, &val TSRMLS_CC);
    2338           0 :                         if (EG(exception)) {
    2339           0 :                                 goto iterator_done;
    2340             :                         }
    2341           0 :                         if (iter->funcs->get_current_key) {
    2342             :                                 zval key;
    2343           0 :                                 iter->funcs->get_current_key(iter, &key TSRMLS_CC);
    2344           0 :                                 if (EG(exception)) {
    2345           0 :                                         goto iterator_done;
    2346             :                                 }
    2347           0 :                                 array_set_zval_key(Z_ARRVAL_P(array_copy), &key, *val);
    2348           0 :                                 zval_ptr_dtor(val);
    2349             :                                 zval_dtor(&key);
    2350             :                         } else {
    2351           0 :                                 add_next_index_zval(array_copy, *val);
    2352             :                         }
    2353           0 :                         Z_ADDREF_PP(val);
    2354             : 
    2355           0 :                         iter->funcs->move_forward(iter TSRMLS_CC);
    2356           0 :                         if (EG(exception)) {
    2357           0 :                                 goto iterator_done;
    2358             :                         }
    2359             :                 }
    2360             : iterator_done:
    2361           0 :                 iter->funcs->dtor(iter TSRMLS_CC);
    2362           0 :                 if (EG(exception)) {
    2363           0 :                         zval_ptr_dtor(&array_copy);
    2364           0 :                         array_copy = NULL;
    2365             :                 } else {
    2366           0 :                         data = array_copy;
    2367             :                 }
    2368             :         }
    2369             : 
    2370           0 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    2371             :                 sdlAttributePtr *arrayType;
    2372             :                 sdlExtraAttributePtr *ext;
    2373             :                 sdlTypePtr elementType;
    2374             : 
    2375           0 :                 i = zend_hash_num_elements(Z_ARRVAL_P(data));
    2376             : 
    2377           0 :                 if (sdl_type &&
    2378           0 :                     sdl_type->attributes &&
    2379           0 :                     zend_hash_find(sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
    2380             :                       sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType"),
    2381           0 :                       (void **)&arrayType) == SUCCESS &&
    2382           0 :                     (*arrayType)->extraAttributes &&
    2383           0 :                     zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType"), (void **)&ext) == SUCCESS) {
    2384             : 
    2385             :                         char *value, *end;
    2386             :                         zval** el;
    2387             : 
    2388           0 :                         value = estrdup((*ext)->val);
    2389           0 :                         end = strrchr(value,'[');
    2390           0 :                         if (end) {
    2391           0 :                                 *end = '\0';
    2392           0 :                                 end++;
    2393           0 :                                 dimension = calc_dimension(end);
    2394             :                         }
    2395           0 :                         if ((*ext)->ns != NULL) {
    2396           0 :                                 enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, value);
    2397           0 :                                 get_type_str(xmlParam, (*ext)->ns, value, &array_type);
    2398             :                         } else {
    2399           0 :                                 smart_str_appends(&array_type, value);
    2400             :                         }
    2401             : 
    2402           0 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2403           0 :                         dims[0] = i;
    2404           0 :                         el = &data;
    2405           0 :                         for (i = 1; i < dimension; i++) {
    2406           0 :                                 if (el != NULL && Z_TYPE_PP(el) == IS_ARRAY &&
    2407           0 :                                     zend_hash_num_elements(Z_ARRVAL_PP(el)) > 0) {
    2408           0 :                                   zend_hash_internal_pointer_reset(Z_ARRVAL_PP(el));
    2409           0 :                                         zend_hash_get_current_data(Z_ARRVAL_PP(el), (void**)&el);
    2410           0 :                                         if (Z_TYPE_PP(el) == IS_ARRAY) {
    2411           0 :                                                 dims[i] = zend_hash_num_elements(Z_ARRVAL_PP(el));
    2412             :                                         } else {
    2413           0 :                                                 dims[i] = 0;
    2414             :                                         }
    2415             :                                 }
    2416             :                         }
    2417             : 
    2418           0 :                         smart_str_append_long(&array_size, dims[0]);
    2419           0 :                         for (i=1; i<dimension; i++) {
    2420           0 :                                 smart_str_appendc(&array_size, ',');
    2421           0 :                                 smart_str_append_long(&array_size, dims[i]);
    2422             :                         }
    2423             : 
    2424           0 :                         efree(value);
    2425             : 
    2426           0 :                 } else if (sdl_type &&
    2427           0 :                            sdl_type->attributes &&
    2428           0 :                            zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
    2429             :                              sizeof(SOAP_1_2_ENC_NAMESPACE":itemType"),
    2430           0 :                              (void **)&arrayType) == SUCCESS &&
    2431           0 :                            (*arrayType)->extraAttributes &&
    2432           0 :                            zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType"), (void **)&ext) == SUCCESS) {
    2433           0 :                         if ((*ext)->ns != NULL) {
    2434           0 :                                 enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, (*ext)->val);
    2435           0 :                                 get_type_str(xmlParam, (*ext)->ns, (*ext)->val, &array_type);
    2436             :                         } else {
    2437           0 :                                 smart_str_appends(&array_type, (*ext)->val);
    2438             :                         }
    2439           0 :                         if (zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2440             :                                            sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
    2441           0 :                                            (void **)&arrayType) == SUCCESS &&
    2442           0 :                             (*arrayType)->extraAttributes &&
    2443           0 :                             zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
    2444           0 :                                 dimension = calc_dimension_12((*ext)->val);
    2445           0 :                                 dims = get_position_12(dimension, (*ext)->val);
    2446           0 :                                 if (dims[0] == 0) {dims[0] = i;}
    2447             : 
    2448           0 :                                 smart_str_append_long(&array_size, dims[0]);
    2449           0 :                                 for (i=1; i<dimension; i++) {
    2450           0 :                                         smart_str_appendc(&array_size, ',');
    2451           0 :                                         smart_str_append_long(&array_size, dims[i]);
    2452             :                                 }
    2453             :                         } else {
    2454           0 :                                 dims = emalloc(sizeof(int));
    2455           0 :                                 *dims = 0;
    2456           0 :                                 smart_str_append_long(&array_size, i);
    2457             :                         }
    2458           0 :                 } else if (sdl_type &&
    2459           0 :                            sdl_type->attributes &&
    2460           0 :                            zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2461             :                              sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
    2462           0 :                              (void **)&arrayType) == SUCCESS &&
    2463           0 :                            (*arrayType)->extraAttributes &&
    2464           0 :                            zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraySize"), (void **)&ext) == SUCCESS) {
    2465           0 :                         dimension = calc_dimension_12((*ext)->val);
    2466           0 :                         dims = get_position_12(dimension, (*ext)->val);
    2467           0 :                         if (dims[0] == 0) {dims[0] = i;}
    2468             : 
    2469           0 :                         smart_str_append_long(&array_size, dims[0]);
    2470           0 :                         for (i=1; i<dimension; i++) {
    2471           0 :                                 smart_str_appendc(&array_size, ',');
    2472           0 :                                 smart_str_append_long(&array_size, dims[i]);
    2473             :                         }
    2474             : 
    2475           0 :                         if (sdl_type && sdl_type->elements &&
    2476           0 :                             zend_hash_num_elements(sdl_type->elements) == 1 &&
    2477           0 :                             (zend_hash_internal_pointer_reset(sdl_type->elements),
    2478             :                              zend_hash_get_current_data(sdl_type->elements, (void**)&elementType) == SUCCESS) &&
    2479           0 :                                         (elementType = *(sdlTypePtr*)elementType) != NULL &&
    2480           0 :                              elementType->encode && elementType->encode->details.type_str) {
    2481           0 :                                 element_type = elementType;
    2482           0 :                                 enc = elementType->encode;
    2483           0 :                                 get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
    2484             :                         } else {
    2485           0 :                                 enc = get_array_type(xmlParam, data, &array_type TSRMLS_CC);
    2486             :                         }
    2487           0 :                 } else if (sdl_type && sdl_type->elements &&
    2488           0 :                            zend_hash_num_elements(sdl_type->elements) == 1 &&
    2489           0 :                            (zend_hash_internal_pointer_reset(sdl_type->elements),
    2490             :                             zend_hash_get_current_data(sdl_type->elements, (void**)&elementType) == SUCCESS) &&
    2491           0 :                            (elementType = *(sdlTypePtr*)elementType) != NULL &&
    2492           0 :                            elementType->encode && elementType->encode->details.type_str) {
    2493             : 
    2494           0 :                         element_type = elementType;
    2495           0 :                         enc = elementType->encode;
    2496           0 :                         get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
    2497             : 
    2498           0 :                         smart_str_append_long(&array_size, i);
    2499             : 
    2500           0 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2501           0 :                         dims[0] = i;
    2502             :                 } else {
    2503             : 
    2504           0 :                         enc = get_array_type(xmlParam, data, &array_type TSRMLS_CC);
    2505           0 :                         smart_str_append_long(&array_size, i);
    2506           0 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2507           0 :                         dims[0] = i;
    2508             :                 }
    2509             : 
    2510           0 :                 if (style == SOAP_ENCODED) {
    2511           0 :                         if (soap_version == SOAP_1_1) {
    2512           0 :                                 smart_str_0(&array_type);
    2513           0 :                                 if (strcmp(array_type.c,"xsd:anyType") == 0) {
    2514           0 :                                         smart_str_free(&array_type);
    2515           0 :                                         smart_str_appendl(&array_type,"xsd:ur-type",sizeof("xsd:ur-type")-1);
    2516             :                                 }
    2517           0 :                                 smart_str_appendc(&array_type, '[');
    2518           0 :                                 smart_str_append(&array_type, &array_size);
    2519           0 :                                 smart_str_appendc(&array_type, ']');
    2520           0 :                                 smart_str_0(&array_type);
    2521           0 :                                 set_ns_prop(xmlParam, SOAP_1_1_ENC_NAMESPACE, "arrayType", array_type.c);
    2522             :                         } else {
    2523           0 :                                 int i = 0;
    2524           0 :                                 while (i < array_size.len) {
    2525           0 :                                         if (array_size.c[i] == ',') {array_size.c[i] = ' ';}
    2526           0 :                                         ++i;
    2527             :                                 }
    2528           0 :                                 smart_str_0(&array_type);
    2529           0 :                                 smart_str_0(&array_size);
    2530           0 :                                 set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "itemType", array_type.c);
    2531           0 :                                 set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "arraySize", array_size.c);
    2532             :                         }
    2533             :                 }
    2534           0 :                 smart_str_free(&array_type);
    2535           0 :                 smart_str_free(&array_size);
    2536             : 
    2537           0 :                 add_xml_array_elements(xmlParam, element_type, enc, enc?encode_add_ns(xmlParam,enc->details.ns):NULL, dimension, dims, data, style TSRMLS_CC);
    2538           0 :                 efree(dims);
    2539             :         }
    2540           0 :         if (style == SOAP_ENCODED) {
    2541           0 :                 if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
    2542           0 :                         set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
    2543             :                 } else {
    2544           0 :                         set_ns_and_type(xmlParam, type);
    2545             :                 }
    2546             :         }
    2547             : 
    2548           0 :         if (array_copy) {
    2549           0 :                 zval_ptr_dtor(&array_copy);
    2550             :         }
    2551             : 
    2552           0 :         return xmlParam;
    2553             : }
    2554             : 
    2555           0 : static zval *to_zval_array(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    2556             : {
    2557             :         zval *ret;
    2558             :         xmlNodePtr trav;
    2559           0 :         encodePtr enc = NULL;
    2560           0 :         int dimension = 1;
    2561           0 :         int* dims = NULL;
    2562           0 :         int* pos = NULL;
    2563             :         xmlAttrPtr attr;
    2564             :         sdlPtr sdl;
    2565             :         sdlAttributePtr *arrayType;
    2566             :         sdlExtraAttributePtr *ext;
    2567             :         sdlTypePtr elementType;
    2568             : 
    2569           0 :         MAKE_STD_ZVAL(ret);
    2570           0 :         FIND_XML_NULL(data, ret);
    2571           0 :         sdl = SOAP_GLOBAL(sdl);
    2572             : 
    2573           0 :         if (data &&
    2574           0 :             (attr = get_attribute(data->properties,"arrayType")) &&
    2575           0 :             attr->children && attr->children->content) {
    2576             :                 char *type, *end, *ns;
    2577             :                 xmlNsPtr nsptr;
    2578             : 
    2579           0 :                 parse_namespace(attr->children->content, &type, &ns);
    2580           0 :                 nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
    2581             : 
    2582           0 :                 end = strrchr(type,'[');
    2583           0 :                 if (end) {
    2584           0 :                         *end = '\0';
    2585           0 :                         dimension = calc_dimension(end+1);
    2586           0 :                         dims = get_position(dimension, end+1);
    2587             :                 }
    2588           0 :                 if (nsptr != NULL) {
    2589           0 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
    2590             :                 }
    2591           0 :                 efree(type);
    2592           0 :                 if (ns) {efree(ns);}
    2593             : 
    2594           0 :         } else if ((attr = get_attribute(data->properties,"itemType")) &&
    2595           0 :             attr->children &&
    2596           0 :             attr->children->content) {
    2597             :                 char *type, *ns;
    2598             :                 xmlNsPtr nsptr;
    2599             : 
    2600           0 :                 parse_namespace(attr->children->content, &type, &ns);
    2601           0 :                 nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
    2602           0 :                 if (nsptr != NULL) {
    2603           0 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
    2604             :                 }
    2605           0 :                 efree(type);
    2606           0 :                 if (ns) {efree(ns);}
    2607             : 
    2608           0 :                 if ((attr = get_attribute(data->properties,"arraySize")) &&
    2609           0 :                     attr->children && attr->children->content) {
    2610           0 :                         dimension = calc_dimension_12((char*)attr->children->content);
    2611           0 :                         dims = get_position_12(dimension, (char*)attr->children->content);
    2612             :                 } else {
    2613           0 :                         dims = emalloc(sizeof(int));
    2614           0 :                         *dims = 0;
    2615             :                 }
    2616             : 
    2617           0 :         } else if ((attr = get_attribute(data->properties,"arraySize")) &&
    2618           0 :             attr->children && attr->children->content) {
    2619             : 
    2620           0 :                 dimension = calc_dimension_12((char*)attr->children->content);
    2621           0 :                 dims = get_position_12(dimension, (char*)attr->children->content);
    2622             : 
    2623           0 :         } else if (type->sdl_type != NULL &&
    2624           0 :                    type->sdl_type->attributes != NULL &&
    2625           0 :                    zend_hash_find(type->sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
    2626             :                                   sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType"),
    2627           0 :                                   (void **)&arrayType) == SUCCESS &&
    2628           0 :                    (*arrayType)->extraAttributes &&
    2629           0 :                    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType"), (void **)&ext) == SUCCESS) {
    2630             :                 char *type, *end;
    2631             : 
    2632           0 :                 type = estrdup((*ext)->val);
    2633           0 :                 end = strrchr(type,'[');
    2634           0 :                 if (end) {
    2635           0 :                         *end = '\0';
    2636             :                 }
    2637           0 :                 if ((*ext)->ns != NULL) {
    2638           0 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, type);
    2639             :                 }
    2640           0 :                 efree(type);
    2641             : 
    2642           0 :                 dims = emalloc(sizeof(int));
    2643           0 :                 *dims = 0;
    2644             : 
    2645           0 :         } else if (type->sdl_type != NULL &&
    2646           0 :                    type->sdl_type->attributes != NULL &&
    2647           0 :                    zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
    2648             :                                   sizeof(SOAP_1_2_ENC_NAMESPACE":itemType"),
    2649           0 :                                   (void **)&arrayType) == SUCCESS &&
    2650           0 :                    (*arrayType)->extraAttributes &&
    2651           0 :                    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType"), (void **)&ext) == SUCCESS) {
    2652             : 
    2653           0 :                 if ((*ext)->ns != NULL) {
    2654           0 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, (*ext)->val);
    2655             :                 }
    2656             : 
    2657           0 :                 if (zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2658             :                                    sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
    2659           0 :                                    (void **)&arrayType) == SUCCESS &&
    2660           0 :                     (*arrayType)->extraAttributes &&
    2661           0 :                     zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
    2662           0 :                         dimension = calc_dimension_12((*ext)->val);
    2663           0 :                         dims = get_position_12(dimension, (*ext)->val);
    2664             :                 } else {
    2665           0 :                         dims = emalloc(sizeof(int));
    2666           0 :                         *dims = 0;
    2667             :                 }
    2668           0 :         } else if (type->sdl_type != NULL &&
    2669           0 :                    type->sdl_type->attributes != NULL &&
    2670           0 :                    zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2671             :                                   sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
    2672           0 :                                   (void **)&arrayType) == SUCCESS &&
    2673           0 :                    (*arrayType)->extraAttributes &&
    2674           0 :                    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
    2675             : 
    2676           0 :                 dimension = calc_dimension_12((*ext)->val);
    2677           0 :                 dims = get_position_12(dimension, (*ext)->val);
    2678           0 :                 if (type->sdl_type && type->sdl_type->elements &&
    2679           0 :                     zend_hash_num_elements(type->sdl_type->elements) == 1 &&
    2680           0 :                     (zend_hash_internal_pointer_reset(type->sdl_type->elements),
    2681           0 :                      zend_hash_get_current_data(type->sdl_type->elements, (void**)&elementType) == SUCCESS) &&
    2682           0 :                     (elementType = *(sdlTypePtr*)elementType) != NULL &&
    2683           0 :                     elementType->encode) {
    2684           0 :                         enc = elementType->encode;
    2685             :                 }
    2686           0 :         } else if (type->sdl_type && type->sdl_type->elements &&
    2687           0 :                    zend_hash_num_elements(type->sdl_type->elements) == 1 &&
    2688           0 :                    (zend_hash_internal_pointer_reset(type->sdl_type->elements),
    2689           0 :                     zend_hash_get_current_data(type->sdl_type->elements, (void**)&elementType) == SUCCESS) &&
    2690           0 :                    (elementType = *(sdlTypePtr*)elementType) != NULL &&
    2691           0 :                    elementType->encode) {
    2692           0 :                 enc = elementType->encode;
    2693             :         }
    2694           0 :         if (dims == NULL) {
    2695           0 :                 dimension = 1;
    2696           0 :                 dims = emalloc(sizeof(int));
    2697           0 :                 *dims = 0;
    2698             :         }
    2699           0 :         pos = safe_emalloc(sizeof(int), dimension, 0);
    2700           0 :         memset(pos,0,sizeof(int)*dimension);
    2701           0 :         if (data &&
    2702           0 :             (attr = get_attribute(data->properties,"offset")) &&
    2703           0 :              attr->children && attr->children->content) {
    2704           0 :                 char* tmp = strrchr((char*)attr->children->content,'[');
    2705             : 
    2706           0 :                 if (tmp == NULL) {
    2707           0 :                         tmp = (char*)attr->children->content;
    2708             :                 }
    2709           0 :                 get_position_ex(dimension, tmp, &pos);
    2710             :         }
    2711             : 
    2712           0 :         array_init(ret);
    2713           0 :         trav = data->children;
    2714           0 :         while (trav) {
    2715           0 :                 if (trav->type == XML_ELEMENT_NODE) {
    2716             :                         int i;
    2717             :                         zval *tmpVal, *ar;
    2718           0 :                         xmlAttrPtr position = get_attribute(trav->properties,"position");
    2719             : 
    2720           0 :                         tmpVal = master_to_zval(enc, trav TSRMLS_CC);
    2721           0 :                         if (position != NULL && position->children && position->children->content) {
    2722           0 :                                 char* tmp = strrchr((char*)position->children->content, '[');
    2723           0 :                                 if (tmp == NULL) {
    2724           0 :                                         tmp = (char*)position->children->content;
    2725             :                                 }
    2726           0 :                                 get_position_ex(dimension, tmp, &pos);
    2727             :                         }
    2728             : 
    2729             :                         /* Get/Create intermediate arrays for multidimensional arrays */
    2730           0 :                         i = 0;
    2731           0 :                         ar = ret;
    2732           0 :                         while (i < dimension-1) {
    2733             :                                 zval** ar2;
    2734           0 :                                 if (zend_hash_index_find(Z_ARRVAL_P(ar), pos[i], (void**)&ar2) == SUCCESS) {
    2735           0 :                                         ar = *ar2;
    2736             :                                 } else {
    2737             :                                         zval *tmpAr;
    2738           0 :                                         MAKE_STD_ZVAL(tmpAr);
    2739           0 :                                         array_init(tmpAr);
    2740           0 :                                         zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpAr, sizeof(zval*), (void**)&ar2);
    2741           0 :                                         ar = *ar2;
    2742             :                                 }
    2743           0 :                                 i++;
    2744             :                         }
    2745           0 :                         zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpVal, sizeof(zval *), NULL);
    2746             : 
    2747             :                         /* Increment position */
    2748           0 :                         i = dimension;
    2749           0 :                         while (i > 0) {
    2750           0 :                           i--;
    2751           0 :                           pos[i]++;
    2752           0 :                                 if (pos[i] >= dims[i]) {
    2753           0 :                                         if (i > 0) {
    2754           0 :                                                 pos[i] = 0;
    2755             :                                         } else {
    2756             :                                                 /* TODO: Array index overflow */
    2757             :                                         }
    2758             :                                 } else {
    2759           0 :                                   break;
    2760             :                                 }
    2761             :                         }
    2762             :                 }
    2763           0 :                 trav = trav->next;
    2764             :         }
    2765           0 :         efree(dims);
    2766           0 :         efree(pos);
    2767           0 :         return ret;
    2768             : }
    2769             : 
    2770             : /* Map encode/decode */
    2771           0 : static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    2772             : {
    2773             :         xmlNodePtr xmlParam;
    2774             :         int i;
    2775             : 
    2776           0 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2777           0 :         xmlAddChild(parent, xmlParam);
    2778           0 :         FIND_ZVAL_NULL(data, xmlParam, style);
    2779             : 
    2780           0 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    2781           0 :                 i = zend_hash_num_elements(Z_ARRVAL_P(data));
    2782           0 :                 zend_hash_internal_pointer_reset(data->value.ht);
    2783           0 :                 for (;i > 0;i--) {
    2784             :                         xmlNodePtr xparam, item;
    2785             :                         xmlNodePtr key;
    2786             :                         zval **temp_data;
    2787             :                         char *key_val;
    2788             :                         ulong int_val;
    2789             : 
    2790           0 :                         zend_hash_get_current_data(data->value.ht, (void **)&temp_data);
    2791           0 :                         item = xmlNewNode(NULL, BAD_CAST("item"));
    2792           0 :                         xmlAddChild(xmlParam, item);
    2793           0 :                         key = xmlNewNode(NULL, BAD_CAST("key"));
    2794           0 :                         xmlAddChild(item,key);
    2795           0 :                         if (zend_hash_get_current_key(data->value.ht, &key_val, &int_val, FALSE) == HASH_KEY_IS_STRING) {
    2796           0 :                                 if (style == SOAP_ENCODED) {
    2797           0 :                                         set_xsi_type(key, "xsd:string");
    2798             :                                 }
    2799           0 :                                 xmlNodeSetContent(key, BAD_CAST(key_val));
    2800             :                         } else {
    2801           0 :                                 smart_str tmp = {0};
    2802           0 :                                 smart_str_append_long(&tmp, int_val);
    2803           0 :                                 smart_str_0(&tmp);
    2804             : 
    2805           0 :                                 if (style == SOAP_ENCODED) {
    2806           0 :                                         set_xsi_type(key, "xsd:int");
    2807             :                                 }
    2808           0 :                                 xmlNodeSetContentLen(key, BAD_CAST(tmp.c), tmp.len);
    2809             : 
    2810           0 :                                 smart_str_free(&tmp);
    2811             :                         }
    2812             : 
    2813           0 :                         xparam = master_to_xml(get_conversion((*temp_data)->type), (*temp_data), style, item TSRMLS_CC);
    2814           0 :                         xmlNodeSetName(xparam, BAD_CAST("value"));
    2815             : 
    2816           0 :                         zend_hash_move_forward(data->value.ht);
    2817             :                 }
    2818             :         }
    2819           0 :         if (style == SOAP_ENCODED) {
    2820           0 :                 set_ns_and_type(xmlParam, type);
    2821             :         }
    2822             : 
    2823           0 :         return xmlParam;
    2824             : }
    2825             : 
    2826           0 : static zval *to_zval_map(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    2827             : {
    2828             :         zval *ret, *key, *value;
    2829             :         xmlNodePtr trav, item, xmlKey, xmlValue;
    2830             : 
    2831           0 :         MAKE_STD_ZVAL(ret);
    2832           0 :         FIND_XML_NULL(data, ret);
    2833             : 
    2834           0 :         if (data && data->children) {
    2835           0 :                 array_init(ret);
    2836           0 :                 trav = data->children;
    2837             : 
    2838           0 :                 trav = data->children;
    2839           0 :                 FOREACHNODE(trav, "item", item) {
    2840           0 :                         xmlKey = get_node(item->children, "key");
    2841           0 :                         if (!xmlKey) {
    2842           0 :                                 soap_error0(E_ERROR,  "Encoding: Can't decode apache map, missing key");
    2843             :                         }
    2844             : 
    2845           0 :                         xmlValue = get_node(item->children, "value");
    2846           0 :                         if (!xmlKey) {
    2847           0 :                                 soap_error0(E_ERROR,  "Encoding: Can't decode apache map, missing value");
    2848             :                         }
    2849             : 
    2850           0 :                         key = master_to_zval(NULL, xmlKey TSRMLS_CC);
    2851           0 :                         value = master_to_zval(NULL, xmlValue TSRMLS_CC);
    2852             : 
    2853           0 :                         if (Z_TYPE_P(key) == IS_STRING) {
    2854           0 :                                 zend_symtable_update(Z_ARRVAL_P(ret), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &value, sizeof(zval *), NULL);
    2855           0 :                         } else if (Z_TYPE_P(key) == IS_LONG) {
    2856           0 :                                 zend_hash_index_update(Z_ARRVAL_P(ret), Z_LVAL_P(key), &value, sizeof(zval *), NULL);
    2857             :                         } else {
    2858           0 :                                 soap_error0(E_ERROR,  "Encoding: Can't decode apache map, only Strings or Longs are allowd as keys");
    2859             :                         }
    2860           0 :                         zval_ptr_dtor(&key);
    2861             :                 }
    2862           0 :                 ENDFOREACH(trav);
    2863             :         } else {
    2864           0 :                 ZVAL_NULL(ret);
    2865             :         }
    2866           0 :         return ret;
    2867             : }
    2868             : 
    2869             : /* Unknown encode/decode */
    2870           0 : static xmlNodePtr guess_xml_convert(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    2871             : {
    2872             :         encodePtr  enc;
    2873             :         xmlNodePtr ret;
    2874             : 
    2875           0 :         if (data) {
    2876           0 :                 enc = get_conversion(data->type);
    2877             :         } else {
    2878           0 :                 enc = get_conversion(IS_NULL);
    2879             :         }
    2880           0 :         ret = master_to_xml_int(enc, data, style, parent, 0 TSRMLS_CC);
    2881             : /*
    2882             :         if (style == SOAP_LITERAL && SOAP_GLOBAL(sdl)) {
    2883             :                 set_ns_and_type(ret, &enc->details);
    2884             :         }
    2885             : */
    2886           0 :         return ret;
    2887             : }
    2888             : 
    2889           0 : static zval *guess_zval_convert(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    2890             : {
    2891           0 :         encodePtr enc = NULL;
    2892             :         xmlAttrPtr tmpattr;
    2893           0 :         xmlChar *type_name = NULL;
    2894             :         zval *ret;
    2895             : 
    2896           0 :         data = check_and_resolve_href(data);
    2897             : 
    2898           0 :         if (data == NULL) {
    2899           0 :                 enc = get_conversion(IS_NULL);
    2900           0 :         } else if (data->properties && get_attribute_ex(data->properties, "nil", XSI_NAMESPACE)) {
    2901           0 :                 enc = get_conversion(IS_NULL);
    2902             :         } else {
    2903           0 :                 tmpattr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
    2904           0 :                 if (tmpattr != NULL) {
    2905           0 :                   type_name = tmpattr->children->content;
    2906           0 :                         enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, tmpattr->children->content);
    2907           0 :                         if (enc && type == &enc->details) {
    2908           0 :                                 enc = NULL;
    2909             :                         }
    2910           0 :                         if (enc != NULL) {
    2911           0 :                           encodePtr tmp = enc;
    2912           0 :                           while (tmp &&
    2913           0 :                                  tmp->details.sdl_type != NULL &&
    2914           0 :                                  tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
    2915           0 :                             if (enc == tmp->details.sdl_type->encode ||
    2916           0 :                                 tmp == tmp->details.sdl_type->encode) {
    2917           0 :                                 enc = NULL;
    2918           0 :                                 break;
    2919             :                             }
    2920           0 :                             tmp = tmp->details.sdl_type->encode;
    2921             :                           }
    2922             :                         }
    2923             :                 }
    2924             : 
    2925           0 :                 if (enc == NULL) {
    2926             :                         /* Didn't have a type, totally guess here */
    2927             :                         /* Logic: has children = IS_OBJECT else IS_STRING */
    2928             :                         xmlNodePtr trav;
    2929             : 
    2930           0 :                         if (get_attribute(data->properties, "arrayType") ||
    2931           0 :                             get_attribute(data->properties, "itemType") ||
    2932           0 :                             get_attribute(data->properties, "arraySize")) {
    2933           0 :                                 enc = get_conversion(SOAP_ENC_ARRAY);
    2934             :                         } else {
    2935           0 :                                 enc = get_conversion(XSD_STRING);
    2936           0 :                                 trav = data->children;
    2937           0 :                                 while (trav != NULL) {
    2938           0 :                                         if (trav->type == XML_ELEMENT_NODE) {
    2939           0 :                                                 enc = get_conversion(SOAP_ENC_OBJECT);
    2940           0 :                                                 break;
    2941             :                                         }
    2942           0 :                                         trav = trav->next;
    2943             :                                 }
    2944             :                         }
    2945             :                 }
    2946             :         }
    2947           0 :         ret = master_to_zval_int(enc, data TSRMLS_CC);
    2948           0 :         if (SOAP_GLOBAL(sdl) && type_name && enc->details.sdl_type) {
    2949             :                 zval* soapvar;
    2950             :                 char *ns, *cptype;
    2951             :                 xmlNsPtr nsptr;
    2952             : 
    2953           0 :                 MAKE_STD_ZVAL(soapvar);
    2954           0 :                 object_init_ex(soapvar, soap_var_class_entry);
    2955           0 :                 add_property_long(soapvar, "enc_type", enc->details.type);
    2956             :                 Z_DELREF_P(ret);
    2957           0 :                 add_property_zval(soapvar, "enc_value", ret);
    2958           0 :                 parse_namespace(type_name, &cptype, &ns);
    2959           0 :                 nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
    2960           0 :                 add_property_string(soapvar, "enc_stype", cptype, 1);
    2961           0 :                 if (nsptr) {
    2962           0 :                         add_property_string(soapvar, "enc_ns", (char*)nsptr->href, 1);
    2963             :                 }
    2964           0 :                 efree(cptype);
    2965           0 :                 if (ns) {efree(ns);}
    2966           0 :                 ret = soapvar;
    2967             :         }
    2968           0 :         return ret;
    2969             : }
    2970             : 
    2971             : /* Time encode/decode */
    2972           0 : static xmlNodePtr to_xml_datetime_ex(encodeTypePtr type, zval *data, char *format, int style, xmlNodePtr parent TSRMLS_DC)
    2973             : {
    2974             :         /* logic hacked from ext/standard/datetime.c */
    2975             :         struct tm *ta, tmbuf;
    2976             :         time_t timestamp;
    2977           0 :         int max_reallocs = 5;
    2978           0 :         size_t buf_len=64, real_len;
    2979             :         char *buf;
    2980             :         char tzbuf[8];
    2981             : 
    2982             :         xmlNodePtr xmlParam;
    2983             : 
    2984           0 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2985           0 :         xmlAddChild(parent, xmlParam);
    2986           0 :         FIND_ZVAL_NULL(data, xmlParam, style);
    2987             : 
    2988           0 :         if (Z_TYPE_P(data) == IS_LONG) {
    2989           0 :                 timestamp = Z_LVAL_P(data);
    2990           0 :                 ta = php_localtime_r(&timestamp, &tmbuf);
    2991             :                 /*ta = php_gmtime_r(&timestamp, &tmbuf);*/
    2992           0 :                 if (!ta) {
    2993           0 :                         soap_error1(E_ERROR, "Encoding: Invalid timestamp %ld", Z_LVAL_P(data));
    2994             :                 }
    2995             : 
    2996           0 :                 buf = (char *) emalloc(buf_len);
    2997           0 :                 while ((real_len = strftime(buf, buf_len, format, ta)) == buf_len || real_len == 0) {
    2998           0 :                         buf_len *= 2;
    2999           0 :                         buf = (char *) erealloc(buf, buf_len);
    3000           0 :                         if (!--max_reallocs) break;
    3001             :                 }
    3002             : 
    3003             :                 /* Time zone support */
    3004             : #ifdef HAVE_TM_GMTOFF
    3005           0 :                 snprintf(tzbuf, sizeof(tzbuf), "%c%02d:%02d", (ta->tm_gmtoff < 0) ? '-' : '+', abs(ta->tm_gmtoff / 3600), abs( (ta->tm_gmtoff % 3600) / 60 ));
    3006             : #else
    3007             : # if defined(__CYGWIN__) || defined(NETWARE)
    3008             :                 snprintf(tzbuf, sizeof(tzbuf), "%c%02d:%02d", ((ta->tm_isdst ? _timezone - 3600:_timezone)>0)?'-':'+', abs((ta->tm_isdst ? _timezone - 3600 : _timezone) / 3600), abs(((ta->tm_isdst ? _timezone - 3600 : _timezone) % 3600) / 60));
    3009             : # else
    3010             :                 snprintf(tzbuf, sizeof(tzbuf), "%c%02d:%02d", ((ta->tm_isdst ? timezone - 3600:timezone)>0)?'-':'+', abs((ta->tm_isdst ? timezone - 3600 : timezone) / 3600), abs(((ta->tm_isdst ? timezone - 3600 : timezone) % 3600) / 60));
    3011             : # endif
    3012             : #endif
    3013           0 :                 if (strcmp(tzbuf,"+00:00") == 0) {
    3014           0 :                   strcpy(tzbuf,"Z");
    3015           0 :                   real_len++;
    3016             :                 } else {
    3017           0 :                         real_len += 6;
    3018             :                 }
    3019           0 :                 if (real_len >= buf_len) {
    3020           0 :                         buf = (char *) erealloc(buf, real_len+1);
    3021             :                 }
    3022           0 :                 strcat(buf, tzbuf);
    3023             : 
    3024           0 :                 xmlNodeSetContent(xmlParam, BAD_CAST(buf));
    3025           0 :                 efree(buf);
    3026           0 :         } else if (Z_TYPE_P(data) == IS_STRING) {
    3027           0 :                 xmlNodeSetContentLen(xmlParam, BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
    3028             :         }
    3029             : 
    3030           0 :         if (style == SOAP_ENCODED) {
    3031           0 :                 set_ns_and_type(xmlParam, type);
    3032             :         }
    3033           0 :         return xmlParam;
    3034             : }
    3035             : 
    3036           0 : static xmlNodePtr to_xml_duration(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3037             : {
    3038             :         /* TODO: '-'?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T([0-9]+H)?([0-9]+M)?([0-9]+S)? */
    3039           0 :         return to_xml_string(type, data, style, parent TSRMLS_CC);
    3040             : }
    3041             : 
    3042           0 : static xmlNodePtr to_xml_datetime(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3043             : {
    3044           0 :         return to_xml_datetime_ex(type, data, "%Y-%m-%dT%H:%M:%S", style, parent TSRMLS_CC);
    3045             : }
    3046             : 
    3047           0 : static xmlNodePtr to_xml_time(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3048             : {
    3049             :         /* TODO: microsecconds */
    3050           0 :         return to_xml_datetime_ex(type, data, "%H:%M:%S", style, parent TSRMLS_CC);
    3051             : }
    3052             : 
    3053           0 : static xmlNodePtr to_xml_date(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3054             : {
    3055           0 :         return to_xml_datetime_ex(type, data, "%Y-%m-%d", style, parent TSRMLS_CC);
    3056             : }
    3057             : 
    3058           0 : static xmlNodePtr to_xml_gyearmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3059             : {
    3060           0 :         return to_xml_datetime_ex(type, data, "%Y-%m", style, parent TSRMLS_CC);
    3061             : }
    3062             : 
    3063           0 : static xmlNodePtr to_xml_gyear(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3064             : {
    3065           0 :         return to_xml_datetime_ex(type, data, "%Y", style, parent TSRMLS_CC);
    3066             : }
    3067             : 
    3068           0 : static xmlNodePtr to_xml_gmonthday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3069             : {
    3070           0 :         return to_xml_datetime_ex(type, data, "--%m-%d", style, parent TSRMLS_CC);
    3071             : }
    3072             : 
    3073           0 : static xmlNodePtr to_xml_gday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3074             : {
    3075           0 :         return to_xml_datetime_ex(type, data, "---%d", style, parent TSRMLS_CC);
    3076             : }
    3077             : 
    3078           0 : static xmlNodePtr to_xml_gmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3079             : {
    3080           0 :         return to_xml_datetime_ex(type, data, "--%m--", style, parent TSRMLS_CC);
    3081             : }
    3082             : 
    3083           0 : static zval* to_zval_list(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC) {
    3084             :         /*FIXME*/
    3085           0 :         return to_zval_stringc(enc, data TSRMLS_CC);
    3086             : }
    3087             : 
    3088           0 : static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC) {
    3089             :         xmlNodePtr ret;
    3090           0 :         encodePtr list_enc = NULL;
    3091             : 
    3092           0 :         if (enc->sdl_type && enc->sdl_type->kind == XSD_TYPEKIND_LIST && enc->sdl_type->elements) {
    3093             :                 sdlTypePtr *type;
    3094             : 
    3095           0 :                 zend_hash_internal_pointer_reset(enc->sdl_type->elements);
    3096           0 :                 if (zend_hash_get_current_data(enc->sdl_type->elements, (void**)&type) == SUCCESS) {
    3097           0 :                         list_enc = (*type)->encode;
    3098             :                 }
    3099             :         }
    3100             : 
    3101           0 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    3102           0 :         xmlAddChild(parent, ret);
    3103           0 :         FIND_ZVAL_NULL(data, ret, style);
    3104           0 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    3105             :                 zval **tmp;
    3106           0 :                 smart_str list = {0};
    3107           0 :                 HashTable *ht = Z_ARRVAL_P(data);
    3108             : 
    3109           0 :                 zend_hash_internal_pointer_reset(ht);
    3110           0 :                 while (zend_hash_get_current_data(ht, (void**)&tmp) == SUCCESS) {
    3111           0 :                         xmlNodePtr dummy = master_to_xml(list_enc, *tmp, SOAP_LITERAL, ret TSRMLS_CC);
    3112           0 :                         if (dummy && dummy->children && dummy->children->content) {
    3113           0 :                                 if (list.len != 0) {
    3114           0 :                                         smart_str_appendc(&list, ' ');
    3115             :                                 }
    3116           0 :                                 smart_str_appends(&list, (char*)dummy->children->content);
    3117             :                         } else {
    3118           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    3119             :                         }
    3120           0 :                         xmlUnlinkNode(dummy);
    3121           0 :                         xmlFreeNode(dummy);
    3122           0 :                         zend_hash_move_forward(ht);
    3123             :                 }
    3124           0 :                 smart_str_0(&list);
    3125           0 :                 xmlNodeSetContentLen(ret, BAD_CAST(list.c), list.len);
    3126           0 :                 smart_str_free(&list);
    3127             :         } else {
    3128           0 :                 zval tmp = *data;
    3129             :                 char *str, *start, *next;
    3130           0 :                 smart_str list = {0};
    3131             : 
    3132           0 :                 if (Z_TYPE_P(data) != IS_STRING) {
    3133             :                         zval_copy_ctor(&tmp);
    3134           0 :                         convert_to_string(&tmp);
    3135           0 :                         data = &tmp;
    3136             :                 }
    3137           0 :                 str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
    3138           0 :                 whiteSpace_collapse(BAD_CAST(str));
    3139           0 :                 start = str;
    3140           0 :                 while (start != NULL && *start != '\0') {
    3141             :                         xmlNodePtr dummy;
    3142             :                         zval dummy_zval;
    3143             : 
    3144           0 :                         next = strchr(start,' ');
    3145           0 :                         if (next != NULL) {
    3146           0 :                           *next = '\0';
    3147           0 :                           next++;
    3148             :                         }
    3149           0 :                         ZVAL_STRING(&dummy_zval, start, 0);
    3150           0 :                         dummy = master_to_xml(list_enc, &dummy_zval, SOAP_LITERAL, ret TSRMLS_CC);
    3151           0 :                         if (dummy && dummy->children && dummy->children->content) {
    3152           0 :                                 if (list.len != 0) {
    3153           0 :                                         smart_str_appendc(&list, ' ');
    3154             :                                 }
    3155           0 :                                 smart_str_appends(&list, (char*)dummy->children->content);
    3156             :                         } else {
    3157           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    3158             :                         }
    3159           0 :                         xmlUnlinkNode(dummy);
    3160           0 :                         xmlFreeNode(dummy);
    3161             : 
    3162           0 :                         start = next;
    3163             :                 }
    3164           0 :                 smart_str_0(&list);
    3165           0 :                 xmlNodeSetContentLen(ret, BAD_CAST(list.c), list.len);
    3166           0 :                 smart_str_free(&list);
    3167           0 :                 efree(str);
    3168           0 :                 if (data == &tmp) {zval_dtor(&tmp);}
    3169             :         }
    3170           0 :         return ret;
    3171             : }
    3172             : 
    3173           0 : static xmlNodePtr to_xml_list1(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC) {
    3174             :         /*FIXME: minLength=1 */
    3175           0 :         return to_xml_list(enc,data,style, parent TSRMLS_CC);
    3176             : }
    3177             : 
    3178           0 : static zval* to_zval_union(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC) {
    3179             :         /*FIXME*/
    3180           0 :         return to_zval_list(enc, data TSRMLS_CC);
    3181             : }
    3182             : 
    3183           0 : static xmlNodePtr to_xml_union(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC) {
    3184             :         /*FIXME*/
    3185           0 :         return to_xml_list(enc,data,style, parent TSRMLS_CC);
    3186             : }
    3187             : 
    3188           0 : static zval *to_zval_any(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    3189             : {
    3190             :         xmlBufferPtr buf;
    3191             :         zval *ret;
    3192             : 
    3193           0 :         if (SOAP_GLOBAL(sdl) && SOAP_GLOBAL(sdl)->elements && data->name) {
    3194           0 :                 smart_str nscat = {0};          
    3195             :                 sdlTypePtr *sdl_type;
    3196             : 
    3197           0 :                 if (data->ns && data->ns->href) {
    3198           0 :                         smart_str_appends(&nscat, (char*)data->ns->href);
    3199           0 :                         smart_str_appendc(&nscat, ':');                     
    3200             :                 }
    3201           0 :                 smart_str_appends(&nscat, (char*)data->name);
    3202           0 :                 smart_str_0(&nscat);
    3203             : 
    3204           0 :                 if (zend_hash_find(SOAP_GLOBAL(sdl)->elements, nscat.c, nscat.len+1, (void **)&sdl_type) == SUCCESS &&
    3205           0 :                     (*sdl_type)->encode) {
    3206           0 :                         smart_str_free(&nscat);
    3207           0 :                         return master_to_zval_int((*sdl_type)->encode, data TSRMLS_CC);
    3208             :                 }               
    3209           0 :                 smart_str_free(&nscat);
    3210             :         }
    3211             : 
    3212           0 :         buf = xmlBufferCreate();
    3213           0 :         xmlNodeDump(buf, NULL, data, 0, 0);
    3214           0 :         MAKE_STD_ZVAL(ret);
    3215           0 :         ZVAL_STRING(ret, (char*)xmlBufferContent(buf), 1);
    3216           0 :         xmlBufferFree(buf);
    3217           0 :         return ret;
    3218             : }
    3219             : 
    3220           0 : static xmlNodePtr to_xml_any(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3221             : {
    3222           0 :         xmlNodePtr ret = NULL;
    3223             : 
    3224           0 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    3225             :                 HashPosition pos;
    3226             :                 zval **el;
    3227           0 :                 encodePtr enc = get_conversion(XSD_ANYXML);
    3228             :                 char *name;
    3229             :                 uint name_len;
    3230             :                 ulong idx;
    3231             : 
    3232           0 :                 for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(data), &pos);
    3233           0 :                      zend_hash_get_current_data_ex(Z_ARRVAL_P(data), (void **) &el, &pos) == SUCCESS;
    3234           0 :                      zend_hash_move_forward_ex(Z_ARRVAL_P(data), &pos)) {
    3235           0 :                         ret = master_to_xml(enc, *el, style, parent TSRMLS_CC);
    3236           0 :                     if (ret &&
    3237           0 :                         ret->name != xmlStringTextNoenc &&
    3238           0 :                         zend_hash_get_current_key_ex(Z_ARRVAL_P(data), &name, &name_len, &idx, 0, &pos) == HASH_KEY_IS_STRING) {
    3239           0 :                                 xmlNodeSetName(ret, BAD_CAST(name));
    3240             :                     }
    3241             :                 }
    3242           0 :                 return ret;
    3243             :         }
    3244           0 :         if (Z_TYPE_P(data) == IS_STRING) {
    3245           0 :                 ret = xmlNewTextLen(BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
    3246             :         } else {
    3247           0 :                 zval tmp = *data;
    3248             : 
    3249             :                 zval_copy_ctor(&tmp);
    3250           0 :                 convert_to_string(&tmp);
    3251           0 :                 ret = xmlNewTextLen(BAD_CAST(Z_STRVAL(tmp)), Z_STRLEN(tmp));
    3252             :                 zval_dtor(&tmp);
    3253             :         }
    3254             : 
    3255           0 :         ret->name = xmlStringTextNoenc;
    3256           0 :         ret->parent = parent;
    3257           0 :         ret->doc = parent->doc;
    3258           0 :         ret->prev = parent->last;
    3259           0 :         ret->next = NULL;
    3260           0 :         if (parent->last) {
    3261           0 :                 parent->last->next = ret;
    3262             :         } else {
    3263           0 :                 parent->children = ret;
    3264             :         }
    3265           0 :         parent->last = ret;
    3266             : 
    3267           0 :         return ret;
    3268             : }
    3269             : 
    3270           0 : zval *sdl_guess_convert_zval(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC)
    3271             : {
    3272             :         sdlTypePtr type;
    3273             : 
    3274           0 :         type = enc->sdl_type;
    3275           0 :         if (type == NULL) {
    3276           0 :                 return guess_zval_convert(enc, data TSRMLS_CC);
    3277             :         }
    3278             : /*FIXME: restriction support
    3279             :         if (type && type->restrictions &&
    3280             :             data &&  data->children && data->children->content) {
    3281             :                 if (type->restrictions->whiteSpace && type->restrictions->whiteSpace->value) {
    3282             :                         if (strcmp(type->restrictions->whiteSpace->value,"replace") == 0) {
    3283             :                                 whiteSpace_replace(data->children->content);
    3284             :                         } else if (strcmp(type->restrictions->whiteSpace->value,"collapse") == 0) {
    3285             :                                 whiteSpace_collapse(data->children->content);
    3286             :                         }
    3287             :                 }
    3288             :                 if (type->restrictions->enumeration) {
    3289             :                         if (!zend_hash_exists(type->restrictions->enumeration,data->children->content,strlen(data->children->content)+1)) {
    3290             :                                 soap_error1(E_WARNING, "Encoding: Restriction: invalid enumeration value \"%s\"", data->children->content);
    3291             :                         }
    3292             :                 }
    3293             :                 if (type->restrictions->minLength &&
    3294             :                     strlen(data->children->content) < type->restrictions->minLength->value) {
    3295             :                   soap_error0(E_WARNING, "Encoding: Restriction: length less than 'minLength'");
    3296             :                 }
    3297             :                 if (type->restrictions->maxLength &&
    3298             :                     strlen(data->children->content) > type->restrictions->maxLength->value) {
    3299             :                   soap_error0(E_WARNING, "Encoding: Restriction: length greater than 'maxLength'");
    3300             :                 }
    3301             :                 if (type->restrictions->length &&
    3302             :                     strlen(data->children->content) != type->restrictions->length->value) {
    3303             :                   soap_error0(E_WARNING, "Encoding: Restriction: length is not equal to 'length'");
    3304             :                 }
    3305             :         }
    3306             : */
    3307           0 :         switch (type->kind) {
    3308             :                 case XSD_TYPEKIND_SIMPLE:
    3309           0 :                         if (type->encode && enc != &type->encode->details) {
    3310           0 :                                 return master_to_zval_int(type->encode, data TSRMLS_CC);
    3311             :                         } else {
    3312           0 :                                 return guess_zval_convert(enc, data TSRMLS_CC);
    3313             :                         }
    3314             :                         break;
    3315             :                 case XSD_TYPEKIND_LIST:
    3316           0 :                         return to_zval_list(enc, data TSRMLS_CC);
    3317             :                 case XSD_TYPEKIND_UNION:
    3318           0 :                         return to_zval_union(enc, data TSRMLS_CC);
    3319             :                 case XSD_TYPEKIND_COMPLEX:
    3320             :                 case XSD_TYPEKIND_RESTRICTION:
    3321             :                 case XSD_TYPEKIND_EXTENSION:
    3322           0 :                         if (type->encode &&
    3323           0 :                             (type->encode->details.type == IS_ARRAY ||
    3324           0 :                              type->encode->details.type == SOAP_ENC_ARRAY)) {
    3325           0 :                                 return to_zval_array(enc, data TSRMLS_CC);
    3326             :                         }
    3327           0 :                         return to_zval_object(enc, data TSRMLS_CC);
    3328             :                 default:
    3329           0 :                 soap_error0(E_ERROR, "Encoding: Internal Error");
    3330           0 :                         return guess_zval_convert(enc, data TSRMLS_CC);
    3331             :         }
    3332             : }
    3333             : 
    3334           0 : xmlNodePtr sdl_guess_convert_xml(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3335             : {
    3336             :         sdlTypePtr type;
    3337           0 :         xmlNodePtr ret = NULL;
    3338             : 
    3339           0 :         type = enc->sdl_type;
    3340             : 
    3341           0 :         if (type == NULL) {
    3342           0 :                 ret = guess_xml_convert(enc, data, style, parent TSRMLS_CC);
    3343           0 :                 if (style == SOAP_ENCODED) {
    3344           0 :                         set_ns_and_type(ret, enc);
    3345             :                 }
    3346           0 :                 return ret;
    3347             :         }
    3348             : /*FIXME: restriction support
    3349             :         if (type) {
    3350             :                 if (type->restrictions && Z_TYPE_P(data) == IS_STRING) {
    3351             :                         if (type->restrictions->enumeration) {
    3352             :                                 if (!zend_hash_exists(type->restrictions->enumeration,Z_STRVAL_P(data),Z_STRLEN_P(data)+1)) {
    3353             :                                         soap_error1(E_WARNING, "Encoding: Restriction: invalid enumeration value \"%s\".", Z_STRVAL_P(data));
    3354             :                                 }
    3355             :                         }
    3356             :                         if (type->restrictions->minLength &&
    3357             :                             Z_STRLEN_P(data) < type->restrictions->minLength->value) {
    3358             :                         soap_error0(E_WARNING, "Encoding: Restriction: length less than 'minLength'");
    3359             :                         }
    3360             :                         if (type->restrictions->maxLength &&
    3361             :                             Z_STRLEN_P(data) > type->restrictions->maxLength->value) {
    3362             :                         soap_error0(E_WARNING, "Encoding: Restriction: length greater than 'maxLength'");
    3363             :                         }
    3364             :                         if (type->restrictions->length &&
    3365             :                             Z_STRLEN_P(data) != type->restrictions->length->value) {
    3366             :                         soap_error0(E_WARNING, "Encoding: Restriction: length is not equal to 'length'");
    3367             :                         }
    3368             :                 }
    3369             :         }
    3370             : */
    3371           0 :         switch(type->kind) {
    3372             :                 case XSD_TYPEKIND_SIMPLE:
    3373           0 :                         if (type->encode && enc != &type->encode->details) {
    3374           0 :                                 ret = master_to_xml(type->encode, data, style, parent TSRMLS_CC);
    3375             :                         } else {
    3376           0 :                                 ret = guess_xml_convert(enc, data, style, parent TSRMLS_CC);
    3377             :                         }
    3378           0 :                         break;
    3379             :                 case XSD_TYPEKIND_LIST:
    3380           0 :                         ret = to_xml_list(enc, data, style, parent TSRMLS_CC);
    3381           0 :                         break;
    3382             :                 case XSD_TYPEKIND_UNION:
    3383           0 :                         ret = to_xml_union(enc, data, style, parent TSRMLS_CC);
    3384           0 :                         break;
    3385             :                 case XSD_TYPEKIND_COMPLEX:
    3386             :                 case XSD_TYPEKIND_RESTRICTION:
    3387             :                 case XSD_TYPEKIND_EXTENSION:
    3388           0 :                         if (type->encode &&
    3389           0 :                             (type->encode->details.type == IS_ARRAY ||
    3390           0 :                              type->encode->details.type == SOAP_ENC_ARRAY)) {
    3391           0 :                                 return to_xml_array(enc, data, style, parent TSRMLS_CC);
    3392             :                         } else {
    3393           0 :                                 return to_xml_object(enc, data, style, parent TSRMLS_CC);
    3394             :                         }
    3395             :                         break;
    3396             :                 default:
    3397           0 :                 soap_error0(E_ERROR, "Encoding: Internal Error");
    3398             :                         break;
    3399             :         }
    3400           0 :         if (style == SOAP_ENCODED) {
    3401           0 :                 set_ns_and_type(ret, enc);
    3402             :         }
    3403           0 :         return ret;
    3404             : }
    3405             : 
    3406           0 : static xmlNodePtr check_and_resolve_href(xmlNodePtr data)
    3407             : {
    3408           0 :         if (data && data->properties) {
    3409             :                 xmlAttrPtr href;
    3410             : 
    3411           0 :                 href = data->properties;
    3412             :                 while (1) {
    3413           0 :                         href = get_attribute(href, "href");
    3414           0 :                         if (href == NULL || href->ns == NULL) {break;}
    3415           0 :                         href = href->next;
    3416           0 :                 }
    3417           0 :                 if (href) {
    3418             :                         /*  Internal href try and find node */
    3419           0 :                         if (href->children->content[0] == '#') {
    3420           0 :                                 xmlNodePtr ret = get_node_with_attribute_recursive(data->doc->children, NULL, "id", (char*)&href->children->content[1]);
    3421           0 :                                 if (!ret) {
    3422           0 :                                         soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
    3423             :                                 }
    3424           0 :                                 return ret;
    3425             :                         } else {
    3426             :                                 /*  TODO: External href....? */
    3427           0 :                                 soap_error1(E_ERROR, "Encoding: External reference '%s'", href->children->content);
    3428             :                         }
    3429             :                 }
    3430             :                 /* SOAP 1.2 enc:id enc:ref */
    3431           0 :                 href = get_attribute_ex(data->properties, "ref", SOAP_1_2_ENC_NAMESPACE);
    3432           0 :                 if (href) {
    3433             :                         xmlChar* id;
    3434             :                         xmlNodePtr ret;
    3435             : 
    3436           0 :                         if (href->children->content[0] == '#') {
    3437           0 :                                 id = href->children->content+1;
    3438             :                         } else {
    3439           0 :                                 id = href->children->content;
    3440             :                         }
    3441           0 :                         ret = get_node_with_attribute_recursive_ex(data->doc->children, NULL, NULL, "id", (char*)id, SOAP_1_2_ENC_NAMESPACE);
    3442           0 :                         if (!ret) {
    3443           0 :                                 soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
    3444           0 :                         } else if (ret == data) {
    3445           0 :                                 soap_error1(E_ERROR, "Encoding: Violation of id and ref information items '%s'", href->children->content);
    3446             :                         }
    3447           0 :                         return ret;
    3448             :                 }
    3449             :         }
    3450           0 :         return data;
    3451             : }
    3452             : 
    3453           0 : static void set_ns_and_type(xmlNodePtr node, encodeTypePtr type)
    3454             : {
    3455           0 :         set_ns_and_type_ex(node, type->ns, type->type_str);
    3456           0 : }
    3457             : 
    3458           0 : static void set_ns_and_type_ex(xmlNodePtr node, char *ns, char *type)
    3459             : {
    3460           0 :         smart_str nstype = {0};
    3461           0 :         get_type_str(node, ns, type, &nstype);
    3462           0 :         set_xsi_type(node, nstype.c);
    3463           0 :         smart_str_free(&nstype);
    3464           0 : }
    3465             : 
    3466           0 : static xmlNsPtr xmlSearchNsPrefixByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
    3467             : {
    3468             :         xmlNsPtr cur;
    3469           0 :         xmlNodePtr orig = node;
    3470             : 
    3471           0 :         while (node) {
    3472           0 :                 if (node->type == XML_ENTITY_REF_NODE ||
    3473           0 :                     node->type == XML_ENTITY_NODE ||
    3474           0 :                     node->type == XML_ENTITY_DECL) {
    3475           0 :                         return NULL;
    3476             :                 }
    3477           0 :                 if (node->type == XML_ELEMENT_NODE) {
    3478           0 :                         cur = node->nsDef;
    3479           0 :                         while (cur != NULL) {
    3480           0 :                                 if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
    3481           0 :                                         if (xmlSearchNs(doc, node, cur->prefix) == cur) {
    3482           0 :                                                 return cur;
    3483             :                                         }
    3484             :                                 }
    3485           0 :                                 cur = cur->next;
    3486             :                         }
    3487           0 :                         if (orig != node) {
    3488           0 :                                 cur = node->ns;
    3489           0 :                                 if (cur != NULL) {
    3490           0 :                                         if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
    3491           0 :                                                 if (xmlSearchNs(doc, node, cur->prefix) == cur) {
    3492           0 :                                                         return cur;
    3493             :                                                 }
    3494             :                                         }
    3495             :                                 }
    3496             :                         }    
    3497             :                 }
    3498           0 :                 node = node->parent;
    3499             :         }
    3500           0 :         return NULL;
    3501             : }
    3502             : 
    3503           0 : xmlNsPtr encode_add_ns(xmlNodePtr node, const char* ns)
    3504             : {
    3505             :         xmlNsPtr xmlns;
    3506             : 
    3507           0 :         if (ns == NULL) {
    3508           0 :           return NULL;
    3509             :         }
    3510             : 
    3511           0 :         xmlns = xmlSearchNsByHref(node->doc, node, BAD_CAST(ns));
    3512           0 :         if (xmlns != NULL && xmlns->prefix == NULL) {
    3513           0 :                 xmlns = xmlSearchNsPrefixByHref(node->doc, node, BAD_CAST(ns));
    3514             :         }
    3515           0 :         if (xmlns == NULL) {
    3516             :                 xmlChar* prefix;
    3517             :         TSRMLS_FETCH();
    3518             : 
    3519           0 :                 if (zend_hash_find(&SOAP_GLOBAL(defEncNs), (char*)ns, strlen(ns) + 1, (void **)&prefix) == SUCCESS) {
    3520           0 :                         xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), prefix);
    3521             :                 } else {
    3522           0 :                         smart_str prefix = {0};
    3523           0 :                         int num = ++SOAP_GLOBAL(cur_uniq_ns);
    3524             : 
    3525             :                         while (1) {
    3526           0 :                                 smart_str_appendl(&prefix, "ns", 2);
    3527           0 :                                 smart_str_append_long(&prefix, num);
    3528           0 :                                 smart_str_0(&prefix);
    3529           0 :                                 if (xmlSearchNs(node->doc, node, BAD_CAST(prefix.c)) == NULL) {
    3530           0 :                                         break;
    3531             :                                 }
    3532           0 :                                 smart_str_free(&prefix);
    3533           0 :                                 prefix.c = NULL;
    3534           0 :                                 prefix.len = 0;
    3535           0 :                                 num = ++SOAP_GLOBAL(cur_uniq_ns);
    3536           0 :                         }
    3537             : 
    3538           0 :                         xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), BAD_CAST(prefix.c));
    3539           0 :                         smart_str_free(&prefix);
    3540             :                 }
    3541             :         }
    3542           0 :         return xmlns;
    3543             : }
    3544             : 
    3545           0 : static void set_ns_prop(xmlNodePtr node, char *ns, char *name, char *val)
    3546             : {
    3547           0 :         xmlSetNsProp(node, encode_add_ns(node, ns), BAD_CAST(name), BAD_CAST(val));
    3548           0 : }
    3549             : 
    3550           0 : static void set_xsi_nil(xmlNodePtr node)
    3551             : {
    3552           0 :         set_ns_prop(node, XSI_NAMESPACE, "nil", "true");
    3553           0 : }
    3554             : 
    3555           0 : static void set_xsi_type(xmlNodePtr node, char *type)
    3556             : {
    3557           0 :         set_ns_prop(node, XSI_NAMESPACE, "type", type);
    3558           0 : }
    3559             : 
    3560           0 : void encode_reset_ns()
    3561             : {
    3562             :         TSRMLS_FETCH();
    3563           0 :         SOAP_GLOBAL(cur_uniq_ns) = 0;
    3564           0 :         SOAP_GLOBAL(cur_uniq_ref) = 0;
    3565           0 :         if (SOAP_GLOBAL(ref_map)) {
    3566           0 :                 zend_hash_destroy(SOAP_GLOBAL(ref_map));
    3567             :         } else {
    3568           0 :                 SOAP_GLOBAL(ref_map) = emalloc(sizeof(HashTable));
    3569             :         }
    3570           0 :         zend_hash_init(SOAP_GLOBAL(ref_map), 0, NULL, NULL, 0);
    3571           0 : }
    3572             : 
    3573           0 : void encode_finish()
    3574             : {
    3575             :         TSRMLS_FETCH();
    3576           0 :         SOAP_GLOBAL(cur_uniq_ns) = 0;
    3577           0 :         SOAP_GLOBAL(cur_uniq_ref) = 0;
    3578           0 :         if (SOAP_GLOBAL(ref_map)) {
    3579           0 :                 zend_hash_destroy(SOAP_GLOBAL(ref_map));
    3580           0 :                 efree(SOAP_GLOBAL(ref_map));
    3581           0 :                 SOAP_GLOBAL(ref_map) = NULL;
    3582             :         }
    3583           0 : }
    3584             : 
    3585           0 : encodePtr get_conversion(int encode)
    3586             : {
    3587           0 :         encodePtr *enc = NULL;
    3588             :         TSRMLS_FETCH();
    3589             : 
    3590           0 :         if (zend_hash_index_find(&SOAP_GLOBAL(defEncIndex), encode, (void **)&enc) == FAILURE) {
    3591           0 :                 soap_error0(E_ERROR,  "Encoding: Cannot find encoding");
    3592           0 :                 return NULL;
    3593             :         } else {
    3594           0 :                 return *enc;
    3595             :         }
    3596             : }
    3597             : 
    3598           0 : static int is_map(zval *array)
    3599             : {
    3600           0 :         int i, count = zend_hash_num_elements(Z_ARRVAL_P(array));
    3601             : 
    3602           0 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(array));
    3603           0 :         for (i = 0; i < count; i++) {
    3604             :                 char *str_index;
    3605             :                 ulong num_index;
    3606             : 
    3607           0 :                 if (zend_hash_get_current_key(Z_ARRVAL_P(array), &str_index, &num_index, 0) == HASH_KEY_IS_STRING ||
    3608           0 :                     num_index != i) {
    3609           0 :                         return TRUE;
    3610             :                 }
    3611           0 :                 zend_hash_move_forward(Z_ARRVAL_P(array));
    3612             :         }
    3613           0 :         return FALSE;
    3614             : }
    3615             : 
    3616           0 : static encodePtr get_array_type(xmlNodePtr node, zval *array, smart_str *type TSRMLS_DC)
    3617             : {
    3618             :         HashTable *ht;
    3619             :         int i, count, cur_type, prev_type, different;
    3620             :         zval **tmp;
    3621           0 :         char *prev_stype = NULL, *cur_stype = NULL, *prev_ns = NULL, *cur_ns = NULL;
    3622             : 
    3623           0 :         if (!array || Z_TYPE_P(array) != IS_ARRAY) {
    3624           0 :                 smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
    3625           0 :                 return get_conversion(XSD_ANYTYPE);
    3626             :         }
    3627             : 
    3628           0 :         different = FALSE;
    3629           0 :         cur_type = prev_type = 0;
    3630           0 :         ht = HASH_OF(array);
    3631           0 :         count = zend_hash_num_elements(ht);
    3632             : 
    3633           0 :         zend_hash_internal_pointer_reset(ht);
    3634           0 :         for (i = 0;i < count;i++) {
    3635           0 :                 zend_hash_get_current_data(ht, (void **)&tmp);
    3636             : 
    3637           0 :                 if (Z_TYPE_PP(tmp) == IS_OBJECT &&
    3638           0 :                     Z_OBJCE_PP(tmp) == soap_var_class_entry) {
    3639             :                         zval **ztype;
    3640             : 
    3641           0 :                         if (zend_hash_find(Z_OBJPROP_PP(tmp), "enc_type", sizeof("enc_type"), (void **)&ztype) == FAILURE) {
    3642           0 :                                 soap_error0(E_ERROR,  "Encoding: SoapVar has no 'enc_type' property");
    3643             :                         }
    3644           0 :                         cur_type = Z_LVAL_PP(ztype);
    3645             : 
    3646           0 :                         if (zend_hash_find(Z_OBJPROP_PP(tmp), "enc_stype", sizeof("enc_stype"), (void **)&ztype) == SUCCESS) {
    3647           0 :                                 cur_stype = Z_STRVAL_PP(ztype);
    3648             :                         } else {
    3649           0 :                                 cur_stype = NULL;
    3650             :                         }
    3651             : 
    3652           0 :                         if (zend_hash_find(Z_OBJPROP_PP(tmp), "enc_ns", sizeof("enc_ns"), (void **)&ztype) == SUCCESS) {
    3653           0 :                                 cur_ns = Z_STRVAL_PP(ztype);
    3654             :                         } else {
    3655           0 :                                 cur_ns = NULL;
    3656             :                         }
    3657             : 
    3658           0 :                 } else if (Z_TYPE_PP(tmp) == IS_ARRAY && is_map(*tmp)) {
    3659           0 :                         cur_type = APACHE_MAP;
    3660           0 :                         cur_stype = NULL;
    3661           0 :                         cur_ns = NULL;
    3662             :                 } else {
    3663           0 :                         cur_type = Z_TYPE_PP(tmp);
    3664           0 :                         cur_stype = NULL;
    3665           0 :                         cur_ns = NULL;
    3666             :                 }
    3667             : 
    3668           0 :                 if (i > 0) {
    3669           0 :                         if ((cur_type != prev_type) ||
    3670           0 :                             (cur_stype != NULL && prev_stype != NULL && strcmp(cur_stype,prev_stype) != 0) ||
    3671             :                             (cur_stype == NULL && cur_stype != prev_stype) ||
    3672           0 :                             (cur_ns != NULL && prev_ns != NULL && strcmp(cur_ns,prev_ns) != 0) ||
    3673             :                             (cur_ns == NULL && cur_ns != prev_ns)) {
    3674           0 :                                 different = TRUE;
    3675           0 :                                 break;
    3676             :                         }
    3677             :                 }
    3678             : 
    3679           0 :                 prev_type = cur_type;
    3680           0 :                 prev_stype = cur_stype;
    3681           0 :                 prev_ns = cur_ns;
    3682           0 :                 zend_hash_move_forward(ht);
    3683             :         }
    3684             : 
    3685           0 :         if (different || count == 0) {
    3686           0 :                 smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
    3687           0 :                 return get_conversion(XSD_ANYTYPE);
    3688             :         } else {
    3689             :                 encodePtr enc;
    3690             : 
    3691           0 :                 if (cur_stype != NULL) {
    3692           0 :                         smart_str array_type = {0};
    3693             : 
    3694           0 :                         if (cur_ns) {
    3695           0 :                                 xmlNsPtr ns = encode_add_ns(node, cur_ns);
    3696             : 
    3697           0 :                                 smart_str_appends(type, (char*)ns->prefix);
    3698           0 :                                 smart_str_appendc(type, ':');
    3699           0 :                                 smart_str_appends(&array_type, cur_ns);
    3700           0 :                                 smart_str_appendc(&array_type, ':');
    3701             :                         }
    3702           0 :                         smart_str_appends(type, cur_stype);
    3703           0 :                         smart_str_0(type);
    3704           0 :                         smart_str_appends(&array_type, cur_stype);
    3705           0 :                         smart_str_0(&array_type);
    3706             : 
    3707           0 :                         enc = get_encoder_ex(SOAP_GLOBAL(sdl), array_type.c, array_type.len);
    3708           0 :                         smart_str_free(&array_type);
    3709           0 :                         return enc;
    3710             :                 } else {
    3711           0 :                         enc = get_conversion(cur_type);
    3712           0 :                         get_type_str(node, enc->details.ns, enc->details.type_str, type);
    3713           0 :                         return enc;
    3714             :                 }
    3715             :         }
    3716             : }
    3717             : 
    3718           0 : static void get_type_str(xmlNodePtr node, const char* ns, const char* type, smart_str* ret)
    3719             : {
    3720             :         TSRMLS_FETCH();
    3721             : 
    3722           0 :         if (ns) {
    3723             :                 xmlNsPtr xmlns;
    3724           0 :                 if (SOAP_GLOBAL(soap_version) == SOAP_1_2 &&
    3725           0 :                     strcmp(ns,SOAP_1_1_ENC_NAMESPACE) == 0) {
    3726           0 :                         ns = SOAP_1_2_ENC_NAMESPACE;
    3727           0 :                 } else if (SOAP_GLOBAL(soap_version) == SOAP_1_1 &&
    3728           0 :                            strcmp(ns,SOAP_1_2_ENC_NAMESPACE) == 0) {
    3729           0 :                         ns = SOAP_1_1_ENC_NAMESPACE;
    3730             :                 }
    3731           0 :                 xmlns = encode_add_ns(node, ns);
    3732           0 :                 smart_str_appends(ret, (char*)xmlns->prefix);
    3733           0 :                 smart_str_appendc(ret, ':');
    3734             :         }
    3735           0 :         smart_str_appendl(ret, type, strlen(type));
    3736           0 :         smart_str_0(ret);
    3737           0 : }
    3738             : 
    3739           0 : static void delete_mapping(void *data)
    3740             : {
    3741           0 :         soapMappingPtr map = (soapMappingPtr)data;
    3742             : 
    3743           0 :         if (map->to_xml) {
    3744           0 :                 zval_ptr_dtor(&map->to_xml);
    3745             :         }
    3746           0 :         if (map->to_zval) {
    3747           0 :                 zval_ptr_dtor(&map->to_zval);
    3748             :         }
    3749           0 :         efree(map);
    3750           0 : }
    3751             : 
    3752           0 : void delete_encoder(void *encode)
    3753             : {
    3754           0 :         encodePtr t = *((encodePtr*)encode);
    3755           0 :         if (t->details.ns) {
    3756           0 :                 efree(t->details.ns);
    3757             :         }
    3758           0 :         if (t->details.type_str) {
    3759           0 :                 efree(t->details.type_str);
    3760             :         }
    3761           0 :         if (t->details.map) {
    3762           0 :                 delete_mapping(t->details.map);
    3763             :         }
    3764           0 :         efree(t);
    3765           0 : }
    3766             : 
    3767           0 : void delete_encoder_persistent(void *encode)
    3768             : {
    3769           0 :         encodePtr t = *((encodePtr*)encode);
    3770           0 :         if (t->details.ns) {
    3771           0 :                 free(t->details.ns);
    3772             :         }
    3773           0 :         if (t->details.type_str) {
    3774           0 :                 free(t->details.type_str);
    3775             :         }
    3776             :         /* we should never have mapping in persistent encoder */
    3777             :         assert(t->details.map == NULL);
    3778           0 :         free(t);
    3779           0 : }

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:56 +0000 (8 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.