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

Generated by: LCOV version 1.10

Generated at Sat, 02 May 2015 22:46:37 +0000 (2 days ago)

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