1 /* 2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #ifndef OPENSSL_HEADER_MEM_H 11 #define OPENSSL_HEADER_MEM_H 12 13 #include <openssl/base.h> 14 15 #include <stdlib.h> 16 #include <stdarg.h> 17 18 #if defined(__cplusplus) 19 extern "C" { 20 #endif 21 22 23 // Memory and string functions, see also buf.h. 24 // 25 // BoringSSL has its own set of allocation functions, which keep track of 26 // allocation lengths and zero them out before freeing. All memory returned by 27 // BoringSSL API calls must therefore generally be freed using |OPENSSL_free| 28 // unless stated otherwise. 29 30 31 #ifndef _BORINGSSL_PROHIBIT_OPENSSL_MALLOC 32 // OPENSSL_malloc is similar to a regular |malloc|, but allocates additional 33 // private data. The resulting pointer must be freed with |OPENSSL_free|. In 34 // the case of a malloc failure, prior to returning NULL |OPENSSL_malloc| will 35 // push |ERR_R_MALLOC_FAILURE| onto the openssl error stack. 36 OPENSSL_EXPORT void *OPENSSL_malloc(size_t size); 37 38 // OPENSSL_zalloc behaves like |OPENSSL_malloc| except it also initializes the 39 // resulting memory to zero. 40 OPENSSL_EXPORT void *OPENSSL_zalloc(size_t size); 41 42 // OPENSSL_calloc is similar to a regular |calloc|, but allocates data with 43 // |OPENSSL_malloc|. On overflow, it will push |ERR_R_OVERFLOW| onto the error 44 // queue. 45 OPENSSL_EXPORT void *OPENSSL_calloc(size_t num, size_t size); 46 47 // OPENSSL_realloc returns a pointer to a buffer of |new_size| bytes that 48 // contains the contents of |ptr|. Unlike |realloc|, a new buffer is always 49 // allocated and the data at |ptr| is always wiped and freed. Memory is 50 // allocated with |OPENSSL_malloc| and must be freed with |OPENSSL_free|. 51 OPENSSL_EXPORT void *OPENSSL_realloc(void *ptr, size_t new_size); 52 #endif // !_BORINGSSL_PROHIBIT_OPENSSL_MALLOC 53 54 // OPENSSL_free does nothing if |ptr| is NULL. Otherwise it zeros out the 55 // memory allocated at |ptr| and frees it along with the private data. 56 // It must only be used on on |ptr| values obtained from |OPENSSL_malloc| 57 OPENSSL_EXPORT void OPENSSL_free(void *ptr); 58 59 // OPENSSL_cleanse zeros out |len| bytes of memory at |ptr|. This is similar to 60 // |memset_s| from C11. 61 OPENSSL_EXPORT void OPENSSL_cleanse(void *ptr, size_t len); 62 63 // CRYPTO_memcmp returns zero iff the |len| bytes at |a| and |b| are equal. It 64 // takes an amount of time dependent on |len|, but independent of the contents 65 // of |a| and |b|. Unlike memcmp, it cannot be used to put elements into a 66 // defined order as the return value when a != b is undefined, other than to be 67 // non-zero. 68 OPENSSL_EXPORT int CRYPTO_memcmp(const void *a, const void *b, size_t len); 69 70 // OPENSSL_hash32 implements the 32 bit, FNV-1a hash. 71 OPENSSL_EXPORT uint32_t OPENSSL_hash32(const void *ptr, size_t len); 72 73 // OPENSSL_strhash calls |OPENSSL_hash32| on the NUL-terminated string |s|. 74 OPENSSL_EXPORT uint32_t OPENSSL_strhash(const char *s); 75 76 // OPENSSL_strdup has the same behaviour as strdup(3). 77 OPENSSL_EXPORT char *OPENSSL_strdup(const char *s); 78 79 // OPENSSL_strnlen has the same behaviour as strnlen(3). 80 OPENSSL_EXPORT size_t OPENSSL_strnlen(const char *s, size_t len); 81 82 // OPENSSL_isalpha is a locale-independent, ASCII-only version of isalpha(3), It 83 // only recognizes 'a' through 'z' and 'A' through 'Z' as alphabetic. 84 OPENSSL_EXPORT int OPENSSL_isalpha(int c); 85 86 // OPENSSL_isdigit is a locale-independent, ASCII-only version of isdigit(3), It 87 // only recognizes '0' through '9' as digits. 88 OPENSSL_EXPORT int OPENSSL_isdigit(int c); 89 90 // OPENSSL_isxdigit is a locale-independent, ASCII-only version of isxdigit(3), 91 // It only recognizes '0' through '9', 'a' through 'f', and 'A through 'F' as 92 // digits. 93 OPENSSL_EXPORT int OPENSSL_isxdigit(int c); 94 95 // OPENSSL_fromxdigit returns one if |c| is a hexadecimal digit as recognized 96 // by OPENSSL_isxdigit, and sets |out| to the corresponding value. Otherwise 97 // zero is returned. 98 OPENSSL_EXPORT int OPENSSL_fromxdigit(uint8_t *out, int c); 99 100 // OPENSSL_isalnum is a locale-independent, ASCII-only version of isalnum(3), It 101 // only recognizes what |OPENSSL_isalpha| and |OPENSSL_isdigit| recognize. 102 OPENSSL_EXPORT int OPENSSL_isalnum(int c); 103 104 // OPENSSL_tolower is a locale-independent, ASCII-only version of tolower(3). It 105 // only lowercases ASCII values. Other values are returned as-is. 106 OPENSSL_EXPORT int OPENSSL_tolower(int c); 107 108 // OPENSSL_isspace is a locale-independent, ASCII-only version of isspace(3). It 109 // only recognizes '\t', '\n', '\v', '\f', '\r', and ' '. 110 OPENSSL_EXPORT int OPENSSL_isspace(int c); 111 112 // OPENSSL_strcasecmp is a locale-independent, ASCII-only version of 113 // strcasecmp(3). 114 OPENSSL_EXPORT int OPENSSL_strcasecmp(const char *a, const char *b); 115 116 // OPENSSL_strncasecmp is a locale-independent, ASCII-only version of 117 // strncasecmp(3). 118 OPENSSL_EXPORT int OPENSSL_strncasecmp(const char *a, const char *b, size_t n); 119 120 // DECIMAL_SIZE returns an upper bound for the length of the decimal 121 // representation of the given type. 122 #define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1) 123 124 // BIO_snprintf has the same behavior as snprintf(3). 125 OPENSSL_EXPORT int BIO_snprintf(char *buf, size_t n, const char *format, ...) 126 OPENSSL_PRINTF_FORMAT_FUNC(3, 4); 127 128 // BIO_vsnprintf has the same behavior as vsnprintf(3). 129 OPENSSL_EXPORT int BIO_vsnprintf(char *buf, size_t n, const char *format, 130 va_list args) OPENSSL_PRINTF_FORMAT_FUNC(3, 0); 131 132 // OPENSSL_vasprintf has the same behavior as vasprintf(3), except that 133 // memory allocated in a returned string must be freed with |OPENSSL_free|. 134 OPENSSL_EXPORT int OPENSSL_vasprintf(char **str, const char *format, 135 va_list args) 136 OPENSSL_PRINTF_FORMAT_FUNC(2, 0); 137 138 // OPENSSL_asprintf has the same behavior as asprintf(3), except that 139 // memory allocated in a returned string must be freed with |OPENSSL_free|. 140 OPENSSL_EXPORT int OPENSSL_asprintf(char **str, const char *format, ...) 141 OPENSSL_PRINTF_FORMAT_FUNC(2, 3); 142 143 // OPENSSL_strndup returns an allocated, duplicate of |str|, which is, at most, 144 // |size| bytes. The result is always NUL terminated. The memory allocated 145 // must be freed with |OPENSSL_free|. 146 OPENSSL_EXPORT char *OPENSSL_strndup(const char *str, size_t size); 147 148 // OPENSSL_memdup returns an allocated, duplicate of |size| bytes from |data| or 149 // NULL on allocation failure. The memory allocated must be freed with 150 // |OPENSSL_free|. 151 OPENSSL_EXPORT void *OPENSSL_memdup(const void *data, size_t size); 152 153 // OPENSSL_strlcpy acts like strlcpy(3). 154 OPENSSL_EXPORT size_t OPENSSL_strlcpy(char *dst, const char *src, 155 size_t dst_size); 156 157 // OPENSSL_strlcat acts like strlcat(3). 158 OPENSSL_EXPORT size_t OPENSSL_strlcat(char *dst, const char *src, 159 size_t dst_size); 160 161 162 // Deprecated functions. 163 164 // CRYPTO_malloc calls |OPENSSL_malloc|. |file| and |line| are ignored. 165 OPENSSL_EXPORT void *CRYPTO_malloc(size_t size, const char *file, int line); 166 167 // CRYPTO_realloc calls |OPENSSL_realloc|. |file| and |line| are ignored. 168 OPENSSL_EXPORT void *CRYPTO_realloc(void *ptr, size_t new_size, 169 const char *file, int line); 170 171 // CRYPTO_free calls |OPENSSL_free|. |file| and |line| are ignored. 172 OPENSSL_EXPORT void CRYPTO_free(void *ptr, const char *file, int line); 173 174 // OPENSSL_clear_free calls |OPENSSL_free|. BoringSSL automatically clears all 175 // allocations on free, but we define |OPENSSL_clear_free| for compatibility. 176 OPENSSL_EXPORT void OPENSSL_clear_free(void *ptr, size_t len); 177 178 // CRYPTO_secure_malloc_init returns zero. 179 OPENSSL_EXPORT int CRYPTO_secure_malloc_init(size_t size, size_t min_size); 180 181 // CRYPTO_secure_malloc_initialized returns zero. 182 OPENSSL_EXPORT int CRYPTO_secure_malloc_initialized(void); 183 184 // CRYPTO_secure_used returns zero. 185 OPENSSL_EXPORT size_t CRYPTO_secure_used(void); 186 187 // OPENSSL_secure_malloc calls |OPENSSL_malloc|. 188 OPENSSL_EXPORT void *OPENSSL_secure_malloc(size_t size); 189 190 // OPENSSL_secure_clear_free calls |OPENSSL_clear_free|. 191 OPENSSL_EXPORT void OPENSSL_secure_clear_free(void *ptr, size_t len); 192 193 194 #if defined(__cplusplus) 195 } // extern C 196 197 extern "C++" { 198 199 BSSL_NAMESPACE_BEGIN 200 201 BORINGSSL_MAKE_DELETER(char, OPENSSL_free) 202 BORINGSSL_MAKE_DELETER(uint8_t, OPENSSL_free) 203 204 BSSL_NAMESPACE_END 205 206 } // extern C++ 207 208 #endif 209 210 #endif // OPENSSL_HEADER_MEM_H 211