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