From 64d51b529091525e339bc3a9e548c920ec0cdf6c Mon Sep 17 00:00:00 2001 From: Richard Harrison Date: Fri, 18 Jan 2019 12:04:06 +0100 Subject: [PATCH] sg_file: Add compute hash method --- simgear/io/sg_file.cxx | 25 +++++++++++++++++++++++++ simgear/io/sg_file.hxx | 3 +++ 2 files changed, 28 insertions(+) diff --git a/simgear/io/sg_file.cxx b/simgear/io/sg_file.cxx index be629e7e..fcba988e 100644 --- a/simgear/io/sg_file.cxx +++ b/simgear/io/sg_file.cxx @@ -64,6 +64,31 @@ SGFile::SGFile( int existingFd ) : SGFile::~SGFile() { } +#include +#include +#include "simgear/misc/strutils.hxx" + +std::string SGFile::computeHash() +{ + if (!file_name.exists()) + return std::string(); + simgear::sha1nfo info; + sha1_init(&info); + char* buf = static_cast(malloc(1024 * 1024)); + size_t readLen; + SGBinaryFile f(file_name); + if (!f.open(SG_IO_IN)) { + throw sg_io_exception("Couldn't open file for compute hash", file_name); + } + while ((readLen = f.read(buf, 1024 * 1024)) > 0) { + sha1_write(&info, buf, readLen); + } + + f.close(); + free(buf); + std::string hashBytes((char*)sha1_result(&info), HASH_LENGTH); + return simgear::strutils::encodeHex(hashBytes); +} // open the file based on specified direction bool SGFile::open( const SGProtocolDir d ) { diff --git a/simgear/io/sg_file.hxx b/simgear/io/sg_file.hxx index 613f097c..267295b0 100644 --- a/simgear/io/sg_file.hxx +++ b/simgear/io/sg_file.hxx @@ -87,6 +87,9 @@ public: /** @return true of eof conditions exists */ virtual bool eof() const { return eof_flag; }; + + std::string SGFile::computeHash(); + }; class SGBinaryFile : public SGFile {