2021-11-10 19:24:17 +00:00
# include "hash.hpp"
2021-11-15 16:40:22 +00:00
# include <extern/smhasher/MurmurHash3.h>
2021-11-10 19:24:17 +00:00
2021-11-15 16:40:22 +00:00
# include <array>
2021-11-10 19:24:17 +00:00
# include <cstdint>
# include <istream>
# include <string>
namespace Files
{
2022-06-19 11:28:33 +00:00
std : : array < std : : uint64_t , 2 > getHash ( const std : : filesystem : : path & fileName , std : : istream & stream )
2021-11-10 19:24:17 +00:00
{
2021-11-15 16:40:22 +00:00
std : : array < std : : uint64_t , 2 > hash { 0 , 0 } ;
2021-11-10 19:24:17 +00:00
try
{
const auto start = stream . tellg ( ) ;
const auto exceptions = stream . exceptions ( ) ;
stream . exceptions ( std : : ios_base : : badbit ) ;
while ( stream )
{
2021-11-15 16:40:22 +00:00
std : : array < char , 4096 > value ;
stream . read ( value . data ( ) , value . size ( ) ) ;
const std : : streamsize read = stream . gcount ( ) ;
if ( read = = 0 )
break ;
std : : array < std : : uint64_t , 2 > blockHash { 0 , 0 } ;
MurmurHash3_x64_128 ( value . data ( ) , static_cast < int > ( read ) , hash . data ( ) , blockHash . data ( ) ) ;
hash = blockHash ;
2021-11-10 19:24:17 +00:00
}
stream . clear ( ) ;
2022-04-15 01:06:53 +00:00
stream . exceptions ( exceptions ) ;
2021-11-10 19:24:17 +00:00
stream . seekg ( start ) ;
}
catch ( const std : : exception & e )
{
2022-06-19 11:28:33 +00:00
throw std : : runtime_error ( " Error while reading \" " + fileName . string ( ) + " \" to get hash: " + std : : string ( e . what ( ) ) ) ; //TODO(Project579): This will probably break in windows with unicode paths
2021-11-10 19:24:17 +00:00
}
return hash ;
}
}