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: 1748 2038 85.8 %
Date: 2014-10-24 Functions: 84 87 96.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 24 Oct 2014 05:21:54 +0000 (2 days ago)

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