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