/rust/registry/src/index.crates.io-1949cf8c6b5b557f/aws-lc-sys-0.32.2/aws-lc/crypto/mem.c
Line | Count | Source |
1 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | | * All rights reserved. |
3 | | * |
4 | | * This package is an SSL implementation written |
5 | | * by Eric Young (eay@cryptsoft.com). |
6 | | * The implementation was written so as to conform with Netscapes SSL. |
7 | | * |
8 | | * This library is free for commercial and non-commercial use as long as |
9 | | * the following conditions are aheared to. The following conditions |
10 | | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | | * included with this distribution is covered by the same copyright terms |
13 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
14 | | * |
15 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | | * the code are not to be removed. |
17 | | * If this package is used in a product, Eric Young should be given attribution |
18 | | * as the author of the parts of the library used. |
19 | | * This can be in the form of a textual message at program startup or |
20 | | * in documentation (online or textual) provided with the package. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * 1. Redistributions of source code must retain the copyright |
26 | | * notice, this list of conditions and the following disclaimer. |
27 | | * 2. Redistributions in binary form must reproduce the above copyright |
28 | | * notice, this list of conditions and the following disclaimer in the |
29 | | * documentation and/or other materials provided with the distribution. |
30 | | * 3. All advertising materials mentioning features or use of this software |
31 | | * must display the following acknowledgement: |
32 | | * "This product includes cryptographic software written by |
33 | | * Eric Young (eay@cryptsoft.com)" |
34 | | * The word 'cryptographic' can be left out if the rouines from the library |
35 | | * being used are not cryptographic related :-). |
36 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | | * the apps directory (application code) you must include an acknowledgement: |
38 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | | * SUCH DAMAGE. |
51 | | * |
52 | | * The licence and distribution terms for any publically available version or |
53 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | | * copied and put under another distribution licence |
55 | | * [including the GNU Public Licence.] */ |
56 | | |
57 | | #include <openssl/mem.h> |
58 | | |
59 | | #include <assert.h> |
60 | | #include <errno.h> |
61 | | #include <limits.h> |
62 | | #include <stdarg.h> |
63 | | #include <stdio.h> |
64 | | #include <stdlib.h> |
65 | | |
66 | | #include <openssl/err.h> |
67 | | |
68 | | #if defined(OPENSSL_WINDOWS) |
69 | | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
70 | | #include <windows.h> |
71 | | OPENSSL_MSVC_PRAGMA(warning(pop)) |
72 | | #endif |
73 | | |
74 | | #include "internal.h" |
75 | | |
76 | | |
77 | 0 | #define OPENSSL_MALLOC_PREFIX 8 |
78 | | OPENSSL_STATIC_ASSERT(OPENSSL_MALLOC_PREFIX >= sizeof(size_t), |
79 | | size_t_too_large) |
80 | | |
81 | | #if defined(OPENSSL_ASAN) |
82 | | void __asan_poison_memory_region(const volatile void *addr, size_t size); |
83 | | void __asan_unpoison_memory_region(const volatile void *addr, size_t size); |
84 | | #else |
85 | 0 | static void __asan_poison_memory_region(const void *addr, size_t size) {} |
86 | 0 | static void __asan_unpoison_memory_region(const void *addr, size_t size) {} |
87 | | #endif |
88 | | |
89 | 0 | #define AWSLC_FILE "" |
90 | 0 | #define AWSLC_LINE 0 |
91 | | |
92 | | // sdallocx is a sized |free| function. By passing the size (which we happen to |
93 | | // always know in BoringSSL), the malloc implementation can save work. We cannot |
94 | | // depend on |sdallocx| being available, however, so it's a weak symbol. |
95 | | // |
96 | | // This will always be safe, but will only be overridden if the malloc |
97 | | // implementation is statically linked with BoringSSL. So, if |sdallocx| is |
98 | | // provided in, say, libc.so, we still won't use it because that's dynamically |
99 | | // linked. This isn't an ideal result, but its helps in some cases. |
100 | | WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags)) |
101 | | |
102 | | // The following four functions can be defined to override default heap |
103 | | // allocation and freeing. If defined, it is the responsibility of |
104 | | // |OPENSSL_memory_free| to zero out the memory before returning it to the |
105 | | // system. |OPENSSL_memory_free| will not be passed NULL pointers. |
106 | | // |
107 | | // WARNING: These functions are called on every allocation and free in |
108 | | // BoringSSL across the entire process. They may be called by any code in the |
109 | | // process which calls BoringSSL, including in process initializers and thread |
110 | | // destructors. When called, BoringSSL may hold pthreads locks. Any other code |
111 | | // in the process which, directly or indirectly, calls BoringSSL may be on the |
112 | | // call stack and may itself be using arbitrary synchronization primitives. |
113 | | // |
114 | | // As a result, these functions may not have the usual programming environment |
115 | | // available to most C or C++ code. In particular, they may not call into |
116 | | // BoringSSL, or any library which depends on BoringSSL. Any synchronization |
117 | | // primitives used must tolerate every other synchronization primitive linked |
118 | | // into the process, including pthreads locks. Failing to meet these constraints |
119 | | // may result in deadlocks, crashes, or memory corruption. |
120 | | WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size)) |
121 | | WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr)) |
122 | | WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr)) |
123 | | WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_realloc, (void *ptr, size_t size)) |
124 | | |
125 | | // Control function for crypto memory debugging Always returns 0 in AWS-LC |
126 | | // |mode| Unused parameter AWS-LC doesn't support |CRYPTO_MDEBUG| |
127 | | // Always returns 0 |
128 | 0 | int CRYPTO_mem_ctrl(int mode) { |
129 | 0 | #if defined (OPENSSL_NO_CRYPTO_MDEBUG) |
130 | 0 | (void)mode; // Silence unused parameter warning |
131 | 0 | return 0; |
132 | | #else |
133 | | #error "Must compile with OPENSSL_NO_CRYPTO_MDEBUG defined." |
134 | | #endif |
135 | 0 | } |
136 | | |
137 | | // Below can be customized by |CRYPTO_set_mem_functions| only once. |
138 | | static void *(*malloc_impl)(size_t, const char *, int) = NULL; |
139 | | static void *(*realloc_impl)(void *, size_t, const char *, int) = NULL; |
140 | | static void (*free_impl)(void *, const char *, int) = NULL; |
141 | | |
142 | | int CRYPTO_set_mem_functions( |
143 | | void *(*m)(size_t, const char *, int), |
144 | | void *(*r)(void *, size_t, const char *, int), |
145 | 0 | void (*f)(void *, const char *, int)) { |
146 | 0 | if (m == NULL || r == NULL || f == NULL) { |
147 | 0 | return 0; |
148 | 0 | } |
149 | | // |malloc_impl|, |realloc_impl| and |free_impl| can be set only once. |
150 | 0 | if (malloc_impl != NULL || realloc_impl != NULL || free_impl != NULL) { |
151 | 0 | return 0; |
152 | 0 | } |
153 | 0 | if (OPENSSL_memory_alloc != NULL || |
154 | 0 | OPENSSL_memory_free != NULL || |
155 | 0 | OPENSSL_memory_get_size != NULL || |
156 | 0 | OPENSSL_memory_realloc != NULL) { |
157 | | // |OPENSSL_malloc/free/realloc| are customized by overriding the symbols. |
158 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
159 | 0 | return 0; |
160 | 0 | } |
161 | 0 | malloc_impl = m; |
162 | 0 | realloc_impl = r; |
163 | 0 | free_impl = f; |
164 | 0 | return 1; |
165 | 0 | } |
166 | | |
167 | 0 | void *OPENSSL_malloc(size_t size) { |
168 | 0 | if (malloc_impl != NULL) { |
169 | 0 | assert(OPENSSL_memory_alloc == NULL); |
170 | 0 | assert(OPENSSL_memory_realloc == NULL); |
171 | 0 | assert(OPENSSL_memory_free == NULL); |
172 | 0 | assert(OPENSSL_memory_get_size == NULL); |
173 | 0 | assert(realloc_impl != NULL); |
174 | 0 | assert(free_impl != NULL); |
175 | 0 | return malloc_impl(size, AWSLC_FILE, AWSLC_LINE); |
176 | 0 | } |
177 | 0 | if (OPENSSL_memory_alloc != NULL) { |
178 | 0 | assert(OPENSSL_memory_free != NULL); |
179 | 0 | assert(OPENSSL_memory_get_size != NULL); |
180 | 0 | void *ptr = OPENSSL_memory_alloc(size); |
181 | 0 | if (ptr == NULL && size != 0) { |
182 | 0 | goto err; |
183 | 0 | } |
184 | 0 | return ptr; |
185 | 0 | } |
186 | | |
187 | 0 | if (size + OPENSSL_MALLOC_PREFIX < size) { |
188 | 0 | goto err; |
189 | 0 | } |
190 | | |
191 | 0 | void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX); |
192 | 0 | if (ptr == NULL) { |
193 | 0 | goto err; |
194 | 0 | } |
195 | | |
196 | 0 | *(size_t *)ptr = size; |
197 | |
|
198 | 0 | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
199 | 0 | return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX; |
200 | | |
201 | 0 | err: |
202 | | // This only works because ERR does not call OPENSSL_malloc. |
203 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
204 | 0 | return NULL; |
205 | 0 | } |
206 | | |
207 | 0 | void *OPENSSL_zalloc(size_t size) { |
208 | 0 | void *ret = OPENSSL_malloc(size); |
209 | 0 | if (ret != NULL) { |
210 | 0 | OPENSSL_memset(ret, 0, size); |
211 | 0 | } |
212 | 0 | return ret; |
213 | 0 | } |
214 | | |
215 | 0 | void *OPENSSL_calloc(size_t num, size_t size) { |
216 | 0 | if (size != 0 && num > SIZE_MAX / size) { |
217 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
218 | 0 | return NULL; |
219 | 0 | } |
220 | | |
221 | 0 | return OPENSSL_zalloc(num * size); |
222 | 0 | } |
223 | | |
224 | 0 | void OPENSSL_free(void *orig_ptr) { |
225 | 0 | if (orig_ptr == NULL) { |
226 | 0 | return; |
227 | 0 | } |
228 | 0 | if (free_impl != NULL) { |
229 | 0 | assert(OPENSSL_memory_alloc == NULL); |
230 | 0 | assert(OPENSSL_memory_realloc == NULL); |
231 | 0 | assert(OPENSSL_memory_free == NULL); |
232 | 0 | assert(OPENSSL_memory_get_size == NULL); |
233 | 0 | assert(malloc_impl != NULL); |
234 | 0 | assert(realloc_impl != NULL); |
235 | 0 | free_impl(orig_ptr, AWSLC_FILE, AWSLC_LINE); |
236 | 0 | return; |
237 | 0 | } |
238 | | |
239 | 0 | if (OPENSSL_memory_free != NULL) { |
240 | 0 | OPENSSL_memory_free(orig_ptr); |
241 | 0 | return; |
242 | 0 | } |
243 | | |
244 | 0 | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
245 | 0 | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
246 | |
|
247 | 0 | size_t size = *(size_t *)ptr; |
248 | 0 | OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX); |
249 | | |
250 | | // ASan knows to intercept malloc and free, but not sdallocx. |
251 | | #if defined(OPENSSL_ASAN) |
252 | | (void)sdallocx; |
253 | | free(ptr); |
254 | | (void) sdallocx; |
255 | | #else |
256 | 0 | if (sdallocx) { |
257 | 0 | sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */); |
258 | 0 | } else { |
259 | 0 | free(ptr); |
260 | 0 | } |
261 | 0 | #endif |
262 | 0 | } |
263 | | |
264 | 0 | void *OPENSSL_realloc(void *orig_ptr, size_t new_size) { |
265 | 0 | if (orig_ptr == NULL) { |
266 | 0 | return OPENSSL_malloc(new_size); |
267 | 0 | } |
268 | 0 | if (realloc_impl != NULL) { |
269 | 0 | assert(OPENSSL_memory_alloc == NULL); |
270 | 0 | assert(OPENSSL_memory_realloc == NULL); |
271 | 0 | assert(OPENSSL_memory_free == NULL); |
272 | 0 | assert(OPENSSL_memory_get_size == NULL); |
273 | 0 | assert(malloc_impl != NULL); |
274 | 0 | assert(free_impl != NULL); |
275 | 0 | return realloc_impl(orig_ptr, new_size, AWSLC_FILE, AWSLC_LINE); |
276 | 0 | } |
277 | 0 | if (OPENSSL_memory_realloc != NULL) { |
278 | 0 | assert(OPENSSL_memory_alloc != NULL); |
279 | 0 | assert(OPENSSL_memory_free != NULL); |
280 | 0 | assert(OPENSSL_memory_get_size != NULL); |
281 | 0 | return OPENSSL_memory_realloc(orig_ptr, new_size); |
282 | 0 | } |
283 | 0 | size_t old_size; |
284 | 0 | if (OPENSSL_memory_get_size != NULL) { |
285 | 0 | old_size = OPENSSL_memory_get_size(orig_ptr); |
286 | 0 | } else { |
287 | 0 | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
288 | 0 | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
289 | 0 | old_size = *(size_t *)ptr; |
290 | 0 | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
291 | 0 | } |
292 | |
|
293 | 0 | void *ret = OPENSSL_malloc(new_size); |
294 | 0 | if (ret == NULL) { |
295 | 0 | return NULL; |
296 | 0 | } |
297 | | |
298 | 0 | size_t to_copy = new_size; |
299 | 0 | if (old_size < to_copy) { |
300 | 0 | to_copy = old_size; |
301 | 0 | } |
302 | |
|
303 | 0 | memcpy(ret, orig_ptr, to_copy); |
304 | 0 | OPENSSL_free(orig_ptr); |
305 | |
|
306 | 0 | return ret; |
307 | 0 | } |
308 | | |
309 | 0 | void OPENSSL_cleanse(void *ptr, size_t len) { |
310 | |
|
311 | 0 | if (ptr == NULL || len == 0) { |
312 | 0 | return; |
313 | 0 | } |
314 | | |
315 | | #if defined(OPENSSL_WINDOWS) |
316 | | SecureZeroMemory(ptr, len); |
317 | | #else |
318 | 0 | OPENSSL_memset(ptr, 0, len); |
319 | |
|
320 | 0 | #if !defined(OPENSSL_NO_ASM) |
321 | | /* As best as we can tell, this is sufficient to break any optimisations that |
322 | | might try to eliminate "superfluous" memsets. If there's an easy way to |
323 | | detect memset_s, it would be better to use that. */ |
324 | 0 | __asm__ __volatile__("" : : "r"(ptr) : "memory"); |
325 | 0 | #endif |
326 | 0 | #endif // !OPENSSL_NO_ASM |
327 | 0 | } |
328 | | |
329 | 0 | void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); } |
330 | | |
331 | 0 | int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; } |
332 | | |
333 | 0 | int CRYPTO_secure_malloc_initialized(void) { return 0; } |
334 | | |
335 | 0 | size_t CRYPTO_secure_used(void) { return 0; } |
336 | | |
337 | 0 | void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); } |
338 | | |
339 | 0 | void *OPENSSL_secure_zalloc(size_t size) { return OPENSSL_zalloc(size); } |
340 | | |
341 | 0 | void OPENSSL_secure_clear_free(void *ptr, size_t len) { |
342 | 0 | OPENSSL_clear_free(ptr, len); |
343 | 0 | } |
344 | | |
345 | 0 | int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { |
346 | 0 | const uint8_t *a = in_a; |
347 | 0 | const uint8_t *b = in_b; |
348 | 0 | uint8_t x = 0; |
349 | |
|
350 | 0 | for (size_t i = 0; i < len; i++) { |
351 | 0 | x |= a[i] ^ b[i]; |
352 | 0 | } |
353 | |
|
354 | 0 | return x; |
355 | 0 | } |
356 | | |
357 | 0 | uint32_t OPENSSL_hash32(const void *ptr, size_t len) { |
358 | | // These are the FNV-1a parameters for 32 bits. |
359 | 0 | static const uint32_t kPrime = 16777619u; |
360 | 0 | static const uint32_t kOffsetBasis = 2166136261u; |
361 | |
|
362 | 0 | const uint8_t *in = ptr; |
363 | 0 | uint32_t h = kOffsetBasis; |
364 | |
|
365 | 0 | for (size_t i = 0; i < len; i++) { |
366 | 0 | h ^= in[i]; |
367 | 0 | h *= kPrime; |
368 | 0 | } |
369 | |
|
370 | 0 | return h; |
371 | 0 | } |
372 | | |
373 | 0 | uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); } |
374 | | |
375 | 0 | size_t OPENSSL_strnlen(const char *s, size_t len) { |
376 | 0 | for (size_t i = 0; i < len; i++) { |
377 | 0 | if (s[i] == 0) { |
378 | 0 | return i; |
379 | 0 | } |
380 | 0 | } |
381 | | |
382 | 0 | return len; |
383 | 0 | } |
384 | | |
385 | 0 | char *OPENSSL_strdup(const char *s) { |
386 | 0 | if (s == NULL) { |
387 | 0 | return NULL; |
388 | 0 | } |
389 | | // Copy the NUL terminator. |
390 | 0 | return OPENSSL_memdup(s, strlen(s) + 1); |
391 | 0 | } |
392 | | |
393 | 0 | int OPENSSL_isalpha(int c) { |
394 | 0 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
395 | 0 | } |
396 | | |
397 | 0 | int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; } |
398 | | |
399 | 0 | int OPENSSL_isxdigit(int c) { |
400 | 0 | return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
401 | 0 | } |
402 | | |
403 | 0 | int OPENSSL_fromxdigit(uint8_t *out, int c) { |
404 | 0 | if (OPENSSL_isdigit(c)) { |
405 | 0 | *out = c - '0'; |
406 | 0 | return 1; |
407 | 0 | } |
408 | 0 | if ('a' <= c && c <= 'f') { |
409 | 0 | *out = c - 'a' + 10; |
410 | 0 | return 1; |
411 | 0 | } |
412 | 0 | if ('A' <= c && c <= 'F') { |
413 | 0 | *out = c - 'A' + 10; |
414 | 0 | return 1; |
415 | 0 | } |
416 | 0 | return 0; |
417 | 0 | } |
418 | | |
419 | 0 | uint8_t *OPENSSL_hexstr2buf(const char *str, size_t *len) { |
420 | 0 | if (str == NULL || len == NULL) { |
421 | 0 | return NULL; |
422 | 0 | } |
423 | | |
424 | 0 | const size_t slen = OPENSSL_strnlen(str, INT16_MAX); |
425 | 0 | if (slen % 2 != 0) { |
426 | 0 | return NULL; |
427 | 0 | } |
428 | | |
429 | 0 | const size_t buflen = slen / 2; |
430 | 0 | uint8_t *buf = OPENSSL_zalloc(buflen); |
431 | 0 | if (buf == NULL) { |
432 | 0 | return NULL; |
433 | 0 | } |
434 | | |
435 | 0 | for (size_t i = 0; i < buflen; i++) { |
436 | 0 | uint8_t hi, lo; |
437 | 0 | if (!OPENSSL_fromxdigit(&hi, str[2 * i]) || |
438 | 0 | !OPENSSL_fromxdigit(&lo, str[2 * i + 1])) { |
439 | 0 | OPENSSL_free(buf); |
440 | 0 | return NULL; |
441 | 0 | } |
442 | 0 | buf[i] = (hi << 4) | lo; |
443 | 0 | } |
444 | | |
445 | 0 | *len = buflen; |
446 | 0 | return buf; |
447 | 0 | } |
448 | | |
449 | 0 | int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); } |
450 | | |
451 | 0 | int OPENSSL_tolower(int c) { |
452 | 0 | if (c >= 'A' && c <= 'Z') { |
453 | 0 | return c + ('a' - 'A'); |
454 | 0 | } |
455 | 0 | return c; |
456 | 0 | } |
457 | | |
458 | 0 | int OPENSSL_isspace(int c) { |
459 | 0 | return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || |
460 | 0 | c == ' '; |
461 | 0 | } |
462 | | |
463 | 0 | int OPENSSL_strcasecmp(const char *a, const char *b) { |
464 | 0 | for (size_t i = 0;; i++) { |
465 | 0 | const int aa = OPENSSL_tolower(a[i]); |
466 | 0 | const int bb = OPENSSL_tolower(b[i]); |
467 | |
|
468 | 0 | if (aa < bb) { |
469 | 0 | return -1; |
470 | 0 | } else if (aa > bb) { |
471 | 0 | return 1; |
472 | 0 | } else if (aa == 0) { |
473 | 0 | return 0; |
474 | 0 | } |
475 | 0 | } |
476 | 0 | } |
477 | | |
478 | 0 | int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { |
479 | 0 | for (size_t i = 0; i < n; i++) { |
480 | 0 | const int aa = OPENSSL_tolower(a[i]); |
481 | 0 | const int bb = OPENSSL_tolower(b[i]); |
482 | |
|
483 | 0 | if (aa < bb) { |
484 | 0 | return -1; |
485 | 0 | } else if (aa > bb) { |
486 | 0 | return 1; |
487 | 0 | } else if (aa == 0) { |
488 | 0 | return 0; |
489 | 0 | } |
490 | 0 | } |
491 | | |
492 | 0 | return 0; |
493 | 0 | } |
494 | | |
495 | 0 | int BIO_snprintf(char *buf, size_t n, const char *format, ...) { |
496 | 0 | va_list args; |
497 | 0 | va_start(args, format); |
498 | 0 | int ret = BIO_vsnprintf(buf, n, format, args); |
499 | 0 | va_end(args); |
500 | 0 | return ret; |
501 | 0 | } |
502 | | |
503 | 0 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { |
504 | 0 | return vsnprintf(buf, n, format, args); |
505 | 0 | } |
506 | | |
507 | | int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, |
508 | 0 | int system_malloc) { |
509 | 0 | void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc; |
510 | 0 | void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free; |
511 | 0 | void *(*reallocate)(void *, size_t) = |
512 | 0 | system_malloc ? realloc : OPENSSL_realloc; |
513 | 0 | char *candidate = NULL; |
514 | 0 | size_t candidate_len = 64; // TODO(bbe) what's the best initial size? |
515 | |
|
516 | 0 | if ((candidate = allocate(candidate_len)) == NULL) { |
517 | 0 | goto err; |
518 | 0 | } |
519 | 0 | va_list args_copy; |
520 | 0 | va_copy(args_copy, args); |
521 | 0 | int ret = vsnprintf(candidate, candidate_len, format, args_copy); |
522 | 0 | va_end(args_copy); |
523 | 0 | if (ret < 0) { |
524 | 0 | goto err; |
525 | 0 | } |
526 | 0 | if ((size_t)ret >= candidate_len) { |
527 | | // Too big to fit in allocation. |
528 | 0 | char *tmp; |
529 | |
|
530 | 0 | candidate_len = (size_t)ret + 1; |
531 | 0 | if ((tmp = reallocate(candidate, candidate_len)) == NULL) { |
532 | 0 | goto err; |
533 | 0 | } |
534 | 0 | candidate = tmp; |
535 | 0 | ret = vsnprintf(candidate, candidate_len, format, args); |
536 | 0 | } |
537 | | // At this point this should not happen unless vsnprintf is insane. |
538 | 0 | if (ret < 0 || (size_t)ret >= candidate_len) { |
539 | 0 | goto err; |
540 | 0 | } |
541 | 0 | *str = candidate; |
542 | 0 | return ret; |
543 | | |
544 | 0 | err: |
545 | 0 | deallocate(candidate); |
546 | 0 | *str = NULL; |
547 | 0 | errno = ENOMEM; |
548 | 0 | return -1; |
549 | 0 | } |
550 | | |
551 | 0 | int OPENSSL_vasprintf(char **str, const char *format, va_list args) { |
552 | 0 | return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0); |
553 | 0 | } |
554 | | |
555 | 0 | int OPENSSL_asprintf(char **str, const char *format, ...) { |
556 | 0 | va_list args; |
557 | 0 | va_start(args, format); |
558 | 0 | int ret = OPENSSL_vasprintf(str, format, args); |
559 | 0 | va_end(args); |
560 | 0 | return ret; |
561 | 0 | } |
562 | | |
563 | 0 | char *OPENSSL_strndup(const char *str, size_t size) { |
564 | 0 | size = OPENSSL_strnlen(str, size); |
565 | |
|
566 | 0 | size_t alloc_size = size + 1; |
567 | 0 | if (alloc_size < size) { |
568 | | // overflow |
569 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
570 | 0 | return NULL; |
571 | 0 | } |
572 | 0 | char *ret = OPENSSL_malloc(alloc_size); |
573 | 0 | if (ret == NULL) { |
574 | 0 | return NULL; |
575 | 0 | } |
576 | | |
577 | 0 | OPENSSL_memcpy(ret, str, size); |
578 | 0 | ret[size] = '\0'; |
579 | 0 | return ret; |
580 | 0 | } |
581 | | |
582 | 0 | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) { |
583 | 0 | size_t l = 0; |
584 | |
|
585 | 0 | for (; dst_size > 1 && *src; dst_size--) { |
586 | 0 | *dst++ = *src++; |
587 | 0 | l++; |
588 | 0 | } |
589 | |
|
590 | 0 | if (dst_size) { |
591 | 0 | *dst = 0; |
592 | 0 | } |
593 | |
|
594 | 0 | return l + strlen(src); |
595 | 0 | } |
596 | | |
597 | 0 | size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) { |
598 | 0 | size_t l = 0; |
599 | 0 | for (; dst_size > 0 && *dst; dst_size--, dst++) { |
600 | 0 | l++; |
601 | 0 | } |
602 | 0 | return l + OPENSSL_strlcpy(dst, src, dst_size); |
603 | 0 | } |
604 | | |
605 | 0 | void *OPENSSL_memdup(const void *data, size_t size) { |
606 | 0 | if (size == 0) { |
607 | 0 | return NULL; |
608 | 0 | } |
609 | | |
610 | 0 | void *ret = OPENSSL_malloc(size); |
611 | 0 | if (ret == NULL) { |
612 | 0 | return NULL; |
613 | 0 | } |
614 | | |
615 | 0 | OPENSSL_memcpy(ret, data, size); |
616 | 0 | return ret; |
617 | 0 | } |
618 | | |
619 | 0 | void *CRYPTO_malloc(size_t size, const char *file, int line) { |
620 | 0 | return OPENSSL_malloc(size); |
621 | 0 | } |
622 | | |
623 | 0 | void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) { |
624 | 0 | return OPENSSL_realloc(ptr, new_size); |
625 | 0 | } |
626 | | |
627 | 0 | void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); } |