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: 1610 1892 85.1 %
Date: 2015-09-02 Functions: 84 87 96.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:17 +0000 (32 hours ago)

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