2 * Copyright 2003-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #include "internal/e_os.h"
13 #ifndef OPENSSL_NO_LOCALE
15 # ifdef OPENSSL_SYS_MACOSX
19 #include <openssl/crypto.h>
20 #include "internal/cryptlib.h"
22 #define DEFAULT_SEPARATOR ':'
25 char *CRYPTO_strdup(const char *str, const char* file, int line)
31 ret = CRYPTO_malloc(strlen(str) + 1, file, line);
37 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
45 maxlen = OPENSSL_strnlen(str, s);
47 ret = CRYPTO_malloc(maxlen + 1, file, line);
49 memcpy(ret, str, maxlen);
50 ret[maxlen] = CH_ZERO;
55 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
59 if (data == NULL || siz >= INT_MAX)
62 ret = CRYPTO_malloc(siz, file, line);
64 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
67 return memcpy(ret, data, siz);
70 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
74 for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
79 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
82 for (; size > 1 && *src; size--) {
88 return l + strlen(src);
91 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
94 for (; size > 0 && *dst; size--, dst++)
96 return l + OPENSSL_strlcpy(dst, src, size);
99 int OPENSSL_hexchar2int(unsigned char c)
101 #ifdef CHARSET_EBCDIC
142 static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
143 const char *str, const char sep)
146 unsigned char ch, cl;
148 const unsigned char *p;
151 for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
153 /* A separator of CH_ZERO means there is no separator */
154 if (ch == sep && sep != CH_ZERO)
158 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
161 cli = OPENSSL_hexchar2int(cl);
162 chi = OPENSSL_hexchar2int(ch);
163 if (cli < 0 || chi < 0) {
164 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
170 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
173 *q++ = (unsigned char)((chi << 4) | cli);
183 * Given a string of hex digits convert to a buffer
185 int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
186 const char *str, const char sep)
188 return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
191 unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
195 size_t buf_n, tmp_buflen;
199 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
203 if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
204 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
211 if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
213 *buflen = (long)tmp_buflen;
220 unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
222 return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
225 static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
226 const unsigned char *buf, size_t buflen,
229 static const char hexdig[] = "0123456789ABCDEF";
230 const unsigned char *p;
233 int has_sep = (sep != CH_ZERO);
234 size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
236 if (strlength != NULL)
241 if (str_n < (unsigned long)len) {
242 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
247 for (i = 0, p = buf; i < buflen; i++, p++) {
248 *q++ = hexdig[(*p >> 4) & 0xf];
249 *q++ = hexdig[*p & 0xf];
257 #ifdef CHARSET_EBCDIC
258 ebcdic2ascii(str, str, q - str - 1);
263 int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
264 const unsigned char *buf, size_t buflen,
267 return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
270 char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
276 return OPENSSL_zalloc(1);
278 tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
279 if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) {
280 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
284 if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
292 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
293 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
294 * on EBCDIC machines)
296 char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
298 return ossl_buf2hexstr_sep(buf, buflen, ':');
301 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
303 #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
304 return !strerror_s(buf, buflen, errnum);
305 #elif defined(_GNU_SOURCE)
309 * GNU strerror_r may not actually set buf.
310 * It can return a pointer to some (immutable) static string in which case
311 * buf is left unused.
313 err = strerror_r(errnum, buf, buflen);
314 if (err == NULL || buflen == 0)
317 * If err is statically allocated, err != buf and we need to copy the data.
318 * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
319 * since src and dest are not annotated with __restrict and the function
320 * reads src byte for byte and writes to dest.
321 * If err == buf we do not have to copy anything.
324 OPENSSL_strlcpy(buf, err, buflen);
326 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
327 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
329 * We can use "real" strerror_r. The OpenSSL version differs in that it
330 * gives 1 on success and 0 on failure for consistency with other OpenSSL
331 * functions. Real strerror_r does it the other way around
333 return !strerror_r(errnum, buf, buflen);
337 /* Fall back to non-thread safe strerror()...its all we can do */
340 err = strerror(errnum);
341 /* Can this ever happen? */
344 OPENSSL_strlcpy(buf, err, buflen);
349 #ifndef OPENSSL_NO_LOCALE
352 static locale_t ossl_c_locale(void) {
356 int ossl_init_casecmp_int(void) {
357 # ifdef OPENSSL_SYS_WINDOWS
358 loc = _create_locale(LC_COLLATE, "C");
360 loc = newlocale(LC_COLLATE_MASK, "C", (locale_t) 0);
362 return (loc == (locale_t)0) ? 0 : 1;
365 void ossl_deinit_casecmp(void) {
370 int OPENSSL_strcasecmp(const char *s1, const char *s2)
372 locale_t l = ossl_c_locale();
374 /* Fallback in case of locale initialization failure */
375 if (l == (locale_t)0)
376 return strcasecmp(s1, s2);
377 return strcasecmp_l(s1, s2, l);
380 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
382 locale_t l = ossl_c_locale();
384 /* Fallback in case of locale initialization failure */
385 if (l == (locale_t)0)
386 return strncasecmp(s1, s2, n);
387 return strncasecmp_l(s1, s2, n, l);
390 int ossl_init_casecmp_int(void) {
394 void ossl_deinit_casecmp(void) {
397 int OPENSSL_strcasecmp(const char *s1, const char *s2)
399 return strcasecmp(s1, s2);
402 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
404 return strncasecmp(s1, s2, n);