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"
21 #include "internal/thread_once.h"
23 #define DEFAULT_SEPARATOR ':'
26 char *CRYPTO_strdup(const char *str, const char* file, int line)
32 ret = CRYPTO_malloc(strlen(str) + 1, file, line);
38 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
46 maxlen = OPENSSL_strnlen(str, s);
48 ret = CRYPTO_malloc(maxlen + 1, file, line);
50 memcpy(ret, str, maxlen);
51 ret[maxlen] = CH_ZERO;
56 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
60 if (data == NULL || siz >= INT_MAX)
63 ret = CRYPTO_malloc(siz, file, line);
65 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
68 return memcpy(ret, data, siz);
71 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
75 for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
80 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
83 for (; size > 1 && *src; size--) {
89 return l + strlen(src);
92 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
95 for (; size > 0 && *dst; size--, dst++)
97 return l + OPENSSL_strlcpy(dst, src, size);
100 int OPENSSL_hexchar2int(unsigned char c)
102 #ifdef CHARSET_EBCDIC
143 static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
144 const char *str, const char sep)
147 unsigned char ch, cl;
149 const unsigned char *p;
152 for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
154 /* A separator of CH_ZERO means there is no separator */
155 if (ch == sep && sep != CH_ZERO)
159 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
162 cli = OPENSSL_hexchar2int(cl);
163 chi = OPENSSL_hexchar2int(ch);
164 if (cli < 0 || chi < 0) {
165 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
171 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
174 *q++ = (unsigned char)((chi << 4) | cli);
184 * Given a string of hex digits convert to a buffer
186 int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
187 const char *str, const char sep)
189 return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
192 unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
196 size_t buf_n, tmp_buflen;
200 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
204 if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
205 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
212 if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
214 *buflen = (long)tmp_buflen;
221 unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
223 return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
226 static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
227 const unsigned char *buf, size_t buflen,
230 static const char hexdig[] = "0123456789ABCDEF";
231 const unsigned char *p;
234 int has_sep = (sep != CH_ZERO);
235 size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
237 if (strlength != NULL)
242 if (str_n < (unsigned long)len) {
243 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
248 for (i = 0, p = buf; i < buflen; i++, p++) {
249 *q++ = hexdig[(*p >> 4) & 0xf];
250 *q++ = hexdig[*p & 0xf];
258 #ifdef CHARSET_EBCDIC
259 ebcdic2ascii(str, str, q - str - 1);
264 int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
265 const unsigned char *buf, size_t buflen,
268 return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
271 char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
277 return OPENSSL_zalloc(1);
279 tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
280 if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) {
281 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
285 if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
293 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
294 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
295 * on EBCDIC machines)
297 char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
299 return ossl_buf2hexstr_sep(buf, buflen, ':');
302 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
304 #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
305 return !strerror_s(buf, buflen, errnum);
306 #elif defined(_GNU_SOURCE)
310 * GNU strerror_r may not actually set buf.
311 * It can return a pointer to some (immutable) static string in which case
312 * buf is left unused.
314 err = strerror_r(errnum, buf, buflen);
315 if (err == NULL || buflen == 0)
318 * If err is statically allocated, err != buf and we need to copy the data.
319 * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
320 * since src and dest are not annotated with __restrict and the function
321 * reads src byte for byte and writes to dest.
322 * If err == buf we do not have to copy anything.
325 OPENSSL_strlcpy(buf, err, buflen);
327 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
328 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
330 * We can use "real" strerror_r. The OpenSSL version differs in that it
331 * gives 1 on success and 0 on failure for consistency with other OpenSSL
332 * functions. Real strerror_r does it the other way around
334 return !strerror_r(errnum, buf, buflen);
338 /* Fall back to non-thread safe strerror()...its all we can do */
341 err = strerror(errnum);
342 /* Can this ever happen? */
345 OPENSSL_strlcpy(buf, err, buflen);
350 #ifndef OPENSSL_NO_LOCALE
352 static CRYPTO_ONCE casecmp = CRYPTO_ONCE_STATIC_INIT;
353 DEFINE_RUN_ONCE_STATIC(init_casecmp)
355 int ret = ossl_init_casecmp_int();
360 int ossl_init_casecmp(void)
362 if (!RUN_ONCE(&casecmp, init_casecmp))
370 static locale_t ossl_c_locale(void)
373 if (!ossl_init_casecmp())
379 int ossl_init_casecmp_int(void)
381 # ifdef OPENSSL_SYS_WINDOWS
382 loc = _create_locale(LC_COLLATE, "C");
384 loc = newlocale(LC_COLLATE_MASK, "C", (locale_t) 0);
386 return (loc == (locale_t)0) ? 0 : 1;
389 void ossl_deinit_casecmp(void)
391 if (loc != (locale_t)0) {
397 int OPENSSL_strcasecmp(const char *s1, const char *s2)
399 locale_t l = ossl_c_locale();
401 /* Fallback in case of locale initialization failure */
402 if (l == (locale_t)0)
403 return strcasecmp(s1, s2);
404 return strcasecmp_l(s1, s2, l);
407 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
409 locale_t l = ossl_c_locale();
411 /* Fallback in case of locale initialization failure */
412 if (l == (locale_t)0)
413 return strncasecmp(s1, s2, n);
414 return strncasecmp_l(s1, s2, n, l);
417 int ossl_init_casecmp(void)
422 int ossl_init_casecmp_int(void)
427 void ossl_deinit_casecmp(void)
431 int OPENSSL_strcasecmp(const char *s1, const char *s2)
433 return strcasecmp(s1, s2);
436 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
438 return strncasecmp(s1, s2, n);