Coverage Report

Created: 2025-07-23 07:04

/rust/registry/src/index.crates.io-6f17d22bba15001f/aws-lc-sys-0.28.0/aws-lc/crypto/mem.c
Line
Count
Source (jump to first uncovered line)
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
// Below can be customized by |CRYPTO_set_mem_functions| only once.
126
static void *(*malloc_impl)(size_t, const char *, int) = NULL;
127
static void *(*realloc_impl)(void *, size_t, const char *, int) = NULL;
128
static void (*free_impl)(void *, const char *, int) = NULL;
129
130
int CRYPTO_set_mem_functions(
131
  void *(*m)(size_t, const char *, int),
132
  void *(*r)(void *, size_t, const char *, int),
133
0
  void (*f)(void *, const char *, int)) {
134
0
  if (m == NULL || r == NULL || f == NULL) {
135
0
    return 0;
136
0
  }
137
  // |malloc_impl|, |realloc_impl| and |free_impl| can be set only once.
138
0
  if (malloc_impl != NULL || realloc_impl != NULL || free_impl != NULL) {
139
0
    return 0;
140
0
  }
141
0
  if (OPENSSL_memory_alloc != NULL ||
142
0
      OPENSSL_memory_free != NULL ||
143
0
      OPENSSL_memory_get_size != NULL ||
144
0
      OPENSSL_memory_realloc != NULL) {
145
    // |OPENSSL_malloc/free/realloc| are customized by overriding the symbols.
146
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
147
0
    return 0;
148
0
  }
149
0
  malloc_impl = m;
150
0
  realloc_impl = r;
151
0
  free_impl = f;
152
0
  return 1;
153
0
}
154
155
0
void *OPENSSL_malloc(size_t size) {
156
0
  if (malloc_impl != NULL) {
157
0
    assert(OPENSSL_memory_alloc == NULL);
158
0
    assert(OPENSSL_memory_realloc == NULL);
159
0
    assert(OPENSSL_memory_free == NULL);
160
0
    assert(OPENSSL_memory_get_size == NULL);
161
0
    assert(realloc_impl != NULL);
162
0
    assert(free_impl != NULL);
163
0
    return malloc_impl(size, AWSLC_FILE, AWSLC_LINE);
164
0
  }
165
0
  if (OPENSSL_memory_alloc != NULL) {
166
0
    assert(OPENSSL_memory_free != NULL);
167
0
    assert(OPENSSL_memory_get_size != NULL);
168
0
    void *ptr = OPENSSL_memory_alloc(size);
169
0
    if (ptr == NULL && size != 0) {
170
0
      goto err;
171
0
    }
172
0
    return ptr;
173
0
  }
174
175
0
  if (size + OPENSSL_MALLOC_PREFIX < size) {
176
0
    goto err;
177
0
  }
178
179
0
  void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX);
180
0
  if (ptr == NULL) {
181
0
    goto err;
182
0
  }
183
184
0
  *(size_t *)ptr = size;
185
186
0
  __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
187
0
  return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
188
189
0
 err:
190
  // This only works because ERR does not call OPENSSL_malloc.
191
0
  OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
192
0
  return NULL;
193
0
}
194
195
0
void *OPENSSL_zalloc(size_t size) {
196
0
  void *ret = OPENSSL_malloc(size);
197
0
  if (ret != NULL) {
198
0
    OPENSSL_memset(ret, 0, size);
199
0
  }
200
0
  return ret;
201
0
}
202
203
0
void *OPENSSL_calloc(size_t num, size_t size) {
204
0
  if (size != 0 && num > SIZE_MAX / size) {
205
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
206
0
    return NULL;
207
0
  }
208
209
0
  return OPENSSL_zalloc(num * size);
210
0
}
211
212
0
void OPENSSL_free(void *orig_ptr) {
213
0
  if (orig_ptr == NULL) {
214
0
    return;
215
0
  }
216
0
  if (free_impl != NULL) {
217
0
    assert(OPENSSL_memory_alloc == NULL);
218
0
    assert(OPENSSL_memory_realloc == NULL);
219
0
    assert(OPENSSL_memory_free == NULL);
220
0
    assert(OPENSSL_memory_get_size == NULL);
221
0
    assert(malloc_impl != NULL);
222
0
    assert(realloc_impl != NULL);
223
0
    free_impl(orig_ptr, AWSLC_FILE, AWSLC_LINE);
224
0
    return;
225
0
  }
226
227
0
  if (OPENSSL_memory_free != NULL) {
228
0
    OPENSSL_memory_free(orig_ptr);
229
0
    return;
230
0
  }
231
232
0
  void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
233
0
  __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
234
235
0
  size_t size = *(size_t *)ptr;
236
0
  OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
237
238
// ASan knows to intercept malloc and free, but not sdallocx.
239
#if defined(OPENSSL_ASAN)
240
  (void)sdallocx;
241
  free(ptr);
242
  (void) sdallocx;
243
#else
244
0
  if (sdallocx) {
245
0
    sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
246
0
  } else {
247
0
    free(ptr);
248
0
  }
249
0
#endif
250
0
}
251
252
0
void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
253
0
  if (orig_ptr == NULL) {
254
0
    return OPENSSL_malloc(new_size);
255
0
  }
