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