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