From 0494a72849eaed829956e9a40deb8bffaaeaae68 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Fri, 3 Mar 2023 13:19:12 +0100 Subject: [PATCH] Core/Stream: Add HashAppend overload --- include/Nazara/Core/Algorithm.hpp | 4 +-- include/Nazara/Core/Algorithm.inl | 8 +++--- include/Nazara/Core/Stream.hpp | 2 ++ src/Nazara/Core/Stream.cpp | 42 +++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/include/Nazara/Core/Algorithm.hpp b/include/Nazara/Core/Algorithm.hpp index 5a331bcd0..9fa3629df 100644 --- a/include/Nazara/Core/Algorithm.hpp +++ b/include/Nazara/Core/Algorithm.hpp @@ -23,8 +23,8 @@ namespace Nz { class ByteArray; - template ByteArray ComputeHash(HashType hash, const T& v); - template ByteArray ComputeHash(AbstractHash& hash, const T& v); + template ByteArray ComputeHash(HashType hash, T&& v); + template ByteArray ComputeHash(AbstractHash& hash, T&& v); inline bool HashAppend(AbstractHash* hash, const std::string_view& v); diff --git a/include/Nazara/Core/Algorithm.inl b/include/Nazara/Core/Algorithm.inl index 984a427a8..8dec7b1a4 100644 --- a/include/Nazara/Core/Algorithm.inl +++ b/include/Nazara/Core/Algorithm.inl @@ -30,9 +30,9 @@ namespace Nz * \see ComputeHash */ template - ByteArray ComputeHash(HashType hash, const T& v) + ByteArray ComputeHash(HashType hash, T&& v) { - return ComputeHash(*AbstractHash::Get(hash), v); + return ComputeHash(*AbstractHash::Get(hash), std::forward(v)); } /*! @@ -49,11 +49,11 @@ namespace Nz * \see ComputeHash */ template - ByteArray ComputeHash(AbstractHash& hash, const T& v) + ByteArray ComputeHash(AbstractHash& hash, T&& v) { hash.Begin(); - HashAppend(hash, v); + HashAppend(hash, std::forward(v)); return hash.End(); } diff --git a/include/Nazara/Core/Stream.hpp b/include/Nazara/Core/Stream.hpp index 640c5a507..fd29223ac 100644 --- a/include/Nazara/Core/Stream.hpp +++ b/include/Nazara/Core/Stream.hpp @@ -83,6 +83,8 @@ namespace Nz StreamOptionFlags m_streamOptions; UInt64 m_bufferCursor; }; + + NAZARA_CORE_API bool HashAppend(AbstractHash& hash, Stream& stream); } #include diff --git a/src/Nazara/Core/Stream.cpp b/src/Nazara/Core/Stream.cpp index 102f739d5..94e556449 100644 --- a/src/Nazara/Core/Stream.cpp +++ b/src/Nazara/Core/Stream.cpp @@ -281,4 +281,46 @@ namespace Nz NazaraError("Stream set the MemoryMapped option but did not implement GetMemoryMappedPointer"); return nullptr; } + + bool HashAppend(AbstractHash& hash, Stream& stream) + { + if (stream.IsMemoryMapped()) + { + const void* ptr = stream.GetMappedPointer(); + UInt64 size = stream.GetSize(); + if (ptr && size > 0) + { + hash.Append(static_cast(ptr), size); + return true; + } + } + + std::array content; + + // Save and restore cursor position after the call + std::size_t cursorPos = stream.GetCursorPos(); + CallOnExit restoreCursorPos([&] { stream.SetCursorPos(cursorPos); }); + + stream.SetCursorPos(0); + + while (!stream.EndOfStream()) + { + std::size_t readSize = stream.Read(&content[0], content.size()); + if (readSize > 0) + hash.Append(&content[0], readSize); + + if (readSize != content.size()) + { + if (!stream.EndOfStream()) + { + NazaraError("failed to read from stream"); + return false; + } + + break; + } + } + + return true; + } }