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

Generated by: LCOV version 1.10

Generated at Wed, 24 Aug 2016 12:20:29 +0000 (3 days ago)

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