os/ossrv/ssl/libcrypto/src/crypto/comp/c_zlib.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/comp/c_zlib.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,646 @@
     1.4 +/*
     1.5 +Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.6 +
     1.7 +Redistribution and use in source and binary forms, with or without 
     1.8 +modification, are permitted provided that the following conditions are met:
     1.9 +
    1.10 +* Redistributions of source code must retain the above copyright notice, this 
    1.11 +  list of conditions and the following disclaimer.
    1.12 +* Redistributions in binary form must reproduce the above copyright notice, 
    1.13 +  this list of conditions and the following disclaimer in the documentation 
    1.14 +  and/or other materials provided with the distribution.
    1.15 +* Neither the name of Nokia Corporation nor the names of its contributors 
    1.16 +  may be used to endorse or promote products derived from this software 
    1.17 +  without specific prior written permission.
    1.18 +
    1.19 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    1.20 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    1.21 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    1.22 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
    1.23 +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
    1.24 +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
    1.25 +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
    1.26 +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
    1.27 +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.28 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 +
    1.30 +Description:
    1.31 +*/
    1.32 +
    1.33 +#include <stdio.h>
    1.34 +#include <stdlib.h>
    1.35 +#include <string.h>
    1.36 +#include <openssl/objects.h>
    1.37 +#include <openssl/comp.h>
    1.38 +#include <openssl/err.h>
    1.39 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.40 +#include "libcrypto_wsd_macros.h"
    1.41 +#include "libcrypto_wsd.h"
    1.42 +#endif
    1.43 +
    1.44 +
    1.45 +COMP_METHOD *COMP_zlib(void );
    1.46 +#ifndef EMULATOR
    1.47 +static COMP_METHOD zlib_method_nozlib={
    1.48 +	NID_undef,
    1.49 +	"(undef)",
    1.50 +	NULL,
    1.51 +	NULL,
    1.52 +	NULL,
    1.53 +	NULL,
    1.54 +	NULL,
    1.55 +	NULL,
    1.56 +	};
    1.57 +#else//EMULATOR
    1.58 +  GET_STATIC_VAR_FROM_TLS(zlib_method_nozlib,c_zlib,COMP_METHOD)
    1.59 +  #define zlib_method_nozlib (*GET_WSD_VAR_NAME(zlib_method_nozlib,c_zlib, s)())
    1.60 +  const COMP_METHOD temp_s_zlib_method_nozlib={
    1.61 +	NID_undef,
    1.62 +	"(undef)",
    1.63 +	NULL,
    1.64 +	NULL,
    1.65 +	NULL,
    1.66 +	NULL,
    1.67 +	NULL,
    1.68 +	NULL,
    1.69 +	};
    1.70 +#endif//EMULATOR
    1.71 +
    1.72 +#ifndef ZLIB
    1.73 +#undef ZLIB_SHARED
    1.74 +#else
    1.75 +
    1.76 +#include <zlib.h>
    1.77 +
    1.78 +static int zlib_stateful_init(COMP_CTX *ctx);
    1.79 +static void zlib_stateful_finish(COMP_CTX *ctx);
    1.80 +static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
    1.81 +	unsigned int olen, unsigned char *in, unsigned int ilen);
    1.82 +static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
    1.83 +	unsigned int olen, unsigned char *in, unsigned int ilen);
    1.84 +
    1.85 +#if 0
    1.86 +static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
    1.87 +	unsigned int olen, unsigned char *in, unsigned int ilen);
    1.88 +static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
    1.89 +	unsigned int olen, unsigned char *in, unsigned int ilen);
    1.90 +
    1.91 +static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
    1.92 +	uLong sourceLen);
    1.93 +
    1.94 +static COMP_METHOD zlib_stateless_method={
    1.95 +	NID_zlib_compression,
    1.96 +	LN_zlib_compression,
    1.97 +	NULL,
    1.98 +	NULL,
    1.99 +	zlib_compress_block,
   1.100 +	zlib_expand_block,
   1.101 +	NULL,
   1.102 +	NULL,
   1.103 +	};
   1.104 +#endif
   1.105 +#ifndef EMULATOR
   1.106 +static COMP_METHOD zlib_stateful_method={
   1.107 +	NID_zlib_compression,
   1.108 +	LN_zlib_compression,
   1.109 +	zlib_stateful_init,
   1.110 +	zlib_stateful_finish,
   1.111 +	zlib_stateful_compress_block,
   1.112 +	zlib_stateful_expand_block,
   1.113 +	NULL,
   1.114 +	NULL,
   1.115 +	};
   1.116 +#else//EMULATOR
   1.117 +  GET_STATIC_VAR_FROM_TLS(zlib_stateful_method,c_zlib,COMP_METHOD)
   1.118 +  #define zlib_stateful_method (*GET_WSD_VAR_NAME(zlib_stateful_method,c_zlib, s)())
   1.119 +  const COMP_METHOD temp_s_zlib_stateful_method={
   1.120 +	NID_zlib_compression,
   1.121 +	LN_zlib_compression,
   1.122 +	zlib_stateful_init,
   1.123 +	zlib_stateful_finish,
   1.124 +	zlib_stateful_compress_block,
   1.125 +	zlib_stateful_expand_block,
   1.126 +	NULL,
   1.127 +	NULL,
   1.128 +	};
   1.129 +#endif//EMULATOR
   1.130 +
   1.131 +/* 
   1.132 + * When OpenSSL is built on Windows, we do not want to require that
   1.133 + * the ZLIB.DLL be available in order for the OpenSSL DLLs to
   1.134 + * work.  Therefore, all ZLIB routines are loaded at run time
   1.135 + * and we do not link to a .LIB file.
   1.136 + */
   1.137 +#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
   1.138 +# include <windows.h>
   1.139 +
   1.140 +# define Z_CALLCONV _stdcall
   1.141 +# ifndef ZLIB_SHARED
   1.142 +#  define ZLIB_SHARED
   1.143 +# endif
   1.144 +#else
   1.145 +# define Z_CALLCONV
   1.146 +#endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
   1.147 +
   1.148 +#ifdef ZLIB_SHARED
   1.149 +#include <openssl/dso.h>
   1.150 +
   1.151 +/* Prototypes for built in stubs */
   1.152 +#if 0
   1.153 +static int stub_compress(Bytef *dest,uLongf *destLen,
   1.154 +	const Bytef *source, uLong sourceLen);
   1.155 +#endif
   1.156 +static int stub_inflateEnd(z_streamp strm);
   1.157 +static int stub_inflate(z_streamp strm, int flush);
   1.158 +static int stub_inflateInit_(z_streamp strm, const char * version,
   1.159 +	int stream_size);
   1.160 +static int stub_deflateEnd(z_streamp strm);
   1.161 +static int stub_deflate(z_streamp strm, int flush);
   1.162 +static int stub_deflateInit_(z_streamp strm, int level,
   1.163 +	const char * version, int stream_size);
   1.164 +
   1.165 +#ifndef EMULATOR
   1.166 +/* Function pointers */
   1.167 +typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
   1.168 +	const Bytef *source, uLong sourceLen);
   1.169 +typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
   1.170 +typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
   1.171 +typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
   1.172 +	const char * version, int stream_size);
   1.173 +typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
   1.174 +typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
   1.175 +typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
   1.176 +	const char * version, int stream_size);
   1.177 +#endif	
   1.178 +	
   1.179 +#ifndef EMULATOR	
   1.180 +static compress_ft	p_compress=NULL;
   1.181 +static inflateEnd_ft	p_inflateEnd=NULL;
   1.182 +static inflate_ft	p_inflate=NULL;
   1.183 +static inflateInit__ft	p_inflateInit_=NULL;
   1.184 +
   1.185 +static deflateEnd_ft	p_deflateEnd=NULL;
   1.186 +static deflate_ft	p_deflate=NULL;
   1.187 +static deflateInit__ft	p_deflateInit_=NULL;
   1.188 +static int zlib_loaded = 0;     /* only attempt to init func pts once */
   1.189 +static DSO *zlib_dso = NULL;
   1.190 +#else
   1.191 +GET_STATIC_VAR_FROM_TLS(p_compress,c_zlib,compress_ft)
   1.192 +#define p_compress (*GET_WSD_VAR_NAME(p_compress,c_zlib, s)())
   1.193 +
   1.194 +GET_STATIC_VAR_FROM_TLS(p_inflateEnd,c_zlib,inflateEnd_ft)
   1.195 +#define p_inflateEnd (*GET_WSD_VAR_NAME(p_inflateEnd,c_zlib, s)())
   1.196 +
   1.197 +
   1.198 +GET_STATIC_VAR_FROM_TLS(p_inflate,c_zlib,inflate_ft)
   1.199 +#define p_inflate (*GET_WSD_VAR_NAME(p_inflate,c_zlib, s)())
   1.200 +
   1.201 +GET_STATIC_VAR_FROM_TLS(p_inflateInit_,c_zlib,inflateInit__ft)
   1.202 +#define p_inflateInit_ (*GET_WSD_VAR_NAME(p_inflateInit_,c_zlib, s)())
   1.203 +
   1.204 +GET_STATIC_VAR_FROM_TLS(p_deflateEnd,c_zlib,deflateEnd_ft)
   1.205 +#define p_deflateEnd (*GET_WSD_VAR_NAME(p_deflateEnd,c_zlib, s)())
   1.206 +
   1.207 +GET_STATIC_VAR_FROM_TLS(p_deflate,c_zlib,deflate_ft)
   1.208 +#define p_deflate (*GET_WSD_VAR_NAME(p_deflate,c_zlib, s)())
   1.209 +
   1.210 +GET_STATIC_VAR_FROM_TLS(p_deflateInit_,c_zlib,deflateInit__ft)
   1.211 +#define p_deflateInit_ (*GET_WSD_VAR_NAME(p_deflateInit_,c_zlib, s)())
   1.212 +
   1.213 +
   1.214 +GET_STATIC_VAR_FROM_TLS(zlib_loaded ,c_zlib,int)
   1.215 +#define zlib_loaded  (*GET_WSD_VAR_NAME(zlib_loaded ,c_zlib, s)())
   1.216 +
   1.217 +GET_STATIC_VAR_FROM_TLS(zlib_dso ,c_zlib,DSO *)
   1.218 +#define zlib_dso  (*GET_WSD_VAR_NAME(zlib_dso ,c_zlib, s)())
   1.219 +
   1.220 +#endif
   1.221 +
   1.222 +#define compress                stub_compress
   1.223 +#define inflateEnd              stub_inflateEnd
   1.224 +#define inflate                 stub_inflate
   1.225 +#define inflateInit_            stub_inflateInit_
   1.226 +#define deflateEnd              stub_deflateEnd
   1.227 +#define deflate                 stub_deflate
   1.228 +#define deflateInit_            stub_deflateInit_
   1.229 +#endif /* ZLIB_SHARED */
   1.230 +
   1.231 +struct zlib_state
   1.232 +	{
   1.233 +	z_stream istream;
   1.234 +	z_stream ostream;
   1.235 +	};
   1.236 +
   1.237 +#ifndef EMULATOR
   1.238 +static int zlib_stateful_ex_idx = -1;
   1.239 +#else
   1.240 +GET_STATIC_VAR_FROM_TLS(zlib_stateful_ex_idx,c_zlib,int)
   1.241 +#define zlib_stateful_ex_idx (*GET_WSD_VAR_NAME(zlib_stateful_ex_idx,c_zlib, s)())
   1.242 +#endif
   1.243 +
   1.244 +static void zlib_stateful_free_ex_data(void *obj, void *item,
   1.245 +	CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
   1.246 +	{
   1.247 +	struct zlib_state *state = (struct zlib_state *)item;
   1.248 +	inflateEnd(&state->istream);
   1.249 +	deflateEnd(&state->ostream);
   1.250 +	OPENSSL_free(state);
   1.251 +	}
   1.252 +
   1.253 +static int zlib_stateful_init(COMP_CTX *ctx)
   1.254 +	{
   1.255 +	int err;
   1.256 +	struct zlib_state *state =
   1.257 +		(struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
   1.258 +
   1.259 +	if (state == NULL)
   1.260 +		goto err;
   1.261 +
   1.262 +	state->istream.zalloc = Z_NULL;
   1.263 +	state->istream.zfree = Z_NULL;
   1.264 +	state->istream.opaque = Z_NULL;
   1.265 +	state->istream.next_in = Z_NULL;
   1.266 +	state->istream.next_out = Z_NULL;
   1.267 +	state->istream.avail_in = 0;
   1.268 +	state->istream.avail_out = 0;
   1.269 +	err = inflateInit_(&state->istream,
   1.270 +		ZLIB_VERSION, sizeof(z_stream));
   1.271 +	if (err != Z_OK)
   1.272 +		goto err;
   1.273 +
   1.274 +	state->ostream.zalloc = Z_NULL;
   1.275 +	state->ostream.zfree = Z_NULL;
   1.276 +	state->ostream.opaque = Z_NULL;
   1.277 +	state->ostream.next_in = Z_NULL;
   1.278 +	state->ostream.next_out = Z_NULL;
   1.279 +	state->ostream.avail_in = 0;
   1.280 +	state->ostream.avail_out = 0;
   1.281 +	err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
   1.282 +		ZLIB_VERSION, sizeof(z_stream));
   1.283 +	if (err != Z_OK)
   1.284 +		goto err;
   1.285 +
   1.286 +	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
   1.287 +	if (zlib_stateful_ex_idx == -1)
   1.288 +		{
   1.289 +		CRYPTO_w_lock(CRYPTO_LOCK_COMP);
   1.290 +		if (zlib_stateful_ex_idx == -1)
   1.291 +			zlib_stateful_ex_idx =
   1.292 +				CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
   1.293 +					0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
   1.294 +		CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
   1.295 +		if (zlib_stateful_ex_idx == -1)
   1.296 +			goto err;
   1.297 +		}
   1.298 +	CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
   1.299 +	return 1;
   1.300 + err:
   1.301 +	if (state) OPENSSL_free(state);
   1.302 +	return 0;
   1.303 +	}
   1.304 +
   1.305 +static void zlib_stateful_finish(COMP_CTX *ctx)
   1.306 +	{
   1.307 +	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
   1.308 +	}
   1.309 +
   1.310 +static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
   1.311 +	unsigned int olen, unsigned char *in, unsigned int ilen)
   1.312 +	{
   1.313 +	int err = Z_OK;
   1.314 +	struct zlib_state *state =
   1.315 +		(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
   1.316 +			zlib_stateful_ex_idx);
   1.317 +
   1.318 +	if (state == NULL)
   1.319 +		return -1;
   1.320 +
   1.321 +	state->ostream.next_in = in;
   1.322 +	state->ostream.avail_in = ilen;
   1.323 +	state->ostream.next_out = out;
   1.324 +	state->ostream.avail_out = olen;
   1.325 +	if (ilen > 0)
   1.326 +		err = deflate(&state->ostream, Z_SYNC_FLUSH);
   1.327 +	if (err != Z_OK)
   1.328 +		return -1;
   1.329 +#ifdef DEBUG_ZLIB
   1.330 +	fprintf(stderr,"compress(%4d)->%4d %s\n",
   1.331 +		ilen,olen - state->ostream.avail_out,
   1.332 +		(ilen != olen - state->ostream.avail_out)?"zlib":"clear");
   1.333 +#endif
   1.334 +	return olen - state->ostream.avail_out;
   1.335 +	}
   1.336 +
   1.337 +static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
   1.338 +	unsigned int olen, unsigned char *in, unsigned int ilen)
   1.339 +	{
   1.340 +	int err = Z_OK;
   1.341 +
   1.342 +	struct zlib_state *state =
   1.343 +		(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
   1.344 +			zlib_stateful_ex_idx);
   1.345 +
   1.346 +	if (state == NULL)
   1.347 +		return 0;
   1.348 +
   1.349 +	state->istream.next_in = in;
   1.350 +	state->istream.avail_in = ilen;
   1.351 +	state->istream.next_out = out;
   1.352 +	state->istream.avail_out = olen;
   1.353 +	if (ilen > 0)
   1.354 +		err = inflate(&state->istream, Z_SYNC_FLUSH);
   1.355 +	if (err != Z_OK)
   1.356 +		return -1;
   1.357 +#ifdef DEBUG_ZLIB
   1.358 +	fprintf(stderr,"expand(%4d)->%4d %s\n",
   1.359 +		ilen,olen - state->istream.avail_out,
   1.360 +		(ilen != olen - state->istream.avail_out)?"zlib":"clear");
   1.361 +#endif
   1.362 +	return olen - state->istream.avail_out;
   1.363 +	}
   1.364 +
   1.365 +#if 0
   1.366 +static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
   1.367 +	unsigned int olen, unsigned char *in, unsigned int ilen)
   1.368 +	{
   1.369 +	unsigned long l;
   1.370 +	int i;
   1.371 +	int clear=1;
   1.372 +
   1.373 +	if (ilen > 128)
   1.374 +		{
   1.375 +		out[0]=1;
   1.376 +		l=olen-1;
   1.377 +		i=compress(&(out[1]),&l,in,(unsigned long)ilen);
   1.378 +		if (i != Z_OK)
   1.379 +			return(-1);
   1.380 +		if (ilen > l)
   1.381 +			{
   1.382 +			clear=0;
   1.383 +			l++;
   1.384 +			}
   1.385 +		}
   1.386 +	if (clear)
   1.387 +		{
   1.388 +		out[0]=0;
   1.389 +		memcpy(&(out[1]),in,ilen);
   1.390 +		l=ilen+1;
   1.391 +		}
   1.392 +#ifdef DEBUG_ZLIB
   1.393 +	fprintf(stderr,"compress(%4d)->%4d %s\n",
   1.394 +		ilen,(int)l,(clear)?"clear":"zlib");
   1.395 +#endif
   1.396 +	return((int)l);
   1.397 +	}
   1.398 +
   1.399 +static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
   1.400 +	unsigned int olen, unsigned char *in, unsigned int ilen)
   1.401 +	{
   1.402 +	unsigned long l;
   1.403 +	int i;
   1.404 +
   1.405 +	if (in[0])
   1.406 +		{
   1.407 +		l=olen;
   1.408 +		i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
   1.409 +		if (i != Z_OK)
   1.410 +			return(-1);
   1.411 +		}
   1.412 +	else
   1.413 +		{
   1.414 +		memcpy(out,&(in[1]),ilen-1);
   1.415 +		l=ilen-1;
   1.416 +		}
   1.417 +#ifdef DEBUG_ZLIB
   1.418 +        fprintf(stderr,"expand  (%4d)->%4d %s\n",
   1.419 +		ilen,(int)l,in[0]?"zlib":"clear");
   1.420 +#endif
   1.421 +	return((int)l);
   1.422 +	}
   1.423 +
   1.424 +static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
   1.425 +	     uLong sourceLen)
   1.426 +{
   1.427 +    z_stream stream;
   1.428 +    int err;
   1.429 +
   1.430 +    stream.next_in = (Bytef*)source;
   1.431 +    stream.avail_in = (uInt)sourceLen;
   1.432 +    /* Check for source > 64K on 16-bit machine: */
   1.433 +    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
   1.434 +
   1.435 +    stream.next_out = dest;
   1.436 +    stream.avail_out = (uInt)*destLen;
   1.437 +    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
   1.438 +
   1.439 +    stream.zalloc = (alloc_func)0;
   1.440 +    stream.zfree = (free_func)0;
   1.441 +
   1.442 +    err = inflateInit_(&stream,
   1.443 +	    ZLIB_VERSION, sizeof(z_stream));
   1.444 +    if (err != Z_OK) return err;
   1.445 +
   1.446 +    err = inflate(&stream, Z_FINISH);
   1.447 +    if (err != Z_STREAM_END) {
   1.448 +        inflateEnd(&stream);
   1.449 +        return err;
   1.450 +    }
   1.451 +    *destLen = stream.total_out;
   1.452 +
   1.453 +    err = inflateEnd(&stream);
   1.454 +    return err;
   1.455 +}
   1.456 +#endif
   1.457 +
   1.458 +#endif
   1.459 +
   1.460 +EXPORT_C COMP_METHOD *COMP_zlib(void)
   1.461 +	{
   1.462 +	COMP_METHOD *meth = &zlib_method_nozlib;
   1.463 +
   1.464 +#ifdef ZLIB_SHARED
   1.465 +	if (!zlib_loaded)
   1.466 +		{
   1.467 +#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
   1.468 +		zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
   1.469 +		if (!zlib_dso)
   1.470 +			{
   1.471 +			zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
   1.472 +			if (zlib_dso)
   1.473 +				{
   1.474 +				/* Clear the errors from the first failed
   1.475 +				   DSO_load() */
   1.476 +				ERR_clear_error();
   1.477 +				}
   1.478 +			}
   1.479 +#else
   1.480 +		zlib_dso = DSO_load(NULL, "z", NULL, 0);
   1.481 +#endif
   1.482 +#ifndef SYMBIAN
   1.483 +		if (zlib_dso != NULL)
   1.484 +			{
   1.485 +			p_compress
   1.486 +				= (compress_ft) DSO_bind_func(zlib_dso,
   1.487 +					"compress");
   1.488 +			p_inflateEnd
   1.489 +				= (inflateEnd_ft) DSO_bind_func(zlib_dso,
   1.490 +					"inflateEnd");
   1.491 +			p_inflate
   1.492 +				= (inflate_ft) DSO_bind_func(zlib_dso,
   1.493 +					"inflate");
   1.494 +			p_inflateInit_
   1.495 +				= (inflateInit__ft) DSO_bind_func(zlib_dso,
   1.496 +					"inflateInit_");
   1.497 +			p_deflateEnd
   1.498 +				= (deflateEnd_ft) DSO_bind_func(zlib_dso,
   1.499 +					"deflateEnd");
   1.500 +			p_deflate
   1.501 +				= (deflate_ft) DSO_bind_func(zlib_dso,
   1.502 +					"deflate");
   1.503 +			p_deflateInit_
   1.504 +				= (deflateInit__ft) DSO_bind_func(zlib_dso,
   1.505 +					"deflateInit_");
   1.506 +			zlib_loaded++;
   1.507 +			}
   1.508 +#else
   1.509 +#ifdef LIBDL_ONLY_ORDINALS
   1.510 +#define zlib_compress "2"
   1.511 +#define zlib_inflateEnd "34"
   1.512 +#define zlib_inflate "33"
   1.513 +#define zlib_inflateInit_ "36"
   1.514 +#define zlib_deflateEnd "9"
   1.515 +#define zlib_deflate "6"
   1.516 +#define zlib_deflateInit_ "11"
   1.517 +#else
   1.518 +#define zlib_compress compress
   1.519 +#define zlib_inflateEnd inflateEnd
   1.520 +#define zlib_inflate inflate
   1.521 +#define zlib_inflateInit_ inflateInit_
   1.522 +#define zlib_deflateEnd deflateEnd
   1.523 +#define zlib_deflate deflate
   1.524 +#define zlib_deflateInit_ deflateInit_
   1.525 +#endif
   1.526 +	    if (zlib_dso != NULL)
   1.527 +			{
   1.528 +			p_compress
   1.529 +				= (compress_ft) DSO_bind_func(zlib_dso,
   1.530 +					(const char*)zlib_compress);
   1.531 +			p_inflateEnd
   1.532 +				= (inflateEnd_ft) DSO_bind_func(zlib_dso,
   1.533 +					(const char*)zlib_inflateEnd);
   1.534 +			p_inflate
   1.535 +				= (inflate_ft) DSO_bind_func(zlib_dso,
   1.536 +					(const char*)zlib_inflate);
   1.537 +			p_inflateInit_
   1.538 +				= (inflateInit__ft) DSO_bind_func(zlib_dso,
   1.539 +					(const char*)zlib_inflateInit_);
   1.540 +			p_deflateEnd
   1.541 +				= (deflateEnd_ft) DSO_bind_func(zlib_dso,
   1.542 +					(const char*)zlib_deflateEnd);
   1.543 +			p_deflate
   1.544 +				= (deflate_ft) DSO_bind_func(zlib_dso,
   1.545 +					(const char*)zlib_deflate);
   1.546 +			p_deflateInit_
   1.547 +				= (deflateInit__ft) DSO_bind_func(zlib_dso,
   1.548 +					(const char*)zlib_deflateInit_);
   1.549 +			zlib_loaded++;
   1.550 +			}
   1.551 +
   1.552 +#endif			
   1.553 +		}
   1.554 +
   1.555 +#endif
   1.556 +#if defined(ZLIB) || defined(ZLIB_SHARED)
   1.557 +			{
   1.558 +		/* init zlib_stateful_ex_idx here so that in a multi-process
   1.559 +		 * application it's enough to intialize openssl before forking
   1.560 +		 * (idx will be inherited in all the children) */
   1.561 +		if (zlib_stateful_ex_idx == -1)
   1.562 +			{
   1.563 +			CRYPTO_w_lock(CRYPTO_LOCK_COMP);
   1.564 +			if (zlib_stateful_ex_idx == -1)
   1.565 +				zlib_stateful_ex_idx =
   1.566 +					CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
   1.567 +						0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
   1.568 +			CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
   1.569 +			if (zlib_stateful_ex_idx == -1)
   1.570 +				goto err;
   1.571 +			}
   1.572 +		
   1.573 +		meth = &zlib_stateful_method;
   1.574 +		}
   1.575 +err:	
   1.576 +#endif
   1.577 +
   1.578 +	return(meth);
   1.579 +	}
   1.580 +
   1.581 +#ifdef ZLIB_SHARED
   1.582 +#if 0
   1.583 +/* Stubs for each function to be dynamicly loaded */
   1.584 +static int 
   1.585 +stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
   1.586 +	{
   1.587 +	if (p_compress)
   1.588 +		return(p_compress(dest,destLen,source,sourceLen));
   1.589 +	else
   1.590 +		return(Z_MEM_ERROR);
   1.591 +	}
   1.592 +#endif
   1.593 +
   1.594 +static int
   1.595 +stub_inflateEnd(z_streamp strm)
   1.596 +	{
   1.597 +	if ( p_inflateEnd )
   1.598 +		return(p_inflateEnd(strm));
   1.599 +	else
   1.600 +		return(Z_MEM_ERROR);
   1.601 +	}
   1.602 +
   1.603 +static int
   1.604 +stub_inflate(z_streamp strm, int flush)
   1.605 +	{
   1.606 +	if ( p_inflate )
   1.607 +		return(p_inflate(strm,flush));
   1.608 +	else
   1.609 +		return(Z_MEM_ERROR);
   1.610 +	}
   1.611 +
   1.612 +static int
   1.613 +stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
   1.614 +	{
   1.615 +	if ( p_inflateInit_ )
   1.616 +		return(p_inflateInit_(strm,version,stream_size));
   1.617 +	else
   1.618 +		return(Z_MEM_ERROR);
   1.619 +	}
   1.620 +
   1.621 +static int
   1.622 +stub_deflateEnd(z_streamp strm)
   1.623 +	{
   1.624 +	if ( p_deflateEnd )
   1.625 +		return(p_deflateEnd(strm));
   1.626 +	else
   1.627 +		return(Z_MEM_ERROR);
   1.628 +	}
   1.629 +
   1.630 +static int
   1.631 +stub_deflate(z_streamp strm, int flush)
   1.632 +	{
   1.633 +	if ( p_deflate )
   1.634 +		return(p_deflate(strm,flush));
   1.635 +	else
   1.636 +		return(Z_MEM_ERROR);
   1.637 +	}
   1.638 +
   1.639 +static int
   1.640 +stub_deflateInit_(z_streamp strm, int level,
   1.641 +	const char * version, int stream_size)
   1.642 +	{
   1.643 +	if ( p_deflateInit_ )
   1.644 +		return(p_deflateInit_(strm,level,version,stream_size));
   1.645 +	else
   1.646 +		return(Z_MEM_ERROR);
   1.647 +	}
   1.648 +
   1.649 +#endif /* ZLIB_SHARED */