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

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:23 +0000 (47 hours ago)

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