PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/soap - php_sdl.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1523 2088 72.9 %
Date: 2015-05-21 Functions: 61 73 83.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:03 +0000 (4 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.