From db7526d989bdfecb6fac2079929efce94cead52c Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sat, 28 Aug 2021 13:41:32 -0700 Subject: [PATCH] updated read binary file so its fast now... --- xtils.hpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/xtils.hpp b/xtils.hpp index 8d53adc..68f92ab 100644 --- a/xtils.hpp +++ b/xtils.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -69,20 +70,20 @@ namespace xtils auto open_binary_file( const std::string &file, std::vector< uint8_t > &data ) -> bool { - std::ifstream fstr( file, std::ios::binary ); + auto file_size = std::filesystem::file_size( std::filesystem::path( file ) ); - if ( !fstr.is_open() ) + if ( !file_size ) return false; - fstr.unsetf( std::ios::skipws ); - fstr.seekg( 0, std::ios::end ); + OFSTRUCT of; + auto hfile = OpenFile( file.c_str(), &of, NULL ); - const auto file_size = fstr.tellg(); + if ( ( HANDLE )hfile == INVALID_HANDLE_VALUE ) + return false; - fstr.seekg( NULL, std::ios::beg ); - data.reserve( static_cast< uint32_t >( file_size ) ); - data.insert( data.begin(), std::istream_iterator< uint8_t >( fstr ), std::istream_iterator< uint8_t >() ); - return true; + DWORD bytes_read; + data.resize( file_size ); + return ReadFile( ( HANDLE )hfile, data.data(), file_size, &bytes_read, nullptr ); } auto image_base( const char *image_path ) -> std::uintptr_t @@ -95,7 +96,7 @@ namespace xtils return NT_HEADER( image_header )->OptionalHeader.ImageBase; } - auto image_size(const char* image_path) -> std::uintptr_t + auto image_size( const char *image_path ) -> std::uintptr_t { char image_header[ PAGE_4K ]; std::ifstream file( image_path, std::ios::binary );