From 9e9b005b3be7fb9f0d9b4e739282f32d6ece53ac Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 2 Dec 2017 16:44:38 +0100 Subject: [PATCH] Changed: Centralize hexadecimal conversions --HG-- branch : develop --- code/nel/include/nel/misc/common.h | 20 ++++ code/nel/src/misc/common.cpp | 103 ++++++++++++++++++ code/nel/src/misc/md5.cpp | 41 +------ .../common/src/game_share/entity_types.h | 11 -- 4 files changed, 125 insertions(+), 50 deletions(-) diff --git a/code/nel/include/nel/misc/common.h b/code/nel/include/nel/misc/common.h index 180139ec0..eb70fce9e 100644 --- a/code/nel/include/nel/misc/common.h +++ b/code/nel/include/nel/misc/common.h @@ -233,6 +233,26 @@ char toLower ( const char ch ); // convert only one character std::string toUpper ( const std::string &str); void toUpper ( char *str); + +/** + * Convert to an hexadecimal std::string + */ +std::string toHexa(const uint8 &b); +std::string toHexa(const uint8 *data, uint size); +std::string toHexa(const std::string &str); +std::string toHexa(const char *str); + +/** +* Convert from an hexadecimal std::string +*/ +bool fromHexa(const std::string &hexa, uint8 &b); +bool fromHexa(const std::string &hexa, uint8 *data); +bool fromHexa(const std::string &hexa, std::string &str); +bool fromHexa(const char *hexa, uint8 &b); +bool fromHexa(const char *hexa, uint8 *data); +bool fromHexa(const char *hexa, std::string &str); +bool fromHexa(const char hexa, uint8 &b); + // Remove all the characters <= 32 (tab, space, new line, return, vertical tab etc..) at the beginning and at the end of a string template T trim (const T &str) { diff --git a/code/nel/src/misc/common.cpp b/code/nel/src/misc/common.cpp index 23b02e668..bb6badd18 100644 --- a/code/nel/src/misc/common.cpp +++ b/code/nel/src/misc/common.cpp @@ -662,6 +662,109 @@ void toUpper(char *str) } } +std::string toHexa(const uint8 &b) +{ + return toString("%02hhx", b); +} + +std::string toHexa(const uint8 *data, uint size) +{ + std::string res; + + // hexadecimal string will be always twice the original size + res.reserve(size * 2); + + // process each byte + for (uint i = 0; i < size; ++i) + { + res += toHexa(data[i]); + } + + return res; +} + +std::string toHexa(const std::string &str) +{ + return toHexa((uint8*)str.c_str(), (uint)str.length()); +} + +std::string toHexa(const char *str) +{ + return toHexa((uint8*)str, (uint)strlen(str)); +} + +bool fromHexa(const std::string &hexa, uint8 &b) +{ + return fromHexa(hexa.c_str(), b); +} + +bool fromHexa(const std::string &hexa, uint8 *data) +{ + return fromHexa(hexa.c_str(), data); +} + +bool fromHexa(const std::string &hexa, std::string &str) +{ + return fromHexa(hexa.c_str(), str); +} + +bool fromHexa(const char *hexa, uint8 &b) +{ + char c1 = *hexa; + char c2 = *(hexa+1); + uint8 x1, x2; + if (!fromHexa(c1, x1)) return false; + if (!fromHexa(c2, x2)) return false; + + b = (x1 << 4) | x2; + + return true; +} + +bool fromHexa(const char *hexa, uint8 *data) +{ + // length of the string + uint len = strlen(hexa); + + // process each byte + for (uint i = 0; i < len; i += 2) + { + if (!fromHexa(hexa + i, *(data++))) return false; + } + + return true; +} + +bool fromHexa(const char *hexa, std::string &str) +{ + str.resize(strlen(hexa) * 2); + + return fromHexa(hexa, (uint8*)str.c_str()); +} + +bool fromHexa(const char hexa, uint8 &b) +{ + if (hexa >= '0' && hexa <= '9') + { + b = hexa - '0'; + return true; + } + + if (hexa >= 'A' && hexa <= 'F') + { + b = hexa - 'A' + 10; + return true; + } + + if (hexa >= 'a' && hexa <= 'f') + { + b = hexa - 'a' + 10; + return true; + } + + return false; +} + std::string formatThousands(const std::string& s) { sint i, k; diff --git a/code/nel/src/misc/md5.cpp b/code/nel/src/misc/md5.cpp index eaf9df6cc..052bfa0b2 100644 --- a/code/nel/src/misc/md5.cpp +++ b/code/nel/src/misc/md5.cpp @@ -118,31 +118,6 @@ CHashKeyMD5 getMD5(const uint8 *buffer, uint32 size) return Message_Digest; } -// **************************************************************************** -// Helper -// **************************************************************************** -static bool fromHex(char c, uint8 &x) -{ - if (c >= '0' && c <= '9') - { - x = c - '0'; - return true; - } - else if (c >= 'A' && c <= 'F') - { - x = c - 'A' + 10; - return true; - } - else if (c >= 'a' && c <= 'f') - { - x = c - 'a' + 10; - return true; - } - - nlwarning("cannot convert to hexa"); - return false; -} - // **************************************************************************** // **************************************************************************** // CHashKeyMD5 @@ -159,10 +134,7 @@ void CHashKeyMD5::clear() // **************************************************************************** string CHashKeyMD5::toString() const { - string sTmp; - for (uint32 i = 0; i < 16; ++i) - sTmp += NLMISC::toString("%02x", Data[i]); - return sTmp; + return toHexa(Data, 16); } // **************************************************************************** @@ -174,16 +146,7 @@ bool CHashKeyMD5::fromString(const std::string &in) return false; } - for (uint32 i = 0; i < 16; ++i) - { - char c1 = in[2*i]; - char c2 = in[2*i+1]; - uint8 x1, x2; - if (!fromHex(c1, x1)) return false; - if (!fromHex(c2, x2)) return false; - Data[i] = (x1 << 4) | x2; - } - return true; + return fromHexa(in, Data); } // **************************************************************************** diff --git a/code/ryzom/common/src/game_share/entity_types.h b/code/ryzom/common/src/game_share/entity_types.h index 189e7c803..23f4bd700 100644 --- a/code/ryzom/common/src/game_share/entity_types.h +++ b/code/ryzom/common/src/game_share/entity_types.h @@ -560,17 +560,6 @@ inline CLFECOMMON::TCoord getAbsoluteCoordinateFrom64( uint64 posvalue ) }*/ -inline std::string toHexaString(const std::vector &v) -{ - std::string res; - for (uint i = 0; i < v.size(); i++) - { - res += NLMISC::toString("%x",v[i]); - } - return res; -} - - #endif // NL_ENTITY_TYPES_H /* End of entity_types.h */