diff options
author | Pauli <pauli@openssl.org> | 2022-05-19 12:51:07 +1000 |
---|---|---|
committer | Pauli <pauli@openssl.org> | 2022-05-23 09:54:29 +1000 |
commit | d971d89786149d47efbf4d58f48eb94e65aaa07f (patch) | |
tree | ddabf48bafcacdf26ade279b7b3233b26a4fe004 /crypto/o_str.c | |
parent | 27e8f212d8383e78e698d7049c210b13a0cc8889 (diff) |
strcasecmp: implement strcasecmp and strncasecmp
Rather than relying on the locale code working, instead implement these
functions directly.
Fixes #18322
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Tim Hudson <tjh@openssl.org>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/18344)
(cherry picked from commit fb4cdca053fb9d3f0e11eeaf31f4b4ff87f69a95)
Diffstat (limited to 'crypto/o_str.c')
-rw-r--r-- | crypto/o_str.c | 102 |
1 files changed, 14 insertions, 88 deletions
diff --git a/crypto/o_str.c b/crypto/o_str.c index 789de7bd4d..7fa487dd5f 100644 --- a/crypto/o_str.c +++ b/crypto/o_str.c @@ -10,13 +10,8 @@ #include "e_os.h" #include <string.h> #include <limits.h> -#ifndef OPENSSL_NO_LOCALE -# include <locale.h> -# ifdef OPENSSL_SYS_MACOSX -# include <xlocale.h> -# endif -#endif #include <openssl/crypto.h> +#include "crypto/ctype.h" #include "internal/cryptlib.h" #include "internal/thread_once.h" @@ -347,94 +342,25 @@ int openssl_strerror_r(int errnum, char *buf, size_t buflen) #endif } -#ifndef OPENSSL_NO_LOCALE -# ifndef FIPS_MODULE -static CRYPTO_ONCE casecmp = CRYPTO_ONCE_STATIC_INIT; -DEFINE_RUN_ONCE_STATIC(init_casecmp) -{ - int ret = ossl_init_casecmp_int(); - - return ret; -} - -int ossl_init_casecmp(void) -{ - if (!RUN_ONCE(&casecmp, init_casecmp)) - return 0; - return 1; -} -# endif - -static locale_t loc; - -static locale_t ossl_c_locale(void) -{ -# ifndef FIPS_MODULE - if (!ossl_init_casecmp()) - return (locale_t)0; -# endif - return loc; -} - -int ossl_init_casecmp_int(void) -{ -# ifdef OPENSSL_SYS_WINDOWS - loc = _create_locale(LC_COLLATE, "C"); -# else - loc = newlocale(LC_COLLATE_MASK, "C", (locale_t) 0); -# endif - return (loc == (locale_t)0) ? 0 : 1; -} - -void ossl_deinit_casecmp(void) -{ - if (loc != (locale_t)0) { - freelocale(loc); - loc = (locale_t)0; - } -} - int OPENSSL_strcasecmp(const char *s1, const char *s2) { - locale_t l = ossl_c_locale(); + int t; - /* Fallback in case of locale initialization failure */ - if (l == (locale_t)0) - return strcasecmp(s1, s2); - return strcasecmp_l(s1, s2, l); + while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0) + if (*s1++ == '\0') + return 0; + return t; } int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n) { - locale_t l = ossl_c_locale(); - - /* Fallback in case of locale initialization failure */ - if (l == (locale_t)0) - return strncasecmp(s1, s2, n); - return strncasecmp_l(s1, s2, n, l); -} -#else -int ossl_init_casecmp(void) -{ - return 1; -} - -int ossl_init_casecmp_int(void) -{ - return 1; -} - -void ossl_deinit_casecmp(void) -{ -} - -int OPENSSL_strcasecmp(const char *s1, const char *s2) -{ - return strcasecmp(s1, s2); -} + int t; + size_t i; -int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n) -{ - return strncasecmp(s1, s2, n); + for (i = 0; i < n; i++) + if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0) + return t; + else if (*s1++ == '\0') + return 0; + return 0; } -#endif |