- fix Building without Nagra not possible at Nagra_Merlin https://trac.streamboard...
[oscam.git] / cscrypt / mem.c
blobae99081f1fbf9723eec96a628d2954715a37eee6
1 #include "bn.h"
3 #ifndef WITH_LIBCRYPTO
4 //FIXME Not checked on threadsafety yet; after checking please remove this line
5 /* crypto/mem.c */
6 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
7 * All rights reserved.
9 * This package is an SSL implementation written
10 * by Eric Young (eay@cryptsoft.com).
11 * The implementation was written so as to conform with Netscapes SSL.
13 * This library is free for commercial and non-commercial use as long as
14 * the following conditions are aheared to. The following conditions
15 * apply to all code found in this distribution, be it the RC4, RSA,
16 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
17 * included with this distribution is covered by the same copyright terms
18 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
20 * Copyright remains Eric Young's, and as such any Copyright notices in
21 * the code are not to be removed.
22 * If this package is used in a product, Eric Young should be given attribution
23 * as the author of the parts of the library used.
24 * This can be in the form of a textual message at program startup or
25 * in documentation (online or textual) provided with the package.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * "This product includes cryptographic software written by
38 * Eric Young (eay@cryptsoft.com)"
39 * The word 'cryptographic' can be left out if the rouines from the library
40 * being used are not cryptographic related :-).
41 * 4. If you include any Windows specific code (or a derivative thereof) from
42 * the apps directory (application code) you must include an acknowledgement:
43 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
45 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
57 * The license and distribution terms for any publically available version or
58 * derivative of this code cannot be changed. i.e. this code cannot simply be
59 * copied and put under another distribution license
60 * [including the GNU Public License.]
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include "openssl_mods.h"
68 static int allow_customize = 1; /* we provide flexible functions for */
69 static int allow_customize_debug = 1;/* exchanging memory-related functions at
70 * run-time, but this must be done
71 * before any blocks are actually
72 * allocated; or we'll run into huge
73 * problems when malloc/free pairs
74 * don't match etc. */
76 /* may be changed as long as `allow_customize' is set */
77 static void *(*malloc_locked_func)(size_t) = malloc;
78 static void (*free_locked_func)(void *) = free;
79 static void *(*malloc_func)(size_t) = malloc;
80 static void *(*realloc_func)(void *, size_t) = realloc;
81 static void (*free_func)(void *) = free;
83 /* may be changed as long as `allow_customize_debug' is set */
84 /* XXX use correct function pointer types */
85 #ifdef CRYPTO_MDEBUG
86 /* use default functions from mem_dbg.c */
87 static void (*malloc_debug_func)(void *, int, const char *, int, int)
88 = CRYPTO_dbg_malloc;
89 static void (*realloc_debug_func)(void *, void *, int, const char *, int, int)
90 = CRYPTO_dbg_realloc;
91 static void (*free_debug_func)(void *, int) = CRYPTO_dbg_free;
92 static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
93 static long(*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
94 #else
95 /* applications can use CRYPTO_malloc_debug_init() to select above case
96 * at run-time */
97 static void (*malloc_debug_func)(void *, int, const char *, int, int) = NULL;
98 static void (*realloc_debug_func)(void *, void *, int, const char *, int, int)
99 = NULL;
100 static void (*free_debug_func)(void *, int) = NULL;
101 static void (*set_debug_options_func)(long) = NULL;
102 static long(*get_debug_options_func)(void) = NULL;
103 #endif
106 int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
107 void (*f)(void *))
109 if(!allow_customize)
110 { return 0; }
111 if((m == NULL) || (r == NULL) || (f == NULL))
112 { return 0; }
113 malloc_func = m;
114 realloc_func = r;
115 free_func = f;
116 malloc_locked_func = m;
117 free_locked_func = f;
118 return 1;
121 int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
123 if(!allow_customize)
124 { return 0; }
125 if((m == NULL) || (f == NULL))
126 { return 0; }
127 malloc_locked_func = m;
128 free_locked_func = f;
129 return 1;
132 int CRYPTO_set_mem_debug_functions(void (*m)(void *, int, const char *, int, int),
133 void (*r)(void *, void *, int, const char *, int, int),
134 void (*f)(void *, int),
135 void (*so)(long),
136 long(*go)(void))
138 if(!allow_customize_debug)
139 { return 0; }
140 malloc_debug_func = m;
141 realloc_debug_func = r;
142 free_debug_func = f;
143 set_debug_options_func = so;
144 get_debug_options_func = go;
145 return 1;
148 void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
149 void (**f)(void *))
151 if(m != NULL) { *m = malloc_func; }
152 if(r != NULL) { *r = realloc_func; }
153 if(f != NULL) { *f = free_func; }
156 void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
158 if(m != NULL) { *m = malloc_locked_func; }
159 if(f != NULL) { *f = free_locked_func; }
162 void CRYPTO_get_mem_debug_functions(void (**m)(void *, int, const char *, int, int),
163 void (**r)(void *, void *, int, const char *, int, int),
164 void (**f)(void *, int),
165 void (**so)(long),
166 long(**go)(void))
168 if(m != NULL) { *m = malloc_debug_func; }
169 if(r != NULL) { *r = realloc_debug_func; }
170 if(f != NULL) { *f = free_debug_func; }
171 if(so != NULL) { *so = set_debug_options_func; }
172 if(go != NULL) { *go = get_debug_options_func; }
176 void *CRYPTO_malloc_locked(int num, const char *file, int line)
178 void *ret = NULL;
180 allow_customize = 0;
181 if(malloc_debug_func != NULL)
183 allow_customize_debug = 0;
184 malloc_debug_func(NULL, num, file, line, 0);
186 ret = malloc_locked_func(num);
187 #ifdef LEVITTE_DEBUG
188 fprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\n", ret, num);
189 #endif
190 if(malloc_debug_func != NULL)
191 { malloc_debug_func(ret, num, file, line, 1); }
193 return ret;
196 void CRYPTO_free_locked(void *str)
198 if(free_debug_func != NULL)
199 { free_debug_func(str, 0); }
200 #ifdef LEVITTE_DEBUG
201 fprintf(stderr, "LEVITTE_DEBUG: < 0x%p\n", str);
202 #endif
203 free_locked_func(str);
204 if(free_debug_func != NULL)
205 { free_debug_func(NULL, 1); }
208 void *CRYPTO_malloc(int num, const char *file, int line)
210 void *ret = NULL;
212 allow_customize = 0;
213 if(malloc_debug_func != NULL)
215 allow_customize_debug = 0;
216 malloc_debug_func(NULL, num, file, line, 0);
218 ret = malloc_func(num);
219 #ifdef LEVITTE_DEBUG
220 fprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\n", ret, num);
221 #endif
222 if(malloc_debug_func != NULL)
223 { malloc_debug_func(ret, num, file, line, 1); }
225 return ret;
228 void *CRYPTO_realloc(void *str, int num, const char *file, int line)
230 void *ret = NULL;
232 if(realloc_debug_func != NULL)
233 { realloc_debug_func(str, NULL, num, file, line, 0); }
234 ret = realloc_func(str, num);
235 #ifdef LEVITTE_DEBUG
236 fprintf(stderr, "LEVITTE_DEBUG: | 0x%p -> 0x%p (%d)\n", str, ret, num);
237 #endif
238 if(realloc_debug_func != NULL)
239 { realloc_debug_func(str, ret, num, file, line, 1); }
241 return ret;
244 void CRYPTO_free(void *str)
246 if(free_debug_func != NULL)
247 { free_debug_func(str, 0); }
248 #ifdef LEVITTE_DEBUG
249 fprintf(stderr, "LEVITTE_DEBUG: < 0x%p\n", str);
250 #endif
251 free_func(str);
252 if(free_debug_func != NULL)
253 { free_debug_func(NULL, 1); }
256 void *CRYPTO_remalloc(void *a, int num)
258 if(a != NULL) { OPENSSL_free(a); }
259 a = (char *)OPENSSL_malloc(num);
260 return (a);
264 void CRYPTO_set_mem_debug_options(long bits)
266 if(set_debug_options_func != NULL)
267 { set_debug_options_func(bits); }
270 long CRYPTO_get_mem_debug_options(void)
272 if(get_debug_options_func != NULL)
273 { return get_debug_options_func(); }
274 return 0;
276 #endif