os/ossrv/ssl/libcrypto/src/crypto/mem.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/mem.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
/*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
sl@0
    62
sl@0
    63
#include <stdio.h>
sl@0
    64
#include <stdlib.h>
sl@0
    65
#include <openssl/crypto.h>
sl@0
    66
#include "cryptlib.h"
sl@0
    67
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    68
#include "libcrypto_wsd_macros.h"
sl@0
    69
#include "libcrypto_wsd.h"
sl@0
    70
#endif
sl@0
    71
sl@0
    72
#ifndef EMULATOR
sl@0
    73
static int allow_customize = 1;      /* we provide flexible functions for */
sl@0
    74
static int allow_customize_debug = 1;/* exchanging memory-related functions at
sl@0
    75
                                      * run-time, but this must be done
sl@0
    76
                                      * before any blocks are actually
sl@0
    77
                                      * allocated; or we'll run into huge
sl@0
    78
                                      * problems when malloc/free pairs
sl@0
    79
                                      * don't match etc. */
sl@0
    80
#else
sl@0
    81
 GET_STATIC_VAR_FROM_TLS(allow_customize,mem,int)
sl@0
    82
 #define allow_customize (*GET_WSD_VAR_NAME(allow_customize,mem,s)())
sl@0
    83
 GET_STATIC_VAR_FROM_TLS(allow_customize_debug,mem,int)
sl@0
    84
 #define allow_customize_debug (*GET_WSD_VAR_NAME(allow_customize_debug,mem,s)())
sl@0
    85
#endif
sl@0
    86
sl@0
    87
sl@0
    88
/* the following pointers may be changed as long as 'allow_customize' is set */
sl@0
    89
#ifndef EMULATOR
sl@0
    90
typedef void *(*malloc_func_t)(size_t);
sl@0
    91
static void *(*malloc_func)(size_t)         = NULL;
sl@0
    92
static malloc_func_t const malloc_func_def = malloc;
sl@0
    93
static void *default_malloc_ex(size_t num, const char *file, int line)
sl@0
    94
	{ 
sl@0
    95
		if(malloc_func)
sl@0
    96
			return malloc_func(num); 
sl@0
    97
		else
sl@0
    98
			return malloc_func_def(num);
sl@0
    99
	}
sl@0
   100
#else
sl@0
   101
#define malloc_func libcrypto_ImpurePtr()->malloc_func
sl@0
   102
void *default_malloc_ex(size_t num, const char *file, int line)
sl@0
   103
	{ return malloc_func(num); }
sl@0
   104
#endif
sl@0
   105
#ifndef EMULATOR
sl@0
   106
static void *(*malloc_ex_func)(size_t, const char *file, int line)
sl@0
   107
        = default_malloc_ex;
sl@0
   108
#else
sl@0
   109
#define malloc_ex_func libcrypto_ImpurePtr()->malloc_ex_func        
sl@0
   110
#endif        
sl@0
   111
sl@0
   112
#ifndef EMULATOR
sl@0
   113
typedef void *(*realloc_func_t)(void *, size_t);
sl@0
   114
static void *(*realloc_func)(void *, size_t)= NULL;
sl@0
   115
static realloc_func_t const  realloc_func_def = realloc ;
sl@0
   116
static void *default_realloc_ex(void *str, size_t num,
sl@0
   117
        const char *file, int line)
sl@0
   118
	{ 
sl@0
   119
		if(realloc_func)
sl@0
   120
			return realloc_func(str,num); 
sl@0
   121
		else
sl@0
   122
			return realloc_func_def(str,num);		 
sl@0
   123
	}
sl@0
   124
#else
sl@0
   125
#define realloc_func libcrypto_ImpurePtr()->realloc_func
sl@0
   126
void *default_realloc_ex(void *str, size_t num,
sl@0
   127
        const char *file, int line)
sl@0
   128
	{ return realloc_func(str,num); }
sl@0
   129
#endif
sl@0
   130
sl@0
   131
sl@0
   132
#ifndef EMULATOR	
sl@0
   133
static void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
sl@0
   134
        = default_realloc_ex;
sl@0
   135
#else
sl@0
   136
