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: 1616 1897 85.2 %
Date: 2016-09-27 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         557 : static zend_bool soap_check_zval_ref(zval *data, xmlNodePtr node) {
     287             :         xmlNodePtr node_ptr;
     288             : 
     289         557 :         if (SOAP_GLOBAL(ref_map)) {
     290         557 :                 if (Z_TYPE_P(data) == IS_OBJECT) {
     291         402 :                         data = (zval*)Z_OBJ_P(data);
     292             :                 }
     293        1114 :                 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         491 :                         zend_hash_index_update_ptr(SOAP_GLOBAL(ref_map), (zend_ulong)data, node);
     348             :                 }
     349             :         }
     350         491 :         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        2832 : static xmlNodePtr master_to_xml_int(encodePtr encode, zval *data, int style, xmlNodePtr parent, int check_class_map)
     379             : {
     380        2832 :         xmlNodePtr node = NULL;
     381        2832 :         int add_type = 0;
     382             : 
     383             :         /* Special handling of class SoapVar */
     384        6284 :         if (data &&
     385             :             Z_TYPE_P(data) == IS_OBJECT &&
     386         529 :             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        2888 :                 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        2724 :                 if (encode == NULL) {
     483         131 :                         encode = get_conversion(UNKNOWN_TYPE);
     484             :                 }
     485        2724 :                 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        2724 :                 if (encode->to_xml) {
     501        2724 :                         node = encode->to_xml(&encode->details, data, style, parent);
     502        2720 :                         if (add_type) {
     503           1 :                                 set_ns_and_type(node, &encode->details);
     504             :                         }
     505             :                 }
     506             :         }
     507        2828 :         return node;
     508             : }
     509             : 
     510        2695 : xmlNodePtr master_to_xml(encodePtr encode, zval *data, int style, xmlNodePtr parent)
     511             : {
     512        2695 :         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         821 : 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         821 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
     837         821 :         xmlAddChild(parent, ret);
     838        1641 :         FIND_ZVAL_NULL(data, ret, style);
     839             : 
     840         813 :         if (Z_TYPE_P(data) == IS_STRING) {
     841         811 :                 str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
     842         811 :                 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         813 :         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         813 :         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         812 :         text = xmlNewTextLen(BAD_CAST(str), new_len);
     906         812 :         xmlAddChild(ret, text);
     907         812 :         efree(str);
     908             : 
     909         812 :         if (style == SOAP_ENCODED) {
     910         435 :                 set_ns_and_type(ret, type);
     911             :         }
     912         812 :         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         589 : static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    1801             : {
    1802             :         xmlNodePtr xmlParam;
    1803         589 :         HashTable *prop = NULL;
    1804         589 :         sdlTypePtr sdlType = type->sdl_type;
    1805             : 
    1806        1163 :         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         557 :         if (Z_TYPE_P(data) == IS_OBJECT) {
    1817         402 :                 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         557 :         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          43 :                 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    1950          43 :                 xmlAddChild(parent, xmlParam);
    1951             : 
    1952          43 :                 if (soap_check_zval_ref(data, xmlParam)) {
    1953           8 :                         return xmlParam;
    1954             :                 }
    1955          35 :                 if (prop != NULL) {
    1956             :                         zval *zprop;
    1957             :                         zend_string *str_key;
    1958             :                         xmlNodePtr property;
    1959             : 
    1960         287 :                         ZEND_HASH_FOREACH_STR_KEY_VAL_IND(prop, str_key, zprop) {
    1961          84 :                                 ZVAL_DEREF(zprop);
    1962          84 :                                 property = master_to_xml(get_conversion(Z_TYPE_P(zprop)), zprop, style, xmlParam);
    1963             : 
    1964          84 :                                 if (str_key) {
    1965             :                                         const char *prop_name;
    1966             : 
    1967          60 :                                         if (Z_TYPE_P(data) == IS_OBJECT) {
    1968             :                                                 const char *class_name;
    1969             : 
    1970          60 :                                                 zend_unmangle_property_name(str_key, &class_name, &prop_name);
    1971             :                                         } else {
    1972           0 :                                                 prop_name = ZSTR_VAL(str_key);
    1973             :                                         }
    1974          60 :                                         if (prop_name) {
    1975          60 :                                                 xmlNodeSetName(property, BAD_CAST(prop_name));
    1976             :                                         }
    1977             :                                 }
    1978             :                         } ZEND_HASH_FOREACH_END();
    1979             :                 }
    1980          35 :                 if (style == SOAP_ENCODED) {
    1981          33 :                         set_ns_and_type(xmlParam, type);
    1982             :                 }
    1983             :         }
    1984         543 :         return xmlParam;
    1985             : }
    1986             : 
    1987             : /* Array encode/decode */
    1988          23 : static xmlNodePtr guess_array_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    1989             : {
    1990          23 :         encodePtr enc = NULL;
    1991             : 
    1992          46 :         if (data && Z_TYPE_P(data) == IS_ARRAY) {
    1993          23 :                 if (is_map(data)) {
    1994           6 :                         enc = get_conversion(APACHE_MAP);
    1995             :                 } else {
    1996          17 :                         enc = get_conversion(SOAP_ENC_ARRAY);
    1997             :                 }
    1998             :         }
    1999          23 :         if (!enc) {
    2000           0 :                 enc = get_conversion(IS_NULL);
    2001             :         }
    2002             : 
    2003          23 :         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         132 : 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         132 :         int j = 0;
    2110             :         zval *zdata;
    2111             :         xmlNodePtr xparam;
    2112             : 
    2113         396 :         if (data && Z_TYPE_P(data) == IS_ARRAY) {
    2114         990 :                 ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(data), zdata) {
    2115         286 :                         if (j >= dims[0]) {
    2116           0 :                                 break;
    2117             :                         }
    2118         286 :                         ZVAL_DEREF(zdata);
    2119         286 :                         if (dimension == 1) {
    2120         268 :                                 if (enc == NULL) {
    2121           2 :                                         xparam = master_to_xml(get_conversion(Z_TYPE_P(zdata)), zdata, style, xmlParam);
    2122             :                                 } else {
    2123         266 :                                         xparam = master_to_xml(enc, zdata, style, xmlParam);
    2124             :                                 }
    2125             : 
    2126         268 :                                 if (type) {
    2127          16 :                                         xmlNodeSetName(xparam, BAD_CAST(type->name));
    2128         264 :                                 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
    2129          12 :                                         xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
    2130          12 :                                         xmlSetNs(xparam, ns);
    2131             :                                 } else {
    2132         240 :                                         xmlNodeSetName(xparam, BAD_CAST("item"));
    2133             :                                 }
    2134             :                         } else {
    2135          18 :                                 add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, zdata, style);
    2136             :                         }
    2137         286 :                         j++;
    2138             :                 } ZEND_HASH_FOREACH_END();
    2139             : 
    2140         132 :                 if (dimension == 1) {
    2141         246 :                         while (j < dims[0]) {
    2142           0 :                                 xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2143           0 :                                 xmlAddChild(xmlParam, xparam);
    2144             : 
    2145           0 :                                 if (type) {
    2146           0 :                                         xmlNodeSetName(xparam, BAD_CAST(type->name));
    2147           0 :                                 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
    2148           0 :                                         xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
    2149           0 :                                         xmlSetNs(xparam, ns);
    2150             :                                 } else {
    2151           0 :                                         xmlNodeSetName(xparam, BAD_CAST("item"));
    2152             :                                 }
    2153             : 
    2154           0 :                                 j++;
    2155             :                         }
    2156             :                 } else {
    2157          18 :                         while (j < dims[0]) {
    2158           0 :                                 add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style);
    2159           0 :                                 j++;
    2160             :                         }
    2161             :                 }
    2162             :         } else {
    2163           0 :                 for (j=0; j<dims[0]; j++) {
    2164           0 :                         if (dimension == 1) {
    2165             :                                 xmlNodePtr xparam;
    2166             : 
    2167           0 :                                 xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2168           0 :                                 xmlAddChild(xmlParam, xparam);
    2169           0 :                                 if (type) {
    2170           0 :                                         xmlNodeSetName(xparam, BAD_CAST(type->name));
    2171           0 :                                 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
    2172           0 :                                         xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
    2173           0 :                                         xmlSetNs(xparam, ns);
    2174             :                                 } else {
    2175           0 :                                         xmlNodeSetName(xparam, BAD_CAST("item"));
    2176             :                                 }
    2177             :                         } else {
    2178           0 :                           add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style);
    2179             :                         }
    2180             :                 }
    2181             :         }
    2182         132 : }
    2183             : 
    2184             : static inline int array_num_elements(HashTable* ht)
    2185             : {
    2186             :         if (ht->nNumUsed &&
    2187             :             Z_TYPE(ht->arData[ht->nNumUsed-1].val) != IS_UNDEF &&
    2188             :             ht->arData[ht->nNumUsed-1].key == NULL) {
    2189             :             return ht->arData[ht->nNumUsed-1].h - 1;
    2190             :         }
    2191             :         return 0;
    2192             : }
    2193             : 
    2194         119 : static xmlNodePtr to_xml_array(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2195             : {
    2196         119 :         sdlTypePtr sdl_type = type->sdl_type;
    2197         119 :         sdlTypePtr element_type = NULL;
    2198         119 :         smart_str array_type = {0}, array_size = {0};
    2199             :         int i;
    2200             :         xmlNodePtr xmlParam;
    2201         119 :         encodePtr enc = NULL;
    2202         119 :         int dimension = 1;
    2203             :         int* dims;
    2204             :         int soap_version;
    2205             :         zval array_copy;
    2206             : 
    2207         119 :         ZVAL_UNDEF(&array_copy);
    2208         119 :         soap_version = SOAP_GLOBAL(soap_version);
    2209             : 
    2210         119 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2211         119 :         xmlAddChild(parent, xmlParam);
    2212             : 
    2213         237 :         if (!data || Z_TYPE_P(data) == IS_NULL) {
    2214           5 :                 if (style == SOAP_ENCODED) {
    2215           5 :                         set_xsi_nil(xmlParam);
    2216           5 :                         if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
    2217           0 :                                 set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
    2218             :                         } else {
    2219           5 :                                 set_ns_and_type(xmlParam, type);
    2220             :                         }
    2221             :                 }
    2222           5 :                 return xmlParam;
    2223             :         }
    2224             : 
    2225         114 :         if (Z_TYPE_P(data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(data), zend_ce_traversable)) {
    2226             :                 zend_object_iterator   *iter;
    2227           2 :                 zend_class_entry       *ce = Z_OBJCE_P(data);
    2228             :                 zval                   *val;
    2229             : 
    2230           2 :                 array_init(&array_copy);
    2231             : 
    2232           2 :                 iter = ce->get_iterator(ce, data, 0);
    2233             : 
    2234           2 :                 if (EG(exception)) {
    2235           0 :                         goto iterator_done;
    2236             :                 }
    2237             : 
    2238           2 :                 if (iter->funcs->rewind) {
    2239           2 :                         iter->funcs->rewind(iter);
    2240           2 :                         if (EG(exception)) {
    2241           0 :                                 goto iterator_done;
    2242             :                         }
    2243             :                 }
    2244             : 
    2245          24 :                 while (iter->funcs->valid(iter) == SUCCESS) {
    2246          20 :                         if (EG(exception)) {
    2247           0 :                                 goto iterator_done;
    2248             :                         }
    2249             : 
    2250          20 :                         val = iter->funcs->get_current_data(iter);
    2251          20 :                         if (EG(exception)) {
    2252           0 :                                 goto iterator_done;
    2253             :                         }
    2254          20 :                         if (iter->funcs->get_current_key) {
    2255             :                                 zval key;
    2256          20 :                                 iter->funcs->get_current_key(iter, &key);
    2257          20 :                                 if (EG(exception)) {
    2258           0 :                                         goto iterator_done;
    2259             :                                 }
    2260          20 :                                 array_set_zval_key(Z_ARRVAL(array_copy), &key, val);
    2261          20 :                                 zval_ptr_dtor(val);
    2262             :                                 zval_dtor(&key);
    2263             :                         } else {
    2264           0 :                                 add_next_index_zval(&array_copy, val);
    2265             :                         }
    2266             :                         Z_ADDREF_P(val);
    2267             : 
    2268          20 :                         iter->funcs->move_forward(iter);
    2269          20 :                         if (EG(exception)) {
    2270           0 :                                 goto iterator_done;
    2271             :                         }
    2272             :                 }
    2273             : iterator_done:
    2274           2 :                 OBJ_RELEASE(&iter->std);
    2275           2 :                 if (EG(exception)) {
    2276           0 :                         zval_ptr_dtor(&array_copy);
    2277           0 :                         ZVAL_UNDEF(&array_copy);
    2278             :                 } else {
    2279           2 :                         data = &array_copy;
    2280             :                 }
    2281             :         }
    2282             : 
    2283         114 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    2284             :                 sdlAttributePtr arrayType;
    2285             :                 sdlExtraAttributePtr ext;
    2286             :                 sdlTypePtr elementType;
    2287             : 
    2288         114 :                 i = zend_hash_num_elements(Z_ARRVAL_P(data));
    2289             : 
    2290         489 :                 if (sdl_type &&
    2291          86 :                     sdl_type->attributes &&
    2292          80 :                     (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
    2293             :                       sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType")-1)) != NULL &&
    2294          73 :                     arrayType->extraAttributes &&
    2295          73 :                     (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType")-1)) != NULL) {
    2296             : 
    2297             :                         char *value, *end;
    2298             :                         zval *el;
    2299             : 
    2300          63 :                         value = estrdup(ext->val);
    2301          63 :                         end = strrchr(value,'[');
    2302          63 :                         if (end) {
    2303          63 :                                 *end = '\0';
    2304          63 :                                 end++;
    2305          63 :                                 dimension = calc_dimension(end);
    2306             :                         }
    2307          63 :                         if (ext->ns != NULL) {
    2308          63 :                                 enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, value);
    2309          63 :                                 get_type_str(xmlParam, ext->ns, value, &array_type);
    2310             :                         } else {
    2311           0 :                                 smart_str_appends(&array_type, value);
    2312             :                         }
    2313             : 
    2314          63 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2315          63 :                         dims[0] = i;
    2316          63 :                         el = data;
    2317          68 :                         for (i = 1; i < dimension; i++) {
    2318          15 :                                 if (el != NULL && Z_TYPE_P(el) == IS_ARRAY &&
    2319           5 :                                     zend_hash_num_elements(Z_ARRVAL_P(el)) > 0) {
    2320          20 :                                     ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(el), el) {
    2321           5 :                                         break;
    2322             :                                     } ZEND_HASH_FOREACH_END();
    2323           5 :                                         if (Z_TYPE_P(el) == IS_ARRAY) {
    2324           5 :                                                 dims[i] = zend_hash_num_elements(Z_ARRVAL_P(el));
    2325             :                                         } else {
    2326           0 :                                                 dims[i] = 0;
    2327             :                                         }
    2328             :                                 }
    2329             :                         }
    2330             : 
    2331          63 :                         smart_str_append_long(&array_size, dims[0]);
    2332          68 :                         for (i=1; i<dimension; i++) {
    2333             :                                 smart_str_appendc(&array_size, ',');
    2334           5 :                                 smart_str_append_long(&array_size, dims[i]);
    2335             :                         }
    2336             : 
    2337          63 :                         efree(value);
    2338             : 
    2339         106 :                 } else if (sdl_type &&
    2340          23 :                            sdl_type->attributes &&
    2341          17 :                            (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
    2342             :                              sizeof(SOAP_1_2_ENC_NAMESPACE":itemType")-1)) != NULL &&
    2343           5 :                            arrayType->extraAttributes &&
    2344           5 :                            (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType")-1)) != NULL) {
    2345           5 :                         if (ext->ns != NULL) {
    2346           5 :                                 enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, ext->val);
    2347           5 :                                 get_type_str(xmlParam, ext->ns, ext->val, &array_type);
    2348             :                         } else {
    2349           0 :                                 smart_str_appends(&array_type, ext->val);
    2350             :                         }
    2351          25 :                         if ((arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2352             :                                            sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
    2353           5 :                             arrayType->extraAttributes &&
    2354           5 :                             (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize")-1)) != NULL) {
    2355           5 :                                 dimension = calc_dimension_12(ext->val);
    2356           5 :                                 dims = get_position_12(dimension, ext->val);
    2357           5 :                                 if (dims[0] == 0) {dims[0] = i;}
    2358             : 
    2359           5 :                                 smart_str_append_long(&array_size, dims[0]);
    2360           7 :                                 for (i=1; i<dimension; i++) {
    2361             :                                         smart_str_appendc(&array_size, ',');
    2362           2 :                                         smart_str_append_long(&array_size, dims[i]);
    2363             :                                 }
    2364             :                         } else {
    2365           0 :                                 dims = emalloc(sizeof(int));
    2366           0 :                                 *dims = 0;
    2367           0 :                                 smart_str_append_long(&array_size, i);
    2368             :                         }
    2369          82 :                 } else if (sdl_type &&
    2370          18 :                            sdl_type->attributes &&
    2371          12 :                            (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2372             :                              sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
    2373           2 :                            arrayType->extraAttributes &&
    2374           2 :                            (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraySize")-1)) != NULL) {
    2375           2 :                         dimension = calc_dimension_12(ext->val);
    2376           2 :                         dims = get_position_12(dimension, ext->val);
    2377           2 :                         if (dims[0] == 0) {dims[0] = i;}
    2378             : 
    2379           2 :                         smart_str_append_long(&array_size, dims[0]);
    2380           4 :                         for (i=1; i<dimension; i++) {
    2381             :                                 smart_str_appendc(&array_size, ',');
    2382           2 :                                 smart_str_append_long(&array_size, dims[i]);
    2383             :                         }
    2384             : 
    2385          12 :                         if (sdl_type && sdl_type->elements &&
    2386           2 :                             zend_hash_num_elements(sdl_type->elements) == 1 &&
    2387           2 :                             (zend_hash_internal_pointer_reset(sdl_type->elements),
    2388           2 :                              (elementType = zend_hash_get_current_data_ptr(sdl_type->elements)) != NULL) &&
    2389           4 :                              elementType->encode && elementType->encode->details.type_str) {
    2390           2 :                                 element_type = elementType;
    2391           2 :                                 enc = elementType->encode;
    2392           2 :                                 get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
    2393             :                         } else {
    2394           0 :                                 enc = get_array_type(xmlParam, data, &array_type);
    2395             :                         }
    2396          74 :                 } else if (sdl_type && sdl_type->elements &&
    2397           6 :                            zend_hash_num_elements(sdl_type->elements) == 1 &&
    2398           6 :                            (zend_hash_internal_pointer_reset(sdl_type->elements),
    2399           6 :                             (elementType = zend_hash_get_current_data_ptr(sdl_type->elements)) != NULL) &&
    2400          12 :                            elementType->encode && elementType->encode->details.type_str) {
    2401             : 
    2402           6 :                         element_type = elementType;
    2403           6 :                         enc = elementType->encode;
    2404           6 :                         get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
    2405             : 
    2406           6 :                         smart_str_append_long(&array_size, i);
    2407             : 
    2408           6 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2409           6 :                         dims[0] = i;
    2410             :                 } else {
    2411             : 
    2412          38 :                         enc = get_array_type(xmlParam, data, &array_type);
    2413          38 :                         smart_str_append_long(&array_size, i);
    2414          38 :                         dims = safe_emalloc(sizeof(int), dimension, 0);
    2415          38 :                         dims[0] = i;
    2416             :                 }
    2417             : 
    2418         114 :                 if (style == SOAP_ENCODED) {
    2419         105 :                         if (soap_version == SOAP_1_1) {
    2420             :                                 smart_str_0(&array_type);
    2421          98 :                                 if (strcmp(ZSTR_VAL(array_type.s),"xsd:anyType") == 0) {
    2422             :                                         smart_str_free(&array_type);
    2423             :                                         smart_str_appendl(&array_type,"xsd:ur-type",sizeof("xsd:ur-type")-1);
    2424             :                                 }
    2425             :                                 smart_str_appendc(&array_type, '[');
    2426             :                                 smart_str_append_smart_str(&array_type, &array_size);
    2427             :                                 smart_str_appendc(&array_type, ']');
    2428             :                                 smart_str_0(&array_type);
    2429          98 :                                 set_ns_prop(xmlParam, SOAP_1_1_ENC_NAMESPACE, "arrayType", ZSTR_VAL(array_type.s));
    2430             :                         } else {
    2431           7 :                                 size_t i = 0;
    2432          21 :                                 while (i < ZSTR_LEN(array_size.s)) {
    2433           7 :                                         if (ZSTR_VAL(array_size.s)[i] == ',') {ZSTR_VAL(array_size.s)[i] = ' ';}
    2434           7 :                                         ++i;
    2435             :                                 }
    2436             :                                 smart_str_0(&array_type);
    2437             :                                 smart_str_0(&array_size);
    2438           7 :                                 set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "itemType", ZSTR_VAL(array_type.s));
    2439           7 :                                 set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "arraySize", ZSTR_VAL(array_size.s));
    2440             :                         }
    2441             :                 }
    2442             :                 smart_str_free(&array_type);
    2443             :                 smart_str_free(&array_size);
    2444             : 
    2445         114 :                 add_xml_array_elements(xmlParam, element_type, enc, enc?encode_add_ns(xmlParam,enc->details.ns):NULL, dimension, dims, data, style);
    2446         114 :                 efree(dims);
    2447             :         }
    2448         114 :         if (style == SOAP_ENCODED) {
    2449         105 :                 if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
    2450           4 :                         set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
    2451             :                 } else {
    2452         101 :                         set_ns_and_type(xmlParam, type);
    2453             :                 }
    2454             :         }
    2455             : 
    2456         114 :         zval_ptr_dtor(&array_copy);
    2457             : 
    2458         114 :         return xmlParam;
    2459             : }
    2460             : 
    2461         686 : static zval *to_zval_array(zval *ret, encodeTypePtr type, xmlNodePtr data)
    2462             : {
    2463             :         xmlNodePtr trav;
    2464         686 :         encodePtr enc = NULL;
    2465         686 :         int dimension = 1;
    2466         686 :         int* dims = NULL;
    2467         686 :         int* pos = NULL;
    2468             :         xmlAttrPtr attr;
    2469             :         sdlAttributePtr arrayType;
    2470             :         sdlExtraAttributePtr ext;
    2471             :         sdlTypePtr elementType;
    2472             : 
    2473         686 :         ZVAL_NULL(ret);
    2474         686 :         FIND_XML_NULL(data, ret);
    2475             : 
    2476        3364 :         if (data &&
    2477         683 :             (attr = get_attribute(data->properties,"arrayType")) &&
    2478        1332 :             attr->children && attr->children->content) {
    2479             :                 char *type, *end, *ns;
    2480             :                 xmlNsPtr nsptr;
    2481             : 
    2482         666 :                 parse_namespace(attr->children->content, &type, &ns);
    2483         666 :                 nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
    2484             : 
    2485         666 :                 end = strrchr(type,'[');
    2486         666 :                 if (end) {
    2487         666 :                         *end = '\0';
    2488         666 :                         dimension = calc_dimension(end+1);
    2489         666 :                         dims = get_position(dimension, end+1);
    2490             :                 }
    2491         666 :                 if (nsptr != NULL) {
    2492         666 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
    2493             :                 }
    2494         666 :                 efree(type);
    2495         666 :                 if (ns) {efree(ns);}
    2496             : 
    2497          46 :         } else if ((attr = get_attribute(data->properties,"itemType")) &&
    2498          10 :             attr->children &&
    2499          10 :             attr->children->content) {
    2500             :                 char *type, *ns;
    2501             :                 xmlNsPtr nsptr;
    2502             : 
    2503          10 :                 parse_namespace(attr->children->content, &type, &ns);
    2504          10 :                 nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
    2505          10 :                 if (nsptr != NULL) {
    2506          10 :                         enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
    2507             :                 }
    2508          10 :                 efree(type);
    2509          10 :                 if (ns) {efree(ns);}
    2510             : 
    2511          36 :                 if ((attr = get_attribute(data->properties,"arraySize")) &&
    2512          18 :                     attr->children && attr->children->content) {
    2513           9 :                         dimension = calc_dimension_12((char*)attr->children->content);
    2514           8 :                         dims = get_position_12(dimension, (char*)attr->children->content);
    2515             :                 } else {
    2516           1 :                         dims = emalloc(sizeof(int));
    2517           1 :                         *dims = 0;
    2518             :                 }
    2519             : 
    2520          10 :         } else if ((attr = get_attribute(data->properties,"arraySize")) &&
    2521           2 :             attr->children && attr->children->content) {
    2522             : 
    2523           1 :                 dimension = calc_dimension_12((char*)attr->children->content);
    2524           1 :                 dims = get_position_12(dimension, (char*)attr->children->content);
    2525             : 
    2526          19 :         } else if (type->sdl_type != NULL &&
    2527           6 :                    type->sdl_type->attributes != NULL &&
    2528           4 :                    (arrayType = zend_hash_str_find_ptr(type->sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
    2529             :                                   sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType")-1)) != NULL &&
    2530           1 :                    arrayType->extraAttributes &&
    2531           1 :                    (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType")-1)) != NULL) {
    2532             :                 char *type, *end;
    2533             : 
    2534           1 :                 type = estrdup(ext->val);
    2535           1 :                 end = strrchr(type,'[');
    2536           1 :                 if (end) {
    2537           1 :                         *end = '\0';
    2538             :                 }
    2539           1 :                 if (ext->ns != NULL) {
    2540           1 :                         enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, type);
    2541             :                 }
    2542           1 :                 efree(type);
    2543             : 
    2544           1 :                 dims = emalloc(sizeof(int));
    2545           1 :                 *dims = 0;
    2546             : 
    2547          19 :         } else if (type->sdl_type != NULL &&
    2548           5 :                    type->sdl_type->attributes != NULL &&
    2549           3 :                    (arrayType = zend_hash_str_find_ptr(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
    2550             :                                   sizeof(SOAP_1_2_ENC_NAMESPACE":itemType")-1)) != NULL &&
    2551           2 :                    arrayType->extraAttributes &&
    2552           2 :                    (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType")-1)) != NULL) {
    2553             : 
    2554           2 :                 if (ext->ns != NULL) {
    2555           2 :                         enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, ext->val);
    2556             :                 }
    2557             : 
    2558          10 :                 if ((arrayType = zend_hash_str_find_ptr(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2559             :                                    sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
    2560           2 :                     arrayType->extraAttributes &&
    2561           2 :                     (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize")-1)) != NULL) {
    2562           2 :                         dimension = calc_dimension_12(ext->val);
    2563           2 :                         dims = get_position_12(dimension, ext->val);
    2564             :                 } else {
    2565           0 :                         dims = emalloc(sizeof(int));
    2566           0 :                         *dims = 0;
    2567             :                 }
    2568          10 :         } else if (type->sdl_type != NULL &&
    2569           3 :                    type->sdl_type->attributes != NULL &&
    2570           1 :                    (arrayType = zend_hash_str_find_ptr(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
    2571             :                                   sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
    2572           1 :                    arrayType->extraAttributes &&
    2573           1 :                    (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize")-1)) != NULL) {
    2574             : 
    2575           1 :                 dimension = calc_dimension_12(ext->val);
    2576           1 :                 dims = get_position_12(dimension, ext->val);
    2577           4 :                 if (type->sdl_type && type->sdl_type->elements &&
    2578           1 :                     zend_hash_num_elements(type->sdl_type->elements) == 1 &&
    2579           1 :                     (zend_hash_internal_pointer_reset(type->sdl_type->elements),
    2580           1 :                      (elementType = zend_hash_get_current_data_ptr(type->sdl_type->elements)) != NULL) &&
    2581           1 :                     elementType->encode) {
    2582           1 :                         enc = elementType->encode;
    2583             :                 }
    2584           8 :         } else if (type->sdl_type && type->sdl_type->elements &&
    2585           2 :                    zend_hash_num_elements(type->sdl_type->elements) == 1 &&
    2586           2 :                    (zend_hash_internal_pointer_reset(type->sdl_type->elements),
    2587           2 :                     (elementType = zend_hash_get_current_data_ptr(type->sdl_type->elements)) != NULL) &&
    2588           2 :                    elementType->encode) {
    2589           2 :                 enc = elementType->encode;
    2590             :         }
    2591         682 :         if (dims == NULL) {
    2592           2 :                 dimension = 1;
    2593           2 :                 dims = emalloc(sizeof(int));
    2594           2 :                 *dims = 0;
    2595             :         }
    2596         682 :         pos = safe_emalloc(sizeof(int), dimension, 0);
    2597         682 :         memset(pos,0,sizeof(int)*dimension);
    2598        1364 :         if (data &&
    2599         682 :             (attr = get_attribute(data->properties,"offset")) &&
    2600           0 :              attr->children && attr->children->content) {
    2601           0 :                 char* tmp = strrchr((char*)attr->children->content,'[');
    2602             : 
    2603           0 :                 if (tmp == NULL) {
    2604           0 :                         tmp = (char*)attr->children->content;
    2605             :                 }
    2606           0 :                 get_position_ex(dimension, tmp, &pos);
    2607             :         }
    2608             : 
    2609         682 :         array_init(ret);
    2610         682 :         trav = data->children;
    2611        2219 :         while (trav) {
    2612         858 :                 if (trav->type == XML_ELEMENT_NODE) {
    2613             :                         int i;
    2614             :                         zval tmpVal, *ar;
    2615         858 :                         xmlAttrPtr position = get_attribute(trav->properties,"position");
    2616             : 
    2617         858 :                         ZVAL_NULL(&tmpVal);
    2618         858 :                         master_to_zval(&tmpVal, enc, trav);
    2619         855 :                         if (position != NULL && position->children && position->children->content) {
    2620           0 :                                 char* tmp = strrchr((char*)position->children->content, '[');
    2621           0 :                                 if (tmp == NULL) {
    2622           0 :                                         tmp = (char*)position->children->content;
    2623             :                                 }
    2624           0 :                                 get_position_ex(dimension, tmp, &pos);
    2625             :                         }
    2626             : 
    2627             :                         /* Get/Create intermediate arrays for multidimensional arrays */
    2628         855 :                         i = 0;
    2629         855 :                         ar = ret;
    2630        1726 :                         while (i < dimension-1) {
    2631             :                                 zval* ar2;
    2632          16 :                                 if ((ar2 = zend_hash_index_find(Z_ARRVAL_P(ar), pos[i])) != NULL) {
    2633           4 :                                         ar = ar2;
    2634             :                                 } else {
    2635             :                                         zval tmpAr;
    2636          12 :                                         array_init(&tmpAr);
    2637          12 :                                         ar = zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpAr);
    2638             :                                 }
    2639          16 :                                 i++;
    2640             :                         }
    2641         855 :                         zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpVal);
    2642             : 
    2643             :                         /* Increment position */
    2644         855 :                         i = dimension;
    2645        2405 :                         while (i > 0) {
    2646         867 :                           i--;
    2647         867 :                           pos[i]++;
    2648         867 :                                 if (pos[i] >= dims[i]) {
    2649         695 :                                         if (i > 0) {
    2650          12 :                                                 pos[i] = 0;
    2651             :                                         } else {
    2652             :                                                 /* TODO: Array index overflow */
    2653             :                                         }
    2654             :                                 } else {
    2655         172 :                                   break;
    2656             :                                 }
    2657             :                         }
    2658             :                 }
    2659         855 :                 trav = trav->next;
    2660             :         }
    2661         679 :         efree(dims);
    2662         679 :         efree(pos);
    2663         679 :         return ret;
    2664             : }
    2665             : 
    2666             : /* Map encode/decode */
    2667           8 : static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2668             : {
    2669             :         zval *temp_data;
    2670             :         zend_string *key_val;
    2671             :         zend_ulong int_val;
    2672             :         xmlNodePtr xmlParam;
    2673             :         xmlNodePtr xparam, item;
    2674             :         xmlNodePtr key;
    2675             : 
    2676           8 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2677           8 :         xmlAddChild(parent, xmlParam);
    2678          16 :         FIND_ZVAL_NULL(data, xmlParam, style);
    2679             : 
    2680           8 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    2681          56 :                 ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(data), int_val, key_val, temp_data) {
    2682          16 :                         item = xmlNewNode(NULL, BAD_CAST("item"));
    2683          16 :                         xmlAddChild(xmlParam, item);
    2684          16 :                         key = xmlNewNode(NULL, BAD_CAST("key"));
    2685          16 :                         xmlAddChild(item,key);
    2686          16 :                         if (key_val) {
    2687          10 :                                 if (style == SOAP_ENCODED) {
    2688          10 :                                         set_xsi_type(key, "xsd:string");
    2689             :                                 }
    2690          10 :                                 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          16 :                         ZVAL_DEREF(temp_data);
    2705          16 :                         xparam = master_to_xml(get_conversion(Z_TYPE_P(temp_data)), temp_data, style, item);
    2706          16 :                         xmlNodeSetName(xparam, BAD_CAST("value"));
    2707             :                 } ZEND_HASH_FOREACH_END();
    2708             :         }
    2709           8 :         if (style == SOAP_ENCODED) {
    2710           8 :                 set_ns_and_type(xmlParam, type);
    2711             :         }
    2712             : 
    2713           8 :         return xmlParam;
    2714             : }
    2715             : 
    2716         104 : static zval *to_zval_map(zval *ret, encodeTypePtr type, xmlNodePtr data)
    2717             : {
    2718             :         zval key, value;
    2719             :         xmlNodePtr trav, item, xmlKey, xmlValue;
    2720             : 
    2721         104 :         ZVAL_NULL(ret);
    2722         104 :         FIND_XML_NULL(data, ret);
    2723             : 
    2724         208 :         if (data && data->children) {
    2725         104 :                 array_init(ret);
    2726         104 :                 trav = data->children;
    2727             : 
    2728         104 :                 trav = data->children;
    2729         614 :                 FOREACHNODE(trav, "item", item) {
    2730         614 :                         xmlKey = get_node(item->children, "key");
    2731         614 :                         if (!xmlKey) {
    2732           0 :                                 soap_error0(E_ERROR,  "Encoding: Can't decode apache map, missing key");
    2733             :                         }
    2734             : 
    2735         614 :                         xmlValue = get_node(item->children, "value");
    2736         614 :                         if (!xmlKey) {
    2737           0 :                                 soap_error0(E_ERROR,  "Encoding: Can't decode apache map, missing value");
    2738             :                         }
    2739             : 
    2740         614 :                         ZVAL_NULL(&key);
    2741         614 :                         master_to_zval(&key, NULL, xmlKey);
    2742         614 :                         ZVAL_NULL(&value);
    2743         614 :                         master_to_zval(&value, NULL, xmlValue);
    2744             : 
    2745         614 :                         if (Z_TYPE(key) == IS_STRING) {
    2746         611 :                                 zend_symtable_update(Z_ARRVAL_P(ret), Z_STR(key), &value);
    2747           3 :                         } else if (Z_TYPE(key) == IS_LONG) {
    2748           3 :                                 zend_hash_index_update(Z_ARRVAL_P(ret), Z_LVAL(key), &value);
    2749             :                         } else {
    2750           0 :                                 soap_error0(E_ERROR,  "Encoding: Can't decode apache map, only Strings or Longs are allowd as keys");
    2751             :                         }
    2752         614 :                         zval_ptr_dtor(&key);
    2753             :                 }
    2754         614 :                 ENDFOREACH(trav);
    2755             :         } else {
    2756           0 :                 ZVAL_NULL(ret);
    2757             :         }
    2758         104 :         return ret;
    2759             : }
    2760             : 
    2761             : /* Unknown encode/decode */
    2762         137 : static xmlNodePtr guess_xml_convert(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2763             : {
    2764             :         encodePtr  enc;
    2765             :         xmlNodePtr ret;
    2766             : 
    2767         137 :         if (data) {
    2768         137 :                 enc = get_conversion(Z_TYPE_P(data));
    2769             :         } else {
    2770           0 :                 enc = get_conversion(IS_NULL);
    2771             :         }
    2772         137 :         ret = master_to_xml_int(enc, data, style, parent, 0);
    2773             : /*
    2774             :         if (style == SOAP_LITERAL && SOAP_GLOBAL(sdl)) {
    2775             :                 set_ns_and_type(ret, &enc->details);
    2776             :         }
    2777             : */
    2778         136 :         return ret;
    2779             : }
    2780             : 
    2781        1361 : static zval *guess_zval_convert(zval *ret, encodeTypePtr type, xmlNodePtr data)
    2782             : {
    2783        1361 :         encodePtr enc = NULL;
    2784             :         xmlAttrPtr tmpattr;
    2785        1361 :         xmlChar *type_name = NULL;
    2786             : 
    2787        1361 :         data = check_and_resolve_href(data);
    2788             : 
    2789        1361 :         if (data == NULL) {
    2790           0 :                 enc = get_conversion(IS_NULL);
    2791        1362 :         } else if (data->properties && get_attribute_ex(data->properties, "nil", XSI_NAMESPACE)) {
    2792           1 :                 enc = get_conversion(IS_NULL);
    2793             :         } else {
    2794        1360 :                 tmpattr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
    2795        1360 :                 if (tmpattr != NULL) {
    2796        1329 :                   type_name = tmpattr->children->content;
    2797        1329 :                         enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, tmpattr->children->content);
    2798        1329 :                         if (enc && type == &enc->details) {
    2799           0 :                                 enc = NULL;
    2800             :                         }
    2801        1329 :                         if (enc != NULL) {
    2802        1328 :                           encodePtr tmp = enc;
    2803        3991 :                           while (tmp &&
    2804        1329 :                                  tmp->details.sdl_type != NULL &&
    2805           5 :                                  tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
    2806           4 :                             if (enc == tmp->details.sdl_type->encode ||
    2807           2 :                                 tmp == tmp->details.sdl_type->encode) {
    2808           1 :                                 enc = NULL;
    2809           1 :                                 break;
    2810             :                             }
    2811           1 :                             tmp = tmp->details.sdl_type->encode;
    2812             :                           }
    2813             :                         }
    2814             :                 }
    2815             : 
    2816        1360 :                 if (enc == NULL) {
    2817             :                         /* Didn't have a type, totally guess here */
    2818             :                         /* Logic: has children = IS_OBJECT else IS_STRING */
    2819             :                         xmlNodePtr trav;
    2820             : 
    2821          98 :                         if (get_attribute(data->properties, "arrayType") ||
    2822          32 :                             get_attribute(data->properties, "itemType") ||
    2823          32 :                             get_attribute(data->properties, "arraySize")) {
    2824           1 :                                 enc = get_conversion(SOAP_ENC_ARRAY);
    2825             :                         } else {
    2826          32 :                                 enc = get_conversion(XSD_STRING);
    2827          32 :                                 trav = data->children;
    2828          87 :                                 while (trav != NULL) {
    2829          32 :                                         if (trav->type == XML_ELEMENT_NODE) {
    2830           9 :                                                 enc = get_conversion(SOAP_ENC_OBJECT);
    2831           9 :                                                 break;
    2832             :                                         }
    2833          23 :                                         trav = trav->next;
    2834             :                                 }
    2835             :                         }
    2836             :                 }
    2837             :         }
    2838        1361 :         master_to_zval_int(ret, enc, data);
    2839        1361 :         if (SOAP_GLOBAL(sdl) && type_name && enc->details.sdl_type) {
    2840             :                 zval soapvar;
    2841             :                 char *ns, *cptype;
    2842             :                 xmlNsPtr nsptr;
    2843             : 
    2844           3 :                 object_init_ex(&soapvar, soap_var_class_entry);
    2845           3 :                 add_property_long(&soapvar, "enc_type", enc->details.type);
    2846             :                 Z_DELREF_P(ret);
    2847           3 :                 add_property_zval(&soapvar, "enc_value", ret);
    2848           3 :                 parse_namespace(type_name, &cptype, &ns);
    2849           3 :                 nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
    2850           3 :                 add_property_string(&soapvar, "enc_stype", cptype);
    2851           3 :                 if (nsptr) {
    2852           3 :                         add_property_string(&soapvar, "enc_ns", (char*)nsptr->href);
    2853             :                 }
    2854           3 :                 efree(cptype);
    2855           3 :                 if (ns) {efree(ns);}
    2856           3 :                 ZVAL_COPY_VALUE(ret, &soapvar);
    2857             :         }
    2858        1361 :         return ret;
    2859             : }
    2860             : 
    2861             : /* Time encode/decode */
    2862          23 : static xmlNodePtr to_xml_datetime_ex(encodeTypePtr type, zval *data, char *format, int style, xmlNodePtr parent)
    2863             : {
    2864             :         /* logic hacked from ext/standard/datetime.c */
    2865             :         struct tm *ta, tmbuf;
    2866             :         time_t timestamp;
    2867          23 :         int max_reallocs = 5;
    2868          23 :         size_t buf_len=64, real_len;
    2869             :         char *buf;
    2870             :         char tzbuf[8];
    2871             : 
    2872             :         xmlNodePtr xmlParam;
    2873             : 
    2874          23 :         xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2875          23 :         xmlAddChild(parent, xmlParam);
    2876          46 :         FIND_ZVAL_NULL(data, xmlParam, style);
    2877             : 
    2878          23 :         if (Z_TYPE_P(data) == IS_LONG) {
    2879           8 :                 timestamp = Z_LVAL_P(data);
    2880           8 :                 ta = php_localtime_r(&timestamp, &tmbuf);
    2881             :                 /*ta = php_gmtime_r(&timestamp, &tmbuf);*/
    2882           8 :                 if (!ta) {
    2883           0 :                         soap_error1(E_ERROR, "Encoding: Invalid timestamp " ZEND_LONG_FMT, Z_LVAL_P(data));
    2884             :                 }
    2885             : 
    2886           8 :                 buf = (char *) emalloc(buf_len);
    2887          16 :                 while ((real_len = strftime(buf, buf_len, format, ta)) == buf_len || real_len == 0) {
    2888           0 :                         buf_len *= 2;
    2889           0 :                         buf = (char *) erealloc(buf, buf_len);
    2890           0 :                         if (!--max_reallocs) break;
    2891             :                 }
    2892             : 
    2893             :                 /* Time zone support */
    2894             : #ifdef HAVE_TM_GMTOFF
    2895           8 :                 snprintf(tzbuf, sizeof(tzbuf), "%c%02d:%02d", (ta->tm_gmtoff < 0) ? '-' : '+', abs(ta->tm_gmtoff / 3600), abs( (ta->tm_gmtoff % 3600) / 60 ));
    2896             : #else
    2897             : # if defined(__CYGWIN__) || defined(NETWARE) || (defined(PHP_WIN32) && defined(_MSC_VER) && _MSC_VER >= 1900)
    2898             :                 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));
    2899             : # else
    2900             :                 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));
    2901             : # endif
    2902             : #endif
    2903           8 :                 if (strcmp(tzbuf,"+00:00") == 0) {
    2904           8 :                   strcpy(tzbuf,"Z");
    2905           8 :                   real_len++;
    2906             :                 } else {
    2907           0 :                         real_len += 6;
    2908             :                 }
    2909           8 :                 if (real_len >= buf_len) {
    2910           0 :                         buf = (char *) erealloc(buf, real_len+1);
    2911             :                 }
    2912           8 :                 strcat(buf, tzbuf);
    2913             : 
    2914           8 :                 xmlNodeSetContent(xmlParam, BAD_CAST(buf));
    2915           8 :                 efree(buf);
    2916          15 :         } else if (Z_TYPE_P(data) == IS_STRING) {
    2917          15 :                 xmlNodeSetContentLen(xmlParam, BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
    2918             :         }
    2919             : 
    2920          23 :         if (style == SOAP_ENCODED) {
    2921          14 :                 set_ns_and_type(xmlParam, type);
    2922             :         }
    2923          23 :         return xmlParam;
    2924             : }
    2925             : 
    2926           0 : static xmlNodePtr to_xml_duration(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2927             : {
    2928             :         /* TODO: '-'?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T([0-9]+H)?([0-9]+M)?([0-9]+S)? */
    2929           0 :         return to_xml_string(type, data, style, parent);
    2930             : }
    2931             : 
    2932          16 : static xmlNodePtr to_xml_datetime(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2933             : {
    2934          16 :         return to_xml_datetime_ex(type, data, "%Y-%m-%dT%H:%M:%S", style, parent);
    2935             : }
    2936             : 
    2937           1 : static xmlNodePtr to_xml_time(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2938             : {
    2939             :         /* TODO: microsecconds */
    2940           1 :         return to_xml_datetime_ex(type, data, "%H:%M:%S", style, parent);
    2941             : }
    2942             : 
    2943           1 : static xmlNodePtr to_xml_date(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2944             : {
    2945           1 :         return to_xml_datetime_ex(type, data, "%Y-%m-%d", style, parent);
    2946             : }
    2947             : 
    2948           1 : static xmlNodePtr to_xml_gyearmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2949             : {
    2950           1 :         return to_xml_datetime_ex(type, data, "%Y-%m", style, parent);
    2951             : }
    2952             : 
    2953           1 : static xmlNodePtr to_xml_gyear(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2954             : {
    2955           1 :         return to_xml_datetime_ex(type, data, "%Y", style, parent);
    2956             : }
    2957             : 
    2958           1 : static xmlNodePtr to_xml_gmonthday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2959             : {
    2960           1 :         return to_xml_datetime_ex(type, data, "--%m-%d", style, parent);
    2961             : }
    2962             : 
    2963           1 : static xmlNodePtr to_xml_gday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2964             : {
    2965           1 :         return to_xml_datetime_ex(type, data, "---%d", style, parent);
    2966             : }
    2967             : 
    2968           1 : static xmlNodePtr to_xml_gmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    2969             : {
    2970           1 :         return to_xml_datetime_ex(type, data, "--%m--", style, parent);
    2971             : }
    2972             : 
    2973          16 : static zval* to_zval_list(zval *ret, encodeTypePtr enc, xmlNodePtr data) {
    2974             :         /*FIXME*/
    2975          16 :         return to_zval_stringc(ret, enc, data);
    2976             : }
    2977             : 
    2978          23 : static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent) {
    2979             :         xmlNodePtr ret;
    2980          23 :         encodePtr list_enc = NULL;
    2981             : 
    2982          23 :         if (enc->sdl_type && enc->sdl_type->kind == XSD_TYPEKIND_LIST && enc->sdl_type->elements) {
    2983             :                 sdlTypePtr type;
    2984             : 
    2985          18 :                 ZEND_HASH_FOREACH_PTR(enc->sdl_type->elements, type) {
    2986           6 :                         list_enc = type->encode;
    2987           6 :                         break;
    2988             :                 } ZEND_HASH_FOREACH_END();
    2989             :         }
    2990             : 
    2991          23 :         ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
    2992          23 :         xmlAddChild(parent, ret);
    2993          46 :         FIND_ZVAL_NULL(data, ret, style);
    2994          23 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    2995             :                 zval *tmp;
    2996           4 :                 smart_str list = {0};
    2997           4 :                 HashTable *ht = Z_ARRVAL_P(data);
    2998             : 
    2999          22 :                 ZEND_HASH_FOREACH_VAL(ht, tmp) {
    3000           9 :                         xmlNodePtr dummy = master_to_xml(list_enc, tmp, SOAP_LITERAL, ret);
    3001          18 :                         if (dummy && dummy->children && dummy->children->content) {
    3002           9 :                                 if (list.s && ZSTR_LEN(list.s) != 0) {
    3003             :                                         smart_str_appendc(&list, ' ');
    3004             :                                 }
    3005           9 :                                 smart_str_appends(&list, (char*)dummy->children->content);
    3006             :                         } else {
    3007           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    3008             :                         }
    3009           9 :                         xmlUnlinkNode(dummy);
    3010           9 :                         xmlFreeNode(dummy);
    3011             :                 } ZEND_HASH_FOREACH_END();
    3012             :                 smart_str_0(&list);
    3013           4 :                 if (list.s) {
    3014           4 :                         xmlNodeSetContentLen(ret, BAD_CAST(ZSTR_VAL(list.s)), ZSTR_LEN(list.s));
    3015             :                 } else {
    3016           0 :                         xmlNodeSetContentLen(ret, BAD_CAST(""), 0);
    3017             :                 }
    3018             :                 smart_str_free(&list);
    3019             :         } else {
    3020             :                 zval tmp;
    3021             :                 char *str, *start, *next;
    3022          19 :                 smart_str list = {0};
    3023             : 
    3024          19 :                 if (Z_TYPE_P(data) != IS_STRING) {
    3025          10 :                         ZVAL_STR(&tmp, zval_get_string(data));
    3026           5 :                         data = &tmp;
    3027             :                 }
    3028          19 :                 str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
    3029          19 :                 whiteSpace_collapse(BAD_CAST(str));
    3030          19 :                 start = str;
    3031          63 :                 while (start != NULL && *start != '\0') {
    3032             :                         xmlNodePtr dummy;
    3033             :                         zval dummy_zval;
    3034             : 
    3035          25 :                         next = strchr(start,' ');
    3036          25 :                         if (next != NULL) {
    3037           6 :                           *next = '\0';
    3038           6 :                           next++;
    3039             :                         }
    3040          50 :                         ZVAL_STRING(&dummy_zval, start);
    3041          25 :                         dummy = master_to_xml(list_enc, &dummy_zval, SOAP_LITERAL, ret);
    3042          25 :                         zval_ptr_dtor(&dummy_zval);
    3043          50 :                         if (dummy && dummy->children && dummy->children->content) {
    3044          25 :                                 if (list.s && ZSTR_LEN(list.s) != 0) {
    3045             :                                         smart_str_appendc(&list, ' ');
    3046             :                                 }
    3047          25 :                                 smart_str_appends(&list, (char*)dummy->children->content);
    3048             :                         } else {
    3049           0 :                                 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
    3050             :                         }
    3051          25 :                         xmlUnlinkNode(dummy);
    3052          25 :                         xmlFreeNode(dummy);
    3053             : 
    3054          25 :                         start = next;
    3055             :                 }
    3056             :                 smart_str_0(&list);
    3057          19 :                 if (list.s) {
    3058          19 :                         xmlNodeSetContentLen(ret, BAD_CAST(ZSTR_VAL(list.s)), ZSTR_LEN(list.s));
    3059             :                 } else {
    3060           0 :                         xmlNodeSetContentLen(ret, BAD_CAST(""), 0);
    3061             :                 }
    3062             :                 smart_str_free(&list);
    3063          19 :                 efree(str);
    3064          19 :                 if (data == &tmp) {zval_dtor(&tmp);}
    3065             :         }
    3066          23 :         return ret;
    3067             : }
    3068             : 
    3069           0 : static xmlNodePtr to_xml_list1(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent) {
    3070             :         /*FIXME: minLength=1 */
    3071           0 :         return to_xml_list(enc,data,style, parent);
    3072             : }
    3073             : 
    3074          10 : static zval* to_zval_union(zval *ret, encodeTypePtr enc, xmlNodePtr data) {
    3075             :         /*FIXME*/
    3076          10 :         return to_zval_list(ret, enc, data);
    3077             : }
    3078             : 
    3079          17 : static xmlNodePtr to_xml_union(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent) {
    3080             :         /*FIXME*/
    3081          17 :         return to_xml_list(enc,data,style, parent);
    3082             : }
    3083             : 
    3084           4 : static zval *to_zval_any(zval *ret, encodeTypePtr type, xmlNodePtr data)
    3085             : {
    3086             :         xmlBufferPtr buf;
    3087             : 
    3088           4 :         if (SOAP_GLOBAL(sdl) && SOAP_GLOBAL(sdl)->elements && data->name) {
    3089           4 :                 smart_str nscat = {0};
    3090             :                 sdlTypePtr sdl_type;
    3091             : 
    3092           4 :                 if (data->ns && data->ns->href) {
    3093           1 :                         smart_str_appends(&nscat, (char*)data->ns->href);
    3094             :                         smart_str_appendc(&nscat, ':');
    3095             :                 }
    3096           4 :                 smart_str_appends(&nscat, (char*)data->name);
    3097             :                 smart_str_0(&nscat);
    3098             : 
    3099           8 :                 if ((sdl_type = zend_hash_find_ptr(SOAP_GLOBAL(sdl)->elements, nscat.s)) != NULL &&
    3100           0 :                     sdl_type->encode) {
    3101             :                         smart_str_free(&nscat);
    3102           0 :                         return master_to_zval_int(ret, sdl_type->encode, data);
    3103             :                 }
    3104             :                 smart_str_free(&nscat);
    3105             :         }
    3106             : 
    3107           4 :         buf = xmlBufferCreate();
    3108           4 :         xmlNodeDump(buf, NULL, data, 0, 0);
    3109           8 :         ZVAL_STRING(ret, (char*)xmlBufferContent(buf));
    3110           4 :         xmlBufferFree(buf);
    3111           4 :         return ret;
    3112             : }
    3113             : 
    3114           8 : static xmlNodePtr to_xml_any(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
    3115             : {
    3116           8 :         xmlNodePtr ret = NULL;
    3117             : 
    3118           8 :         if (Z_TYPE_P(data) == IS_ARRAY) {
    3119             :                 zval *el;
    3120           1 :                 encodePtr enc = get_conversion(XSD_ANYXML);
    3121             :                 zend_string *name;
    3122             : 
    3123           3 :                 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(data), name, el) {
    3124           1 :                         ret = master_to_xml(enc, el, style, parent);
    3125           2 :                     if (ret &&
    3126           1 :                         ret->name != xmlStringTextNoenc) {
    3127           1 :                                 xmlNodeSetName(ret, BAD_CAST(ZSTR_VAL(name)));
    3128             :                     }
    3129             :                 } ZEND_HASH_FOREACH_END();
    3130           1 :                 return ret;
    3131             :         }
    3132           7 :         if (Z_TYPE_P(data) == IS_STRING) {
    3133           7 :                 ret = xmlNewTextLen(BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
    3134             :         } else {
    3135           0 :                 zend_string *tmp = zval_get_string(data);
    3136           0 :                 ret = xmlNewTextLen(BAD_CAST(ZSTR_VAL(tmp)), ZSTR_LEN(tmp));
    3137             :                 zend_string_release(tmp);
    3138             :         }
    3139             : 
    3140           7 :         ret->name = xmlStringTextNoenc;
    3141           7 :         ret->parent = parent;
    3142           7 :         ret->doc = parent->doc;
    3143           7 :         ret->prev = parent->last;
    3144           7 :         ret->next = NULL;
    3145           7 :         if (parent->last) {
    3146           2 :                 parent->last->next = ret;
    3147             :         } else {
    3148           5 :                 parent->children = ret;
    3149             :         }
    3150           7 :         parent->last = ret;
    3151             : 
    3152           7 :         return ret;
    3153             : }
    3154             : 
    3155         573 : zval *sdl_guess_convert_zval(zval *ret, encodeTypePtr enc, xmlNodePtr data)
    3156             : {
    3157             :         sdlTypePtr type;
    3158             : 
    3159         573 :         type = enc->sdl_type;
    3160         573 :         if (type == NULL) {
    3161           0 :                 return guess_zval_convert(ret, enc, data);
    3162             :         }
    3163             : /*FIXME: restriction support
    3164             :         if (type && type->restrictions &&
    3165             :             data &&  data->children && data->children->content) {
    3166             :                 if (type->restrictions->whiteSpace && type->restrictions->whiteSpace->value) {
    3167             :                         if (strcmp(type->restrictions->whiteSpace->value,"replace") == 0) {
    3168             :                                 whiteSpace_replace(data->children->content);
    3169             :                         } else if (strcmp(type->restrictions->whiteSpace->value,"collapse") == 0) {
    3170             :                                 whiteSpace_collapse(data->children->content);
    3171             :                         }
    3172             :                 }
    3173             :                 if (type->restrictions->enumeration) {
    3174             :                         if (!zend_hash_exists(type->restrictions->enumeration,data->children->content,strlen(data->children->content)+1)) {
    3175             :                                 soap_error1(E_WARNING, "Encoding: Restriction: invalid enumeration value \"%s\"", data->children->content);
    3176             :                         }
    3177             :                 }
    3178             :                 if (type->restrictions->minLength &&
    3179             :                     strlen(data->children->content) < type->restrictions->minLength->value) {
    3180             :                   soap_error0(E_WARNING, "Encoding: Restriction: length less than 'minLength'");
    3181             :                 }
    3182             :                 if (type->restrictions->maxLength &&
    3183             :                     strlen(data->children->content) > type->restrictions->maxLength->value) {
    3184             :                   soap_error0(E_WARNING, "Encoding: Restriction: length greater than 'maxLength'");
    3185             :                 }
    3186             :                 if (type->restrictions->length &&
    3187             :                     strlen(data->children->content) != type->restrictions->length->value) {
    3188             :                   soap_error0(E_WARNING, "Encoding: Restriction: length is not equal to 'length'");
    3189             :                 }
    3190             :         }
    3191             : */
    3192         573 :         switch (type->kind) {
    3193             :                 case XSD_TYPEKIND_SIMPLE:
    3194          37 :                         if (type->encode && enc != &type->encode->details) {
    3195          36 :                                 return master_to_zval_int(ret, type->encode, data);
    3196             :                         } else {
    3197           1 :                                 return guess_zval_convert(ret, enc, data);
    3198             :                         }
    3199             :                         break;
    3200             :                 case XSD_TYPEKIND_LIST:
    3201           6 :                         return to_zval_list(ret, enc, data);
    3202             :                 case XSD_TYPEKIND_UNION:
    3203          10 :                         return to_zval_union(ret, enc, data);
    3204             :                 case XSD_TYPEKIND_COMPLEX:
    3205             :                 case XSD_TYPEKIND_RESTRICTION:
    3206             :                 case XSD_TYPEKIND_EXTENSION:
    3207         688 :                         if (type->encode &&
    3208         111 :                             (type->encode->details.type == IS_ARRAY ||
    3209          57 :                              type->encode->details.type == SOAP_ENC_ARRAY)) {
    3210          54 :                                 return to_zval_array(ret, enc, data);
    3211             :                         }
    3212         466 :                         return to_zval_object(ret, enc, data);
    3213             :                 default:
    3214           0 :                 soap_error0(E_ERROR, "Encoding: Internal Error");
    3215           0 :                         return guess_zval_convert(ret, enc, data);
    3216             :         }
    3217             : }
    3218             : 
    3219         704 : xmlNodePtr sdl_guess_convert_xml(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent)
    3220             : {
    3221             :         sdlTypePtr type;
    3222         704 :         xmlNodePtr ret = NULL;
    3223             : 
    3224         704 :         type = enc->sdl_type;
    3225             : 
    3226         704 :         if (type == NULL) {
    3227           0 :                 ret = guess_xml_convert(enc, data, style, parent);
    3228           0 :                 if (style == SOAP_ENCODED) {
    3229           0 :                         set_ns_and_type(ret, enc);
    3230             :                 }
    3231           0 :                 return ret;
    3232             :         }
    3233             : /*FIXME: restriction support
    3234             :         if (type) {
    3235             :                 if (type->restrictions && Z_TYPE_P(data) == IS_STRING) {
    3236             :                         if (type->restrictions->enumeration) {
    3237             :                                 if (!zend_hash_exists(type->restrictions->enumeration,Z_STRVAL_P(data),Z_STRLEN_P(data)+1)) {
    3238             :                                         soap_error1(E_WARNING, "Encoding: Restriction: invalid enumeration value \"%s\".", Z_STRVAL_P(data));
    3239             :                                 }
    3240             :                         }
    3241             :                         if (type->restrictions->minLength &&
    3242             :                             Z_STRLEN_P(data) < type->restrictions->minLength->value) {
    3243             :                         soap_error0(E_WARNING, "Encoding: Restriction: length less than 'minLength'");
    3244             :                         }
    3245             :                         if (type->restrictions->maxLength &&
    3246             :                             Z_STRLEN_P(data) > type->restrictions->maxLength->value) {
    3247             :                         soap_error0(E_WARNING, "Encoding: Restriction: length greater than 'maxLength'");
    3248             :                         }
    3249             :                         if (type->restrictions->length &&
    3250             :                             Z_STRLEN_P(data) != type->restrictions->length->value) {
    3251             :                         soap_error0(E_WARNING, "Encoding: Restriction: length is not equal to 'length'");
    3252             :                         }
    3253             :                 }
    3254             :         }
    3255             : */
    3256         704 :         switch(type->kind) {
    3257             :                 case XSD_TYPEKIND_SIMPLE:
    3258          88 :                         if (type->encode && enc != &type->encode->details) {
    3259          43 :                                 ret = master_to_xml(type->encode, data, style, parent);
    3260             :                         } else {
    3261           2 :                                 ret = guess_xml_convert(enc, data, style, parent);
    3262             :                         }
    3263          45 :                         break;
    3264             :                 case XSD_TYPEKIND_LIST:
    3265           6 :                         ret = to_xml_list(enc, data, style, parent);
    3266           6 :                         break;
    3267             :                 case XSD_TYPEKIND_UNION:
    3268          17 :                         ret = to_xml_union(enc, data, style, parent);
    3269          17 :                         break;
    3270             :                 case XSD_TYPEKIND_COMPLEX:
    3271             :                 case XSD_TYPEKIND_RESTRICTION:
    3272             :                 case XSD_TYPEKIND_EXTENSION:
    3273         858 :                         if (type->encode &&
    3274         156 :                             (type->encode->details.type == IS_ARRAY ||
    3275          66 :                              type->encode->details.type == SOAP_ENC_ARRAY)) {
    3276          90 :                                 return to_xml_array(enc, data, style, parent);
    3277             :                         } else {
    3278         546 :                                 return to_xml_object(enc, data, style, parent);
    3279             :                         }
    3280             :                         break;
    3281             :                 default:
    3282           0 :                 soap_error0(E_ERROR, "Encoding: Internal Error");
    3283             :                         break;
    3284             :         }
    3285          68 :         if (style == SOAP_ENCODED) {
    3286          33 :                 set_ns_and_type(ret, enc);
    3287             :         }
    3288          68 :         return ret;
    3289             : }
    3290             : 
    3291        6358 : static xmlNodePtr check_and_resolve_href(xmlNodePtr data)
    3292             : {
    3293        6358 :         if (data && data->properties) {
    3294             :                 xmlAttrPtr href;
    3295             : 
    3296        5350 :                 href = data->properties;
    3297             :                 while (1) {
    3298        5352 :                         href = get_attribute(href, "href");
    3299        5352 :                         if (href == NULL || href->ns == NULL) {break;}
    3300           2 :                         href = href->next;
    3301           2 :                 }
    3302        5350 :                 if (href) {
    3303             :                         /*  Internal href try and find node */
    3304         219 :                         if (href->children->content[0] == '#') {
    3305         219 :                                 xmlNodePtr ret = get_node_with_attribute_recursive(data->doc->children, NULL, "id", (char*)&href->children->content[1]);
    3306         219 :                                 if (!ret) {
    3307           0 :                                         soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
    3308             :                                 }
    3309         219 :                                 return ret;
    3310             :                         } else {
    3311             :                                 /*  TODO: External href....? */
    3312           0 :                                 soap_error1(E_ERROR, "Encoding: External reference '%s'", href->children->content);
    3313             :                         }
    3314             :                 }
    3315             :                 /* SOAP 1.2 enc:id enc:ref */
    3316        5131 :                 href = get_attribute_ex(data->properties, "ref", SOAP_1_2_ENC_NAMESPACE);
    3317        5131 :                 if (href) {
    3318             :                         xmlChar* id;
    3319             :                         xmlNodePtr ret;
    3320             : 
    3321           4 :                         if (href->children->content[0] == '#') {
    3322           3 :                                 id = href->children->content+1;
    3323             :                         } else {
    3324           1 :                                 id = href->children->content;
    3325             :                         }
    3326           4 :                         ret = get_node_with_attribute_recursive_ex(data->doc->children, NULL, NULL, "id", (char*)id, SOAP_1_2_ENC_NAMESPACE);
    3327           4 :                         if (!ret) {
    3328           1 :                                 soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
    3329           3 :                         } else if (ret == data) {
    3330           1 :                                 soap_error1(E_ERROR, "Encoding: Violation of id and ref information items '%s'", href->children->content);
    3331             :                         }
    3332           2 :                         return ret;
    3333             :                 }
    3334             :         }
    3335        6135 :         return data;
    3336             : }
    3337             : 
    3338        1405 : static void set_ns_and_type(xmlNodePtr node, encodeTypePtr type)
    3339             : {
    3340        1405 :         set_ns_and_type_ex(node, type->ns, type->type_str);
    3341        1405 : }
    3342             : 
    3343        1431 : static void set_ns_and_type_ex(xmlNodePtr node, char *ns, char *type)
    3344             : {
    3345        1431 :         smart_str nstype = {0};
    3346        1431 :         get_type_str(node, ns, type, &nstype);
    3347        1431 :         set_xsi_type(node, ZSTR_VAL(nstype.s));
    3348             :         smart_str_free(&nstype);
    3349        1431 : }
    3350             : 
    3351           1 : static xmlNsPtr xmlSearchNsPrefixByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
    3352             : {
    3353             :         xmlNsPtr cur;
    3354           1 :         xmlNodePtr orig = node;
    3355             : 
    3356           3 :         while (node) {
    3357           6 :                 if (node->type == XML_ENTITY_REF_NODE ||
    3358           2 :                     node->type == XML_ENTITY_NODE ||
    3359           2 :                     node->type == XML_ENTITY_DECL) {
    3360           0 :                         return NULL;
    3361             :                 }
    3362           2 :                 if (node->type == XML_ELEMENT_NODE) {
    3363           2 :                         cur = node->nsDef;
    3364           6 :                         while (cur != NULL) {
    3365           2 :                                 if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
    3366           0 :                                         if (xmlSearchNs(doc, node, cur->prefix) == cur) {
    3367           0 :                                                 return cur;
    3368             :                                         }
    3369             :                                 }
    3370           2 :                                 cur = cur->next;
    3371             :                         }
    3372           2 :                         if (orig != node) {
    3373           1 :                                 cur = node->ns;
    3374           1 :                                 if (cur != NULL) {
    3375           1 :                                         if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
    3376           1 :                                                 if (xmlSearchNs(doc, node, cur->prefix) == cur) {
    3377           1 :                                                         return cur;
    3378             :                                                 }
    3379             :                                         }
    3380             :                                 }
    3381             :                         }
    3382             :                 }
    3383           1 :                 node = node->parent;
    3384             :         }
    3385           0 :         return NULL;
    3386             : }
    3387             : 
    3388        4776 : xmlNsPtr encode_add_ns(xmlNodePtr node, const char* ns)
    3389             : {
    3390             :         xmlNsPtr xmlns;
    3391             : 
    3392        4776 :         if (ns == NULL) {
    3393         104 :           return NULL;
    3394             :         }
    3395             : 
    3396        4672 :         xmlns = xmlSearchNsByHref(node->doc, node, BAD_CAST(ns));
    3397        4672 :         if (xmlns != NULL && xmlns->prefix == NULL) {
    3398           1 :                 xmlns = xmlSearchNsPrefixByHref(node->doc, node, BAD_CAST(ns));
    3399             :         }
    3400        4672 :         if (xmlns == NULL) {
    3401             :                 xmlChar* prefix;
    3402             : 
    3403        3956 :                 if ((prefix = zend_hash_str_find_ptr(&SOAP_GLOBAL(defEncNs), (char*)ns, strlen(ns))) != NULL) {
    3404        1047 :                         xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), prefix);
    3405             :                 } else {
    3406         931 :                         smart_str prefix = {0};
    3407         931 :                         int num = ++SOAP_GLOBAL(cur_uniq_ns);
    3408             : 
    3409             :                         while (1) {
    3410             :                                 smart_str_appendl(&prefix, "ns", 2);
    3411         931 :                                 smart_str_append_long(&prefix, num);
    3412             :                                 smart_str_0(&prefix);
    3413         931 :                                 if (xmlSearchNs(node->doc, node, BAD_CAST(ZSTR_VAL(prefix.s))) == NULL) {
    3414         931 :                                         break;
    3415             :                                 }
    3416             :                                 smart_str_free(&prefix);
    3417           0 :                                 prefix.s = NULL;
    3418           0 :                                 num = ++SOAP_GLOBAL(cur_uniq_ns);
    3419           0 :                         }
    3420             : 
    3421         931 :                         xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), BAD_CAST(prefix.s ? ZSTR_VAL(prefix.s) : ""));
    3422             :                         smart_str_free(&prefix);
    3423             :                 }
    3424             :         }
    3425        4672 :         return xmlns;
    3426             : }
    3427             : 
    3428        1597 : static void set_ns_prop(xmlNodePtr node, char *ns, char *name, char *val)
    3429             : {
    3430        1597 :         xmlSetNsProp(node, encode_add_ns(node, ns), BAD_CAST(name), BAD_CAST(val));
    3431        1597 : }
    3432             : 
    3433          34 : static void set_xsi_nil(xmlNodePtr node)
    3434             : {
    3435          34 :         set_ns_prop(node, XSI_NAMESPACE, "nil", "true");
    3436          34 : }
    3437             : 
    3438        1447 : static void set_xsi_type(xmlNodePtr node, char *type)
    3439             : {
    3440        1447 :         set_ns_prop(node, XSI_NAMESPACE, "type", type);
    3441        1447 : }
    3442             : 
    3443        1431 : void encode_reset_ns()
    3444             : {
    3445        1431 :         SOAP_GLOBAL(cur_uniq_ns) = 0;
    3446        1431 :         SOAP_GLOBAL(cur_uniq_ref) = 0;
    3447        1431 :         if (SOAP_GLOBAL(ref_map)) {
    3448          20 :                 zend_hash_destroy(SOAP_GLOBAL(ref_map));
    3449             :         } else {
    3450        1411 :                 SOAP_GLOBAL(ref_map) = emalloc(sizeof(HashTable));
    3451             :         }
    3452        1431 :         zend_hash_init(SOAP_GLOBAL(ref_map), 0, NULL, NULL, 0);
    3453        1431 : }
    3454             : 
    3455        1410 : void encode_finish()
    3456             : {
    3457        1410 :         SOAP_GLOBAL(cur_uniq_ns) = 0;
    3458        1410 :         SOAP_GLOBAL(cur_uniq_ref) = 0;
    3459        1410 :         if (SOAP_GLOBAL(ref_map)) {
    3460        1410 :                 zend_hash_destroy(SOAP_GLOBAL(ref_map));
    3461        1410 :                 efree(SOAP_GLOBAL(ref_map));
    3462        1410 :                 SOAP_GLOBAL(ref_map) = NULL;
    3463             :         }
    3464        1410 : }
    3465             : 
    3466        2058 : encodePtr get_conversion(int encode)
    3467             : {
    3468             :         encodePtr enc;
    3469             : 
    3470        4116 :         if ((enc = zend_hash_index_find_ptr(&SOAP_GLOBAL(defEncIndex), encode)) == NULL) {
    3471           0 :                 soap_error0(E_ERROR,  "Encoding: Cannot find encoding");
    3472           0 :                 return NULL;
    3473             :         } else {
    3474        2058 :                 return enc;
    3475             :         }
    3476             : }
    3477             : 
    3478         190 : static int is_map(zval *array)
    3479             : {
    3480             :         zend_ulong index;
    3481             :         zend_string *key;
    3482         190 :         zend_ulong i = 0;
    3483             : 
    3484         598 :         ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(array), index, key) {
    3485         269 :                 if (key || index != i) {
    3486         130 :                         return TRUE;
    3487             :                 }
    3488         139 :                 i++;
    3489             :         } ZEND_HASH_FOREACH_END();
    3490          60 :         return FALSE;
    3491             : }
    3492             : 
    3493          38 : static encodePtr get_array_type(xmlNodePtr node, zval *array, smart_str *type)
    3494             : {
    3495             :         HashTable *ht;
    3496             :         int i, cur_type, prev_type, different;
    3497             :         zval *tmp;
    3498          38 :         char *prev_stype = NULL, *cur_stype = NULL, *prev_ns = NULL, *cur_ns = NULL;
    3499             : 
    3500          76 :         if (!array || Z_TYPE_P(array) != IS_ARRAY) {
    3501             :                 smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
    3502           0 :                 return get_conversion(XSD_ANYTYPE);
    3503             :         }
    3504             : 
    3505          38 :         i = 0;
    3506          38 :         different = FALSE;
    3507          38 :         cur_type = prev_type = 0;
    3508          38 :         ht = Z_ARRVAL_P(array);
    3509             : 
    3510         251 :         ZEND_HASH_FOREACH_VAL_IND(ht, tmp) {
    3511          71 :                 ZVAL_DEREF(tmp);
    3512         137 :                 if (Z_TYPE_P(tmp) == IS_OBJECT &&
    3513          44 :                     Z_OBJCE_P(tmp) == soap_var_class_entry) {
    3514             :                         zval *ztype;
    3515             : 
    3516          44 :                         if ((ztype = zend_hash_str_find(Z_OBJPROP_P(tmp), "enc_type", sizeof("enc_type")-1)) == NULL ||
    3517             :                             Z_TYPE_P(ztype) != IS_LONG) {
    3518           0 :                                 soap_error0(E_ERROR,  "Encoding: SoapVar has no 'enc_type' property");
    3519             :                         }
    3520          22 :                         cur_type = Z_LVAL_P(ztype);
    3521             : 
    3522          26 :                         if ((ztype = zend_hash_str_find(Z_OBJPROP_P(tmp), "enc_stype", sizeof("enc_stype")-1)) != NULL &&
    3523             :                             Z_TYPE_P(ztype) == IS_STRING) {
    3524           2 :                                 cur_stype = Z_STRVAL_P(ztype);
    3525             :                         } else {
    3526          20 :                                 cur_stype = NULL;
    3527             :                         }
    3528             : 
    3529          26 :                         if ((ztype = zend_hash_str_find(Z_OBJPROP_P(tmp), "enc_ns", sizeof("enc_ns")-1)) != NULL &&
    3530             :                             Z_TYPE_P(ztype) == IS_STRING) {
    3531           2 :                                 cur_ns = Z_STRVAL_P(ztype);
    3532             :                         } else {
    3533          20 :                                 cur_ns = NULL;
    3534             :                         }
    3535             : 
    3536          49 :                 } else if (Z_TYPE_P(tmp) == IS_ARRAY && is_map(tmp)) {
    3537           0 :                         cur_type = APACHE_MAP;
    3538           0 :                         cur_stype = NULL;
    3539           0 :                         cur_ns = NULL;
    3540             :                 } else {
    3541          49 :                         cur_type = Z_TYPE_P(tmp);
    3542          49 :                         cur_stype = NULL;
    3543          49 :                         cur_ns = NULL;
    3544             :                 }
    3545             : 
    3546          71 :                 if (i > 0) {
    3547          37 :                         if ((cur_type != prev_type) ||
    3548           1 :                             (cur_stype != NULL && prev_stype != NULL && strcmp(cur_stype,prev_stype) != 0) ||
    3549             :                             (cur_stype == NULL && cur_stype != prev_stype) ||
    3550           1 :                             (cur_ns != NULL && prev_ns != NULL && strcmp(cur_ns,prev_ns) != 0) ||
    3551             :                             (cur_ns == NULL && cur_ns != prev_ns)) {
    3552           0 :                                 different = TRUE;
    3553           0 :                                 break;
    3554             :                         }
    3555             :                 }
    3556             : 
    3557          71 :                 prev_type = cur_type;
    3558          71 :                 prev_stype = cur_stype;
    3559          71 :                 prev_ns = cur_ns;
    3560          71 :                 i++;
    3561             :         } ZEND_HASH_FOREACH_END();
    3562             : 
    3563          38 :         if (different || i == 0) {
    3564             :                 smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
    3565           2 :                 return get_conversion(XSD_ANYTYPE);
    3566             :         } else {
    3567             :                 encodePtr enc;
    3568             : 
    3569          36 :                 if (cur_stype != NULL) {
    3570           1 :                         smart_str array_type = {0};
    3571             : 
    3572           1 :                         if (cur_ns) {
    3573           1 :                                 xmlNsPtr ns = encode_add_ns(node, cur_ns);
    3574             : 
    3575           1 :                                 smart_str_appends(type, (char*)ns->prefix);
    3576             :                                 smart_str_appendc(type, ':');
    3577           1 :                                 smart_str_appends(&array_type, cur_ns);
    3578             :                                 smart_str_appendc(&array_type, ':');
    3579             :                         }
    3580           1 :                         smart_str_appends(type, cur_stype);
    3581             :                         smart_str_0(type);
    3582           1 :                         smart_str_appends(&array_type, cur_stype);
    3583             :                         smart_str_0(&array_type);
    3584             : 
    3585           1 :                         enc = get_encoder_ex(SOAP_GLOBAL(sdl), ZSTR_VAL(array_type.s), ZSTR_LEN(array_type.s));
    3586             :                         smart_str_free(&array_type);
    3587           1 :                         return enc;
    3588             :                 } else {
    3589          35 :                         enc = get_conversion(cur_type);
    3590          35 :                         get_type_str(node, enc->details.ns, enc->details.type_str, type);
    3591          35 :                         return enc;
    3592             :                 }
    3593             :         }
    3594             :         return NULL;
    3595             : }
    3596             : 
    3597        1542 : static void get_type_str(xmlNodePtr node, const char* ns, const char* type, smart_str* ret)
    3598             : {
    3599             : 
    3600        1542 :         if (ns) {
    3601             :                 xmlNsPtr xmlns;
    3602        1616 :                 if (SOAP_GLOBAL(soap_version) == SOAP_1_2 &&
    3603          71 :                     strcmp(ns,SOAP_1_1_ENC_NAMESPACE) == 0) {
    3604           3 :                         ns = SOAP_1_2_ENC_NAMESPACE;
    3605        3010 :                 } else if (SOAP_GLOBAL(soap_version) == SOAP_1_1 &&
    3606        1471 :                            strcmp(ns,SOAP_1_2_ENC_NAMESPACE) == 0) {
    3607           0 :                         ns = SOAP_1_1_ENC_NAMESPACE;
    3608             :                 }
    3609        1542 :                 xmlns = encode_add_ns(node, ns);
    3610        1542 :                 smart_str_appends(ret, (char*)xmlns->prefix);
    3611             :                 smart_str_appendc(ret, ':');
    3612             :         }
    3613        1542 :         smart_str_appendl(ret, type, strlen(type));
    3614             :         smart_str_0(ret);
    3615        1542 : }
    3616             : 
    3617          15 : static void delete_mapping(void *data)
    3618             : {
    3619          15 :         soapMappingPtr map = (soapMappingPtr)data;
    3620             : 
    3621          15 :         zval_ptr_dtor(&map->to_xml);
    3622          15 :         zval_ptr_dtor(&map->to_zval);
    3623          15 :         efree(map);
    3624          15 : }
    3625             : 
    3626        8045 : void delete_encoder(zval *zv)
    3627             : {
    3628        8045 :         encodePtr t = Z_PTR_P(zv);
    3629        8045 :         if (t->details.ns) {
    3630        8045 :                 efree(t->details.ns);
    3631             :         }
    3632        8045 :         if (t->details.type_str) {
    3633        8045 :                 efree(t->details.type_str);
    3634             :         }
    3635        8045 :         if (t->details.map) {
    3636          15 :                 delete_mapping(t->details.map);
    3637             :         }
    3638        8045 :         efree(t);
    3639        8045 : }
    3640             : 
    3641          10 : void delete_encoder_persistent(zval *zv)
    3642             : {
    3643          10 :         encodePtr t = Z_PTR_P(zv);
    3644          10 :         if (t->details.ns) {
    3645          10 :                 free(t->details.ns);
    3646             :         }
    3647          10 :         if (t->details.type_str) {
    3648          10 :                 free(t->details.type_str);
    3649             :         }
    3650             :         /* we should never have mapping in persistent encoder */
    3651             :         assert(t->details.map == NULL);
    3652          10 :         free(t);
    3653          10 : }

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:06 +0000 (2 days ago)

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