#include "helpers/encoding_file.hpp" #include #include namespace hex { EncodingFile::EncodingFile(Type type, std::string_view path) { std::ifstream encodingFile(path.data()); switch (type) { case Type::Thingy: parseThingyFile(encodingFile); break; default: throw std::runtime_error("Invalid encoding file type"); } } std::pair EncodingFile::getEncodingFor(const std::vector &buffer) const { for (auto iter = this->m_mapping.rbegin(); iter != this->m_mapping.rend(); iter++) { auto &[size, mapping] = *iter; if (size > buffer.size()) continue; auto key = std::vector(buffer.begin(), buffer.begin() + size); if (mapping.contains(key)) return { mapping.at(key), size }; } return { ".", 1 }; } void EncodingFile::parseThingyFile(std::ifstream &content) { for (std::string line; std::getline(content, line);) { auto entry = hex::splitString(line, "="); if (entry.size() != 2) return; auto &from = entry[0]; auto &to = entry[1]; hex::trim(from); hex::trim(to); auto fromBytes = hex::parseByteString(from); if (!this->m_mapping.contains(fromBytes.size())) this->m_mapping.insert({ fromBytes.size(), { } }); this->m_mapping[fromBytes.size()].insert({ fromBytes, to }); this->m_longestSequence = std::max(this->m_longestSequence, fromBytes.size()); } } }