#define realloc_ex_func libcrypto_ImpurePtr()->realloc_ex_func
sl@0
   137
#endif        
sl@0
   138
sl@0
   139
#ifndef EMULATOR
sl@0
   140
typedef void (*free_func_t)(void *);
sl@0
   141
static void (*free_func)(void *)            = NULL;
sl@0
   142
static free_func_t const free_func_def = free;
sl@0
   143
#else
sl@0
   144
#define free_func_openssl libcrypto_ImpurePtr()->free_func_openssl
sl@0
   145
#endif
sl@0
   146
sl@0
   147
#ifndef EMULATOR
sl@0
   148
typedef void *(*malloc_locked_func_t)(size_t);
sl@0
   149
static void *(*malloc_locked_func)(size_t)  = NULL;
sl@0
   150
static malloc_locked_func_t const malloc_locked_func_def = malloc;
sl@0
   151
#else
sl@0
   152
#define  malloc_locked_func libcrypto_ImpurePtr()->malloc_locked_func
sl@0
   153
#endif
sl@0
   154
sl@0
   155
#ifndef EMULATOR	
sl@0
   156
static void *default_malloc_locked_ex(size_t num, const char *file, int line)
sl@0
   157
	{ 
sl@0
   158
		if(malloc_locked_func)
sl@0
   159
			return malloc_locked_func(num); 
sl@0
   160
		else
sl@0
   161
			return malloc_locked_func_def(num);
sl@0
   162
		}
sl@0
   163
sl@0
   164
static void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
sl@0
   165
        = default_malloc_locked_ex;
sl@0
   166
    
sl@0
   167
typedef void (*free_locked_func_t)(void *);
sl@0
   168
static void (*free_locked_func)(void *)     = NULL;
sl@0
   169
static free_locked_func_t const free_locked_func_def = free;
sl@0
   170
#else
sl@0
   171
void *default_malloc_locked_ex(size_t num, const char *file, int line)
sl@0
   172
	{ return malloc_locked_func(num); }
sl@0
   173
sl@0
   174
#define malloc_locked_ex_func libcrypto_ImpurePtr()->malloc_locked_ex_func
sl@0
   175
#define free_locked_func libcrypto_ImpurePtr()->free_locked_func
sl@0
   176
#endif
sl@0
   177
sl@0
   178
sl@0
   179
sl@0
   180
sl@0
   181
/* may be changed as long as 'allow_customize_debug' is set */
sl@0
   182
/* XXX use correct function pointer types */
sl@0
   183
#ifdef CRYPTO_MDEBUG
sl@0
   184
/* use default functions from mem_dbg.c */
sl@0
   185
#ifndef EMULATOR
sl@0
   186
static void (*malloc_debug_func)(void *,int,const char *,int,int)
sl@0
   187
	= CRYPTO_dbg_malloc;
sl@0
   188
static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
sl@0
   189
	= CRYPTO_dbg_realloc;
sl@0
   190
static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free;
sl@0
   191
static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
sl@0
   192
static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
sl@0
   193
#else
sl@0
   194
#define malloc_debug_func libcrypto_ImpurePtr()->malloc_debug_func
sl@0
   195
#define realloc_debug_func libcrypto_ImpurePtr()->realloc_debug_func
sl@0
   196
#define free_debug_func libcrypto_ImpurePtr()->free_debug_func
sl@0
   197
#define set_debug_options_func libcrypto_ImpurePtr()->set_debug_options_func
sl@0
   198
#define get_debug_options_func libcrypto_ImpurePtr()->get_debug_options_func
sl@0
   199
sl@0
   200
#endif
sl@0
   201
#else
sl@0
   202
/* applications can use CRYPTO_malloc_debug_init() to select above case
sl@0
   203
 * at run-time */
sl@0
   204
static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL;
sl@0
   205
static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
sl@0
   206
	= NULL;
sl@0
   207
static void (*free_debug_func)(void *,int) = NULL;
sl@0
   208
static void (*set_debug_options_func)(long) = NULL;
sl@0
   209
static long (*get_debug_options_func)(void) = NULL;
sl@0
   210
#endif
sl@0
   211
