xref: /PHP-7.4/ext/soap/php_encoding.c (revision 75cb6782)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Brad Lafountain <rodif_bl@yahoo.com>                        |
16   |          Shane Caraveo <shane@caraveo.com>                           |
17   |          Dmitry Stogov <dmitry@php.net>                              |
18   +----------------------------------------------------------------------+
19 */
20 
21 #include <time.h>
22 
23 #include "php_soap.h"
24 #include "ext/libxml/php_libxml.h"
25 #include "ext/standard/base64.h"
26 #include <libxml/parserInternals.h>
27 #include "zend_strtod.h"
28 #include "zend_interfaces.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, 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 const encode defaultEncoding[] = {
143 	{{UNKNOWN_TYPE, NULL, NULL, NULL, NULL}, guess_zval_convert, guess_xml_convert},
144 
145 	{{IS_NULL, "nil", XSI_NAMESPACE, NULL, NULL}, to_zval_null, to_xml_null},
146 	{{IS_STRING, XSD_STRING_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_string, to_xml_string},
147 	{{IS_LONG, XSD_INT_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
148 	{{IS_DOUBLE, XSD_FLOAT_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_double, to_xml_double},
149 	{{IS_FALSE, XSD_BOOLEAN_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_bool, to_xml_bool},
150 	{{IS_TRUE, XSD_BOOLEAN_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_bool, to_xml_bool},
151 	{{IS_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_1_ENC_NAMESPACE, NULL, NULL}, to_zval_array, guess_array_map},
152 	{{IS_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_1_ENC_NAMESPACE, NULL, NULL}, to_zval_object, to_xml_object},
153 	{{IS_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_2_ENC_NAMESPACE, NULL, NULL}, to_zval_array, guess_array_map},
154 	{{IS_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_2_ENC_NAMESPACE, NULL, NULL}, to_zval_object, to_xml_object},
155 
156 	{{XSD_STRING, XSD_STRING_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_string, to_xml_string},
157 	{{XSD_BOOLEAN, XSD_BOOLEAN_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_bool, to_xml_bool},
158 	{{XSD_DECIMAL, XSD_DECIMAL_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
159 	{{XSD_FLOAT, XSD_FLOAT_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_double, to_xml_double},
160 	{{XSD_DOUBLE, XSD_DOUBLE_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_double, to_xml_double},
161 
162 	{{XSD_DATETIME, XSD_DATETIME_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_datetime},
163 	{{XSD_TIME, XSD_TIME_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_time},
164 	{{XSD_DATE, XSD_DATE_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_date},
165 	{{XSD_GYEARMONTH, XSD_GYEARMONTH_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_gyearmonth},
166 	{{XSD_GYEAR, XSD_GYEAR_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_gyear},
167 	{{XSD_GMONTHDAY, XSD_GMONTHDAY_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_gmonthday},
168 	{{XSD_GDAY, XSD_GDAY_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_gday},
169 	{{XSD_GMONTH, XSD_GMONTH_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_gmonth},
170 	{{XSD_DURATION, XSD_DURATION_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_duration},
171 
172 	{{XSD_HEXBINARY, XSD_HEXBINARY_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_hexbin, to_xml_hexbin},
173 	{{XSD_BASE64BINARY, XSD_BASE64BINARY_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_base64, to_xml_base64},
174 
175 	{{XSD_LONG, XSD_LONG_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
176 	{{XSD_INT, XSD_INT_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
177 	{{XSD_SHORT, XSD_SHORT_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
178 	{{XSD_BYTE, XSD_BYTE_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
179 	{{XSD_NONPOSITIVEINTEGER, XSD_NONPOSITIVEINTEGER_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
180 	{{XSD_POSITIVEINTEGER, XSD_POSITIVEINTEGER_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
181 	{{XSD_NONNEGATIVEINTEGER, XSD_NONNEGATIVEINTEGER_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
182 	{{XSD_NEGATIVEINTEGER, XSD_NEGATIVEINTEGER_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
183 	{{XSD_UNSIGNEDBYTE, XSD_UNSIGNEDBYTE_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
184 	{{XSD_UNSIGNEDSHORT, XSD_UNSIGNEDSHORT_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
185 	{{XSD_UNSIGNEDINT, XSD_UNSIGNEDINT_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
186 	{{XSD_UNSIGNEDLONG, XSD_UNSIGNEDLONG_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
187 	{{XSD_INTEGER, XSD_INTEGER_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
188 
189 	{{XSD_ANYTYPE, XSD_ANYTYPE_STRING, XSD_NAMESPACE, NULL, NULL}, guess_zval_convert, guess_xml_convert},
190 	{{XSD_UR_TYPE, XSD_UR_TYPE_STRING, XSD_NAMESPACE, NULL, NULL}, guess_zval_convert, guess_xml_convert},
191 	{{XSD_ANYURI, XSD_ANYURI_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
192 	{{XSD_QNAME, XSD_QNAME_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
193 	{{XSD_NOTATION, XSD_NOTATION_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
194 	{{XSD_NORMALIZEDSTRING, XSD_NORMALIZEDSTRING_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringr, to_xml_string},
195 	{{XSD_TOKEN, XSD_TOKEN_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
196 	{{XSD_LANGUAGE, XSD_LANGUAGE_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
197 	{{XSD_NMTOKEN, XSD_NMTOKEN_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
198 	{{XSD_NMTOKENS, XSD_NMTOKENS_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_list1},
199 	{{XSD_NAME, XSD_NAME_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
200 	{{XSD_NCNAME, XSD_NCNAME_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
201 	{{XSD_ID, XSD_ID_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
202 	{{XSD_IDREF, XSD_IDREF_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
203 	{{XSD_IDREFS, XSD_IDREFS_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_list1},
204 	{{XSD_ENTITY, XSD_ENTITY_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
205 	{{XSD_ENTITIES, XSD_ENTITIES_STRING, XSD_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_list1},
206 
207 	{{APACHE_MAP, APACHE_MAP_STRING, APACHE_NAMESPACE, NULL, NULL}, to_zval_map, to_xml_map},
208 
209 	{{SOAP_ENC_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_1_ENC_NAMESPACE, NULL, NULL}, to_zval_object, to_xml_object},
210 	{{SOAP_ENC_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_1_ENC_NAMESPACE, NULL, NULL}, to_zval_array, to_xml_array},
211 	{{SOAP_ENC_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_2_ENC_NAMESPACE, NULL, NULL}, to_zval_object, to_xml_object},
212 	{{SOAP_ENC_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_2_ENC_NAMESPACE, 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}, to_zval_string, to_xml_string},
216 	{{XSD_BOOLEAN, XSD_BOOLEAN_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_bool, to_xml_bool},
217 	{{XSD_DECIMAL, XSD_DECIMAL_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
218 	{{XSD_FLOAT, XSD_FLOAT_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_double, to_xml_double},
219 	{{XSD_DOUBLE, XSD_DOUBLE_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_double, to_xml_double},
220 
221 	{{XSD_LONG, XSD_LONG_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
222 	{{XSD_INT, XSD_INT_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
223 	{{XSD_SHORT, XSD_SHORT_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
224 	{{XSD_BYTE, XSD_BYTE_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_long, to_xml_long},
225 	{{XSD_1999_TIMEINSTANT, XSD_1999_TIMEINSTANT_STRING, XSD_1999_NAMESPACE, NULL, NULL}, to_zval_stringc, to_xml_string},
226 
227 	{{XSD_ANYXML, "<anyXML>", "<anyXML>", NULL, NULL}, to_zval_any, to_xml_any},
228 
229 	{{END_KNOWN_TYPES, 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 (strcmp(enc->details.type_str, type) == 0) {
277 				return enc;
278 			}
279 		} ZEND_HASH_FOREACH_END();
280 	}
281 	return NULL;
282 }
283 
soap_check_zval_ref(zval * data,xmlNodePtr node)284 static zend_bool soap_check_zval_ref(zval *data, xmlNodePtr node) {
285 	xmlNodePtr node_ptr;
286 
287 	if (SOAP_GLOBAL(ref_map)) {
288 		if (Z_TYPE_P(data) == IS_OBJECT) {
289 			data = (zval*)Z_OBJ_P(data);
290 		}
291 		if ((node_ptr = zend_hash_index_find_ptr(SOAP_GLOBAL(ref_map), (zend_ulong)data)) != NULL) {
292 			xmlAttrPtr attr = node_ptr->properties;
293 			char *id;
294 			smart_str prefix = {0};
295 
296 			if (node_ptr == node) {
297 				return 0;
298 			}
299 			if (SOAP_GLOBAL(soap_version) == SOAP_1_1) {
300 				while (1) {
301 					attr = get_attribute(attr, "id");
302 					if (attr == NULL || attr->ns == NULL) {
303 						break;
304 					}
305 					attr = attr->next;
306 				}
307 				if (attr) {
308 					id = (char*)attr->children->content;
309 					smart_str_appendc(&prefix, '#');
310 					smart_str_appends(&prefix, id);
311 					smart_str_0(&prefix);
312 					id = ZSTR_VAL(prefix.s);
313 				} else {
314 					SOAP_GLOBAL(cur_uniq_ref)++;
315 					smart_str_appendl(&prefix, "#ref", 4);
316 					smart_str_append_long(&prefix, SOAP_GLOBAL(cur_uniq_ref));
317 					smart_str_0(&prefix);
318 					id = ZSTR_VAL(prefix.s);
319 					xmlSetProp(node_ptr, BAD_CAST("id"), BAD_CAST(id+1));
320 				}
321 				xmlSetProp(node, BAD_CAST("href"), BAD_CAST(id));
322 			} else {
323 				attr = get_attribute_ex(attr, "id", SOAP_1_2_ENC_NAMESPACE);
324 				if (attr) {
325 					id = (char*)attr->children->content;
326 					smart_str_appendc(&prefix, '#');
327 					smart_str_appends(&prefix, id);
328 					smart_str_0(&prefix);
329 					id = ZSTR_VAL(prefix.s);
330 				} else {
331 					SOAP_GLOBAL(cur_uniq_ref)++;
332 					smart_str_appendl(&prefix, "#ref", 4);
333 					smart_str_append_long(&prefix, SOAP_GLOBAL(cur_uniq_ref));
334 					smart_str_0(&prefix);
335 					id = ZSTR_VAL(prefix.s);
336 					set_ns_prop(node_ptr, SOAP_1_2_ENC_NAMESPACE, "id", id+1);
337 				}
338 				set_ns_prop(node, SOAP_1_2_ENC_NAMESPACE, "ref", id);
339 			}
340 			smart_str_free(&prefix);
341 			return 1;
342 		} else {
343 			zend_hash_index_update_ptr(SOAP_GLOBAL(ref_map), (zend_ulong)data, node);
344 		}
345 	}
346 	return 0;
347 }
348 
soap_check_xml_ref(zval * data,xmlNodePtr node)349 static zend_bool soap_check_xml_ref(zval *data, xmlNodePtr node)
350 {
351 	zval *data_ptr;
352 
353 	if (SOAP_GLOBAL(ref_map)) {
354 		if ((data_ptr = zend_hash_index_find(SOAP_GLOBAL(ref_map), (zend_ulong)node)) != NULL) {
355 			if (!Z_REFCOUNTED_P(data) ||
356 			    !Z_REFCOUNTED_P(data_ptr) ||
357 			    Z_COUNTED_P(data) != Z_COUNTED_P(data_ptr)) {
358 				zval_ptr_dtor(data);
359 				ZVAL_COPY(data, data_ptr);
360 				return 1;
361 			}
362 		}
363 	}
364 	return 0;
365 }
366 
soap_add_xml_ref(zval * data,xmlNodePtr node)367 static void soap_add_xml_ref(zval *data, xmlNodePtr node)
368 {
369 	if (SOAP_GLOBAL(ref_map)) {
370 		zend_hash_index_update(SOAP_GLOBAL(ref_map), (zend_ulong)node, data);
371 	}
372 }
373 
master_to_xml_int(encodePtr encode,zval * data,int style,xmlNodePtr parent,int check_class_map)374 static xmlNodePtr master_to_xml_int(encodePtr encode, zval *data, int style, xmlNodePtr parent, int check_class_map)
375 {
376 	xmlNodePtr node = NULL;
377 	int add_type = 0;
378 
379 	if (data) {
380 		ZVAL_DEREF(data);
381 	}
382 
383 	/* Special handling of class SoapVar */
384 	if (data &&
385 	    Z_TYPE_P(data) == IS_OBJECT &&
386 	    Z_OBJCE_P(data) == soap_var_class_entry) {
387 		zval *ztype, *zdata, *zns, *zstype, *zname, *znamens;
388 		encodePtr enc = NULL;
389 		HashTable *ht = Z_OBJPROP_P(data);
390 
391 		if ((ztype = zend_hash_str_find_deref(ht, "enc_type", sizeof("enc_type")-1)) == NULL ||
392 		    Z_TYPE_P(ztype) != IS_LONG) {
393 			soap_error0(E_ERROR, "Encoding: SoapVar has no 'enc_type' property");
394 		}
395 
396 		if ((zstype = zend_hash_str_find_deref(ht, "enc_stype", sizeof("enc_stype")-1)) != NULL &&
397 		    Z_TYPE_P(zstype) == IS_STRING) {
398 			if ((zns = zend_hash_str_find_deref(ht, "enc_ns", sizeof("enc_ns")-1)) != NULL &&
399 			    Z_TYPE_P(zns) == IS_STRING) {
400 				enc = get_encoder(SOAP_GLOBAL(sdl), Z_STRVAL_P(zns), Z_STRVAL_P(zstype));
401 			} else {
402 				zns = NULL;
403 				enc = get_encoder_ex(SOAP_GLOBAL(sdl), Z_STRVAL_P(zstype), Z_STRLEN_P(zstype));
404 			}
405 			if (enc == NULL && SOAP_GLOBAL(typemap)) {
406 				smart_str nscat = {0};
407 
408 				if (zns != NULL) {
409 					smart_str_appendl(&nscat, Z_STRVAL_P(zns), Z_STRLEN_P(zns));
410 					smart_str_appendc(&nscat, ':');
411 				}
412 				smart_str_appendl(&nscat, Z_STRVAL_P(zstype), Z_STRLEN_P(zstype));
413 				smart_str_0(&nscat);
414 				enc = zend_hash_find_ptr(SOAP_GLOBAL(typemap), nscat.s);
415 				smart_str_free(&nscat);
416 			}
417 		}
418 		if (enc == NULL) {
419 			enc = get_conversion(Z_LVAL_P(ztype));
420 		}
421 		if (enc == NULL) {
422 			enc = encode;
423 		}
424 
425 		zdata = zend_hash_str_find_deref(ht, "enc_value", sizeof("enc_value")-1);
426 		node = master_to_xml(enc, zdata, style, parent);
427 
428 		if (style == SOAP_ENCODED || (SOAP_GLOBAL(sdl) && encode != enc)) {
429 			if ((zstype = zend_hash_str_find_deref(ht, "enc_stype", sizeof("enc_stype")-1)) != NULL &&
430 			    Z_TYPE_P(zstype) == IS_STRING) {
431 				if ((zns = zend_hash_str_find_deref(ht, "enc_ns", sizeof("enc_ns")-1)) != NULL &&
432 				    Z_TYPE_P(zns) == IS_STRING) {
433 					set_ns_and_type_ex(node, Z_STRVAL_P(zns), Z_STRVAL_P(zstype));
434 				} else {
435 					set_ns_and_type_ex(node, NULL, Z_STRVAL_P(zstype));
436 				}
437 			}
438 		}
439 
440 		if ((zname = zend_hash_str_find_deref(ht, "enc_name", sizeof("enc_name")-1)) != NULL &&
441 		    Z_TYPE_P(zname) == IS_STRING) {
442 			xmlNodeSetName(node, BAD_CAST(Z_STRVAL_P(zname)));
443 		}
444 		if ((znamens = zend_hash_str_find_deref(ht, "enc_namens", sizeof("enc_namens")-1)) != NULL &&
445 		    Z_TYPE_P(znamens) == IS_STRING) {
446 			xmlNsPtr nsp = encode_add_ns(node, Z_STRVAL_P(znamens));
447 			xmlSetNs(node, nsp);
448 		}
449 	} else {
450 		if (check_class_map && SOAP_GLOBAL(class_map) && data &&
451 		    Z_TYPE_P(data) == IS_OBJECT &&
452 		    !GC_IS_RECURSIVE(Z_OBJPROP_P(data))) {
453 			zend_class_entry *ce = Z_OBJCE_P(data);
454 			zval *tmp;
455 			zend_string *type_name;
456 
457 			ZEND_HASH_FOREACH_STR_KEY_VAL(SOAP_GLOBAL(class_map), type_name, tmp) {
458 				ZVAL_DEREF(tmp);
459 				if (Z_TYPE_P(tmp) == IS_STRING &&
460 				    ZSTR_LEN(ce->name) == Z_STRLEN_P(tmp) &&
461 				    zend_binary_strncasecmp(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), Z_STRVAL_P(tmp), ZSTR_LEN(ce->name), ZSTR_LEN(ce->name)) == 0 &&
462 				    type_name) {
463 
464 					/* TODO: namespace isn't stored */
465 					encodePtr enc = NULL;
466 					if (SOAP_GLOBAL(sdl)) {
467 						enc = get_encoder(SOAP_GLOBAL(sdl), SOAP_GLOBAL(sdl)->target_ns, ZSTR_VAL(type_name));
468 						if (!enc) {
469 							enc = find_encoder_by_type_name(SOAP_GLOBAL(sdl), ZSTR_VAL(type_name));
470 						}
471 					}
472 					if (enc) {
473 						if (encode != enc && style == SOAP_LITERAL) {
474 							add_type = 1;
475 						}
476 						encode = enc;
477 					}
478 					break;
479 				}
480 			} ZEND_HASH_FOREACH_END();
481 		}
482 
483 		if (encode == NULL) {
484 			encode = get_conversion(UNKNOWN_TYPE);
485 		}
486 		if (SOAP_GLOBAL(typemap) && encode->details.type_str) {
487 			smart_str nscat = {0};
488 			encodePtr new_enc;
489 
490 			if (encode->details.ns) {
491 				smart_str_appends(&nscat, encode->details.ns);
492 				smart_str_appendc(&nscat, ':');
493 			}
494 			smart_str_appends(&nscat, encode->details.type_str);
495 			smart_str_0(&nscat);
496 			if ((new_enc = zend_hash_find_ptr(SOAP_GLOBAL(typemap), nscat.s)) != NULL) {
497 				encode = new_enc;
498 			}
499 			smart_str_free(&nscat);
500 		}
501 		if (encode->to_xml) {
502 			node = encode->to_xml(&encode->details, data, style, parent);
503 			if (add_type) {
504 				set_ns_and_type(node, &encode->details);
505 			}
506 		}
507 	}
508 	return node;
509 }
510 
master_to_xml(encodePtr encode,zval * data,int style,xmlNodePtr parent)511 xmlNodePtr master_to_xml(encodePtr encode, zval *data, int style, xmlNodePtr parent)
512 {
513 	return master_to_xml_int(encode, data, style, parent, 1);
514 }
515 
master_to_zval_int(zval * ret,encodePtr encode,xmlNodePtr data)516 static zval *master_to_zval_int(zval *ret, encodePtr encode, xmlNodePtr data)
517 {
518 	if (SOAP_GLOBAL(typemap)) {
519 		if (encode->details.type_str) {
520 			smart_str nscat = {0};
521 			encodePtr new_enc;
522 
523 			if (encode->details.ns) {
524 				smart_str_appends(&nscat, encode->details.ns);
525 				smart_str_appendc(&nscat, ':');
526 			}
527 			smart_str_appends(&nscat, encode->details.type_str);
528 			smart_str_0(&nscat);
529 			if ((new_enc = zend_hash_find_ptr(SOAP_GLOBAL(typemap), nscat.s)) != NULL) {
530 				encode = new_enc;
531 			}
532 			smart_str_free(&nscat);
533 		} else {
534 			xmlAttrPtr type_attr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
535 
536 			if (type_attr != NULL) {
537 				encodePtr new_enc;
538 				xmlNsPtr nsptr;
539 				char *ns, *cptype;
540 				smart_str nscat = {0};
541 
542 				parse_namespace(type_attr->children->content, &cptype, &ns);
543 				nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
544 				if (nsptr != NULL) {
545 					smart_str_appends(&nscat, (char*)nsptr->href);
546 					smart_str_appendc(&nscat, ':');
547 				}
548 				smart_str_appends(&nscat, cptype);
549 				smart_str_0(&nscat);
550 				efree(cptype);
551 				if (ns) {efree(ns);}
552 				if ((new_enc = zend_hash_find_ptr(SOAP_GLOBAL(typemap), nscat.s)) != NULL) {
553 					encode = new_enc;
554 				}
555 				smart_str_free(&nscat);
556 			}
557 		}
558 	}
559 	if (encode->to_zval) {
560 		ret = encode->to_zval(ret, &encode->details, data);
561 	}
562 	return ret;
563 }
564 
master_to_zval(zval * ret,encodePtr encode,xmlNodePtr data)565 zval *master_to_zval(zval *ret, encodePtr encode, xmlNodePtr data)
566 {
567 	data = check_and_resolve_href(data);
568 
569 	if (encode == NULL) {
570 		encode = get_conversion(UNKNOWN_TYPE);
571 	} else {
572 		/* Use xsi:type if it is defined */
573 		xmlAttrPtr type_attr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
574 
575 		if (type_attr != NULL) {
576 			encodePtr  enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, type_attr->children->content);
577 
578 			if (enc != NULL && enc != encode) {
579 			  encodePtr tmp = enc;
580 			  while (tmp &&
581 			         tmp->details.sdl_type != NULL &&
582 			         tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
583 			    if (enc == tmp->details.sdl_type->encode ||
584 			        tmp == tmp->details.sdl_type->encode) {
585 			    	enc = NULL;
586 			    	break;
587 			    }
588 			    tmp = tmp->details.sdl_type->encode;
589 			  }
590 			  if (enc != NULL) {
591 			    encode = enc;
592 			  }
593 			}
594 		}
595 	}
596 	return master_to_zval_int(ret, encode, data);
597 }
598 
to_xml_user(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)599 xmlNodePtr to_xml_user(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
600 {
601 	xmlNodePtr ret = NULL;
602 	zval return_value;
603 
604 	if (type && type->map && Z_TYPE(type->map->to_xml) != IS_UNDEF) {
605 		ZVAL_NULL(&return_value);
606 
607 		if (call_user_function(NULL, NULL, &type->map->to_xml, &return_value, 1, data) == FAILURE) {
608 			soap_error0(E_ERROR, "Encoding: Error calling to_xml callback");
609 		}
610 		if (Z_TYPE(return_value) == IS_STRING) {
611 			xmlDocPtr doc = soap_xmlParseMemory(Z_STRVAL(return_value), Z_STRLEN(return_value));
612 			if (doc && doc->children) {
613 				ret = xmlDocCopyNode(doc->children, parent->doc, 1);
614 			}
615 			xmlFreeDoc(doc);
616 		}
617 
618 		zval_ptr_dtor(&return_value);
619 	}
620 	if (!ret) {
621 		ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
622 	}
623 	xmlAddChild(parent, ret);
624 	if (style == SOAP_ENCODED) {
625 		set_ns_and_type(ret, type);
626 	}
627 	return ret;
628 }
629 
to_zval_user(zval * ret,encodeTypePtr type,xmlNodePtr node)630 zval *to_zval_user(zval *ret, encodeTypePtr type, xmlNodePtr node)
631 {
632 	if (type && type->map && Z_TYPE(type->map->to_zval) != IS_UNDEF) {
633 		xmlBufferPtr buf;
634 		zval data;
635 		xmlNodePtr copy;
636 
637 		copy = xmlCopyNode(node, 1);
638 		buf = xmlBufferCreate();
639 		xmlNodeDump(buf, NULL, copy, 0, 0);
640 		ZVAL_STRING(&data, (char*)xmlBufferContent(buf));
641 		xmlBufferFree(buf);
642 		xmlFreeNode(copy);
643 
644 		if (call_user_function(NULL, NULL, &type->map->to_zval, ret, 1, &data) == FAILURE) {
645 			soap_error0(E_ERROR, "Encoding: Error calling from_xml callback");
646 		} else if (EG(exception)) {
647 			ZVAL_NULL(ret);
648 		}
649 		zval_ptr_dtor(&data);
650 	} else {
651 		ZVAL_NULL(ret);
652 	}
653 	return ret;
654 }
655 
656 /* TODO: get rid of "bogus".. ither by passing in the already created xmlnode or passing in the node name */
657 /* String encode/decode */
to_zval_string(zval * ret,encodeTypePtr type,xmlNodePtr data)658 static zval *to_zval_string(zval *ret, encodeTypePtr type, xmlNodePtr data)
659 {
660 	ZVAL_NULL(ret);
661 	FIND_XML_NULL(data, ret);
662 	if (data && data->children) {
663 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
664 			if (SOAP_GLOBAL(encoding) != NULL) {
665 				xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
666 				xmlBufferPtr out = xmlBufferCreate();
667 				int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
668 
669 				if (n >= 0) {
670 					ZVAL_STRING(ret, (char*)xmlBufferContent(out));
671 				} else {
672 					ZVAL_STRING(ret, (char*)data->children->content);
673 				}
674 				xmlBufferFree(out);
675 				xmlBufferFree(in);
676 			} else {
677 				ZVAL_STRING(ret, (char*)data->children->content);
678 			}
679 		} else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
680 			ZVAL_STRING(ret, (char*)data->children->content);
681 		} else {
682 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
683 		}
684 	} else {
685 		ZVAL_EMPTY_STRING(ret);
686 	}
687 	return ret;
688 }
689 
to_zval_stringr(zval * ret,encodeTypePtr type,xmlNodePtr data)690 static zval *to_zval_stringr(zval *ret, encodeTypePtr type, xmlNodePtr data)
691 {
692 	ZVAL_NULL(ret);
693 	FIND_XML_NULL(data, ret);
694 	if (data && data->children) {
695 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
696 			whiteSpace_replace(data->children->content);
697 			if (SOAP_GLOBAL(encoding) != NULL) {
698 				xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
699 				xmlBufferPtr out = xmlBufferCreate();
700 				int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
701 
702 				if (n >= 0) {
703 					ZVAL_STRING(ret, (char*)xmlBufferContent(out));
704 				} else {
705 					ZVAL_STRING(ret, (char*)data->children->content);
706 				}
707 				xmlBufferFree(out);
708 				xmlBufferFree(in);
709 			} else {
710 				ZVAL_STRING(ret, (char*)data->children->content);
711 			}
712 		} else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
713 			ZVAL_STRING(ret, (char*)data->children->content);
714 		} else {
715 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
716 		}
717 	} else {
718 		ZVAL_EMPTY_STRING(ret);
719 	}
720 	return ret;
721 }
722 
to_zval_stringc(zval * ret,encodeTypePtr type,xmlNodePtr data)723 static zval *to_zval_stringc(zval *ret, encodeTypePtr type, xmlNodePtr data)
724 {
725 	ZVAL_NULL(ret);
726 	FIND_XML_NULL(data, ret);
727 	if (data && data->children) {
728 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
729 			whiteSpace_collapse(data->children->content);
730 			if (SOAP_GLOBAL(encoding) != NULL) {
731 				xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
732 				xmlBufferPtr out = xmlBufferCreate();
733 				int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
734 
735 				if (n >= 0) {
736 					ZVAL_STRING(ret, (char*)xmlBufferContent(out));
737 				} else {
738 					ZVAL_STRING(ret, (char*)data->children->content);
739 				}
740 				xmlBufferFree(out);
741 				xmlBufferFree(in);
742 			} else {
743 				ZVAL_STRING(ret, (char*)data->children->content);
744 			}
745 		} else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
746 			ZVAL_STRING(ret, (char*)data->children->content);
747 		} else {
748 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
749 		}
750 	} else {
751 		ZVAL_EMPTY_STRING(ret);
752 	}
753 	return ret;
754 }
755 
to_zval_base64(zval * ret,encodeTypePtr type,xmlNodePtr data)756 static zval *to_zval_base64(zval *ret, encodeTypePtr type, xmlNodePtr data)
757 {
758 	zend_string *str;
759 
760 	ZVAL_NULL(ret);
761 	FIND_XML_NULL(data, ret);
762 	if (data && data->children) {
763 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
764 			whiteSpace_collapse(data->children->content);
765 			str = php_base64_decode(data->children->content, strlen((char*)data->children->content));
766 			if (!str) {
767 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
768 			}
769 			ZVAL_STR(ret, str);
770 		} else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
771 			str = php_base64_decode(data->children->content, strlen((char*)data->children->content));
772 			if (!str) {
773 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
774 			}
775 			ZVAL_STR(ret, str);
776 		} else {
777 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
778 		}
779 	} else {
780 		ZVAL_EMPTY_STRING(ret);
781 	}
782 	return ret;
783 }
784 
to_zval_hexbin(zval * ret,encodeTypePtr type,xmlNodePtr data)785 static zval *to_zval_hexbin(zval *ret, encodeTypePtr type, xmlNodePtr data)
786 {
787 	zend_string *str;
788 	size_t i, j;
789 	unsigned char c;
790 
791 	ZVAL_NULL(ret);
792 	FIND_XML_NULL(data, ret);
793 	if (data && data->children) {
794 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
795 			whiteSpace_collapse(data->children->content);
796 		} else if (data->children->type != XML_CDATA_SECTION_NODE || data->children->next != NULL) {
797 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
798 			return ret;
799 		}
800 		str = zend_string_alloc(strlen((char*)data->children->content) / 2, 0);
801 		for (i = j = 0; i < ZSTR_LEN(str); i++) {
802 			c = data->children->content[j++];
803 			if (c >= '0' && c <= '9') {
804 				ZSTR_VAL(str)[i] = (c - '0') << 4;
805 			} else if (c >= 'a' && c <= 'f') {
806 				ZSTR_VAL(str)[i] = (c - 'a' + 10) << 4;
807 			} else if (c >= 'A' && c <= 'F') {
808 				ZSTR_VAL(str)[i] = (c - 'A' + 10) << 4;
809 			} else {
810 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
811 			}
812 			c = data->children->content[j++];
813 			if (c >= '0' && c <= '9') {
814 				ZSTR_VAL(str)[i] |= c - '0';
815 			} else if (c >= 'a' && c <= 'f') {
816 				ZSTR_VAL(str)[i] |= c - 'a' + 10;
817 			} else if (c >= 'A' && c <= 'F') {
818 				ZSTR_VAL(str)[i] |= c - 'A' + 10;
819 			} else {
820 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
821 			}
822 		}
823 		ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
824 		ZVAL_NEW_STR(ret, str);
825 	} else {
826 		ZVAL_EMPTY_STRING(ret);
827 	}
828 	return ret;
829 }
830 
to_xml_string(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)831 static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
832 {
833 	xmlNodePtr ret, text;
834 	char *str;
835 	int new_len;
836 
837 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
838 	xmlAddChild(parent, ret);
839 	FIND_ZVAL_NULL(data, ret, style);
840 
841 	if (Z_TYPE_P(data) == IS_STRING) {
842 		str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
843 		new_len = Z_STRLEN_P(data);
844 	} else {
845 		zend_string *tmp = zval_get_string_func(data);
846 		str = estrndup(ZSTR_VAL(tmp), ZSTR_LEN(tmp));
847 		new_len = ZSTR_LEN(tmp);
848 		zend_string_release_ex(tmp, 0);
849 	}
850 
851 	if (SOAP_GLOBAL(encoding) != NULL) {
852 		xmlBufferPtr in  = xmlBufferCreateStatic(str, new_len);
853 		xmlBufferPtr out = xmlBufferCreate();
854 		int n = xmlCharEncInFunc(SOAP_GLOBAL(encoding), out, in);
855 
856 		if (n >= 0) {
857 			efree(str);
858 			str = estrdup((char*)xmlBufferContent(out));
859 			new_len = n;
860 		}
861 		xmlBufferFree(out);
862 		xmlBufferFree(in);
863 	}
864 
865 	if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) {
866 		char *err = emalloc(new_len + 8);
867 		char c;
868 		int i;
869 
870 		memcpy(err, str, new_len+1);
871 		for (i = 0; (c = err[i++]);) {
872 			if ((c & 0x80) == 0) {
873 			} else if ((c & 0xe0) == 0xc0) {
874 				if ((err[i] & 0xc0) != 0x80) {
875 					break;
876 				}
877 				i++;
878 			} else if ((c & 0xf0) == 0xe0) {
879 				if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80) {
880 					break;
881 				}
882 				i += 2;
883 			} else if ((c & 0xf8) == 0xf0) {
884 				if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80 || (err[i+2] & 0xc0) != 0x80) {
885 					break;
886 				}
887 				i += 3;
888 			} else {
889 				break;
890 			}
891 		}
892 		if (c) {
893 			err[i-1] = '\\';
894 			err[i++] = 'x';
895 			err[i++] = ((unsigned char)c >> 4) + ((((unsigned char)c >> 4) > 9) ? ('a' - 10) : '0');
896 			err[i++] = (c & 15) + (((c & 15) > 9) ? ('a' - 10) : '0');
897 			err[i++] = '.';
898 			err[i++] = '.';
899 			err[i++] = '.';
900 			err[i++] = 0;
901 		}
902 
903 		soap_error1(E_ERROR,  "Encoding: string '%s' is not a valid utf-8 string", err);
904 	}
905 
906 	text = xmlNewTextLen(BAD_CAST(str), new_len);
907 	xmlAddChild(ret, text);
908 	efree(str);
909 
910 	if (style == SOAP_ENCODED) {
911 		set_ns_and_type(ret, type);
912 	}
913 	return ret;
914 }
915 
to_xml_base64(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)916 static xmlNodePtr to_xml_base64(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
917 {
918 	xmlNodePtr ret, text;
919 	zend_string *str;
920 
921 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
922 	xmlAddChild(parent, ret);
923 	FIND_ZVAL_NULL(data, ret, style);
924 
925 	if (Z_TYPE_P(data) == IS_STRING) {
926 		str = php_base64_encode((unsigned char*)Z_STRVAL_P(data), Z_STRLEN_P(data));
927 	} else {
928 		zend_string *tmp = zval_get_string_func(data);
929 		str = php_base64_encode((unsigned char*) ZSTR_VAL(tmp), ZSTR_LEN(tmp));
930 		zend_string_release_ex(tmp, 0);
931 	}
932 
933 	text = xmlNewTextLen(BAD_CAST(ZSTR_VAL(str)), ZSTR_LEN(str));
934 	xmlAddChild(ret, text);
935 	zend_string_release_ex(str, 0);
936 
937 	if (style == SOAP_ENCODED) {
938 		set_ns_and_type(ret, type);
939 	}
940 	return ret;
941 }
942 
to_xml_hexbin(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)943 static xmlNodePtr to_xml_hexbin(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
944 {
945 	static char hexconvtab[] = "0123456789ABCDEF";
946 	xmlNodePtr ret, text;
947 	unsigned char *str;
948 	zval tmp;
949 	size_t i, j;
950 
951 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
952 	xmlAddChild(parent, ret);
953 	FIND_ZVAL_NULL(data, ret, style);
954 
955 	if (Z_TYPE_P(data) != IS_STRING) {
956 		ZVAL_STR(&tmp, zval_get_string_func(data));
957 		data = &tmp;
958 	}
959 	str = (unsigned char *) safe_emalloc(Z_STRLEN_P(data) * 2, sizeof(char), 1);
960 
961 	for (i = j = 0; i < Z_STRLEN_P(data); i++) {
962 		str[j++] = hexconvtab[((unsigned char)Z_STRVAL_P(data)[i]) >> 4];
963 		str[j++] = hexconvtab[((unsigned char)Z_STRVAL_P(data)[i]) & 15];
964 	}
965 	str[j] = '\0';
966 
967 	text = xmlNewTextLen(str, Z_STRLEN_P(data) * 2 * sizeof(char));
968 	xmlAddChild(ret, text);
969 	efree(str);
970 	if (data == &tmp) {
971 		zval_ptr_dtor_str(&tmp);
972 	}
973 
974 	if (style == SOAP_ENCODED) {
975 		set_ns_and_type(ret, type);
976 	}
977 	return ret;
978 }
979 
to_zval_double(zval * ret,encodeTypePtr type,xmlNodePtr data)980 static zval *to_zval_double(zval *ret, encodeTypePtr type, xmlNodePtr data)
981 {
982 	ZVAL_NULL(ret);
983 	FIND_XML_NULL(data, ret);
984 
985 	if (data && data->children) {
986 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
987 			zend_long lval;
988 			double dval;
989 
990 			whiteSpace_collapse(data->children->content);
991 			switch (is_numeric_string((char*)data->children->content, strlen((char*)data->children->content), &lval, &dval, 0)) {
992 				case IS_LONG:
993 					ZVAL_DOUBLE(ret, lval);
994 					break;
995 				case IS_DOUBLE:
996 					ZVAL_DOUBLE(ret, dval);
997 					break;
998 				default:
999 					if (strncasecmp((char*)data->children->content, "NaN", sizeof("NaN")-1) == 0) {
1000 						ZVAL_DOUBLE(ret, php_get_nan());
1001 					} else if (strncasecmp((char*)data->children->content, "INF", sizeof("INF")-1) == 0) {
1002 						ZVAL_DOUBLE(ret, php_get_inf());
1003 					} else if (strncasecmp((char*)data->children->content, "-INF", sizeof("-INF")-1) == 0) {
1004 						ZVAL_DOUBLE(ret, -php_get_inf());
1005 					} else {
1006 						soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1007 					}
1008 			}
1009 		} else {
1010 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1011 		}
1012 	} else {
1013 		ZVAL_NULL(ret);
1014 	}
1015 	return ret;
1016 }
1017 
to_zval_long(zval * ret,encodeTypePtr type,xmlNodePtr data)1018 static zval *to_zval_long(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 			errno = 0;
1030 
1031 			switch (is_numeric_string((char*)data->children->content, strlen((char*)data->children->content), &lval, &dval, 0)) {
1032 				case IS_LONG:
1033 					ZVAL_LONG(ret, lval);
1034 					break;
1035 				case IS_DOUBLE:
1036 					ZVAL_DOUBLE(ret, dval);
1037 					break;
1038 				default:
1039 					soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1040 			}
1041 		} else {
1042 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1043 		}
1044 	} else {
1045 		ZVAL_NULL(ret);
1046 	}
1047 	return ret;
1048 }
1049 
to_xml_long(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)1050 static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
1051 {
1052 	xmlNodePtr ret;
1053 
1054 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1055 	xmlAddChild(parent, ret);
1056 	FIND_ZVAL_NULL(data, ret, style);
1057 
1058 	if (Z_TYPE_P(data) == IS_DOUBLE) {
1059 		char s[256];
1060 
1061 		snprintf(s, sizeof(s), "%0.0F",floor(Z_DVAL_P(data)));
1062 		xmlNodeSetContent(ret, BAD_CAST(s));
1063 	} else {
1064 		zend_string *str = zend_long_to_str(zval_get_long(data));
1065 		xmlNodeSetContentLen(ret, BAD_CAST(ZSTR_VAL(str)), ZSTR_LEN(str));
1066 		zend_string_release_ex(str, 0);
1067 	}
1068 
1069 	if (style == SOAP_ENCODED) {
1070 		set_ns_and_type(ret, type);
1071 	}
1072 	return ret;
1073 }
1074 
to_xml_double(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)1075 static xmlNodePtr to_xml_double(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
1076 {
1077 	xmlNodePtr ret;
1078 	zval tmp;
1079 	char *str;
1080 
1081 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1082 	xmlAddChild(parent, ret);
1083 	FIND_ZVAL_NULL(data, ret, style);
1084 
1085 	ZVAL_DOUBLE(&tmp, zval_get_double(data));
1086 
1087 	str = (char *) safe_emalloc(EG(precision) >= 0 ? EG(precision) : 17, 1, MAX_LENGTH_OF_DOUBLE + 1);
1088 	php_gcvt(Z_DVAL(tmp), EG(precision), '.', 'E', str);
1089 	xmlNodeSetContentLen(ret, BAD_CAST(str), strlen(str));
1090 	efree(str);
1091 
1092 	if (style == SOAP_ENCODED) {
1093 		set_ns_and_type(ret, type);
1094 	}
1095 	return ret;
1096 }
1097 
to_zval_bool(zval * ret,encodeTypePtr type,xmlNodePtr data)1098 static zval *to_zval_bool(zval *ret, encodeTypePtr type, xmlNodePtr data)
1099 {
1100 	ZVAL_NULL(ret);
1101 	FIND_XML_NULL(data, ret);
1102 
1103 	if (data && data->children) {
1104 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
1105 			whiteSpace_collapse(data->children->content);
1106 			if (stricmp((char*)data->children->content, "true") == 0 ||
1107 				stricmp((char*)data->children->content, "t") == 0 ||
1108 				strcmp((char*)data->children->content, "1") == 0) {
1109 				ZVAL_TRUE(ret);
1110 			} else if (stricmp((char*)data->children->content, "false") == 0 ||
1111 				stricmp((char*)data->children->content, "f") == 0 ||
1112 				strcmp((char*)data->children->content, "0") == 0) {
1113 				ZVAL_FALSE(ret);
1114 			} else {
1115 				ZVAL_STRING(ret, (char*)data->children->content);
1116 				convert_to_boolean(ret);
1117 			}
1118 		} else {
1119 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1120 		}
1121 	} else {
1122 		ZVAL_NULL(ret);
1123 	}
1124 	return ret;
1125 }
1126 
to_xml_bool(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)1127 static xmlNodePtr to_xml_bool(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
1128 {
1129 	xmlNodePtr ret;
1130 
1131 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1132 	xmlAddChild(parent, ret);
1133 	FIND_ZVAL_NULL(data, ret, style);
1134 
1135 	if (zend_is_true(data)) {
1136 		xmlNodeSetContent(ret, BAD_CAST("true"));
1137 	} else {
1138 		xmlNodeSetContent(ret, BAD_CAST("false"));
1139 	}
1140 
1141 	if (style == SOAP_ENCODED) {
1142 		set_ns_and_type(ret, type);
1143 	}
1144 	return ret;
1145 }
1146 
1147 /* Null encode/decode */
to_zval_null(zval * ret,encodeTypePtr type,xmlNodePtr data)1148 static zval *to_zval_null(zval *ret, encodeTypePtr type, xmlNodePtr data)
1149 {
1150 	ZVAL_NULL(ret);
1151 	return ret;
1152 }
1153 
to_xml_null(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)1154 static xmlNodePtr to_xml_null(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
1155 {
1156 	xmlNodePtr ret;
1157 
1158 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1159 	xmlAddChild(parent, ret);
1160 	if (style == SOAP_ENCODED) {
1161 		set_xsi_nil(ret);
1162 	}
1163 	return ret;
1164 }
1165 
set_zval_property(zval * object,char * name,zval * val)1166 static void set_zval_property(zval* object, char* name, zval* val)
1167 {
1168 	zend_update_property(Z_OBJCE_P(object), object, name, strlen(name), val);
1169 	Z_TRY_DELREF_P(val);
1170 }
1171 
get_zval_property(zval * object,char * name,zval * rv)1172 static zval* get_zval_property(zval* object, char* name, zval *rv)
1173 {
1174 	if (Z_TYPE_P(object) == IS_OBJECT) {
1175 		zval *data = zend_read_property(Z_OBJCE_P(object), object, name, strlen(name), 1, rv);
1176 		if (data == &EG(uninitialized_zval)) {
1177 			return NULL;
1178 		}
1179 		ZVAL_DEREF(data);
1180 		return data;
1181 	} else if (Z_TYPE_P(object) == IS_ARRAY) {
1182 		return zend_hash_str_find_deref(Z_ARRVAL_P(object), name, strlen(name));
1183 	}
1184 	return NULL;
1185 }
1186 
unset_zval_property(zval * object,char * name)1187 static void unset_zval_property(zval* object, char* name)
1188 {
1189 	if (Z_TYPE_P(object) == IS_OBJECT) {
1190 		zend_unset_property(Z_OBJCE_P(object), object, name, strlen(name));
1191 	} else if (Z_TYPE_P(object) == IS_ARRAY) {
1192 		zend_hash_str_del(Z_ARRVAL_P(object), name, strlen(name));
1193 	}
1194 }
1195 
model_to_zval_any(zval * ret,xmlNodePtr node)1196 static void model_to_zval_any(zval *ret, xmlNodePtr node)
1197 {
1198 	zval rv, arr, val, keepVal;
1199 	zval* any = NULL;
1200 	char* name = NULL;
1201 
1202 	while (node != NULL) {
1203 		if (get_zval_property(ret, (char*)node->name, &rv) == NULL) {
1204 
1205 			ZVAL_NULL(&val);
1206 			master_to_zval(&val, get_conversion(XSD_ANYXML), node);
1207 
1208 			if (any && Z_TYPE_P(any) != IS_ARRAY) {
1209 				/* Convert into array */
1210 				array_init(&arr);
1211 				if (name) {
1212 					add_assoc_zval(&arr, name, any);
1213 				} else {
1214 					add_next_index_zval(&arr, any);
1215 				}
1216 				any = &arr;
1217 			}
1218 
1219 			if (Z_TYPE(val) == IS_STRING && *Z_STRVAL(val) == '<') {
1220 				name = NULL;
1221 				while (node->next != NULL) {
1222 					zval val2;
1223 
1224 					ZVAL_NULL(&val2);
1225 					master_to_zval(&val2, get_conversion(XSD_ANYXML), node->next);
1226 					if (Z_TYPE(val2) != IS_STRING ||  *Z_STRVAL(val) != '<') {
1227 						Z_TRY_DELREF(val2);
1228 						break;
1229 					}
1230 					concat_function(&val, &val, &val2);
1231 					zval_ptr_dtor(&val2);
1232 					node = node->next;
1233 				}
1234 			} else {
1235 				name = (char*)node->name;
1236 			}
1237 
1238 			if (any == NULL) {
1239 				if (name) {
1240 					/* Convert into array */
1241 					array_init(&arr);
1242 					add_assoc_zval(&arr, name, &val);
1243 					any = &arr;
1244 					name = NULL;
1245 				} else {
1246 					ZVAL_COPY_VALUE(&keepVal, &val);
1247 					any = &keepVal;
1248 				}
1249 			} else {
1250 				/* Add array element */
1251 				if (name) {
1252 					zval *el;
1253 					if ((el = zend_hash_str_find(Z_ARRVAL_P(any), name, strlen(name))) != NULL) {
1254 						if (Z_TYPE_P(el) != IS_ARRAY) {
1255 							/* Convert into array */
1256 							array_init(&arr);
1257 							add_next_index_zval(&arr, el);
1258 							el = &arr;
1259 						}
1260 						add_next_index_zval(el, &val);
1261 					} else {
1262 						add_assoc_zval(any, name, &val);
1263 					}
1264 				} else {
1265 					add_next_index_zval(any, &val);
1266 				}
1267 				name = NULL;
1268 			}
1269 		}
1270 		node = node->next;
1271 	}
1272 	if (any) {
1273 		set_zval_property(ret, name ? name : "any", any);
1274 	}
1275 }
1276 
model_to_zval_object(zval * ret,sdlContentModelPtr model,xmlNodePtr data,sdlPtr sdl)1277 static void model_to_zval_object(zval *ret, sdlContentModelPtr model, xmlNodePtr data, sdlPtr sdl)
1278 {
1279 	switch (model->kind) {
1280 		case XSD_CONTENT_ELEMENT:
1281 			if (model->u.element->name) {
1282 				xmlNodePtr node = get_node(data->children, model->u.element->name);
1283 
1284 				if (node) {
1285 					zval val;
1286 					xmlNodePtr r_node;
1287 
1288 					ZVAL_NULL(&val);
1289 					r_node = check_and_resolve_href(node);
1290 					if (r_node && r_node->children && r_node->children->content) {
1291 						if (model->u.element->fixed && strcmp(model->u.element->fixed, (char*)r_node->children->content) != 0) {
1292 							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);
1293 						}
1294 						master_to_zval(&val, model->u.element->encode, r_node);
1295 					} else if (model->u.element->fixed) {
1296 						xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1297 						xmlNodeSetContent(dummy, BAD_CAST(model->u.element->fixed));
1298 						master_to_zval(&val, model->u.element->encode, dummy);
1299 						xmlFreeNode(dummy);
1300 					} else if (model->u.element->def && !model->u.element->nillable) {
1301 						xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1302 						xmlNodeSetContent(dummy, BAD_CAST(model->u.element->def));
1303 						master_to_zval(&val, model->u.element->encode, dummy);
1304 						xmlFreeNode(dummy);
1305 					} else {
1306 						master_to_zval(&val, model->u.element->encode, r_node);
1307 					}
1308 					if ((node = get_node(node->next, model->u.element->name)) != NULL) {
1309 						zval array;
1310 
1311 						array_init(&array);
1312 						add_next_index_zval(&array, &val);
1313 						do {
1314 							ZVAL_NULL(&val);
1315 							if (node && node->children && node->children->content) {
1316 								if (model->u.element->fixed && strcmp(model->u.element->fixed, (char*)node->children->content) != 0) {
1317 									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);
1318 								}
1319 								master_to_zval(&val, model->u.element->encode, node);
1320 							} else if (model->u.element->fixed) {
1321 								xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1322 								xmlNodeSetContent(dummy, BAD_CAST(model->u.element->fixed));
1323 								master_to_zval(&val, model->u.element->encode, dummy);
1324 								xmlFreeNode(dummy);
1325 							} else if (model->u.element->def && !model->u.element->nillable) {
1326 								xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1327 								xmlNodeSetContent(dummy, BAD_CAST(model->u.element->def));
1328 								master_to_zval(&val, model->u.element->encode, dummy);
1329 								xmlFreeNode(dummy);
1330 							} else {
1331 								master_to_zval(&val, model->u.element->encode, node);
1332 							}
1333 							add_next_index_zval(&array, &val);
1334 						} while ((node = get_node(node->next, model->u.element->name)) != NULL);
1335 						ZVAL_COPY_VALUE(&val, &array);
1336 					} else if ((Z_TYPE(val) != IS_NULL || !model->u.element->nillable) &&
1337 					           (SOAP_GLOBAL(features) & SOAP_SINGLE_ELEMENT_ARRAYS) &&
1338 					           (model->max_occurs == -1 || model->max_occurs > 1)) {
1339 						zval array;
1340 
1341 						array_init(&array);
1342 						add_next_index_zval(&array, &val);
1343 						ZVAL_COPY_VALUE(&val, &array);
1344 					}
1345 					set_zval_property(ret, model->u.element->name, &val);
1346 				}
1347 			}
1348 			break;
1349 		case XSD_CONTENT_ALL:
1350 		case XSD_CONTENT_SEQUENCE:
1351 		case XSD_CONTENT_CHOICE: {
1352 			sdlContentModelPtr tmp;
1353 			sdlContentModelPtr any = NULL;
1354 
1355 			ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
1356 				if (tmp->kind == XSD_CONTENT_ANY) {
1357 					any = tmp;
1358 				} else {
1359 					model_to_zval_object(ret, tmp, data, sdl);
1360 				}
1361 			} ZEND_HASH_FOREACH_END();
1362 			if (any) {
1363 				model_to_zval_any(ret, data->children);
1364 			}
1365 			break;
1366 		}
1367 		case XSD_CONTENT_GROUP:
1368 			model_to_zval_object(ret, model->u.group->model, data, sdl);
1369 			break;
1370 		default:
1371 		  break;
1372 	}
1373 }
1374 
1375 /* Struct encode/decode */
to_zval_object_ex(zval * ret,encodeTypePtr type,xmlNodePtr data,zend_class_entry * pce)1376 static zval *to_zval_object_ex(zval *ret, encodeTypePtr type, xmlNodePtr data, zend_class_entry *pce)
1377 {
1378 	xmlNodePtr trav;
1379 	sdlPtr sdl;
1380 	sdlTypePtr sdlType = type->sdl_type;
1381 	zend_class_entry *ce = ZEND_STANDARD_CLASS_DEF_PTR;
1382 	zval *redo_any = NULL, rv, arr;
1383 
1384 	if (pce) {
1385 		ce = pce;
1386 	} else if (SOAP_GLOBAL(class_map) && type->type_str) {
1387 		zval              *classname;
1388 		zend_class_entry  *tmp;
1389 
1390 		if ((classname = zend_hash_str_find_deref(SOAP_GLOBAL(class_map), type->type_str, strlen(type->type_str))) != NULL &&
1391 		    Z_TYPE_P(classname) == IS_STRING &&
1392 		    (tmp = zend_fetch_class(Z_STR_P(classname), ZEND_FETCH_CLASS_AUTO)) != NULL) {
1393 			ce = tmp;
1394 		}
1395 	}
1396 	sdl = SOAP_GLOBAL(sdl);
1397 	if (sdlType) {
1398 		if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
1399 		    sdlType->encode && type != &sdlType->encode->details) {
1400 			encodePtr enc;
1401 
1402 			enc = sdlType->encode;
1403 			while (enc && enc->details.sdl_type &&
1404 			       enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1405 			       enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1406 			       enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1407 				enc = enc->details.sdl_type->encode;
1408 			}
1409 			if (enc) {
1410 				zval base;
1411 
1412 				ZVAL_NULL(ret);
1413 				if (soap_check_xml_ref(ret, data)) {
1414 					return ret;
1415 				}
1416 
1417 				object_init_ex(ret, ce);
1418 				master_to_zval_int(&base, enc, data);
1419 				set_zval_property(ret, "_", &base);
1420 			} else {
1421 				ZVAL_NULL(ret);
1422 				FIND_XML_NULL(data, ret);
1423 				if (soap_check_xml_ref(ret, data)) {
1424 					return ret;
1425 				}
1426 				object_init_ex(ret, ce);
1427 				soap_add_xml_ref(ret, data);
1428 			}
1429 		} else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
1430 		           sdlType->encode &&
1431 		           type != &sdlType->encode->details) {
1432 			if (sdlType->encode->details.sdl_type &&
1433 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1434 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1435 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1436 
1437 				CHECK_XML_NULL(data);
1438 				if (soap_check_xml_ref(ret, data)) {
1439 					return ret;
1440 				}
1441 
1442 			    if (ce != ZEND_STANDARD_CLASS_DEF_PTR &&
1443 			        sdlType->encode->to_zval == sdl_guess_convert_zval &&
1444 			        sdlType->encode->details.sdl_type != NULL &&
1445 			        (sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_COMPLEX ||
1446 			         sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_RESTRICTION ||
1447 			         sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_EXTENSION) &&
1448 			        (sdlType->encode->details.sdl_type->encode == NULL ||
1449 			         (sdlType->encode->details.sdl_type->encode->details.type != IS_ARRAY &&
1450 			          sdlType->encode->details.sdl_type->encode->details.type != SOAP_ENC_ARRAY))) {
1451 					to_zval_object_ex(ret, &sdlType->encode->details, data, ce);
1452 			    } else {
1453 					master_to_zval_int(ret, sdlType->encode, data);
1454 				}
1455 
1456 				soap_add_xml_ref(ret, data);
1457 
1458 				redo_any = get_zval_property(ret, "any", &rv);
1459 				if (Z_TYPE_P(ret) == IS_OBJECT && ce != ZEND_STANDARD_CLASS_DEF_PTR) {
1460 					zend_object *zobj = Z_OBJ_P(ret);
1461 					zobj->ce = ce;
1462 				}
1463 			} else {
1464 				zval base;
1465 
1466 				ZVAL_NULL(ret);
1467 				if (soap_check_xml_ref(ret, data)) {
1468 					return ret;
1469 				}
1470 
1471 				object_init_ex(ret, ce);
1472 				soap_add_xml_ref(ret, data);
1473 				master_to_zval_int(&base, sdlType->encode, data);
1474 				set_zval_property(ret, "_", &base);
1475 			}
1476 		} else {
1477 			ZVAL_NULL(ret);
1478 			FIND_XML_NULL(data, ret);
1479 			if (soap_check_xml_ref(ret, data)) {
1480 				return ret;
1481 			}
1482 			object_init_ex(ret, ce);
1483 			soap_add_xml_ref(ret, data);
1484 		}
1485 		if (sdlType->model) {
1486 			model_to_zval_object(ret, sdlType->model, data, sdl);
1487 			if (redo_any) {
1488 				if (!get_zval_property(ret, "any", &rv)) {
1489 					model_to_zval_any(ret, data->children);
1490 					soap_add_xml_ref(ret, data);
1491 				} else {
1492 					unset_zval_property(ret, "any");
1493 				}
1494 			}
1495 		}
1496 		if (sdlType->attributes) {
1497 			sdlAttributePtr attr;
1498 
1499 			ZEND_HASH_FOREACH_PTR(sdlType->attributes, attr) {
1500 				if (attr->name) {
1501 					xmlAttrPtr val = get_attribute(data->properties, attr->name);
1502 					char *str_val = NULL;
1503 
1504 					if (val && val->children && val->children->content) {
1505 						str_val = (char*)val->children->content;
1506 						if (attr->fixed && strcmp(attr->fixed, str_val) != 0) {
1507 							soap_error3(E_ERROR, "Encoding: Attribute '%s' has fixed value '%s' (value '%s' is not allowed)", attr->name, attr->fixed, str_val);
1508 						}
1509 					} else if (attr->fixed) {
1510 						str_val = attr->fixed;
1511 					} else if (attr->def) {
1512 						str_val = attr->def;
1513 					}
1514 					if (str_val) {
1515 						xmlNodePtr dummy, text;
1516 						zval data;
1517 
1518 						dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1519 						text = xmlNewText(BAD_CAST(str_val));
1520 						xmlAddChild(dummy, text);
1521 						ZVAL_NULL(&data);
1522 						/* TODO: There are other places using dummy nodes -- generalize? */
1523 						zend_try {
1524 							master_to_zval(&data, attr->encode, dummy);
1525 						} zend_catch {
1526 							xmlFreeNode(dummy);
1527 							zend_bailout();
1528 						} zend_end_try();
1529 						xmlFreeNode(dummy);
1530 						set_zval_property(ret, attr->name, &data);
1531 					}
1532 				}
1533 			} ZEND_HASH_FOREACH_END();
1534 		}
1535 	} else {
1536 		ZVAL_NULL(ret);
1537 		FIND_XML_NULL(data, ret);
1538 		if (soap_check_xml_ref(ret, data)) {
1539 			return ret;
1540 		}
1541 
1542 		object_init_ex(ret, ce);
1543 		soap_add_xml_ref(ret, data);
1544 		trav = data->children;
1545 
1546 		while (trav != NULL) {
1547 			if (trav->type == XML_ELEMENT_NODE) {
1548 				zval  tmpVal, rv;
1549 				zval *prop;
1550 
1551 				ZVAL_NULL(&tmpVal);
1552 				master_to_zval(&tmpVal, NULL, trav);
1553 
1554 				prop = get_zval_property(ret, (char*)trav->name, &rv);
1555 				if (!prop) {
1556 					if (!trav->next || !get_node(trav->next, (char*)trav->name)) {
1557 						set_zval_property(ret, (char*)trav->name, &tmpVal);
1558 					} else {
1559 						zval arr;
1560 
1561 						array_init(&arr);
1562 						add_next_index_zval(&arr, &tmpVal);
1563 						set_zval_property(ret, (char*)trav->name, &arr);
1564 					}
1565 				} else {
1566 					/* Property already exist - make array */
1567 					if (Z_TYPE_P(prop) != IS_ARRAY) {
1568 						/* Convert into array */
1569 						array_init(&arr);
1570 						Z_ADDREF_P(prop);
1571 						add_next_index_zval(&arr, prop);
1572 						set_zval_property(ret, (char*)trav->name, &arr);
1573 						prop = &arr;
1574 					}
1575 					/* Add array element */
1576 					add_next_index_zval(prop, &tmpVal);
1577 				}
1578 			}
1579 			trav = trav->next;
1580 		}
1581 	}
1582 	return ret;
1583 }
1584 
to_zval_object(zval * ret,encodeTypePtr type,xmlNodePtr data)1585 static zval *to_zval_object(zval *ret, encodeTypePtr type, xmlNodePtr data)
1586 {
1587 	return to_zval_object_ex(ret, type, data, NULL);
1588 }
1589 
1590 
model_to_xml_object(xmlNodePtr node,sdlContentModelPtr model,zval * object,int style,int strict)1591 static int model_to_xml_object(xmlNodePtr node, sdlContentModelPtr model, zval *object, int style, int strict)
1592 {
1593 	switch (model->kind) {
1594 		case XSD_CONTENT_ELEMENT: {
1595 			zval *data;
1596 			xmlNodePtr property;
1597 			encodePtr enc;
1598 			zval rv;
1599 
1600 			data = get_zval_property(object, model->u.element->name, &rv);
1601 			if (data &&
1602 			    Z_TYPE_P(data) == IS_NULL &&
1603 			    !model->u.element->nillable &&
1604 			    model->min_occurs > 0 &&
1605 			    !strict) {
1606 				return 0;
1607 			}
1608 			if (data) {
1609 				enc = model->u.element->encode;
1610 				if ((model->max_occurs == -1 || model->max_occurs > 1) &&
1611 				    Z_TYPE_P(data) == IS_ARRAY &&
1612 				    !is_map(data)) {
1613 					HashTable *ht = Z_ARRVAL_P(data);
1614 					zval *val;
1615 
1616 					ZEND_HASH_FOREACH_VAL(ht, val) {
1617 						ZVAL_DEREF(val);
1618 						if (Z_TYPE_P(val) == IS_NULL && model->u.element->nillable) {
1619 							property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1620 							xmlAddChild(node, property);
1621 							set_xsi_nil(property);
1622 						} else {
1623 							property = master_to_xml(enc, val, style, node);
1624 							if (property->children && property->children->content &&
1625 							    model->u.element->fixed && strcmp(model->u.element->fixed, (char*)property->children->content) != 0) {
1626 								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);
1627 							}
1628 						}
1629 						xmlNodeSetName(property, BAD_CAST(model->u.element->name));
1630 						if (style == SOAP_LITERAL &&
1631 						    model->u.element->namens &&
1632 						    model->u.element->form == XSD_FORM_QUALIFIED) {
1633 							xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1634 							xmlSetNs(property, nsp);
1635 						}
1636 					} ZEND_HASH_FOREACH_END();
1637 				} else {
1638 					if (Z_TYPE_P(data) == IS_NULL && model->u.element->nillable) {
1639 						property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1640 						xmlAddChild(node, property);
1641 						set_xsi_nil(property);
1642 					} else if (Z_TYPE_P(data) == IS_NULL && model->min_occurs == 0) {
1643 						return 1;
1644 					} else {
1645 						property = master_to_xml(enc, data, style, node);
1646 						if (property->children && property->children->content &&
1647 						    model->u.element->fixed && strcmp(model->u.element->fixed, (char*)property->children->content) != 0) {
1648 							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);
1649 						}
1650 					}
1651 					xmlNodeSetName(property, BAD_CAST(model->u.element->name));
1652 					if (style == SOAP_LITERAL &&
1653 					    model->u.element->namens &&
1654 					    model->u.element->form == XSD_FORM_QUALIFIED) {
1655 						xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1656 						xmlSetNs(property, nsp);
1657 					}
1658 				}
1659 				return 1;
1660 			} else if (strict && model->u.element->nillable && model->min_occurs > 0) {
1661 				property = xmlNewNode(NULL, BAD_CAST(model->u.element->name));
1662 				xmlAddChild(node, property);
1663 				set_xsi_nil(property);
1664 				if (style == SOAP_LITERAL &&
1665 				    model->u.element->namens &&
1666 				    model->u.element->form == XSD_FORM_QUALIFIED) {
1667 					xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1668 					xmlSetNs(property, nsp);
1669 				}
1670 				return 1;
1671 			} else if (model->min_occurs == 0) {
1672 				return 2;
1673 			} else {
1674 				if (strict) {
1675 					soap_error1(E_ERROR,  "Encoding: object has no '%s' property", model->u.element->name);
1676 				}
1677 				return 0;
1678 			}
1679 			break;
1680 		}
1681 		case XSD_CONTENT_ANY: {
1682 			zval *data;
1683 			encodePtr enc;
1684 			zval rv;
1685 
1686 			data = get_zval_property(object, "any", &rv);
1687 			if (data) {
1688 				enc = get_conversion(XSD_ANYXML);
1689 				if ((model->max_occurs == -1 || model->max_occurs > 1) &&
1690 				    Z_TYPE_P(data) == IS_ARRAY &&
1691 				    !is_map(data)) {
1692 					HashTable *ht = Z_ARRVAL_P(data);
1693 					zval *val;
1694 
1695 					ZEND_HASH_FOREACH_VAL(ht, val) {
1696 						master_to_xml(enc, val, style, node);
1697 					} ZEND_HASH_FOREACH_END();
1698 				} else {
1699 					master_to_xml(enc, data, style, node);
1700 				}
1701 				return 1;
1702 			} else if (model->min_occurs == 0) {
1703 				return 2;
1704 			} else {
1705 				if (strict) {
1706 					soap_error0(E_ERROR,  "Encoding: object has no 'any' property");
1707 				}
1708 				return 0;
1709 			}
1710 			break;
1711 		}
1712 		case XSD_CONTENT_SEQUENCE:
1713 		case XSD_CONTENT_ALL: {
1714 			sdlContentModelPtr tmp;
1715 
1716 			ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
1717 				if (!model_to_xml_object(node, tmp, object, style, strict && (tmp->min_occurs > 0))) {
1718 					if (!strict || tmp->min_occurs > 0) {
1719 						return 0;
1720 					}
1721 				}
1722 				strict = 1;
1723 			} ZEND_HASH_FOREACH_END();
1724 			return 1;
1725 		}
1726 		case XSD_CONTENT_CHOICE: {
1727 			sdlContentModelPtr tmp;
1728 			int ret = 0;
1729 
1730 			ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
1731 				int tmp_ret = model_to_xml_object(node, tmp, object, style, 0);
1732 				if (tmp_ret == 1) {
1733 					return 1;
1734 				} else if (tmp_ret != 0) {
1735 					ret = 1;
1736 				}
1737 			} ZEND_HASH_FOREACH_END();
1738 			return ret;
1739 		}
1740 		case XSD_CONTENT_GROUP: {
1741 			return model_to_xml_object(node, model->u.group->model, object, style, strict && model->min_occurs > 0);
1742 		}
1743 		default:
1744 		  break;
1745 	}
1746 	return 1;
1747 }
1748 
model_array_element(sdlContentModelPtr model)1749 static sdlTypePtr model_array_element(sdlContentModelPtr model)
1750 {
1751 	switch (model->kind) {
1752 		case XSD_CONTENT_ELEMENT: {
1753 			if (model->max_occurs == -1 || model->max_occurs > 1) {
1754 			  return model->u.element;
1755 			} else {
1756 			  return NULL;
1757 			}
1758 		}
1759 		case XSD_CONTENT_SEQUENCE:
1760 		case XSD_CONTENT_ALL:
1761 		case XSD_CONTENT_CHOICE: {
1762 			sdlContentModelPtr tmp;
1763 
1764 			if (zend_hash_num_elements(model->u.content) != 1) {
1765 			  return NULL;
1766 			}
1767 			ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
1768 				return model_array_element(tmp);
1769 			} ZEND_HASH_FOREACH_END();
1770 		}
1771 		case XSD_CONTENT_GROUP: {
1772 			return model_array_element(model->u.group->model);
1773 		}
1774 		default:
1775 		  break;
1776 	}
1777 	return NULL;
1778 }
1779 
to_xml_object(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)1780 static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
1781 {
1782 	xmlNodePtr xmlParam;
1783 	HashTable *prop = NULL;
1784 	sdlTypePtr sdlType = type->sdl_type;
1785 
1786 	if (!data || Z_TYPE_P(data) == IS_NULL) {
1787 		xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1788 		xmlAddChild(parent, xmlParam);
1789 		if (style == SOAP_ENCODED) {
1790 			set_xsi_nil(xmlParam);
1791 			set_ns_and_type(xmlParam, type);
1792 		}
1793 		return xmlParam;
1794 	}
1795 
1796 	if (Z_TYPE_P(data) == IS_OBJECT) {
1797 		prop = Z_OBJPROP_P(data);
1798 	} else if (Z_TYPE_P(data) == IS_ARRAY) {
1799 		prop = Z_ARRVAL_P(data);
1800 	}
1801 
1802 	if (sdlType) {
1803 		if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
1804 		    sdlType->encode && type != &sdlType->encode->details) {
1805 			encodePtr enc;
1806 
1807 			enc = sdlType->encode;
1808 			while (enc && enc->details.sdl_type &&
1809 			       enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1810 			       enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1811 			       enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1812 				enc = enc->details.sdl_type->encode;
1813 			}
1814 			if (enc) {
1815 				zval rv;
1816 				zval *tmp = get_zval_property(data, "_", &rv);
1817 				if (tmp) {
1818 					xmlParam = master_to_xml(enc, tmp, style, parent);
1819 				} else if (prop == NULL) {
1820 					xmlParam = master_to_xml(enc, data, style, parent);
1821 				} else {
1822 					xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1823 					xmlAddChild(parent, xmlParam);
1824 				}
1825 			} else {
1826 				xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1827 				xmlAddChild(parent, xmlParam);
1828 			}
1829 		} else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
1830 		           sdlType->encode && type != &sdlType->encode->details) {
1831 			if (sdlType->encode->details.sdl_type &&
1832 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1833 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1834 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1835 
1836 				if (prop) { GC_TRY_PROTECT_RECURSION(prop); }
1837 				xmlParam = master_to_xml(sdlType->encode, data, style, parent);
1838 				if (prop) { GC_TRY_UNPROTECT_RECURSION(prop); }
1839 			} else {
1840 				zval rv;
1841 				zval *tmp = get_zval_property(data, "_", &rv);
1842 
1843 				if (tmp) {
1844 					xmlParam = master_to_xml(sdlType->encode, tmp, style, parent);
1845 				} else if (prop == NULL) {
1846 					xmlParam = master_to_xml(sdlType->encode, data, style, parent);
1847 				} else {
1848 					xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1849 					xmlAddChild(parent, xmlParam);
1850 				}
1851 			}
1852 		} else {
1853 			xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1854 			xmlAddChild(parent, xmlParam);
1855 		}
1856 
1857 		if (soap_check_zval_ref(data, xmlParam)) {
1858 			return xmlParam;
1859 		}
1860 		if (prop != NULL) {
1861 			sdlTypePtr array_el;
1862 
1863 			if (Z_TYPE_P(data) == IS_ARRAY &&
1864 		      !is_map(data) &&
1865 		      sdlType->attributes == NULL &&
1866 		      sdlType->model != NULL &&
1867 		      (array_el = model_array_element(sdlType->model)) != NULL) {
1868 				zval *val;
1869 
1870 				ZEND_HASH_FOREACH_VAL(prop, val) {
1871 					xmlNodePtr property;
1872 					ZVAL_DEREF(val);
1873 					if (Z_TYPE_P(val) == IS_NULL && array_el->nillable) {
1874 						property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1875 						xmlAddChild(xmlParam, property);
1876 						set_xsi_nil(property);
1877 					} else {
1878 						property = master_to_xml(array_el->encode, val, style, xmlParam);
1879 					}
1880 					xmlNodeSetName(property, BAD_CAST(array_el->name));
1881 					if (style == SOAP_LITERAL &&
1882 					   array_el->namens &&
1883 					   array_el->form == XSD_FORM_QUALIFIED) {
1884 						xmlNsPtr nsp = encode_add_ns(property, array_el->namens);
1885 						xmlSetNs(property, nsp);
1886 					}
1887 				} ZEND_HASH_FOREACH_END();
1888 			} else if (sdlType->model) {
1889 				model_to_xml_object(xmlParam, sdlType->model, data, style, 1);
1890 			}
1891 			if (sdlType->attributes) {
1892 				sdlAttributePtr attr;
1893 				zval *zattr, rv;
1894 
1895 				ZEND_HASH_FOREACH_PTR(sdlType->attributes, attr) {
1896 					if (attr->name) {
1897 						zattr = get_zval_property(data, attr->name, &rv);
1898 						if (zattr) {
1899 							xmlNodePtr dummy;
1900 
1901 							dummy = master_to_xml(attr->encode, zattr, SOAP_LITERAL, xmlParam);
1902 							if (dummy->children && dummy->children->content) {
1903 								if (attr->fixed && strcmp(attr->fixed, (char*)dummy->children->content) != 0) {
1904 									soap_error3(E_ERROR, "Encoding: Attribute '%s' has fixed value '%s' (value '%s' is not allowed)", attr->name, attr->fixed, dummy->children->content);
1905 								}
1906 								/* we need to handle xml: namespace specially, since it is
1907 								   an implicit schema. Otherwise, use form.
1908 								*/
1909 								if (attr->namens &&
1910 								    (!strncmp(attr->namens, XML_NAMESPACE, sizeof(XML_NAMESPACE)) ||
1911 								     attr->form == XSD_FORM_QUALIFIED)) {
1912 									xmlNsPtr nsp = encode_add_ns(xmlParam, attr->namens);
1913 
1914 									xmlSetNsProp(xmlParam, nsp, BAD_CAST(attr->name), dummy->children->content);
1915 								} else {
1916 									xmlSetProp(xmlParam, BAD_CAST(attr->name), dummy->children->content);
1917 								}
1918 							}
1919 							xmlUnlinkNode(dummy);
1920 							xmlFreeNode(dummy);
1921 						}
1922 					}
1923 				} ZEND_HASH_FOREACH_END();
1924 			}
1925 		}
1926 		if (style == SOAP_ENCODED) {
1927 			set_ns_and_type(xmlParam, type);
1928 		}
1929 	} else {
1930 		xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1931 		xmlAddChild(parent, xmlParam);
1932 
1933 		if (soap_check_zval_ref(data, xmlParam)) {
1934 			return xmlParam;
1935 		}
1936 		if (prop != NULL) {
1937 			zval *zprop;
1938 			zend_string *str_key;
1939 			xmlNodePtr property;
1940 
1941 			ZEND_HASH_FOREACH_STR_KEY_VAL_IND(prop, str_key, zprop) {
1942 				ZVAL_DEREF(zprop);
1943 				property = master_to_xml(get_conversion(Z_TYPE_P(zprop)), zprop, style, xmlParam);
1944 
1945 				if (str_key) {
1946 					const char *prop_name;
1947 
1948 					if (Z_TYPE_P(data) == IS_OBJECT) {
1949 						const char *class_name;
1950 
1951 						zend_unmangle_property_name(str_key, &class_name, &prop_name);
1952 					} else {
1953 						prop_name = ZSTR_VAL(str_key);
1954 					}
1955 					if (prop_name) {
1956 						xmlNodeSetName(property, BAD_CAST(prop_name));
1957 					}
1958 				}
1959 			} ZEND_HASH_FOREACH_END();
1960 		}
1961 		if (style == SOAP_ENCODED) {
1962 			set_ns_and_type(xmlParam, type);
1963 		}
1964 	}
1965 	return xmlParam;
1966 }
1967 
1968 /* Array encode/decode */
guess_array_map(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)1969 static xmlNodePtr guess_array_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
1970 {
1971 	encodePtr enc = NULL;
1972 
1973 	if (data && Z_TYPE_P(data) == IS_ARRAY) {
1974 		if (is_map(data)) {
1975 			enc = get_conversion(APACHE_MAP);
1976 		} else {
1977 			enc = get_conversion(SOAP_ENC_ARRAY);
1978 		}
1979 	}
1980 	if (!enc) {
1981 		enc = get_conversion(IS_NULL);
1982 	}
1983 
1984 	return master_to_xml(enc, data, style, parent);
1985 }
1986 
calc_dimension_12(const char * str)1987 static int calc_dimension_12(const char* str)
1988 {
1989 	int i = 0, flag = 0;
1990 	while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
1991 		str++;
1992 	}
1993 	if (*str == '*') {
1994 		i++;
1995 		str++;
1996 	}
1997 	while (*str != '\0') {
1998 		if (*str >= '0' && *str <= '9') {
1999 			if (flag == 0) {
2000 	   		i++;
2001 	   		flag = 1;
2002 	   	}
2003 	  } else if (*str == '*') {
2004 			soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
2005 		} else {
2006 			flag = 0;
2007 		}
2008 		str++;
2009 	}
2010 	return i;
2011 }
2012 
get_position_12(int dimension,const char * str)2013 static int* get_position_12(int dimension, const char* str)
2014 {
2015 	int *pos;
2016 	int i = -1, flag = 0;
2017 
2018 	pos = safe_emalloc(sizeof(int), dimension, 0);
2019 	memset(pos,0,sizeof(int)*dimension);
2020 	while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
2021 		str++;
2022 	}
2023 	if (*str == '*') {
2024 		str++;
2025 		i++;
2026 	}
2027 	while (*str != '\0') {
2028 		if (*str >= '0' && *str <= '9') {
2029 			if (flag == 0) {
2030 				i++;
2031 				flag = 1;
2032 			}
2033 			pos[i] = (pos[i]*10)+(*str-'0');
2034 		} else if (*str == '*') {
2035 			soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
2036 		} else {
2037 		  flag = 0;
2038 		}
2039 		str++;
2040 	}
2041 	return pos;
2042 }
2043 
calc_dimension(const char * str)2044 static int calc_dimension(const char* str)
2045 {
2046 	int i = 1;
2047 	while (*str != ']' && *str != '\0') {
2048 		if (*str == ',') {
2049     		i++;
2050 		}
2051 		str++;
2052 	}
2053 	return i;
2054 }
2055 
get_position_ex(int dimension,const char * str,int ** pos)2056 static void get_position_ex(int dimension, const char* str, int** pos)
2057 {
2058 	int i = 0;
2059 
2060 	memset(*pos,0,sizeof(int)*dimension);
2061 	while (*str != ']' && *str != '\0' && i < dimension) {
2062 		if (*str >= '0' && *str <= '9') {
2063 			(*pos)[i] = ((*pos)[i]*10)+(*str-'0');
2064 		} else if (*str == ',') {
2065 			i++;
2066 		}
2067 		str++;
2068 	}
2069 }
2070 
get_position(int dimension,const char * str)2071 static int* get_position(int dimension, const char* str)
2072 {
2073 	int *pos;
2074 
2075 	pos = safe_emalloc(sizeof(int), dimension, 0);
2076 	get_position_ex(dimension, str, &pos);
2077 	return pos;
2078 }
2079 
add_xml_array_elements(xmlNodePtr xmlParam,sdlTypePtr type,encodePtr enc,xmlNsPtr ns,int dimension,int * dims,zval * data,int style)2080 static void add_xml_array_elements(xmlNodePtr xmlParam,
2081                                    sdlTypePtr type,
2082                                    encodePtr enc,
2083                                    xmlNsPtr ns,
2084                                    int dimension ,
2085                                    int* dims,
2086                                    zval* data,
2087                                    int style
2088                                   )
2089 {
2090 	int j = 0;
2091 	zval *zdata;
2092 	xmlNodePtr xparam;
2093 
2094 	if (data && Z_TYPE_P(data) == IS_ARRAY) {
2095 		ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(data), zdata) {
2096 	 		if (j >= dims[0]) {
2097 	 			break;
2098 	 		}
2099 			ZVAL_DEREF(zdata);
2100  			if (dimension == 1) {
2101  				if (enc == NULL) {
2102 					xparam = master_to_xml(get_conversion(Z_TYPE_P(zdata)), zdata, style, xmlParam);
2103 				} else {
2104 					xparam = master_to_xml(enc, zdata, style, xmlParam);
2105 	 			}
2106 
2107 	 			if (type) {
2108  					xmlNodeSetName(xparam, BAD_CAST(type->name));
2109  				} else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
2110 					xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
2111 					xmlSetNs(xparam, ns);
2112 				} else {
2113  					xmlNodeSetName(xparam, BAD_CAST("item"));
2114  				}
2115  			} else {
2116  				add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, zdata, style);
2117  			}
2118  			j++;
2119  		} ZEND_HASH_FOREACH_END();
2120 
2121 		if (dimension == 1) {
2122 	 		while (j < dims[0]) {
2123 				xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2124 				xmlAddChild(xmlParam, xparam);
2125 
2126 	 			if (type) {
2127  					xmlNodeSetName(xparam, BAD_CAST(type->name));
2128  				} else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
2129 					xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
2130 					xmlSetNs(xparam, ns);
2131 				} else {
2132  					xmlNodeSetName(xparam, BAD_CAST("item"));
2133  				}
2134 
2135  				j++;
2136  			}
2137 		} else {
2138 	 		while (j < dims[0]) {
2139 				add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style);
2140  				j++;
2141  			}
2142 		}
2143  	} else {
2144 		for (j=0; j<dims[0]; j++) {
2145  			if (dimension == 1) {
2146 	 			xmlNodePtr xparam;
2147 
2148 				xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2149 				xmlAddChild(xmlParam, xparam);
2150 	 			if (type) {
2151  					xmlNodeSetName(xparam, BAD_CAST(type->name));
2152  				} else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
2153 					xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
2154 					xmlSetNs(xparam, ns);
2155 				} else {
2156  					xmlNodeSetName(xparam, BAD_CAST("item"));
2157  				}
2158  			} else {
2159  			  add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style);
2160  			}
2161 		}
2162  	}
2163 }
2164 
to_xml_array(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2165 static xmlNodePtr to_xml_array(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2166 {
2167 	sdlTypePtr sdl_type = type->sdl_type;
2168 	sdlTypePtr element_type = NULL;
2169 	smart_str array_type = {0}, array_size = {0};
2170 	int i;
2171 	xmlNodePtr xmlParam;
2172 	encodePtr enc = NULL;
2173 	int dimension = 1;
2174 	int* dims;
2175 	int soap_version;
2176 	zval array_copy;
2177 
2178 	ZVAL_UNDEF(&array_copy);
2179 	soap_version = SOAP_GLOBAL(soap_version);
2180 
2181 	xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2182 	xmlAddChild(parent, xmlParam);
2183 
2184 	if (!data || Z_TYPE_P(data) == IS_NULL) {
2185 		if (style == SOAP_ENCODED) {
2186 			set_xsi_nil(xmlParam);
2187 			if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
2188 				set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
2189 			} else {
2190 				set_ns_and_type(xmlParam, type);
2191 			}
2192 		}
2193 		return xmlParam;
2194 	}
2195 
2196 	if (Z_TYPE_P(data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(data), zend_ce_traversable)) {
2197 		zend_object_iterator   *iter;
2198 		zend_class_entry       *ce = Z_OBJCE_P(data);
2199 		zval                   *val;
2200 
2201 		array_init(&array_copy);
2202 
2203 		iter = ce->get_iterator(ce, data, 0);
2204 
2205 		if (EG(exception)) {
2206 			goto iterator_done;
2207 		}
2208 
2209 		if (iter->funcs->rewind) {
2210 			iter->funcs->rewind(iter);
2211 			if (EG(exception)) {
2212 				goto iterator_done;
2213 			}
2214 		}
2215 
2216 		while (iter->funcs->valid(iter) == SUCCESS) {
2217 			if (EG(exception)) {
2218 				goto iterator_done;
2219 			}
2220 
2221 			val = iter->funcs->get_current_data(iter);
2222 			if (EG(exception)) {
2223 				goto iterator_done;
2224 			}
2225 			if (iter->funcs->get_current_key) {
2226 				zval key;
2227 				iter->funcs->get_current_key(iter, &key);
2228 				if (EG(exception)) {
2229 					goto iterator_done;
2230 				}
2231 				array_set_zval_key(Z_ARRVAL(array_copy), &key, val);
2232 				zval_ptr_dtor(val);
2233 				zval_ptr_dtor(&key);
2234 			} else {
2235 				add_next_index_zval(&array_copy, val);
2236 			}
2237 			Z_TRY_ADDREF_P(val);
2238 
2239 			iter->funcs->move_forward(iter);
2240 			if (EG(exception)) {
2241 				goto iterator_done;
2242 			}
2243 		}
2244 iterator_done:
2245 		OBJ_RELEASE(&iter->std);
2246 		if (EG(exception)) {
2247 			zval_ptr_dtor(&array_copy);
2248 			ZVAL_UNDEF(&array_copy);
2249 		} else {
2250 			data = &array_copy;
2251 		}
2252 	}
2253 
2254 	if (Z_TYPE_P(data) == IS_ARRAY) {
2255 		sdlAttributePtr arrayType;
2256 		sdlExtraAttributePtr ext;
2257 		sdlTypePtr elementType;
2258 
2259 		i = zend_hash_num_elements(Z_ARRVAL_P(data));
2260 
2261 		if (sdl_type &&
2262 		    sdl_type->attributes &&
2263 		    (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
2264 		      sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType")-1)) != NULL &&
2265 		    arrayType->extraAttributes &&
2266 		    (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType")-1)) != NULL) {
2267 
2268 			char *value, *end;
2269 			zval *el;
2270 
2271 			value = estrdup(ext->val);
2272 			end = strrchr(value,'[');
2273 			if (end) {
2274 				*end = '\0';
2275 				end++;
2276 				dimension = calc_dimension(end);
2277 			}
2278 			if (ext->ns != NULL) {
2279 				enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, value);
2280 				get_type_str(xmlParam, ext->ns, value, &array_type);
2281 			} else {
2282 				smart_str_appends(&array_type, value);
2283 			}
2284 
2285 			dims = safe_emalloc(sizeof(int), dimension, 0);
2286 			dims[0] = i;
2287 			el = data;
2288 			for (i = 1; i < dimension; i++) {
2289 				if (el != NULL && Z_TYPE_P(el) == IS_ARRAY &&
2290 				    zend_hash_num_elements(Z_ARRVAL_P(el)) > 0) {
2291 				    ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(el), el) {
2292 				    	break;
2293 				    } ZEND_HASH_FOREACH_END();
2294 					ZVAL_DEREF(el);
2295 					if (Z_TYPE_P(el) == IS_ARRAY) {
2296 						dims[i] = zend_hash_num_elements(Z_ARRVAL_P(el));
2297 					} else {
2298 						dims[i] = 0;
2299 					}
2300 				}
2301 			}
2302 
2303 			smart_str_append_long(&array_size, dims[0]);
2304 			for (i=1; i<dimension; i++) {
2305 				smart_str_appendc(&array_size, ',');
2306 				smart_str_append_long(&array_size, dims[i]);
2307 			}
2308 
2309 			efree(value);
2310 
2311 		} else if (sdl_type &&
2312 		           sdl_type->attributes &&
2313 		           (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
2314 		             sizeof(SOAP_1_2_ENC_NAMESPACE":itemType")-1)) != NULL &&
2315 		           arrayType->extraAttributes &&
2316 		           (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType")-1)) != NULL) {
2317 			if (ext->ns != NULL) {
2318 				enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, ext->val);
2319 				get_type_str(xmlParam, ext->ns, ext->val, &array_type);
2320 			} else {
2321 				smart_str_appends(&array_type, ext->val);
2322 			}
2323 			if ((arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2324 			                   sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
2325 			    arrayType->extraAttributes &&
2326 			    (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize")-1)) != NULL) {
2327 				dimension = calc_dimension_12(ext->val);
2328 				dims = get_position_12(dimension, ext->val);
2329 				if (dims[0] == 0) {dims[0] = i;}
2330 
2331 				smart_str_append_long(&array_size, dims[0]);
2332 				for (i=1; i<dimension; i++) {
2333 					smart_str_appendc(&array_size, ',');
2334 					smart_str_append_long(&array_size, dims[i]);
2335 				}
2336 			} else {
2337 				dims = emalloc(sizeof(int));
2338 				*dims = 0;
2339 				smart_str_append_long(&array_size, i);
2340 			}
2341 		} else if (sdl_type &&
2342 		           sdl_type->attributes &&
2343 		           (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2344 		             sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
2345 		           arrayType->extraAttributes &&
2346 		           (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraySize")-1)) != NULL) {
2347 			dimension = calc_dimension_12(ext->val);
2348 			dims = get_position_12(dimension, ext->val);
2349 			if (dims[0] == 0) {dims[0] = i;}
2350 
2351 			smart_str_append_long(&array_size, dims[0]);
2352 			for (i=1; i<dimension; i++) {
2353 				smart_str_appendc(&array_size, ',');
2354 				smart_str_append_long(&array_size, dims[i]);
2355 			}
2356 
2357 			if (sdl_type && sdl_type->elements &&
2358 			    zend_hash_num_elements(sdl_type->elements) == 1 &&
2359 			    (zend_hash_internal_pointer_reset(sdl_type->elements),
2360 			     (elementType = zend_hash_get_current_data_ptr(sdl_type->elements)) != NULL) &&
2361 			     elementType->encode && elementType->encode->details.type_str) {
2362 				element_type = elementType;
2363 				enc = elementType->encode;
2364 				get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
2365 			} else {
2366 				enc = get_array_type(xmlParam, data, &array_type);
2367 			}
2368 		} else if (sdl_type && sdl_type->elements &&
2369 		           zend_hash_num_elements(sdl_type->elements) == 1 &&
2370 		           (zend_hash_internal_pointer_reset(sdl_type->elements),
2371 		            (elementType = zend_hash_get_current_data_ptr(sdl_type->elements)) != NULL) &&
2372 		           elementType->encode && elementType->encode->details.type_str) {
2373 
2374 			element_type = elementType;
2375 			enc = elementType->encode;
2376 			get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
2377 
2378 			smart_str_append_long(&array_size, i);
2379 
2380 			dims = safe_emalloc(sizeof(int), dimension, 0);
2381 			dims[0] = i;
2382 		} else {
2383 
2384 			enc = get_array_type(xmlParam, data, &array_type);
2385 			smart_str_append_long(&array_size, i);
2386 			dims = safe_emalloc(sizeof(int), dimension, 0);
2387 			dims[0] = i;
2388 		}
2389 
2390 		if (style == SOAP_ENCODED) {
2391 			if (soap_version == SOAP_1_1) {
2392 				smart_str_0(&array_type);
2393 				if (strcmp(ZSTR_VAL(array_type.s),"xsd:anyType") == 0) {
2394 					smart_str_free(&array_type);
2395 					smart_str_appendl(&array_type,"xsd:ur-type",sizeof("xsd:ur-type")-1);
2396 				}
2397 				smart_str_appendc(&array_type, '[');
2398 				smart_str_append_smart_str(&array_type, &array_size);
2399 				smart_str_appendc(&array_type, ']');
2400 				smart_str_0(&array_type);
2401 				set_ns_prop(xmlParam, SOAP_1_1_ENC_NAMESPACE, "arrayType", ZSTR_VAL(array_type.s));
2402 			} else {
2403 				size_t i = 0;
2404 				while (i < ZSTR_LEN(array_size.s)) {
2405 					if (ZSTR_VAL(array_size.s)[i] == ',') {ZSTR_VAL(array_size.s)[i] = ' ';}
2406 					++i;
2407 				}
2408 				smart_str_0(&array_type);
2409 				smart_str_0(&array_size);
2410 				set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "itemType", ZSTR_VAL(array_type.s));
2411 				set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "arraySize", ZSTR_VAL(array_size.s));
2412 			}
2413 		}
2414 		smart_str_free(&array_type);
2415 		smart_str_free(&array_size);
2416 
2417 		add_xml_array_elements(xmlParam, element_type, enc, enc?encode_add_ns(xmlParam,enc->details.ns):NULL, dimension, dims, data, style);
2418 		efree(dims);
2419 	}
2420 	if (style == SOAP_ENCODED) {
2421 		if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
2422 			set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
2423 		} else {
2424 			set_ns_and_type(xmlParam, type);
2425 		}
2426 	}
2427 
2428 	zval_ptr_dtor(&array_copy);
2429 
2430 	return xmlParam;
2431 }
2432 
to_zval_array(zval * ret,encodeTypePtr type,xmlNodePtr data)2433 static zval *to_zval_array(zval *ret, encodeTypePtr type, xmlNodePtr data)
2434 {
2435 	xmlNodePtr trav;
2436 	encodePtr enc = NULL;
2437 	int dimension = 1;
2438 	int* dims = NULL;
2439 	int* pos = NULL;
2440 	xmlAttrPtr attr;
2441 	sdlAttributePtr arrayType;
2442 	sdlExtraAttributePtr ext;
2443 	sdlTypePtr elementType;
2444 
2445 	ZVAL_NULL(ret);
2446 	FIND_XML_NULL(data, ret);
2447 
2448 	if (data &&
2449 	    (attr = get_attribute(data->properties,"arrayType")) &&
2450 	    attr->children && attr->children->content) {
2451 		char *type, *end, *ns;
2452 		xmlNsPtr nsptr;
2453 
2454 		parse_namespace(attr->children->content, &type, &ns);
2455 		nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
2456 
2457 		end = strrchr(type,'[');
2458 		if (end) {
2459 			*end = '\0';
2460 			dimension = calc_dimension(end+1);
2461 			dims = get_position(dimension, end+1);
2462 		}
2463 		if (nsptr != NULL) {
2464 			enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
2465 		}
2466