os/ossrv/ssl/libcrypto/src/crypto/mem.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/mem.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,554 @@
     1.4 +/* crypto/mem.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +/*
    1.62 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.63 + */
    1.64 +
    1.65 +
    1.66 +#include <stdio.h>
    1.67 +#include <stdlib.h>
    1.68 +#include <openssl/crypto.h>
    1.69 +#include "cryptlib.h"
    1.70 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.71 +#include "libcrypto_wsd_macros.h"
    1.72 +#include "libcrypto_wsd.h"
    1.73 +#endif
    1.74 +
    1.75 +#ifndef EMULATOR
    1.76 +static int allow_customize = 1;      /* we provide flexible functions for */
    1.77 +static int allow_customize_debug = 1;/* exchanging memory-related functions at
    1.78 +                                      * run-time, but this must be done
    1.79 +                                      * before any blocks are actually
    1.80 +                                      * allocated; or we'll run into huge
    1.81 +                                      * problems when malloc/free pairs
    1.82 +                                      * don't match etc. */
    1.83 +#else
    1.84 + GET_STATIC_VAR_FROM_TLS(allow_customize,mem,int)
    1.85 + #define allow_customize (*GET_WSD_VAR_NAME(allow_customize,mem,s)())
    1.86 + GET_STATIC_VAR_FROM_TLS(allow_customize_debug,mem,int)
    1.87 + #define allow_customize_debug (*GET_WSD_VAR_NAME(allow_customize_debug,mem,s)())
    1.88 +#endif
    1.89 +
    1.90 +
    1.91 +/* the following pointers may be changed as long as 'allow_customize' is set */
    1.92 +#ifndef EMULATOR
    1.93 +typedef void *(*malloc_func_t)(size_t);
    1.94 +static void *(*malloc_func)(size_t)         = NULL;
    1.95 +static malloc_func_t const malloc_func_def = malloc;
    1.96 +static void *default_malloc_ex(size_t num, const char *file, int line)
    1.97 +	{ 
    1.98 +		if(malloc_func)
    1.99 +			return malloc_func(num); 
   1.100 +		else
   1.101 +			return malloc_func_def(num);
   1.102 +	}
   1.103 +#else
   1.104 +#define malloc_func libcrypto_ImpurePtr()->malloc_func
   1.105 +void *default_malloc_ex(size_t num, const char *file, int line)
   1.106 +	{ return malloc_func(num); }
   1.107 +#endif
   1.108 +#ifndef EMULATOR
   1.109 +static void *(*malloc_ex_func)(size_t, const char *file, int line)
   1.110 +        = default_malloc_ex;
   1.111 +#else
   1.112 +#define malloc_ex_func libcrypto_ImpurePtr()->malloc_ex_func        
   1.113 +#endif        
   1.114 +
   1.115 +#ifndef EMULATOR
   1.116 +typedef void *(*realloc_func_t)(void *, size_t);
   1.117 +static void *(*realloc_func)(void *, size_t)= NULL;
   1.118 +static realloc_func_t const  realloc_func_def = realloc ;
   1.119 +static void *default_realloc_ex(void *str, size_t num,
   1.120 +        const char *file, int line)
   1.121 +	{ 
   1.122 +		if(realloc_func)
   1.123 +			return realloc_func(str,num); 
   1.124 +		else
   1.125 +			return realloc_func_def(str,num);		 
   1.126 +	}
   1.127 +#else
   1.128 +#define realloc_func libcrypto_ImpurePtr()->realloc_func
   1.129 +void *default_realloc_ex(void *str, size_t num,
   1.130 +        const char *file, int line)
   1.131 +	{ return realloc_func(str,num); }
   1.132 +#endif
   1.133 +
   1.134 +
   1.135 +#ifndef EMULATOR	
   1.136 +static void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
   1.137 +        = default_realloc_ex;
   1.138 +#else
   1.139 +#define realloc_ex_func libcrypto_ImpurePtr()->realloc_ex_func
   1.140 +#endif        
   1.141 +
   1.142 +#ifndef EMULATOR
   1.143 +typedef void (*free_func_t)(void *);
   1.144 +static void (*free_func)(void *)            = NULL;
   1.145 +static free_func_t const free_func_def = free;
   1.146 +#else
   1.147 +#define free_func_openssl libcrypto_ImpurePtr()->free_func_openssl
   1.148 +#endif
   1.149 +
   1.150 +#ifndef EMULATOR
   1.151 +typedef void *(*malloc_locked_func_t)(size_t);
   1.152 +static void *(*malloc_locked_func)(size_t)  = NULL;
   1.153 +static malloc_locked_func_t const malloc_locked_func_def = malloc;
   1.154 +#else
   1.155 +#define  malloc_locked_func libcrypto_ImpurePtr()->malloc_locked_func
   1.156 +#endif
   1.157 +
   1.158 +#ifndef EMULATOR	
   1.159 +static void *default_malloc_locked_ex(size_t num, const char *file, int line)
   1.160 +	{ 
   1.161 +		if(malloc_locked_func)
   1.162 +			return malloc_locked_func(num); 
   1.163 +		else
   1.164 +			return malloc_locked_func_def(num);
   1.165 +		}
   1.166 +
   1.167 +static void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
   1.168 +        = default_malloc_locked_ex;
   1.169 +    
   1.170 +typedef void (*free_locked_func_t)(void *);
   1.171 +static void (*free_locked_func)(void *)     = NULL;
   1.172 +static free_locked_func_t const free_locked_func_def = free;
   1.173 +#else
   1.174 +void *default_malloc_locked_ex(size_t num, const char *file, int line)
   1.175 +	{ return malloc_locked_func(num); }
   1.176 +
   1.177 +#define malloc_locked_ex_func libcrypto_ImpurePtr()->malloc_locked_ex_func
   1.178 +#define free_locked_func libcrypto_ImpurePtr()->free_locked_func
   1.179 +#endif
   1.180 +
   1.181 +
   1.182 +
   1.183 +
   1.184 +/* may be changed as long as 'allow_customize_debug' is set */
   1.185 +/* XXX use correct function pointer types */
   1.186 +#ifdef CRYPTO_MDEBUG
   1.187 +/* use default functions from mem_dbg.c */
   1.188 +#ifndef EMULATOR
   1.189 +static void (*malloc_debug_func)(void *,int,const char *,int,int)
   1.190 +	= CRYPTO_dbg_malloc;
   1.191 +static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
   1.192 +	= CRYPTO_dbg_realloc;
   1.193 +static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free;
   1.194 +static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
   1.195 +static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
   1.196 +#else
   1.197 +#define malloc_debug_func libcrypto_ImpurePtr()->malloc_debug_func
   1.198 +#define realloc_debug_func libcrypto_ImpurePtr()->realloc_debug_func
   1.199 +#define free_debug_func libcrypto_ImpurePtr()->free_debug_func
   1.200 +#define set_debug_options_func libcrypto_ImpurePtr()->set_debug_options_func
   1.201 +#define get_debug_options_func libcrypto_ImpurePtr()->get_debug_options_func
   1.202 +
   1.203 +#endif
   1.204 +#else
   1.205 +/* applications can use CRYPTO_malloc_debug_init() to select above case
   1.206 + * at run-time */
   1.207 +static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL;
   1.208 +static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
   1.209 +	= NULL;
   1.210 +static void (*free_debug_func)(void *,int) = NULL;
   1.211 +static void (*set_debug_options_func)(long) = NULL;
   1.212 +static long (*get_debug_options_func)(void) = NULL;
   1.213 +#endif
   1.214 +
   1.215 +
   1.216 +EXPORT_C int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
   1.217 +	void (*f)(void *))
   1.218 +	{
   1.219 +	if (!allow_customize)
   1.220 +		return 0;
   1.221 +	if ((m == 0) || (r == 0) || (f == 0))
   1.222 +		return 0;
   1.223 +	malloc_func=m; malloc_ex_func=default_malloc_ex;
   1.224 +	realloc_func=r; realloc_ex_func=default_realloc_ex;
   1.225 +#ifndef EMULATOR
   1.226 +    free_func=f;
   1.227 +#else	
   1.228 +	free_func_openssl=f;
   1.229 +#endif	
   1.230 +	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
   1.231 +	free_locked_func=f;
   1.232 +	return 1;
   1.233 +	}
   1.234 +
   1.235 +EXPORT_C int CRYPTO_set_mem_ex_functions(
   1.236 +        void *(*m)(size_t,const char *,int),
   1.237 +        void *(*r)(void *, size_t,const char *,int),
   1.238 +	void (*f)(void *))
   1.239 +	{
   1.240 +	if (!allow_customize)
   1.241 +		return 0;
   1.242 +	if ((m == 0) || (r == 0) || (f == 0))
   1.243 +		return 0;
   1.244 +	malloc_func=0; malloc_ex_func=m;
   1.245 +	realloc_func=0; realloc_ex_func=r;
   1.246 +#ifndef EMULATOR
   1.247 +    free_func=f;
   1.248 +#else	
   1.249 +	free_func_openssl=f;
   1.250 +#endif	
   1.251 +	malloc_locked_func=0; malloc_locked_ex_func=m;
   1.252 +	free_locked_func=f;
   1.253 +	return 1;
   1.254 +	}
   1.255 +
   1.256 +EXPORT_C int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
   1.257 +	{
   1.258 +	if (!allow_customize)
   1.259 +		return 0;
   1.260 +	if ((m == NULL) || (f == NULL))
   1.261 +		return 0;
   1.262 +	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
   1.263 +	free_locked_func=f;
   1.264 +	return 1;
   1.265 +	}
   1.266 +
   1.267 +EXPORT_C int CRYPTO_set_locked_mem_ex_functions(
   1.268 +        void *(*m)(size_t,const char *,int),
   1.269 +        void (*f)(void *))
   1.270 +	{
   1.271 +	if (!allow_customize)
   1.272 +		return 0;
   1.273 +	if ((m == NULL) || (f == NULL))
   1.274 +		return 0;
   1.275 +	malloc_locked_func=0; malloc_locked_ex_func=m;
   1.276 +#ifndef EMULATOR	
   1.277 +    free_func=f;
   1.278 +#else
   1.279 +	free_func_openssl=f;
   1.280 +#endif	
   1.281 +	return 1;
   1.282 +	}
   1.283 +
   1.284 +EXPORT_C int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
   1.285 +				   void (*r)(void *,void *,int,const char *,int,int),
   1.286 +				   void (*f)(void *,int),
   1.287 +				   void (*so)(long),
   1.288 +				   long (*go)(void))
   1.289 +	{
   1.290 +	if (!allow_customize_debug)
   1.291 +		return 0;
   1.292 +	malloc_debug_func=m;
   1.293 +	realloc_debug_func=r;
   1.294 +	free_debug_func=f;
   1.295 +	set_debug_options_func=so;
   1.296 +	get_debug_options_func=go;
   1.297 +	return 1;
   1.298 +	}
   1.299 +
   1.300 +
   1.301 +EXPORT_C void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
   1.302 +	void (**f)(void *))
   1.303 +	{
   1.304 +	if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? 
   1.305 +#ifndef	EMULATOR	
   1.306 +	                     (malloc_func!=NULL?malloc_func:malloc_func_def) : 0;
   1.307 +#else
   1.308 +											 malloc_func : 0;
   1.309 +#endif
   1.310 +	                     
   1.311 +	if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? 
   1.312 +#ifndef	EMULATOR	
   1.313 +	                     (realloc_func!=NULL?realloc_func:realloc_func_def) : 0;
   1.314 +#else
   1.315 +											 realloc_func : 0;
   1.316 +#endif
   1.317 +	                     
   1.318 +#ifndef EMULATOR	
   1.319 +   if (f != NULL) *f=(free_func?free_func:free_func_def);
   1.320 +#else
   1.321 +	if (f != NULL) *f=free_func_openssl;
   1.322 +#endif	
   1.323 +	}
   1.324 +
   1.325 +EXPORT_C void CRYPTO_get_mem_ex_functions(
   1.326 +        void *(**m)(size_t,const char *,int),
   1.327 +        void *(**r)(void *, size_t,const char *,int),
   1.328 +	void (**f)(void *))
   1.329 +	{
   1.330 +	if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ?
   1.331 +	                    malloc_ex_func : 0;
   1.332 +	if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ?
   1.333 +	                    realloc_ex_func : 0;
   1.334 +	#ifndef EMULATOR	
   1.335 +	if (f != NULL) *f=(free_func?free_func:free_func_def);
   1.336 +	#else
   1.337 +	if (f != NULL) *f=free_func_openssl;
   1.338 +	#endif
   1.339 +	}
   1.340 +
   1.341 +EXPORT_C void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
   1.342 +	{
   1.343 +	if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? 
   1.344 +#ifndef	EMULATOR
   1.345 +	                     (malloc_locked_func?malloc_locked_func:malloc_locked_func_def) : 0;
   1.346 +#else
   1.347 +												malloc_locked_func : 0;
   1.348 +#endif	                     
   1.349 +#ifndef	EMULATOR
   1.350 +	if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def);
   1.351 +#else
   1.352 +	if (f != NULL) *f=free_locked_func;
   1.353 +#endif	
   1.354 +	}
   1.355 +
   1.356 +EXPORT_C void CRYPTO_get_locked_mem_ex_functions(
   1.357 +        void *(**m)(size_t,const char *,int),
   1.358 +        void (**f)(void *))
   1.359 +	{
   1.360 +	if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
   1.361 +	                    malloc_locked_ex_func : 0;
   1.362 +#ifndef	EMULATOR	                    
   1.363 +	if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def);
   1.364 +#else
   1.365 +	if (f != NULL) *f= free_locked_func;
   1.366 +#endif	
   1.367 +	}
   1.368 +
   1.369 +EXPORT_C void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
   1.370 +				    void (**r)(void *,void *,int,const char *,int,int),
   1.371 +				    void (**f)(void *,int),
   1.372 +				    void (**so)(long),
   1.373 +				    long (**go)(void))
   1.374 +	{
   1.375 +	if (m != NULL) *m=malloc_debug_func;
   1.376 +	if (r != NULL) *r=realloc_debug_func;
   1.377 +	if (f != NULL) *f=free_debug_func;
   1.378 +	if (so != NULL) *so=set_debug_options_func;
   1.379 +	if (go != NULL) *go=get_debug_options_func;
   1.380 +	}
   1.381 +
   1.382 +#ifdef EMULATOR
   1.383 +
   1.384 +unsigned char GET_WSD_VAR_NAME(cleanse_ctr,mem_clr, g)();
   1.385 +#define cleanse_ctr (*GET_WSD_VAR_NAME(cleanse_ctr,mem_clr, g)())
   1.386 +#endif
   1.387 +
   1.388 +EXPORT_C void *CRYPTO_malloc_locked(int num, const char *file, int line)
   1.389 +	{
   1.390 +	void *ret = NULL;
   1.391 +	extern unsigned char cleanse_ctr;
   1.392 +
   1.393 +	if (num <= 0) return NULL;
   1.394 +
   1.395 +	allow_customize = 0;
   1.396 +	if (malloc_debug_func != NULL)
   1.397 +		{
   1.398 +		allow_customize_debug = 0;
   1.399 +		malloc_debug_func(NULL, num, file, line, 0);
   1.400 +		}
   1.401 +	ret = malloc_locked_ex_func(num,file,line);
   1.402 +#ifdef LEVITTE_DEBUG_MEM
   1.403 +	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
   1.404 +#endif
   1.405 +	if (malloc_debug_func != NULL)
   1.406 +		malloc_debug_func(ret, num, file, line, 1);
   1.407 +
   1.408 +        /* Create a dependency on the value of 'cleanse_ctr' so our memory
   1.409 +         * sanitisation function can't be optimised out. NB: We only do
   1.410 +         * this for >2Kb so the overhead doesn't bother us. */
   1.411 +        if(ret && (num > 2048))
   1.412 +		((unsigned char *)ret)[0] = cleanse_ctr;
   1.413 +
   1.414 +	return ret;
   1.415 +	}
   1.416 +
   1.417 +EXPORT_C void CRYPTO_free_locked(void *str)
   1.418 +	{
   1.419 +	if (free_debug_func != NULL)
   1.420 +		free_debug_func(str, 0);
   1.421 +#ifdef LEVITTE_DEBUG_MEM
   1.422 +	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
   1.423 +#endif
   1.424 +#ifndef	EMULATOR
   1.425 +	free_locked_func?free_locked_func(str):free_locked_func_def(str);
   1.426 +#else
   1.427 +	free_locked_func(str);
   1.428 +#endif	
   1.429 +	if (free_debug_func != NULL)
   1.430 +		free_debug_func(NULL, 1);
   1.431 +	}
   1.432 +
   1.433 +
   1.434 +
   1.435 +EXPORT_C void *CRYPTO_malloc(int num, const char *file, int line)
   1.436 +	{
   1.437 +	void *ret = NULL;
   1.438 +	extern unsigned char cleanse_ctr;
   1.439 +
   1.440 +	if (num <= 0) return NULL;
   1.441 +
   1.442 +	allow_customize = 0;
   1.443 +	if (malloc_debug_func != NULL)
   1.444 +		{
   1.445 +		allow_customize_debug = 0;
   1.446 +		malloc_debug_func(NULL, num, file, line, 0);
   1.447 +		}
   1.448 +	ret = malloc_ex_func(num,file,line);
   1.449 +#ifdef LEVITTE_DEBUG_MEM
   1.450 +	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
   1.451 +#endif
   1.452 +	if (malloc_debug_func != NULL)
   1.453 +		malloc_debug_func(ret, num, file, line, 1);
   1.454 +
   1.455 +        /* Create a dependency on the value of 'cleanse_ctr' so our memory
   1.456 +         * sanitisation function can't be optimised out. NB: We only do
   1.457 +         * this for >2Kb so the overhead doesn't bother us. */
   1.458 +        if(ret && (num > 2048))
   1.459 +                ((unsigned char *)ret)[0] = cleanse_ctr;
   1.460 +
   1.461 +	return ret;
   1.462 +	}
   1.463 +
   1.464 +EXPORT_C void *CRYPTO_realloc(void *str, int num, const char *file, int line)
   1.465 +	{
   1.466 +	void *ret = NULL;
   1.467 +
   1.468 +	if (str == NULL)
   1.469 +		return CRYPTO_malloc(num, file, line);
   1.470 +
   1.471 +	if (num <= 0) return NULL;
   1.472 +
   1.473 +	if (realloc_debug_func != NULL)
   1.474 +		realloc_debug_func(str, NULL, num, file, line, 0);
   1.475 +	ret = realloc_ex_func(str,num,file,line);
   1.476 +#ifdef LEVITTE_DEBUG_MEM
   1.477 +	fprintf(stderr, "LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n", str, ret, num);
   1.478 +#endif
   1.479 +	if (realloc_debug_func != NULL)
   1.480 +		realloc_debug_func(str, ret, num, file, line, 1);
   1.481 +
   1.482 +	return ret;
   1.483 +	}
   1.484 +
   1.485 +EXPORT_C void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
   1.486 +			   int line)
   1.487 +	{
   1.488 +	void *ret = NULL;
   1.489 +
   1.490 +	if (str == NULL)
   1.491 +		return CRYPTO_malloc(num, file, line);
   1.492 +
   1.493 +	if (num <= 0) return NULL;
   1.494 +
   1.495 +	if (realloc_debug_func != NULL)
   1.496 +		realloc_debug_func(str, NULL, num, file, line, 0);
   1.497 +	ret=malloc_ex_func(num,file,line);
   1.498 +	if(ret)
   1.499 +		{
   1.500 +		memcpy(ret,str,old_len);
   1.501 +		OPENSSL_cleanse(str,old_len);
   1.502 +		#ifndef EMULATOR
   1.503 +		free_func?free_func(str):free_func_def(str);
   1.504 +		#else
   1.505 +		free_func_openssl(str);
   1.506 +		#endif
   1.507 +		}
   1.508 +#ifdef LEVITTE_DEBUG_MEM
   1.509 +	fprintf(stderr,
   1.510 +		"LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n",
   1.511 +		str, ret, num);
   1.512 +#endif
   1.513 +	if (realloc_debug_func != NULL)
   1.514 +		realloc_debug_func(str, ret, num, file, line, 1);
   1.515 +
   1.516 +	return ret;
   1.517 +	}
   1.518 +	
   1.519 +
   1.520 +EXPORT_C void CRYPTO_free(void *str)
   1.521 +	{
   1.522 +	if (free_debug_func != NULL)
   1.523 +		free_debug_func(str, 0);
   1.524 +#ifdef LEVITTE_DEBUG_MEM
   1.525 +	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
   1.526 +#endif
   1.527 +#ifndef EMULATOR
   1.528 +	free_func?free_func(str):free_func_def(str);
   1.529 +#else
   1.530 +	free_func_openssl(str);
   1.531 +#endif	
   1.532 +	if (free_debug_func != NULL)
   1.533 +		free_debug_func(NULL, 1);
   1.534 +	}
   1.535 +
   1.536 +
   1.537 +EXPORT_C void *CRYPTO_remalloc(void *a, int num, const char *file, int line)
   1.538 +	{
   1.539 +	if (a != NULL) OPENSSL_free(a);
   1.540 +	a=(char *)OPENSSL_malloc(num);
   1.541 +	return(a);
   1.542 +	}
   1.543 +
   1.544 +EXPORT_C void CRYPTO_set_mem_debug_options(long bits)
   1.545 +	{
   1.546 +	if (set_debug_options_func != NULL)
   1.547 +		set_debug_options_func(bits);
   1.548 +	}
   1.549 +
   1.550 +EXPORT_C long CRYPTO_get_mem_debug_options(void)
   1.551 +	{
   1.552 +	if (get_debug_options_func != NULL)
   1.553 +		return get_debug_options_func();
   1.554 +	return 0;
   1.555 +	}
   1.556 +
   1.557 +