updated read binary file so its fast now...

master
_xeroxz 3 years ago
parent fdcafdbbcb
commit db7526d989

@ -4,6 +4,7 @@
#include <algorithm> #include <algorithm>
#include <cstdint> #include <cstdint>
#include <filesystem>
#include <fstream> #include <fstream>
#include <functional> #include <functional>
#include <map> #include <map>
@ -69,20 +70,20 @@ namespace xtils
auto open_binary_file( const std::string &file, std::vector< uint8_t > &data ) -> bool 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; return false;
fstr.unsetf( std::ios::skipws ); OFSTRUCT of;
fstr.seekg( 0, std::ios::end ); 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 ); DWORD bytes_read;
data.reserve( static_cast< uint32_t >( file_size ) ); data.resize( file_size );
data.insert( data.begin(), std::istream_iterator< uint8_t >( fstr ), std::istream_iterator< uint8_t >() ); return ReadFile( ( HANDLE )hfile, data.data(), file_size, &bytes_read, nullptr );
return true;
} }
auto image_base( const char *image_path ) -> std::uintptr_t auto image_base( const char *image_path ) -> std::uintptr_t
@ -95,7 +96,7 @@ namespace xtils
return NT_HEADER( image_header )->OptionalHeader.ImageBase; 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 ]; char image_header[ PAGE_4K ];
std::ifstream file( image_path, std::ios::binary ); std::ifstream file( image_path, std::ios::binary );

Loading…
Cancel
Save