sl@0
   212
sl@0
   213
EXPORT_C int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
sl@0
   214
	void (*f)(void *))
sl@0
   215
	{
sl@0
   216
	if (!allow_customize)
sl@0
   217
		return 0;
sl@0
   218
	if ((m == 0) || (r == 0) || (f == 0))
sl@0
   219
		return 0;
sl@0
   220
	malloc_func=m; malloc_ex_func=default_malloc_ex;
sl@0
   221
	realloc_func=r; realloc_ex_func=default_realloc_ex;
sl@0
   222
#ifndef EMULATOR
sl@0
   223
    free_func=f;
sl@0
   224
#else	
sl@0
   225
	free_func_openssl=f;
sl@0
   226
#endif	
sl@0
   227
	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
sl@0
   228
	free_locked_func=f;
sl@0
   229
	return 1;
sl@0
   230
	}
sl@0
   231
sl@0
   232
EXPORT_C int CRYPTO_set_mem_ex_functions(
sl@0
   233
        void *(*m)(size_t,const char *,int),
sl@0
   234
        void *(*r)(void *, size_t,const char *,int),
sl@0
   235
	void (*f)(void *))
sl@0
   236
	{
sl@0
   237
	if (!allow_customize)
sl@0
   238
		return 0;
sl@0
   239
	if ((m == 0) || (r == 0) || (f == 0))
sl@0
   240
		return 0;
sl@0
   241
	malloc_func=0; malloc_ex_func=m;
sl@0
   242
	realloc_func=0; realloc_ex_func=r;
sl@0
   243
#ifndef EMULATOR
sl@0
   244
    free_func=f;
sl@0
   245
#else	
sl@0
   246
	free_func_openssl=f;
sl@0
   247
#endif	
sl@0
   248
	malloc_locked_func=0; malloc_locked_ex_func=m;
sl@0
   249
	free_locked_func=f;
sl@0
   250
	return 1;
sl@0
   251
	}
sl@0
   252
sl@0
   253
EXPORT_C int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
sl@0
   254
	{
sl@0
   255
	if (!allow_customize)
sl@0
   256
		return 0;
sl@0
   257
	if ((m == NULL) || (f == NULL))
sl@0
   258
		return 0;
sl@0
   259
	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
sl@0
   260
	free_locked_func=f;
sl@0
   261
	return 1;
sl@0
   262
	}
sl@0
   263
sl@0
   264
EXPORT_C int CRYPTO_set_locked_mem_ex_functions(
sl@0
   265
        void *(*m)(size_t,const char *,int),
sl@0
   266
        void (*f)(void *))
sl@0
   267
	{
sl@0
   268
	if (!allow_customize)
sl@0
   269
		return 0;
sl@0
   270
	if ((m == NULL) || (f == NULL))
sl@0
   271
		return 0;
sl@0
   272
	malloc_locked_func=0; malloc_locked_ex_func=m;
sl@0
   273
#ifndef EMULATOR	
sl@0
   274
    free_func=f;
sl@0
   275
#else
sl@0
   276
	free_func_openssl=f;
sl@0
   277
#endif	
sl@0
   278
	return 1;
sl@0
   279
	}
sl@0
   280
sl@0
   281
EXPORT_C int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
sl@0
   282
				   void (*r)(void *,void *,int,const char *,int,int),
sl@0
   283
				   void (*f)(void *,int),
sl@0
   284
				   void (*so)(long),
sl@0
   285
				   long (*go)(void))
sl@0
   286
	{
sl@0
   287
	if (!allow_customize_debug)
sl@0
   288
		return 0;
sl@0
   289
	malloc_debug_func=m;
sl@0
   290
	realloc_debug_func=r;
sl@0
   291
	free_debug_func=f;
sl@0
   292
	set_debug_options_func=so;
sl@0
   293
	get_debug_options_func=go;
sl@0
   294
	return 1;
sl@0
   295
	}
sl@0
   296
sl@0
   297
sl@0
   298
EXPORT_C void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
sl@0
   299
	void (**f)(void *))