256
0
  if (realloc_impl != NULL) {
257
0
    assert(OPENSSL_memory_alloc == NULL);
258
0
    assert(OPENSSL_memory_realloc == NULL);
259
0
    assert(OPENSSL_memory_free == NULL);
260
0
    assert(OPENSSL_memory_get_size == NULL);
261
0
    assert(malloc_impl != NULL);
262
0
    assert(free_impl != NULL);
263
0
    return realloc_impl(orig_ptr, new_size, AWSLC_FILE, AWSLC_LINE);
264
0
  }
265
0
  if (OPENSSL_memory_realloc != NULL) {
266
0
    assert(OPENSSL_memory_alloc != NULL);
267
0
    assert(OPENSSL_memory_free != NULL);
268
0
    assert(OPENSSL_memory_get_size != NULL);
269
0
    return OPENSSL_memory_realloc(orig_ptr, new_size);
270
0
  }
271
0
  size_t old_size;
272
0
  if (OPENSSL_memory_get_size != NULL) {
273
0
    old_size = OPENSSL_memory_get_size(orig_ptr);
274
0
  } else {
275
0
    void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
276
0
    __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
277
0
    old_size = *(size_t *)ptr;
278
0
    __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
279
0
  }
280
281
0
  void *ret = OPENSSL_malloc(new_size);
282
0
  if (ret == NULL) {
283
0
    return NULL;
284
0
  }
285
286
0
  size_t to_copy = new_size;
287
0
  if (old_size < to_copy) {
288
0
    to_copy = old_size;
289
0
  }
290
291
0
  memcpy(ret, orig_ptr, to_copy);
292
0
  OPENSSL_free(orig_ptr);
293
294
0
  return ret;
295
0
}
296
297
0
void OPENSSL_cleanse(void *ptr, size_t len) {
298
#if defined(OPENSSL_WINDOWS)
299
  SecureZeroMemory(ptr, len);
300
#else
301
0
  OPENSSL_memset(ptr, 0, len);
302
303
0
#if !defined(OPENSSL_NO_ASM)
304
  /* As best as we can tell, this is sufficient to break any optimisations that
305
     might try to eliminate "superfluous" memsets. If there's an easy way to
306
     detect memset_s, it would be better to use that. */
307
0
  __asm__ __volatile__("" : : "r"(ptr) : "memory");
308
0
#endif
309
0
#endif  // !OPENSSL_NO_ASM
310
0
}
311
312
0
void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); }
313
314
0
int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; }
315
316
0
int CRYPTO_secure_malloc_initialized(void) { return 0; }
317
318
0
size_t CRYPTO_secure_used(void) { return 0; }
319
320
0
void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); }
321
322
0
void *OPENSSL_secure_zalloc(size_t size) { return OPENSSL_zalloc(size); }
323
324
0
void OPENSSL_secure_clear_free(void *ptr, size_t len) {
325
0
  OPENSSL_clear_free(ptr, len);
326
0
}
327
328
0
int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
329
0
  const uint8_t *a = in_a;
330
0
  const uint8_t *b = in_b;
331
0
  uint8_t x = 0;
332
333
0
  for (size_t i = 0; i < len; i++) {
334
0
    x |= a[i] ^ b[i];
335
0
  }
336
337
0
  return x;
338
0
}
339
340
0
uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
341
  // These are the FNV-1a parameters for 32 bits.
342
0
  static const uint32_t kPrime = 16777619u;
