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