diff options
author | Pauli <pauli@openssl.org> | 2022-05-19 12:23:55 +1000 |
---|---|---|
committer | Pauli <pauli@openssl.org> | 2022-05-23 09:51:28 +1000 |
commit | 286053fc8f78e34828a576830ef879c021640aee (patch) | |
tree | 8d4c8924c2c8c74f78d887de9fd84ba35b24424e /crypto/ctype.c | |
parent | 4d02d500aac80c136e3d6582b908e0fab77bbf42 (diff) |
tolower: refine the tolower code to avoid a memory access
This improves the performance of this function and the ones that rely on it
(ossl_lh_strcasehash primarily).
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)
Diffstat (limited to 'crypto/ctype.c')
-rw-r--r-- | crypto/ctype.c | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/crypto/ctype.c b/crypto/ctype.c index 2165213889..de2e836ff7 100644 --- a/crypto/ctype.c +++ b/crypto/ctype.c @@ -257,6 +257,36 @@ int ossl_ctype_check(int c, unsigned int mask) return a >= 0 && a < max && (ctype_char_map[a] & mask) != 0; } +/* + * Implement some of the simplier functions directly to avoid the overhead of + * accessing memory via ctype_char_map[]. + */ + +#define ASCII_IS_DIGIT(c) (c >= 0x30 && c <= 0x39) +#define ASCII_IS_UPPER(c) (c >= 0x41 && c <= 0x5A) +#define ASCII_IS_LOWER(c) (c >= 0x61 && c <= 0x7A) + +int ossl_isdigit(int c) +{ + int a = ossl_toascii(c); + + return ASCII_IS_DIGIT(a); +} + +int ossl_isupper(int c) +{ + int a = ossl_toascii(c); + + return ASCII_IS_UPPER(a); +} + +int ossl_islower(int c) +{ + int a = ossl_toascii(c); + + return ASCII_IS_LOWER(a); +} + #if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST) static const int case_change = 0x40; #else @@ -265,16 +295,19 @@ static const int case_change = 0x20; int ossl_tolower(int c) { - return ossl_isupper(c) ? c ^ case_change : c; + int a = ossl_toascii(c); + + return ASCII_IS_UPPER(a) ? c ^ case_change : c; } int ossl_toupper(int c) { - return ossl_islower(c) ? c ^ case_change : c; + int a = ossl_toascii(c); + + return ASCII_IS_LOWER(a) ? c ^ case_change : c; } -int ossl_ascii_isdigit(const char inchar) { - if (inchar > 0x2F && inchar < 0x3A) - return 1; - return 0; +int ossl_ascii_isdigit(int c) +{ + return ASCII_IS_DIGIT(c); } |