343
0
  static const uint32_t kOffsetBasis = 2166136261u;
344
345
0
  const uint8_t *in = ptr;
346
0
  uint32_t h = kOffsetBasis;
347
348
0
  for (size_t i = 0; i < len; i++) {
349
0
    h ^= in[i];
350
0
    h *= kPrime;
351
0
  }
352
353
0
  return h;
354
0
}
355
356
0
uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); }
357
358
0
size_t OPENSSL_strnlen(const char *s, size_t len) {
359
0
  for (size_t i = 0; i < len; i++) {
360
0
    if (s[i] == 0) {
361
0
      return i;
362
0
    }
363
0
  }
364
365
0
  return len;
366
0
}
367
368
0
char *OPENSSL_strdup(const char *s) {
369
0
  if (s == NULL) {
370
0
    return NULL;
371
0
  }
372
  // Copy the NUL terminator.
373
0
  return OPENSSL_memdup(s, strlen(s) + 1);
374
0
}
375
376
0
int OPENSSL_isalpha(int c) {
377
0
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
378
0
}
379
380
0
int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; }
381
382
0
int OPENSSL_isxdigit(int c) {
383
0
  return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
384
0
}
385
386
0
int OPENSSL_fromxdigit(uint8_t *out, int c) {
387
0
  if (OPENSSL_isdigit(c)) {
388
0
    *out = c - '0';
389
0
    return 1;
390
0
  }
391
0
  if ('a' <= c && c <= 'f') {
392
0
    *out = c - 'a' + 10;
393
0
    return 1;
394
0
  }
395
0
  if ('A' <= c && c <= 'F') {
396
0
    *out = c - 'A' + 10;
397
0
    return 1;
398
0
  }
399
0
  return 0;
400
0
}
401
402
0
uint8_t *OPENSSL_hexstr2buf(const char *str, size_t *len) {
403
0
  if (str == NULL || len == NULL) {
404
0
    return NULL;
405
0
  }
406
407
0
  const size_t slen = OPENSSL_strnlen(str, INT16_MAX);
408
0
  if (slen % 2 != 0) {
409
0
    return NULL;
410
0
  }
411
412
0
  const size_t buflen = slen / 2;
413
0
  uint8_t *buf = OPENSSL_zalloc(buflen);
414
0
  if (buf == NULL) {
415
0
    return NULL;
416
0
  }
417
418
0
  for (size_t i = 0; i < buflen; i++) {
419
0
    uint8_t hi, lo;
420
0
    if (!OPENSSL_fromxdigit(&hi, str[2 * i]) ||
421
0
        !OPENSSL_fromxdigit(&lo, str[2 * i + 1])) {
422
0
      OPENSSL_free(buf);
423
0
      return NULL;
424
0
    }
425
0
    buf[i] = (hi << 4) | lo;
426
0
  }
427
428
0
  *len = buflen;
429
0
  return buf;
430
0
}
431
432
0
int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); }
433
434
0
int OPENSSL_tolower(int c) {
435
0
  if (c >= 'A' && c <= 'Z') {
436
0
    return c + ('a' - 'A');
437
0
  }
438
0
  return c;
439
0
}
440
441
0
int OPENSSL_isspace(int c) {
442
0
  return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' ||
443
0
         c == ' ';
444
0
}
445
446
0
int OPENSSL_strcasecmp(const char *a, const char *b) {
447
0
  for (size_t i = 0;; i++) {
448
0
    const int aa = OPENSSL_tolower(a[i]);
449
0
    const int bb = OPENSSL_tolower(b[i]);
450
451
0
    if (aa < bb) {
452
0
      return -1;
453
0
    } else if (aa > bb) {
454
0
      return 1;
455
0
    } else if (aa == 0) {
456
0
      return 0;
457
0
    }
458
0
  }
459
0
}
460
461
0
int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
462
0
  for (size_t i = 0; i < n; i++) {
463
0
    const int aa = OPENSSL_tolower(a[i]);
464
0
    const int bb = OPENSSL_tolower(b[i]);
465
466
0
    if (aa < bb) {
467
0
      return -1;
468
0
    } else if (aa > bb) {
469
0
      return 1;
470
0
    } else if (aa == 0) {
471
0
      return 0;
472
0
    }
473
0
  }
474
475
0
  return 0;
