From 1b3fa4da5540274c28b4786be2af2cb4c1c6b64e Mon Sep 17 00:00:00 2001 From: Redbeanw44602 Date: Sun, 2 Mar 2025 12:08:10 +0800 Subject: [PATCH] feat: json serializer of bound_symbol & magic_entry. --- src/data_format/bound_symbol_list.cpp | 6 +----- src/data_format/type/bound_symbol.cpp | 19 +++++++++++++++++++ src/data_format/type/bound_symbol.h | 11 +++++++---- src/data_format/type/magic_entry.cpp | 15 +++++++++++++++ src/data_format/type/magic_entry.h | 7 +++++++ src/tools/blob-extractor/main.cpp | 9 +-------- 6 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 src/data_format/type/bound_symbol.cpp create mode 100644 src/data_format/type/magic_entry.cpp diff --git a/src/data_format/bound_symbol_list.cpp b/src/data_format/bound_symbol_list.cpp index b312989..24366fb 100644 --- a/src/data_format/bound_symbol_list.cpp +++ b/src/data_format/bound_symbol_list.cpp @@ -28,11 +28,7 @@ void BoundSymbolList::write(const fs::path& path) const { nlohmann::json data; for (const auto& entity : m_entities) { - data.emplace_back(nlohmann::json{ - {"symbol", entity.m_symbol_name}, - {"rva", entity.m_rva }, - {"is_function", entity.m_is_function} - }); + data.emplace_back(entity); } ofs << data.dump(4); diff --git a/src/data_format/type/bound_symbol.cpp b/src/data_format/type/bound_symbol.cpp new file mode 100644 index 0000000..075863f --- /dev/null +++ b/src/data_format/type/bound_symbol.cpp @@ -0,0 +1,19 @@ +#include "data_format/type/bound_symbol.h" + +#include + +namespace di { + +void to_json(nlohmann::json& json, const BoundSymbol& symbol) { + json["symbol"] = symbol.m_symbol_name; + json["rva"] = symbol.m_rva; + json["is_function"] = symbol.m_is_function; +} + +void from_json(const nlohmann::json& json, BoundSymbol& symbol) { + symbol.m_symbol_name = json["symbol"]; + symbol.m_rva = json["rva"]; + symbol.m_is_function = json["is_function"]; +} + +} // namespace di \ No newline at end of file diff --git a/src/data_format/type/bound_symbol.h b/src/data_format/type/bound_symbol.h index fbfd7c0..cf9c599 100644 --- a/src/data_format/type/bound_symbol.h +++ b/src/data_format/type/bound_symbol.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace di { @@ -15,6 +15,9 @@ struct BoundSymbol { } }; +void to_json(nlohmann::json& json, const BoundSymbol& symbol); +void from_json(const nlohmann::json& json, BoundSymbol& symbol); + } // namespace di namespace std { @@ -23,9 +26,9 @@ template <> struct hash { constexpr size_t operator()(const di::BoundSymbol& symbol) const { size_t seed = 0; - boost::hash_combine(seed, symbol.m_symbol_name); - boost::hash_combine(seed, symbol.m_rva); - boost::hash_combine(seed, symbol.m_is_function); + hash_combine(seed, symbol.m_symbol_name); + hash_combine(seed, symbol.m_rva); + hash_combine(seed, symbol.m_is_function); return seed; } }; diff --git a/src/data_format/type/magic_entry.cpp b/src/data_format/type/magic_entry.cpp new file mode 100644 index 0000000..745b0c6 --- /dev/null +++ b/src/data_format/type/magic_entry.cpp @@ -0,0 +1,15 @@ +#include "data_format/type/magic_entry.h" + +#include + +namespace di { + +void to_json(nlohmann::json& json, const MagicEntry& entry) { + json["rva"] = entry.rva; + json["is_function"] = entry.is_function(); + json["_unk2"] = entry._unk2(); + json["is_verbose"] = entry.is_verbose(); + json["_unk4"] = entry._unk4(); +} + +} // namespace di \ No newline at end of file diff --git a/src/data_format/type/magic_entry.h b/src/data_format/type/magic_entry.h index 14a6035..450de4e 100644 --- a/src/data_format/type/magic_entry.h +++ b/src/data_format/type/magic_entry.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace di { struct MagicEntry { @@ -18,4 +20,9 @@ struct MagicEntry { constexpr bool _unk4() const { return flags[3]; } }; +void to_json(nlohmann::json& json, const MagicEntry& entry); + +// TODO +// void from_json(const nlohmann::json& json, MagicEntry& entry); + } // namespace di diff --git a/src/tools/blob-extractor/main.cpp b/src/tools/blob-extractor/main.cpp index d11474b..bce1498 100644 --- a/src/tools/blob-extractor/main.cpp +++ b/src/tools/blob-extractor/main.cpp @@ -42,14 +42,7 @@ int main(int argc, char* argv[]) try { nlohmann::json data; blob.for_each([&data](hash_t hash, const MagicEntry& entry) { - data.emplace_back(nlohmann::json{ - {"hash", hash }, - {"rva", entry.rva }, - {"is_function", entry.is_function()}, - {"_unk2", entry._unk2() }, - {"is_verbose", entry.is_verbose() }, - {"_unk4", entry._unk4() } - }); + data[hash] = entry; }); std::ofstream ofs(args.m_output_path);