From 23ea7ecbcc7687cb3814bf38e6be80fd4b4a4d7a Mon Sep 17 00:00:00 2001 From: Andrey Akhmichin Date: Thu, 3 Sep 2020 20:58:57 +0500 Subject: [PATCH] public: optimize MD5_Print function. --- public/crclib.c | 6 +----- public/crtlib.c | 28 ++++++++++++++++++++++++++++ public/crtlib.h | 2 ++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/public/crclib.c b/public/crclib.c index 9dbae361..e31beb8d 100644 --- a/public/crclib.c +++ b/public/crclib.c @@ -438,16 +438,12 @@ transform hash to hexadecimal printable symbols char *MD5_Print( byte hash[16] ) { static char szReturn[64]; - char szChunk[10]; int i; memset( szReturn, 0, 64 ); for( i = 0; i < 16; i++ ) - { - Q_snprintf( szChunk, sizeof( szChunk ), "%02X", hash[i] ); - Q_strncat( szReturn, szChunk, sizeof( szReturn )); - } + COM_Hex2String( hash[i], &szReturn[i * 2] ); return szReturn; } diff --git a/public/crtlib.c b/public/crtlib.c index bd67d490..27d8d81d 100644 --- a/public/crtlib.c +++ b/public/crtlib.c @@ -918,6 +918,33 @@ void COM_PathSlashFix( char *path ) Q_strcpy( &path[len], "/" ); } +/* +============ +COM_Hex2Char +============ +*/ +char COM_Hex2Char( uint8_t hex ) +{ + if( hex >= 0x0 && hex <= 0x9 ) + hex += '0'; + else if( hex >= 0xA && hex <= 0xF ) + hex += '7'; + + return (char)hex; +} + +/* +============ +COM_Hex2String +============ +*/ +void COM_Hex2String( uint8_t hex, char *str ) +{ + *str++ = COM_Hex2Char( hex >> 4 ); + *str++ = COM_Hex2Char( hex & 0x0F ); + *str = '\0'; +} + int matchpattern( const char *in, const char *pattern, qboolean caseinsensitive ) { return matchpattern_with_separator( in, pattern, caseinsensitive, "/\\:", false ); @@ -983,3 +1010,4 @@ int matchpattern_with_separator( const char *in, const char *pattern, qboolean c return 0; // reached end of pattern but not end of input return 1; // success } + diff --git a/public/crtlib.h b/public/crtlib.h index d5c71f21..f64db6f2 100644 --- a/public/crtlib.h +++ b/public/crtlib.h @@ -84,6 +84,8 @@ const char *COM_FileWithoutPath( const char *in ); void COM_StripExtension( char *path ); void COM_RemoveLineFeed( char *str ); void COM_PathSlashFix( char *path ); +char COM_Hex2Char( uint8_t hex ); +void COM_Hex2String( uint8_t hex, char *str ); #define COM_CheckString( string ) ( ( !string || !*string ) ? 0 : 1 ) int matchpattern( const char *in, const char *pattern, qboolean caseinsensitive ); int matchpattern_with_separator( const char *in, const char *pattern, qboolean caseinsensitive, const char *separators, qboolean wildcard_least_one );