From 9bb55626d51f217466bc08104d2f32eb0bf57c02 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Thu, 30 Apr 2026 22:29:38 +0100 Subject: Switch ascii comparisons over to our own casemap functions. --- include/casemap.h | 33 +++++++++++++++++++++++++++++---- include/configreader.h | 2 +- include/modules/hash.h | 2 +- include/utility/string.h | 27 --------------------------- 4 files changed, 31 insertions(+), 33 deletions(-) (limited to 'include') diff --git a/include/casemap.h b/include/casemap.h index b31c7ed18..74c2b01c5 100644 --- a/include/casemap.h +++ b/include/casemap.h @@ -56,20 +56,20 @@ namespace insp using casemapped_unordered_map = std::unordered_map, casemapped_equals_obj>; /** Determines if \p str1 and \p str2 are equivalent when compared case insensitively using the - * configured IRC casemapping. + * specified IRC casemapping. * @param str1 The first string to compare. * @param str2 The second string to compare. * @return True if \p str1 and \p str2 are equivalent; otherwise, false. */ - CoreExport bool casemapped_equals(const std::string_view& str1, const std::string_view& str2); + CoreExport bool casemapped_equals(const std::string_view& str1, const std::string_view& str2, const casemap* map = nullptr); /** Determines if \p str1 is lexographically less than \p str2 when compared insensitively using - * the configured IRC casemapping. + * the specified IRC casemapping. * @param str1 The first string to compare. * @param str2 The second string to compare. * @return True if \p str1 is ranked lower than \p str2; otherwise, false. */ - CoreExport bool casemapped_less(const std::string_view& str1, const std::string_view& str2); + CoreExport bool casemapped_less(const std::string_view& str1, const std::string_view& str2, const casemap* map = nullptr); /** Determines the unique hash of \p str for use in a hash map when hashed insensitively using * the configured IRC casemapping. @@ -85,6 +85,31 @@ extern CoreExport const insp::casemap ascii_case_insensitive_map[256]; /** The currently configured casemapping. This defaults to \ref ascii_case_insensitive_map. */ extern CoreExport const insp::casemap* national_case_insensitive_map; +namespace insp +{ + /** Determines if \p str1 and \p str2 are equivalent when compared case insensitively using + * ASCII casemapping. + * @param str1 The first string to compare. + * @param str2 The second string to compare. + * @return True if \p str1 and \p str2 are equivalent; otherwise, false. + */ + inline bool ascii_equals(const std::string_view& str1, const std::string_view& str2) + { + return casemapped_equals(str1, str2, ascii_case_insensitive_map); + } + + /** Determines if \p str1 is lexographically less than \p str2 when compared insensitively using + * ASCII casemapping. + * @param str1 The first string to compare. + * @param str2 The second string to compare. + * @return True if \p str1 is ranked lower than \p str2; otherwise, false. + */ + inline bool ascii_less(const std::string_view& str1, const std::string_view& str2) + { + return casemapped_less(str1, str2, ascii_case_insensitive_map); + } +} + /** IRC casemapping variant of \p std::equal_to. */ template struct insp::casemapped_equals_obj diff --git a/include/configreader.h b/include/configreader.h index 22e6bd6b1..b7352fdca 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -149,7 +149,7 @@ public: return def; for (const auto& [enumkey, enumval] : enumvals) - if (strcasecmp(val.c_str(), enumkey) == 0) + if (insp::ascii_equals(val, enumkey)) return enumval; // Unfortunately we have to iterate this twice. diff --git a/include/modules/hash.h b/include/modules/hash.h index c25ef3fcd..ee9e7b5c7 100644 --- a/include/modules/hash.h +++ b/include/modules/hash.h @@ -275,7 +275,7 @@ inline bool Hash::CheckPassword(const std::string& password, const std::string& // The hash algorithm wasn't provided by any modules. If its plain // text then we can check it internally. - if (algorithm.empty() || insp::equalsci(algorithm, "plaintext")) + if (algorithm.empty() || insp::ascii_equals(algorithm, "plaintext")) return InspIRCd::TimingSafeCompare(password, value); ServerInstance->Logs.Debug("HASH", "Unable to check password hashed with an unknown algorithm: {}", algorithm); diff --git a/include/utility/string.h b/include/utility/string.h index dc432b0f2..d70990cf0 100644 --- a/include/utility/string.h +++ b/include/utility/string.h @@ -43,31 +43,4 @@ namespace insp joined.shrink_to_fit(); return joined; } - - /** Get underlying C string of the string passed as parameter. Useful in template functions. - * @param str A `const char*` string. - */ - inline const char* tocstr(const char* str) - { - return str; - } - - /** Get underlying C string of the string passed as parameter. Useful in template functions. - * @param str A `std::string` string. - */ - inline const char* tocstr(const std::string& str) - { - return str.c_str(); - } - - /** Check if two strings are equal case insensitively. - * @param str1 First string to compare. - * @param str2 Second string to compare. - * @return True if the strings are equal case-insensitively; otherwise, false. - */ - template - inline bool equalsci(const S1& str1, const S2& str2) - { - return (!strcasecmp(tocstr(str1), tocstr(str2))); - } } -- cgit v1.3.1-10-gc9f91