/rust/registry/src/index.crates.io-1949cf8c6b5b557f/aws-lc-sys-0.26.0/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 | | // Windows doesn't really support weak symbols as of May 2019, and Clang on |
90 | | // Windows will emit strong symbols instead. See |
91 | | // https://bugs.llvm.org/show_bug.cgi?id=37598 |
92 | | #if defined(__ELF__) && defined(__GNUC__) |
93 | | #define WEAK_SYMBOL_FUNC(rettype, name, args) \ |
94 | | rettype name args __attribute__((weak)); |
95 | | #else |
96 | | #define WEAK_SYMBOL_FUNC(rettype, name, args) static rettype(*name) args = NULL; |
97 | | #endif |
98 | | |
99 | 0 | #define AWSLC_FILE "" |
100 | 0 | #define AWSLC_LINE 0 |
101 | | |
102 | | // sdallocx is a sized |free| function. By passing the size (which we happen to |
103 | | // always know in BoringSSL), the malloc implementation can save work. We cannot |
104 | | // depend on |sdallocx| being available, however, so it's a weak symbol. |
105 | | // |
106 | | // This will always be safe, but will only be overridden if the malloc |
107 | | // implementation is statically linked with BoringSSL. So, if |sdallocx| is |
108 | | // provided in, say, libc.so, we still won't use it because that's dynamically |
109 | | // linked. This isn't an ideal result, but its helps in some cases. |
110 | | WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags)) |
111 | | |
112 | | // The following four functions can be defined to override default heap |
113 | | // allocation and freeing. If defined, it is the responsibility of |
114 | | // |OPENSSL_memory_free| to zero out the memory before returning it to the |
115 | | // system. |OPENSSL_memory_free| will not be passed NULL pointers. |
116 | | // |
117 | | // WARNING: These functions are called on every allocation and free in |
118 | | // BoringSSL across the entire process. They may be called by any code in the |
119 | | // process which calls BoringSSL, including in process initializers and thread |
120 | | // destructors. When called, BoringSSL may hold pthreads locks. Any other code |
121 | | // in the process which, directly or indirectly, calls BoringSSL may be on the |
122 | | // call stack and may itself be using arbitrary synchronization primitives. |
123 | | // |
124 | | // As a result, these functions may not have the usual programming environment |
125 | | // available to most C or C++ code. In particular, they may not call into |
126 | | // BoringSSL, or any library which depends on BoringSSL. Any synchronization |
127 | | // primitives used must tolerate every other synchronization primitive linked |
128 | | // into the process, including pthreads locks. Failing to meet these constraints |
129 | | // may result in deadlocks, crashes, or memory corruption. |
130 | | WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size)) |
131 | | WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr)) |
132 | | WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr)) |
133 | | WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_realloc, (void *ptr, size_t size)) |
134 | | |
135 | | // Below can be customized by |CRYPTO_set_mem_functions| only once. |
136 | | static void *(*malloc_impl)(size_t, const char *, int) = NULL; |
137 | | static void *(*realloc_impl)(void *, size_t, const char *, int) = NULL; |
138 | | static void (*free_impl)(void *, const char *, int) = NULL; |
139 | | |
140 | | int CRYPTO_set_mem_functions( |
141 | | void *(*m)(size_t, const char *, int), |
142 | | void *(*r)(void *, size_t, const char *, int), |
143 | 0 | void (*f)(void *, const char *, int)) { |
144 | 0 | if (m == NULL || r == NULL || f == NULL) { |
145 | 0 | return 0; |
146 | 0 | } |
147 | | // |malloc_impl|, |realloc_impl| and |free_impl| can be set only once. |
148 | 0 | if (malloc_impl != NULL || realloc_impl != NULL || free_impl != NULL) { |
149 | 0 | return 0; |
150 | 0 | } |
151 | 0 | if (OPENSSL_memory_alloc != NULL || |
152 | 0 | OPENSSL_memory_free != NULL || |
153 | 0 | OPENSSL_memory_get_size != NULL || |
154 | 0 | OPENSSL_memory_realloc != NULL) { |
155 | | // |OPENSSL_malloc/free/realloc| are customized by overriding the symbols. |
156 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
157 | 0 | return 0; |
158 | 0 | } |
159 | 0 | malloc_impl = m; |
160 | 0 | realloc_impl = r; |
161 | 0 | free_impl = f; |
162 | 0 | return 1; |
163 | 0 | } |
164 | | |
165 | 0 | void *OPENSSL_malloc(size_t size) { |
166 | 0 | if (malloc_impl != NULL) { |
167 | 0 | assert(OPENSSL_memory_alloc == NULL); |
168 | 0 | assert(OPENSSL_memory_realloc == NULL); |
169 | 0 | assert(OPENSSL_memory_free == NULL); |
170 | 0 | assert(OPENSSL_memory_get_size == NULL); |
171 | 0 | assert(realloc_impl != NULL); |
172 | 0 | assert(free_impl != NULL); |
173 | 0 | return malloc_impl(size, AWSLC_FILE, AWSLC_LINE); |
174 | 0 | } |
175 | 0 | if (OPENSSL_memory_alloc != NULL) { |
176 | 0 | assert(OPENSSL_memory_free != NULL); |
177 | 0 | assert(OPENSSL_memory_get_size != NULL); |
178 | 0 | void *ptr = OPENSSL_memory_alloc(size); |
179 | 0 | if (ptr == NULL && size != 0) { |
180 | 0 | goto err; |
181 | 0 | } |
182 | 0 | return ptr; |
183 | 0 | } |
184 | | |
185 | 0 | if (size + OPENSSL_MALLOC_PREFIX < size) { |
186 | 0 | goto err; |
187 | 0 | } |
188 | | |
189 | 0 | void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX); |
190 | 0 | if (ptr == NULL) { |
191 | 0 | goto err; |
192 | 0 | } |
193 | | |
194 | 0 | *(size_t *)ptr = size; |
195 | |
|
196 | 0 | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
197 | 0 | return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX; |
198 | | |
199 | 0 | err: |
200 | | // This only works because ERR does not call OPENSSL_malloc. |
201 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
202 | 0 | return NULL; |
203 | 0 | } |
204 | | |
205 | 0 | void *OPENSSL_zalloc(size_t size) { |
206 | 0 | void *ret = OPENSSL_malloc(size); |
207 | 0 | if (ret != NULL) { |
208 | 0 | OPENSSL_memset(ret, 0, size); |
209 | 0 | } |
210 | 0 | return ret; |
211 | 0 | } |
212 | | |
213 | 0 | void *OPENSSL_calloc(size_t num, size_t size) { |
214 | 0 | if (size != 0 && num > SIZE_MAX / size) { |
215 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
216 | 0 | return NULL; |
217 | 0 | } |
218 | | |
219 | 0 | return OPENSSL_zalloc(num * size); |
220 | 0 | } |
221 | | |
222 | 0 | void OPENSSL_free(void *orig_ptr) { |
223 | 0 | if (orig_ptr == NULL) { |
224 | 0 | return; |
225 | 0 | } |
226 | 0 | if (free_impl != NULL) { |
227 | 0 | assert(OPENSSL_memory_alloc == NULL); |
228 | 0 | assert(OPENSSL_memory_realloc == NULL); |
229 | 0 | assert(OPENSSL_memory_free == NULL); |
230 | 0 | assert(OPENSSL_memory_get_size == NULL); |
231 | 0 | assert(malloc_impl != NULL); |
232 | 0 | assert(realloc_impl != NULL); |
233 | 0 | free_impl(orig_ptr, AWSLC_FILE, AWSLC_LINE); |
234 | 0 | return; |
235 | 0 | } |
236 | | |
237 | 0 | if (OPENSSL_memory_free != NULL) { |
238 | 0 | OPENSSL_memory_free(orig_ptr); |
239 | 0 | return; |
240 | 0 | } |
241 | | |
242 | 0 | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
243 | 0 | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
244 | |
|
245 | 0 | size_t size = *(size_t *)ptr; |
246 | 0 | OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX); |
247 | | |
248 | | // ASan knows to intercept malloc and free, but not sdallocx. |
249 | | #if defined(OPENSSL_ASAN) |
250 | | (void)sdallocx; |
251 | | free(ptr); |
252 | | (void) sdallocx; |
253 | | #else |
254 | 0 | if (sdallocx) { |
255 | 0 | sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */); |
256 | 0 | } else { |
257 | 0 | free(ptr); |
258 | 0 | } |
259 | 0 | #endif |
260 | 0 | } |
261 | | |
262 | 0 | void *OPENSSL_realloc(void *orig_ptr, size_t new_size) { |
263 | 0 | if (orig_ptr == NULL) { |
264 | 0 | return OPENSSL_malloc(new_size); |
265 | 0 | } |
266 | 0 | if (realloc_impl != NULL) { |
267 | 0 | assert(OPENSSL_memory_alloc == NULL); |
268 | 0 | assert(OPENSSL_memory_realloc == NULL); |
269 | 0 | assert(OPENSSL_memory_free == NULL); |
270 | 0 | assert(OPENSSL_memory_get_size == NULL); |
271 | 0 | assert(malloc_impl != NULL); |
272 | 0 | assert(free_impl != NULL); |
273 | 0 | return realloc_impl(orig_ptr, new_size, AWSLC_FILE, AWSLC_LINE); |
274 | 0 | } |
275 | 0 | if (OPENSSL_memory_realloc != NULL) { |
276 | 0 | assert(OPENSSL_memory_alloc != NULL); |
277 | 0 | assert(OPENSSL_memory_free != NULL); |
278 | 0 | assert(OPENSSL_memory_get_size != NULL); |
279 | 0 | return OPENSSL_memory_realloc(orig_ptr, new_size); |
280 | 0 | } |
281 | 0 | size_t old_size; |
282 | 0 | if (OPENSSL_memory_get_size != NULL) { |
283 | 0 | old_size = OPENSSL_memory_get_size(orig_ptr); |
284 | 0 | } else { |
285 | 0 | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
286 | 0 | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
287 | 0 | old_size = *(size_t *)ptr; |
288 | 0 | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
289 | 0 | } |
290 | |
|
291 | 0 | void *ret = OPENSSL_malloc(new_size); |
292 | 0 | if (ret == NULL) { |
293 | 0 | return NULL; |
294 | 0 | } |
295 | | |
296 | 0 | size_t to_copy = new_size; |
297 | 0 | if (old_size < to_copy) { |
298 | 0 | to_copy = old_size; |
299 | 0 | } |
300 | |
|
301 | 0 | memcpy(ret, orig_ptr, to_copy); |
302 | 0 | OPENSSL_free(orig_ptr); |
303 | |
|
304 | 0 | return ret; |
305 | 0 | } |
306 | | |
307 | 0 | void OPENSSL_cleanse(void *ptr, size_t len) { |
308 | | #if defined(OPENSSL_WINDOWS) |
309 | | SecureZeroMemory(ptr, len); |
310 | | #else |
311 | 0 | OPENSSL_memset(ptr, 0, len); |
312 | |
|
313 | 0 | #if !defined(OPENSSL_NO_ASM) |
314 | | /* As best as we can tell, this is sufficient to break any optimisations that |
315 | | might try to eliminate "superfluous" memsets. If there's an easy way to |
316 | | detect memset_s, it would be better to use that. */ |
317 | 0 | __asm__ __volatile__("" : : "r"(ptr) : "memory"); |
318 | 0 | #endif |
319 | 0 | #endif // !OPENSSL_NO_ASM |
320 | 0 | } |
321 | | |
322 | 0 | void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); } |
323 | | |
324 | 0 | int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; } |
325 | | |
326 | 0 | int CRYPTO_secure_malloc_initialized(void) { return 0; } |
327 | | |
328 | 0 | size_t CRYPTO_secure_used(void) { return 0; } |
329 | | |
330 | 0 | void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); } |
331 | | |
332 | 0 | void *OPENSSL_secure_zalloc(size_t size) { return OPENSSL_zalloc(size); } |
333 | | |
334 | 0 | void OPENSSL_secure_clear_free(void *ptr, size_t len) { |
335 | 0 | OPENSSL_clear_free(ptr, len); |
336 | 0 | } |
337 | | |
338 | 0 | int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { |
339 | 0 | const uint8_t *a = in_a; |
340 | 0 | const uint8_t *b = in_b; |
341 | 0 | uint8_t x = 0; |
342 | |
|
343 | 0 | for (size_t i = 0; i < len; i++) { |
344 | 0 | x |= a[i] ^ b[i]; |
345 | 0 | } |
346 | |
|
347 | 0 | return x; |
348 | 0 | } |
349 | | |
350 | 0 | uint32_t OPENSSL_hash32(const void *ptr, size_t len) { |
351 | | // These are the FNV-1a parameters for 32 bits. |
352 | 0 | static const uint32_t kPrime = 16777619u; |
353 | 0 | static const uint32_t kOffsetBasis = 2166136261u; |
354 | |
|
355 | 0 | const uint8_t *in = ptr; |
356 | 0 | uint32_t h = kOffsetBasis; |
357 | |
|
358 | 0 | for (size_t i = 0; i < len; i++) { |
359 | 0 | h ^= in[i]; |
360 | 0 | h *= kPrime; |
361 | 0 | } |
362 | |
|
363 | 0 | return h; |
364 | 0 | } |
365 | | |
366 | 0 | uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); } |
367 | | |
368 | 0 | size_t OPENSSL_strnlen(const char *s, size_t len) { |
369 | 0 | for (size_t i = 0; i < len; i++) { |
370 | 0 | if (s[i] == 0) { |
371 | 0 | return i; |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | return len; |
376 | 0 | } |
377 | | |
378 | 0 | char *OPENSSL_strdup(const char *s) { |
379 | 0 | if (s == NULL) { |
380 | 0 | return NULL; |
381 | 0 | } |
382 | | // Copy the NUL terminator. |
383 | 0 | return OPENSSL_memdup(s, strlen(s) + 1); |
384 | 0 | } |
385 | | |
386 | 0 | int OPENSSL_isalpha(int c) { |
387 | 0 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
388 | 0 | } |
389 | | |
390 | 0 | int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; } |
391 | | |
392 | 0 | int OPENSSL_isxdigit(int c) { |
393 | 0 | return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
394 | 0 | } |
395 | | |
396 | 0 | int OPENSSL_fromxdigit(uint8_t *out, int c) { |
397 | 0 | if (OPENSSL_isdigit(c)) { |
398 | 0 | *out = c - '0'; |
399 | 0 | return 1; |
400 | 0 | } |
401 | 0 | if ('a' <= c && c <= 'f') { |
402 | 0 | *out = c - 'a' + 10; |
403 | 0 | return 1; |
404 | 0 | } |
405 | 0 | if ('A' <= c && c <= 'F') { |
406 | 0 | *out = c - 'A' + 10; |
407 | 0 | return 1; |
408 | 0 | } |
409 | 0 | return 0; |
410 | 0 | } |
411 | | |
412 | 0 | uint8_t *OPENSSL_hexstr2buf(const char *str, size_t *len) { |
413 | 0 | if (str == NULL || len == NULL) { |
414 | 0 | return NULL; |
415 | 0 | } |
416 | | |
417 | 0 | const size_t slen = OPENSSL_strnlen(str, INT16_MAX); |
418 | 0 | if (slen % 2 != 0) { |
419 | 0 | return NULL; |
420 | 0 | } |
421 | | |
422 | 0 | const size_t buflen = slen / 2; |
423 | 0 | uint8_t *buf = OPENSSL_zalloc(buflen); |
424 | 0 | if (buf == NULL) { |
425 | 0 | return NULL; |
426 | 0 | } |
427 | | |
428 | 0 | for (size_t i = 0; i < buflen; i++) { |
429 | 0 | uint8_t hi, lo; |
430 | 0 | if (!OPENSSL_fromxdigit(&hi, str[2 * i]) || |
431 | 0 | !OPENSSL_fromxdigit(&lo, str[2 * i + 1])) { |
432 | 0 | OPENSSL_free(buf); |
433 | 0 | return NULL; |
434 | 0 | } |
435 | 0 | buf[i] = (hi << 4) | lo; |
436 | 0 | } |
437 | | |
438 | 0 | *len = buflen; |
439 | 0 | return buf; |
440 | 0 | } |
441 | | |
442 | 0 | int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); } |
443 | | |
444 | 0 | int OPENSSL_tolower(int c) { |
445 | 0 | if (c >= 'A' && c <= 'Z') { |
446 | 0 | return c + ('a' - 'A'); |
447 | 0 | } |
448 | 0 | return c; |
449 | 0 | } |
450 | | |
451 | 0 | int OPENSSL_isspace(int c) { |
452 | 0 | return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || |
453 | 0 | c == ' '; |
454 | 0 | } |
455 | | |
456 | 0 | int OPENSSL_strcasecmp(const char *a, const char *b) { |
457 | 0 | for (size_t i = 0;; i++) { |
458 | 0 | const int aa = OPENSSL_tolower(a[i]); |
459 | 0 | const int bb = OPENSSL_tolower(b[i]); |
460 | |
|
461 | 0 | if (aa < bb) { |
462 | 0 | return -1; |
463 | 0 | } else if (aa > bb) { |
464 | 0 | return 1; |
465 | 0 | } else if (aa == 0) { |
466 | 0 | return 0; |
467 | 0 | } |
468 | 0 | } |
469 | 0 | } |
470 | | |
471 | 0 | int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { |
472 | 0 | for (size_t i = 0; i < n; i++) { |
473 | 0 | const int aa = OPENSSL_tolower(a[i]); |
474 | 0 | const int bb = OPENSSL_tolower(b[i]); |
475 | |
|
476 | 0 | if (aa < bb) { |
477 | 0 | return -1; |
478 | 0 | } else if (aa > bb) { |
479 | 0 | return 1; |
480 | 0 | } else if (aa == 0) { |
481 | 0 | return 0; |
482 | 0 | } |
483 | 0 | } |
484 | | |
485 | 0 | return 0; |
486 | 0 | } |
487 | | |
488 | 0 | int BIO_snprintf(char *buf, size_t n, const char *format, ...) { |
489 | 0 | va_list args; |
490 | 0 | va_start(args, format); |
491 | 0 | int ret = BIO_vsnprintf(buf, n, format, args); |
492 | 0 | va_end(args); |
493 | 0 | return ret; |
494 | 0 | } |
495 | | |
496 | 0 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { |
497 | 0 | return vsnprintf(buf, n, format, args); |
498 | 0 | } |
499 | | |
500 | | int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, |
501 | 0 | int system_malloc) { |
502 | 0 | void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc; |
503 | 0 | void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free; |
504 | 0 | void *(*reallocate)(void *, size_t) = |
505 | 0 | system_malloc ? realloc : OPENSSL_realloc; |
506 | 0 | char *candidate = NULL; |
507 | 0 | size_t candidate_len = 64; // TODO(bbe) what's the best initial size? |
508 | |
|
509 | 0 | if ((candidate = allocate(candidate_len)) == NULL) { |
510 | 0 | goto err; |
511 | 0 | } |
512 | 0 | va_list args_copy; |
513 | 0 | va_copy(args_copy, args); |
514 | 0 | int ret = vsnprintf(candidate, candidate_len, format, args_copy); |
515 | 0 | va_end(args_copy); |
516 | 0 | if (ret < 0) { |
517 | 0 | goto err; |
518 | 0 | } |
519 | 0 | if ((size_t)ret >= candidate_len) { |
520 | | // Too big to fit in allocation. |
521 | 0 | char *tmp; |
522 | |
|
523 | 0 | candidate_len = (size_t)ret + 1; |
524 | 0 | if ((tmp = reallocate(candidate, candidate_len)) == NULL) { |
525 | 0 | goto err; |
526 | 0 | } |
527 | 0 | candidate = tmp; |
528 | 0 | ret = vsnprintf(candidate, candidate_len, format, args); |
529 | 0 | } |
530 | | // At this point this should not happen unless vsnprintf is insane. |
531 | 0 | if (ret < 0 || (size_t)ret >= candidate_len) { |
532 | 0 | goto err; |
533 | 0 | } |
534 | 0 | *str = candidate; |
535 | 0 | return ret; |
536 | | |
537 | 0 | err: |
538 | 0 | deallocate(candidate); |
539 | 0 | *str = NULL; |
540 | 0 | errno = ENOMEM; |
541 | 0 | return -1; |
542 | 0 | } |
543 | | |
544 | 0 | int OPENSSL_vasprintf(char **str, const char *format, va_list args) { |
545 | 0 | return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0); |
546 | 0 | } |
547 | | |
548 | 0 | int OPENSSL_asprintf(char **str, const char *format, ...) { |
549 | 0 | va_list args; |
550 | 0 | va_start(args, format); |
551 | 0 | int ret = OPENSSL_vasprintf(str, format, args); |
552 | 0 | va_end(args); |
553 | 0 | return ret; |
554 | 0 | } |
555 | | |
556 | 0 | char *OPENSSL_strndup(const char *str, size_t size) { |
557 | 0 | size = OPENSSL_strnlen(str, size); |
558 | |
|
559 | 0 | size_t alloc_size = size + 1; |
560 | 0 | if (alloc_size < size) { |
561 | | // overflow |
562 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
563 | 0 | return NULL; |
564 | 0 | } |
565 | 0 | char *ret = OPENSSL_malloc(alloc_size); |
566 | 0 | if (ret == NULL) { |
567 | 0 | return NULL; |
568 | 0 | } |
569 | | |
570 | 0 | OPENSSL_memcpy(ret, str, size); |
571 | 0 | ret[size] = '\0'; |
572 | 0 | return ret; |
573 | 0 | } |
574 | | |
575 | 0 | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) { |
576 | 0 | size_t l = 0; |
577 | |
|
578 | 0 | for (; dst_size > 1 && *src; dst_size--) { |
579 | 0 | *dst++ = *src++; |
580 | 0 | l++; |
581 | 0 | } |
582 | |
|
583 | 0 | if (dst_size) { |
584 | 0 | *dst = 0; |
585 | 0 | } |
586 | |
|
587 | 0 | return l + strlen(src); |
588 | 0 | } |
589 | | |
590 | 0 | size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) { |
591 | 0 | size_t l = 0; |
592 | 0 | for (; dst_size > 0 && *dst; dst_size--, dst++) { |
593 | 0 | l++; |
594 | 0 | } |
595 | 0 | return l + OPENSSL_strlcpy(dst, src, dst_size); |
596 | 0 | } |
597 | | |
598 | 0 | void *OPENSSL_memdup(const void *data, size_t size) { |
599 | 0 | if (size == 0) { |
600 | 0 | return NULL; |
601 | 0 | } |
602 | | |
603 | 0 | void *ret = OPENSSL_malloc(size); |
604 | 0 | if (ret == NULL) { |
605 | 0 | return NULL; |
606 | 0 | } |
607 | | |
608 | 0 | OPENSSL_memcpy(ret, data, size); |
609 | 0 | return ret; |
610 | 0 | } |
611 | | |
612 | 0 | void *CRYPTO_malloc(size_t size, const char *file, int line) { |
613 | 0 | return OPENSSL_malloc(size); |
614 | 0 | } |
615 | | |
616 | 0 | void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) { |
617 | 0 | return OPENSSL_realloc(ptr, new_size); |
618 | 0 | } |
619 | | |
620 | 0 | void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); } |