sl@0
   300
	{
sl@0
   301
	if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? 
sl@0
   302
#ifndef	EMULATOR	
sl@0
   303
	                     (malloc_func!=NULL?malloc_func:malloc_func_def) : 0;
sl@0
   304
#else
sl@0
   305
											 malloc_func : 0;
sl@0
   306
#endif
sl@0
   307
	                     
sl@0
   308
	if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? 
sl@0
   309
#ifndef	EMULATOR	
sl@0
   310
	                     (realloc_func!=NULL?realloc_func:realloc_func_def) : 0;
sl@0
   311
#else
sl@0
   312
											 realloc_func : 0;
sl@0
   313
#endif
sl@0
   314
	                     
sl@0
   315
#ifndef EMULATOR	
sl@0
   316
   if (f != NULL) *f=(free_func?free_func:free_func_def);
sl@0
   317
#else
sl@0
   318
	if (f != NULL) *f=free_func_openssl;
sl@0
   319
#endif	
sl@0
   320
	}
sl@0
   321
sl@0
   322
EXPORT_C void CRYPTO_get_mem_ex_functions(
sl@0
   323
        void *(**m)(size_t,const char *,int),
sl@0
   324
        void *(**r)(void *, size_t,const char *,int),
sl@0
   325
	void (**f)(void *))
sl@0
   326
	{
sl@0
   327
	if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ?
sl@0
   328
	                    malloc_ex_func : 0;
sl@0
   329
	if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ?
sl@0
   330
	                    realloc_ex_func : 0;
sl@0
   331
	#ifndef EMULATOR	
sl@0
   332
	if (f != NULL) *f=(free_func?free_func:free_func_def);
sl@0
   333
	#else
sl@0
   334
	if (f != NULL) *f=free_func_openssl;
sl@0
   335
	#endif
sl@0
   336
	}
sl@0
   337
sl@0
   338
EXPORT_C void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
sl@0
   339
	{
sl@0
   340
	if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? 
sl@0
   341
#ifndef	EMULATOR
sl@0
   342
	                     (malloc_locked_func?malloc_locked_func:malloc_locked_func_def) : 0;
sl@0
   343
#else
sl@0
   344
												malloc_locked_func : 0;
sl@0
   345
#endif	                     
sl@0
   346
#ifndef	EMULATOR
sl@0
   347
	if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def);
sl@0
   348
#else
sl@0
   349
	if (f != NULL) *f=free_locked_func;
sl@0
   350
#endif	
sl@0
   351
	}
sl@0
   352
sl@0
   353
EXPORT_C void CRYPTO_get_locked_mem_ex_functions(
sl@0
   354
        void *(**m)(size_t,const char *,int),
sl@0
   355
        void (**f)(void *))
sl@0
   356
	{
sl@0
   357
	if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
sl@0
   358
	                    malloc_locked_ex_func : 0;
sl@0
   359
#ifndef	EMULATOR	                    
sl@0
   360
	if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def);
sl@0
   361
#else
sl@0
   362
	if (f != NULL) *f= free_locked_func;
sl@0
   363
#endif	
sl@0
   364
	}
sl@0
   365
sl@0
   366
EXPORT_C void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
sl@0
   367
				    void (**r)(void *,void *,int,const char *,int,int),
sl@0
   368
				    void (**f)(void *,int),
sl@0
   369
				    void (**so)(long),
sl@0
   370
				    long (**go)(void))
sl@0
   371
	{
sl@0
   372
	if (m != NULL) *m=malloc_debug_func;
sl@0
   373
	if (r != NULL) *r=realloc_debug_func;
sl@0
   374
	if (f != NULL) *f=free_debug_func;
sl@0
   375
	if (so != NULL) *so=set_debug_options_func;
sl@0
   376
	if (go != NULL) *go=get_debug_options_func;
sl@0
   377
	}
sl@0
   378
sl@0
   379
#ifdef EMULATOR
sl@0
   380
sl@0
   381
unsigned char GET_WSD_VAR_NAME(cleanse_ctr,mem_clr, g)();
sl@0
   382
#define cleanse_ctr (*GET_WSD_VAR_NAME(cleanse_ctr,mem_clr, g)())
sl@0
   383
#endif
sl@0
   384
