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: 1749 2035 85.9 %
Date: 2014-08-04 Functions: 84 87 96.6 %
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        1450 : void whiteSpace_replace(xmlChar* str)
     241             : {
     242       14218 :         while (*str != '\0') {
     243       11318 :                 if (*str == '\x9' || *str == '\xA' || *str == '\xD') {
     244           2 :                         *str = ' ';
     245             :                 }
     246       11318 :                 str++;
     247             :         }
     248        1450 : }
     249             : 
     250        1450 : void whiteSpace_collapse(xmlChar* str)
     251             : {
     252             :         xmlChar *pos;
     253             :         xmlChar old;
     254             : 
     255        1450 :         pos = str;
     256        1450 :         whiteSpace_replace(str);
     257        2909 :         while (*str == ' ') {
     258           9 :                 str++;
     259             :         }
     260        1450 :         old = '\0';
     261       14209 :         while (*str != '\0') {
     262       11309 :                 if (*str != ' ' || old != ' ') {
     263       11303 :                         *pos = *str;
     264       11303 :                         pos++;
     265             :                 }
     266       11309 :                 old = *str;
     267       11309 :                 str++;
     268             :         }
     269        1450 :         if (old == ' ') {
     270           1 :                 --pos;
     271             :         }
     272        1450 :         *pos = '\0';
     273        1450 : }
     274             : 
     275          16 : static encodePtr find_encoder_by_type_name(sdlPtr sdl, const char *type)
     276             : {
     277          16 :         if (sdl && sdl->encoders) {
     278             :                 HashPosition pos;
     279             :                 encodePtr *enc;
     280             : 
     281         179 :                 for (zend_hash_internal_pointer_reset_ex(sdl->encoders, &pos);
     282         163 :                      zend_hash_get_current_data_ex(sdl->encoders, (void **) &enc, &pos) == SUCCESS;
     283         147 :                      zend_hash_move_forward_ex(sdl->encoders, &pos)) {
     284         163 :                     if (strcmp((*enc)->details.type_str, type) == 0) {
     285          16 :                                 return *enc;
     286             :                         }
     287             :                 }
     288             :         }
     289           0 :         return NULL;
     290             : }
     291             : 
     292         551 : static zend_bool soap_check_zval_ref(zval *data, xmlNodePtr node TSRMLS_DC) {
     293             :         xmlNodePtr *node_ptr;
     294             : 
     295         551 :         if (SOAP_GLOBAL(ref_map)) {
     296         551 :                 if (Z_TYPE_P(data) == IS_OBJECT) {
     297         397 :                         data = (zval*)zend_objects_get_address(data TSRMLS_CC);
     298             :                 }
     299         551 :                 if (zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)data, (void**)&node_ptr) == SUCCESS) {
     300          65 :                         xmlAttrPtr attr = (*node_ptr)->properties;
     301             :                         char *id;
     302          65 :                         smart_str prefix = {0};
     303             : 
     304          65 :                         if (*node_ptr == node) {
     305          53 :                                 return 0;
     306             :                         }
     307          12 :                         xmlNodeSetName(node, (*node_ptr)->name);
     308          12 :                         xmlSetNs(node, (*node_ptr)->ns);
     309          12 :                         if (SOAP_GLOBAL(soap_version) == SOAP_1_1) {
     310             :                                 while (1) {
     311           9 :                                         attr = get_attribute(attr, "id");
     312           9 :                                         if (attr == NULL || attr->ns == NULL) {
     313             :                                                 break;
     314             :                                         }
     315           0 :                                         attr = attr->next;
     316           0 :                                 }
     317           9 :                                 if (attr) {
     318           2 :                                         id = (char*)attr->children->content;
     319           2 :                                         smart_str_appendc(&prefix, '#');
     320           2 :                                         smart_str_appends(&prefix, id);
     321           2 :                                         smart_str_0(&prefix);
     322           2 :                                         id = prefix.c;
     323             :                                 } else {
     324           7 :                                         SOAP_GLOBAL(cur_uniq_ref)++;
     325           7 :                                         smart_str_appendl(&prefix, "#ref", 4);
     326           7 :                                         smart_str_append_long(&prefix, SOAP_GLOBAL(cur_uniq_ref));
     327           7 :                                         smart_str_0(&prefix);
     328           7 :                                         id = prefix.c;
     329           7 :                                         xmlSetProp((*node_ptr), BAD_CAST("id"), BAD_CAST(id+1));
     330             :                                 }
     331           9 :                                 xmlSetProp(node, BAD_CAST("href"), BAD_CAST(id));
     332             :                         } else {
     333           3 :                                 attr = get_attribute_ex(attr, "id", SOAP_1_2_ENC_NAMESPACE);
     334           3 :                                 if (attr) {
     335           2 :                                         id = (char*)attr->children->content;
     336           2 :                                         smart_str_appendc(&prefix, '#');
     337           2 :                                         smart_str_appends(&prefix, id);
     338           2 :                                         smart_str_0(&prefix);
     339           2 :                                         id = prefix.c;
     340             :                                 } else {
     341           1 :                                         SOAP_GLOBAL(cur_uniq_ref)++;
     342           1 :                                         smart_str_appendl(&prefix, "#ref", 4);
     343           1 :                                         smart_str_append_long(&prefix, SOAP_GLOBAL(cur_uniq_ref));
     344           1 :                                         smart_str_0(&prefix);
     345           1 :                                         id = prefix.c;
     346           1 :                                         set_ns_prop((*node_ptr), SOAP_1_2_ENC_NAMESPACE, "id", id+1);
     347             :                                 }
     348           3 :                                 set_ns_prop(node, SOAP_1_2_ENC_NAMESPACE, "ref", id);
     349             :                         }
     350          12 :                         smart_str_free(&prefix);
     351          12 :                         return 1;
     352             :                 } else {
     353         486 :                         zend_hash_index_update(SOAP_GLOBAL(ref_map), (ulong)data, (void**)&node, sizeof(xmlNodePtr), NULL);
     354             :                 }
     355             :         }
     356         486 :         return 0;
     357             : }
     358             : 
     359          46 : static zval* soap_find_xml_ref(xmlNodePtr node TSRMLS_DC)
     360             : {
     361             :         zval **data_ptr;
     362             : 
     363          92 :         if (SOAP_GLOBAL(ref_map) && 
     364          46 :             zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)node, (void**)&data_ptr) == SUCCESS) {
     365           1 :                 Z_SET_ISREF_PP(data_ptr);
     366           1 :                 Z_ADDREF_PP(data_ptr);
     367           1 :                 return *data_ptr;
     368             :         }
     369          45 :         return NULL;
     370             : }
     371             : 
     372         488 : static zend_bool soap_check_xml_ref(zval **data, xmlNodePtr node TSRMLS_DC)
     373             : {
     374             :         zval **data_ptr;
     375             : 
     376         488 :         if (SOAP_GLOBAL(ref_map)) {
     377         488 :                 if (zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)node, (void**)&data_ptr) == SUCCESS) {
     378          49 :                         if (*data != *data_ptr) {
     379           4 :                                 zval_ptr_dtor(data);
     380           4 :                                 *data = *data_ptr;
     381           4 :                                 Z_SET_ISREF_PP(data);
     382           4 :                                 Z_ADDREF_PP(data);
     383           4 :                                 return 1;
     384             :                         }
     385             :                 } else {
     386         439 :                         zend_hash_index_update(SOAP_GLOBAL(ref_map), (ulong)node, (void**)data, sizeof(zval*), NULL);
     387             :                 }
     388             :         }
     389         484 :         return 0;
     390             : }
     391             : 
     392        2805 : static xmlNodePtr master_to_xml_int(encodePtr encode, zval *data, int style, xmlNodePtr parent, int check_class_map TSRMLS_DC)
     393             : {
     394        2805 :         xmlNodePtr node = NULL;
     395        2805 :         int add_type = 0;
     396             : 
     397             :         /* Special handling of class SoapVar */
     398        6220 :         if (data &&
     399        2788 :             Z_TYPE_P(data) == IS_OBJECT &&
     400         521 :             Z_OBJCE_P(data) == soap_var_class_entry) {
     401             :                 zval **ztype, **zdata, **zns, **zstype, **zname, **znamens;
     402         106 :                 encodePtr enc = NULL;
     403         106 :                 HashTable *ht = Z_OBJPROP_P(data);
     404             : 
     405         106 :                 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         106 :                 if (zend_hash_find(ht, "enc_stype", sizeof("enc_stype"), (void **)&zstype) == SUCCESS) {
     410          22 :                         if (zend_hash_find(ht, "enc_ns", sizeof("enc_ns"), (void **)&zns) == SUCCESS) {
     411          22 :                                 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          22 :                         if (enc == NULL && SOAP_GLOBAL(typemap)) {
     417             :                                 encodePtr *new_enc;
     418           2 :                                 smart_str nscat = {0};
     419             : 
     420           2 :                                 if (zns != NULL) {
     421           2 :                                         smart_str_appendl(&nscat, Z_STRVAL_PP(zns), Z_STRLEN_PP(zns));
     422           2 :                                         smart_str_appendc(&nscat, ':');
     423             :                                 }
     424           2 :                                 smart_str_appendl(&nscat, Z_STRVAL_PP(zstype), Z_STRLEN_PP(zstype));
     425           2 :                                 smart_str_0(&nscat);
     426           2 :                                 if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
     427           2 :                                         enc = *new_enc;
     428             :                                 }
     429           2 :                                 smart_str_free(&nscat);                     
     430             :                         }
     431             :                 }
     432         106 :                 if (enc == NULL) {
     433         100 :                         enc = get_conversion(Z_LVAL_P(*ztype));
     434             :                 }
     435         106 :                 if (enc == NULL) {
     436           0 :                         enc = encode;
     437             :                 }
     438             : 
     439         106 :                 if (zend_hash_find(ht, "enc_value", sizeof("enc_value"), (void **)&zdata) == FAILURE) {
     440           2 :                         node = master_to_xml(enc, NULL, style, parent TSRMLS_CC);
     441             :                 } else {
     442         104 :                         node = master_to_xml(enc, *zdata, style, parent TSRMLS_CC);
     443             :                 }
     444             : 
     445         106 :                 if (style == SOAP_ENCODED || (SOAP_GLOBAL(sdl) && encode != enc)) {
     446         106 :                         if (zend_hash_find(ht, "enc_stype", sizeof("enc_stype"), (void **)&zstype) == SUCCESS) {
     447          22 :                                 if (zend_hash_find(ht, "enc_ns", sizeof("enc_ns"), (void **)&zns) == SUCCESS) {
     448          22 :                                         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         106 :                 if (zend_hash_find(ht, "enc_name", sizeof("enc_name"), (void **)&zname) == SUCCESS) {
     456          25 :                         xmlNodeSetName(node, BAD_CAST(Z_STRVAL_PP(zname)));
     457             :                 }
     458         106 :                 if (zend_hash_find(ht, "enc_namens", sizeof("enc_namens"), (void **)&znamens) == SUCCESS) {
     459           2 :                         xmlNsPtr nsp = encode_add_ns(node, Z_STRVAL_PP(znamens));
     460           2 :                         xmlSetNs(node, nsp);
     461             :                 }
     462             :         } else {
     463        2855 :                 if (check_class_map && SOAP_GLOBAL(class_map) && data &&
     464         118 :                     Z_TYPE_P(data) == IS_OBJECT &&
     465          38 :                     !Z_OBJPROP_P(data)->nApplyCount) {
     466          23 :                         zend_class_entry *ce = Z_OBJCE_P(data);
     467             :                         HashPosition pos;
     468             :                         zval **tmp;
     469          23 :                         char *type_name = NULL;
     470             :                         uint type_len;
     471             :                         ulong idx;
     472             : 
     473          65 :                         for (zend_hash_internal_pointer_reset_ex(SOAP_GLOBAL(class_map), &pos);
     474          42 :                              zend_hash_get_current_data_ex(SOAP_GLOBAL(class_map), (void **) &tmp, &pos) == SUCCESS;
     475          19 :                              zend_hash_move_forward_ex(SOAP_GLOBAL(class_map), &pos)) {
     476         135 :                                 if (Z_TYPE_PP(tmp) == IS_STRING &&
     477          42 :                                     ce->name_length == Z_STRLEN_PP(tmp) &&
     478          28 :                                     zend_binary_strncasecmp(ce->name, ce->name_length, Z_STRVAL_PP(tmp), ce->name_length, ce->name_length) == 0 &&
     479          23 :                                     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          23 :                                         encodePtr enc = NULL;
     483          23 :                                         if (SOAP_GLOBAL(sdl)) {
     484          22 :                                                 enc = get_encoder(SOAP_GLOBAL(sdl), SOAP_GLOBAL(sdl)->target_ns, type_name);
     485          22 :                                                 if (!enc) {
     486          16 :                                                         enc = find_encoder_by_type_name(SOAP_GLOBAL(sdl), type_name);
     487             :                                                 }
     488             :                                         }
     489          23 :                                         if (enc) {
     490          22 :                                                 if (encode != enc && style == SOAP_LITERAL) {
     491           0 :                                                         add_type = 1;                                           
     492             :                                                 }
     493          22 :                                                 encode = enc;
     494             :                                         }
     495          23 :                                         break;
     496             :                                 }
     497             :                         }
     498             :                 }
     499             : 
     500        2699 :                 if (encode == NULL) {
     501         128 :                         encode = get_conversion(UNKNOWN_TYPE);
     502             :                 }
     503        2699 :                 if (SOAP_GLOBAL(typemap) && encode->details.type_str) {
     504          18 :                         smart_str nscat = {0};
     505             :                         encodePtr *new_enc;
     506             : 
     507          18 :                         if (encode->details.ns) {
     508          18 :                                 smart_str_appends(&nscat, encode->details.ns);
     509          18 :                                 smart_str_appendc(&nscat, ':');
     510             :                         }
     511          18 :                         smart_str_appends(&nscat, encode->details.type_str);
     512          18 :                         smart_str_0(&nscat);
     513          18 :                         if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
     514           7 :                                 encode = *new_enc;
     515             :                         }
     516          18 :                         smart_str_free(&nscat);                     
     517             :                 }
     518        2699 :                 if (encode->to_xml) {
     519        2699 :                         node = encode->to_xml(&encode->details, data, style, parent TSRMLS_CC);
     520        2695 :                         if (add_type) {
     521           0 :                                 set_ns_and_type(node, &encode->details);
     522             :                         }
     523             :                 }
     524             :         }
     525        2801 :         return node;
     526             : }
     527             : 
     528        2671 : xmlNodePtr master_to_xml(encodePtr encode, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
     529             : {
     530        2671 :         return master_to_xml_int(encode, data, style, parent, 1 TSRMLS_CC);
     531             : }
     532             : 
     533        5353 : static zval *master_to_zval_int(encodePtr encode, xmlNodePtr data TSRMLS_DC)
     534             : {
     535        5353 :         zval *ret = NULL;
     536             : 
     537        5353 :         if (SOAP_GLOBAL(typemap)) {
     538          19 :                 if (encode->details.type_str) {
     539          11 :                         smart_str nscat = {0};
     540             :                         encodePtr *new_enc;
     541             : 
     542          11 :                         if (encode->details.ns) {
     543          11 :                                 smart_str_appends(&nscat, encode->details.ns);
     544          11 :                                 smart_str_appendc(&nscat, ':');
     545             :                         }
     546          11 :                         smart_str_appends(&nscat, encode->details.type_str);
     547          11 :                         smart_str_0(&nscat);
     548          11 :                         if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
     549           4 :                                 encode = *new_enc;
     550             :                         }
     551          11 :                         smart_str_free(&nscat);                     
     552             :                 } else {
     553           8 :                         xmlAttrPtr type_attr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
     554             : 
     555           8 :                         if (type_attr != NULL) {
     556             :                                 encodePtr *new_enc;
     557             :                                 xmlNsPtr nsptr;
     558             :                                 char *ns, *cptype;
     559           6 :                                 smart_str nscat = {0};
     560             : 
     561           6 :                                 parse_namespace(type_attr->children->content, &cptype, &ns);
     562           6 :                                 nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
     563           6 :                                 if (nsptr != NULL) {
     564           6 :                                         smart_str_appends(&nscat, (char*)nsptr->href);
     565           6 :                                         smart_str_appendc(&nscat, ':');
     566             :                                 }
     567           6 :                                 smart_str_appends(&nscat, cptype);
     568           6 :                                 smart_str_0(&nscat);
     569           6 :                                 efree(cptype);
     570           6 :                                 if (ns) {efree(ns);}
     571           6 :                                 if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
     572           2 :                                         encode = *new_enc;
     573             :                                 }
     574           6 :                                 smart_str_free(&nscat);                     
     575             :                         }
     576             :                 }
     577             :         }
     578        5353 :         if (encode->to_zval) {
     579        5353 :                 ret = encode->to_zval(&encode->details, data TSRMLS_CC);
     580             :         }
     581        5344 :         return ret;
     582             : }
     583             : 
     584        3917 : zval *master_to_zval(encodePtr encode, xmlNodePtr data TSRMLS_DC)
     585             : {
     586        3917 :         data = check_and_resolve_href(data);
     587             : 
     588        3915 :         if (encode == NULL) {
     589        1362 :                 encode = get_conversion(UNKNOWN_TYPE);
     590             :         } else {
     591             :                 /* Use xsi:type if it is defined */
     592        2553 :                 xmlAttrPtr type_attr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
     593             : 
     594        2553 :                 if (type_attr != NULL) {
     595        1881 :                         encodePtr  enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, type_attr->children->content);
     596             : 
     597        1881 :                         if (enc != NULL && enc != encode) {
     598         649 :                           encodePtr tmp = enc;
     599        1962 :                           while (tmp &&
     600         652 :                                  tmp->details.sdl_type != NULL &&
     601           9 :                                  tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
     602           6 :                             if (enc == tmp->details.sdl_type->encode ||
     603           3 :                                 tmp == tmp->details.sdl_type->encode) {
     604           0 :                                 enc = NULL;
     605           0 :                                 break;
     606             :                             }
     607           3 :                             tmp = tmp->details.sdl_type->encode;
     608             :                           }
     609         649 :                           if (enc != NULL) {
     610         649 :                             encode = enc;
     611             :                           }
     612             :                         }
     613             :                 }
     614             :         }
     615        3915 :         return master_to_zval_int(encode, data TSRMLS_CC);
     616             : }
     617             : 
     618           7 : xmlNodePtr to_xml_user(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
     619             : {
     620           7 :         xmlNodePtr ret = NULL;
     621             :         zval *return_value;
     622             : 
     623           7 :         if (type && type->map && type->map->to_xml) {
     624           7 :                 MAKE_STD_ZVAL(return_value);
     625             : 
     626           7 :                 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           7 :                 if (Z_TYPE_P(return_value) == IS_STRING) {              
     630           5 :                         xmlDocPtr doc = soap_xmlParseMemory(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
     631           5 :                         if (doc && doc->children) {                          
     632           5 :                                 ret = xmlDocCopyNode(doc->children, parent->doc, 1);
     633             :                         }
     634           5 :                         xmlFreeDoc(doc);
     635             :                 }
     636             : 
     637           7 :                 zval_ptr_dtor(&return_value);
     638             :         }
     639           7 :         if (!ret) {
     640           2 :                 ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     641             :         }
     642           7 :         xmlAddChild(parent, ret);
     643           7 :         if (style == SOAP_ENCODED) {
     644           7 :                 set_ns_and_type(ret, type);
     645             :         }
     646           7 :         return ret;
     647             : }
     648             : 
     649           6 : zval *to_zval_user(encodeTypePtr type, xmlNodePtr node TSRMLS_DC)
     650             : {
     651             :         zval *return_value;
     652             : 
     653          12 :         if (type && type->map && type->map->to_zval) {
     654             :                 xmlBufferPtr buf;
     655             :                 zval *data;
     656             :                 xmlNodePtr copy;
     657             : 
     658           6 :                 copy = xmlCopyNode(node, 1);
     659           6 :                 buf = xmlBufferCreate();
     660           6 :                 xmlNodeDump(buf, NULL, copy, 0, 0);
     661           6 :                 MAKE_STD_ZVAL(data);
     662           6 :                 ZVAL_STRING(data, (char*)xmlBufferContent(buf), 1);
     663           6 :                 xmlBufferFree(buf);
     664           6 :                 xmlFreeNode(copy);
     665             : 
     666           6 :                 ALLOC_INIT_ZVAL(return_value);
     667             :                 
     668           6 :                 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           6 :                 zval_ptr_dtor(&data);
     672             :         } else {
     673           0 :                 ALLOC_INIT_ZVAL(return_value);
     674             :         }
     675           6 :         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        1250 : static zval *to_zval_string(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
     681             : {
     682             :         zval *ret;
     683        1250 :         MAKE_STD_ZVAL(ret);
     684        1250 :         FIND_XML_NULL(data, ret);
     685        2480 :         if (data && data->children) {
     686        2475 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     687        1237 :                         if (SOAP_GLOBAL(encoding) != NULL) {
     688         804 :                                 xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
     689         804 :                                 xmlBufferPtr out = xmlBufferCreate();
     690         804 :                                 int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
     691             : 
     692         804 :                                 if (n >= 0) {
     693         804 :                                         ZVAL_STRING(ret, (char*)xmlBufferContent(out), 1);
     694             :                                 } else {
     695           0 :                                         ZVAL_STRING(ret, (char*)data->children->content, 1);
     696             :                                 }
     697         804 :                                 xmlBufferFree(out);
     698         804 :                                 xmlBufferFree(in);
     699             :                         } else {
     700         433 :                                 ZVAL_STRING(ret, (char*)data->children->content, 1);
     701             :                         }
     702           1 :                 } 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           1 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     706             :                 }
     707             :         } else {
     708           5 :                 ZVAL_EMPTY_STRING(ret);
     709             :         }
     710        1242 :         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         242 : static zval *to_zval_stringc(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
     748             : {
     749             :         zval *ret;
     750         242 :         MAKE_STD_ZVAL(ret);
     751         242 :         FIND_XML_NULL(data, ret);
     752         484 :         if (data && data->children) {
     753         484 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     754         242 :                         whiteSpace_collapse(data->children->content);
     755         242 :                         if (SOAP_GLOBAL(encoding) != NULL) {
     756         200 :                                 xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
     757         200 :                                 xmlBufferPtr out = xmlBufferCreate();
     758         200 :                                 int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
     759             : 
     760         200 :                                 if (n >= 0) {
     761         200 :                                         ZVAL_STRING(ret, (char*)xmlBufferContent(out), 1);
     762             :                                 } else {
     763           0 :                                         ZVAL_STRING(ret, (char*)data->children->content, 1);
     764             :                                 }
     765         200 :                                 xmlBufferFree(out);
     766         200 :                                 xmlBufferFree(in);
     767             :                         } else {
     768          42 :                                 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         242 :         return ret;
     779             : }
     780             : 
     781           4 : static zval *to_zval_base64(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
     782             : {
     783             :         zval *ret;
     784             :         char *str;
     785             :         int str_len;
     786             : 
     787           4 :         MAKE_STD_ZVAL(ret);
     788           4 :         FIND_XML_NULL(data, ret);
     789           8 :         if (data && data->children) {
     790           8 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     791           4 :                         whiteSpace_collapse(data->children->content);
     792           4 :                         str = (char*)php_base64_decode(data->children->content, strlen((char*)data->children->content), &str_len);
     793           4 :                         if (!str) {
     794           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     795             :                         }
     796           4 :                         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           4 :         return ret;
     810             : }
     811             : 
     812           3 : 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           3 :         MAKE_STD_ZVAL(ret);
     820           3 :         FIND_XML_NULL(data, ret);
     821           6 :         if (data && data->children) {
     822           6 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
     823           3 :                         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           3 :                 str_len = strlen((char*)data->children->content) / 2;
     829           3 :                 str = emalloc(str_len+1);
     830          20 :                 for (i = j = 0; i < str_len; i++) {
     831          17 :                         c = data->children->content[j++];
     832          33 :                         if (c >= '0' && c <= '9') {
     833          16 :                                 str[i] = (c - '0') << 4;
     834           1 :                         } else if (c >= 'a' && c <= 'f') {
     835           0 :                                 str[i] = (c - 'a' + 10) << 4;
     836           2 :                         } else if (c >= 'A' && c <= 'F') {
     837           1 :                                 str[i] = (c - 'A' + 10) << 4;
     838             :                         } else {
     839           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     840             :                         }
     841          17 :                         c = data->children->content[j++];
     842          30 :                         if (c >= '0' && c <= '9') {
     843          13 :                                 str[i] |= c - '0';
     844           4 :                         } else if (c >= 'a' && c <= 'f') {
     845           0 :                                 str[i] |= c - 'a' + 10;
     846           8 :                         } else if (c >= 'A' && c <= 'F') {
     847           4 :                                 str[i] |= c - 'A' + 10;
     848             :                         } else {
     849           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
     850             :                         }
     851             :                 }
     852           3 :                 str[str_len] = '\0';
     853           3 :                 ZVAL_STRINGL(ret, (char*)str, str_len, 0);
     854             :         } else {
     855           0 :                 ZVAL_EMPTY_STRING(ret);
     856             :         }
     857           3 :         return ret;
     858             : }
     859             : 
     860         815 : 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         815 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     867         815 :         xmlAddChild(parent, ret);
     868         815 :         FIND_ZVAL_NULL(data, ret, style);
     869             : 
     870         807 :         if (Z_TYPE_P(data) == IS_STRING) {
     871         805 :                 str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
     872         805 :                 new_len = Z_STRLEN_P(data);
     873             :         } else {
     874           2 :                 zval tmp = *data;
     875             : 
     876             :                 zval_copy_ctor(&tmp);
     877           2 :                 convert_to_string(&tmp);
     878           2 :                 str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
     879           2 :                 new_len = Z_STRLEN(tmp);
     880             :                 zval_dtor(&tmp);
     881             :         }
     882             : 
     883         807 :         if (SOAP_GLOBAL(encoding) != NULL) {
     884          46 :                 xmlBufferPtr in  = xmlBufferCreateStatic(str, new_len);
     885          46 :                 xmlBufferPtr out = xmlBufferCreate();
     886          46 :                 int n = xmlCharEncInFunc(SOAP_GLOBAL(encoding), out, in);
     887             : 
     888          46 :                 if (n >= 0) {
     889          46 :                         efree(str);
     890          46 :                         str = estrdup((char*)xmlBufferContent(out));
     891          46 :                         new_len = n;
     892             :                 }
     893          46 :                 xmlBufferFree(out);
     894          46 :                 xmlBufferFree(in);
     895             :         }
     896             : 
     897         807 :         if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) {
     898           1 :                 char *err = emalloc(new_len + 8);
     899             :                 char c;
     900             :                 int i;
     901             :                 
     902           1 :                 memcpy(err, str, new_len+1);
     903           7 :                 for (i = 0; (c = err[i++]);) {
     904           6 :                         if ((c & 0x80) == 0) {
     905           1 :                         } else if ((c & 0xe0) == 0xc0) {
     906           0 :                                 if ((err[i] & 0xc0) != 0x80) {
     907           0 :                                         break;
     908             :                                 }
     909           0 :                                 i++;
     910           1 :                         } 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           1 :                         } 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           1 :                                 break;
     922             :                         }
     923             :                 }
     924           1 :                 if (c) {
     925           1 :                         err[i-1] = '\\';
     926           1 :                         err[i++] = 'x';
     927           1 :                         err[i++] = ((unsigned char)c >> 4) + ((((unsigned char)c >> 4) > 9) ? ('a' - 10) : '0');
     928           1 :                         err[i++] = (c & 15) + (((c & 15) > 9) ? ('a' - 10) : '0');
     929           1 :                         err[i++] = '.';
     930           1 :                         err[i++] = '.';
     931           1 :                         err[i++] = '.';
     932           1 :                         err[i++] = 0;
     933             :                 }
     934             : 
     935           1 :                 soap_error1(E_ERROR,  "Encoding: string '%s' is not a valid utf-8 string", err);
     936             :         }
     937             : 
     938         806 :         text = xmlNewTextLen(BAD_CAST(str), new_len);
     939         806 :         xmlAddChild(ret, text);
     940         806 :         efree(str);
     941             : 
     942         806 :         if (style == SOAP_ENCODED) {
     943         431 :                 set_ns_and_type(ret, type);
     944             :         }
     945         806 :         return ret;
     946             : }
     947             : 
     948           8 : 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           8 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     955           8 :         xmlAddChild(parent, ret);
     956           8 :         FIND_ZVAL_NULL(data, ret, style);
     957             : 
     958           8 :         if (Z_TYPE_P(data) == IS_STRING) {
     959           8 :                 str = php_base64_encode((unsigned char*)Z_STRVAL_P(data), Z_STRLEN_P(data), &str_len);
     960           8 :                 text = xmlNewTextLen(str, str_len);
     961           8 :                 xmlAddChild(ret, text);
     962           8 :                 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           8 :         if (style == SOAP_ENCODED) {
     976           6 :                 set_ns_and_type(ret, type);
     977             :         }
     978           8 :         return ret;
     979             : }
     980             : 
     981           7 : 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           7 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     990           7 :         xmlAddChild(parent, ret);
     991           7 :         FIND_ZVAL_NULL(data, ret, style);
     992             : 
     993           7 :         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           7 :         str = (unsigned char *) safe_emalloc(Z_STRLEN_P(data) * 2, sizeof(char), 1);
    1000             : 
    1001          47 :         for (i = j = 0; i < Z_STRLEN_P(data); i++) {
    1002          40 :                 str[j++] = hexconvtab[((unsigned char)Z_STRVAL_P(data)[i]) >> 4];
    1003          40 :                 str[j++] = hexconvtab[((unsigned char)Z_STRVAL_P(data)[i]) & 15];
    1004             :         }
    1005           7 :         str[j] = '\0';
    1006             : 
    1007           7 :         text = xmlNewTextLen(str, Z_STRLEN_P(data) * 2 * sizeof(char));
    1008           7 :         xmlAddChild(ret, text);
    1009           7 :         efree(str);
    1010           7 :         if (data == &tmp) {
    1011             :                 zval_dtor(&tmp);
    1012             :         }
    1013             : 
    1014           7 :         if (style == SOAP_ENCODED) {
    1015           5 :                 set_ns_and_type(ret, type);
    1016             :         }
    1017           7 :         return ret;
    1018             : }
    1019             : 
    1020         671 : static zval *to_zval_double(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1021             : {
    1022             :         zval *ret;
    1023         671 :         MAKE_STD_ZVAL(ret);
    1024         671 :         FIND_XML_NULL(data, ret);
    1025             : 
    1026        1342 :         if (data && data->children) {
    1027        1342 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
    1028             :                         long lval;
    1029             :                         double dval;
    1030             : 
    1031         671 :                         whiteSpace_collapse(data->children->content);
    1032         671 :                         switch (is_numeric_string((char*)data->children->content, strlen((char*)data->children->content), &lval, &dval, 0)) {
    1033             :                                 case IS_LONG:
    1034          19 :                                         Z_TYPE_P(ret) = IS_DOUBLE;
    1035          19 :                                         Z_DVAL_P(ret) = lval;
    1036          19 :                                         break;
    1037             :                                 case IS_DOUBLE:
    1038         646 :                                         Z_TYPE_P(ret) = IS_DOUBLE;
    1039         646 :                                         Z_DVAL_P(ret) = dval;
    1040         646 :                                         break;
    1041             :                                 default:
    1042           6 :                                         if (strncasecmp((char*)data->children->content, "NaN", sizeof("NaN")-1) == 0) {
    1043           2 :                                                 ZVAL_DOUBLE(ret, php_get_nan());
    1044           4 :                                         } else if (strncasecmp((char*)data->children->content, "INF", sizeof("INF")-1) == 0) {
    1045           2 :                                                 ZVAL_DOUBLE(ret, php_get_inf());
    1046           2 :                                         } else if (strncasecmp((char*)data->children->content, "-INF", sizeof("-INF")-1) == 0) {
    1047           2 :                                                 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         671 :         return ret;
    1059             : }
    1060             : 
    1061         479 : static zval *to_zval_long(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1062             : {
    1063             :         zval *ret;
    1064         479 :         MAKE_STD_ZVAL(ret);
    1065         479 :         FIND_XML_NULL(data, ret);
    1066             : 
    1067         954 :         if (data && data->children) {
    1068         954 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
    1069             :                         long lval;
    1070             :                         double dval;
    1071             : 
    1072         477 :                         whiteSpace_collapse(data->children->content);
    1073         477 :                         errno = 0;
    1074             : 
    1075         477 :                         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         476 :                                         Z_LVAL_P(ret) = lval;
    1078         476 :                                         break;
    1079             :                                 case IS_DOUBLE:
    1080           0 :                                         Z_DVAL_P(ret) = dval;
    1081           0 :                                         break;
    1082             :                                 default:
    1083           1 :                                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    1084             :                         }
    1085             :                 } else {
    1086           1 :                         soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    1087             :                 }
    1088             :         } else {
    1089           0 :                 ZVAL_NULL(ret);
    1090             :         }
    1091         476 :         return ret;
    1092             : }
    1093             : 
    1094         589 : static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1095             : {
    1096             :         xmlNodePtr ret;
    1097             : 
    1098         589 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1099         589 :         xmlAddChild(parent, ret);
    1100         589 :         FIND_ZVAL_NULL(data, ret, style);
    1101             : 
    1102         588 :         if (Z_TYPE_P(data) == IS_DOUBLE) {
    1103             :                 char s[256];
    1104             : 
    1105          91 :                 snprintf(s, sizeof(s), "%0.0F",floor(Z_DVAL_P(data)));
    1106          91 :                 xmlNodeSetContent(ret, BAD_CAST(s));
    1107             :         } else {
    1108         497 :                 zval tmp = *data;
    1109             : 
    1110             :                 zval_copy_ctor(&tmp);
    1111         497 :                 if (Z_TYPE(tmp) != IS_LONG) {
    1112           5 :                         convert_to_long(&tmp);
    1113             :                 }
    1114         497 :                 convert_to_string(&tmp);
    1115         497 :                 xmlNodeSetContentLen(ret, BAD_CAST(Z_STRVAL(tmp)), Z_STRLEN(tmp));
    1116             :                 zval_dtor(&tmp);
    1117             :         }
    1118             : 
    1119         588 :         if (style == SOAP_ENCODED) {
    1120         344 :                 set_ns_and_type(ret, type);
    1121             :         }
    1122         588 :         return ret;
    1123             : }
    1124             : 
    1125         268 : 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         268 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1132         268 :         xmlAddChild(parent, ret);
    1133         268 :         FIND_ZVAL_NULL(data, ret, style);
    1134             : 
    1135         268 :         tmp = *data;
    1136         268 :         if (Z_TYPE(tmp) != IS_DOUBLE) {
    1137             :                 zval_copy_ctor(&tmp);
    1138          21 :                 convert_to_double(&tmp);
    1139             :         }
    1140             :         
    1141         268 :         str = (char *) safe_emalloc(EG(precision), 1, MAX_LENGTH_OF_DOUBLE + 1);
    1142         268 :         php_gcvt(Z_DVAL(tmp), EG(precision), '.', 'E', str);
    1143         268 :         xmlNodeSetContentLen(ret, BAD_CAST(str), strlen(str));
    1144         268 :         efree(str);
    1145             : 
    1146         268 :         if (style == SOAP_ENCODED) {
    1147         158 :                 set_ns_and_type(ret, type);
    1148             :         }
    1149         268 :         return ret;
    1150             : }
    1151             : 
    1152          24 : static zval *to_zval_bool(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1153             : {
    1154             :         zval *ret;
    1155          24 :         MAKE_STD_ZVAL(ret);
    1156          24 :         FIND_XML_NULL(data, ret);
    1157             : 
    1158          48 :         if (data && data->children) {
    1159          48 :                 if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
    1160          24 :                         whiteSpace_collapse(data->children->content);
    1161          55 :                         if (stricmp((char*)data->children->content, "true") == 0 ||
    1162           6 :                                 stricmp((char*)data->children->content, "t") == 0 ||
    1163           6 :                                 strcmp((char*)data->children->content, "1") == 0) {
    1164          19 :                                 ZVAL_BOOL(ret, 1);
    1165          10 :                         } 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           5 :                                 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          24 :         return ret;
    1180             : }
    1181             : 
    1182          44 : static xmlNodePtr to_xml_bool(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1183             : {
    1184             :         xmlNodePtr ret;
    1185             : 
    1186          44 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1187          44 :         xmlAddChild(parent, ret);
    1188          44 :         FIND_ZVAL_NULL(data, ret, style);
    1189             : 
    1190          44 :         if (zend_is_true(data TSRMLS_CC)) {
    1191          32 :                 xmlNodeSetContent(ret, BAD_CAST("true"));
    1192             :         } else {
    1193          12 :                 xmlNodeSetContent(ret, BAD_CAST("false"));
    1194             :         }
    1195             : 
    1196          44 :         if (style == SOAP_ENCODED) {
    1197          31 :                 set_ns_and_type(ret, type);
    1198             :         }
    1199          44 :         return ret;
    1200             : }
    1201             : 
    1202             : /* Null encode/decode */
    1203           1 : static zval *to_zval_null(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1204             : {
    1205             :         zval *ret;
    1206           1 :         MAKE_STD_ZVAL(ret);
    1207           1 :         ZVAL_NULL(ret);
    1208           1 :         return ret;
    1209             : }
    1210             : 
    1211           7 : static xmlNodePtr to_xml_null(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1212             : {
    1213             :         xmlNodePtr ret;
    1214             : 
    1215           7 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1216           7 :         xmlAddChild(parent, ret);
    1217           7 :         if (style == SOAP_ENCODED) {
    1218           7 :                 set_xsi_nil(ret);
    1219             :         }
    1220           7 :         return ret;
    1221             : }
    1222             : 
    1223        1220 : static void set_zval_property(zval* object, char* name, zval* val TSRMLS_DC)
    1224             : {
    1225             :         zend_class_entry *old_scope;
    1226             : 
    1227        1220 :         old_scope = EG(scope);
    1228        1220 :         EG(scope) = Z_OBJCE_P(object);
    1229             :         Z_DELREF_P(val);
    1230        1220 :         add_property_zval(object, name, val);
    1231        1220 :         EG(scope) = old_scope;
    1232        1220 : }
    1233             : 
    1234        1205 : static zval* get_zval_property(zval* object, char* name TSRMLS_DC)
    1235             : {
    1236        1205 :         if (Z_TYPE_P(object) == IS_OBJECT) {
    1237             :                 zval member;
    1238             :                 zval *data;
    1239             :                 zend_class_entry *old_scope;
    1240             : 
    1241         986 :                 INIT_PZVAL(&member);
    1242         986 :                 ZVAL_STRING(&member, name, 0);
    1243         986 :                 old_scope = EG(scope);
    1244         986 :                 EG(scope) = Z_OBJCE_P(object);
    1245         986 :                 data = Z_OBJ_HT_P(object)->read_property(object, &member, BP_VAR_IS, 0 TSRMLS_CC);
    1246         986 :                 if (data == EG(uninitialized_zval_ptr)) {
    1247             :                         /* Hack for bug #32455 */
    1248             :                         zend_property_info *property_info;
    1249             : 
    1250         120 :                         property_info = zend_get_property_info(Z_OBJCE_P(object), &member, 1 TSRMLS_CC);
    1251         120 :                         EG(scope) = old_scope;
    1252         120 :                         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         120 :                         return NULL;
    1256             :                 }
    1257         866 :                 EG(scope) = old_scope;
    1258         866 :                 return data;
    1259         219 :         } else if (Z_TYPE_P(object) == IS_ARRAY) {
    1260             :                 zval **data_ptr;
    1261             : 
    1262         218 :                 if (zend_hash_find(Z_ARRVAL_P(object), name, strlen(name)+1, (void**)&data_ptr) == SUCCESS) {
    1263         210 :                   return *data_ptr;
    1264             :                 }
    1265             :         }
    1266           9 :   return NULL;
    1267             : }
    1268             : 
    1269           2 : static void unset_zval_property(zval* object, char* name TSRMLS_DC)
    1270             : {
    1271           2 :         if (Z_TYPE_P(object) == IS_OBJECT) {
    1272             :                 zval member;
    1273             :                 zend_class_entry *old_scope;
    1274             : 
    1275           2 :                 INIT_PZVAL(&member);
    1276           2 :                 ZVAL_STRING(&member, name, 0);
    1277           2 :                 old_scope = EG(scope);
    1278           2 :                 EG(scope) = Z_OBJCE_P(object);
    1279           2 :                 Z_OBJ_HT_P(object)->unset_property(object, &member, 0 TSRMLS_CC);
    1280           2 :                 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           2 : }
    1285             : 
    1286           6 : static void model_to_zval_any(zval *ret, xmlNodePtr node TSRMLS_DC)
    1287             : {
    1288           6 :         zval* any = NULL;
    1289           6 :         char* name = NULL;
    1290             : 
    1291          29 :         while (node != NULL) {
    1292          17 :                 if (get_zval_property(ret, (char*)node->name TSRMLS_CC) == NULL) {
    1293           5 :                         zval* val = master_to_zval(get_conversion(XSD_ANYXML), node TSRMLS_CC);
    1294             :                         
    1295           5 :                         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           7 :                         if (Z_TYPE_P(val) == IS_STRING && *Z_STRVAL_P(val) == '<') {
    1310           2 :                                 name = NULL;
    1311           5 :                                 while (node->next != NULL) {
    1312           1 :                                         zval* val2 = master_to_zval(get_conversion(XSD_ANYXML), node->next TSRMLS_CC);
    1313           1 :                                         if (Z_TYPE_P(val2) != IS_STRING ||  *Z_STRVAL_P(val) != '<') {
    1314             :                                                 break;
    1315             :                                         }
    1316           1 :                                         add_string_to_string(val, val, val2);
    1317           1 :                                         zval_ptr_dtor(&val2);
    1318           1 :                                         node = node->next;
    1319             :                                 }
    1320             :                         } else {
    1321           3 :                                 name = (char*)node->name;
    1322             :                         }
    1323             : 
    1324           5 :                         if (any == NULL) {
    1325           5 :                                 if (name) {
    1326             :                                         /* Convert into array */
    1327             :                                         zval *arr;
    1328             : 
    1329           3 :                                         MAKE_STD_ZVAL(arr);
    1330           3 :                                         array_init(arr);
    1331           3 :                                         add_assoc_zval(arr, name, val);
    1332           3 :                                         any = arr;
    1333           3 :                                         name = NULL;
    1334             :                                 } else {
    1335           2 :                                         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          17 :                 node = node->next;
    1362             :         }
    1363           6 :         if (any) {
    1364           5 :                 set_zval_property(ret, name ? name : "any", any TSRMLS_CC);
    1365             :         }
    1366           6 : }
    1367             : 
    1368        1543 : static void model_to_zval_object(zval *ret, sdlContentModelPtr model, xmlNodePtr data, sdlPtr sdl TSRMLS_DC)
    1369             : {
    1370        1543 :         switch (model->kind) {
    1371             :                 case XSD_CONTENT_ELEMENT:
    1372        1119 :                         if (model->u.element->name) {
    1373        1119 :                                 xmlNodePtr node = get_node(data->children, model->u.element->name);
    1374             : 
    1375        1119 :                                 if (node) {
    1376             :                                         zval *val;
    1377             :                                         xmlNodePtr r_node;
    1378             : 
    1379        1080 :                                         r_node = check_and_resolve_href(node);
    1380        1945 :                                         if (r_node && r_node->children && r_node->children->content) {
    1381         865 :                                                 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         865 :                                                 val = master_to_zval(model->u.element->encode, r_node TSRMLS_CC);
    1385         215 :                                         } 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         215 :                                         } 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         215 :                                                 val = master_to_zval(model->u.element->encode, r_node TSRMLS_CC);
    1397             :                                         }
    1398        1080 :                                         if ((node = get_node(node->next, model->u.element->name)) != NULL) {
    1399             :                                                 zval *array;
    1400             : 
    1401          21 :                                                 MAKE_STD_ZVAL(array);
    1402          21 :                                                 array_init(array);
    1403          21 :                                                 add_next_index_zval(array, val);
    1404             :                                                 do {
    1405          67 :                                                         if (node && node->children && node->children->content) {
    1406          30 :                                                                 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          30 :                                                                 val = master_to_zval(model->u.element->encode, node TSRMLS_CC);
    1410           7 :                                                         } 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           7 :                                                         } 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           7 :                                                                 val = master_to_zval(model->u.element->encode, node TSRMLS_CC);
    1422             :                                                         }
    1423          37 :                                                         add_next_index_zval(array, val);
    1424          37 :                                                 } while ((node = get_node(node->next, model->u.element->name)) != NULL);
    1425          21 :                                                 val = array;
    1426        2119 :                                         } else if ((Z_TYPE_P(val) != IS_NULL || !model->u.element->nillable) &&
    1427        1058 :                                                    (SOAP_GLOBAL(features) & SOAP_SINGLE_ELEMENT_ARRAYS) &&
    1428           2 :                                                    (model->max_occurs == -1 || model->max_occurs > 1)) {
    1429             :                                                 zval *array;
    1430             : 
    1431           2 :                                                 MAKE_STD_ZVAL(array);
    1432           2 :                                                 array_init(array);
    1433           2 :                                                 add_next_index_zval(array, val);
    1434           2 :                                                 val = array;
    1435             :                                         }
    1436        1080 :                                         set_zval_property(ret, model->u.element->name, val TSRMLS_CC);
    1437             :                                 }
    1438             :                         }
    1439        1119 :                         break;
    1440             :                 case XSD_CONTENT_ALL:
    1441             :                 case XSD_CONTENT_SEQUENCE:
    1442             :                 case XSD_CONTENT_CHOICE: {
    1443             :                         sdlContentModelPtr *tmp;
    1444             :                         HashPosition pos;
    1445         423 :                         sdlContentModelPtr any = NULL;
    1446             : 
    1447         423 :                         zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
    1448        1975 :                         while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
    1449        1129 :                                 if ((*tmp)->kind == XSD_CONTENT_ANY) {
    1450           5 :                                         any = *tmp;
    1451             :                                 } else {
    1452        1124 :                                         model_to_zval_object(ret, *tmp, data, sdl TSRMLS_CC);
    1453             :                                 }
    1454        1129 :                                 zend_hash_move_forward_ex(model->u.content, &pos);
    1455             :                         }
    1456         423 :                         if (any) {
    1457           5 :                                 model_to_zval_any(ret, data->children TSRMLS_CC);
    1458             :                         }
    1459         423 :                         break;
    1460             :                 }
    1461             :                 case XSD_CONTENT_GROUP:
    1462           1 :                         model_to_zval_object(ret, model->u.group->model, data, sdl TSRMLS_CC);
    1463             :                         break;
    1464             :                 default:
    1465             :                   break;
    1466             :         }
    1467        1543 : }
    1468             : 
    1469             : /* Struct encode/decode */
    1470         495 : 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         495 :         sdlTypePtr sdlType = type->sdl_type;
    1476         495 :         zend_class_entry *ce = ZEND_STANDARD_CLASS_DEF_PTR;
    1477         495 :         zval *redo_any = NULL;
    1478             : 
    1479         495 :         if (pce) {
    1480          13 :                 ce = pce;
    1481         482 :         } else if (SOAP_GLOBAL(class_map) && type->type_str) {
    1482             :                 zval             **classname;
    1483             :                 zend_class_entry  *tmp;
    1484             : 
    1485          67 :                 if (zend_hash_find(SOAP_GLOBAL(class_map), type->type_str, strlen(type->type_str)+1, (void**)&classname) == SUCCESS &&
    1486          21 :                     Z_TYPE_PP(classname) == IS_STRING &&
    1487          21 :                     (tmp = zend_fetch_class(Z_STRVAL_PP(classname), Z_STRLEN_PP(classname), ZEND_FETCH_CLASS_AUTO TSRMLS_CC)) != NULL) {
    1488          21 :                         ce = tmp;
    1489             :                 }
    1490             :         }
    1491         495 :         sdl = SOAP_GLOBAL(sdl);
    1492         495 :         if (sdlType) {
    1493         494 :                 if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
    1494          10 :                     sdlType->encode && type != &sdlType->encode->details) {
    1495             :                         encodePtr enc;
    1496             : 
    1497           5 :                         enc = sdlType->encode;
    1498          22 :                         while (enc && enc->details.sdl_type &&
    1499           3 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
    1500           3 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
    1501           3 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
    1502           3 :                                 enc = enc->details.sdl_type->encode;
    1503             :                         }
    1504           5 :                         if (enc) {
    1505             :                                 zval *base;
    1506             : 
    1507           4 :                                 ALLOC_INIT_ZVAL(ret);
    1508           4 :                                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1509           0 :                                         return ret;
    1510             :                                 }
    1511             : 
    1512           4 :                                 object_init_ex(ret, ce);
    1513           4 :                                 base = master_to_zval_int(enc, data TSRMLS_CC);
    1514           4 :                                 set_zval_property(ret, "_", base TSRMLS_CC);
    1515             :                         } else {
    1516           1 :                                 ALLOC_INIT_ZVAL(ret);
    1517           1 :                                 FIND_XML_NULL(data, ret);
    1518           1 :                                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1519           0 :                                         return ret;
    1520             :                                 }
    1521           1 :                                 object_init_ex(ret, ce);
    1522             :                         }
    1523         628 :                 } else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
    1524          52 :                            sdlType->encode &&
    1525          52 :                            type != &sdlType->encode->details) {
    1526         238 :                         if (sdlType->encode->details.sdl_type &&
    1527          47 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
    1528          47 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
    1529          47 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
    1530             : 
    1531          47 :                                 CHECK_XML_NULL(data);
    1532          46 :                                 if ((ret = soap_find_xml_ref(data TSRMLS_CC)) != NULL) {
    1533           1 :                                         return ret;
    1534             :                                 }
    1535             : 
    1536         110 :                             if (ce != ZEND_STANDARD_CLASS_DEF_PTR &&
    1537          13 :                                 sdlType->encode->to_zval == sdl_guess_convert_zval &&
    1538          13 :                                 sdlType->encode->details.sdl_type != NULL &&
    1539          13 :                                 (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          13 :                                 (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          13 :                                         ret = to_zval_object_ex(&sdlType->encode->details, data, ce TSRMLS_CC);
    1546             :                             } else {
    1547          32 :                                         ret = master_to_zval_int(sdlType->encode, data TSRMLS_CC);
    1548             :                                 }
    1549          45 :                                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1550           0 :                                         return ret;
    1551             :                                 }
    1552          45 :                                 redo_any = get_zval_property(ret, "any" TSRMLS_CC);
    1553          45 :                                 if (Z_TYPE_P(ret) == IS_OBJECT && ce != ZEND_STANDARD_CLASS_DEF_PTR) {
    1554          13 :                                         zend_object *zobj = zend_objects_get_address(ret TSRMLS_CC);
    1555          13 :                                         zobj->ce = ce;
    1556             :                                 }
    1557             :                         } else {
    1558             :                                 zval *base;
    1559             : 
    1560           5 :                                 ALLOC_INIT_ZVAL(ret);
    1561           5 :                                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1562           0 :                                         return ret;
    1563             :                                 }
    1564             : 
    1565           5 :                                 object_init_ex(ret, ce);
    1566           5 :                                 base = master_to_zval_int(sdlType->encode, data TSRMLS_CC);
    1567           5 :                                 set_zval_property(ret, "_", base TSRMLS_CC);
    1568             :                         }
    1569             :                 } else {
    1570         422 :                         ALLOC_INIT_ZVAL(ret);
    1571         422 :                         FIND_XML_NULL(data, ret);
    1572         417 :                         if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1573           3 :                                 return ret;
    1574             :                         }
    1575         414 :                         object_init_ex(ret, ce);
    1576             :                 }
    1577         469 :                 if (sdlType->model) {
    1578         418 :                         if (redo_any) {
    1579           2 :                                 Z_ADDREF_P(redo_any);
    1580           2 :                                 unset_zval_property(ret, "any" TSRMLS_CC);
    1581             :                         }
    1582         418 :                         model_to_zval_object(ret, sdlType->model, data, sdl TSRMLS_CC);
    1583         418 :                         if (redo_any) {
    1584           2 :                                 zval *tmp = get_zval_property(ret, "any" TSRMLS_CC);
    1585             : 
    1586           2 :                                 if (tmp == NULL) {
    1587           1 :                                         model_to_zval_any(ret, data->children TSRMLS_CC);
    1588           1 :                                 } else if (Z_REFCOUNT_P(tmp) == 0) {
    1589             :                                         zval_dtor(tmp);
    1590           1 :                                         efree(tmp);
    1591             :                                 }
    1592           2 :                                 zval_ptr_dtor(&redo_any);
    1593             :                         }
    1594             :                 }
    1595         469 :                 if (sdlType->attributes) {
    1596             :                         sdlAttributePtr *attr;
    1597             :                         HashPosition pos;
    1598             : 
    1599          57 :                         zend_hash_internal_pointer_reset_ex(sdlType->attributes, &pos);
    1600         203 :                         while (zend_hash_get_current_data_ex(sdlType->attributes, (void**)&attr, &pos) == SUCCESS) {
    1601          90 :                                 if ((*attr)->name) {
    1602          90 :                                         xmlAttrPtr val = get_attribute(data->properties, (*attr)->name);
    1603          90 :                                         char *str_val = NULL;
    1604             : 
    1605         175 :                                         if (val && val->children && val->children->content) {
    1606          85 :                                                 str_val = (char*)val->children->content;
    1607          85 :                                                 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           5 :                                         } else if ((*attr)->fixed) {
    1611           1 :                                                 str_val = (*attr)->fixed;
    1612           4 :                                         } else if ((*attr)->def) {
    1613           3 :                                                 str_val = (*attr)->def;
    1614             :                                         }
    1615          90 :                                         if (str_val) {
    1616             :                                                 xmlNodePtr dummy, text;
    1617             :                                                 zval *data;
    1618             : 
    1619          89 :                                                 dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1620          89 :                                                 text = xmlNewText(BAD_CAST(str_val));
    1621          89 :                                                 xmlAddChild(dummy, text);
    1622          89 :                                                 data = master_to_zval((*attr)->encode, dummy TSRMLS_CC);
    1623          88 :                                                 xmlFreeNode(dummy);
    1624          88 :                                                 set_zval_property(ret, (*attr)->name, data TSRMLS_CC);
    1625             :                                         }
    1626             :                                 }
    1627          89 :                                 zend_hash_move_forward_ex(sdlType->attributes, &pos);
    1628             :                         }
    1629             :                 }
    1630             :         } else {
    1631             : 
    1632          16 :                 ALLOC_INIT_ZVAL(ret);
    1633          16 :                 FIND_XML_NULL(data, ret);
    1634          16 :                 if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
    1635           1 :                         return ret;
    1636             :                 }
    1637             : 
    1638          15 :                 object_init_ex(ret, ce);
    1639          15 :                 trav = data->children;
    1640             : 
    1641          68 :                 while (trav != NULL) {
    1642          38 :                         if (trav->type == XML_ELEMENT_NODE) {
    1643             :                                 zval  *tmpVal;
    1644             :                                 zval *prop;
    1645             : 
    1646          38 :                                 tmpVal = master_to_zval(NULL, trav TSRMLS_CC);
    1647             : 
    1648          38 :                                 prop = get_zval_property(ret, (char*)trav->name TSRMLS_CC);
    1649          38 :                                 if (!prop) {
    1650          76 :                                         if (!trav->next || !get_node(trav->next, (char*)trav->name)) {
    1651          38 :                                                 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          38 :                         trav = trav->next;
    1678             :                 }
    1679             :         }
    1680         483 :         return ret;
    1681             : }
    1682             : 
    1683         482 : static zval *to_zval_object(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    1684             : {
    1685         482 :         return to_zval_object_ex(type, data, NULL TSRMLS_CC);
    1686             : }
    1687             : 
    1688             : 
    1689        1451 : static int model_to_xml_object(xmlNodePtr node, sdlContentModelPtr model, zval *object, int style, int strict TSRMLS_DC)
    1690             : {
    1691        1451 :         switch (model->kind) {
    1692             :                 case XSD_CONTENT_ELEMENT: {
    1693             :                         zval *data;
    1694             :                         xmlNodePtr property;
    1695             :                         encodePtr enc;
    1696             : 
    1697         995 :                         data = get_zval_property(object, model->u.element->name TSRMLS_CC);
    1698        1969 :                         if (data &&
    1699         959 :                             Z_TYPE_P(data) == IS_NULL &&
    1700           8 :                             !model->u.element->nillable &&
    1701           7 :                             model->min_occurs > 0 &&
    1702             :                             !strict) {
    1703           0 :                                 return 0;
    1704             :                         }
    1705         995 :                         if (data) {
    1706         959 :                                 enc = model->u.element->encode;
    1707        1017 :                                 if ((model->max_occurs == -1 || model->max_occurs > 1) &&
    1708          22 :                                     Z_TYPE_P(data) == IS_ARRAY &&
    1709          18 :                                     !is_map(data)) {
    1710          18 :                                         HashTable *ht = Z_ARRVAL_P(data);
    1711             :                                         zval **val;
    1712             : 
    1713          18 :                                         zend_hash_internal_pointer_reset(ht);
    1714          78 :                                         while (zend_hash_get_current_data(ht,(void**)&val) == SUCCESS) {
    1715          42 :                                                 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          42 :                                                         property = master_to_xml(enc, *val, style, node TSRMLS_CC);
    1721          72 :                                                         if (property->children && property->children->content &&
    1722          30 :                                                             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          42 :                                                 xmlNodeSetName(property, BAD_CAST(model->u.element->name));
    1727         120 :                                                 if (style == SOAP_LITERAL &&
    1728          39 :                                                     model->u.element->namens &&
    1729          39 :                                                     model->u.element->form == XSD_FORM_QUALIFIED) {
    1730          39 :                                                         xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
    1731          39 :                                                         xmlSetNs(property, nsp);
    1732             :                                                 }
    1733          42 :                                                 zend_hash_move_forward(ht);
    1734             :                                         }
    1735             :                                 } else {
    1736         942 :                                         if (Z_TYPE_P(data) == IS_NULL && model->u.element->nillable) {
    1737           1 :                                                 property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1738           1 :                                                 xmlAddChild(node, property);
    1739           1 :                                                 set_xsi_nil(property);
    1740         940 :                                         } else if (Z_TYPE_P(data) == IS_NULL && model->min_occurs == 0) {
    1741           1 :                                                 return 1;
    1742             :                                         } else {
    1743         939 :                                                 property = master_to_xml(enc, data, style, node TSRMLS_CC);
    1744        1730 :                                                 if (property->children && property->children->content &&
    1745         791 :                                                     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         940 :                                         xmlNodeSetName(property, BAD_CAST(model->u.element->name));
    1750        1930 :                                         if (style == SOAP_LITERAL &&
    1751         495 :                                             model->u.element->namens &&
    1752         495 :                                             model->u.element->form == XSD_FORM_QUALIFIED) {
    1753         483 :                                                 xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
    1754         483 :                                                 xmlSetNs(property, nsp);
    1755             :                                         }
    1756             :                                 }
    1757         958 :                                 return 1;
    1758          36 :                         } 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          36 :                         } else if (model->min_occurs == 0) {
    1770          34 :                                 return 2;
    1771             :                         } else {
    1772           2 :                                 if (strict) {
    1773           1 :                                         soap_error1(E_ERROR,  "Encoding: object has no '%s' property", model->u.element->name);
    1774             :                                 }
    1775           1 :                                 return 0;
    1776             :                         }
    1777             :                         break;
    1778             :                 }
    1779             :                 case XSD_CONTENT_ANY: {
    1780             :                         zval *data;
    1781             :                         xmlNodePtr property;
    1782             :                         encodePtr enc;
    1783             : 
    1784           5 :                         data = get_zval_property(object, "any" TSRMLS_CC);
    1785           5 :                         if (data) {
    1786           5 :                                 enc = get_conversion(XSD_ANYXML);
    1787           5 :                                 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           5 :                                         property = master_to_xml(enc, data, style, node TSRMLS_CC);
    1800             :                                 }
    1801           5 :                                 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         446 :                         zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
    1818        1890 :                         while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
    1819        1000 :                                 if (!model_to_xml_object(node, *tmp, object, style, strict && ((*tmp)->min_occurs > 0) TSRMLS_CC)) {
    1820           1 :                                         if (!strict || (*tmp)->min_occurs > 0) {
    1821           1 :                                                 return 0;
    1822             :                                         }
    1823             :                                 }
    1824         998 :                                 strict = 1;
    1825         998 :                                 zend_hash_move_forward_ex(model->u.content, &pos);
    1826             :                         }
    1827         444 :                         return 1;
    1828             :                 }
    1829             :                 case XSD_CONTENT_CHOICE: {
    1830             :                         sdlContentModelPtr *tmp;
    1831             :                         HashPosition pos;
    1832           4 :                         int ret = 0;
    1833             : 
    1834           4 :                         zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
    1835          11 :                         while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
    1836           7 :                                 int tmp_ret = model_to_xml_object(node, *tmp, object, style, 0 TSRMLS_CC);
    1837           7 :                                 if (tmp_ret == 1) {
    1838           4 :                                         return 1;
    1839           3 :                                 } else if (tmp_ret != 0) {
    1840           2 :                                         ret = 1;
    1841             :                                 }
    1842           3 :                                 zend_hash_move_forward_ex(model->u.content, &pos);
    1843             :                         }
    1844           0 :                         return ret;
    1845             :                 }
    1846             :                 case XSD_CONTENT_GROUP: {
    1847           1 :                         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          44 : static sdlTypePtr model_array_element(sdlContentModelPtr model)
    1856             : {
    1857          44 :         switch (model->kind) {
    1858             :                 case XSD_CONTENT_ELEMENT: {
    1859          22 :                         if (model->max_occurs == -1 || model->max_occurs > 1) {
    1860          20 :                           return model->u.element;
    1861             :                         } else {
    1862           2 :                           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          22 :                         if (zend_hash_num_elements(model->u.content) != 1) {
    1872           0 :                           return NULL;
    1873             :                         }
    1874          22 :                         zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
    1875          22 :                         zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos);
    1876          22 :                         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         583 : static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    1888             : {
    1889             :         xmlNodePtr xmlParam;
    1890         583 :         HashTable *prop = NULL;
    1891             :         int i;
    1892         583 :         sdlTypePtr sdlType = type->sdl_type;
    1893             : 
    1894         583 :         if (!data || Z_TYPE_P(data) == IS_NULL) {
    1895          32 :                 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1896          32 :                 xmlAddChild(parent, xmlParam);
    1897          32 :                 if (style == SOAP_ENCODED) {
    1898          11 :                         set_xsi_nil(xmlParam);
    1899          11 :                         set_ns_and_type(xmlParam, type);
    1900             :                 }
    1901          32 :                 return xmlParam;
    1902             :         }
    1903             : 
    1904         551 :         if (Z_TYPE_P(data) == IS_OBJECT) {
    1905         397 :                 prop = Z_OBJPROP_P(data);
    1906         154 :         } else if (Z_TYPE_P(data) == IS_ARRAY) {
    1907         153 :                 prop = Z_ARRVAL_P(data);
    1908             :         }
    1909             : 
    1910         551 :         if (sdlType) {
    1911         525 :                 if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
    1912          10 :                     sdlType->encode && type != &sdlType->encode->details) {
    1913             :                         encodePtr enc;
    1914             : 
    1915           5 :                         enc = sdlType->encode;
    1916          22 :                         while (enc && enc->details.sdl_type &&
    1917           3 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
    1918           3 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
    1919           3 :                                enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
    1920           3 :                                 enc = enc->details.sdl_type->encode;
    1921             :                         }
    1922           5 :                         if (enc) {
    1923           4 :                                 zval *tmp = get_zval_property(data, "_" TSRMLS_CC);
    1924           4 :                                 if (tmp) {
    1925           4 :                                         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           1 :                                 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1934           1 :                                 xmlAddChild(parent, xmlParam);
    1935             :                         }
    1936         685 :                 } else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
    1937         120 :                            sdlType->encode && type != &sdlType->encode->details) {
    1938         272 :                         if (sdlType->encode->details.sdl_type &&
    1939          53 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
    1940          53 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
    1941          53 :                             sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
    1942             : 
    1943          53 :                                 if (prop) prop->nApplyCount++;
    1944          53 :                                 xmlParam = master_to_xml(sdlType->encode, data, style, parent TSRMLS_CC);
    1945          53 :                                 if (prop) prop->nApplyCount--;
    1946             :                         } else {
    1947           7 :                                 zval *tmp = get_zval_property(data, "_" TSRMLS_CC);
    1948             : 
    1949           7 :                                 if (tmp) {
    1950           6 :                                         xmlParam = master_to_xml(sdlType->encode, tmp, style, parent TSRMLS_CC);
    1951           1 :                                 } else if (prop == NULL) {
    1952           1 :                                         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         445 :                         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1960         445 :                         xmlAddChild(parent, xmlParam);
    1961             :                 }
    1962             : 
    1963         510 :                 if (soap_check_zval_ref(data, xmlParam TSRMLS_CC)) {
    1964           4 :                         return xmlParam;
    1965             :                 }
    1966         506 :                 if (prop != NULL) {
    1967             :                         sdlTypePtr array_el;
    1968             : 
    1969         737 :                         if (Z_TYPE_P(data) == IS_ARRAY &&
    1970         146 :                       !is_map(data) &&
    1971          22 :                       sdlType->attributes == NULL &&
    1972          22 :                       sdlType->model != NULL &&
    1973          22 :                       (array_el = model_array_element(sdlType->model)) != NULL) {
    1974             :                                 zval **val;
    1975             : 
    1976          20 :                                 zend_hash_internal_pointer_reset(prop);
    1977          93 :                                 while (zend_hash_get_current_data(prop,(void**)&val) == SUCCESS) {
    1978             :                                         xmlNodePtr property;
    1979          55 :                                         if (Z_TYPE_PP(val) == IS_NULL && array_el->nillable) {
    1980           2 :                                                 property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1981           2 :                                                 xmlAddChild(xmlParam, property);
    1982           2 :                                                 set_xsi_nil(property);
    1983             :                                         } else {
    1984          51 :                                                 property = master_to_xml(array_el->encode, *val, style, xmlParam TSRMLS_CC);
    1985             :                                         }
    1986          53 :                                         xmlNodeSetName(property, BAD_CAST(array_el->name));
    1987         157 :                                         if (style == SOAP_LITERAL &&
    1988          52 :                                            array_el->namens &&
    1989          52 :                                            array_el->form == XSD_FORM_QUALIFIED) {
    1990          52 :                                                 xmlNsPtr nsp = encode_add_ns(property, array_el->namens);
    1991          52 :                                                 xmlSetNs(property, nsp);
    1992             :                                         }
    1993          53 :                                         zend_hash_move_forward(prop);
    1994             :                                 }
    1995         485 :                         } else if (sdlType->model) {
    1996         443 :                                 model_to_xml_object(xmlParam, sdlType->model, data, style, 1 TSRMLS_CC);
    1997             :                         }
    1998         504 :                         if (sdlType->attributes) {
    1999             :                                 sdlAttributePtr *attr;
    2000             :                                 zval *zattr;
    2001             :                                 HashPosition pos;
    2002             : 
    2003          58 :                                 zend_hash_internal_pointer_reset_ex(sdlType->attributes, &pos);
    2004         207 :                                 while (zend_hash_get_current_data_ex(sdlType->attributes, (void**)&attr, &pos) == SUCCESS) {
    2005          92 :                                         if ((*attr)->name) {
    2006          92 :                                                 zattr = get_zval_property(data, (*attr)->name TSRMLS_CC);
    2007          92 :                                                 if (zattr) {
    2008             :                                                         xmlNodePtr dummy;
    2009             : 
    2010          87 :                                                         dummy = master_to_xml((*attr)->encode, zattr, SOAP_LITERAL, xmlParam TSRMLS_CC);
    2011          87 :                                                         if (dummy->children && dummy->children->content) {
    2012          87 :                                                                 if ((*attr)->fixed && strcmp((*attr)->fixed, (char*)dummy->children->content) != 0) {
    2013           1 :                                                                         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         263 :                                                                 if ((*attr)->namens &&
    2019          86 :                                                                     (!strncmp((*attr)->namens, XML_NAMESPACE, sizeof(XML_NAMESPACE)) ||
    2020          82 :                                                                      (*attr)->form == XSD_FORM_QUALIFIED)) {
    2021           9 :                                                                         xmlNsPtr nsp = encode_add_ns(xmlParam, (*attr)->namens);
    2022             : 
    2023           9 :                                                                         xmlSetNsProp(xmlParam, nsp, BAD_CAST((*attr)->name), dummy->children->content);
    2024             :                                                                 } else {
    2025          77 :                                                                         xmlSetProp(xmlParam, BAD_CAST((*attr)->name), dummy->children->content);
    2026             :                                                                 }
    2027             :                                                         }
    2028          86 :                                                         xmlUnlinkNode(dummy);
    2029          86 :                                                         xmlFreeNode(dummy);
    2030             :                                                 }
    2031             :                                         }
    2032          91 :                                         zend_hash_move_forward_ex(sdlType->attributes, &pos);
    2033             :                                 }
    2034             :                         }
    2035             :                 }
    2036         504 :                 if (style == SOAP_ENCODED) {
    2037         213 :                         set_ns_and_type(xmlParam, type);
    2038             :                 }
    2039             :         } else {
    2040          41 :                 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2041          41 :                 xmlAddChild(parent, xmlParam);
    2042             : 
    2043          41 :                 if (soap_check_zval_ref(data, xmlParam TSRMLS_CC)) {
    2044           8 :                         return xmlParam;
    2045             :                 }
    2046          33 :                 if (prop != NULL) {
    2047          33 :                         i = zend_hash_num_elements(prop);
    2048          33 :                         zend_hash_internal_pointer_reset(prop);
    2049             : 
    2050         115 :                         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          82 :                                 key_type = zend_hash_get_current_key_ex(prop, &str_key, &str_key_len, &index, FALSE, NULL);
    2059          82 :                                 zend_hash_get_current_data(prop, (void **)&zprop);
    2060             : 
    2061          82 :                                 property = master_to_xml(get_conversion((*zprop)->type), (*zprop), style, xmlParam TSRMLS_CC);
    2062             : 
    2063          82 :                                 if (key_type == HASH_KEY_IS_STRING) {
    2064             :                                         const char *prop_name;
    2065             : 
    2066          59 :                                         if (Z_TYPE_P(data) == IS_OBJECT) {
    2067             :                                                 const char *class_name;
    2068             : 
    2069          59 :                                                 zend_unmangle_property_name(str_key, str_key_len-1, &class_name, &prop_name);
    2070             :                                         } else {
    2071           0 :                                                 prop_name = str_key;
    2072             :                                         }
    2073          59 :                                         if (prop_name) {
    2074          59 :                                                 xmlNodeSetName(property, BAD_CAST(prop_name));
    2075             :                                         }
    2076             :                                 }
    2077          82 :                                 zend_hash_move_forward(prop);
    2078             :                         }
    2079             :                 }
    2080          33 :                 if (style == SOAP_ENCODED) {
    2081          31 :                         set_ns_and_type(xmlParam, type);
    2082             :                 }
    2083             :         }
    2084         537 :         return xmlParam;
    2085             : }
    2086             : 
    2087             : /* Array encode/decode */
    2088          18 : static xmlNodePtr guess_array_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    2089             : {
    2090          18 :         encodePtr enc = NULL;
    2091             : 
    2092          18 :         if (data && Z_TYPE_P(data) == IS_ARRAY) {
    2093          18 :                 if (is_map(data)) {
    2094           4 :                         enc = get_conversion(APACHE_MAP);
    2095             :                 } else {
    2096          14 :                         enc = get_conversion(SOAP_ENC_ARRAY);
    2097             :                 }
    2098             :         }
    2099          18 :         if (!enc) {
    2100           0 :                 enc = get_conversion(IS_NULL);
    2101             :         }
    2102             : 
    2103          18 :         return master_to_xml(enc, data, style, parent TSRMLS_CC);
    2104             : }
    2105             : 
    2106          20 : static int calc_dimension_12(const char* str)
    2107             : {
    2108          20 :         int i = 0, flag = 0;
    2109          40 :         while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
    2110           0 :                 str++;
    2111             :         }
    2112          20 :         if (*str == '*') {
    2113          11 :                 i++;
    2114          11 :                 str++;
    2115             :         }
    2116          62 :         while (*str != '\0') {
    2117          38 :                 if (*str >= '0' && *str <= '9') {
    2118          15 :                         if (flag == 0) {
    2119          15 :                         i++;
    2120          15 :                         flag = 1;
    2121             :                 }
    2122           8 :           } else if (*str == '*') {
    2123           1 :                         soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
    2124             :                 } else {
    2125           7 :                         flag = 0;
    2126             :                 }
    2127          22 :                 str++;
    2128             :         }
    2129          19 :         return i;
    2130             : }
    2131             : 
    2132          19 : static int* get_position_12(int dimension, const char* str)
    2133             : {
    2134             :         int *pos;
    2135          19 :         int i = -1, flag = 0;
    2136             : 
    2137          19 :         pos = safe_emalloc(sizeof(int), dimension, 0);
    2138          19 :         memset(pos,0,sizeof(int)*dimension);
    2139          38 :         while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
    2140           0 :                 str++;
    2141             :         }
    2142          19 :         if (*str == '*') {
    2143          11 :                 str++;
    2144          11 :                 i++;
    2145             :         }
    2146          58 :         while (*str != '\0') {
    2147          34 :                 if (*str >= '0' && *str <= '9') {
    2148          14 :                         if (flag == 0) {
    2149          14 :                                 i++;
    2150          14 :                                 flag = 1;
    2151             :                         }
    2152          14 :                         pos[i] = (pos[i]*10)+(*str-'0');
    2153           6 :                 } else if (*str == '*') {
    2154           0 :                         soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
    2155             :                 } else {
    2156           6 :                   flag = 0;
    2157             :                 }
    2158          20 :                 str++;
    2159             :         }
    2160          19 :         return pos;
    2161             : }
    2162             : 
    2163         729 : static int calc_dimension(const char* str)
    2164             : {
    2165         729 :         int i = 1;
    2166        2147 :         while (*str != ']' && *str != '\0') {
    2167         689 :                 if (*str == ',') {
    2168           9 :                 i++;
    2169             :                 }
    2170         689 :                 str++;
    2171             :         }
    2172         729 :         return i;
    2173             : }
    2174             : 
    2175         666 : static void get_position_ex(int dimension, const char* str, int** pos)
    2176             : {
    2177         666 :         int i = 0;
    2178             : 
    2179         666 :         memset(*pos,0,sizeof(int)*dimension);
    2180        2016 :         while (*str != ']' && *str != '\0' && i < dimension) {
    2181        1364 :                 if (*str >= '0' && *str <= '9') {
    2182         680 :                         (*pos)[i] = ((*pos)[i]*10)+(*str-'0');
    2183           4 :                 } else if (*str == ',') {
    2184           4 :                         i++;
    2185             :                 }
    2186         684 :                 str++;
    2187             :         }
    2188         666 : }
    2189             : 
    2190         666 : static int* get_position(int dimension, const char* str)
    2191             : {
    2192             :         int *pos;
    2193             : 
    2194         666 :         pos = safe_emalloc(sizeof(int), dimension, 0);
    2195         666 :         get_position_ex(dimension, str, &pos);
    2196         666 :         return pos;
    2197             : }
    2198             : 
    2199         129 : 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         258 :         if (data && Z_TYPE_P(data) == IS_ARRAY) {
    2212         129 :                 zend_hash_internal_pointer_reset(data->value.ht);
    2213         412 :                 for (j=0; j<dims[0]; j++) {
    2214             :                         zval **zdata;
    2215             : 
    2216         283 :                         if (zend_hash_get_current_data(data->value.ht, (void **)&zdata) != SUCCESS) {
    2217           0 :                                 zdata = NULL;
    2218             :                         }
    2219         283 :                         if (dimension == 1) {
    2220             :                                 xmlNodePtr xparam;
    2221             : 
    2222         265 :                                 if (zdata) {
    2223         265 :                                         if (enc == NULL) {
    2224           2 :                                                 xparam = master_to_xml(get_conversion((*zdata)->type), (*zdata), style, xmlParam TSRMLS_CC);
    2225             :                                         } else {
    2226         263 :                                                 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         265 :                                 if (type) {
    2234          16 :                                         xmlNodeSetName(xparam, BAD_CAST(type->name));
    2235         261 :                                 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
    2236          12 :                                         xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
    2237          12 :                                         xmlSetNs(xparam, ns);
    2238             :                                 } else {
    2239         237 :                                         xmlNodeSetName(xparam, BAD_CAST("item"));
    2240             :                                 }
    2241             :                         } else {
    2242          18 :                                 if (zdata) {
    2243          18 :                                   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         283 :                         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         129 : }
    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         116 : static xmlNodePtr to_xml_array(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    2281             : {
    2282         116 :         sdlTypePtr sdl_type = type->sdl_type;
    2283         116 :         sdlTypePtr element_type = NULL;
    2284         116 :         smart_str array_type = {0}, array_size = {0};
    2285             :         int i;
    2286             :         xmlNodePtr xmlParam;
    2287         116 :         encodePtr enc = NULL;
    2288         116 :         int dimension = 1;
    2289             :         int* dims;
    2290             :         int soap_version;
    2291         116 :         zval *array_copy = NULL;
    2292             : 
    2293         116 :         soap_version = SOAP_GLOBAL(soap_version);
    2294             : 
    2295         116 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2296         116 :         xmlAddChild(parent, xmlParam);
    2297             : 
    2298         116 :         if (!data || Z_TYPE_P(data) == IS_NULL) {
    2299           5 :                 if (style == SOAP_ENCODED) {
    2300           5 :                         set_xsi_nil(xmlParam);
    2301           5 :                         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           5 :                                 set_ns_and_type(xmlParam, type);
    2305             :                         }
    2306             :                 }
    2307           5 :                 return xmlParam;
    2308             :         }
    2309             : 
    2310         111 :         if (Z_TYPE_P(data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(data), zend_ce_traversable TSRMLS_CC)) {
    2311             :                 zend_object_iterator   *iter;
    2312           2 :                 zend_class_entry       *ce = Z_OBJCE_P(data);
    2313             :                 zval                  **val;
    2314             : 
    2315           2 :                 ALLOC_ZVAL(array_copy);
    2316           2 :                 INIT_PZVAL(array_copy);
    2317           2 :                 array_init(array_copy);
    2318             : 
    2319           2 :                 iter = ce->get_iterator(ce, data, 0 TSRMLS_CC);
    2320             : 
    2321           2 :                 if (EG(exception)) {
    2322           0 :                         goto iterator_done;
    2323             :                 }
    2324             : 
    2325           2 :                 if (iter->funcs->rewind) {
    2326           2 :                         iter->funcs->rewind(iter TSRMLS_CC);
    2327           2 :                         if (EG(exception)) {
    2328           0 :                                 goto iterator_done;
    2329             :                         }
    2330             :                 }
    2331             : 
    2332          24 :                 while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
    2333          20 :                         if (EG(exception)) {
    2334           0 :                                 goto iterator_done;
    2335             :                         }
    2336             : 
    2337          20 :                         iter->funcs->get_current_data(iter, &val TSRMLS_CC);
    2338          20 :                         if (EG(exception)) {
    2339           0 :                                 goto iterator_done;
    2340             :                         }
    2341          20 :                         if (iter->funcs->get_current_key) {
    2342             :                                 zval key;
    2343          20 :                                 iter->funcs->get_current_key(iter, &key TSRMLS_CC);
    2344          20 :                                 if (EG(exception)) {
    2345           0 :                                         goto iterator_done;
    2346             :                                 }
    2347          20 :                                 array_set_zval_key(Z_ARRVAL_P(array_copy), &key, *val);
    2348          20 :                                 zval_ptr_dtor(val);
    2349             :                                 zval_dtor(&key);
    2350             :                         } else {
    2351           0 :                                 add_next_index_zval(array_copy, *val);
    2352             :                         }
    2353          20 :                         Z_ADDREF_PP(val);
    2354             : 
    2355          20 :                         iter->funcs->move_forward(iter TSRMLS_CC);
    2356          20 :                         if (EG(exception)) {
    2357           0 :                                 goto iterator_done;
    2358             :                         }
    2359             :                 }
    2360             : iterator_done:
    2361           2 :                 iter->funcs->dtor(iter TSRMLS_CC);
    2362           2 :                 if (EG(exception)) {
    2363           0 :                         zval_ptr_dtor(&array_copy);
    2364           0 :                         array_copy = NULL;
    2365             :                 } else {
    2366           2 :                         data = array_copy;
    2367             :                 }
    2368             :         }
    2369             : 
    2370         111 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    2371             :                 sdlAttributePtr *arrayType;
    2372             :                 sdlExtraAttributePtr *ext;
    2373             :                 sdlTypePtr elementType;
    2374             : 
    2375         111 :                 i = zend_hash_num_elements(Z_ARRVAL_P(data));
    2376             : 
    2377         486 :                 if (sdl_type &&
    2378          86 :                     sdl_type->attributes &&
    2379          80 :                     zend_hash_find(sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
    2380             :                       sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType"),
    2381         160 :                       (void **)&arrayType) == SUCCESS &&
    2382          73 :                     (*arrayType)->extraAttributes &&
    2383          73 :                     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          63 :                         value = estrdup((*ext)->val);
    2389          63 :                         end = strrchr(value,'[');
    2390          63 :                         if (end) {
    2391          63 :                                 *end = '\0';
    2392          63 :                                 end++;
    2393          63 :                                 dimension = calc_dimension(end);
    2394             :                         }
    2395          63 :                         if ((*ext)->ns != NULL) {
    2396          63 :                                 enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, value);
    2397          63 :                                 get_type_str(xmlParam, (*ext)->ns, value, &array_type);
    2398             :                         } else {
    2399           0 :                                 smart_str_appends(&array_type, value);
    2400             :                         }
    2401             : 
    2402          63 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2403          63 :                         dims[0] = i;
    2404          63 :                         el = &data;
    2405          68 :                         for (i = 1; i < dimension; i++) {
    2406          10 :                                 if (el != NULL && Z_TYPE_PP(el) == IS_ARRAY &&
    2407           5 :                                     zend_hash_num_elements(Z_ARRVAL_PP(el)) > 0) {
    2408           5 :                                   zend_hash_internal_pointer_reset(Z_ARRVAL_PP(el));
    2409           5 :                                         zend_hash_get_current_data(Z_ARRVAL_PP(el), (void**)&el);
    2410           5 :                                         if (Z_TYPE_PP(el) == IS_ARRAY) {
    2411           5 :                                                 dims[i] = zend_hash_num_elements(Z_ARRVAL_PP(el));
    2412             :                                         } else {
    2413           0 :                                                 dims[i] = 0;
    2414             :                                         }
    2415             :                                 }
    2416             :                         }
    2417             : 
    2418          63 :                         smart_str_append_long(&array_size, dims[0]);
    2419          68 :                         for (i=1; i<dimension; i++) {
    2420           5 :                                 smart_str_appendc(&array_size, ',');
    2421           5 :                                 smart_str_append_long(&array_size, dims[i]);
    2422             :                         }
    2423             : 
    2424          63 :                         efree(value);
    2425             : 
    2426         103 :                 } else if (sdl_type &&
    2427          23 :                            sdl_type->attributes &&
    2428          17 :                            zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
    2429             :                              sizeof(SOAP_1_2_ENC_NAMESPACE":itemType"),
    2430          34 :                              (void **)&arrayType) == SUCCESS &&
    2431           5 :                            (*arrayType)->extraAttributes &&
    2432           5 :                            zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType"), (void **)&ext) == SUCCESS) {
    2433           5 :                         if ((*ext)->ns != NULL) {
    2434           5 :                                 enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, (*ext)->val);
    2435           5 :                                 get_type_str(xmlParam, (*ext)->ns, (*ext)->val, &array_type);
    2436             :                         } else {
    2437           0 :                                 smart_str_appends(&array_type, (*ext)->val);
    2438             :                         }
    2439          25 :                         if (zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2440             :                                            sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
    2441          10 :                                            (void **)&arrayType) == SUCCESS &&
    2442           5 :                             (*arrayType)->extraAttributes &&
    2443           5 :                             zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
    2444           5 :                                 dimension = calc_dimension_12((*ext)->val);
    2445           5 :                                 dims = get_position_12(dimension, (*ext)->val);
    2446           5 :                                 if (dims[0] == 0) {dims[0] = i;}
    2447             : 
    2448           5 :                                 smart_str_append_long(&array_size, dims[0]);
    2449           7 :                                 for (i=1; i<dimension; i++) {
    2450           2 :                                         smart_str_appendc(&array_size, ',');
    2451           2 :                                         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          79 :                 } else if (sdl_type &&
    2459          18 :                            sdl_type->attributes &&
    2460          12 :                            zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2461             :                              sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
    2462          24 :                              (void **)&arrayType) == SUCCESS &&
    2463           2 :                            (*arrayType)->extraAttributes &&
    2464           2 :                            zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraySize"), (void **)&ext) == SUCCESS) {
    2465           2 :                         dimension = calc_dimension_12((*ext)->val);
    2466           2 :                         dims = get_position_12(dimension, (*ext)->val);
    2467           2 :                         if (dims[0] == 0) {dims[0] = i;}
    2468             : 
    2469           2 :                         smart_str_append_long(&array_size, dims[0]);
    2470           4 :                         for (i=1; i<dimension; i++) {
    2471           2 :                                 smart_str_appendc(&array_size, ',');
    2472           2 :                                 smart_str_append_long(&array_size, dims[i]);
    2473             :                         }
    2474             : 
    2475          16 :                         if (sdl_type && sdl_type->elements &&
    2476           2 :                             zend_hash_num_elements(sdl_type->elements) == 1 &&
    2477           2 :                             (zend_hash_internal_pointer_reset(sdl_type->elements),
    2478             :                              zend_hash_get_current_data(sdl_type->elements, (void**)&elementType) == SUCCESS) &&
    2479           4 :                                         (elementType = *(sdlTypePtr*)elementType) != NULL &&
    2480           4 :                              elementType->encode && elementType->encode->details.type_str) {
    2481           2 :                                 element_type = elementType;
    2482           2 :                                 enc = elementType->encode;
    2483           2 :                                 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          83 :                 } else if (sdl_type && sdl_type->elements &&
    2488           6 :                            zend_hash_num_elements(sdl_type->elements) == 1 &&
    2489           6 :                            (zend_hash_internal_pointer_reset(sdl_type->elements),
    2490             :                             zend_hash_get_current_data(sdl_type->elements, (void**)&elementType) == SUCCESS) &&
    2491          12 :                            (elementType = *(sdlTypePtr*)elementType) != NULL &&
    2492          12 :                            elementType->encode && elementType->encode->details.type_str) {
    2493             : 
    2494           6 :                         element_type = elementType;
    2495           6 :                         enc = elementType->encode;
    2496           6 :                         get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
    2497             : 
    2498           6 :                         smart_str_append_long(&array_size, i);
    2499             : 
    2500           6 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2501           6 :                         dims[0] = i;
    2502             :                 } else {
    2503             : 
    2504          35 :                         enc = get_array_type(xmlParam, data, &array_type TSRMLS_CC);
    2505          35 :                         smart_str_append_long(&array_size, i);
    2506          35 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2507          35 :                         dims[0] = i;
    2508             :                 }
    2509             : 
    2510         111 :                 if (style == SOAP_ENCODED) {
    2511         102 :                         if (soap_version == SOAP_1_1) {
    2512          95 :                                 smart_str_0(&array_type);
    2513          95 :                                 if (strcmp(array_type.c,"xsd:anyType") == 0) {
    2514           2 :                                         smart_str_free(&array_type);
    2515           2 :                                         smart_str_appendl(&array_type,"xsd:ur-type",sizeof("xsd:ur-type")-1);
    2516             :                                 }
    2517          95 :                                 smart_str_appendc(&array_type, '[');
    2518          95 :                                 smart_str_append(&array_type, &array_size);
    2519          95 :                                 smart_str_appendc(&array_type, ']');
    2520          95 :                                 smart_str_0(&array_type);
    2521          95 :                                 set_ns_prop(xmlParam, SOAP_1_1_ENC_NAMESPACE, "arrayType", array_type.c);
    2522             :                         } else {
    2523           7 :                                 int i = 0;
    2524          21 :                                 while (i < array_size.len) {
    2525           7 :                                         if (array_size.c[i] == ',') {array_size.c[i] = ' ';}
    2526           7 :                                         ++i;
    2527             :                                 }
    2528           7 :                                 smart_str_0(&array_type);
    2529           7 :                                 smart_str_0(&array_size);
    2530           7 :                                 set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "itemType", array_type.c);
    2531           7 :                                 set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "arraySize", array_size.c);
    2532             :                         }
    2533             :                 }
    2534         111 :                 smart_str_free(&array_type);
    2535         111 :                 smart_str_free(&array_size);
    2536             : 
    2537         111 :                 add_xml_array_elements(xmlParam, element_type, enc, enc?encode_add_ns(xmlParam,enc->details.ns):NULL, dimension, dims, data, style TSRMLS_CC);
    2538         111 :                 efree(dims);
    2539             :         }
    2540         111 :         if (style == SOAP_ENCODED) {
    2541         102 :                 if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
    2542           4 :                         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          98 :                         set_ns_and_type(xmlParam, type);
    2545             :                 }
    2546             :         }
    2547             : 
    2548         111 :         if (array_copy) {
    2549           2 :                 zval_ptr_dtor(&array_copy);
    2550             :         }
    2551             : 
    2552         111 :         return xmlParam;
    2553             : }
    2554             : 
    2555         686 : static zval *to_zval_array(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    2556             : {
    2557             :         zval *ret;
    2558             :         xmlNodePtr trav;
    2559         686 :         encodePtr enc = NULL;
    2560         686 :         int dimension = 1;
    2561         686 :         int* dims = NULL;
    2562         686 :         int* pos = NULL;
    2563             :         xmlAttrPtr attr;
    2564             :         sdlPtr sdl;
    2565             :         sdlAttributePtr *arrayType;
    2566             :         sdlExtraAttributePtr *ext;
    2567             :         sdlTypePtr elementType;
    2568             : 
    2569         686 :         MAKE_STD_ZVAL(ret);
    2570         686 :         FIND_XML_NULL(data, ret);
    2571         683 :         sdl = SOAP_GLOBAL(sdl);
    2572             : 
    2573        3364 :         if (data &&
    2574         683 :             (attr = get_attribute(data->properties,"arrayType")) &&
    2575        1332 :             attr->children && attr->children->content) {
    2576             :                 char *type, *end, *ns;
    2577             :                 xmlNsPtr nsptr;
    2578             : 
    2579         666 :                 parse_namespace(attr->children->content, &type, &ns);
    2580         666 :                 nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
    2581             : 
    2582         666 :                 end = strrchr(type,'[');
    2583         666 :                 if (end) {
    2584         666 :                         *end = '\0';
    2585         666 :                         dimension = calc_dimension(end+1);
    2586         666 :                         dims = get_position(dimension, end+1);
    2587             :                 }
    2588         666 :                 if (nsptr != NULL) {
    2589         666 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
    2590             :                 }
    2591         666 :                 efree(type);
    2592         666 :                 if (ns) {efree(ns);}
    2593             : 
    2594          46 :         } else if ((attr = get_attribute(data->properties,"itemType")) &&
    2595          10 :             attr->children &&
    2596          10 :             attr->children->content) {
    2597             :                 char *type, *ns;
    2598             :                 xmlNsPtr nsptr;
    2599             : 
    2600          10 :                 parse_namespace(attr->children->content, &type, &ns);
    2601          10 :                 nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
    2602          10 :                 if (nsptr != NULL) {
    2603          10 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
    2604             :                 }
    2605          10 :                 efree(type);
    2606          10 :                 if (ns) {efree(ns);}
    2607             : 
    2608          36 :                 if ((attr = get_attribute(data->properties,"arraySize")) &&
    2609          18 :                     attr->children && attr->children->content) {
    2610           9 :                         dimension = calc_dimension_12((char*)attr->children->content);
    2611           8 :                         dims = get_position_12(dimension, (char*)attr->children->content);
    2612             :                 } else {
    2613           1 :                         dims = emalloc(sizeof(int));
    2614           1 :                         *dims = 0;
    2615             :                 }
    2616             : 
    2617          10 :         } else if ((attr = get_attribute(data->properties,"arraySize")) &&
    2618           2 :             attr->children && attr->children->content) {
    2619             : 
    2620           1 :                 dimension = calc_dimension_12((char*)attr->children->content);
    2621           1 :                 dims = get_position_12(dimension, (char*)attr->children->content);
    2622             : 
    2623          19 :         } else if (type->sdl_type != NULL &&
    2624           6 :                    type->sdl_type->attributes != NULL &&
    2625           4 :                    zend_hash_find(type->sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
    2626             :                                   sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType"),
    2627           8 :                                   (void **)&arrayType) == SUCCESS &&
    2628           1 :                    (*arrayType)->extraAttributes &&
    2629           1 :                    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType"), (void **)&ext) == SUCCESS) {
    2630             :                 char *type, *end;
    2631             : 
    2632           1 :                 type = estrdup((*ext)->val);
    2633           1 :                 end = strrchr(type,'[');
    2634           1 :                 if (end) {
    2635           1 :                         *end = '\0';
    2636             :                 }
    2637           1 :                 if ((*ext)->ns != NULL) {
    2638           1 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, type);
    2639             :                 }
    2640           1 :                 efree(type);
    2641             : 
    2642           1 :                 dims = emalloc(sizeof(int));
    2643           1 :                 *dims = 0;
    2644             : 
    2645          19 :         } else if (type->sdl_type != NULL &&
    2646           5 :                    type->sdl_type->attributes != NULL &&
    2647           3 :                    zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
    2648             :                                   sizeof(SOAP_1_2_ENC_NAMESPACE":itemType"),
    2649           6 :                                   (void **)&arrayType) == SUCCESS &&
    2650           2 :                    (*arrayType)->extraAttributes &&
    2651           2 :                    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType"), (void **)&ext) == SUCCESS) {
    2652             : 
    2653           2 :                 if ((*ext)->ns != NULL) {
    2654           2 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, (*ext)->val);
    2655             :                 }
    2656             : 
    2657          10 :                 if (zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2658             :                                    sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
    2659           4 :                                    (void **)&arrayType) == SUCCESS &&
    2660           2 :                     (*arrayType)->extraAttributes &&
    2661           2 :                     zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
    2662           2 :                         dimension = calc_dimension_12((*ext)->val);
    2663           2 :                         dims = get_position_12(dimension, (*ext)->val);
    2664             :                 } else {
    2665           0 :                         dims = emalloc(sizeof(int));
    2666           0 :                         *dims = 0;
    2667             :                 }
    2668          10 :         } else if (type->sdl_type != NULL &&
    2669           3 :                    type->sdl_type->attributes != NULL &&
    2670           1 :                    zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2671             :                                   sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
    2672           2 :                                   (void **)&arrayType) == SUCCESS &&
    2673           1 :                    (*arrayType)->extraAttributes &&
    2674           1 :                    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
    2675             : 
    2676           1 :                 dimension = calc_dimension_12((*ext)->val);
    2677           1 :                 dims = get_position_12(dimension, (*ext)->val);
    2678           7 :                 if (type->sdl_type && type->sdl_type->elements &&
    2679           1 :                     zend_hash_num_elements(type->sdl_type->elements) == 1 &&
    2680           1 :                     (zend_hash_internal_pointer_reset(type->sdl_type->elements),
    2681           1 :                      zend_hash_get_current_data(type->sdl_type->elements, (void**)&elementType) == SUCCESS) &&
    2682           2 :                     (elementType = *(sdlTypePtr*)elementType) != NULL &&
    2683           1 :                     elementType->encode) {
    2684           1 :                         enc = elementType->encode;
    2685             :                 }
    2686          14 :         } else if (type->sdl_type && type->sdl_type->elements &&
    2687           2 :                    zend_hash_num_elements(type->sdl_type->elements) == 1 &&
    2688           2 :                    (zend_hash_internal_pointer_reset(type->sdl_type->elements),
    2689           2 :                     zend_hash_get_current_data(type->sdl_type->elements, (void**)&elementType) == SUCCESS) &&
    2690           4 :                    (elementType = *(sdlTypePtr*)elementType) != NULL &&
    2691           2 :                    elementType->encode) {
    2692           2 :                 enc = elementType->encode;
    2693             :         }
    2694         682 :         if (dims == NULL) {
    2695           2 :                 dimension = 1;
    2696           2 :                 dims = emalloc(sizeof(int));
    2697           2 :                 *dims = 0;
    2698             :         }
    2699         682 :         pos = safe_emalloc(sizeof(int), dimension, 0);
    2700         682 :         memset(pos,0,sizeof(int)*dimension);
    2701        1364 :         if (data &&
    2702         682 :             (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         682 :         array_init(ret);
    2713         682 :         trav = data->children;
    2714        2219 :         while (trav) {
    2715         858 :                 if (trav->type == XML_ELEMENT_NODE) {
    2716             :                         int i;
    2717             :                         zval *tmpVal, *ar;
    2718         858 :                         xmlAttrPtr position = get_attribute(trav->properties,"position");
    2719             : 
    2720         858 :                         tmpVal = master_to_zval(enc, trav TSRMLS_CC);
    2721         855 :                         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         855 :                         i = 0;
    2731         855 :                         ar = ret;
    2732        1726 :                         while (i < dimension-1) {
    2733             :                                 zval** ar2;
    2734          16 :                                 if (zend_hash_index_find(Z_ARRVAL_P(ar), pos[i], (void**)&ar2) == SUCCESS) {
    2735           4 :                                         ar = *ar2;
    2736             :                                 } else {
    2737             :                                         zval *tmpAr;
    2738          12 :                                         MAKE_STD_ZVAL(tmpAr);
    2739          12 :                                         array_init(tmpAr);
    2740          12 :                                         zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpAr, sizeof(zval*), (void**)&ar2);
    2741          12 :                                         ar = *ar2;
    2742             :                                 }
    2743          16 :                                 i++;
    2744             :                         }
    2745         855 :                         zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpVal, sizeof(zval *), NULL);
    2746             : 
    2747             :                         /* Increment position */
    2748         855 :                         i = dimension;
    2749        2405 :                         while (i > 0) {
    2750         867 :                           i--;
    2751         867 :                           pos[i]++;
    2752         867 :                                 if (pos[i] >= dims[i]) {
    2753         695 :                                         if (i > 0) {
    2754          12 :                                                 pos[i] = 0;
    2755             :                                         } else {
    2756             :                                                 /* TODO: Array index overflow */
    2757             :                                         }
    2758             :                                 } else {
    2759         172 :                                   break;
    2760             :                                 }
    2761             :                         }
    2762             :                 }
    2763         855 :                 trav = trav->next;
    2764             :         }
    2765         679 :         efree(dims);
    2766         679 :         efree(pos);
    2767         679 :         return ret;
    2768             : }
    2769             : 
    2770             : /* Map encode/decode */
    2771           6 : static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    2772             : {
    2773             :         xmlNodePtr xmlParam;
    2774             :         int i;
    2775             : 
    2776           6 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2777           6 :         xmlAddChild(parent, xmlParam);
    2778           6 :         FIND_ZVAL_NULL(data, xmlParam, style);
    2779             : 
    2780           6 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    2781           6 :                 i = zend_hash_num_elements(Z_ARRVAL_P(data));
    2782           6 :                 zend_hash_internal_pointer_reset(data->value.ht);
    2783          20 :                 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          14 :                         zend_hash_get_current_data(data->value.ht, (void **)&temp_data);
    2791          14 :                         item = xmlNewNode(NULL, BAD_CAST("item"));
    2792          14 :                         xmlAddChild(xmlParam, item);
    2793          14 :                         key = xmlNewNode(NULL, BAD_CAST("key"));
    2794          14 :                         xmlAddChild(item,key);
    2795          14 :                         if (zend_hash_get_current_key(data->value.ht, &key_val, &int_val, FALSE) == HASH_KEY_IS_STRING) {
    2796           8 :                                 if (style == SOAP_ENCODED) {
    2797           8 :                                         set_xsi_type(key, "xsd:string");
    2798             :                                 }
    2799           8 :                                 xmlNodeSetContent(key, BAD_CAST(key_val));
    2800             :                         } else {
    2801           6 :                                 smart_str tmp = {0};
    2802           6 :                                 smart_str_append_long(&tmp, int_val);
    2803           6 :                                 smart_str_0(&tmp);
    2804             : 
    2805           6 :                                 if (style == SOAP_ENCODED) {
    2806           6 :                                         set_xsi_type(key, "xsd:int");
    2807             :                                 }
    2808           6 :                                 xmlNodeSetContentLen(key, BAD_CAST(tmp.c), tmp.len);
    2809             : 
    2810           6 :                                 smart_str_free(&tmp);
    2811             :                         }
    2812             : 
    2813          14 :                         xparam = master_to_xml(get_conversion((*temp_data)->type), (*temp_data), style, item TSRMLS_CC);
    2814          14 :                         xmlNodeSetName(xparam, BAD_CAST("value"));
    2815             : 
    2816          14 :                         zend_hash_move_forward(data->value.ht);
    2817             :                 }
    2818             :         }
    2819           6 :         if (style == SOAP_ENCODED) {
    2820           6 :                 set_ns_and_type(xmlParam, type);
    2821             :         }
    2822             : 
    2823           6 :         return xmlParam;
    2824             : }
    2825             : 
    2826         104 : 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         104 :         MAKE_STD_ZVAL(ret);
    2832         104 :         FIND_XML_NULL(data, ret);
    2833             : 
    2834         208 :         if (data && data->children) {
    2835         104 :                 array_init(ret);
    2836         104 :                 trav = data->children;
    2837             : 
    2838         104 :                 trav = data->children;
    2839         614 :                 FOREACHNODE(trav, "item", item) {
    2840         614 :                         xmlKey = get_node(item->children, "key");
    2841         614 :                         if (!xmlKey) {
    2842           0 :                                 soap_error0(E_ERROR,  "Encoding: Can't decode apache map, missing key");
    2843             :                         }
    2844             : 
    2845         614 :                         xmlValue = get_node(item->children, "value");
    2846         614 :                         if (!xmlKey) {
    2847           0 :                                 soap_error0(E_ERROR,  "Encoding: Can't decode apache map, missing value");
    2848             :                         }
    2849             : 
    2850         614 :                         key = master_to_zval(NULL, xmlKey TSRMLS_CC);
    2851         614 :                         value = master_to_zval(NULL, xmlValue TSRMLS_CC);
    2852             : 
    2853         614 :                         if (Z_TYPE_P(key) == IS_STRING) {
    2854         611 :                                 zend_symtable_update(Z_ARRVAL_P(ret), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &value, sizeof(zval *), NULL);
    2855           3 :                         } else if (Z_TYPE_P(key) == IS_LONG) {
    2856           3 :                                 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         614 :                         zval_ptr_dtor(&key);
    2861             :                 }
    2862         614 :                 ENDFOREACH(trav);
    2863             :         } else {
    2864           0 :                 ZVAL_NULL(ret);
    2865             :         }
    2866         104 :         return ret;
    2867             : }
    2868             : 
    2869             : /* Unknown encode/decode */
    2870         134 : static xmlNodePtr guess_xml_convert(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    2871             : {
    2872             :         encodePtr  enc;
    2873             :         xmlNodePtr ret;
    2874             : 
    2875         134 :         if (data) {
    2876         134 :                 enc = get_conversion(data->type);
    2877             :         } else {
    2878           0 :                 enc = get_conversion(IS_NULL);
    2879             :         }
    2880         134 :         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         133 :         return ret;
    2887             : }
    2888             : 
    2889        1361 : static zval *guess_zval_convert(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    2890             : {
    2891        1361 :         encodePtr enc = NULL;
    2892             :         xmlAttrPtr tmpattr;
    2893        1361 :         xmlChar *type_name = NULL;
    2894             :         zval *ret;
    2895             : 
    2896        1361 :         data = check_and_resolve_href(data);
    2897             : 
    2898        1361 :         if (data == NULL) {
    2899           0 :                 enc = get_conversion(IS_NULL);
    2900        1362 :         } else if (data->properties && get_attribute_ex(data->properties, "nil", XSI_NAMESPACE)) {
    2901           1 :                 enc = get_conversion(IS_NULL);
    2902             :         } else {
    2903        1360 :                 tmpattr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
    2904        1360 :                 if (tmpattr != NULL) {
    2905        1329 :                   type_name = tmpattr->children->content;
    2906        1329 :                         enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, tmpattr->children->content);
    2907        1329 :                         if (enc && type == &enc->details) {
    2908           0 :                                 enc = NULL;
    2909             :                         }
    2910        1329 :                         if (enc != NULL) {
    2911        1328 :                           encodePtr tmp = enc;
    2912        3991 :                           while (tmp &&
    2913        1329 :                                  tmp->details.sdl_type != NULL &&
    2914           5 :                                  tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
    2915           4 :                             if (enc == tmp->details.sdl_type->encode ||
    2916           2 :                                 tmp == tmp->details.sdl_type->encode) {
    2917           1 :                                 enc = NULL;
    2918           1 :                                 break;
    2919             :                             }
    2920           1 :                             tmp = tmp->details.sdl_type->encode;
    2921             :                           }
    2922             :                         }
    2923             :                 }
    2924             : 
    2925        1360 :                 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          98 :                         if (get_attribute(data->properties, "arrayType") ||
    2931          32 :                             get_attribute(data->properties, "itemType") ||
    2932          32 :                             get_attribute(data->properties, "arraySize")) {
    2933           1 :                                 enc = get_conversion(SOAP_ENC_ARRAY);
    2934             :                         } else {
    2935          32 :                                 enc = get_conversion(XSD_STRING);
    2936          32 :                                 trav = data->children;
    2937          87 :                                 while (trav != NULL) {
    2938          32 :                                         if (trav->type == XML_ELEMENT_NODE) {
    2939           9 :                                                 enc = get_conversion(SOAP_ENC_OBJECT);
    2940           9 :                                                 break;
    2941             :                                         }
    2942          23 :                                         trav = trav->next;
    2943             :                                 }
    2944             :                         }
    2945             :                 }
    2946             :         }
    2947        1361 :         ret = master_to_zval_int(enc, data TSRMLS_CC);
    2948        1361 :         if (SOAP_GLOBAL(sdl) && type_name && enc->details.sdl_type) {
    2949             :                 zval* soapvar;
    2950             :                 char *ns, *cptype;
    2951             :                 xmlNsPtr nsptr;
    2952             : 
    2953           3 :                 MAKE_STD_ZVAL(soapvar);
    2954           3 :                 object_init_ex(soapvar, soap_var_class_entry);
    2955           3 :                 add_property_long(soapvar, "enc_type", enc->details.type);
    2956             :                 Z_DELREF_P(ret);
    2957           3 :                 add_property_zval(soapvar, "enc_value", ret);
    2958           3 :                 parse_namespace(type_name, &cptype, &ns);
    2959           3 :                 nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
    2960           3 :                 add_property_string(soapvar, "enc_stype", cptype, 1);
    2961           3 :                 if (nsptr) {
    2962           3 :                         add_property_string(soapvar, "enc_ns", (char*)nsptr->href, 1);
    2963             :                 }
    2964           3 :                 efree(cptype);
    2965           3 :                 if (ns) {efree(ns);}
    2966           3 :                 ret = soapvar;
    2967             :         }
    2968        1361 :         return ret;
    2969             : }
    2970             : 
    2971             : /* Time encode/decode */
    2972          23 : 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          23 :         int max_reallocs = 5;
    2978          23 :         size_t buf_len=64, real_len;
    2979             :         char *buf;
    2980             :         char tzbuf[8];
    2981             : 
    2982             :         xmlNodePtr xmlParam;
    2983             : 
    2984          23 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2985          23 :         xmlAddChild(parent, xmlParam);
    2986          23 :         FIND_ZVAL_NULL(data, xmlParam, style);
    2987             : 
    2988          23 :         if (Z_TYPE_P(data) == IS_LONG) {
    2989           8 :                 timestamp = Z_LVAL_P(data);
    2990           8 :                 ta = php_localtime_r(&timestamp, &tmbuf);
    2991             :                 /*ta = php_gmtime_r(&timestamp, &tmbuf);*/
    2992           8 :                 if (!ta) {
    2993           0 :                         soap_error1(E_ERROR, "Encoding: Invalid timestamp %ld", Z_LVAL_P(data));
    2994             :                 }
    2995             : 
    2996           8 :                 buf = (char *) emalloc(buf_len);
    2997          16 :                 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           8 :                 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           8 :                 if (strcmp(tzbuf,"+00:00") == 0) {
    3014           8 :                   strcpy(tzbuf,"Z");
    3015           8 :                   real_len++;
    3016             :                 } else {
    3017           0 :                         real_len += 6;
    3018             :                 }
    3019           8 :                 if (real_len >= buf_len) {
    3020           0 :                         buf = (char *) erealloc(buf, real_len+1);
    3021             :                 }
    3022           8 :                 strcat(buf, tzbuf);
    3023             : 
    3024           8 :                 xmlNodeSetContent(xmlParam, BAD_CAST(buf));
    3025           8 :                 efree(buf);
    3026          15 :         } else if (Z_TYPE_P(data) == IS_STRING) {
    3027          15 :                 xmlNodeSetContentLen(xmlParam, BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
    3028             :         }
    3029             : 
    3030          23 :         if (style == SOAP_ENCODED) {
    3031          14 :                 set_ns_and_type(xmlParam, type);
    3032             :         }
    3033          23 :         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          16 : static xmlNodePtr to_xml_datetime(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3043             : {
    3044          16 :         return to_xml_datetime_ex(type, data, "%Y-%m-%dT%H:%M:%S", style, parent TSRMLS_CC);
    3045             : }
    3046             : 
    3047           1 : static xmlNodePtr to_xml_time(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3048             : {
    3049             :         /* TODO: microsecconds */
    3050           1 :         return to_xml_datetime_ex(type, data, "%H:%M:%S", style, parent TSRMLS_CC);
    3051             : }
    3052             : 
    3053           1 : static xmlNodePtr to_xml_date(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3054             : {
    3055           1 :         return to_xml_datetime_ex(type, data, "%Y-%m-%d", style, parent TSRMLS_CC);
    3056             : }
    3057             : 
    3058           1 : static xmlNodePtr to_xml_gyearmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3059             : {
    3060           1 :         return to_xml_datetime_ex(type, data, "%Y-%m", style, parent TSRMLS_CC);
    3061             : }
    3062             : 
    3063           1 : static xmlNodePtr to_xml_gyear(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3064             : {
    3065           1 :         return to_xml_datetime_ex(type, data, "%Y", style, parent TSRMLS_CC);
    3066             : }
    3067             : 
    3068           1 : static xmlNodePtr to_xml_gmonthday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3069             : {
    3070           1 :         return to_xml_datetime_ex(type, data, "--%m-%d", style, parent TSRMLS_CC);
    3071             : }
    3072             : 
    3073           1 : static xmlNodePtr to_xml_gday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3074             : {
    3075           1 :         return to_xml_datetime_ex(type, data, "---%d", style, parent TSRMLS_CC);
    3076             : }
    3077             : 
    3078           1 : static xmlNodePtr to_xml_gmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3079             : {
    3080           1 :         return to_xml_datetime_ex(type, data, "--%m--", style, parent TSRMLS_CC);
    3081             : }
    3082             : 
    3083          16 : static zval* to_zval_list(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC) {
    3084             :         /*FIXME*/
    3085          16 :         return to_zval_stringc(enc, data TSRMLS_CC);
    3086             : }
    3087             : 
    3088          23 : static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC) {
    3089             :         xmlNodePtr ret;
    3090          23 :         encodePtr list_enc = NULL;
    3091             : 
    3092          23 :         if (enc->sdl_type && enc->sdl_type->kind == XSD_TYPEKIND_LIST && enc->sdl_type->elements) {
    3093             :                 sdlTypePtr *type;
    3094             : 
    3095           6 :                 zend_hash_internal_pointer_reset(enc->sdl_type->elements);
    3096           6 :                 if (zend_hash_get_current_data(enc->sdl_type->elements, (void**)&type) == SUCCESS) {
    3097           6 :                         list_enc = (*type)->encode;
    3098             :                 }
    3099             :         }
    3100             : 
    3101          23 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    3102          23 :         xmlAddChild(parent, ret);
    3103          23 :         FIND_ZVAL_NULL(data, ret, style);
    3104          23 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    3105             :                 zval **tmp;
    3106           4 :                 smart_str list = {0};
    3107           4 :                 HashTable *ht = Z_ARRVAL_P(data);
    3108             : 
    3109           4 :                 zend_hash_internal_pointer_reset(ht);
    3110          17 :                 while (zend_hash_get_current_data(ht, (void**)&tmp) == SUCCESS) {
    3111           9 :                         xmlNodePtr dummy = master_to_xml(list_enc, *tmp, SOAP_LITERAL, ret TSRMLS_CC);
    3112          18 :                         if (dummy && dummy->children && dummy->children->content) {
    3113           9 :                                 if (list.len != 0) {
    3114           5 :                                         smart_str_appendc(&list, ' ');
    3115             :                                 }
    3116           9 :                                 smart_str_appends(&list, (char*)dummy->children->content);
    3117             :                         } else {
    3118           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    3119             :                         }
    3120           9 :                         xmlUnlinkNode(dummy);
    3121           9 :                         xmlFreeNode(dummy);
    3122           9 :                         zend_hash_move_forward(ht);
    3123             :                 }
    3124           4 :                 smart_str_0(&list);
    3125           4 :                 xmlNodeSetContentLen(ret, BAD_CAST(list.c), list.len);
    3126           4 :                 smart_str_free(&list);
    3127             :         } else {
    3128          19 :                 zval tmp = *data;
    3129             :                 char *str, *start, *next;
    3130          19 :                 smart_str list = {0};
    3131             : 
    3132          19 :                 if (Z_TYPE_P(data) != IS_STRING) {
    3133             :                         zval_copy_ctor(&tmp);
    3134           5 :                         convert_to_string(&tmp);
    3135           5 :                         data = &tmp;
    3136             :                 }
    3137          19 :                 str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
    3138          19 :                 whiteSpace_collapse(BAD_CAST(str));
    3139          19 :                 start = str;
    3140          63 :                 while (start != NULL && *start != '\0') {
    3141             :                         xmlNodePtr dummy;
    3142             :                         zval dummy_zval;
    3143             : 
    3144          25 :                         next = strchr(start,' ');
    3145          25 :                         if (next != NULL) {
    3146           6 :                           *next = '\0';
    3147           6 :                           next++;
    3148             :                         }
    3149          25 :                         ZVAL_STRING(&dummy_zval, start, 0);
    3150          25 :                         dummy = master_to_xml(list_enc, &dummy_zval, SOAP_LITERAL, ret TSRMLS_CC);
    3151          50 :                         if (dummy && dummy->children && dummy->children->content) {
    3152          25 :                                 if (list.len != 0) {
    3153           6 :                                         smart_str_appendc(&list, ' ');
    3154             :                                 }
    3155          25 :                                 smart_str_appends(&list, (char*)dummy->children->content);
    3156             :                         } else {
    3157           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    3158             :                         }
    3159          25 :                         xmlUnlinkNode(dummy);
    3160          25 :                         xmlFreeNode(dummy);
    3161             : 
    3162          25 :                         start = next;
    3163             :                 }
    3164          19 :                 smart_str_0(&list);
    3165          19 :                 xmlNodeSetContentLen(ret, BAD_CAST(list.c), list.len);
    3166          19 :                 smart_str_free(&list);
    3167          19 :                 efree(str);
    3168          19 :                 if (data == &tmp) {zval_dtor(&tmp);}
    3169             :         }
    3170          23 :         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          10 : static zval* to_zval_union(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC) {
    3179             :         /*FIXME*/
    3180          10 :         return to_zval_list(enc, data TSRMLS_CC);
    3181             : }
    3182             : 
    3183          17 : static xmlNodePtr to_xml_union(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC) {
    3184             :         /*FIXME*/
    3185          17 :         return to_xml_list(enc,data,style, parent TSRMLS_CC);
    3186             : }
    3187             : 
    3188           4 : static zval *to_zval_any(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
    3189             : {
    3190             :         xmlBufferPtr buf;
    3191             :         zval *ret;
    3192             : 
    3193           4 :         if (SOAP_GLOBAL(sdl) && SOAP_GLOBAL(sdl)->elements && data->name) {
    3194           4 :                 smart_str nscat = {0};          
    3195             :                 sdlTypePtr *sdl_type;
    3196             : 
    3197           4 :                 if (data->ns && data->ns->href) {
    3198           1 :                         smart_str_appends(&nscat, (char*)data->ns->href);
    3199           1 :                         smart_str_appendc(&nscat, ':');                     
    3200             :                 }
    3201           4 :                 smart_str_appends(&nscat, (char*)data->name);
    3202           4 :                 smart_str_0(&nscat);
    3203             : 
    3204           4 :                 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           4 :                 smart_str_free(&nscat);
    3210             :         }
    3211             : 
    3212           4 :         buf = xmlBufferCreate();
    3213           4 :         xmlNodeDump(buf, NULL, data, 0, 0);
    3214           4 :         MAKE_STD_ZVAL(ret);
    3215           4 :         ZVAL_STRING(ret, (char*)xmlBufferContent(buf), 1);
    3216           4 :         xmlBufferFree(buf);
    3217           4 :         return ret;
    3218             : }
    3219             : 
    3220           8 : static xmlNodePtr to_xml_any(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3221             : {
    3222           8 :         xmlNodePtr ret = NULL;
    3223             : 
    3224           8 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    3225             :                 HashPosition pos;
    3226             :                 zval **el;
    3227           1 :                 encodePtr enc = get_conversion(XSD_ANYXML);
    3228             :                 char *name;
    3229             :                 uint name_len;
    3230             :                 ulong idx;
    3231             : 
    3232           3 :                 for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(data), &pos);
    3233           2 :                      zend_hash_get_current_data_ex(Z_ARRVAL_P(data), (void **) &el, &pos) == SUCCESS;
    3234           1 :                      zend_hash_move_forward_ex(Z_ARRVAL_P(data), &pos)) {
    3235           1 :                         ret = master_to_xml(enc, *el, style, parent TSRMLS_CC);
    3236           3 :                     if (ret &&
    3237           1 :                         ret->name != xmlStringTextNoenc &&
    3238           1 :                         zend_hash_get_current_key_ex(Z_ARRVAL_P(data), &name, &name_len, &idx, 0, &pos) == HASH_KEY_IS_STRING) {
    3239           1 :                                 xmlNodeSetName(ret, BAD_CAST(name));
    3240             :                     }
    3241             :                 }
    3242           1 :                 return ret;
    3243             :         }
    3244           7 :         if (Z_TYPE_P(data) == IS_STRING) {
    3245           7 :                 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           7 :         ret->name = xmlStringTextNoenc;
    3256           7 :         ret->parent = parent;
    3257           7 :         ret->doc = parent->doc;
    3258           7 :         ret->prev = parent->last;
    3259           7 :         ret->next = NULL;
    3260           7 :         if (parent->last) {
    3261           2 :                 parent->last->next = ret;
    3262             :         } else {
    3263           5 :                 parent->children = ret;
    3264             :         }
    3265           7 :         parent->last = ret;
    3266             : 
    3267           7 :         return ret;
    3268             : }
    3269             : 
    3270         573 : zval *sdl_guess_convert_zval(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC)
    3271             : {
    3272             :         sdlTypePtr type;
    3273             : 
    3274         573 :         type = enc->sdl_type;
    3275         573 :         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         573 :         switch (type->kind) {
    3308             :                 case XSD_TYPEKIND_SIMPLE:
    3309          37 :                         if (type->encode && enc != &type->encode->details) {
    3310          36 :                                 return master_to_zval_int(type->encode, data TSRMLS_CC);
    3311             :                         } else {
    3312           1 :                                 return guess_zval_convert(enc, data TSRMLS_CC);
    3313             :                         }
    3314             :                         break;
    3315             :                 case XSD_TYPEKIND_LIST:
    3316           6 :                         return to_zval_list(enc, data TSRMLS_CC);
    3317             :                 case XSD_TYPEKIND_UNION:
    3318          10 :                         return to_zval_union(enc, data TSRMLS_CC);
    3319             :                 case XSD_TYPEKIND_COMPLEX:
    3320             :                 case XSD_TYPEKIND_RESTRICTION:
    3321             :                 case XSD_TYPEKIND_EXTENSION:
    3322         688 :                         if (type->encode &&
    3323         111 :                             (type->encode->details.type == IS_ARRAY ||
    3324          57 :                              type->encode->details.type == SOAP_ENC_ARRAY)) {
    3325          54 :                                 return to_zval_array(enc, data TSRMLS_CC);
    3326             :                         }
    3327         466 :                         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         700 : xmlNodePtr sdl_guess_convert_xml(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
    3335             : {
    3336             :         sdlTypePtr type;
    3337         700 :         xmlNodePtr ret = NULL;
    3338             : 
    3339         700 :         type = enc->sdl_type;
    3340             : 
    3341         700 :         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         700 :         switch(type->kind) {
    3372             :                 case XSD_TYPEKIND_SIMPLE:
    3373          88 :                         if (type->encode && enc != &type->encode->details) {
    3374          43 :                                 ret = master_to_xml(type->encode, data, style, parent TSRMLS_CC);
    3375             :                         } else {
    3376           2 :                                 ret = guess_xml_convert(enc, data, style, parent TSRMLS_CC);
    3377             :                         }
    3378          45 :                         break;
    3379             :                 case XSD_TYPEKIND_LIST:
    3380           6 :                         ret = to_xml_list(enc, data, style, parent TSRMLS_CC);
    3381           6 :                         break;
    3382             :                 case XSD_TYPEKIND_UNION:
    3383          17 :                         ret = to_xml_union(enc, data, style, parent TSRMLS_CC);
    3384          17 :                         break;
    3385             :                 case XSD_TYPEKIND_COMPLEX:
    3386             :                 case XSD_TYPEKIND_RESTRICTION:
    3387             :                 case XSD_TYPEKIND_EXTENSION:
    3388         852 :                         if (type->encode &&
    3389         155 :                             (type->encode->details.type == IS_ARRAY ||
    3390          65 :                              type->encode->details.type == SOAP_ENC_ARRAY)) {
    3391          90 :                                 return to_xml_array(enc, data, style, parent TSRMLS_CC);
    3392             :                         } else {
    3393         542 :                                 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          68 :         if (style == SOAP_ENCODED) {
    3401          33 :                 set_ns_and_type(ret, enc);
    3402             :         }
    3403          68 :         return ret;
    3404             : }
    3405             : 
    3406        6358 : static xmlNodePtr check_and_resolve_href(xmlNodePtr data)
    3407             : {
    3408        6358 :         if (data && data->properties) {
    3409             :                 xmlAttrPtr href;
    3410             : 
    3411        5350 :                 href = data->properties;
    3412             :                 while (1) {
    3413        5352 :                         href = get_attribute(href, "href");
    3414        5352 :                         if (href == NULL || href->ns == NULL) {break;}
    3415           2 :                         href = href->next;
    3416           2 :                 }
    3417        5350 :                 if (href) {
    3418             :                         /*  Internal href try and find node */
    3419         219 :                         if (href->children->content[0] == '#') {
    3420         219 :                                 xmlNodePtr ret = get_node_with_attribute_recursive(data->doc->children, NULL, "id", (char*)&href->children->content[1]);
    3421         219 :                                 if (!ret) {
    3422           0 :                                         soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
    3423             :                                 }
    3424         219 :                                 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        5131 :                 href = get_attribute_ex(data->properties, "ref", SOAP_1_2_ENC_NAMESPACE);
    3432        5131 :                 if (href) {
    3433             :                         xmlChar* id;
    3434             :                         xmlNodePtr ret;
    3435             : 
    3436           4 :                         if (href->children->content[0] == '#') {
    3437           3 :                                 id = href->children->content+1;
    3438             :                         } else {
    3439           1 :                                 id = href->children->content;
    3440             :                         }
    3441           4 :                         ret = get_node_with_attribute_recursive_ex(data->doc->children, NULL, NULL, "id", (char*)id, SOAP_1_2_ENC_NAMESPACE);
    3442           4 :                         if (!ret) {
    3443           1 :                                 soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
    3444           3 :                         } else if (ret == data) {
    3445           1 :                                 soap_error1(E_ERROR, "Encoding: Violation of id and ref information items '%s'", href->children->content);
    3446             :                         }
    3447           2 :                         return ret;
    3448             :                 }
    3449             :         }
    3450        6135 :         return data;
    3451             : }
    3452             : 
    3453        1393 : static void set_ns_and_type(xmlNodePtr node, encodeTypePtr type)
    3454             : {
    3455        1393 :         set_ns_and_type_ex(node, type->ns, type->type_str);
    3456        1393 : }
    3457             : 
    3458        1419 : static void set_ns_and_type_ex(xmlNodePtr node, char *ns, char *type)
    3459             : {
    3460        1419 :         smart_str nstype = {0};
    3461        1419 :         get_type_str(node, ns, type, &nstype);
    3462        1419 :         set_xsi_type(node, nstype.c);
    3463        1419 :         smart_str_free(&nstype);
    3464        1419 : }
    3465             : 
    3466           1 : static xmlNsPtr xmlSearchNsPrefixByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
    3467             : {
    3468             :         xmlNsPtr cur;
    3469           1 :         xmlNodePtr orig = node;
    3470             : 
    3471           3 :         while (node) {
    3472           6 :                 if (node->type == XML_ENTITY_REF_NODE ||
    3473           2 :                     node->type == XML_ENTITY_NODE ||
    3474           2 :                     node->type == XML_ENTITY_DECL) {
    3475           0 :                         return NULL;
    3476             :                 }
    3477           2 :                 if (node->type == XML_ELEMENT_NODE) {
    3478           2 :                         cur = node->nsDef;
    3479           6 :                         while (cur != NULL) {
    3480           2 :                                 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           2 :                                 cur = cur->next;
    3486             :                         }
    3487           2 :                         if (orig != node) {
    3488           1 :                                 cur = node->ns;
    3489           1 :                                 if (cur != NULL) {
    3490           1 :                                         if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
    3491           1 :                                                 if (xmlSearchNs(doc, node, cur->prefix) == cur) {
    3492           1 :                                                         return cur;
    3493             :                                                 }
    3494             :                                         }
    3495             :                                 }
    3496             :                         }    
    3497             :                 }
    3498           1 :                 node = node->parent;
    3499             :         }
    3500           0 :         return NULL;
    3501             : }
    3502             : 
    3503        4732 : xmlNsPtr encode_add_ns(xmlNodePtr node, const char* ns)
    3504             : {
    3505             :         xmlNsPtr xmlns;
    3506             : 
    3507        4732 :         if (ns == NULL) {
    3508         104 :           return NULL;
    3509             :         }
    3510             : 
    3511        4628 :         xmlns = xmlSearchNsByHref(node->doc, node, BAD_CAST(ns));
    3512        4628 :         if (xmlns != NULL && xmlns->prefix == NULL) {
    3513           1 :                 xmlns = xmlSearchNsPrefixByHref(node->doc, node, BAD_CAST(ns));
    3514             :         }
    3515        4628 :         if (xmlns == NULL) {
    3516             :                 xmlChar* prefix;
    3517             :         TSRMLS_FETCH();
    3518             : 
    3519        1957 :                 if (zend_hash_find(&SOAP_GLOBAL(defEncNs), (char*)ns, strlen(ns) + 1, (void **)&prefix) == SUCCESS) {
    3520        1035 :                         xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), prefix);
    3521             :                 } else {
    3522         922 :                         smart_str prefix = {0};
    3523         922 :                         int num = ++SOAP_GLOBAL(cur_uniq_ns);
    3524             : 
    3525             :                         while (1) {
    3526         922 :                                 smart_str_appendl(&prefix, "ns", 2);
    3527         922 :                                 smart_str_append_long(&prefix, num);
    3528         922 :                                 smart_str_0(&prefix);
    3529         922 :                                 if (xmlSearchNs(node->doc, node, BAD_CAST(prefix.c)) == NULL) {
    3530         922 :                                         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         922 :                         xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), BAD_CAST(prefix.c));
    3539         922 :                         smart_str_free(&prefix);
    3540             :                 }
    3541             :         }
    3542        4628 :         return xmlns;
    3543             : }
    3544             : 
    3545        1580 : static void set_ns_prop(xmlNodePtr node, char *ns, char *name, char *val)
    3546             : {
    3547        1580 :         xmlSetNsProp(node, encode_add_ns(node, ns), BAD_CAST(name), BAD_CAST(val));
    3548        1580 : }
    3549             : 
    3550          34 : static void set_xsi_nil(xmlNodePtr node)
    3551             : {
    3552          34 :         set_ns_prop(node, XSI_NAMESPACE, "nil", "true");
    3553          34 : }
    3554             : 
    3555        1433 : static void set_xsi_type(xmlNodePtr node, char *type)
    3556             : {
    3557        1433 :         set_ns_prop(node, XSI_NAMESPACE, "type", type);
    3558        1433 : }
    3559             : 
    3560        1415 : void encode_reset_ns()
    3561             : {
    3562             :         TSRMLS_FETCH();
    3563        1415 :         SOAP_GLOBAL(cur_uniq_ns) = 0;
    3564        1415 :         SOAP_GLOBAL(cur_uniq_ref) = 0;
    3565        1415 :         if (SOAP_GLOBAL(ref_map)) {
    3566          20 :                 zend_hash_destroy(SOAP_GLOBAL(ref_map));
    3567             :         } else {
    3568        1395 :                 SOAP_GLOBAL(ref_map) = emalloc(sizeof(HashTable));
    3569             :         }
    3570        1415 :         zend_hash_init(SOAP_GLOBAL(ref_map), 0, NULL, NULL, 0);
    3571        1415 : }
    3572             : 
    3573        1394 : void encode_finish()
    3574             : {
    3575             :         TSRMLS_FETCH();
    3576        1394 :         SOAP_GLOBAL(cur_uniq_ns) = 0;
    3577        1394 :         SOAP_GLOBAL(cur_uniq_ref) = 0;
    3578        1394 :         if (SOAP_GLOBAL(ref_map)) {
    3579        1394 :                 zend_hash_destroy(SOAP_GLOBAL(ref_map));
    3580        1394 :                 efree(SOAP_GLOBAL(ref_map));
    3581        1394 :                 SOAP_GLOBAL(ref_map) = NULL;
    3582             :         }
    3583        1394 : }
    3584             : 
    3585        2036 : encodePtr get_conversion(int encode)
    3586             : {
    3587        2036 :         encodePtr *enc = NULL;
    3588             :         TSRMLS_FETCH();
    3589             : 
    3590        2036 :         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        2036 :                 return *enc;
    3595             :         }
    3596             : }
    3597             : 
    3598         184 : static int is_map(zval *array)
    3599             : {
    3600         184 :         int i, count = zend_hash_num_elements(Z_ARRVAL_P(array));
    3601             : 
    3602         184 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(array));
    3603         319 :         for (i = 0; i < count; i++) {
    3604             :                 char *str_index;
    3605             :                 ulong num_index;
    3606             : 
    3607         400 :                 if (zend_hash_get_current_key(Z_ARRVAL_P(array), &str_index, &num_index, 0) == HASH_KEY_IS_STRING ||
    3608         137 :                     num_index != i) {
    3609         128 :                         return TRUE;
    3610             :                 }
    3611         135 :                 zend_hash_move_forward(Z_ARRVAL_P(array));
    3612             :         }
    3613          56 :         return FALSE;
    3614             : }
    3615             : 
    3616          35 : 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          35 :         char *prev_stype = NULL, *cur_stype = NULL, *prev_ns = NULL, *cur_ns = NULL;
    3622             : 
    3623          35 :         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          35 :         different = FALSE;
    3629          35 :         cur_type = prev_type = 0;
    3630          35 :         ht = HASH_OF(array);
    3631          35 :         count = zend_hash_num_elements(ht);
    3632             : 
    3633          35 :         zend_hash_internal_pointer_reset(ht);
    3634         103 :         for (i = 0;i < count;i++) {
    3635          68 :                 zend_hash_get_current_data(ht, (void **)&tmp);
    3636             : 
    3637         134 :                 if (Z_TYPE_PP(tmp) == IS_OBJECT &&
    3638          44 :                     Z_OBJCE_PP(tmp) == soap_var_class_entry) {
    3639             :                         zval **ztype;
    3640             : 
    3641          22 :                         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          22 :                         cur_type = Z_LVAL_PP(ztype);
    3645             : 
    3646          22 :                         if (zend_hash_find(Z_OBJPROP_PP(tmp), "enc_stype", sizeof("enc_stype"), (void **)&ztype) == SUCCESS) {
    3647           2 :                                 cur_stype = Z_STRVAL_PP(ztype);
    3648             :                         } else {
    3649          20 :                                 cur_stype = NULL;
    3650             :                         }
    3651             : 
    3652          22 :                         if (zend_hash_find(Z_OBJPROP_PP(tmp), "enc_ns", sizeof("enc_ns"), (void **)&ztype) == SUCCESS) {
    3653           2 :                                 cur_ns = Z_STRVAL_PP(ztype);
    3654             :                         } else {
    3655          20 :                                 cur_ns = NULL;
    3656             :                         }
    3657             : 
    3658          46 :                 } 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          46 :                         cur_type = Z_TYPE_PP(tmp);
    3664          46 :                         cur_stype = NULL;
    3665          46 :                         cur_ns = NULL;
    3666             :                 }
    3667             : 
    3668          68 :                 if (i > 0) {
    3669          37 :                         if ((cur_type != prev_type) ||
    3670           1 :                             (cur_stype != NULL && prev_stype != NULL && strcmp(cur_stype,prev_stype) != 0) ||
    3671             :                             (cur_stype == NULL && cur_stype != prev_stype) ||
    3672           1 :                             (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          68 :                 prev_type = cur_type;
    3680          68 :                 prev_stype = cur_stype;
    3681          68 :                 prev_ns = cur_ns;
    3682          68 :                 zend_hash_move_forward(ht);
    3683             :         }
    3684             : 
    3685          35 :         if (different || count == 0) {
    3686           2 :                 smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
    3687           2 :                 return get_conversion(XSD_ANYTYPE);
    3688             :         } else {
    3689             :                 encodePtr enc;
    3690             : 
    3691          33 :                 if (cur_stype != NULL) {
    3692           1 :                         smart_str array_type = {0};
    3693             : 
    3694           1 :                         if (cur_ns) {
    3695           1 :                                 xmlNsPtr ns = encode_add_ns(node, cur_ns);
    3696             : 
    3697           1 :                                 smart_str_appends(type, (char*)ns->prefix);
    3698           1 :                                 smart_str_appendc(type, ':');
    3699           1 :                                 smart_str_appends(&array_type, cur_ns);
    3700           1 :                                 smart_str_appendc(&array_type, ':');
    3701             :                         }
    3702           1 :                         smart_str_appends(type, cur_stype);
    3703           1 :                         smart_str_0(type);
    3704           1 :                         smart_str_appends(&array_type, cur_stype);
    3705           1 :                         smart_str_0(&array_type);
    3706             : 
    3707           1 :                         enc = get_encoder_ex(SOAP_GLOBAL(sdl), array_type.c, array_type.len);
    3708           1 :                         smart_str_free(&array_type);
    3709           1 :                         return enc;
    3710             :                 } else {
    3711          32 :                         enc = get_conversion(cur_type);
    3712          32 :                         get_type_str(node, enc->details.ns, enc->details.type_str, type);
    3713          32 :                         return enc;
    3714             :                 }
    3715             :         }
    3716             : }
    3717             : 
    3718        1527 : static void get_type_str(xmlNodePtr node, const char* ns, const char* type, smart_str* ret)
    3719             : {
    3720             :         TSRMLS_FETCH();
    3721             : 
    3722        1527 :         if (ns) {
    3723             :                 xmlNsPtr xmlns;
    3724        1601 :                 if (SOAP_GLOBAL(soap_version) == SOAP_1_2 &&
    3725          71 :                     strcmp(ns,SOAP_1_1_ENC_NAMESPACE) == 0) {
    3726           3 :                         ns = SOAP_1_2_ENC_NAMESPACE;
    3727        2980 :                 } else if (SOAP_GLOBAL(soap_version) == SOAP_1_1 &&
    3728        1456 :                            strcmp(ns,SOAP_1_2_ENC_NAMESPACE) == 0) {
    3729           0 :                         ns = SOAP_1_1_ENC_NAMESPACE;
    3730             :                 }
    3731        1527 :                 xmlns = encode_add_ns(node, ns);
    3732        1527 :                 smart_str_appends(ret, (char*)xmlns->prefix);
    3733        1527 :                 smart_str_appendc(ret, ':');
    3734             :         }
    3735        1527 :         smart_str_appendl(ret, type, strlen(type));
    3736        1527 :         smart_str_0(ret);
    3737        1527 : }
    3738             : 
    3739          15 : static void delete_mapping(void *data)
    3740             : {
    3741          15 :         soapMappingPtr map = (soapMappingPtr)data;
    3742             : 
    3743          15 :         if (map->to_xml) {
    3744           7 :                 zval_ptr_dtor(&map->to_xml);
    3745             :         }
    3746          15 :         if (map->to_zval) {
    3747           6 :                 zval_ptr_dtor(&map->to_zval);
    3748             :         }
    3749          15 :         efree(map);
    3750          15 : }
    3751             : 
    3752        8026 : void delete_encoder(void *encode)
    3753             : {
    3754        8026 :         encodePtr t = *((encodePtr*)encode);
    3755        8026 :         if (t->details.ns) {
    3756        8026 :                 efree(t->details.ns);
    3757             :         }
    3758        8026 :         if (t->details.type_str) {
    3759        8026 :                 efree(t->details.type_str);
    3760             :         }
    3761        8026 :         if (t->details.map) {
    3762          15 :                 delete_mapping(t->details.map);
    3763             :         }
    3764        8026 :         efree(t);
    3765        8026 : }
    3766             : 
    3767          10 : void delete_encoder_persistent(void *encode)
    3768             : {
    3769          10 :         encodePtr t = *((encodePtr*)encode);
    3770          10 :         if (t->details.ns) {
    3771          10 :                 free(t->details.ns);
    3772             :         }
    3773          10 :         if (t->details.type_str) {
    3774          10 :                 free(t->details.type_str);
    3775             :         }
    3776             :         /* we should never have mapping in persistent encoder */
    3777             :         assert(t->details.map == NULL);
    3778          10 :         free(t);
    3779          10 : }

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:12 +0000 (44 days ago)

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