xref: /PHP-8.3/ext/soap/php_sdl.c (revision 72a2cbcc)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Brad Lafountain <rodif_bl@yahoo.com>                        |
14   |          Shane Caraveo <shane@caraveo.com>                           |
15   |          Dmitry Stogov <dmitry@php.net>                              |
16   +----------------------------------------------------------------------+
17 */
18 
19 #include "php_soap.h"
20 #include "ext/libxml/php_libxml.h"
21 #include "libxml/uri.h"
22 
23 #include "ext/standard/md5.h"
24 #include "zend_virtual_cwd.h"
25 #include "main/php_open_temporary_file.h"
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 
31 #ifndef O_BINARY
32 # define O_BINARY 0
33 #endif
34 
35 static void delete_fault(zval *zv);
36 static void delete_fault_persistent(zval *zv);
37 static void delete_binding(zval *zv);
38 static void delete_binding_persistent(zval *zv);
39 static void delete_function(zval *zv);
40 static void delete_function_persistent(zval *zv);
41 static void delete_parameter(zval *zv);
42 static void delete_parameter_persistent(zval *zv);
43 static void delete_header(zval *header);
44 static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr);
45 static void delete_header_persistent(zval *zv);
46 static void delete_document(zval *zv);
47 
get_encoder_from_prefix(sdlPtr sdl,xmlNodePtr node,const xmlChar * type)48 encodePtr get_encoder_from_prefix(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
49 {
50 	encodePtr enc = NULL;
51 	xmlNsPtr nsptr;
52 	char *ns, *cptype;
53 
54 	parse_namespace(type, &cptype, &ns);
55 	nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
56 	if (nsptr != NULL) {
57 		enc = get_encoder(sdl, (char*)nsptr->href, cptype);
58 		if (enc == NULL) {
59 			enc = get_encoder_ex(sdl, cptype, strlen(cptype));
60 		}
61 	} else {
62 		enc = get_encoder_ex(sdl, (char*)type, xmlStrlen(type));
63 	}
64 	efree(cptype);
65 	if (ns) {efree(ns);}
66 	return enc;
67 }
68 
get_element(sdlPtr sdl,xmlNodePtr node,const xmlChar * type)69 static sdlTypePtr get_element(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
70 {
71 	sdlTypePtr ret = NULL;
72 
73 	if (sdl->elements) {
74 		xmlNsPtr nsptr;
75 		char *ns, *cptype;
76 		sdlTypePtr sdl_type;
77 
78 		parse_namespace(type, &cptype, &ns);
79 		nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
80 		if (nsptr != NULL) {
81 			int ns_len = xmlStrlen(nsptr->href);
82 			int type_len = strlen(cptype);
83 			int len = ns_len + type_len + 1;
84 			char *nscat = emalloc(len + 1);
85 
86 			memcpy(nscat, nsptr->href, ns_len);
87 			nscat[ns_len] = ':';
88 			memcpy(nscat+ns_len+1, cptype, type_len);
89 			nscat[len] = '\0';
90 
91 			if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, nscat, len)) != NULL) {
92 				ret = sdl_type;
93 			} else if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, type_len)) != NULL) {
94 				ret = sdl_type;
95 			}
96 			efree(nscat);
97 		} else {
98 			if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, xmlStrlen(type))) != NULL) {
99 				ret = sdl_type;
100 			}
101 		}
102 
103 		efree(cptype);
104 		if (ns) {efree(ns);}
105 	}
106 	return ret;
107 }
108 
get_encoder(sdlPtr sdl,const char * ns,const char * type)109 encodePtr get_encoder(sdlPtr sdl, const char *ns, const char *type)
110 {
111 	encodePtr enc = NULL;
112 	char *nscat;
113 	int ns_len = ns ? strlen(ns) : 0;
114 	int type_len = strlen(type);
115 	int len = ns_len + type_len + 1;
116 
117 	nscat = emalloc(len + 1);
118 	if (ns) {
119 		memcpy(nscat, ns, ns_len);
120 	}
121 	nscat[ns_len] = ':';
122 	memcpy(nscat+ns_len+1, type, type_len);
123 	nscat[len] = '\0';
124 
125 	enc = get_encoder_ex(sdl, nscat, len);
126 
127 	if (enc == NULL &&
128 	    ((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
129 	      memcmp(ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
130 	     (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
131 	      memcmp(ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
132 		char *enc_nscat;
133 		int enc_ns_len;
134 		int enc_len;
135 
136 		enc_ns_len = sizeof(XSD_NAMESPACE)-1;
137 		enc_len = enc_ns_len + type_len + 1;
138 		enc_nscat = emalloc(enc_len + 1);
139 		memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
140 		enc_nscat[enc_ns_len] = ':';
141 		memcpy(enc_nscat+enc_ns_len+1, type, type_len);
142 		enc_nscat[enc_len] = '\0';
143 
144 		enc = get_encoder_ex(NULL, enc_nscat, enc_len);
145 		efree(enc_nscat);
146 		if (enc && sdl) {
147 			encodePtr new_enc	= pemalloc(sizeof(encode), sdl->is_persistent);
148 			memcpy(new_enc, enc, sizeof(encode));
149 			if (sdl->is_persistent) {
150 				new_enc->details.ns = zend_strndup(ns, ns_len);
151 				new_enc->details.type_str = strdup(new_enc->details.type_str);
152 			} else {
153 				new_enc->details.ns = estrndup(ns, ns_len);
154 				new_enc->details.type_str = estrdup(new_enc->details.type_str);
155 			}
156 			if (sdl->encoders == NULL) {
157 				sdl->encoders = pemalloc(sizeof(HashTable), sdl->is_persistent);
158 				zend_hash_init(sdl->encoders, 0, NULL, sdl->is_persistent ? delete_encoder_persistent : delete_encoder, sdl->is_persistent);
159 			}
160 			zend_hash_str_update_ptr(sdl->encoders, nscat, len, new_enc);
161 			enc = new_enc;
162 		}
163 	}
164 	efree(nscat);
165 	return enc;
166 }
167 
get_encoder_ex(sdlPtr sdl,const char * nscat,int len)168 encodePtr get_encoder_ex(sdlPtr sdl, const char *nscat, int len)
169 {
170 	encodePtr enc;
171 
172 	if ((enc = zend_hash_str_find_ptr(&SOAP_GLOBAL(defEnc), (char*)nscat, len)) != NULL) {
173 		return enc;
174 	} else if (sdl && sdl->encoders && (enc = zend_hash_str_find_ptr(sdl->encoders, (char*)nscat, len)) != NULL) {
175 		return enc;
176 	}
177 	return NULL;
178 }
179 
get_binding_from_type(sdlPtr sdl,sdlBindingType type)180 sdlBindingPtr get_binding_from_type(sdlPtr sdl, sdlBindingType type)
181 {
182 	sdlBindingPtr binding;
183 
184 	if (sdl == NULL) {
185 		return NULL;
186 	}
187 
188 	ZEND_HASH_MAP_FOREACH_PTR(sdl->bindings, binding) {
189 		if (binding->bindingType == type) {
190 			return binding;
191 		}
192 	} ZEND_HASH_FOREACH_END();
193 	return NULL;
194 }
195 
get_binding_from_name(sdlPtr sdl,char * name,char * ns)196 sdlBindingPtr get_binding_from_name(sdlPtr sdl, char *name, char *ns)
197 {
198 	sdlBindingPtr binding;
199 	smart_str key = {0};
200 
201 	smart_str_appends(&key, ns);
202 	smart_str_appendc(&key, ':');
203 	smart_str_appends(&key, name);
204 	smart_str_0(&key);
205 
206 	binding = zend_hash_find_ptr(sdl->bindings, key.s);
207 
208 	smart_str_free(&key);
209 	return binding;
210 }
211 
is_wsdl_element(xmlNodePtr node)212 static int is_wsdl_element(xmlNodePtr node)
213 {
214 	if (node->ns && strcmp((char*)node->ns->href, WSDL_NAMESPACE) != 0) {
215 		xmlAttrPtr attr;
216 		if ((attr = get_attribute_ex(node->properties, "required", WSDL_NAMESPACE)) != NULL &&
217 		     attr->children && attr->children->content &&
218 		     (strcmp((char*)attr->children->content, "1") == 0 ||
219 		      strcmp((char*)attr->children->content, "true") == 0)) {
220 			soap_error1(E_ERROR, "Parsing WSDL: Unknown required WSDL extension '%s'", node->ns->href);
221 		}
222 		return 0;
223 	}
224 	return 1;
225 }
226 
sdl_set_uri_credentials(sdlCtx * ctx,char * uri)227 void sdl_set_uri_credentials(sdlCtx *ctx, char *uri)
228 {
229 	char *s;
230 	size_t l1, l2;
231 	zval context;
232 	zval *header = NULL;
233 
234 	/* check if we load xsd from the same server */
235 	s = strstr(ctx->sdl->source, "://");
236 	if (!s) return;
237 	s = strchr(s+3, '/');
238 	l1 = s ? (size_t)(s - ctx->sdl->source) : strlen(ctx->sdl->source);
239 	s = strstr((char*)uri, "://");
240 	if (!s) return;
241 	s = strchr(s+3, '/');
242 	l2 = s ? (size_t)(s - (char*)uri) : strlen((char*)uri);
243 	if (l1 != l2) {
244 		/* check for http://...:80/ */
245 		if (l1 > 11 &&
246 		    ctx->sdl->source[4] == ':' &&
247 		    ctx->sdl->source[l1-3] == ':' &&
248 		    ctx->sdl->source[l1-2] == '8' &&
249 		    ctx->sdl->source[l1-1] == '0') {
250 			l1 -= 3;
251 		}
252 		if (l2 > 11 &&
253 		    uri[4] == ':' &&
254 		    uri[l2-3] == ':' &&
255 		    uri[l2-2] == '8' &&
256 		    uri[l2-1] == '0') {
257 			l2 -= 3;
258 		}
259 		/* check for https://...:443/ */
260 		if (l1 > 13 &&
261 		    ctx->sdl->source[4] == 's' &&
262 		    ctx->sdl->source[l1-4] == ':' &&
263 		    ctx->sdl->source[l1-3] == '4' &&
264 		    ctx->sdl->source[l1-2] == '4' &&
265 		    ctx->sdl->source[l1-1] == '3') {
266 			l1 -= 4;
267 		}
268 		if (l2 > 13 &&
269 		    uri[4] == 's' &&
270 		    uri[l2-4] == ':' &&
271 		    uri[l2-3] == '4' &&
272 		    uri[l2-2] == '4' &&
273 		    uri[l2-1] == '3') {
274 			l2 -= 4;
275 		}
276 	}
277 	if (l1 != l2 || memcmp(ctx->sdl->source, uri, l1) != 0) {
278 		/* another server. clear authentication credentals */
279 		php_libxml_switch_context(NULL, &context);
280 		php_libxml_switch_context(&context, NULL);
281 		if (Z_TYPE(context) != IS_UNDEF) {
282 			zval *context_ptr = &context;
283 			ctx->context = php_stream_context_from_zval(context_ptr, 1);
284 
285 			if (ctx->context &&
286 			    (header = php_stream_context_get_option(ctx->context, "http", "header")) != NULL &&
287 				Z_TYPE_P(header) == IS_STRING) {
288 				/* TODO: should support header as an array, but this code path is untested */
289 				s = strstr(Z_STRVAL_P(header), "Authorization: Basic");
290 				if (s && (s == Z_STRVAL_P(header) || *(s-1) == '\n' || *(s-1) == '\r')) {
291 					char *rest = strstr(s, "\r\n");
292 					if (rest) {
293 						zval new_header;
294 
295 						rest += 2;
296 						ZVAL_NEW_STR(&new_header, zend_string_alloc(Z_STRLEN_P(header) - (rest - s), 0));
297 						memcpy(Z_STRVAL(new_header), Z_STRVAL_P(header), s - Z_STRVAL_P(header));
298 						memcpy(Z_STRVAL(new_header) + (s - Z_STRVAL_P(header)), rest, Z_STRLEN_P(header) - (rest - Z_STRVAL_P(header)) + 1);
299 						ZVAL_COPY(&ctx->old_header, header);
300 						php_stream_context_set_option(ctx->context, "http", "header", &new_header);
301 						zval_ptr_dtor(&new_header);
302 					}
303 				}
304 			}
305 		}
306 	}
307 }
308 
sdl_restore_uri_credentials(sdlCtx * ctx)309 void sdl_restore_uri_credentials(sdlCtx *ctx)
310 {
311 	if (Z_TYPE(ctx->old_header) != IS_UNDEF) {
312 	    php_stream_context_set_option(ctx->context, "http", "header", &ctx->old_header);
313 	    zval_ptr_dtor(&ctx->old_header);
314 		ZVAL_UNDEF(&ctx->old_header);
315 	}
316 	ctx->context = NULL;
317 }
318 
319 #define SAFE_STR(a) ((a)?((const char *)a):"")
320 
load_wsdl_ex(zval * this_ptr,char * struri,sdlCtx * ctx,int include)321 static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include)
322 {
323 	sdlPtr tmpsdl = ctx->sdl;
324 	xmlDocPtr wsdl;
325 	xmlNodePtr root, definitions, trav;
326 	xmlAttrPtr targetNamespace;
327 
328 	if (zend_hash_str_exists(&ctx->docs, struri, strlen(struri))) {
329 		return;
330 	}
331 
332 	sdl_set_uri_credentials(ctx, struri);
333 	wsdl = soap_xmlParseFile(struri);
334 	sdl_restore_uri_credentials(ctx);
335 
336 	if (!wsdl) {
337 		const xmlError *xmlErrorPtr = xmlGetLastError();
338 
339 		if (xmlErrorPtr) {
340 			soap_error2(E_ERROR, "Parsing WSDL: Couldn't load from '%s' : %s", struri, xmlErrorPtr->message);
341 		} else {
342 			soap_error1(E_ERROR, "Parsing WSDL: Couldn't load from '%s'", struri);
343 		}
344 	}
345 
346 	zend_hash_str_add_ptr(&ctx->docs, struri, strlen(struri), wsdl);
347 
348 	root = wsdl->children;
349 	definitions = get_node_ex(root, "definitions", WSDL_NAMESPACE);
350 	if (!definitions) {
351 		if (include) {
352 			xmlNodePtr schema = get_node_ex(root, "schema", XSD_NAMESPACE);
353 			if (schema) {
354 				load_schema(ctx, schema);
355 				return;
356 			}
357 		}
358 		soap_error1(E_ERROR, "Parsing WSDL: Couldn't find <definitions> in '%s'", struri);
359 	}
360 
361 	if (!include) {
362 		targetNamespace = get_attribute(definitions->properties, "targetNamespace");
363 		if (targetNamespace) {
364 			tmpsdl->target_ns = estrdup((char*)targetNamespace->children->content);
365 		}
366 	}
367 
368 	trav = definitions->children;
369 	while (trav != NULL) {
370 		if (!is_wsdl_element(trav)) {
371 			trav = trav->next;
372 			continue;
373 		}
374 		if (node_is_equal(trav,"types")) {
375 			/* TODO: Only one "types" is allowed */
376 			xmlNodePtr trav2 = trav->children;
377 
378 			while (trav2 != NULL) {
379 				if (node_is_equal_ex(trav2, "schema", XSD_NAMESPACE)) {
380 					load_schema(ctx, trav2);
381 				} else if (is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
382 					soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
383 				}
384 				trav2 = trav2->next;
385 			}
386 		} else if (node_is_equal(trav,"import")) {
387 			/* TODO: namespace ??? */
388 			xmlAttrPtr tmp = get_attribute(trav->properties, "location");
389 			if (tmp) {
390 				xmlChar *uri;
391 				xmlChar *base = xmlNodeGetBase(trav->doc, trav);
392 
393 				if (base == NULL) {
394 					uri = xmlBuildURI(tmp->children->content, trav->doc->URL);
395 				} else {
396 					uri = xmlBuildURI(tmp->children->content, base);
397 					xmlFree(base);
398 				}
399 				load_wsdl_ex(this_ptr, (char*)uri, ctx, 1);
400 				xmlFree(uri);
401 			}
402 
403 		} else if (node_is_equal(trav,"message")) {
404 			xmlAttrPtr name = get_attribute(trav->properties, "name");
405 			if (name && name->children && name->children->content) {
406 				if (zend_hash_str_add_ptr(&ctx->messages, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
407 					soap_error1(E_ERROR, "Parsing WSDL: <message> '%s' already defined", name->children->content);
408 				}
409 			} else {
410 				soap_error0(E_ERROR, "Parsing WSDL: <message> has no name attribute");
411 			}
412 
413 		} else if (node_is_equal(trav,"portType")) {
414 			xmlAttrPtr name = get_attribute(trav->properties, "name");
415 			if (name && name->children && name->children->content) {
416 				if (zend_hash_str_add_ptr(&ctx->portTypes, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
417 					soap_error1(E_ERROR, "Parsing WSDL: <portType> '%s' already defined", name->children->content);
418 				}
419 			} else {
420 				soap_error0(E_ERROR, "Parsing WSDL: <portType> has no name attribute");
421 			}
422 
423 		} else if (node_is_equal(trav,"binding")) {
424 			xmlAttrPtr name = get_attribute(trav->properties, "name");
425 			if (name && name->children && name->children->content) {
426 				if (zend_hash_str_add_ptr(&ctx->bindings, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
427 					soap_error1(E_ERROR, "Parsing WSDL: <binding> '%s' already defined", name->children->content);
428 				}
429 			} else {
430 				soap_error0(E_ERROR, "Parsing WSDL: <binding> has no name attribute");
431 			}
432 
433 		} else if (node_is_equal(trav,"service")) {
434 			xmlAttrPtr name = get_attribute(trav->properties, "name");
435 			if (name && name->children && name->children->content) {
436 				if (zend_hash_str_add_ptr(&ctx->services, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
437 					soap_error1(E_ERROR, "Parsing WSDL: <service> '%s' already defined", name->children->content);
438 				}
439 			} else {
440 				soap_error0(E_ERROR, "Parsing WSDL: <service> has no name attribute");
441 			}
442 		} else if (!node_is_equal(trav,"documentation")) {
443 			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
444 		}
445 		trav = trav->next;
446 	}
447 }
448 
wsdl_soap_binding_header(sdlCtx * ctx,xmlNodePtr header,char * wsdl_soap_namespace,int fault)449 static sdlSoapBindingFunctionHeaderPtr wsdl_soap_binding_header(sdlCtx* ctx, xmlNodePtr header, char* wsdl_soap_namespace, int fault)
450 {
451 	xmlAttrPtr tmp;
452 	xmlNodePtr message, part;
453 	char *ctype;
454 	sdlSoapBindingFunctionHeaderPtr h;
455 
456 	tmp = get_attribute(header->properties, "message");
457 	if (!tmp) {
458 		soap_error0(E_ERROR, "Parsing WSDL: Missing message attribute for <header>");
459 	}
460 
461 	ctype = strrchr((char*)tmp->children->content,':');
462 	if (ctype == NULL) {
463 		ctype = (char*)tmp->children->content;
464 	} else {
465 		++ctype;
466 	}
467 	if ((message = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
468 		soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", tmp->children->content);
469 	}
470 
471 	tmp = get_attribute(header->properties, "part");
472 	if (!tmp) {
473 		soap_error0(E_ERROR, "Parsing WSDL: Missing part attribute for <header>");
474 	}
475 	part = get_node_with_attribute_ex(message->children, "part", WSDL_NAMESPACE, "name", (char*)tmp->children->content, NULL);
476 	if (!part) {
477 		soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", tmp->children->content);
478 	}
479 
480 	h = emalloc(sizeof(sdlSoapBindingFunctionHeader));
481 	memset(h, 0, sizeof(sdlSoapBindingFunctionHeader));
482 	h->name = estrdup((char*)tmp->children->content);
483 
484 	tmp = get_attribute(header->properties, "use");
485 	if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
486 		h->use = SOAP_ENCODED;
487 	} else {
488 		h->use = SOAP_LITERAL;
489 	}
490 
491 	tmp = get_attribute(header->properties, "namespace");
492 	if (tmp) {
493 		h->ns = estrdup((char*)tmp->children->content);
494 	}
495 
496 	if (h->use == SOAP_ENCODED) {
497 		tmp = get_attribute(header->properties, "encodingStyle");
498 		if (tmp) {
499 			if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
500 				h->encodingStyle = SOAP_ENCODING_1_1;
501 			} else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
502 				h->encodingStyle = SOAP_ENCODING_1_2;
503 			} else {
504 				soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
505 			}
506 		} else {
507 			soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
508 		}
509 	}
510 
511 	tmp = get_attribute(part->properties, "type");
512 	if (tmp != NULL) {
513 		h->encode = get_encoder_from_prefix(ctx->sdl, part, tmp->children->content);
514 	} else {
515 		tmp = get_attribute(part->properties, "element");
516 		if (tmp != NULL) {
517 			h->element = get_element(ctx->sdl, part, tmp->children->content);
518 			if (h->element) {
519 				h->encode = h->element->encode;
520 				if (!h->ns && h->element->namens) {
521 					h->ns = estrdup(h->element->namens);
522 				}
523 				if (h->element->name) {
524 					efree(h->name);
525 					h->name = estrdup(h->element->name);
526 				}
527 			}
528 		}
529 	}
530 	if (!fault) {
531 		xmlNodePtr trav = header->children;
532 		while (trav != NULL) {
533 			if (node_is_equal_ex(trav, "headerfault", wsdl_soap_namespace)) {
534 				sdlSoapBindingFunctionHeaderPtr hf = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 1);
535 				smart_str key = {0};
536 
537 				if (h->headerfaults == NULL) {
538 					h->headerfaults = emalloc(sizeof(HashTable));
539 					zend_hash_init(h->headerfaults, 0, NULL, delete_header, 0);
540 				}
541 
542 				if (hf->ns) {
543 					smart_str_appends(&key,hf->ns);
544 					smart_str_appendc(&key,':');
545 				}
546 				smart_str_appends(&key,hf->name);
547 				smart_str_0(&key);
548 				if (zend_hash_add_ptr(h->headerfaults, key.s, hf) == NULL) {
549 					delete_header_int(hf);
550 				}
551 				smart_str_free(&key);
552 			} else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
553 				soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
554 			}
555 			trav = trav->next;
556 		}
557 	}
558 	return h;
559 }
560 
wsdl_soap_binding_body(sdlCtx * ctx,xmlNodePtr node,char * wsdl_soap_namespace,sdlSoapBindingFunctionBody * binding,HashTable * params)561 static void wsdl_soap_binding_body(sdlCtx* ctx, xmlNodePtr node, char* wsdl_soap_namespace, sdlSoapBindingFunctionBody *binding, HashTable* params)
562 {
563 	xmlNodePtr body, trav;
564 	xmlAttrPtr tmp;
565 
566 	trav = node->children;
567 	while (trav != NULL) {
568 		if (node_is_equal_ex(trav, "body", wsdl_soap_namespace)) {
569 			body = trav;
570 
571 			tmp = get_attribute(body->properties, "use");
572 			if (tmp && !strncmp((char*)tmp->children->content, "literal", sizeof("literal"))) {
573 				binding->use = SOAP_LITERAL;
574 			} else {
575 				binding->use = SOAP_ENCODED;
576 			}
577 
578 			tmp = get_attribute(body->properties, "namespace");
579 			if (tmp) {
580 				binding->ns = estrdup((char*)tmp->children->content);
581 			}
582 
583 			tmp = get_attribute(body->properties, "parts");
584 			if (tmp) {
585 				HashTable    ht;
586 				char *parts = (char*)tmp->children->content;
587 
588 				/* Delete all parts those are not in the "parts" attribute */
589 				zend_hash_init(&ht, 0, NULL, delete_parameter, 0);
590 				while (*parts) {
591 					sdlParamPtr param;
592 					int found = 0;
593 					char *end;
594 
595 					while (*parts == ' ') ++parts;
596 					if (*parts == '\0') break;
597 					end = strchr(parts, ' ');
598 					if (end) *end = '\0';
599 					ZEND_HASH_FOREACH_PTR(params, param) {
600 						if (param->paramName &&
601 						    strcmp(parts, param->paramName) == 0) {
602 					  	sdlParamPtr x_param;
603 					  	x_param = emalloc(sizeof(sdlParam));
604 					  	*x_param = *param;
605 					  	param->paramName = NULL;
606 					  	zend_hash_next_index_insert_ptr(&ht, x_param);
607 					  	found = 1;
608 					  	break;
609 						}
610 					} ZEND_HASH_FOREACH_END();
611 					if (!found) {
612 						soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", parts);
613 					}
614 					parts += strlen(parts);
615 					if (end) *end = ' ';
616 				}
617 				zend_hash_destroy(params);
618 				*params = ht;
619 			}
620 
621 			if (binding->use == SOAP_ENCODED) {
622 				tmp = get_attribute(body->properties, "encodingStyle");
623 				if (tmp) {
624 					if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
625 						binding->encodingStyle = SOAP_ENCODING_1_1;
626 					} else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
627 						binding->encodingStyle = SOAP_ENCODING_1_2;
628 					} else {
629 						soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
630 					}
631 				} else {
632 					soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
633 				}
634 			}
635 		} else if (node_is_equal_ex(trav, "header", wsdl_soap_namespace)) {
636 			sdlSoapBindingFunctionHeaderPtr h = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 0);
637 			smart_str key = {0};
638 
639 			if (binding->headers == NULL) {
640 				binding->headers = emalloc(sizeof(HashTable));
641 				zend_hash_init(binding->headers, 0, NULL, delete_header, 0);
642 			}
643 
644 			if (h->ns) {
645 				smart_str_appends(&key,h->ns);
646 				smart_str_appendc(&key,':');
647 			}
648 			smart_str_appends(&key,h->name);
649 			smart_str_0(&key);
650 			if (zend_hash_add_ptr(binding->headers, key.s, h) == NULL) {
651 				delete_header_int(h);
652 			}
653 			smart_str_free(&key);
654 		} else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
655 			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
656 		}
657 		trav = trav->next;
658 	}
659 }
660 
wsdl_message(sdlCtx * ctx,xmlChar * message_name)661 static HashTable* wsdl_message(sdlCtx *ctx, xmlChar* message_name)
662 {
663 	xmlNodePtr trav, part, message = NULL, tmp;
664 	HashTable* parameters = NULL;
665 	char *ctype;
666 
667 	ctype = strrchr((char*)message_name,':');
668 	if (ctype == NULL) {
669 		ctype = (char*)message_name;
670 	} else {
671 		++ctype;
672 	}
673 	if ((tmp = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
674 		soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", message_name);
675 	}
676 	message = tmp;
677 
678 	parameters = emalloc(sizeof(HashTable));
679 	zend_hash_init(parameters, 0, NULL, delete_parameter, 0);
680 
681 	trav = message->children;
682 	while (trav != NULL) {
683 		xmlAttrPtr element, type, name;
684 		sdlParamPtr param;
685 
686 		if (trav->ns != NULL && strcmp((char*)trav->ns->href, WSDL_NAMESPACE) != 0) {
687 			soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>",  SAFE_STR(trav->name));
688 		}
689 		if (node_is_equal(trav,"documentation")) {
690 			trav = trav->next;
691 			continue;
692 		}
693 		if (!node_is_equal(trav,"part")) {
694 			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
695 		}
696 		part = trav;
697 		param = emalloc(sizeof(sdlParam));
698 		memset(param,0,sizeof(sdlParam));
699 		param->order = 0;
700 
701 		name = get_attribute(part->properties, "name");
702 		if (name == NULL) {
703 			soap_error1(E_ERROR, "Parsing WSDL: No name associated with <part> '%s'",  SAFE_STR(message->name));
704 		}
705 
706 		param->paramName = estrdup((char*)name->children->content);
707 
708 		type = get_attribute(part->properties, "type");
709 		if (type != NULL) {
710 			param->encode = get_encoder_from_prefix(ctx->sdl, part, type->children->content);
711 		} else {
712 			element = get_attribute(part->properties, "element");
713 			if (element != NULL) {
714 				param->element = get_element(ctx->sdl, part, element->children->content);
715 				if (param->element) {
716 					param->encode = param->element->encode;
717 				}
718 			}
719 		}
720 
721 		zend_hash_next_index_insert_ptr(parameters, param);
722 
723 		trav = trav->next;
724 	}
725 	return parameters;
726 }
727 
load_wsdl(zval * this_ptr,char * struri)728 static sdlPtr load_wsdl(zval *this_ptr, char *struri)
729 {
730 	sdlCtx ctx;
731 	int i,n;
732 
733 	memset(&ctx,0,sizeof(ctx));
734 	ctx.sdl = emalloc(sizeof(sdl));
735 	memset(ctx.sdl, 0, sizeof(sdl));
736 	ctx.sdl->source = estrdup(struri);
737 	zend_hash_init(&ctx.sdl->functions, 0, NULL, delete_function, 0);
738 
739 	zend_hash_init(&ctx.docs, 0, NULL, delete_document, 0);
740 	zend_hash_init(&ctx.messages, 0, NULL, NULL, 0);
741 	zend_hash_init(&ctx.bindings, 0, NULL, NULL, 0);
742 	zend_hash_init(&ctx.portTypes, 0, NULL, NULL, 0);
743 	zend_hash_init(&ctx.services,  0, NULL, NULL, 0);
744 
745 	zend_try {
746 	load_wsdl_ex(this_ptr, struri, &ctx, 0);
747 	schema_pass2(&ctx);
748 
749 	n = zend_hash_num_elements(&ctx.services);
750 	if (n > 0) {
751 		zend_hash_internal_pointer_reset(&ctx.services);
752 		for (i = 0; i < n; i++) {
753 			xmlNodePtr service, tmp;
754 			xmlNodePtr trav, port;
755 			int has_soap_port = 0;
756 
757 			service = tmp = zend_hash_get_current_data_ptr(&ctx.services);
758 
759 			trav = service->children;
760 			while (trav != NULL) {
761 				xmlAttrPtr type, name, bindingAttr, location;
762 				xmlNodePtr portType, operation;
763 				xmlNodePtr address, binding, trav2;
764 				char *ctype;
765 				sdlBindingPtr tmpbinding;
766 				char *wsdl_soap_namespace = NULL;
767 
768 				if (!is_wsdl_element(trav) || node_is_equal(trav,"documentation")) {
769 					trav = trav->next;
770 					continue;
771 				}
772 				if (!node_is_equal(trav,"port")) {
773 					soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
774 				}
775 
776 				port = trav;
777 
778 				tmpbinding = emalloc(sizeof(sdlBinding));
779 				memset(tmpbinding, 0, sizeof(sdlBinding));
780 
781 				bindingAttr = get_attribute(port->properties, "binding");
782 				if (bindingAttr == NULL) {
783 					soap_error0(E_ERROR, "Parsing WSDL: No binding associated with <port>");
784 				}
785 
786 				/* find address and figure out binding type */
787 				address = NULL;
788 				trav2 = port->children;
789 				while (trav2 != NULL) {
790 					if (node_is_equal(trav2,"address") && trav2->ns) {
791 						if (!strncmp((char*)trav2->ns->href, WSDL_SOAP11_NAMESPACE, sizeof(WSDL_SOAP11_NAMESPACE))) {
792 							address = trav2;
793 							wsdl_soap_namespace = WSDL_SOAP11_NAMESPACE;
794 							tmpbinding->bindingType = BINDING_SOAP;
795 						} else if (!strncmp((char*)trav2->ns->href, WSDL_SOAP12_NAMESPACE, sizeof(WSDL_SOAP12_NAMESPACE))) {
796 							address = trav2;
797 							wsdl_soap_namespace = WSDL_SOAP12_NAMESPACE;
798 							tmpbinding->bindingType = BINDING_SOAP;
799 						} else if (!strncmp((char*)trav2->ns->href, RPC_SOAP12_NAMESPACE, sizeof(RPC_SOAP12_NAMESPACE))) {
800 							address = trav2;
801 							wsdl_soap_namespace = RPC_SOAP12_NAMESPACE;
802 							tmpbinding->bindingType = BINDING_SOAP;
803 						} else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP11_NAMESPACE, sizeof(WSDL_HTTP11_NAMESPACE))) {
804 							address = trav2;
805 							tmpbinding->bindingType = BINDING_HTTP;
806 						} else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP12_NAMESPACE, sizeof(WSDL_HTTP12_NAMESPACE))) {
807 							address = trav2;
808 							tmpbinding->bindingType = BINDING_HTTP;
809 						}
810 					}
811 					if (trav2 != address && is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
812 						soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav2->name));
813 					}
814 				  trav2 = trav2->next;
815 				}
816 				if (!address || tmpbinding->bindingType == BINDING_HTTP) {
817 					if (has_soap_port || trav->next || i < n-1) {
818 						efree(tmpbinding);
819 						trav = trav->next;
820 						continue;
821 					} else if (!address) {
822 						soap_error0(E_ERROR, "Parsing WSDL: No address associated with <port>");
823 					}
824 				}
825 				has_soap_port = 1;
826 
827 				location = get_attribute(address->properties, "location");
828 				if (!location) {
829 					soap_error0(E_ERROR, "Parsing WSDL: No location associated with <port>");
830 				}
831 
832 				tmpbinding->location = estrdup((char*)location->children->content);
833 
834 				ctype = strrchr((char*)bindingAttr->children->content,':');
835 				if (ctype == NULL) {
836 					ctype = (char*)bindingAttr->children->content;
837 				} else {
838 					++ctype;
839 				}
840 				if ((tmp = zend_hash_str_find_ptr(&ctx.bindings, ctype, strlen(ctype))) == NULL) {
841 					soap_error1(E_ERROR, "Parsing WSDL: No <binding> element with name '%s'", ctype);
842 				}
843 				binding = tmp;
844 
845 				if (tmpbinding->bindingType == BINDING_SOAP) {
846 					sdlSoapBindingPtr soapBinding;
847 					xmlNodePtr soapBindingNode;
848 					xmlAttrPtr tmp;
849 
850 					soapBinding = emalloc(sizeof(sdlSoapBinding));
851 					memset(soapBinding, 0, sizeof(sdlSoapBinding));
852 					soapBinding->style = SOAP_DOCUMENT;
853 
854 					soapBindingNode = get_node_ex(binding->children, "binding", wsdl_soap_namespace);
855 					if (soapBindingNode) {
856 						tmp = get_attribute(soapBindingNode->properties, "style");
857 						if (tmp && !strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
858 							soapBinding->style = SOAP_RPC;
859 						}
860 
861 						tmp = get_attribute(soapBindingNode->properties, "transport");
862 						if (tmp) {
863 							if (strncmp((char*)tmp->children->content, WSDL_HTTP_TRANSPORT, sizeof(WSDL_HTTP_TRANSPORT)) == 0) {
864 								soapBinding->transport = SOAP_TRANSPORT_HTTP;
865 							} else {
866 								/* try the next binding */
867 								efree(soapBinding);
868 								efree(tmpbinding->location);
869 								efree(tmpbinding);
870 								trav = trav->next;
871 								continue;
872 							}
873 						}
874 					}
875 					tmpbinding->bindingAttributes = (void *)soapBinding;
876 				}
877 
878 				name = get_attribute(binding->properties, "name");
879 				if (name == NULL) {
880 					soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <binding>");
881 				}
882 				tmpbinding->name = estrdup((char*)name->children->content);
883 
884 				type = get_attribute(binding->properties, "type");
885 				if (type == NULL) {
886 					soap_error0(E_ERROR, "Parsing WSDL: Missing 'type' attribute for <binding>");
887 				}
888 
889 				ctype = strchr((char*)type->children->content,':');
890 				if (ctype == NULL) {
891 					ctype = (char*)type->children->content;
892 				} else {
893 					++ctype;
894 				}
895 				if ((tmp = zend_hash_str_find_ptr(&ctx.portTypes, ctype, strlen(ctype))) == NULL) {
896 					soap_error1(E_ERROR, "Parsing WSDL: Missing <portType> with name '%s'", name->children->content);
897 				}
898 				portType = tmp;
899 
900 				trav2 = binding->children;
901 				while (trav2 != NULL) {
902 					sdlFunctionPtr function;
903 					xmlNodePtr input, output, fault, portTypeOperation, trav3;
904 					xmlAttrPtr op_name, paramOrder;
905 
906 					if ((tmpbinding->bindingType == BINDING_SOAP &&
907 					    node_is_equal_ex(trav2, "binding", wsdl_soap_namespace)) ||
908 					    !is_wsdl_element(trav2) ||
909 					    node_is_equal(trav2,"documentation")) {
910 						trav2 = trav2->next;
911 						continue;
912 					}
913 					if (!node_is_equal(trav2,"operation")) {
914 						soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav2->name));
915 					}
916 
917 					operation = trav2;
918 
919 					op_name = get_attribute(operation->properties, "name");
920 					if (op_name == NULL) {
921 						soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <operation>");
922 					}
923 
924 					trav3 = operation->children;
925 					while  (trav3 != NULL) {
926 						if (tmpbinding->bindingType == BINDING_SOAP &&
927 						    node_is_equal_ex(trav3, "operation", wsdl_soap_namespace)) {
928 						} else if (is_wsdl_element(trav3) &&
929 						           !node_is_equal(trav3,"input") &&
930 						           !node_is_equal(trav3,"output") &&
931 						           !node_is_equal(trav3,"fault") &&
932 						           !node_is_equal(trav3,"documentation")) {
933 							soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav3->name));
934 						}
935 						trav3 = trav3->next;
936 					}
937 
938 					portTypeOperation = get_node_with_attribute_ex(portType->children, "operation", WSDL_NAMESPACE, "name", (char*)op_name->children->content, NULL);
939 					if (portTypeOperation == NULL) {
940 						soap_error1(E_ERROR, "Parsing WSDL: Missing <portType>/<operation> with name '%s'", op_name->children->content);
941 					}
942 
943 					function = emalloc(sizeof(sdlFunction));
944 					memset(function, 0, sizeof(sdlFunction));
945 					function->functionName = estrdup((char*)op_name->children->content);
946 
947 					if (tmpbinding->bindingType == BINDING_SOAP) {
948 						sdlSoapBindingFunctionPtr soapFunctionBinding;
949 						sdlSoapBindingPtr soapBinding;
950 						xmlNodePtr soapOperation;
951 						xmlAttrPtr tmp;
952 
953 						soapFunctionBinding = emalloc(sizeof(sdlSoapBindingFunction));
954 						memset(soapFunctionBinding, 0, sizeof(sdlSoapBindingFunction));
955 						soapBinding = (sdlSoapBindingPtr)tmpbinding->bindingAttributes;
956 						soapFunctionBinding->style = soapBinding->style;
957 
958 						soapOperation = get_node_ex(operation->children, "operation", wsdl_soap_namespace);
959 						if (soapOperation) {
960 							tmp = get_attribute(soapOperation->properties, "soapAction");
961 							if (tmp) {
962 								soapFunctionBinding->soapAction = estrdup((char*)tmp->children->content);
963 							}
964 
965 							tmp = get_attribute(soapOperation->properties, "style");
966 							if (tmp) {
967 								if (!strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
968 									soapFunctionBinding->style = SOAP_RPC;
969 								} else {
970 									soapFunctionBinding->style = SOAP_DOCUMENT;
971 								}
972 							} else {
973 								soapFunctionBinding->style = soapBinding->style;
974 							}
975 						}
976 
977 						function->bindingAttributes = (void *)soapFunctionBinding;
978 					}
979 
980 					input = get_node_ex(portTypeOperation->children, "input", WSDL_NAMESPACE);
981 					if (input != NULL) {
982 						xmlAttrPtr message;
983 
984 						message = get_attribute(input->properties, "message");
985 						if (message == NULL) {
986 							soap_error1(E_ERROR, "Parsing WSDL: Missing name for <input> of '%s'", op_name->children->content);
987 						}
988 						function->requestParameters = wsdl_message(&ctx, message->children->content);
989 
990 /* FIXME
991 						xmlAttrPtr name = get_attribute(input->properties, "name");
992 						if (name != NULL) {
993 							function->requestName = estrdup(name->children->content);
994 						} else {
995 */
996 						{
997 							function->requestName = estrdup(function->functionName);
998 						}
999 
1000 						if (tmpbinding->bindingType == BINDING_SOAP) {
1001 							input = get_node_ex(operation->children, "input", WSDL_NAMESPACE);
1002 							if (input != NULL) {
1003 								sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
1004 								wsdl_soap_binding_body(&ctx, input, wsdl_soap_namespace, &soapFunctionBinding->input, function->requestParameters);
1005 							}
1006 						}
1007 					}
1008 
1009 					output = get_node_ex(portTypeOperation->children, "output", WSDL_NAMESPACE);
1010 					if (output != NULL) {
1011 						xmlAttrPtr message;
1012 
1013 						message = get_attribute(output->properties, "message");
1014 						if (message == NULL) {
1015 							soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
1016 						}
1017 						function->responseParameters = wsdl_message(&ctx, message->children->content);
1018 
1019 /* FIXME
1020 						xmlAttrPtr name = get_attribute(output->properties, "name");
1021 						if (name != NULL) {
1022 							function->responseName = estrdup(name->children->content);
1023 						} else if (input == NULL) {
1024 							function->responseName = estrdup(function->functionName);
1025 						} else {
1026 */
1027 						{
1028 							int len = strlen(function->functionName);
1029 							function->responseName = emalloc(len + sizeof("Response"));
1030 							memcpy(function->responseName, function->functionName, len);
1031 							memcpy(function->responseName+len, "Response", sizeof("Response"));
1032 						}
1033 
1034 						if (tmpbinding->bindingType == BINDING_SOAP) {
1035 							output = get_node_ex(operation->children, "output", WSDL_NAMESPACE);
1036 							if (output != NULL) {
1037 								sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
1038 								wsdl_soap_binding_body(&ctx, output, wsdl_soap_namespace, &soapFunctionBinding->output, function->responseParameters);
1039 							}
1040 						}
1041 					}
1042 
1043 					paramOrder = get_attribute(portTypeOperation->properties, "parameterOrder");
1044 					if (paramOrder) {
1045 						/* FIXME: */
1046 					}
1047 
1048 					fault = portTypeOperation->children;
1049 					while (fault != NULL) {
1050 						if (node_is_equal_ex(fault, "fault", WSDL_NAMESPACE)) {
1051 							xmlAttrPtr message, name;
1052 							sdlFaultPtr f;
1053 
1054 							name = get_attribute(fault->properties, "name");
1055 							if (name == NULL) {
1056 								soap_error1(E_ERROR, "Parsing WSDL: Missing name for <fault> of '%s'", op_name->children->content);
1057 							}
1058 							message = get_attribute(fault->properties, "message");
1059 							if (message == NULL) {
1060 								soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
1061 							}
1062 
1063 							f = emalloc(sizeof(sdlFault));
1064 							memset(f, 0, sizeof(sdlFault));
1065 
1066 							f->name = estrdup((char*)name->children->content);
1067 							f->details = wsdl_message(&ctx, message->children->content);
1068 							if (f->details == NULL || zend_hash_num_elements(f->details) > 1) {
1069 								soap_error1(E_ERROR, "Parsing WSDL: The fault message '%s' must have a single part", message->children->content);
1070 							}
1071 
1072 							if (tmpbinding->bindingType == BINDING_SOAP) {
1073 								xmlNodePtr soap_fault = get_node_with_attribute_ex(operation->children, "fault", WSDL_NAMESPACE, "name", f->name, NULL);
1074 								if (soap_fault != NULL) {
1075 									xmlNodePtr trav = soap_fault->children;
1076 									while (trav != NULL) {
1077 										if (node_is_equal_ex(trav, "fault", wsdl_soap_namespace)) {
1078 											xmlAttrPtr tmp;
1079 										  sdlSoapBindingFunctionFaultPtr binding;
1080 
1081 											binding = f->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
1082 											memset(f->bindingAttributes, 0, sizeof(sdlSoapBindingFunctionFault));
1083 
1084 											tmp = get_attribute(trav->properties, "use");
1085 											if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
1086 												binding->use = SOAP_ENCODED;
1087 											} else {
1088 												binding->use = SOAP_LITERAL;
1089 											}
1090 
1091 											tmp = get_attribute(trav->properties, "namespace");
1092 											if (tmp) {
1093 												binding->ns = estrdup((char*)tmp->children->content);
1094 											}
1095 
1096 											if (binding->use == SOAP_ENCODED) {
1097 												tmp = get_attribute(trav->properties, "encodingStyle");
1098 												if (tmp) {
1099 													if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
1100 														binding->encodingStyle = SOAP_ENCODING_1_1;
1101 													} else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
1102 														binding->encodingStyle = SOAP_ENCODING_1_2;
1103 													} else {
1104 														soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
1105 													}
1106 												} else {
1107 													soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
1108 												}
1109 											}
1110 										} else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
1111 											soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
1112 										}
1113 										trav = trav->next;
1114 									}
1115 								}
1116 							}
1117 							if (function->faults == NULL) {
1118 								function->faults = emalloc(sizeof(HashTable));
1119 								zend_hash_init(function->faults, 0, NULL, delete_fault, 0);
1120 							}
1121 							if (zend_hash_str_add_ptr(function->faults, f->name, strlen(f->name), f) == NULL) {
1122 								soap_error2(E_ERROR, "Parsing WSDL: <fault> with name '%s' already defined in '%s'", f->name, op_name->children->content);
1123 							}
1124 						}
1125 						fault = fault->next;
1126 					}
1127 
1128 					function->binding = tmpbinding;
1129 
1130 					{
1131 						char *tmp = estrdup(function->functionName);
1132 						int  len = strlen(tmp);
1133 
1134 						zend_str_tolower(tmp, len);
1135 						if (zend_hash_str_add_ptr(&ctx.sdl->functions, tmp, len, function) == NULL) {
1136 							zend_hash_next_index_insert_ptr(&ctx.sdl->functions, function);
1137 						}
1138 						efree(tmp);
1139 						if (function->requestName != NULL && strcmp(function->requestName,function->functionName) != 0) {
1140 							if (ctx.sdl->requests == NULL) {
1141 								ctx.sdl->requests = emalloc(sizeof(HashTable));
1142 								zend_hash_init(ctx.sdl->requests, 0, NULL, NULL, 0);
1143 							}
1144 							tmp = estrdup(function->requestName);
1145 							len = strlen(tmp);
1146 							zend_str_tolower(tmp, len);
1147 							zend_hash_str_add_ptr(ctx.sdl->requests, tmp, len, function);
1148 							efree(tmp);
1149 						}
1150 					}
1151 					trav2 = trav2->next;
1152 				}
1153 
1154 				if (!ctx.sdl->bindings) {
1155 					ctx.sdl->bindings = emalloc(sizeof(HashTable));
1156 					zend_hash_init(ctx.sdl->bindings, 0, NULL, delete_binding, 0);
1157 				}
1158 
1159 				if (!zend_hash_str_add_ptr(ctx.sdl->bindings, tmpbinding->name, strlen(tmpbinding->name), tmpbinding)) {
1160 					zend_hash_next_index_insert_ptr(ctx.sdl->bindings, tmpbinding);
1161 				}
1162 				trav= trav->next;
1163 			}
1164 
1165 			zend_hash_move_forward(&ctx.services);
1166 		}
1167 	} else {
1168 		soap_error0(E_ERROR, "Parsing WSDL: Couldn't bind to service");
1169 	}
1170 
1171 	if (ctx.sdl->bindings == NULL || ctx.sdl->bindings->nNumOfElements == 0) {
1172 		soap_error0(E_ERROR, "Parsing WSDL: Could not find any usable binding services in WSDL.");
1173 	}
1174 
1175 	} zend_catch {
1176 		/* Avoid persistent memory leak. */
1177 		zend_hash_destroy(&ctx.docs);
1178 		zend_bailout();
1179 	} zend_end_try();
1180 
1181 	zend_hash_destroy(&ctx.messages);
1182 	zend_hash_destroy(&ctx.bindings);
1183 	zend_hash_destroy(&ctx.portTypes);
1184 	zend_hash_destroy(&ctx.services);
1185 	zend_hash_destroy(&ctx.docs);
1186 
1187 	return ctx.sdl;
1188 }
1189 
1190 #define WSDL_CACHE_VERSION 0x10
1191 
1192 #define WSDL_CACHE_GET(ret,type,buf)   memcpy(&ret,*buf,sizeof(type)); *buf += sizeof(type);
1193 #define WSDL_CACHE_GET_INT(ret,buf)    ret = ((unsigned char)(*buf)[0])|((unsigned char)(*buf)[1]<<8)|((unsigned char)(*buf)[2]<<16)|((unsigned)(*buf)[3]<<24); *buf += 4;
1194 #define WSDL_CACHE_GET_1(ret,type,buf) ret = (type)(**buf); (*buf)++;
1195 #define WSDL_CACHE_GET_N(ret,n,buf)    memcpy(ret,*buf,n); *buf += n;
1196 #define WSDL_CACHE_SKIP(n,buf)         *buf += n;
1197 
1198 #define WSDL_CACHE_PUT_INT(val,buf)    smart_str_appendc(buf,(char)(val & 0xff)); \
1199                                        smart_str_appendc(buf,(char)((val >> 8) & 0xff)); \
1200                                        smart_str_appendc(buf,(char)((val >> 16) & 0xff)); \
1201                                        smart_str_appendc(buf,(char)((val >> 24) & 0xff));
1202 #define WSDL_CACHE_PUT_1(val,buf)      smart_str_appendc(buf,val);
1203 #define WSDL_CACHE_PUT_N(val,n,buf)    smart_str_appendl(buf,(char*)val,n);
1204 
1205 #define WSDL_NO_STRING_MARKER 0x7fffffff
1206 
sdl_deserialize_string(char ** in)1207 static char* sdl_deserialize_string(char **in)
1208 {
1209 	char *s;
1210 	int len;
1211 
1212 	WSDL_CACHE_GET_INT(len, in);
1213 	if (len == WSDL_NO_STRING_MARKER) {
1214 		return NULL;
1215 	} else {
1216 		s = emalloc(len+1);
1217 		WSDL_CACHE_GET_N(s, len, in);
1218 		s[len] = '\0';
1219 		return s;
1220 	}
1221 }
1222 
sdl_deserialize_key(HashTable * ht,void * data,char ** in)1223 static void sdl_deserialize_key(HashTable* ht, void* data, char **in)
1224 {
1225 	int len;
1226 
1227 	WSDL_CACHE_GET_INT(len, in);
1228 	if (len == WSDL_NO_STRING_MARKER) {
1229 		zend_hash_next_index_insert_ptr(ht, data);
1230 	} else {
1231 		zend_hash_str_add_ptr(ht, *in, len, data);
1232 		WSDL_CACHE_SKIP(len, in);
1233 	}
1234 }
1235 
sdl_deserialize_attribute(sdlAttributePtr attr,encodePtr * encoders,char ** in)1236 static void sdl_deserialize_attribute(sdlAttributePtr attr, encodePtr *encoders, char **in)
1237 {
1238 	int i;
1239 
1240 	attr->name = sdl_deserialize_string(in);
1241 	attr->namens = sdl_deserialize_string(in);
1242 	attr->ref = sdl_deserialize_string(in);
1243 	attr->def = sdl_deserialize_string(in);
1244 	attr->fixed = sdl_deserialize_string(in);
1245 	WSDL_CACHE_GET_1(attr->form, sdlForm, in);
1246 	WSDL_CACHE_GET_1(attr->use, sdlUse, in);
1247 	WSDL_CACHE_GET_INT(i, in);
1248 	attr->encode = encoders[i];
1249 	WSDL_CACHE_GET_INT(i, in);
1250 	if (i > 0) {
1251 		attr->extraAttributes = emalloc(sizeof(HashTable));
1252 		zend_hash_init(attr->extraAttributes, i, NULL, delete_extra_attribute, 0);
1253 		while (i > 0) {
1254 			sdlExtraAttributePtr x = emalloc(sizeof(sdlExtraAttribute));
1255 			sdl_deserialize_key(attr->extraAttributes, x, in);
1256 			x->ns = sdl_deserialize_string(in);
1257 			x->val = sdl_deserialize_string(in);
1258 			--i;
1259 		}
1260 	}
1261 }
1262 
sdl_deserialize_resriction_int(char ** in)1263 static sdlRestrictionIntPtr sdl_deserialize_resriction_int(char **in)
1264 {
1265 	if (**in == 1) {
1266 		sdlRestrictionIntPtr x = emalloc(sizeof(sdlRestrictionInt));
1267 		WSDL_CACHE_SKIP(1, in);
1268 		WSDL_CACHE_GET_INT(x->value, in);
1269 		WSDL_CACHE_GET_1(x->fixed, char, in);
1270 		return x;
1271 	} else {
1272 		WSDL_CACHE_SKIP(1, in);
1273 		return NULL;
1274 	}
1275 }
1276 
sdl_deserialize_resriction_char(char ** in)1277 static sdlRestrictionCharPtr sdl_deserialize_resriction_char(char **in)
1278 {
1279 	if (**in == 1) {
1280 		sdlRestrictionCharPtr x = emalloc(sizeof(sdlRestrictionChar));
1281 		WSDL_CACHE_SKIP(1, in);
1282 		x->value = sdl_deserialize_string(in);
1283 		WSDL_CACHE_GET_1(x->fixed, char, in);
1284 		return x;
1285 	} else {
1286 		WSDL_CACHE_SKIP(1, in);
1287 		return NULL;
1288 	}
1289 }
1290 
sdl_deserialize_model(sdlTypePtr * types,sdlTypePtr * elements,char ** in)1291 static sdlContentModelPtr sdl_deserialize_model(sdlTypePtr *types, sdlTypePtr *elements, char **in)
1292 {
1293 	int i;
1294 	sdlContentModelPtr model = emalloc(sizeof(sdlContentModel));
1295 
1296 	WSDL_CACHE_GET_1(model->kind, sdlContentKind, in);
1297 	WSDL_CACHE_GET_INT(model->min_occurs, in);
1298 	WSDL_CACHE_GET_INT(model->max_occurs, in);
1299 	switch (model->kind) {
1300 		case XSD_CONTENT_ELEMENT:
1301 			WSDL_CACHE_GET_INT(i, in);
1302 			model->u.element = elements[i];
1303 			break;
1304 		case XSD_CONTENT_SEQUENCE:
1305 		case XSD_CONTENT_ALL:
1306 		case XSD_CONTENT_CHOICE:
1307 			WSDL_CACHE_GET_INT(i, in);
1308 			model->u.content = emalloc(sizeof(HashTable));
1309 			zend_hash_init(model->u.content, i, NULL, delete_model, 0);
1310 			while (i > 0) {
1311 				sdlContentModelPtr x = sdl_deserialize_model(types, elements, in);
1312 				zend_hash_next_index_insert_ptr(model->u.content, x);
1313 				i--;
1314 			}
1315 			break;
1316 		case XSD_CONTENT_GROUP_REF:
1317 			model->u.group_ref = sdl_deserialize_string(in);
1318 			break;
1319 		case XSD_CONTENT_GROUP:
1320 			WSDL_CACHE_GET_INT(i, in);
1321 			model->u.group = types[i];
1322 			break;
1323 		default:
1324 			break;
1325 	}
1326 	return model;
1327 }
1328 
sdl_deserialize_type(sdlTypePtr type,sdlTypePtr * types,encodePtr * encoders,char ** in)1329 static void sdl_deserialize_type(sdlTypePtr type, sdlTypePtr *types, encodePtr *encoders, char **in)
1330 {
1331 	int i;
1332 	sdlTypePtr *elements = NULL;
1333 
1334 	WSDL_CACHE_GET_1(type->kind, sdlTypeKind, in);
1335 	type->name = sdl_deserialize_string(in);
1336 	type->namens = sdl_deserialize_string(in);
1337 	type->def = sdl_deserialize_string(in);
1338 	type->fixed = sdl_deserialize_string(in);
1339 	type->ref = sdl_deserialize_string(in);
1340 	WSDL_CACHE_GET_1(type->nillable, char, in);
1341 	WSDL_CACHE_GET_1(type->form, sdlForm, in);
1342 
1343 	WSDL_CACHE_GET_INT(i, in);
1344 	type->encode = encoders[i];
1345 
1346 	if (**in == 1) {
1347 		WSDL_CACHE_SKIP(1, in);
1348 		type->restrictions = emalloc(sizeof(sdlRestrictions));
1349 		/*memset(type->restrictions, 0, sizeof(sdlRestrictions));*/
1350 		type->restrictions->minExclusive = sdl_deserialize_resriction_int(in);
1351 		type->restrictions->minInclusive = sdl_deserialize_resriction_int(in);
1352 		type->restrictions->maxExclusive = sdl_deserialize_resriction_int(in);
1353 		type->restrictions->maxInclusive = sdl_deserialize_resriction_int(in);
1354 		type->restrictions->totalDigits = sdl_deserialize_resriction_int(in);
1355 		type->restrictions->fractionDigits = sdl_deserialize_resriction_int(in);
1356 		type->restrictions->length = sdl_deserialize_resriction_int(in);
1357 		type->restrictions->minLength = sdl_deserialize_resriction_int(in);
1358 		type->restrictions->maxLength = sdl_deserialize_resriction_int(in);
1359 		type->restrictions->whiteSpace = sdl_deserialize_resriction_char(in);
1360 		type->restrictions->pattern = sdl_deserialize_resriction_char(in);
1361 		WSDL_CACHE_GET_INT(i, in);
1362 		if (i > 0) {
1363 			type->restrictions->enumeration = emalloc(sizeof(HashTable));
1364 			zend_hash_init(type->restrictions->enumeration, i, NULL, delete_restriction_var_char, 0);
1365 			while (i > 0) {
1366 				sdlRestrictionCharPtr x = sdl_deserialize_resriction_char(in);
1367 				sdl_deserialize_key(type->restrictions->enumeration, x, in);
1368 				--i;
1369 			}
1370 		} else {
1371 			type->restrictions->enumeration = NULL;
1372 		}
1373 	} else {
1374 		WSDL_CACHE_SKIP(1, in);
1375 	}
1376 
1377 	WSDL_CACHE_GET_INT(i, in);
1378 	if (i > 0) {
1379 		elements = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
1380 		elements[0] = NULL;
1381 		type->elements = emalloc(sizeof(HashTable));
1382 		zend_hash_init(type->elements, i, NULL, delete_type, 0);
1383 		while (i > 0) {
1384 			sdlTypePtr t = emalloc(sizeof(sdlType));
1385 			memset(t, 0, sizeof(sdlType));
1386 			sdl_deserialize_key(type->elements, t, in);
1387 			sdl_deserialize_type(t, types, encoders, in);
1388 			elements[i] = t;
1389 			--i;
1390 		}
1391 	}
1392 
1393 	WSDL_CACHE_GET_INT(i, in);
1394 	if (i > 0) {
1395 		type->attributes = emalloc(sizeof(HashTable));
1396 		zend_hash_init(type->attributes, i, NULL, delete_attribute, 0);
1397 		while (i > 0) {
1398 			sdlAttributePtr attr = emalloc(sizeof(sdlAttribute));
1399 			memset(attr, 0, sizeof(sdlAttribute));
1400 			sdl_deserialize_key(type->attributes, attr, in);
1401 			sdl_deserialize_attribute(attr, encoders, in);
1402 			--i;
1403 		}
1404 	}
1405 
1406 	if (**in != 0) {
1407 		WSDL_CACHE_SKIP(1, in);
1408 		type->model = sdl_deserialize_model(types, elements, in);
1409 	} else {
1410 		WSDL_CACHE_SKIP(1, in);
1411 	}
1412 	if (elements != NULL) {
1413 		efree(elements);
1414 	}
1415 }
1416 
sdl_deserialize_encoder(encodePtr enc,sdlTypePtr * types,char ** in)1417 static void sdl_deserialize_encoder(encodePtr enc, sdlTypePtr *types, char **in)
1418 {
1419 	int i;
1420 
1421 	WSDL_CACHE_GET_INT(enc->details.type, in);
1422 	enc->details.type_str = sdl_deserialize_string(in);
1423 	enc->details.ns = sdl_deserialize_string(in);
1424 	WSDL_CACHE_GET_INT(i, in);
1425 	enc->details.sdl_type = types[i];
1426 	enc->to_xml = sdl_guess_convert_xml;
1427 	enc->to_zval = sdl_guess_convert_zval;
1428 
1429 	if (enc->details.sdl_type == NULL) {
1430 		int ns_len = strlen(enc->details.ns);
1431 		int type_len = strlen(enc->details.type_str);
1432 
1433 		if (((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
1434 		      memcmp(enc->details.ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
1435 		     (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
1436 		      memcmp(enc->details.ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
1437 			char *enc_nscat;
1438 			int enc_ns_len;
1439 			int enc_len;
1440 			encodePtr real_enc;
1441 
1442 			enc_ns_len = sizeof(XSD_NAMESPACE)-1;
1443 			enc_len = enc_ns_len + type_len + 1;
1444 			enc_nscat = emalloc(enc_len + 1);
1445 			memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
1446 			enc_nscat[enc_ns_len] = ':';
1447 			memcpy(enc_nscat+enc_ns_len+1, enc->details.type_str, type_len);
1448 			enc_nscat[enc_len] = '\0';
1449 
1450 			real_enc = get_encoder_ex(NULL, enc_nscat, enc_len);
1451 			efree(enc_nscat);
1452 			if (real_enc) {
1453 				enc->to_zval = real_enc->to_zval;
1454 				enc->to_xml = real_enc->to_xml;
1455 			}
1456 		}
1457 	}
1458 }
1459 
sdl_deserialize_soap_body(sdlSoapBindingFunctionBodyPtr body,encodePtr * encoders,sdlTypePtr * types,char ** in)1460 static void sdl_deserialize_soap_body(sdlSoapBindingFunctionBodyPtr body, encodePtr *encoders, sdlTypePtr *types, char **in)
1461 {
1462 	int i, j, n;
1463 
1464 	WSDL_CACHE_GET_1(body->use, sdlEncodingUse, in);
1465 	if (body->use == SOAP_ENCODED) {
1466 		WSDL_CACHE_GET_1(body->encodingStyle, sdlRpcEncodingStyle, in);
1467 	} else {
1468 		body->encodingStyle = SOAP_ENCODING_DEFAULT;
1469 	}
1470 	body->ns = sdl_deserialize_string(in);
1471 	WSDL_CACHE_GET_INT(i, in);
1472 	if (i > 0) {
1473 		body->headers = emalloc(sizeof(HashTable));
1474 		zend_hash_init(body->headers, i, NULL, delete_header, 0);
1475 		while (i > 0) {
1476 			sdlSoapBindingFunctionHeaderPtr tmp = emalloc(sizeof(sdlSoapBindingFunctionHeader));
1477 			memset(tmp, 0, sizeof(sdlSoapBindingFunctionHeader));
1478 			sdl_deserialize_key(body->headers, tmp, in);
1479 			WSDL_CACHE_GET_1(tmp->use, sdlEncodingUse, in);
1480 			if (tmp->use == SOAP_ENCODED) {
1481 				WSDL_CACHE_GET_1(tmp->encodingStyle, sdlRpcEncodingStyle, in);
1482 			} else {
1483 				tmp->encodingStyle = SOAP_ENCODING_DEFAULT;
1484 			}
1485 			tmp->name = sdl_deserialize_string(in);
1486 			tmp->ns = sdl_deserialize_string(in);
1487 			WSDL_CACHE_GET_INT(n, in);
1488 			tmp->encode = encoders[n];
1489 			WSDL_CACHE_GET_INT(n, in);
1490 			tmp->element = types[n];
1491 			--i;
1492 			WSDL_CACHE_GET_INT(j, in);
1493 			if (j > 0) {
1494 				tmp->headerfaults = emalloc(sizeof(HashTable));
1495 				zend_hash_init(tmp->headerfaults, i, NULL, delete_header, 0);
1496 				while (j > 0) {
1497 					sdlSoapBindingFunctionHeaderPtr tmp2 = emalloc(sizeof(sdlSoapBindingFunctionHeader));
1498 					memset(tmp2, 0, sizeof(sdlSoapBindingFunctionHeader));
1499 					sdl_deserialize_key(tmp->headerfaults, tmp2, in);
1500 					WSDL_CACHE_GET_1(tmp2->use, sdlEncodingUse, in);
1501 					if (tmp2->use == SOAP_ENCODED) {
1502 						WSDL_CACHE_GET_1(tmp2->encodingStyle, sdlRpcEncodingStyle, in);
1503 					} else {
1504 						tmp2->encodingStyle = SOAP_ENCODING_DEFAULT;
1505 					}
1506 					tmp2->name = sdl_deserialize_string(in);
1507 					tmp2->ns = sdl_deserialize_string(in);
1508 					WSDL_CACHE_GET_INT(n, in);
1509 					tmp2->encode = encoders[n];
1510 					WSDL_CACHE_GET_INT(n, in);
1511 					tmp2->element = types[n];
1512 					--j;
1513 				}
1514 			}
1515 		}
1516 	}
1517 }
1518 
sdl_deserialize_parameters(encodePtr * encoders,sdlTypePtr * types,char ** in)1519 static HashTable* sdl_deserialize_parameters(encodePtr *encoders, sdlTypePtr *types, char **in)
1520 {
1521 	int i, n;
1522 	HashTable *ht;
1523 
1524 	WSDL_CACHE_GET_INT(i, in);
1525 	if (i == 0) {return NULL;}
1526 	ht = emalloc(sizeof(HashTable));
1527 	zend_hash_init(ht, i, NULL, delete_parameter, 0);
1528 	while (i > 0) {
1529 		sdlParamPtr param = emalloc(sizeof(sdlParam));
1530 		sdl_deserialize_key(ht, param, in);
1531 		param->paramName = sdl_deserialize_string(in);
1532 		WSDL_CACHE_GET_INT(param->order, in);
1533 		WSDL_CACHE_GET_INT(n, in);
1534 		param->encode = encoders[n];
1535 		WSDL_CACHE_GET_INT(n, in);
1536 		param->element = types[n];
1537 		--i;
1538 	}
1539 	return ht;
1540 }
1541 
get_sdl_from_cache(const char * fn,const char * uri,size_t uri_len,time_t t,time_t * cached)1542 static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, size_t uri_len, time_t t, time_t *cached)
1543 {
1544 	sdlPtr sdl;
1545 	time_t old_t;
1546 	int  i, num_groups, num_types, num_elements, num_encoders, num_bindings, num_func;
1547 	sdlFunctionPtr *functions = NULL;
1548 	sdlBindingPtr *bindings;
1549 	sdlTypePtr *types;
1550 	encodePtr *encoders;
1551 	const encode *enc;
1552 
1553 	int f;
1554 	struct stat st;
1555 	char *in, *buf;
1556 
1557 	f = open(fn, O_RDONLY|O_BINARY);
1558 	if (f < 0) {
1559 		return NULL;
1560 	}
1561 	if (fstat(f, &st) != 0) {
1562 		close(f);
1563 		return NULL;
1564 	}
1565 	buf = in = emalloc(st.st_size);
1566 	if (read(f, in, st.st_size) != st.st_size) {
1567 		close(f);
1568 		efree(in);
1569 		return NULL;
1570 	}
1571 	close(f);
1572 
1573 	if (strncmp(in,"wsdl",4) != 0 || in[4] != WSDL_CACHE_VERSION || in[5] != '\0') {
1574 		unlink(fn);
1575 		efree(buf);
1576 		return NULL;
1577 	}
1578 	in += 6;
1579 
1580 	WSDL_CACHE_GET(old_t, time_t, &in);
1581 	if (old_t < t) {
1582 		unlink(fn);
1583 		efree(buf);
1584 		return NULL;
1585 	}
1586 	*cached = old_t;
1587 
1588 	WSDL_CACHE_GET_INT(i, &in);
1589 	if (i != uri_len || strncmp(in, uri, i) != 0) {
1590 		unlink(fn);
1591 		efree(buf);
1592 		return NULL;
1593 	}
1594 	WSDL_CACHE_SKIP(i, &in);
1595 
1596 	sdl = emalloc(sizeof(*sdl));
1597 	memset(sdl, 0, sizeof(*sdl));
1598 
1599 	sdl->source = sdl_deserialize_string(&in);
1600 	sdl->target_ns = sdl_deserialize_string(&in);
1601 
1602 	WSDL_CACHE_GET_INT(num_groups, &in);
1603 	WSDL_CACHE_GET_INT(num_types, &in);
1604 	WSDL_CACHE_GET_INT(num_elements, &in);
1605 	WSDL_CACHE_GET_INT(num_encoders, &in);
1606 
1607 	i = num_groups+num_types+num_elements;
1608 	types = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
1609 	types[0] = NULL;
1610 	while (i > 0) {
1611 		types[i] = emalloc(sizeof(sdlType));
1612 		memset(types[i], 0, sizeof(sdlType));
1613 		i--;
1614 	}
1615 
1616 	i = num_encoders;
1617 	enc = defaultEncoding;
1618 	while (enc->details.type != END_KNOWN_TYPES) {
1619 		i++; enc++;
1620 	}
1621 	encoders = safe_emalloc((i+1), sizeof(encodePtr), 0);
1622 	i = num_encoders;
1623 	encoders[0] = NULL;
1624 	while (i > 0) {
1625 		encoders[i] = emalloc(sizeof(encode));
1626 		memset(encoders[i], 0, sizeof(encode));
1627 		i--;
1628 	}
1629 	i = num_encoders;
1630 	enc = defaultEncoding;
1631 	while (enc->details.type != END_KNOWN_TYPES) {
1632 		encoders[++i] = (encodePtr)enc++;
1633 	}
1634 
1635 	i = 1;
1636 	if (num_groups > 0) {
1637 		sdl->groups = emalloc(sizeof(HashTable));
1638 		zend_hash_init(sdl->groups, num_groups, NULL, delete_type, 0);
1639 		while (i < num_groups+1) {
1640 			sdl_deserialize_key(sdl->groups, types[i], &in);
1641 			sdl_deserialize_type(types[i], types, encoders, &in);
1642 			i++;
1643 		}
1644 	}
1645 
1646 	if (num_types > 0) {
1647 		sdl->types = emalloc(sizeof(HashTable));
1648 		zend_hash_init(sdl->types, num_types, NULL, delete_type, 0);
1649 		while (i < num_groups+num_types+1) {
1650 			sdl_deserialize_key(sdl->types, types[i], &in);
1651 			sdl_deserialize_type(types[i], types, encoders, &in);
1652 			i++;
1653 		}
1654 	}
1655 
1656 	if (num_elements > 0) {
1657 		sdl->elements = emalloc(sizeof(HashTable));
1658 		zend_hash_init(sdl->elements, num_elements, NULL, delete_type, 0);
1659 		while (i < num_groups+num_types+num_elements+1) {
1660 			sdl_deserialize_key(sdl->elements, types[i], &in);
1661 			sdl_deserialize_type(types[i], types, encoders, &in);
1662 			i++;
1663 		}
1664 	}
1665 
1666 	i = 1;
1667 	if (num_encoders > 0) {
1668 		sdl->encoders = emalloc(sizeof(HashTable));
1669 		zend_hash_init(sdl->encoders, num_encoders, NULL, delete_encoder, 0);
1670 		while (i < num_encoders+1) {
1671 			sdl_deserialize_key(sdl->encoders, encoders[i], &in);
1672 			sdl_deserialize_encoder(encoders[i], types, &in);
1673 			i++;
1674 		}
1675 	}
1676 
1677 	/* deserialize bindings */
1678 	WSDL_CACHE_GET_INT(num_bindings, &in);
1679 	bindings = safe_emalloc(num_bindings, sizeof(sdlBindingPtr), 0);
1680 	if (num_bindings > 0) {
1681 		sdl->bindings = emalloc(sizeof(HashTable));
1682 		zend_hash_init(sdl->bindings, num_bindings, NULL, delete_binding, 0);
1683 		for (i = 0; i < num_bindings; i++) {
1684 			sdlBindingPtr binding = emalloc(sizeof(sdlBinding));
1685 			memset(binding, 0, sizeof(sdlBinding));
1686 			sdl_deserialize_key(sdl->bindings, binding, &in);
1687 			binding->name = sdl_deserialize_string(&in);
1688 			binding->location = sdl_deserialize_string(&in);
1689 			WSDL_CACHE_GET_1(binding->bindingType,sdlBindingType,&in);
1690 			if (binding->bindingType == BINDING_SOAP && *in != 0) {
1691 			  sdlSoapBindingPtr soap_binding = binding->bindingAttributes = emalloc(sizeof(sdlSoapBinding));
1692 				WSDL_CACHE_GET_1(soap_binding->style,sdlEncodingStyle,&in);
1693 				WSDL_CACHE_GET_1(soap_binding->transport,sdlTransport,&in);
1694 			} else {
1695 				WSDL_CACHE_SKIP(1,&in);
1696 			}
1697 			bindings[i] = binding;
1698 		}
1699 	}
1700 
1701 	/* deserialize functions */
1702 	WSDL_CACHE_GET_INT(num_func, &in);
1703 	zend_hash_init(&sdl->functions, num_func, NULL, delete_function, 0);
1704 	if (num_func > 0) {
1705 		functions = safe_emalloc(num_func, sizeof(sdlFunctionPtr), 0);
1706 		for (i = 0; i < num_func; i++) {
1707 			int binding_num, num_faults;
1708 			sdlFunctionPtr func = emalloc(sizeof(sdlFunction));
1709 			sdl_deserialize_key(&sdl->functions, func, &in);
1710 			func->functionName = sdl_deserialize_string(&in);
1711 			func->requestName = sdl_deserialize_string(&in);
1712 			func->responseName = sdl_deserialize_string(&in);
1713 
1714 			WSDL_CACHE_GET_INT(binding_num, &in);
1715 			if (binding_num == 0) {
1716 				func->binding = NULL;
1717 			} else {
1718 				func->binding = bindings[binding_num-1];
1719 			}
1720 			if (func->binding && func->binding->bindingType == BINDING_SOAP && *in != 0) {
1721 				sdlSoapBindingFunctionPtr binding = func->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunction));
1722 				memset(binding, 0, sizeof(sdlSoapBindingFunction));
1723 				WSDL_CACHE_GET_1(binding->style,sdlEncodingStyle,&in);
1724 				binding->soapAction = sdl_deserialize_string(&in);
1725 				sdl_deserialize_soap_body(&binding->input, encoders, types, &in);
1726 				sdl_deserialize_soap_body(&binding->output, encoders, types, &in);
1727 			} else {
1728 				WSDL_CACHE_SKIP(1, &in);
1729 				func->bindingAttributes = NULL;
1730 			}
1731 
1732 			func->requestParameters = sdl_deserialize_parameters(encoders, types, &in);
1733 			func->responseParameters = sdl_deserialize_parameters(encoders, types, &in);
1734 
1735 			WSDL_CACHE_GET_INT(num_faults, &in);
1736 			if (num_faults > 0) {
1737 			  int j;
1738 
1739 				func->faults = emalloc(sizeof(HashTable));
1740 				zend_hash_init(func->faults, num_faults, NULL, delete_fault, 0);
1741 
1742 				for (j = 0; j < num_faults; j++) {
1743 					sdlFaultPtr fault = emalloc(sizeof(sdlFault));
1744 
1745 					sdl_deserialize_key(func->faults, fault, &in);
1746 					fault->name =sdl_deserialize_string(&in);
1747 					fault->details =sdl_deserialize_parameters(encoders, types, &in);
1748 					if (*in != 0) {
1749 						sdlSoapBindingFunctionFaultPtr binding = fault->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
1750 						memset(binding, 0, sizeof(sdlSoapBindingFunctionFault));
1751 						WSDL_CACHE_GET_1(binding->use,sdlEncodingUse,&in);
1752 						if (binding->use == SOAP_ENCODED) {
1753 							WSDL_CACHE_GET_1(binding->encodingStyle, sdlRpcEncodingStyle, &in);
1754 						} else {
1755 							binding->encodingStyle = SOAP_ENCODING_DEFAULT;
1756 						}
1757 						binding->ns = sdl_deserialize_string(&in);
1758 					} else {
1759 						WSDL_CACHE_SKIP(1, &in);
1760 						fault->bindingAttributes = NULL;
1761 					}
1762 				}
1763 			} else {
1764 				func->faults = NULL;
1765 			}
1766 			functions[i] = func;
1767 		}
1768 	}
1769 
1770 	/* deserialize requests */
1771 	WSDL_CACHE_GET_INT(i, &in);
1772 	if (i > 0) {
1773 		sdl->requests = emalloc(sizeof(HashTable));
1774 		zend_hash_init(sdl->requests, i, NULL, NULL, 0);
1775 		while (i > 0) {
1776 			int function_num;
1777 
1778 			WSDL_CACHE_GET_INT(function_num, &in);
1779 			sdl_deserialize_key(sdl->requests, functions[function_num-1], &in);
1780 			i--;
1781 		}
1782 	}
1783 
1784 	if (functions) {
1785 		efree(functions);
1786 	}
1787 	efree(bindings);
1788 	efree(encoders);
1789 	efree(types);
1790 	efree(buf);
1791 	return sdl;
1792 }
1793 
sdl_serialize_string(const char * str,smart_str * out)1794 static void sdl_serialize_string(const char *str, smart_str *out)
1795 {
1796 	if (str) {
1797 		int i = strlen(str);
1798 		WSDL_CACHE_PUT_INT(i, out);
1799 		if (i > 0) {
1800 			WSDL_CACHE_PUT_N(str, i, out);
1801 		}
1802 	} else {
1803 		WSDL_CACHE_PUT_INT(WSDL_NO_STRING_MARKER, out);
1804 	}
1805 }
1806 
1807 // TODO: refactor it
sdl_serialize_key(zend_string * key,smart_str * out)1808 static void sdl_serialize_key(zend_string *key, smart_str *out)
1809 {
1810 	if (key) {
1811 		WSDL_CACHE_PUT_INT(ZSTR_LEN(key), out);
1812 		WSDL_CACHE_PUT_N(ZSTR_VAL(key), ZSTR_LEN(key), out);
1813 	} else {
1814 		WSDL_CACHE_PUT_INT(WSDL_NO_STRING_MARKER, out);
1815 	}
1816 }
1817 
sdl_serialize_encoder_ref(encodePtr enc,HashTable * tmp_encoders,smart_str * out)1818 static void sdl_serialize_encoder_ref(encodePtr enc, HashTable *tmp_encoders, smart_str *out) {
1819 	if (enc) {
1820 		zval *encoder_num;
1821 		if ((encoder_num = zend_hash_str_find(tmp_encoders, (char*)&enc, sizeof(enc))) != 0) {
1822 			WSDL_CACHE_PUT_INT(Z_LVAL_P(encoder_num), out);
1823 		} else {
1824 			WSDL_CACHE_PUT_INT(0, out);
1825 		}
1826 	} else {
1827 		WSDL_CACHE_PUT_INT(0, out);
1828 	}
1829 }
1830 
sdl_serialize_type_ref(sdlTypePtr type,HashTable * tmp_types,smart_str * out)1831 static void sdl_serialize_type_ref(sdlTypePtr type, HashTable *tmp_types, smart_str *out) {
1832 	if (type) {
1833 		zval *type_num;
1834 		if ((type_num = zend_hash_str_find(tmp_types, (char*)&type, sizeof(type))) != NULL) {
1835 			WSDL_CACHE_PUT_INT(Z_LVAL_P(type_num), out);
1836 		} else {
1837 			WSDL_CACHE_PUT_INT(0, out);
1838 		}
1839 	} else {
1840 		WSDL_CACHE_PUT_INT(0,out);
1841 	}
1842 }
1843 
sdl_serialize_attribute(sdlAttributePtr attr,HashTable * tmp_encoders,smart_str * out)1844 static void sdl_serialize_attribute(sdlAttributePtr attr, HashTable *tmp_encoders, smart_str *out)
1845 {
1846 	int i;
1847 
1848 	sdl_serialize_string(attr->name, out);
1849 	sdl_serialize_string(attr->namens, out);
1850 	sdl_serialize_string(attr->ref, out);
1851 	sdl_serialize_string(attr->def, out);
1852 	sdl_serialize_string(attr->fixed, out);
1853 	WSDL_CACHE_PUT_1(attr->form, out);
1854 	WSDL_CACHE_PUT_1(attr->use, out);
1855 	sdl_serialize_encoder_ref(attr->encode, tmp_encoders, out);
1856 	if (attr->extraAttributes) {
1857 		i = zend_hash_num_elements(attr->extraAttributes);
1858 	} else {
1859 		i = 0;
1860 	}
1861 	WSDL_CACHE_PUT_INT(i, out);
1862 	if (i > 0) {
1863 		sdlExtraAttributePtr tmp;
1864 		zend_string *key;
1865 
1866 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(attr->extraAttributes, key, tmp) {
1867 			sdl_serialize_key(key, out);
1868 			sdl_serialize_string(tmp->ns, out);
1869 			sdl_serialize_string(tmp->val, out);
1870 		} ZEND_HASH_FOREACH_END();
1871 	}
1872 }
1873 
sdl_serialize_model(sdlContentModelPtr model,HashTable * tmp_types,HashTable * tmp_elements,smart_str * out)1874 static void sdl_serialize_model(sdlContentModelPtr model, HashTable *tmp_types, HashTable *tmp_elements, smart_str *out)
1875 {
1876 	WSDL_CACHE_PUT_1(model->kind, out);
1877 	WSDL_CACHE_PUT_INT(model->min_occurs, out);
1878 	WSDL_CACHE_PUT_INT(model->max_occurs, out);
1879 	switch (model->kind) {
1880 		case XSD_CONTENT_ELEMENT:
1881 			sdl_serialize_type_ref(model->u.element, tmp_elements, out);
1882 			break;
1883 		case XSD_CONTENT_SEQUENCE:
1884 		case XSD_CONTENT_ALL:
1885 		case XSD_CONTENT_CHOICE: {
1886 				sdlContentModelPtr tmp;
1887 				int i = zend_hash_num_elements(model->u.content);
1888 
1889 				WSDL_CACHE_PUT_INT(i, out);
1890 				ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
1891 					sdl_serialize_model(tmp, tmp_types, tmp_elements, out);
1892 				} ZEND_HASH_FOREACH_END();
1893 			}
1894 			break;
1895 		case XSD_CONTENT_GROUP_REF:
1896 			sdl_serialize_string(model->u.group_ref,out);
1897 			break;
1898 		case XSD_CONTENT_GROUP:
1899 			sdl_serialize_type_ref(model->u.group, tmp_types, out);
1900 			break;
1901 		default:
1902 			break;
1903 	}
1904 }
1905 
sdl_serialize_resriction_int(sdlRestrictionIntPtr x,smart_str * out)1906 static void sdl_serialize_resriction_int(sdlRestrictionIntPtr x, smart_str *out)
1907 {
1908 	if (x) {
1909 		WSDL_CACHE_PUT_1(1, out);
1910 		WSDL_CACHE_PUT_INT(x->value, out);
1911 		WSDL_CACHE_PUT_1(x->fixed, out);
1912 	} else {
1913 		WSDL_CACHE_PUT_1(0, out);
1914 	}
1915 }
1916 
sdl_serialize_resriction_char(sdlRestrictionCharPtr x,smart_str * out)1917 static void sdl_serialize_resriction_char(sdlRestrictionCharPtr x, smart_str *out)
1918 {
1919 	if (x) {
1920 		WSDL_CACHE_PUT_1(1, out);
1921 		sdl_serialize_string(x->value, out);
1922 		WSDL_CACHE_PUT_1(x->fixed, out);
1923 	} else {
1924 		WSDL_CACHE_PUT_1(0, out);
1925 	}
1926 }
1927 
sdl_serialize_type(sdlTypePtr type,HashTable * tmp_encoders,HashTable * tmp_types,smart_str * out)1928 static void sdl_serialize_type(sdlTypePtr type, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
1929 {
1930 	int i;
1931 	HashTable *tmp_elements = NULL;
1932 
1933 	WSDL_CACHE_PUT_1(type->kind, out);
1934 	sdl_serialize_string(type->name, out);
1935 	sdl_serialize_string(type->namens, out);
1936 	sdl_serialize_string(type->def, out);
1937 	sdl_serialize_string(type->fixed, out);
1938 	sdl_serialize_string(type->ref, out);
1939 	WSDL_CACHE_PUT_1(type->nillable, out);
1940 	WSDL_CACHE_PUT_1(type->form, out);
1941 	sdl_serialize_encoder_ref(type->encode, tmp_encoders, out);
1942 
1943 	if (type->restrictions) {
1944 		WSDL_CACHE_PUT_1(1, out);
1945 		sdl_serialize_resriction_int(type->restrictions->minExclusive,out);
1946 		sdl_serialize_resriction_int(type->restrictions->minInclusive,out);
1947 		sdl_serialize_resriction_int(type->restrictions->maxExclusive,out);
1948 		sdl_serialize_resriction_int(type->restrictions->maxInclusive,out);
1949 		sdl_serialize_resriction_int(type->restrictions->totalDigits,out);
1950 		sdl_serialize_resriction_int(type->restrictions->fractionDigits,out);
1951 		sdl_serialize_resriction_int(type->restrictions->length,out);
1952 		sdl_serialize_resriction_int(type->restrictions->minLength,out);
1953 		sdl_serialize_resriction_int(type->restrictions->maxLength,out);
1954 		sdl_serialize_resriction_char(type->restrictions->whiteSpace,out);
1955 		sdl_serialize_resriction_char(type->restrictions->pattern,out);
1956 		if (type->restrictions->enumeration) {
1957 			i = zend_hash_num_elements(type->restrictions->enumeration);
1958 		} else {
1959 			i = 0;
1960 		}
1961 		WSDL_CACHE_PUT_INT(i, out);
1962 		if (i > 0) {
1963 			sdlRestrictionCharPtr tmp;
1964 			zend_string *key;
1965 
1966 			ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(type->restrictions->enumeration, key, tmp) {
1967 				sdl_serialize_resriction_char(tmp, out);
1968 				sdl_serialize_key(key, out);
1969 			} ZEND_HASH_FOREACH_END();
1970 		}
1971 	} else {
1972 		WSDL_CACHE_PUT_1(0, out);
1973 	}
1974 	if (type->elements) {
1975 		i = zend_hash_num_elements(type->elements);
1976 	} else {
1977 		i = 0;
1978 	}
1979 	WSDL_CACHE_PUT_INT(i, out);
1980 	if (i > 0) {
1981 		sdlTypePtr tmp;
1982 		zend_string *key;
1983 		zval zv;
1984 
1985 		tmp_elements = emalloc(sizeof(HashTable));
1986 		zend_hash_init(tmp_elements, i, NULL, NULL, 0);
1987 
1988 		ZEND_HASH_FOREACH_STR_KEY_PTR(type->elements, key, tmp) {
1989 			sdl_serialize_key(key, out);
1990 			sdl_serialize_type(tmp, tmp_encoders, tmp_types, out);
1991 			ZVAL_LONG(&zv, i);
1992 			zend_hash_str_add(tmp_elements, (char*)&tmp, sizeof(tmp), &zv);
1993 			i--;
1994 		} ZEND_HASH_FOREACH_END();
1995 	}
1996 
1997 	if (type->attributes) {
1998 		i = zend_hash_num_elements(type->attributes);
1999 	} else {
2000 		i = 0;
2001 	}
2002 	WSDL_CACHE_PUT_INT(i, out);
2003 	if (i > 0) {
2004 		sdlAttributePtr tmp;
2005 		zend_string *key;
2006 
2007 		ZEND_HASH_FOREACH_STR_KEY_PTR(type->attributes, key, tmp) {
2008 			sdl_serialize_key(key, out);
2009 			sdl_serialize_attribute(tmp, tmp_encoders, out);
2010 		} ZEND_HASH_FOREACH_END();
2011 	}
2012 	if (type->model) {
2013 		WSDL_CACHE_PUT_1(1, out);
2014 		sdl_serialize_model(type->model, tmp_types, tmp_elements, out);
2015 	} else {
2016 		WSDL_CACHE_PUT_1(0, out);
2017 	}
2018 	if (tmp_elements != NULL) {
2019 		zend_hash_destroy(tmp_elements);
2020 		efree(tmp_elements);
2021 	}
2022 }
2023 
sdl_serialize_encoder(encodePtr enc,HashTable * tmp_types,smart_str * out)2024 static void sdl_serialize_encoder(encodePtr enc, HashTable *tmp_types, smart_str *out)
2025 {
2026 	WSDL_CACHE_PUT_INT(enc->details.type, out);
2027 	sdl_serialize_string(enc->details.type_str, out);
2028 	sdl_serialize_string(enc->details.ns, out);
2029 	sdl_serialize_type_ref(enc->details.sdl_type, tmp_types, out);
2030 }
2031 
sdl_serialize_parameters(HashTable * ht,HashTable * tmp_encoders,HashTable * tmp_types,smart_str * out)2032 static void sdl_serialize_parameters(HashTable *ht, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
2033 {
2034 	int i;
2035 
2036 	if (ht) {
2037 		i = zend_hash_num_elements(ht);
2038 	} else {
2039 		i = 0;
2040 	}
2041 	WSDL_CACHE_PUT_INT(i, out);
2042 	if (i > 0) {
2043 		sdlParamPtr tmp;
2044 		zend_string *key;
2045 
2046 		ZEND_HASH_FOREACH_STR_KEY_PTR(ht, key, tmp) {
2047 			sdl_serialize_key(key, out);
2048 			sdl_serialize_string(tmp->paramName, out);
2049 			WSDL_CACHE_PUT_INT(tmp->order, out);
2050 			sdl_serialize_encoder_ref(tmp->encode, tmp_encoders, out);
2051 			sdl_serialize_type_ref(tmp->element, tmp_types, out);
2052 		} ZEND_HASH_FOREACH_END();
2053 	}
2054 }
2055 
sdl_serialize_soap_body(sdlSoapBindingFunctionBodyPtr body,HashTable * tmp_encoders,HashTable * tmp_types,smart_str * out)2056 static void sdl_serialize_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
2057 {
2058 	int i, j;
2059 
2060 	WSDL_CACHE_PUT_1(body->use, out);
2061 	if (body->use == SOAP_ENCODED) {
2062 		WSDL_CACHE_PUT_1(body->encodingStyle, out);
2063 	}
2064 	sdl_serialize_string(body->ns, out);
2065 	if (body->headers) {
2066 		i = zend_hash_num_elements(body->headers);
2067 	} else {
2068 		i = 0;
2069 	}
2070 	WSDL_CACHE_PUT_INT(i, out);
2071 	if (i > 0) {
2072 		sdlSoapBindingFunctionHeaderPtr tmp;
2073 		zend_string *key;
2074 
2075 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(body->headers, key, tmp) {
2076 			sdl_serialize_key(key, out);
2077 			WSDL_CACHE_PUT_1(tmp->use, out);
2078 			if (tmp->use == SOAP_ENCODED) {
2079 				WSDL_CACHE_PUT_1(tmp->encodingStyle, out);
2080 			}
2081 			sdl_serialize_string(tmp->name, out);
2082 			sdl_serialize_string(tmp->ns, out);
2083 			sdl_serialize_encoder_ref(tmp->encode, tmp_encoders, out);
2084 			sdl_serialize_type_ref(tmp->element, tmp_types, out);
2085 			if (tmp->headerfaults) {
2086 				j = zend_hash_num_elements(tmp->headerfaults);
2087 			} else {
2088 				j = 0;
2089 			}
2090 			WSDL_CACHE_PUT_INT(j, out);
2091 			if (j > 0) {
2092 				sdlSoapBindingFunctionHeaderPtr tmp2;
2093 				zend_string *key;
2094 
2095 				ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(body->headers, key, tmp2) {
2096 					sdl_serialize_key(key, out);
2097 					WSDL_CACHE_PUT_1(tmp2->use, out);
2098 					if (tmp2->use == SOAP_ENCODED) {
2099 						WSDL_CACHE_PUT_1(tmp2->encodingStyle, out);
2100 					}
2101 					sdl_serialize_string(tmp2->name, out);
2102 					sdl_serialize_string(tmp2->ns, out);
2103 					sdl_serialize_encoder_ref(tmp2->encode, tmp_encoders, out);
2104 					sdl_serialize_type_ref(tmp2->element, tmp_types, out);
2105 				} ZEND_HASH_FOREACH_END();
2106 			}
2107 		} ZEND_HASH_FOREACH_END();
2108 	}
2109 }
2110 
add_sdl_to_cache(const char * fn,const char * uri,time_t t,sdlPtr sdl)2111 static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr sdl)
2112 {
2113 	smart_str buf = {0};
2114 	smart_str *out = &buf;
2115 	int i;
2116 	int type_num = 1;
2117 	int encoder_num = 1;
2118 	int f;
2119 	const encode *enc;
2120 	HashTable tmp_types;
2121 	HashTable tmp_encoders;
2122 	HashTable tmp_bindings;
2123 	HashTable tmp_functions;
2124 
2125 	/* To avoid race conditions, we first create a temporary file and then rename it atomically
2126 	 * at the end of the function. (see bug #66150) */
2127 	zend_string *temp_file_path;
2128 	f = php_open_temporary_fd_ex(SOAP_GLOBAL(cache_dir), "tmp.wsdl.", &temp_file_path, PHP_TMP_FILE_SILENT);
2129 
2130 	if (f < 0) {return;}
2131 
2132 	zend_hash_init(&tmp_types, 0, NULL, NULL, 0);
2133 	zend_hash_init(&tmp_encoders, 0, NULL, NULL, 0);
2134 	zend_hash_init(&tmp_bindings, 0, NULL, NULL, 0);
2135 	zend_hash_init(&tmp_functions, 0, NULL, NULL, 0);
2136 
2137 	WSDL_CACHE_PUT_N("wsdl", 4, out);
2138 	WSDL_CACHE_PUT_1(WSDL_CACHE_VERSION,out);
2139 	WSDL_CACHE_PUT_1(0,out);
2140 	WSDL_CACHE_PUT_N(&t, sizeof(t), out);
2141 
2142 	sdl_serialize_string(uri, out);
2143 	sdl_serialize_string(sdl->source, out);
2144 	sdl_serialize_string(sdl->target_ns, out);
2145 
2146 	if (sdl->groups) {
2147 		i = zend_hash_num_elements(sdl->groups);
2148 	} else {
2149 		i = 0;
2150 	}
2151 	WSDL_CACHE_PUT_INT(i, out);
2152 	if (i > 0) {
2153 		sdlTypePtr tmp;
2154 		zval zv;
2155 
2156 		ZEND_HASH_MAP_FOREACH_PTR(sdl->groups, tmp) {
2157 			ZVAL_LONG(&zv, type_num);
2158 			zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
2159 			++type_num;
2160 		} ZEND_HASH_FOREACH_END();
2161 	}
2162 
2163 	if (sdl->types) {
2164 		i = zend_hash_num_elements(sdl->types);
2165 	} else {
2166 		i = 0;
2167 	}
2168 	WSDL_CACHE_PUT_INT(i, out);
2169 	if (i > 0) {
2170 		sdlTypePtr tmp;
2171 		zval zv;
2172 
2173 		ZEND_HASH_FOREACH_PTR(sdl->types, tmp) {
2174 			ZVAL_LONG(&zv,  type_num);
2175 			zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
2176 			++type_num;
2177 		} ZEND_HASH_FOREACH_END();
2178 	}
2179 
2180 	if (sdl->elements) {
2181 		i = zend_hash_num_elements(sdl->elements);
2182 	} else {
2183 		i = 0;
2184 	}
2185 	WSDL_CACHE_PUT_INT(i, out);
2186 	if (i > 0) {
2187 		sdlTypePtr tmp;
2188 		zval zv;
2189 
2190 		ZEND_HASH_MAP_FOREACH_PTR(sdl->elements, tmp) {
2191 			ZVAL_LONG(&zv, type_num);
2192 			zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
2193 			++type_num;
2194 		} ZEND_HASH_FOREACH_END();
2195 	}
2196 
2197 	if (sdl->encoders) {
2198 		i = zend_hash_num_elements(sdl->encoders);
2199 	} else {
2200 		i = 0;
2201 	}
2202 	WSDL_CACHE_PUT_INT(i, out);
2203 	if (i > 0) {
2204 		encodePtr tmp;
2205 		zval zv;
2206 
2207 		ZEND_HASH_FOREACH_PTR(sdl->encoders, tmp) {
2208 			ZVAL_LONG(&zv, encoder_num);
2209 			zend_hash_str_add(&tmp_encoders, (char*)&tmp, sizeof(tmp), &zv);
2210 			++encoder_num;
2211 		} ZEND_HASH_FOREACH_END();
2212 	}
2213 	enc = defaultEncoding;
2214 	while (enc->details.type != END_KNOWN_TYPES) {
2215 		zval zv;
2216 
2217 		ZVAL_LONG(&zv, encoder_num);
2218 		zend_hash_str_add(&tmp_encoders, (char*)&enc, sizeof(encodePtr), &zv);
2219 		enc++;
2220 		++encoder_num;
2221 	}
2222 
2223 	if (sdl->groups) {
2224 		sdlTypePtr tmp;
2225 		zend_string *key;
2226 
2227 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->groups, key, tmp) {
2228 			sdl_serialize_key(key, out);
2229 			sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
2230 		} ZEND_HASH_FOREACH_END();
2231 	}
2232 
2233 	if (sdl->types) {
2234 		sdlTypePtr tmp;
2235 		zend_string *key;
2236 
2237 		ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->types, key, tmp) {
2238 			sdl_serialize_key(key, out);
2239 			sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
2240 		} ZEND_HASH_FOREACH_END();
2241 	}
2242 
2243 	if (sdl->elements) {
2244 		sdlTypePtr tmp;
2245 		zend_string *key;
2246 
2247 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->elements, key, tmp) {
2248 			sdl_serialize_key(key, out);
2249 			sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
2250 		} ZEND_HASH_FOREACH_END();
2251 	}
2252 
2253 	if (sdl->encoders) {
2254 		encodePtr tmp;
2255 		zend_string *key;
2256 
2257 		ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->encoders, key, tmp) {
2258 			sdl_serialize_key(key, out);
2259 			sdl_serialize_encoder(tmp, &tmp_types, out);
2260 		} ZEND_HASH_FOREACH_END();
2261 	}
2262 
2263 	/* serialize bindings */
2264 	if (sdl->bindings) {
2265 		i = zend_hash_num_elements(sdl->bindings);
2266 	} else {
2267 		i = 0;
2268 	}
2269 	WSDL_CACHE_PUT_INT(i, out);
2270 	if (i > 0) {
2271 		sdlBindingPtr tmp;
2272 		int binding_num = 1;
2273 		zval zv;
2274 		zend_string *key;
2275 
2276 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->bindings, key, tmp) {
2277 			sdl_serialize_key(key, out);
2278 			sdl_serialize_string(tmp->name, out);
2279 			sdl_serialize_string(tmp->location, out);
2280 			WSDL_CACHE_PUT_1(tmp->bindingType,out);
2281 			if (tmp->bindingType == BINDING_SOAP && tmp->bindingAttributes != NULL) {
2282 				sdlSoapBindingPtr binding = (sdlSoapBindingPtr)tmp->bindingAttributes;
2283 				WSDL_CACHE_PUT_1(binding->style, out);
2284 				WSDL_CACHE_PUT_1(binding->transport, out);
2285 			} else {
2286 				WSDL_CACHE_PUT_1(0,out);
2287 			}
2288 
2289 			ZVAL_LONG(&zv, binding_num);
2290 			zend_hash_str_add(&tmp_bindings, (char*)&tmp, sizeof(tmp), &zv);
2291 			binding_num++;
2292 		} ZEND_HASH_FOREACH_END();
2293 	}
2294 
2295 	/* serialize functions */
2296 	i = zend_hash_num_elements(&sdl->functions);
2297 	WSDL_CACHE_PUT_INT(i, out);
2298 	if (i > 0) {
2299 		sdlFunctionPtr tmp;
2300 		zval *binding_num, zv;
2301 		int function_num = 1;
2302 		zend_string *key;
2303 
2304 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&sdl->functions, key, tmp) {
2305 			sdl_serialize_key(key, out);
2306 			sdl_serialize_string(tmp->functionName, out);
2307 			sdl_serialize_string(tmp->requestName, out);
2308 			sdl_serialize_string(tmp->responseName, out);
2309 
2310 			if (tmp->binding) {
2311 				binding_num = zend_hash_str_find(&tmp_bindings,(char*)&tmp->binding, sizeof(tmp->binding));
2312 				if (binding_num) {
2313 					WSDL_CACHE_PUT_INT(Z_LVAL_P(binding_num), out);
2314 					if (Z_LVAL_P(binding_num) >= 0) {
2315 						if (tmp->binding->bindingType == BINDING_SOAP && tmp->bindingAttributes != NULL) {
2316 							sdlSoapBindingFunctionPtr binding = (sdlSoapBindingFunctionPtr)tmp->bindingAttributes;
2317 							WSDL_CACHE_PUT_1(binding->style, out);
2318 							sdl_serialize_string(binding->soapAction, out);
2319 							sdl_serialize_soap_body(&binding->input, &tmp_encoders, &tmp_types, out);
2320 							sdl_serialize_soap_body(&binding->output, &tmp_encoders, &tmp_types, out);
2321 						} else {
2322 							WSDL_CACHE_PUT_1(0,out);
2323 						}
2324 					}
2325 				}
2326 			}
2327 			sdl_serialize_parameters(tmp->requestParameters, &tmp_encoders, &tmp_types, out);
2328 			sdl_serialize_parameters(tmp->responseParameters, &tmp_encoders, &tmp_types, out);
2329 
2330 			if (tmp->faults) {
2331 				sdlFaultPtr fault;
2332 				zend_string *key;
2333 
2334 				WSDL_CACHE_PUT_INT(zend_hash_num_elements(tmp->faults), out);
2335 
2336 				ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(tmp->faults, key, fault) {
2337 					sdl_serialize_key(key, out);
2338 					sdl_serialize_string(fault->name, out);
2339 					sdl_serialize_parameters(fault->details, &tmp_encoders, &tmp_types, out);
2340 					if (tmp->binding->bindingType == BINDING_SOAP && fault->bindingAttributes != NULL) {
2341 						sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
2342 						WSDL_CACHE_PUT_1(binding->use, out);
2343 						if (binding->use == SOAP_ENCODED) {
2344 							WSDL_CACHE_PUT_1(binding->encodingStyle, out);
2345 						}
2346 						sdl_serialize_string(binding->ns, out);
2347 					} else {
2348 						WSDL_CACHE_PUT_1(0, out);
2349 					}
2350 				} ZEND_HASH_FOREACH_END();
2351 			} else {
2352 				WSDL_CACHE_PUT_INT(0, out);
2353 			}
2354 
2355 			ZVAL_LONG(&zv, function_num);
2356 			zend_hash_str_add(&tmp_functions, (char*)&tmp, sizeof(tmp), &zv);
2357 			function_num++;
2358 		} ZEND_HASH_FOREACH_END();
2359 	}
2360 
2361 	/* serialize requests */
2362 	if (sdl->requests) {
2363 		i = zend_hash_num_elements(sdl->requests);
2364 	} else {
2365 		i = 0;
2366 	}
2367 	WSDL_CACHE_PUT_INT(i, out);
2368 	if (i > 0) {
2369 		sdlFunctionPtr tmp;
2370 		zval *function_num;
2371 		zend_string *key;
2372 
2373 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->requests, key, tmp) {
2374 			function_num = zend_hash_str_find(&tmp_functions, (char*)&tmp, sizeof(tmp));
2375 			WSDL_CACHE_PUT_INT(Z_LVAL_P(function_num), out);
2376 			sdl_serialize_key(key, out);
2377 		} ZEND_HASH_FOREACH_END();
2378 	}
2379 
2380 	bool valid_file = write(f, ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)) == ZSTR_LEN(buf.s);
2381 	close(f);
2382 
2383 	/* Make sure that incomplete files (e.g. due to disk space issues, see bug #66150) are not utilised. */
2384 	if (valid_file) {
2385 		/* This is allowed to fail, this means that another process was raced to create the file. */
2386 		if (VCWD_RENAME(ZSTR_VAL(temp_file_path), fn) < 0) {
2387 			VCWD_UNLINK(ZSTR_VAL(temp_file_path));
2388 		}
2389 	}
2390 
2391 	smart_str_free(&buf);
2392 	zend_hash_destroy(&tmp_functions);
2393 	zend_hash_destroy(&tmp_bindings);
2394 	zend_hash_destroy(&tmp_encoders);
2395 	zend_hash_destroy(&tmp_types);
2396 	zend_string_release_ex(temp_file_path, false);
2397 }
2398 
2399 
make_persistent_restriction_int(void * data)2400 static void make_persistent_restriction_int(void *data)
2401 {
2402 	sdlRestrictionIntPtr *rest = (sdlRestrictionIntPtr *)data;
2403 	sdlRestrictionIntPtr prest = NULL;
2404 
2405 	prest = malloc(sizeof(sdlRestrictionInt));
2406 	*prest = **rest;
2407 	*rest = prest;
2408 }
2409 
2410 
make_persistent_restriction_char_int(sdlRestrictionCharPtr * rest)2411 static void make_persistent_restriction_char_int(sdlRestrictionCharPtr *rest)
2412 {
2413 	sdlRestrictionCharPtr prest = NULL;
2414 
2415 	prest = malloc(sizeof(sdlRestrictionChar));
2416 	memset(prest, 0, sizeof(sdlRestrictionChar));
2417 	prest->value = strdup((*rest)->value);
2418 	prest->fixed = (*rest)->fixed;
2419 	*rest = prest;
2420 }
2421 
2422 
make_persistent_sdl_type_ref(sdlTypePtr * type,HashTable * ptr_map,HashTable * bp_types)2423 static void make_persistent_sdl_type_ref(sdlTypePtr *type, HashTable *ptr_map, HashTable *bp_types)
2424 {
2425 	sdlTypePtr tmp;
2426 
2427 	if ((tmp = zend_hash_str_find_ptr(ptr_map, (char *)type, sizeof(sdlTypePtr))) != NULL) {
2428 		*type = tmp;
2429 	} else {
2430 		zend_hash_next_index_insert_ptr(bp_types, *type);
2431 	}
2432 }
2433 
2434 
make_persistent_sdl_encoder_ref(encodePtr * enc,HashTable * ptr_map,HashTable * bp_encoders)2435 static void make_persistent_sdl_encoder_ref(encodePtr *enc, HashTable *ptr_map, HashTable *bp_encoders)
2436 {
2437 	encodePtr tmp;
2438 
2439 	/* do not process defaultEncoding's here */
2440 	if ((*enc) >= defaultEncoding && (*enc) < defaultEncoding + numDefaultEncodings) {
2441 		return;
2442 	}
2443 
2444 	if ((tmp = zend_hash_str_find_ptr(ptr_map, (char *)enc, sizeof(encodePtr))) != NULL) {
2445 		*enc = tmp;
2446 	} else {
2447 		zend_hash_next_index_insert_ptr(bp_encoders, enc);
2448 	}
2449 }
2450 
2451 
make_persistent_sdl_function_headers(HashTable * headers,HashTable * ptr_map)2452 static HashTable* make_persistent_sdl_function_headers(HashTable *headers, HashTable *ptr_map)
2453 {
2454 	HashTable *pheaders;
2455 	sdlSoapBindingFunctionHeaderPtr tmp, pheader;
2456 	encodePtr penc;
2457 	sdlTypePtr ptype;
2458 	zend_string *key;
2459 
2460 	pheaders = malloc(sizeof(HashTable));
2461 	zend_hash_init(pheaders, zend_hash_num_elements(headers), NULL, delete_header_persistent, 1);
2462 
2463 	ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(headers, key, tmp) {
2464 		pheader = malloc(sizeof(sdlSoapBindingFunctionHeader));
2465 		memset(pheader, 0, sizeof(sdlSoapBindingFunctionHeader));
2466 		*pheader = *tmp;
2467 
2468 		if (pheader->name) {
2469 			pheader->name = strdup(pheader->name);
2470 		}
2471 		if (pheader->ns) {
2472 			pheader->ns = strdup(pheader->ns);
2473 		}
2474 
2475 		if (pheader->encode && pheader->encode->details.sdl_type) {
2476 			if ((penc = zend_hash_str_find_ptr(ptr_map, (char*)&pheader->encode, sizeof(encodePtr))) == NULL) {
2477 				assert(0);
2478 			}
2479 			pheader->encode = penc;
2480 		}
2481 		if (pheader->element) {
2482 			if ((ptype = zend_hash_str_find_ptr(ptr_map, (char*)&pheader->element, sizeof(sdlTypePtr))) == NULL) {
2483 				assert(0);
2484 			}
2485 			pheader->element = ptype;
2486 		}
2487 
2488 		if (pheader->headerfaults) {
2489 			pheader->headerfaults = make_persistent_sdl_function_headers(pheader->headerfaults, ptr_map);
2490 		}
2491 
2492 		if (key) {
2493 			/* We have to duplicate key emalloc->malloc */
2494 			zend_hash_str_add_ptr(pheaders, ZSTR_VAL(key), ZSTR_LEN(key), pheader);
2495 		} else {
2496 			zend_hash_next_index_insert_ptr(pheaders, pheader);
2497 		}
2498 	} ZEND_HASH_FOREACH_END();
2499 
2500 	return pheaders;
2501 }
2502 
2503 
make_persistent_sdl_soap_body(sdlSoapBindingFunctionBodyPtr body,HashTable * ptr_map)2504 static void make_persistent_sdl_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *ptr_map)
2505 {
2506 	if (body->ns) {
2507 		body->ns = strdup(body->ns);
2508 	}
2509 
2510 	if (body->headers) {
2511 		body->headers = make_persistent_sdl_function_headers(body->headers, ptr_map);
2512 	}
2513 }
2514 
2515 
make_persistent_sdl_parameters(HashTable * params,HashTable * ptr_map)2516 static HashTable* make_persistent_sdl_parameters(HashTable *params, HashTable *ptr_map)
2517 {
2518 	HashTable *pparams;
2519 	sdlParamPtr tmp, pparam;
2520 	sdlTypePtr ptype;
2521 	encodePtr penc;
2522 	zend_string *key;
2523 
2524 	pparams = malloc(sizeof(HashTable));
2525 	zend_hash_init(pparams, zend_hash_num_elements(params), NULL, delete_parameter_persistent, 1);
2526 
2527 	ZEND_HASH_FOREACH_STR_KEY_PTR(params, key, tmp) {
2528 		pparam = malloc(sizeof(sdlParam));
2529 		memset(pparam, 0, sizeof(sdlParam));
2530 		*pparam = *tmp;
2531 
2532 		if (pparam->paramName) {
2533 			pparam->paramName = strdup(pparam->paramName);
2534 		}
2535 
2536 		if (pparam->encode && pparam->encode->details.sdl_type) {
2537 			if ((penc = zend_hash_str_find_ptr(ptr_map, (char*)&pparam->encode, sizeof(encodePtr))) == NULL) {
2538 				assert(0);
2539 			}
2540 			pparam->encode = penc;
2541 		}
2542 		if (pparam->element) {
2543 			if ((ptype = zend_hash_str_find_ptr(ptr_map, (char*)&pparam->element, sizeof(sdlTypePtr))) == NULL) {
2544 				assert(0);
2545 			}
2546 			pparam->element = ptype;
2547 		}
2548 
2549 		if (key) {
2550 			/* We have to duplicate key emalloc->malloc */
2551 			zend_hash_str_add_ptr(pparams, ZSTR_VAL(key), ZSTR_LEN(key), pparam);
2552 		} else {
2553 			zend_hash_next_index_insert_ptr(pparams, pparam);
2554 		}
2555 	} ZEND_HASH_FOREACH_END();
2556 
2557 	return pparams;
2558 }
2559 
make_persistent_sdl_function_faults(sdlFunctionPtr func,HashTable * faults,HashTable * ptr_map)2560 static HashTable* make_persistent_sdl_function_faults(sdlFunctionPtr func, HashTable *faults, HashTable *ptr_map)
2561 {
2562 	HashTable *pfaults;
2563 	sdlFaultPtr tmp, pfault;
2564 	zend_string *key;
2565 
2566 	pfaults = malloc(sizeof(HashTable));
2567 	zend_hash_init(pfaults, zend_hash_num_elements(faults), NULL, delete_fault_persistent, 1);
2568 
2569 	ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(faults, key, tmp) {
2570 		pfault = malloc(sizeof(sdlFault));
2571 		memset(pfault, 0, sizeof(sdlFault));
2572 		*pfault = *tmp;
2573 
2574 		if (pfault->name) {
2575 			pfault->name = strdup(pfault->name);
2576 		}
2577 		if (pfault->details) {
2578 			pfault->details = make_persistent_sdl_parameters(pfault->details, ptr_map);
2579 		}
2580 
2581 		if (func->binding->bindingType == BINDING_SOAP && pfault->bindingAttributes) {
2582 			sdlSoapBindingFunctionFaultPtr soap_binding;
2583 
2584 		   	soap_binding = malloc(sizeof(sdlSoapBindingFunctionFault));
2585 			memset(soap_binding, 0, sizeof(sdlSoapBindingFunctionFault));
2586 			*soap_binding = *(sdlSoapBindingFunctionFaultPtr)pfault->bindingAttributes;
2587 			if (soap_binding->ns) {
2588 				soap_binding->ns = strdup(soap_binding->ns);
2589 			}
2590 			pfault->bindingAttributes = soap_binding;
2591 		}
2592 
2593 		if (key) {
2594 			/* We have to duplicate key emalloc->malloc */
2595 			zend_hash_str_add_ptr(pfaults, ZSTR_VAL(key), ZSTR_LEN(key), pfault);
2596 		} else {
2597 			zend_hash_next_index_insert_ptr(pfaults, pfault);
2598 		}
2599 
2600 	} ZEND_HASH_FOREACH_END();
2601 
2602 	return pfaults;
2603 }
2604 
2605 
make_persistent_sdl_attribute(sdlAttributePtr attr,HashTable * ptr_map,HashTable * bp_types,HashTable * bp_encoders)2606 static sdlAttributePtr make_persistent_sdl_attribute(sdlAttributePtr attr, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
2607 {
2608 	sdlAttributePtr pattr;
2609 	zend_string *key;
2610 
2611 	pattr = malloc(sizeof(sdlAttribute));
2612 	memset(pattr, 0, sizeof(sdlAttribute));
2613 
2614 	*pattr = *attr;
2615 
2616 	if (pattr->name) {
2617 		pattr->name = strdup(pattr->name);
2618 	}
2619 	if (pattr->namens) {
2620 		pattr->namens = strdup(pattr->namens);
2621 	}
2622 	if (pattr->ref) {
2623 		pattr->ref = strdup(pattr->ref);
2624 	}
2625 	if (pattr->def) {
2626 		pattr->def = strdup(pattr->def);
2627 	}
2628 	if (pattr->fixed) {
2629 		pattr->fixed = strdup(pattr->fixed);
2630 	}
2631 
2632 	/* we do not want to process defaultEncoding's here */
2633 	if (pattr->encode) {
2634 		make_persistent_sdl_encoder_ref(&pattr->encode, ptr_map, bp_encoders);
2635 	}
2636 
2637 	if (pattr->extraAttributes) {
2638 		sdlExtraAttributePtr tmp, pextra;
2639 
2640 		pattr->extraAttributes = malloc(sizeof(HashTable));
2641 		zend_hash_init(pattr->extraAttributes, zend_hash_num_elements(attr->extraAttributes), NULL, delete_extra_attribute_persistent, 1);
2642 
2643 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(attr->extraAttributes, key, tmp) {
2644 			if (key) {
2645 				pextra = malloc(sizeof(sdlExtraAttribute));
2646 				memset(pextra, 0, sizeof(sdlExtraAttribute));
2647 
2648 				if (tmp->ns) {
2649 					pextra->ns = strdup(tmp->ns);
2650 				}
2651 				if (tmp->val) {
2652 					pextra->val = strdup(tmp->val);
2653 				}
2654 
2655 				/* We have to duplicate key emalloc->malloc */
2656 				zend_hash_str_add_ptr(pattr->extraAttributes, ZSTR_VAL(key), ZSTR_LEN(key), pextra);
2657 			}
2658 		} ZEND_HASH_FOREACH_END();
2659 	}
2660 
2661 	return pattr;
2662 }
2663 
2664 
make_persistent_sdl_model(sdlContentModelPtr model,HashTable * ptr_map,HashTable * bp_types,HashTable * bp_encoders)2665 static sdlContentModelPtr make_persistent_sdl_model(sdlContentModelPtr model, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
2666 {
2667 	sdlContentModelPtr pmodel;
2668 	sdlContentModelPtr tmp, pcontent;
2669 
2670 	pmodel = malloc(sizeof(sdlContentModel));
2671 	memset(pmodel, 0, sizeof(sdlContentModel));
2672 	*pmodel = *model;
2673 
2674 	switch (pmodel->kind) {
2675 		case XSD_CONTENT_ELEMENT:
2676 			if (pmodel->u.element) {
2677 				make_persistent_sdl_type_ref(&pmodel->u.element, ptr_map, bp_types);
2678 			}
2679 			break;
2680 
2681 		case XSD_CONTENT_SEQUENCE:
2682 		case XSD_CONTENT_ALL:
2683 		case XSD_CONTENT_CHOICE:
2684 			pmodel->u.content = malloc(sizeof(HashTable));
2685 			zend_hash_init(pmodel->u.content, zend_hash_num_elements(model->u.content), NULL, delete_model_persistent, 1);
2686 
2687 			ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
2688 				pcontent = make_persistent_sdl_model(tmp, ptr_map, bp_types, bp_encoders);
2689 				zend_hash_next_index_insert_ptr(pmodel->u.content, pcontent);
2690 			} ZEND_HASH_FOREACH_END();
2691 			break;
2692 
2693 		case XSD_CONTENT_GROUP_REF:
2694 			if (pmodel->u.group_ref) {
2695 				pmodel->u.group_ref = strdup(pmodel->u.group_ref);
2696 			}
2697 			break;
2698 
2699 		case XSD_CONTENT_GROUP:
2700 			if (pmodel->u.group) {
2701 				make_persistent_sdl_type_ref(&pmodel->u.group, ptr_map, bp_types);
2702 			}
2703 			break;
2704 
2705 		default:
2706 			break;
2707 	}
2708 
2709 	return pmodel;
2710 }
2711 
2712 
make_persistent_sdl_type(sdlTypePtr type,HashTable * ptr_map,HashTable * bp_types,HashTable * bp_encoders)2713 static sdlTypePtr make_persistent_sdl_type(sdlTypePtr type, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
2714 {
2715 	zend_string *key;
2716 	sdlTypePtr ptype = NULL;
2717 
2718 	ptype = malloc(sizeof(sdlType));
2719 	memset(ptype, 0, sizeof(sdlType));
2720 
2721 	*ptype = *type;
2722 
2723 	if (ptype->name) {
2724 		ptype->name = strdup(ptype->name);
2725 	}
2726 	if (ptype->namens) {
2727 		ptype->namens = strdup(ptype->namens);
2728 	}
2729 	if (ptype->def) {
2730 		ptype->def = strdup(ptype->def);
2731 	}
2732 	if (ptype->fixed) {
2733 		ptype->fixed = strdup(ptype->fixed);
2734 	}
2735 	if (ptype->ref) {
2736 		ptype->ref = strdup(ptype->ref);
2737 	}
2738 
2739 	/* we do not want to process defaultEncoding's here */
2740 	if (ptype->encode) {
2741 		make_persistent_sdl_encoder_ref(&ptype->encode, ptr_map, bp_encoders);
2742 	}
2743 
2744 	if (ptype->restrictions) {
2745 		ptype->restrictions = malloc(sizeof(sdlRestrictions));
2746 		memset(ptype->restrictions, 0, sizeof(sdlRestrictions));
2747 		*ptype->restrictions = *type->restrictions;
2748 
2749 		if (ptype->restrictions->minExclusive) {
2750 			make_persistent_restriction_int(&ptype->restrictions->minExclusive);
2751 		}
2752 		if (ptype->restrictions->maxExclusive) {
2753 			make_persistent_restriction_int(&ptype->restrictions->maxExclusive);
2754 		}
2755 		if (ptype->restrictions->minInclusive) {
2756 			make_persistent_restriction_int(&ptype->restrictions->minInclusive);
2757 		}
2758 		if (ptype->restrictions->maxInclusive) {
2759 			make_persistent_restriction_int(&ptype->restrictions->maxInclusive);
2760 		}
2761 		if (ptype->restrictions->totalDigits) {
2762 			make_persistent_restriction_int(&ptype->restrictions->totalDigits);
2763 		}
2764 		if (ptype->restrictions->fractionDigits) {
2765 			make_persistent_restriction_int(&ptype->restrictions->fractionDigits);
2766 		}
2767 		if (ptype->restrictions->length) {
2768 			make_persistent_restriction_int(&ptype->restrictions->length);
2769 		}
2770 		if (ptype->restrictions->minLength) {
2771 			make_persistent_restriction_int(&ptype->restrictions->minLength);
2772 		}
2773 		if (ptype->restrictions->maxLength) {
2774 			make_persistent_restriction_int(&ptype->restrictions->maxLength);
2775 		}
2776 		if (ptype->restrictions->whiteSpace) {
2777 			make_persistent_restriction_char_int(&ptype->restrictions->whiteSpace);
2778 		}
2779 		if (ptype->restrictions->pattern) {
2780 			make_persistent_restriction_char_int(&ptype->restrictions->pattern);
2781 		}
2782 
2783 		if (type->restrictions->enumeration) {
2784 			sdlRestrictionCharPtr tmp, penum;
2785 			ptype->restrictions->enumeration = malloc(sizeof(HashTable));
2786 			zend_hash_init(ptype->restrictions->enumeration, zend_hash_num_elements(type->restrictions->enumeration), NULL, delete_restriction_var_char_persistent, 1);
2787 			ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(type->restrictions->enumeration, key, tmp) {
2788 				penum = tmp;
2789 				make_persistent_restriction_char_int(&penum);
2790 				/* We have to duplicate key emalloc->malloc */
2791 				zend_hash_str_add_ptr(ptype->restrictions->enumeration, ZSTR_VAL(key), ZSTR_LEN(key), penum);
2792 			} ZEND_HASH_FOREACH_END();
2793 		}
2794 	}
2795 
2796 	if (ptype->elements) {
2797 		sdlTypePtr tmp, pelem;
2798 
2799 		ptype->elements = malloc(sizeof(HashTable));
2800 		zend_hash_init(ptype->elements, zend_hash_num_elements(type->elements), NULL, delete_type_persistent, 1);
2801 
2802 		ZEND_HASH_FOREACH_STR_KEY_PTR(type->elements, key, tmp) {
2803 			pelem = make_persistent_sdl_type(tmp, ptr_map, bp_types, bp_encoders);
2804 			if (key) {
2805 				/* We have to duplicate key emalloc->malloc */
2806 				zend_hash_str_add_ptr(ptype->elements, ZSTR_VAL(key), ZSTR_LEN(key), pelem);
2807 			} else {
2808 				zend_hash_next_index_insert_ptr(ptype->elements, pelem);
2809 			}
2810 			zend_hash_str_add_ptr(ptr_map, (char*)&tmp, sizeof(tmp), pelem);
2811 		} ZEND_HASH_FOREACH_END();
2812 	}
2813 
2814 	if (ptype->attributes) {
2815 		sdlAttributePtr tmp, pattr;
2816 
2817 		ptype->attributes = malloc(sizeof(HashTable));
2818 		zend_hash_init(ptype->attributes, zend_hash_num_elements(type->attributes), NULL, delete_attribute_persistent, 1);
2819 
2820 		ZEND_HASH_FOREACH_STR_KEY_PTR(type->attributes, key, tmp) {
2821 			pattr = make_persistent_sdl_attribute(tmp, ptr_map, bp_types, bp_encoders);
2822 			if (key) {
2823 				/* We have to duplicate key emalloc->malloc */
2824 				zend_hash_str_add_ptr(ptype->attributes, ZSTR_VAL(key), ZSTR_LEN(key), pattr);
2825 			} else {
2826 				zend_hash_next_index_insert_ptr(ptype->attributes, pattr);
2827 			}
2828 		} ZEND_HASH_FOREACH_END();
2829 	}
2830 
2831 	if (type->model) {
2832 		ptype->model = make_persistent_sdl_model(ptype->model, ptr_map, bp_types, bp_encoders);
2833 	}
2834 
2835 	return ptype;
2836 }
2837 
make_persistent_sdl_encoder(encodePtr enc,HashTable * ptr_map,HashTable * bp_types,HashTable * bp_encoders)2838 static encodePtr make_persistent_sdl_encoder(encodePtr enc, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
2839 {
2840 	encodePtr penc = NULL;
2841 
2842 	penc = malloc(sizeof(encode));
2843 	memset(penc, 0, sizeof(encode));
2844 
2845 	*penc = *enc;
2846 
2847 	if (penc->details.type_str) {
2848 		penc->details.type_str = strdup(penc->details.type_str);
2849 	}
2850 	if (penc->details.ns) {
2851 		penc->details.ns = strdup(penc->details.ns);
2852 	}
2853 
2854 	if (penc->details.sdl_type) {
2855 		make_persistent_sdl_type_ref(&penc->details.sdl_type, ptr_map, bp_types);
2856 	}
2857 
2858 	return penc;
2859 }
2860 
make_persistent_sdl_binding(sdlBindingPtr bind,HashTable * ptr_map)2861 static sdlBindingPtr make_persistent_sdl_binding(sdlBindingPtr bind, HashTable *ptr_map)
2862 {
2863 	sdlBindingPtr pbind = NULL;
2864 
2865 	pbind = malloc(sizeof(sdlBinding));
2866 	memset(pbind, 0, sizeof(sdlBinding));
2867 
2868 	*pbind = *bind;
2869 
2870 	if (pbind->name) {
2871 		pbind->name = strdup(pbind->name);
2872 	}
2873 	if (pbind->location) {
2874 		pbind->location = strdup(pbind->location);
2875 	}
2876 
2877 	if (pbind->bindingType == BINDING_SOAP && pbind->bindingAttributes) {
2878 		sdlSoapBindingPtr soap_binding;
2879 
2880 		soap_binding = malloc(sizeof(sdlSoapBinding));
2881 		memset(soap_binding, 0, sizeof(sdlSoapBinding));
2882 		*soap_binding = *(sdlSoapBindingPtr)pbind->bindingAttributes;
2883 		pbind->bindingAttributes = soap_binding;
2884 	}
2885 
2886 	return pbind;
2887 }
2888 
make_persistent_sdl_function(sdlFunctionPtr func,HashTable * ptr_map)2889 static sdlFunctionPtr make_persistent_sdl_function(sdlFunctionPtr func, HashTable *ptr_map)
2890 {
2891 	sdlFunctionPtr pfunc = NULL;
2892 
2893 	pfunc = malloc(sizeof(sdlFunction));
2894 	memset(pfunc, 0, sizeof(sdlFunction));
2895 
2896 	*pfunc = *func;
2897 
2898 	if (pfunc->functionName) {
2899 		pfunc->functionName = strdup(pfunc->functionName);
2900 	}
2901 	if (pfunc->requestName) {
2902 		pfunc->requestName = strdup(pfunc->requestName);
2903 	}
2904 	if (pfunc->responseName) {
2905 		pfunc->responseName = strdup(pfunc->responseName);
2906 	}
2907 
2908 	if (pfunc->binding) {
2909 		sdlBindingPtr tmp;
2910 
2911 		if ((tmp = zend_hash_str_find_ptr(ptr_map, (char*)&pfunc->binding, sizeof(pfunc->binding))) == NULL) {
2912 			assert(0);
2913 		}
2914 		pfunc->binding = tmp;
2915 
2916 		if (pfunc->binding->bindingType == BINDING_SOAP && pfunc->bindingAttributes) {
2917 			sdlSoapBindingFunctionPtr soap_binding;
2918 
2919 		   	soap_binding = malloc(sizeof(sdlSoapBindingFunction));
2920 			memset(soap_binding, 0, sizeof(sdlSoapBindingFunction));
2921 			*soap_binding = *(sdlSoapBindingFunctionPtr)pfunc->bindingAttributes;
2922 			if (soap_binding->soapAction) {
2923 				soap_binding->soapAction = strdup(soap_binding->soapAction);
2924 			}
2925 			make_persistent_sdl_soap_body(&soap_binding->input, ptr_map);
2926 			make_persistent_sdl_soap_body(&soap_binding->output, ptr_map);
2927 			pfunc->bindingAttributes = soap_binding;
2928 		}
2929 
2930 		if (pfunc->requestParameters) {
2931 			pfunc->requestParameters = make_persistent_sdl_parameters(pfunc->requestParameters, ptr_map);
2932 		}
2933 		if (pfunc->responseParameters) {
2934 			pfunc->responseParameters = make_persistent_sdl_parameters(pfunc->responseParameters, ptr_map);
2935 		}
2936 		if (pfunc->faults) {
2937 			pfunc->faults = make_persistent_sdl_function_faults(pfunc, pfunc->faults, ptr_map);
2938 		}
2939 	}
2940 
2941 	return pfunc;
2942 }
2943 
make_persistent_sdl(sdlPtr sdl)2944 static sdlPtr make_persistent_sdl(sdlPtr sdl)
2945 {
2946 	sdlPtr psdl = NULL;
2947 	HashTable ptr_map;
2948 	HashTable bp_types, bp_encoders;
2949 	zend_string *key;
2950 
2951 	zend_hash_init(&bp_types, 0, NULL, NULL, 0);
2952 	zend_hash_init(&bp_encoders, 0, NULL, NULL, 0);
2953 	zend_hash_init(&ptr_map, 0, NULL, NULL, 0);
2954 
2955 	psdl = malloc(sizeof(*sdl));
2956 	memset(psdl, 0, sizeof(*sdl));
2957 
2958 	if (sdl->source) {
2959 		psdl->source = strdup(sdl->source);
2960 	}
2961 	if (sdl->target_ns) {
2962 		psdl->target_ns = strdup(sdl->target_ns);
2963 	}
2964 
2965 	if (sdl->groups) {
2966 		sdlTypePtr tmp;
2967 		sdlTypePtr ptype;
2968 
2969 		psdl->groups = malloc(sizeof(HashTable));
2970 		zend_hash_init(psdl->groups, zend_hash_num_elements(sdl->groups), NULL, delete_type_persistent, 1);
2971 
2972 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->groups, key, tmp) {
2973 			ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
2974 			if (key) {
2975 				/* We have to duplicate key emalloc->malloc */
2976 				zend_hash_str_add_ptr(psdl->groups, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
2977 			} else {
2978 				zend_hash_next_index_insert_ptr(psdl->groups, ptype);
2979 			}
2980 			zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
2981 		} ZEND_HASH_FOREACH_END();
2982 	}
2983 
2984 	if (sdl->types) {
2985 		sdlTypePtr tmp;
2986 		sdlTypePtr ptype;
2987 
2988 		psdl->types = malloc(sizeof(HashTable));
2989 		zend_hash_init(psdl->types, zend_hash_num_elements(sdl->types), NULL, delete_type_persistent, 1);
2990 
2991 		ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->types, key, tmp) {
2992 			ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
2993 			if (key) {
2994 				/* We have to duplicate key emalloc->malloc */
2995 				zend_hash_str_add_ptr(psdl->types, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
2996 			} else {
2997 				zend_hash_next_index_insert_ptr(psdl->types, ptype);
2998 			}
2999 			zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
3000 		} ZEND_HASH_FOREACH_END();
3001 	}
3002 
3003 	if (sdl->elements) {
3004 		sdlTypePtr tmp;
3005 		sdlTypePtr ptype;
3006 
3007 		psdl->elements = malloc(sizeof(HashTable));
3008 		zend_hash_init(psdl->elements, zend_hash_num_elements(sdl->elements), NULL, delete_type_persistent, 1);
3009 
3010 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->elements, key, tmp) {
3011 			ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
3012 			if (key) {
3013 				/* We have to duplicate key emalloc->malloc */
3014 				zend_hash_str_add_ptr(psdl->elements, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
3015 			} else {
3016 				zend_hash_next_index_insert_ptr(psdl->elements, ptype);
3017 			}
3018 			zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
3019 		} ZEND_HASH_FOREACH_END();
3020 	}
3021 
3022 	if (sdl->encoders) {
3023 		encodePtr tmp;
3024 		encodePtr penc;
3025 
3026 		psdl->encoders = malloc(sizeof(HashTable));
3027 		zend_hash_init(psdl->encoders, zend_hash_num_elements(sdl->encoders), NULL, delete_encoder_persistent, 1);
3028 
3029 		ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->encoders, key, tmp) {
3030 			penc = make_persistent_sdl_encoder(tmp, &ptr_map, &bp_types, &bp_encoders);
3031 			if (key) {
3032 				/* We have to duplicate key emalloc->malloc */
3033 				zend_hash_str_add_ptr(psdl->encoders, ZSTR_VAL(key), ZSTR_LEN(key), penc);
3034 			} else {
3035 				zend_hash_next_index_insert_ptr(psdl->encoders, penc);
3036 			}
3037 			zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), penc);
3038 		} ZEND_HASH_FOREACH_END();
3039 	}
3040 
3041 	/* do backpatching here */
3042 	if (zend_hash_num_elements(&bp_types)) {
3043 		sdlTypePtr *tmp, ptype = NULL;
3044 
3045 		ZEND_HASH_FOREACH_PTR(&bp_types, tmp) {
3046 			if ((ptype = zend_hash_str_find_ptr(&ptr_map, (char*)tmp, sizeof(*tmp))) == NULL) {
3047 				assert(0);
3048 			}
3049 			*tmp = ptype;
3050 		} ZEND_HASH_FOREACH_END();
3051 	}
3052 	if (zend_hash_num_elements(&bp_encoders)) {
3053 		encodePtr *tmp, penc = NULL;
3054 
3055 		ZEND_HASH_FOREACH_PTR(&bp_encoders, tmp) {
3056 			if ((penc = zend_hash_str_find_ptr(&ptr_map, (char*)tmp, sizeof(*tmp))) == NULL) {
3057 				assert(0);
3058 			}
3059 			*tmp = penc;
3060 		} ZEND_HASH_FOREACH_END();
3061 	}
3062 
3063 
3064 	if (sdl->bindings) {
3065 		sdlBindingPtr tmp;
3066 		sdlBindingPtr pbind;
3067 
3068 		psdl->bindings = malloc(sizeof(HashTable));
3069 		zend_hash_init(psdl->bindings, zend_hash_num_elements(sdl->bindings), NULL, delete_binding_persistent, 1);
3070 
3071 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->bindings, key, tmp) {
3072 			pbind = make_persistent_sdl_binding(tmp, &ptr_map);
3073 			if (key) {
3074 				/* We have to duplicate key emalloc->malloc */
3075 				zend_hash_str_add_ptr(psdl->bindings, ZSTR_VAL(key), ZSTR_LEN(key), pbind);
3076 			} else {
3077 				zend_hash_next_index_insert_ptr(psdl->bindings, pbind);
3078 			}
3079 			zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), pbind);
3080 		} ZEND_HASH_FOREACH_END();
3081 	}
3082 
3083 	zend_hash_init(&psdl->functions, zend_hash_num_elements(&sdl->functions), NULL, delete_function_persistent, 1);
3084 	if (zend_hash_num_elements(&sdl->functions)) {
3085 		sdlFunctionPtr tmp;
3086 		sdlFunctionPtr pfunc;
3087 
3088 		ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&sdl->functions, key, tmp) {
3089 			pfunc = make_persistent_sdl_function(tmp, &ptr_map);
3090 			if (key) {
3091 				/* We have to duplicate key emalloc->malloc */
3092 				zend_hash_str_add_ptr(&psdl->functions, ZSTR_VAL(key), ZSTR_LEN(key), pfunc);
3093 			} else {
3094 				zend_hash_next_index_insert_ptr(&psdl->functions, pfunc);
3095 			}
3096 			zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), pfunc);
3097 		} ZEND_HASH_FOREACH_END();
3098 	}
3099 
3100 	if (sdl->requests) {
3101 		zval *zv;
3102 		sdlFunctionPtr tmp;
3103 		sdlFunctionPtr preq;
3104 
3105 		psdl->requests = malloc(sizeof(HashTable));
3106 		zend_hash_init(psdl->requests, zend_hash_num_elements(sdl->requests), NULL, NULL, 1);
3107 
3108 		ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(sdl->requests, key, zv) {
3109 			tmp = Z_PTR_P(zv);
3110 			if ((preq = zend_hash_str_find_ptr(&ptr_map, (char*)&tmp, sizeof(tmp))) == NULL) {
3111 				assert(0);
3112 			}
3113 			Z_PTR_P(zv) = preq;
3114 			if (key) {
3115 				/* We have to duplicate key emalloc->malloc */
3116 				zend_hash_str_add_ptr(psdl->requests, ZSTR_VAL(key), ZSTR_LEN(key), preq);
3117 			}
3118 		} ZEND_HASH_FOREACH_END();
3119 	}
3120 
3121 	zend_hash_destroy(&ptr_map);
3122 	zend_hash_destroy(&bp_encoders);
3123 	zend_hash_destroy(&bp_types);
3124 
3125 	return psdl;
3126 }
3127 
3128 typedef struct _sdl_cache_bucket {
3129 	sdlPtr sdl;
3130 	time_t time;
3131 } sdl_cache_bucket;
3132 
delete_psdl_int(sdl_cache_bucket * p)3133 static void delete_psdl_int(sdl_cache_bucket *p)
3134 {
3135 	sdlPtr tmp = p->sdl;
3136 
3137 	zend_hash_destroy(&tmp->functions);
3138 	if (tmp->source) {
3139 		free(tmp->source);
3140 	}
3141 	if (tmp->target_ns) {
3142 		free(tmp->target_ns);
3143 	}
3144 	if (tmp->elements) {
3145 		zend_hash_destroy(tmp->elements);
3146 		free(tmp->elements);
3147 	}
3148 	if (tmp->encoders) {
3149 		zend_hash_destroy(tmp->encoders);
3150 		free(tmp->encoders);
3151 	}
3152 	if (tmp->types) {
3153 		zend_hash_destroy(tmp->types);
3154 		free(tmp->types);
3155 	}
3156 	if (tmp->groups) {
3157 		zend_hash_destroy(tmp->groups);
3158 		free(tmp->groups);
3159 	}
3160 	if (tmp->bindings) {
3161 		zend_hash_destroy(tmp->bindings);
3162 		free(tmp->bindings);
3163 	}
3164 	if (tmp->requests) {
3165 		zend_hash_destroy(tmp->requests);
3166 		free(tmp->requests);
3167 	}
3168 	free(tmp);
3169 }
3170 
delete_psdl(zval * zv)3171 static void delete_psdl(zval *zv)
3172 {
3173 	delete_psdl_int(Z_PTR_P(zv));
3174 	free(Z_PTR_P(zv));
3175 }
3176 
get_sdl(zval * this_ptr,char * uri,zend_long cache_wsdl)3177 sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
3178 {
3179 	char  fn[MAXPATHLEN];
3180 	sdlPtr sdl = NULL;
3181 	char* old_error_code = SOAP_GLOBAL(error_code);
3182 	size_t uri_len = 0;
3183 	php_stream_context *context=NULL;
3184 	zval *tmp, orig_context, new_context;
3185 	smart_str headers = {0};
3186 	char* key = NULL;
3187 	time_t t = time(0);
3188 	bool has_proxy_authorization = 0;
3189 	bool has_authorization = 0;
3190 
3191 	ZVAL_UNDEF(&orig_context);
3192 	ZVAL_UNDEF(&new_context);
3193 	if (strchr(uri,':') != NULL || IS_ABSOLUTE_PATH(uri, uri_len)) {
3194 		uri_len = strlen(uri);
3195 	} else if (VCWD_REALPATH(uri, fn) == NULL) {
3196 		cache_wsdl = WSDL_CACHE_NONE;
3197 	} else {
3198 		uri = fn;
3199 		uri_len = strlen(uri);
3200 	}
3201 
3202 	if ((cache_wsdl & WSDL_CACHE_MEMORY) && SOAP_GLOBAL(mem_cache)) {
3203 		sdl_cache_bucket *p;
3204 
3205 		if (NULL != (p = zend_hash_str_find_ptr(SOAP_GLOBAL(mem_cache), uri, uri_len))) {
3206 			if (p->time < t - SOAP_GLOBAL(cache_ttl)) {
3207 				/* in-memory cache entry is expired */
3208 				zend_hash_str_del(&EG(persistent_list), uri, uri_len);
3209 			} else {
3210 				return p->sdl;
3211 			}
3212 		}
3213 	}
3214 
3215 	if ((cache_wsdl & WSDL_CACHE_DISK) && (uri_len < MAXPATHLEN)) {
3216 		time_t t = time(0);
3217 		char md5str[33];
3218 		PHP_MD5_CTX context;
3219 		unsigned char digest[16];
3220 		int len = strlen(SOAP_GLOBAL(cache_dir));
3221 		time_t cached;
3222 		char *user = php_get_current_user();
3223 		int user_len = user ? strlen(user) + 1 : 0;
3224 
3225 		md5str[0] = '\0';
3226 		PHP_MD5Init(&context);
3227 		PHP_MD5Update(&context, (unsigned char*)uri, uri_len);
3228 		PHP_MD5Final(digest, &context);
3229 		make_digest(md5str, digest);
3230 		key = emalloc(len+sizeof("/wsdl-")-1+user_len+2+sizeof(md5str));
3231 		memcpy(key,SOAP_GLOBAL(cache_dir),len);
3232 		memcpy(key+len,"/wsdl-",sizeof("/wsdl-")-1);
3233 		len += sizeof("/wsdl-")-1;
3234 		if (user_len) {
3235 			memcpy(key+len, user, user_len-1);
3236 			len += user_len-1;
3237 			key[len++] = '-';
3238 		}
3239 		if (WSDL_CACHE_VERSION <= 0x9f) {
3240 			key[len++] = (WSDL_CACHE_VERSION >> 8) + '0';
3241 		} else {
3242 			key[len++] = (WSDL_CACHE_VERSION >> 8) - 10 + 'a';
3243 		}
3244 		if ((WSDL_CACHE_VERSION & 0xf) <= 0x9) {
3245 			key[len++] = (WSDL_CACHE_VERSION & 0xf) + '0';
3246 		} else {
3247 			key[len++] = (WSDL_CACHE_VERSION & 0xf) - 10 + 'a';
3248 		}
3249 		memcpy(key+len,md5str,sizeof(md5str));
3250 
3251 		if ((sdl = get_sdl_from_cache(key, uri, uri_len, t-SOAP_GLOBAL(cache_ttl), &cached)) != NULL) {
3252 			t = cached;
3253 			efree(key);
3254 			goto cache_in_memory;
3255 		}
3256 	}
3257 
3258 	if (instanceof_function(Z_OBJCE_P(this_ptr), soap_class_entry)) {
3259 		tmp = Z_CLIENT_STREAM_CONTEXT_P(this_ptr);
3260 		if (Z_TYPE_P(tmp) == IS_RESOURCE) {
3261 			context = php_stream_context_from_zval(tmp, 0);
3262 			/* Share a reference with new_context down below.
3263 			 * For new contexts, the reference is only in new_context so that doesn't need extra refcounting. */
3264 			GC_ADDREF(context->res);
3265 		}
3266 
3267 		tmp = Z_CLIENT_USER_AGENT_P(this_ptr);
3268 		if (Z_TYPE_P(tmp) == IS_STRING && Z_STRLEN_P(tmp) > 0) {
3269 			smart_str_appends(&headers, "User-Agent: ");
3270 			smart_str_appends(&headers, Z_STRVAL_P(tmp));
3271 			smart_str_appends(&headers, "\r\n");
3272 		}
3273 
3274 		zval *proxy_host = Z_CLIENT_PROXY_HOST_P(this_ptr);
3275 		zval *proxy_port = Z_CLIENT_PROXY_PORT_P(this_ptr);
3276 		if (Z_TYPE_P(proxy_host) == IS_STRING && Z_TYPE_P(proxy_port) == IS_LONG) {
3277 			zval str_proxy;
3278 			smart_str proxy = {0};
3279 			smart_str_appends(&proxy,"tcp://");
3280 			smart_str_appends(&proxy,Z_STRVAL_P(proxy_host));
3281 			smart_str_appends(&proxy,":");
3282 			smart_str_append_long(&proxy,Z_LVAL_P(proxy_port));
3283 			ZVAL_STR(&str_proxy, smart_str_extract(&proxy));
3284 
3285 			if (!context) {
3286 				context = php_stream_context_alloc();
3287 			}
3288 			php_stream_context_set_option(context, "http", "proxy", &str_proxy);
3289 			zval_ptr_dtor(&str_proxy);
3290 
3291 			if (uri_len < sizeof("https://")-1 ||
3292 				strncasecmp(uri, "https://", sizeof("https://")-1) != 0) {
3293 				ZVAL_TRUE(&str_proxy);
3294 				php_stream_context_set_option(context, "http", "request_fulluri", &str_proxy);
3295 			}
3296 
3297 			has_proxy_authorization = proxy_authentication(this_ptr, &headers);
3298 		}
3299 
3300 		has_authorization = basic_authentication(this_ptr, &headers);
3301 	}
3302 
3303 	if (!context) {
3304 		context = php_stream_context_alloc();
3305 	}
3306 
3307 	/* Use HTTP/1.1 with "Connection: close" by default */
3308 	if ((tmp = php_stream_context_get_option(context, "http", "protocol_version")) == NULL) {
3309 		zval http_version;
3310 
3311 		ZVAL_DOUBLE(&http_version, 1.1);
3312 		php_stream_context_set_option(context, "http", "protocol_version", &http_version);
3313 		smart_str_appendl(&headers, "Connection: close\r\n", sizeof("Connection: close\r\n")-1);
3314 	}
3315 
3316 	if (headers.s && ZSTR_LEN(headers.s) > 0) {
3317 		zval str_headers;
3318 
3319 		if (!context) {
3320 			context = php_stream_context_alloc();
3321 		} else {
3322 			http_context_headers(context, has_authorization, has_proxy_authorization, 0, &headers);
3323 		}
3324 
3325 		ZVAL_STR(&str_headers, smart_str_extract(&headers));
3326 		php_stream_context_set_option(context, "http", "header", &str_headers);
3327 		zval_ptr_dtor(&str_headers);
3328 	}
3329 
3330 	if (context) {
3331 		ZVAL_RES(&new_context, context->res);
3332 		php_libxml_switch_context(&new_context, &orig_context);
3333 	}
3334 
3335 	SOAP_GLOBAL(error_code) = "WSDL";
3336 
3337 	sdl = load_wsdl(this_ptr, uri);
3338 	if (sdl) {
3339 		sdl->is_persistent = 0;
3340 	}
3341 
3342 	SOAP_GLOBAL(error_code) = old_error_code;
3343 
3344 	if (context) {
3345 		php_libxml_switch_context(&orig_context, NULL);
3346 		zval_ptr_dtor(&new_context);
3347 	}
3348 
3349 	if ((cache_wsdl & WSDL_CACHE_DISK) && key) {
3350 		if (sdl) {
3351 			add_sdl_to_cache(key, uri, t, sdl);
3352 		}
3353 		efree(key);
3354 	}
3355 
3356 cache_in_memory:
3357 	if (cache_wsdl & WSDL_CACHE_MEMORY) {
3358 		if (sdl) {
3359 			sdlPtr psdl;
3360 			sdl_cache_bucket p;
3361 
3362 			if (SOAP_GLOBAL(mem_cache) == NULL) {
3363 				SOAP_GLOBAL(mem_cache) = malloc(sizeof(HashTable));
3364 				zend_hash_init(SOAP_GLOBAL(mem_cache), 0, NULL, delete_psdl, 1);
3365 			} else if (SOAP_GLOBAL(cache_limit) > 0 &&
3366 			           SOAP_GLOBAL(cache_limit) <= (zend_long)zend_hash_num_elements(SOAP_GLOBAL(mem_cache))) {
3367 				/* in-memory cache overflow */
3368 				sdl_cache_bucket *q;
3369 				time_t latest = t;
3370 				zend_string *latest_key = NULL, *key;
3371 
3372 				ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(SOAP_GLOBAL(mem_cache), key, q) {
3373 					if (q->time < latest) {
3374 						latest = q->time;
3375 						latest_key = key;
3376 					}
3377 				} ZEND_HASH_FOREACH_END();
3378 				if (latest_key) {
3379 					zend_hash_del(SOAP_GLOBAL(mem_cache), latest_key);
3380 				} else {
3381 					return sdl;
3382 				}
3383 			}
3384 
3385 			psdl = make_persistent_sdl(sdl);
3386 			psdl->is_persistent = 1;
3387 			p.time = t;
3388 			p.sdl = psdl;
3389 
3390 			zend_hash_str_update_mem(SOAP_GLOBAL(mem_cache), uri,
3391 											uri_len, &p, sizeof(sdl_cache_bucket));
3392 			/* remove non-persitent sdl structure */
3393 			delete_sdl_impl(sdl);
3394 			/* and replace it with persistent one */
3395 			sdl = psdl;
3396 		}
3397 	}
3398 
3399 	return sdl;
3400 }
3401 
3402 /* Deletes */
delete_sdl_impl(void * handle)3403 void delete_sdl_impl(void *handle)
3404 {
3405 	sdlPtr tmp = (sdlPtr)handle;
3406 
3407 	zend_hash_destroy(&tmp->functions);
3408 	if (tmp->source) {
3409 		efree(tmp->source);
3410 	}
3411 	if (tmp->target_ns) {
3412 		efree(tmp->target_ns);
3413 	}
3414 	if (tmp->elements) {
3415 		zend_hash_destroy(tmp->elements);
3416 		efree(tmp->elements);
3417 	}
3418 	if (tmp->encoders) {
3419 		zend_hash_destroy(tmp->encoders);
3420 		efree(tmp->encoders);
3421 	}
3422 	if (tmp->types) {
3423 		zend_hash_destroy(tmp->types);
3424 		efree(tmp->types);
3425 	}
3426 	if (tmp->groups) {
3427 		zend_hash_destroy(tmp->groups);
3428 		efree(tmp->groups);
3429 	}
3430 	if (tmp->bindings) {
3431 		zend_hash_destroy(tmp->bindings);
3432 		efree(tmp->bindings);
3433 	}
3434 	if (tmp->requests) {
3435 		zend_hash_destroy(tmp->requests);
3436 		efree(tmp->requests);
3437 	}
3438 	efree(tmp);
3439 }
3440 
delete_sdl(void * handle)3441 void delete_sdl(void *handle)
3442 {
3443 	sdlPtr tmp = (sdlPtr)handle;
3444 
3445 	if (!tmp->is_persistent) {
3446 		delete_sdl_impl(tmp);
3447 	}
3448 }
3449 
delete_binding(zval * zv)3450 static void delete_binding(zval *zv)
3451 {
3452 	sdlBindingPtr binding = Z_PTR_P(zv);
3453 
3454 	if (binding->location) {
3455 		efree(binding->location);
3456 	}
3457 	if (binding->name) {
3458 		efree(binding->name);
3459 	}
3460 
3461 	if (binding->bindingType == BINDING_SOAP) {
3462 		sdlSoapBindingPtr soapBind = binding->bindingAttributes;
3463 		if (soapBind) {
3464 			efree(soapBind);
3465 		}
3466 	}
3467 	efree(binding);
3468 }
3469 
delete_binding_persistent(zval * zv)3470 static void delete_binding_persistent(zval *zv)
3471 {
3472 	sdlBindingPtr binding = Z_PTR_P(zv);
3473 
3474 	if (binding->location) {
3475 		free(binding->location);
3476 	}
3477 	if (binding->name) {
3478 		free(binding->name);
3479 	}
3480 
3481 	if (binding->bindingType == BINDING_SOAP) {
3482 		sdlSoapBindingPtr soapBind = binding->bindingAttributes;
3483 		if (soapBind) {
3484 			free(soapBind);
3485 		}
3486 	}
3487 	free(binding);
3488 }
3489 
delete_sdl_soap_binding_function_body(sdlSoapBindingFunctionBody body)3490 static void delete_sdl_soap_binding_function_body(sdlSoapBindingFunctionBody body)
3491 {
3492 	if (body.ns) {
3493 		efree(body.ns);
3494 	}
3495 	if (body.headers) {
3496 		zend_hash_destroy(body.headers);
3497 		efree(body.headers);
3498 	}
3499 }
3500 
delete_sdl_soap_binding_function_body_persistent(sdlSoapBindingFunctionBody body)3501 static void delete_sdl_soap_binding_function_body_persistent(sdlSoapBindingFunctionBody body)
3502 {
3503 	if (body.ns) {
3504 		free(body.ns);
3505 	}
3506 	if (body.headers) {
3507 		zend_hash_destroy(body.headers);
3508 		free(body.headers);
3509 	}
3510 }
3511 
delete_function(zval * zv)3512 static void delete_function(zval *zv)
3513 {
3514 	sdlFunctionPtr function = Z_PTR_P(zv);
3515 
3516 	if (function->functionName) {
3517 		efree(function->functionName);
3518 	}
3519 	if (function->requestName) {
3520 		efree(function->requestName);
3521 	}
3522 	if (function->responseName) {
3523 		efree(function->responseName);
3524 	}
3525 	if (function->requestParameters) {
3526 		zend_hash_destroy(function->requestParameters);
3527 		efree(function->requestParameters);
3528 	}
3529 	if (function->responseParameters) {
3530 		zend_hash_destroy(function->responseParameters);
3531 		efree(function->responseParameters);
3532 	}
3533 	if (function->faults) {
3534 		zend_hash_destroy(function->faults);
3535 		efree(function->faults);
3536 	}
3537 
3538 	if (function->bindingAttributes &&
3539 	    function->binding && function->binding->bindingType == BINDING_SOAP) {
3540 		sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
3541 		if (soapFunction->soapAction) {
3542 			efree(soapFunction->soapAction);
3543 		}
3544 		delete_sdl_soap_binding_function_body(soapFunction->input);
3545 		delete_sdl_soap_binding_function_body(soapFunction->output);
3546 		efree(soapFunction);
3547 	}
3548 	efree(function);
3549 }
3550 
delete_function_persistent(zval * zv)3551 static void delete_function_persistent(zval *zv)
3552 {
3553 	sdlFunctionPtr function = Z_PTR_P(zv);
3554 
3555 	if (function->functionName) {
3556 		free(function->functionName);
3557 	}
3558 	if (function->requestName) {
3559 		free(function->requestName);
3560 	}
3561 	if (function->responseName) {
3562 		free(function->responseName);
3563 	}
3564 	if (function->requestParameters) {
3565 		zend_hash_destroy(function->requestParameters);
3566 		free(function->requestParameters);
3567 	}
3568 	if (function->responseParameters) {
3569 		zend_hash_destroy(function->responseParameters);
3570 		free(function->responseParameters);
3571 	}
3572 	if (function->faults) {
3573 		zend_hash_destroy(function->faults);
3574 		free(function->faults);
3575 	}
3576 
3577 	if (function->bindingAttributes &&
3578 	    function->binding && function->binding->bindingType == BINDING_SOAP) {
3579 		sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
3580 		if (soapFunction->soapAction) {
3581 			free(soapFunction->soapAction);
3582 		}
3583 		delete_sdl_soap_binding_function_body_persistent(soapFunction->input);
3584 		delete_sdl_soap_binding_function_body_persistent(soapFunction->output);
3585 		free(soapFunction);
3586 	}
3587 	free(function);
3588 }
3589 
delete_parameter(zval * zv)3590 static void delete_parameter(zval *zv)
3591 {
3592 	sdlParamPtr param = Z_PTR_P(zv);
3593 	if (param->paramName) {
3594 		efree(param->paramName);
3595 	}
3596 	efree(param);
3597 }
3598 
delete_parameter_persistent(zval * zv)3599 static void delete_parameter_persistent(zval *zv)
3600 {
3601 	sdlParamPtr param = Z_PTR_P(zv);
3602 	if (param->paramName) {
3603 		free(param->paramName);
3604 	}
3605 	free(param);
3606 }
3607 
delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr)3608 static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr)
3609 {
3610 	if (hdr->name) {
3611 		efree(hdr->name);
3612 	}
3613 	if (hdr->ns) {
3614 		efree(hdr->ns);
3615 	}
3616 	if (hdr->headerfaults) {
3617 		zend_hash_destroy(hdr->headerfaults);
3618 		efree(hdr->headerfaults);
3619 	}
3620 	efree(hdr);
3621 }
3622 
delete_header(zval * zv)3623 static void delete_header(zval *zv)
3624 {
3625 	delete_header_int(Z_PTR_P(zv));
3626 }
3627 
delete_header_persistent(zval * zv)3628 static void delete_header_persistent(zval *zv)
3629 {
3630 	sdlSoapBindingFunctionHeaderPtr hdr = Z_PTR_P(zv);
3631 	if (hdr->name) {
3632 		free(hdr->name);
3633 	}
3634 	if (hdr->ns) {
3635 		free(hdr->ns);
3636 	}
3637 	if (hdr->headerfaults) {
3638 		zend_hash_destroy(hdr->headerfaults);
3639 		free(hdr->headerfaults);
3640 	}
3641 	free(hdr);
3642 }
3643 
delete_fault(zval * zv)3644 static void delete_fault(zval *zv)
3645 {
3646 	sdlFaultPtr fault = Z_PTR_P(zv);
3647 	if (fault->name) {
3648 		efree(fault->name);
3649 	}
3650 	if (fault->details) {
3651 		zend_hash_destroy(fault->details);
3652 		efree(fault->details);
3653 	}
3654 	if (fault->bindingAttributes) {
3655 		sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
3656 
3657 		if (binding->ns) {
3658 			efree(binding->ns);
3659 		}
3660 		efree(fault->bindingAttributes);
3661 	}
3662 	efree(fault);
3663 }
3664 
delete_fault_persistent(zval * zv)3665 static void delete_fault_persistent(zval *zv)
3666 {
3667 	sdlFaultPtr fault = Z_PTR_P(zv);
3668 	if (fault->name) {
3669 		free(fault->name);
3670 	}
3671 	if (fault->details) {
3672 		zend_hash_destroy(fault->details);
3673 		free(fault->details);
3674 	}
3675 	if (fault->bindingAttributes) {
3676 		sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
3677 
3678 		if (binding->ns) {
3679 			free(binding->ns);
3680 		}
3681 		free(fault->bindingAttributes);
3682 	}
3683 	free(fault);
3684 }
3685 
delete_document(zval * zv)3686 static void delete_document(zval *zv)
3687 {
3688 	xmlDocPtr doc = Z_PTR_P(zv);
3689 	xmlFreeDoc(doc);
3690 }
3691