diff --git a/lib/libimhex/include/hex/helpers/encoding_file.hpp b/lib/libimhex/include/hex/helpers/encoding_file.hpp index 6398dd772..71c93a652 100644 --- a/lib/libimhex/include/hex/helpers/encoding_file.hpp +++ b/lib/libimhex/include/hex/helpers/encoding_file.hpp @@ -28,8 +28,9 @@ namespace hex { EncodingFile& operator=(EncodingFile &&other) noexcept; [[nodiscard]] std::pair getEncodingFor(std::span buffer) const; - [[nodiscard]] size_t getEncodingLengthFor(std::span buffer) const; - [[nodiscard]] size_t getLongestSequence() const { return m_longestSequence; } + [[nodiscard]] u64 getEncodingLengthFor(std::span buffer) const; + [[nodiscard]] u64 getShortestSequence() const { return m_shortestSequence; } + [[nodiscard]] u64 getLongestSequence() const { return m_longestSequence; } [[nodiscard]] bool valid() const { return m_valid; } @@ -45,7 +46,9 @@ namespace hex { std::string m_name; std::string m_tableContent; std::unique_ptr, std::string>>> m_mapping; - size_t m_longestSequence = 0; + + u64 m_shortestSequence = std::numeric_limits::max(); + u64 m_longestSequence = std::numeric_limits::min(); }; } diff --git a/lib/libimhex/source/helpers/encoding_file.cpp b/lib/libimhex/source/helpers/encoding_file.cpp index 09c1b48bd..24152b207 100644 --- a/lib/libimhex/source/helpers/encoding_file.cpp +++ b/lib/libimhex/source/helpers/encoding_file.cpp @@ -15,6 +15,7 @@ namespace hex { m_mapping = std::make_unique, std::string>>>(*other.m_mapping); m_tableContent = other.m_tableContent; m_longestSequence = other.m_longestSequence; + m_shortestSequence = other.m_shortestSequence; m_valid = other.m_valid; m_name = other.m_name; } @@ -23,6 +24,7 @@ namespace hex { m_mapping = std::move(other.m_mapping); m_tableContent = std::move(other.m_tableContent); m_longestSequence = other.m_longestSequence; + m_shortestSequence = other.m_shortestSequence; m_valid = other.m_valid; m_name = std::move(other.m_name); } @@ -66,6 +68,7 @@ namespace hex { m_mapping = std::make_unique, std::string>>>(*other.m_mapping); m_tableContent = other.m_tableContent; m_longestSequence = other.m_longestSequence; + m_shortestSequence = other.m_shortestSequence; m_valid = other.m_valid; m_name = other.m_name; @@ -76,6 +79,7 @@ namespace hex { m_mapping = std::move(other.m_mapping); m_tableContent = std::move(other.m_tableContent); m_longestSequence = other.m_longestSequence; + m_shortestSequence = other.m_shortestSequence; m_valid = other.m_valid; m_name = std::move(other.m_name); @@ -144,6 +148,7 @@ namespace hex { (*m_mapping)[keySize].insert({ std::move(fromBytes), to }); m_longestSequence = std::max(m_longestSequence, keySize); + m_shortestSequence = std::min(m_shortestSequence, keySize); } } diff --git a/plugins/ui/source/ui/hex_editor.cpp b/plugins/ui/source/ui/hex_editor.cpp index 130725e50..73de1655b 100644 --- a/plugins/ui/source/ui/hex_editor.cpp +++ b/plugins/ui/source/ui/hex_editor.cpp @@ -592,14 +592,12 @@ namespace hex::ui { m_encodingLineStartAddresses.push_back(0); } - if (size_t(y) < m_encodingLineStartAddresses.size()) { + const bool singleByteEncoding = m_currCustomEncoding->getLongestSequence() == 1 && m_currCustomEncoding->getShortestSequence() == 1; + if (size_t(y) < m_encodingLineStartAddresses.size() || singleByteEncoding) { std::vector> encodingData; - if (m_encodingLineStartAddresses[y] >= m_bytesPerRow) { - encodingData.emplace_back(y * m_bytesPerRow + m_provider->getBaseAddress() + m_provider->getCurrentPageAddress(), CustomEncodingData(".", 1, ImGuiExt::GetCustomColorU32(ImGuiCustomCol_AdvancedEncodingUnknown))); - m_encodingLineStartAddresses.push_back(0); - } else { - u32 offset = m_encodingLineStartAddresses[y]; + if (singleByteEncoding) { + u64 offset = 0; do { const u64 address = y * m_bytesPerRow + offset + m_provider->getBaseAddress() + m_provider->getCurrentPageAddress(); @@ -608,8 +606,23 @@ namespace hex::ui { offset += result.advance; encodingData.emplace_back(address, result); } while (offset < m_bytesPerRow); + } else { + if (m_encodingLineStartAddresses[y] >= m_bytesPerRow) { + encodingData.emplace_back(y * m_bytesPerRow + m_provider->getBaseAddress() + m_provider->getCurrentPageAddress(), CustomEncodingData(".", 1, ImGuiExt::GetCustomColorU32(ImGuiCustomCol_AdvancedEncodingUnknown))); + m_encodingLineStartAddresses.push_back(0); + } else { + u64 offset = m_encodingLineStartAddresses[y]; + do { + const u64 address = y * m_bytesPerRow + offset + m_provider->getBaseAddress() + m_provider->getCurrentPageAddress(); - m_encodingLineStartAddresses.push_back(offset - m_bytesPerRow); + auto result = queryCustomEncodingData(m_provider, *m_currCustomEncoding, address); + + offset += result.advance; + encodingData.emplace_back(address, result); + } while (offset < m_bytesPerRow); + + m_encodingLineStartAddresses.push_back(offset - m_bytesPerRow); + } } ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(0, 0));