aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/iohash.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2023-05-02 10:01:47 +0200
committerGitHub <[email protected]>2023-05-02 10:01:47 +0200
commit075d17f8ada47e990fe94606c3d21df409223465 (patch)
treee50549b766a2f3c354798a54ff73404217b4c9af /src/zencore/iohash.cpp
parentfix: bundle shouldn't append content zip to zen (diff)
downloadzen-075d17f8ada47e990fe94606c3d21df409223465.tar.xz
zen-075d17f8ada47e990fe94606c3d21df409223465.zip
moved source directories into `/src` (#264)
* moved source directories into `/src` * updated bundle.lua for new `src` path * moved some docs, icon * removed old test trees
Diffstat (limited to 'src/zencore/iohash.cpp')
-rw-r--r--src/zencore/iohash.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/zencore/iohash.cpp b/src/zencore/iohash.cpp
new file mode 100644
index 000000000..77076c133
--- /dev/null
+++ b/src/zencore/iohash.cpp
@@ -0,0 +1,87 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zencore/iohash.h>
+
+#include <zencore/blake3.h>
+#include <zencore/compositebuffer.h>
+#include <zencore/string.h>
+#include <zencore/testing.h>
+
+#include <gsl/gsl-lite.hpp>
+
+namespace zen {
+
+const IoHash IoHash::Zero{}; // Initialized to all zeros
+
+IoHash
+IoHash::HashBuffer(const void* data, size_t byteCount)
+{
+ BLAKE3 b3 = BLAKE3::HashMemory(data, byteCount);
+
+ IoHash io;
+ memcpy(io.Hash, b3.Hash, sizeof io.Hash);
+
+ return io;
+}
+
+IoHash
+IoHash::HashBuffer(const CompositeBuffer& Buffer)
+{
+ IoHashStream Hasher;
+
+ for (const SharedBuffer& Segment : Buffer.GetSegments())
+ {
+ Hasher.Append(Segment.GetData(), Segment.GetSize());
+ }
+
+ return Hasher.GetHash();
+}
+
+IoHash
+IoHash::FromHexString(const char* string)
+{
+ return FromHexString({string, sizeof(IoHash::Hash) * 2});
+}
+
+IoHash
+IoHash::FromHexString(std::string_view string)
+{
+ ZEN_ASSERT(string.size() == 2 * sizeof(IoHash::Hash));
+
+ IoHash io;
+
+ ParseHexBytes(string.data(), string.size(), io.Hash);
+
+ return io;
+}
+
+const char*
+IoHash::ToHexString(char* outString /* 40 characters + NUL terminator */) const
+{
+ ToHexBytes(Hash, sizeof(IoHash), outString);
+ outString[2 * sizeof(IoHash)] = '\0';
+
+ return outString;
+}
+
+StringBuilderBase&
+IoHash::ToHexString(StringBuilderBase& outBuilder) const
+{
+ String_t Str;
+ ToHexString(Str);
+
+ outBuilder.AppendRange(Str, &Str[StringLength]);
+
+ return outBuilder;
+}
+
+std::string
+IoHash::ToHexString() const
+{
+ String_t Str;
+ ToHexString(Str);
+
+ return Str;
+}
+
+} // namespace zen