sl@0
   385
EXPORT_C void *CRYPTO_malloc_locked(int num, const char *file, int line)
sl@0
   386
	{
sl@0
   387
	void *ret = NULL;
sl@0
   388
	extern unsigned char cleanse_ctr;
sl@0
   389
sl@0
   390
	if (num <= 0) return NULL;
sl@0
   391
sl@0
   392
	allow_customize = 0;
sl@0
   393
	if (malloc_debug_func != NULL)
sl@0
   394
		{
sl@0
   395
		allow_customize_debug = 0;
sl@0
   396
		malloc_debug_func(NULL, num, file, line, 0);
sl@0
   397
		}
sl@0
   398
	ret = malloc_locked_ex_func(num,file,line);
sl@0
   399
#ifdef LEVITTE_DEBUG_MEM
sl@0
   400
	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
sl@0
   401
#endif
sl@0
   402
	if (malloc_debug_func != NULL)
sl@0
   403
		malloc_debug_func(ret, num, file, line, 1);
sl@0
   404
sl@0
   405
        /* Create a dependency on the value of 'cleanse_ctr' so our memory
sl@0
   406
         * sanitisation function can't be optimised out. NB: We only do
sl@0
   407
         * this for >2Kb so the overhead doesn't bother us. */
sl@0
   408
        if(ret && (num > 2048))
sl@0
   409
		((unsigned char *)ret)[0] = cleanse_ctr;
sl@0
   410
sl@0
   411
	return ret;
sl@0
   412
	}
sl@0
   413
sl@0
   414
EXPORT_C void CRYPTO_free_locked(void *str)
sl@0
   415
	{
sl@0
   416
	if (free_debug_func != NULL)
sl@0
   417
		free_debug_func(str, 0);
sl@0
   418
#ifdef LEVITTE_DEBUG_MEM
sl@0
   419
	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
sl@0
   420
#endif
sl@0
   421
#ifndef	EMULATOR
sl@0
   422
	free_locked_func?free_locked_func(str):free_locked_func_def(str);
sl@0
   423
#else
sl@0
   424
	free_locked_func(str);
sl@0
   425
#endif	
sl@0
   426
	if (free_debug_func != NULL)
sl@0
   427
		free_debug_func(NULL, 1);
sl@0
   428
	}
sl@0
   429
sl@0
   430
sl@0
   431
sl@0
   432
EXPORT_C void *CRYPTO_malloc(int num, const char *file, int line)
sl@0
   433
	{
sl@0
   434
	void *ret = NULL;
sl@0
   435
	extern unsigned char cleanse_ctr;
sl@0
   436
sl@0
   437
	if (num <= 0) return NULL;
sl@0
   438
sl@0
   439
	allow_customize = 0;
sl@0
   440
	if (malloc_debug_func != NULL)
sl@0
   441
		{
sl@0
   442
		allow_customize_debug = 0;
sl@0
   443
		malloc_debug_func(NULL, num, file, line, 0);
sl@0
   444
		}
sl@0
   445
	ret = malloc_ex_func(num,file,line);
sl@0
   446
#ifdef LEVITTE_DEBUG_MEM
sl@0
   447
	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
sl@0
   448
#endif
sl@0
   449
	if (malloc_debug_func != NULL)
sl@0
   450
		malloc_debug_func(ret, num, file, line, 1);
sl@0
   451
sl@0
   452
        /* Create a dependency on the value of 'cleanse_ctr' so our memory
sl@0
   453
         * sanitisation function can't be optimised out. NB: We only do
sl@0
   454
         * this for >2Kb so the overhead doesn't bother us. */
sl@0
   455
        if(ret && (num > 2048))
sl@0
   456
                ((unsigned char *)ret)[0] = cleanse_ctr;
sl@0
   457
sl@0
   458
	return ret;
sl@0
   459
	}
sl@0
   460
sl@0
   461
