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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:03 +0000 (23 hours ago)

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