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