diff options
| author | Stefan Boberg <[email protected]> | 2024-12-13 12:11:16 +0100 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2024-12-13 12:11:16 +0100 |
| commit | e4a7e9b30023c1c5195de9e78f02c075ff176460 (patch) | |
| tree | aaffd1e1954fba67f34969d2c2ead64d19d9d75f /src | |
| parent | added ComputeOpKey so all instances of mapping key -> Oid is in a single place (diff) | |
| download | zen-e4a7e9b30023c1c5195de9e78f02c075ff176460.tar.xz zen-e4a7e9b30023c1c5195de9e78f02c075ff176460.zip | |
fixed XXH3_128Stream so it initializes the state properly
the old version is still present for now, with a _deprecated suffix
Diffstat (limited to 'src')
| -rw-r--r-- | src/zencore/include/zencore/xxhash.h | 31 | ||||
| -rw-r--r-- | src/zencore/xxhash.cpp | 12 | ||||
| -rw-r--r-- | src/zenserver-test/zenserver-test.cpp | 2 | ||||
| -rw-r--r-- | src/zenserver/projectstore/projectstore.cpp | 2 |
4 files changed, 42 insertions, 5 deletions
diff --git a/src/zencore/include/zencore/xxhash.h b/src/zencore/include/zencore/xxhash.h index 94d98be98..1616e5f93 100644 --- a/src/zencore/include/zencore/xxhash.h +++ b/src/zencore/include/zencore/xxhash.h @@ -61,8 +61,10 @@ struct XXH3_128 struct XXH3_128Stream { + XXH3_128Stream() { Reset(); } + /// Begin streaming hash compute (not needed on freshly constructed instance) - void Reset() { memset(&m_State, 0, sizeof m_State); } + void Reset() { XXH3_128bits_reset(&m_State); } /// Append another chunk XXH3_128Stream& Append(const void* Data, size_t ByteCount) @@ -83,6 +85,33 @@ struct XXH3_128Stream } private: + XXH3_state_s m_State; +}; + +struct XXH3_128Stream_deprecated +{ + /// Begin streaming hash compute (not needed on freshly constructed instance) + void Reset() { memset(&m_State, 0, sizeof m_State); } + + /// Append another chunk + XXH3_128Stream_deprecated& Append(const void* Data, size_t ByteCount) + { + XXH3_128bits_update(&m_State, Data, ByteCount); + return *this; + } + + /// Append another chunk + XXH3_128Stream_deprecated& Append(MemoryView Data) { return Append(Data.GetData(), Data.GetSize()); } + + /// Obtain final hash. If you wish to reuse the instance call reset() + XXH3_128 GetHash() + { + XXH3_128 Hash; + XXH128_canonicalFromHash((XXH128_canonical_t*)Hash.Hash, XXH3_128bits_digest(&m_State)); + return Hash; + } + +private: XXH3_state_s m_State{}; }; diff --git a/src/zencore/xxhash.cpp b/src/zencore/xxhash.cpp index ff88a4372..80d7bc8fd 100644 --- a/src/zencore/xxhash.cpp +++ b/src/zencore/xxhash.cpp @@ -69,12 +69,20 @@ TEST_CASE("XXH3_128") "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"}; + SUBCASE("short_deprecated") + { + XXH3_128Stream_deprecated x; + x.Append(ShortString.data(), ShortString.size()); + const XXH3_128 Hash = x.GetHash(); + CHECK(Hash == XXH3_128::FromHexString("0d44dd7fde8ea2b4ba961e1a26f71f21"sv)); + } + SUBCASE("short") { XXH3_128Stream x; x.Append(ShortString.data(), ShortString.size()); const XXH3_128 Hash = x.GetHash(); - CHECK(Hash == XXH3_128::FromHexString("0d44dd7fde8ea2b4ba961e1a26f71f21"sv)); + CHECK(Hash == XXH3_128::FromHexString("9a4dea864648af82823c8c03e6dd2202"sv)); } SUBCASE("long") @@ -82,7 +90,7 @@ TEST_CASE("XXH3_128") XXH3_128Stream x; x.Append(LongString.data(), LongString.size()); const XXH3_128 Hash = x.GetHash(); - CHECK(Hash == XXH3_128::FromHexString("bc408748826fb22da051517c97c9d181"sv)); + CHECK(Hash == XXH3_128::FromHexString("fbd5e72f7a5894590d1ef49dfcc58b7d"sv)); } } diff --git a/src/zenserver-test/zenserver-test.cpp b/src/zenserver-test/zenserver-test.cpp index 33c4f99cf..ec288f1dc 100644 --- a/src/zenserver-test/zenserver-test.cpp +++ b/src/zenserver-test/zenserver-test.cpp @@ -2868,7 +2868,7 @@ TEST_CASE("project.remote") auto ComputeOpKey = [](const CbObjectView& Op) -> Oid { using namespace std::literals; - XXH3_128Stream KeyHasher; + XXH3_128Stream_deprecated KeyHasher; Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); }); XXH3_128 KeyHash128 = KeyHasher.GetHash(); diff --git a/src/zenserver/projectstore/projectstore.cpp b/src/zenserver/projectstore/projectstore.cpp index d153e7274..1257ac8a8 100644 --- a/src/zenserver/projectstore/projectstore.cpp +++ b/src/zenserver/projectstore/projectstore.cpp @@ -412,7 +412,7 @@ ComputeOpKey(const CbObjectView& Op) { using namespace std::literals; - XXH3_128Stream KeyHasher; + XXH3_128Stream_deprecated KeyHasher; Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); }); XXH3_128 KeyHash128 = KeyHasher.GetHash(); |