From 986a84fa5fe4f068d90347e6b37e27472b3027fc Mon Sep 17 00:00:00 2001 From: Jelle <60387522+JelleInfinity@users.noreply.github.com> Date: Mon, 17 Oct 2022 12:40:30 +0200 Subject: [PATCH] Add prefix support --- pkNX.Containers/Misc/FnvHashDecoder.cs | 11 ++++------- pkNX.HashDecoder/HashDecoderLib.h | 11 +++++++---- pkNX.HashDecoderConsole/HashDecoder.cpp | 14 +++++++++++--- pkNX.HashDecoderConsole/HashDecoder.h | 2 +- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/pkNX.Containers/Misc/FnvHashDecoder.cs b/pkNX.Containers/Misc/FnvHashDecoder.cs index d442ab67..1acced15 100644 --- a/pkNX.Containers/Misc/FnvHashDecoder.cs +++ b/pkNX.Containers/Misc/FnvHashDecoder.cs @@ -21,15 +21,13 @@ public class FnvHashDecoder private const ulong kFnvPrime_64 = 0x00000100000001b3; private const ulong kOffsetBasis_64 = 0xCBF29CE484222645; - public Dictionary> FoundKeys = new(); + public Dictionary FoundKeys = new(); - public Dictionary> Run(int minLength, int maxLength) + public Dictionary Run(int minLength, int maxLength) { Debug.Assert(FnvHash.HashFnv1a_64("stickyball") == 0x15cb1f582d2b05ab); Debug.Assert(FnvHash.HashFnv1a_64("enigma") == 0xC75DDBE25402B11C); - int length = maxLength; - var tasks = new List(); for (int i = 0; i < FnvHashDecoderLib.AllowedCharCount; ++i) @@ -37,14 +35,13 @@ public class FnvHashDecoder int startChar = i; tasks.Add(Task.Run(() => { - var dict = FnvHashDecoderLib.StartDecoding(length, startChar); + var dict = FnvHashDecoderLib.StartDecoding(15, startChar, "pm0025_"); if (dict.Count != 0) { foreach (var x in dict) { - FoundKeys.TryAdd(x.Key, new()); - FoundKeys[x.Key].Add(x.Value); + FoundKeys.TryAdd(x.Key, x.Value); Debug.WriteLine($"{x.Key} {x.Value}"); } } diff --git a/pkNX.HashDecoder/HashDecoderLib.h b/pkNX.HashDecoder/HashDecoderLib.h index d298cca0..18da0399 100644 --- a/pkNX.HashDecoder/HashDecoderLib.h +++ b/pkNX.HashDecoder/HashDecoderLib.h @@ -1,15 +1,17 @@ #pragma once #include "../pkNX.HashDecoderConsole/HashDecoder.h" +#include + using namespace System; namespace pkNXHashDecoder { private ref class FnvHashDecoderWrapper { public: - std::unordered_map StartDecoding(int length, int startChar) + std::unordered_map StartDecoding(int length, int startChar, std::string_view prefix) { - return nativeHandle_->StartDecoding(length, startChar); + return nativeHandle_->StartDecoding(length, startChar, prefix); } property int AllowedCharCount @@ -27,10 +29,11 @@ namespace pkNXHashDecoder { public ref class FnvHashDecoderLib { public: - static System::Collections::Generic::Dictionary^ StartDecoding(int length, int startChar) + static System::Collections::Generic::Dictionary^ StartDecoding(int length, int startChar, System::String^ prefix) { FnvHashDecoderWrapper decoder{}; - auto map = decoder.StartDecoding(length, startChar); + const std::string p = msclr::interop::marshal_as(prefix); + const auto map = decoder.StartDecoding(length, startChar, p); auto dict = gcnew System::Collections::Generic::Dictionary(); diff --git a/pkNX.HashDecoderConsole/HashDecoder.cpp b/pkNX.HashDecoderConsole/HashDecoder.cpp index f437f67a..19e36c2d 100644 --- a/pkNX.HashDecoderConsole/HashDecoder.cpp +++ b/pkNX.HashDecoderConsole/HashDecoder.cpp @@ -16,7 +16,7 @@ std::string FnvHashDecoder::BuildString(const size_t length) const return { chars.begin(), chars.end() }; } -std::unordered_map FnvHashDecoder::StartDecoding(const int length, const int startChar) +std::unordered_map FnvHashDecoder::StartDecoding(const int length, const int startChar, const std::string_view prefix) { std::unordered_map foundKeys; @@ -24,8 +24,16 @@ std::unordered_map FnvHashDecoder::StartDecoding(const in 0xC75DDBE25402B11C, }; + const size_t startIndex = prefix.length(); + // Reset all chars to starting point - charIndex[0] = (uint32_t)startChar; + for (int i = 0; i < prefix.length(); ++i) + { + const auto& ch = prefix[i]; + charIndex[i] = AllowedChars.find_first_of(ch); + } + + charIndex[startIndex] = (uint32_t)startChar; // Build the hash of all chars except the last one hashBases[0] = kOffsetBasis_64; @@ -39,7 +47,7 @@ std::unordered_map FnvHashDecoder::StartDecoding(const in } //LoopTimer timer{}; - while (charIndex[0] == startChar) + while (charIndex[startIndex] == startChar) { // Loop through all AllowedChars const uint64_t hashBase = hashBases[length - 1]; diff --git a/pkNX.HashDecoderConsole/HashDecoder.h b/pkNX.HashDecoderConsole/HashDecoder.h index f3453463..f0111fa0 100644 --- a/pkNX.HashDecoderConsole/HashDecoder.h +++ b/pkNX.HashDecoderConsole/HashDecoder.h @@ -5,7 +5,7 @@ namespace pkNXHashDecoder { class __declspec(align(64)) FnvHashDecoder { public: - std::unordered_map StartDecoding(int length, int startChar); + std::unordered_map StartDecoding(int length, int startChar, std::string_view prefix); constexpr static int MAX_LENGTH = 128; constexpr static std::string_view AllowedChars = "etaoinshrdlcumw_0123456789fgypbvkjxqz/."; // Sorted by most common first