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

Generated by: LCOV version 1.10

Generated at Sun, 27 Jul 2014 12:58:35 +0000 (21 hours ago)

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