476
0
}
477
478
0
int BIO_snprintf(char *buf, size_t n, const char *format, ...) {
479
0
  va_list args;
480
0
  va_start(args, format);
481
0
  int ret = BIO_vsnprintf(buf, n, format, args);
482
0
  va_end(args);
483
0
  return ret;
484
0
}
485
486
0
int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
487
0
  return vsnprintf(buf, n, format, args);
488
0
}
489
490
int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args,
491
0
                               int system_malloc) {
492
0
  void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc;
493
0
  void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free;
494
0
  void *(*reallocate)(void *, size_t) =
495
0
      system_malloc ? realloc : OPENSSL_realloc;
496
0
  char *candidate = NULL;
497
0
  size_t candidate_len = 64;  // TODO(bbe) what's the best initial size?
498
499
0
  if ((candidate = allocate(candidate_len)) == NULL) {
500
0
    goto err;
501
0
  }
502
0
  va_list args_copy;
503
0
  va_copy(args_copy, args);
504
0
  int ret = vsnprintf(candidate, candidate_len, format, args_copy);
505
0
  va_end(args_copy);
506
0
  if (ret < 0) {
507
0
    goto err;
508
0
  }
509
0
  if ((size_t)ret >= candidate_len) {
510
    // Too big to fit in allocation.
511
0
    char *tmp;
512
513
0
    candidate_len = (size_t)ret + 1;
514
0
    if ((tmp = reallocate(candidate, candidate_len)) == NULL) {
515
0
      goto err;
516
0
    }
517
0
    candidate = tmp;
518
0
    ret = vsnprintf(candidate, candidate_len, format, args);
519
0
  }
520
  // At this point this should not happen unless vsnprintf is insane.
521
0
  if (ret < 0 || (size_t)ret >= candidate_len) {
522
0
    goto err;
523
0
  }
524
0
  *str = candidate;
525
0
  return ret;
526
527
0
 err:
528
0
  deallocate(candidate);
529
0
  *str = NULL;
530
0
  errno = ENOMEM;
531
0
  return -1;
532
0
}
533
534
0
int OPENSSL_vasprintf(char **str, const char *format, va_list args) {
535
0
  return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0);
536
0
}
537
538
0
int OPENSSL_asprintf(char **str, const char *format, ...) {
539
0
  va_list args;
540
0
  va_start(args, format);
541
0
  int ret = OPENSSL_vasprintf(str, format, args);
542
0
  va_end(args);
543
0
  return ret;
544
0
}
545
546
0
char *OPENSSL_strndup(const char *str, size_t size) {
547
0
  size = OPENSSL_strnlen(str, size);
548
549
0
  size_t alloc_size = size + 1;
550
0
  if (alloc_size < size) {
551
    // overflow
552
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
553
0
    return NULL;
554
0
  }
555
0
  char *ret = OPENSSL_malloc(alloc_size);
556
0
  if (ret == NULL) {
557
0
    return NULL;
558
0
  }
559
560
0
  OPENSSL_memcpy(ret, str, size);
561
0
  ret[size] = '\0';
562
0
  return ret;
563
0
}
564
565
0
size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) {
566
0
  size_t l = 0;
567
568
0
  for (; dst_size > 1 && *src; dst_size--) {
569
0
    *dst++ = *src++;
570
0
    l++;
571
0
  }
572
573
0
  if (dst_size) {
574
0
    *dst = 0;
575
0
  }
576
577
0
  return l + strlen(src);
578
0
}
579
580
0
size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) {
581
0
  size_t l = 0;
582
0
  for (; dst_size > 0 && *dst; dst_size--, dst++) {
583
0
    l++;
584
0
  }
585
0
  return l + OPENSSL_strlcpy(dst, src, dst_size);
586
0
}
587
588
0
void *OPENSSL_memdup(const void *data, size_t size) {
589
0
  if (size == 0) {
590
0
    return NULL;
591
0
  }
592
593
0
  void *ret = OPENSSL_malloc(size);
594
0
  if (ret == NULL) {
595
0
    return NULL;
596
0
  }
597
598
0
  OPENSSL_memcpy(ret, data, size);
599
0
  return ret;
600
0
}
601
602
0
void *CRYPTO_malloc(size_t size, const char *file, int line) {
603
0
  return OPENSSL_malloc(size);
604
0
}
605
606
0
void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) {
607
0
  return OPENSSL_realloc(ptr, new_size);
608
0
}
609
610
0
void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); }