EXPORT_C void *CRYPTO_realloc(void *str, int num, const char *file, int line)
sl@0
   462
	{
sl@0
   463
	void *ret = NULL;
sl@0
   464
sl@0
   465
	if (str == NULL)
sl@0
   466
		return CRYPTO_malloc(num, file, line);
sl@0
   467
sl@0
   468
	if (num <= 0) return NULL;
sl@0
   469
sl@0
   470
	if (realloc_debug_func != NULL)
sl@0
   471
		realloc_debug_func(str, NULL, num, file, line, 0);
sl@0
   472
	ret = realloc_ex_func(str,num,file,line);
sl@0
   473
#ifdef LEVITTE_DEBUG_MEM
sl@0
   474
	fprintf(stderr, "LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n", str, ret, num);
sl@0
   475
#endif
sl@0
   476
	if (realloc_debug_func != NULL)
sl@0
   477
		realloc_debug_func(str, ret, num, file, line, 1);
sl@0
   478
sl@0
   479
	return ret;
sl@0
   480
	}
sl@0
   481
sl@0
   482
EXPORT_C void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
sl@0
   483
			   int line)
sl@0
   484
	{
sl@0
   485
	void *ret = NULL;
sl@0
   486
sl@0
   487
	if (str == NULL)
sl@0
   488
		return CRYPTO_malloc(num, file, line);
sl@0
   489
sl@0
   490
	if (num <= 0) return NULL;
sl@0
   491
sl@0
   492
	if (realloc_debug_func != NULL)
sl@0
   493
		realloc_debug_func(str, NULL, num, file, line, 0);
sl@0
   494
	ret=malloc_ex_func(num,file,line);
sl@0
   495
	if(ret)
sl@0
   496
		{
sl@0
   497
		memcpy(ret,str,old_len);
sl@0
   498
		OPENSSL_cleanse(str,old_len);
sl@0
   499
		#ifndef EMULATOR
sl@0
   500
		free_func?free_func(str):free_func_def(str);
sl@0
   501
		#else
sl@0
   502
		free_func_openssl(str);
sl@0
   503
		#endif
sl@0
   504
		}
sl@0
   505
#ifdef LEVITTE_DEBUG_MEM
sl@0
   506
	fprintf(stderr,
sl@0
   507
		"LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n",
sl@0
   508
		str, ret, num);
sl@0
   509
#endif
sl@0
   510
	if (realloc_debug_func != NULL)
sl@0
   511
		realloc_debug_func(str, ret, num, file, line, 1);
sl@0
   512
sl@0
   513
	return ret;
sl@0
   514
	}
sl@0
   515
	
sl@0
   516
sl@0
   517
EXPORT_C void CRYPTO_free(void *str)
sl@0
   518
	{
sl@0
   519
	if (free_debug_func != NULL)
sl@0
   520
		free_debug_func(str, 0);
sl@0
   521
#ifdef LEVITTE_DEBUG_MEM
sl@0
   522
	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
sl@0
   523
#endif
sl@0
   524
#ifndef EMULATOR
sl@0
   525
	free_func?free_func(str):free_func_def(str);
sl@0
   526
#else
sl@0
   527
	free_func_openssl(str);
sl@0
   528
#endif	
sl@0
   529
	if (free_debug_func != NULL)
sl@0
   530
		free_debug_func(NULL, 1);
sl@0
   531
	}
sl@0
   532
sl@0
   533
sl@0
   534
EXPORT_C void *CRYPTO_remalloc(void *a, int num, const char *file, int line)
sl@0
   535
	{
sl@0
   536
	if (a != NULL) OPENSSL_free(a);
sl@0
   537
	a=(char *)OPENSSL_malloc(num);
sl@0
   538
	return(a);
sl@0
   539
	}
sl@0
   540
sl@0
   541
EXPORT_C void CRYPTO_set_mem_debug_options(long bits)
sl@0
   542
	{
sl@0
   543
	if (set_debug_options_func != NULL)
sl@0
   544
		set_debug_options_func(bits);
sl@0
   545
	}
sl@0
   546
sl@0
   547
EXPORT_C long CRYPTO_get_mem_debug_options(void)
sl@0
   548
	{
sl@0
   549
	if (get_debug_options_func != NULL)
sl@0
   550
		return get_debug_options_func();
sl@0
   551
	return 0;
sl@0
   552
	}
sl@0
   553
sl@0
   554