1
0
mirror of synced 2024-12-23 21:14:51 +01:00
ImHex/lib/libimhex/source/helpers/encoding_file.cpp

155 lines
4.7 KiB
C++
Raw Normal View History

2021-12-07 22:47:41 +01:00
#include <hex/helpers/encoding_file.hpp>
#include <hex/helpers/utils.hpp>
2023-03-13 08:58:08 +01:00
#include <wolv/io/file.hpp>
2023-03-13 08:58:08 +01:00
#include <wolv/utils/string.hpp>
namespace hex {
EncodingFile::EncodingFile() : m_mapping(std::make_unique<std::map<size_t, std::map<std::vector<u8>, std::string>>>()) {
}
EncodingFile::EncodingFile(const hex::EncodingFile &other) {
2023-12-19 13:10:25 +01:00
m_mapping = std::make_unique<std::map<size_t, std::map<std::vector<u8>, std::string>>>(*other.m_mapping);
m_tableContent = other.m_tableContent;
m_longestSequence = other.m_longestSequence;
m_shortestSequence = other.m_shortestSequence;
2023-12-19 13:10:25 +01:00
m_valid = other.m_valid;
m_name = other.m_name;
}
2023-11-10 20:47:08 +01:00
EncodingFile::EncodingFile(EncodingFile &&other) noexcept {
2023-12-19 13:10:25 +01:00
m_mapping = std::move(other.m_mapping);
m_tableContent = std::move(other.m_tableContent);
m_longestSequence = other.m_longestSequence;
m_shortestSequence = other.m_shortestSequence;
2023-12-19 13:10:25 +01:00
m_valid = other.m_valid;
m_name = std::move(other.m_name);
}
EncodingFile::EncodingFile(Type type, const std::fs::path &path) : EncodingFile() {
auto file = wolv::io::File(path, wolv::io::File::Mode::Read);
switch (type) {
2022-02-01 22:09:44 +01:00
case Type::Thingy:
parse(file.readString());
break;
default:
return;
}
{
2023-12-19 13:10:25 +01:00
m_name = path.stem().string();
m_name = wolv::util::replaceStrings(m_name, "_", " ");
2023-12-19 13:10:25 +01:00
if (!m_name.empty())
m_name[0] = std::toupper(m_name[0]);
}
2023-12-19 13:10:25 +01:00
m_valid = true;
}
EncodingFile::EncodingFile(Type type, const std::string &content) : EncodingFile() {
switch (type) {
case Type::Thingy:
parse(content);
2022-02-01 22:09:44 +01:00
break;
default:
return;
}
2023-12-19 13:10:25 +01:00
m_name = "Unknown";
m_valid = true;
}
EncodingFile &EncodingFile::operator=(const hex::EncodingFile &other) {
2023-12-19 13:10:25 +01:00
m_mapping = std::make_unique<std::map<size_t, std::map<std::vector<u8>, std::string>>>(*other.m_mapping);
m_tableContent = other.m_tableContent;
m_longestSequence = other.m_longestSequence;
m_shortestSequence = other.m_shortestSequence;
2023-12-19 13:10:25 +01:00
m_valid = other.m_valid;
m_name = other.m_name;
return *this;
}
2023-11-10 20:47:08 +01:00
EncodingFile &EncodingFile::operator=(EncodingFile &&other) noexcept {
2023-12-19 13:10:25 +01:00
m_mapping = std::move(other.m_mapping);
m_tableContent = std::move(other.m_tableContent);
m_longestSequence = other.m_longestSequence;
m_shortestSequence = other.m_shortestSequence;
2023-12-19 13:10:25 +01:00
m_valid = other.m_valid;
m_name = std::move(other.m_name);
return *this;
}
std::pair<std::string_view, size_t> EncodingFile::getEncodingFor(std::span<u8> buffer) const {
2023-12-19 13:10:25 +01:00
for (auto riter = m_mapping->crbegin(); riter != m_mapping->crend(); ++riter) {
const auto &[size, mapping] = *riter;
if (size > buffer.size()) continue;
2023-11-10 20:47:08 +01:00
std::vector key(buffer.begin(), buffer.begin() + size);
if (mapping.contains(key))
return { mapping.at(key), size };
}
return { ".", 1 };
}
u64 EncodingFile::getEncodingLengthFor(std::span<u8> buffer) const {
2023-12-19 13:10:25 +01:00
for (auto riter = m_mapping->crbegin(); riter != m_mapping->crend(); ++riter) {
const auto &[size, mapping] = *riter;
if (size > buffer.size()) continue;
2023-11-10 20:47:08 +01:00
std::vector key(buffer.begin(), buffer.begin() + size);
if (mapping.contains(key))
return size;
}
return 1;
}
void EncodingFile::parse(const std::string &content) {
2023-12-19 13:10:25 +01:00
m_tableContent = content;
for (const auto &line : splitString(m_tableContent, "\n")) {
2021-02-14 12:05:58 +01:00
std::string from, to;
{
auto delimiterPos = line.find('=');
ui/ux: Rewrite of the entire hex editor view to make it more flexible (#512) * ui/ux: Initial recreation of the hex editor view * ui/ux: Added back support for editing cells * ux: Make scrolling and selecting bytes feel nice again * ui/ux: Improved byte selecting, added footer * sys: Make math evaluator more generic to support integer only calculations * patterns: Moved value formatting into pattern language * ui/ux: Added Goto and Search popups, improved selection * ui: Added better tooltips for bookmarks and patterns * sys: Use worse hex search algorithm on macOS Sadly it still doesn't support `std::boyer_moore_horsepool_searcher` * ui: Added back missing events, menu items and shortcuts * fix: Bookmark highlighting being rendered off by one * fix: Various macOS build errors * fix: size_t is not u64 on macos * fix: std::fmod and std::pow not working with integer types on macos * fix: Missing semicolons * sys: Added proper integer pow function * ui: Added back support for custom encodings * fix: Editor not jumping to selection when selection gets changed * ui: Turn Hexii setting into a data visualizer * sys: Added back remaining shortcuts * sys: Remove old hex editor files * sys: Moved more legacy things away from the hex editor view, updated localization * fix: Hex editor scrolling behaving weirdly and inconsistently * sys: Cleaned up Hex editor code * sys: Added selection color setting, localized all new settings * fix: Search feature not working correctly * ui: Replace custom ImGui::Disabled function with native ImGui ones * ui: Fix bookmark tooltip rendering issues * fix: Another size_t not being 64 bit issue on MacOS
2022-05-27 20:42:07 +02:00
if (delimiterPos >= line.length())
continue;
2021-02-14 12:05:58 +01:00
from = line.substr(0, delimiterPos);
2022-02-01 22:09:44 +01:00
to = line.substr(delimiterPos + 1);
2021-02-14 12:05:58 +01:00
if (from.empty()) continue;
2021-02-14 12:05:58 +01:00
}
auto fromBytes = hex::parseByteString(from);
if (fromBytes.empty()) continue;
2022-07-16 12:14:06 +02:00
if (to.length() > 1)
2023-03-13 08:58:08 +01:00
to = wolv::util::trim(to);
2022-07-16 12:14:06 +02:00
if (to.empty())
to = " ";
2023-12-19 13:10:25 +01:00
if (!m_mapping->contains(fromBytes.size()))
m_mapping->insert({ fromBytes.size(), {} });
u64 keySize = fromBytes.size();
2023-12-19 13:10:25 +01:00
(*m_mapping)[keySize].insert({ std::move(fromBytes), to });
2023-12-19 13:10:25 +01:00
m_longestSequence = std::max(m_longestSequence, keySize);
m_shortestSequence = std::min(m_shortestSequence, keySize);
}
}
}