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: 1617 1903 85.0 %
Date: 2014-09-27 Functions: 84 87 96.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 27 Sep 2014 16:43:17 +0000 (3 days ago)

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