commit ca8d4522cc5dd57ac514ff90cc0647c487f667ce parent 34dbbd856c954022b7b3dc71c8e6b621cc6a2fc5 Author: Michael Savage <mikejsavage@gmail.com> Date: Tue May 30 03:10:03 +0300 Some simplifications in launcher Diffstat:
launcher/main.cc | | | 40 | ++++++++++++++++++++++++++-------------- |
diff --git a/launcher/main.cc b/launcher/main.cc @@ -104,9 +104,7 @@ static bool parse_version( Version * v, const char * str ) { return true; } -static bool parse_manifest( std::map< std::string, ManifestEntry > * manifest_ptr, const char * data ) { - std::map< std::string, ManifestEntry > & manifest = *manifest_ptr; - +static bool parse_manifest( std::map< std::string, ManifestEntry > & manifest, const char * data ) { for( array< array< const char > > line : gmatch( data, "([^\n]+)" ) ) { ASSERT( line.n == 1 ); @@ -298,7 +296,7 @@ static WORK_QUEUE_CALLBACK( download_remote_version ) { glfwPostEmptyEvent(); } -static int hex2dec( char hex ) { +static u8 hex2dec( char hex ) { if( hex == '0' ) return 0; if( hex == '1' ) return 1; if( hex == '2' ) return 2; @@ -317,7 +315,26 @@ static int hex2dec( char hex ) { if( hex == 'e' || hex == 'E' ) return 14; if( hex == 'f' || hex == 'F' ) return 15; - return -1; + return 255; +} + +bool parse_hex( const array< const char > hex, u8 * bytes ) { + if( hex.n % 2 != 0 ) { + return false; + } + + for( size_t i = 0; i < hex.n / 2; i++ ) { + u8 a = hex2dec( hex[ i * 2 ] ); + u8 b = hex2dec( hex[ i * 2 + 1 ] ); + + if( a == 255 || b == 255 ) { + return false; + } + + bytes[ i ] = a * 16 + b; + } + + return true; } static WORK_QUEUE_CALLBACK( download_manifest ) { @@ -347,13 +364,8 @@ static WORK_QUEUE_CALLBACK( download_manifest ) { // check the manifest signature is ok { u8 signature[ 64 ]; - for( size_t i = 0; i < sizeof( signature ); i++ ) { - int a = hex2dec( body[ i * 2 ] ); - int b = hex2dec( body[ i * 2 + 1 ] ); - - if( a == -1 || b == -1 ) return; - - signature[ i ] = checked_cast< u8 >( a * 16 + b ); + if( !parse_hex( array< const char >( body.c_str(), 128 ), signature ) ) { + return; } manifest_str = body.c_str() + 129; @@ -363,7 +375,7 @@ static WORK_QUEUE_CALLBACK( download_manifest ) { if( signature_ok != 0 ) return; } - bool parse_ok = parse_manifest( &updater->remote_manifest, manifest_str ); + bool parse_ok = parse_manifest( updater->remote_manifest, manifest_str ); if( !parse_ok ) return; // look for files that have changed and files that should be removed @@ -566,7 +578,7 @@ int main() { Updater * updater = SCOPED_ACQUIRE( locked_updater ); updater->retry_at = get_time(); parse_version( &updater->local_version, local_version_str ); - parse_manifest( &updater->local_manifest, local_manifest_str ); + parse_manifest( updater->local_manifest, local_manifest_str ); } // char name[ 64 ] = "travis_rizzle";