2023-11-30 11:23:12 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2022-08-01 13:20:20 +02:00
|
|
|
#include <hex/helpers/crypto.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2023-11-30 14:40:07 +01:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
#include <fmt/chrono.h>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
#include <cstring>
|
2021-08-30 19:12:46 +02:00
|
|
|
#include <codecvt>
|
2021-09-01 02:01:50 +02:00
|
|
|
#include <string>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
|
|
|
#include <imgui_internal.h>
|
2022-02-17 14:43:04 +01:00
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
using Style = ContentRegistry::DataInspector::NumberDisplayStyle;
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
struct GUID {
|
|
|
|
u32 data1;
|
|
|
|
u16 data2;
|
|
|
|
u16 data3;
|
2022-01-24 20:53:17 +01:00
|
|
|
u8 data4[8];
|
2021-01-22 18:01:39 +01:00
|
|
|
};
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
template<std::unsigned_integral T, size_t Size = sizeof(T)>
|
2022-02-15 21:53:39 +01:00
|
|
|
static std::vector<u8> stringToUnsigned(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(u64)) {
|
2022-02-15 21:50:27 +01:00
|
|
|
u64 result = std::strtoull(value.c_str(), nullptr, 0);
|
|
|
|
if (result > std::numeric_limits<T>::max()) return {};
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
std::vector<u8> bytes(Size, 0x00);
|
2022-02-15 21:50:27 +01:00
|
|
|
std::memcpy(bytes.data(), &result, bytes.size());
|
|
|
|
|
|
|
|
if (endian != std::endian::native)
|
|
|
|
std::reverse(bytes.begin(), bytes.end());
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
template<std::signed_integral T, size_t Size = sizeof(T)>
|
2022-02-15 21:53:39 +01:00
|
|
|
static std::vector<u8> stringToSigned(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(u64)) {
|
2022-02-17 14:43:04 +01:00
|
|
|
i64 result = std::strtoll(value.c_str(), nullptr, 0);
|
2022-02-15 21:50:27 +01:00
|
|
|
if (result > std::numeric_limits<T>::max() || result < std::numeric_limits<T>::min()) return {};
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
std::vector<u8> bytes(Size, 0x00);
|
2022-02-15 21:50:27 +01:00
|
|
|
std::memcpy(bytes.data(), &result, bytes.size());
|
|
|
|
|
|
|
|
if (endian != std::endian::native)
|
|
|
|
std::reverse(bytes.begin(), bytes.end());
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<std::floating_point T>
|
2022-02-15 21:53:39 +01:00
|
|
|
static std::vector<u8> stringToFloat(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(long double)) {
|
2022-07-07 18:35:10 +02:00
|
|
|
T result = std::strtold(value.c_str(), nullptr);
|
2022-02-15 21:50:27 +01:00
|
|
|
|
|
|
|
std::vector<u8> bytes(sizeof(T), 0x00);
|
|
|
|
std::memcpy(bytes.data(), &result, bytes.size());
|
|
|
|
|
|
|
|
if (endian != std::endian::native)
|
|
|
|
std::reverse(bytes.begin(), bytes.end());
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
template<std::integral T, size_t Size = sizeof(T)>
|
2022-02-15 21:53:39 +01:00
|
|
|
static std::vector<u8> stringToInteger(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(u64)) {
|
2022-08-05 12:19:50 +02:00
|
|
|
if constexpr (std::unsigned_integral<T>)
|
|
|
|
return stringToUnsigned<T, Size>(value, endian);
|
|
|
|
else if constexpr (std::signed_integral<T>)
|
|
|
|
return stringToSigned<T, Size>(value, endian);
|
2022-02-15 21:50:27 +01:00
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
template<std::integral T, size_t Size = sizeof(T)>
|
|
|
|
static std::string unsignedToString(const std::vector<u8> &buffer, std::endian endian, Style style) requires(sizeof(T) <= sizeof(u64)) {
|
|
|
|
if (buffer.size() < Size)
|
|
|
|
return { };
|
|
|
|
|
|
|
|
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? hex::format("0x{{0:0{}X}}", Size * 2) : hex::format("0o{{0:0{}o}}", Size * 3));
|
|
|
|
|
|
|
|
T value = 0x00;
|
2022-08-15 21:08:09 +02:00
|
|
|
std::memcpy(&value, buffer.data(), std::min(sizeof(T), Size));
|
2022-08-05 12:19:50 +02:00
|
|
|
return hex::format(format, hex::changeEndianess(value, Size, endian));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<std::integral T, size_t Size = sizeof(T)>
|
|
|
|
static std::string signedToString(const std::vector<u8> &buffer, std::endian endian, Style style) requires(sizeof(T) <= sizeof(u64)) {
|
|
|
|
if (buffer.size() < Size)
|
|
|
|
return { };
|
|
|
|
|
|
|
|
auto format = (style == Style::Decimal) ? "{0}{1:d}" : ((style == Style::Hexadecimal) ? hex::format("{{0}}0x{{1:0{}X}}", Size * 2) : hex::format("{{0}}0o{{1:0{}o}}", Size * 3));
|
|
|
|
|
|
|
|
T value = 0x00;
|
2022-08-15 21:08:09 +02:00
|
|
|
std::memcpy(&value, buffer.data(), std::min(sizeof(T), Size));
|
2022-08-28 12:55:26 +02:00
|
|
|
auto number = hex::changeEndianess(value, Size, endian);
|
|
|
|
if (Size != sizeof(T))
|
|
|
|
number = hex::signExtend(Size * 8, number);
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
bool negative = number < 0;
|
|
|
|
|
|
|
|
return hex::format(format, negative ? "-" : "", std::abs(number));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<std::integral T, size_t Size = sizeof(T)>
|
|
|
|
static std::string integerToString(const std::vector<u8> &buffer, std::endian endian, Style style) requires(sizeof(T) <= sizeof(u64)) {
|
|
|
|
if constexpr (std::unsigned_integral<T>)
|
|
|
|
return unsignedToString<T, Size>(buffer, endian, style);
|
|
|
|
else if constexpr (std::signed_integral<T>)
|
|
|
|
return signedToString<T, Size>(buffer, endian, style);
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static hex::ContentRegistry::DataInspector::impl::GeneratorFunction drawString(T func) {
|
|
|
|
return [func](const std::vector<u8> &buffer, std::endian endian, Style style) {
|
2022-08-18 00:24:29 +02:00
|
|
|
return [value = func(buffer, endian, style)]() -> std::string { ImGui::TextUnformatted(value.c_str()); return value; };
|
2022-08-05 12:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
// clang-format off
|
2021-01-22 18:01:39 +01:00
|
|
|
void registerDataInspectorEntries() {
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.binary", sizeof(u8),
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(endian, style);
|
|
|
|
|
2022-05-29 14:57:59 +02:00
|
|
|
std::string binary = hex::format("0b{:08b}", buffer[0]);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-15 21:50:27 +01:00
|
|
|
return [binary] {
|
|
|
|
ImGui::TextUnformatted(binary.c_str());
|
|
|
|
return binary;
|
2022-02-17 14:43:04 +01:00
|
|
|
};
|
2023-02-19 10:25:39 +01:00
|
|
|
}, [](const std::string &value, std::endian endian) -> std::vector<u8> {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(endian);
|
2022-05-17 20:46:42 +02:00
|
|
|
|
2023-02-19 10:25:39 +01:00
|
|
|
std::string binary = value;
|
|
|
|
if (binary.starts_with("0b"))
|
|
|
|
binary = binary.substr(2);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2023-02-19 10:25:39 +01:00
|
|
|
if (binary.size() > 8) return { };
|
2023-02-18 22:20:02 +01:00
|
|
|
|
2023-02-19 10:25:39 +01:00
|
|
|
if (auto result = hex::parseBinaryString(binary); result.has_value())
|
2023-02-18 22:20:02 +01:00
|
|
|
return { result.value() };
|
|
|
|
else
|
|
|
|
return { };
|
2022-02-17 14:43:04 +01:00
|
|
|
}
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-01-15 15:15:25 +01:00
|
|
|
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.u8", sizeof(u8),
|
|
|
|
drawString(integerToString<u8>),
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToInteger<u8>
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.i8", sizeof(i8),
|
2022-08-05 12:19:50 +02:00
|
|
|
drawString(integerToString<i8>),
|
|
|
|
stringToInteger<i8>
|
2022-02-17 14:43:04 +01:00
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.u16", sizeof(u16),
|
2022-08-05 12:19:50 +02:00
|
|
|
drawString(integerToString<u16>),
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToInteger<u16>
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.i16", sizeof(i16),
|
2022-08-05 12:19:50 +02:00
|
|
|
drawString(integerToString<i16>),
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToInteger<i16>
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.u24", 3,
|
|
|
|
drawString(integerToString<u32, 3>),
|
|
|
|
stringToInteger<u32, 3>
|
|
|
|
);
|
2022-01-15 15:15:25 +01:00
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.i24", 3,
|
|
|
|
drawString(integerToString<i32, 3>),
|
|
|
|
stringToInteger<i32, 3>
|
|
|
|
);
|
2022-01-15 15:15:25 +01:00
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.u32", sizeof(u32),
|
|
|
|
drawString(integerToString<u32>),
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToInteger<u32>
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.i32", sizeof(i32),
|
2022-08-05 12:19:50 +02:00
|
|
|
drawString(integerToString<i32>),
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToInteger<i32>
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.u48", 6,
|
|
|
|
drawString(integerToString<u64, 6>),
|
|
|
|
stringToInteger<u64, 6>
|
|
|
|
);
|
2022-01-15 15:15:25 +01:00
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.i48", 6,
|
|
|
|
drawString(integerToString<i64, 6>),
|
|
|
|
stringToInteger<i64, 6>
|
|
|
|
);
|
2022-01-15 15:15:25 +01:00
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.u64", sizeof(u64),
|
|
|
|
drawString(integerToString<u64>),
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToInteger<u64>
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.i64", sizeof(i64),
|
2022-08-05 12:19:50 +02:00
|
|
|
drawString(integerToString<i64>),
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToInteger<i64>
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.float16", sizeof(u16),
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-05-27 20:49:38 +02:00
|
|
|
u16 result = 0;
|
|
|
|
std::memcpy(&result, buffer.data(), sizeof(u16));
|
|
|
|
|
|
|
|
const auto formatString = style == Style::Hexadecimal ? "{0:a}" : "{0:G}";
|
|
|
|
|
|
|
|
auto value = hex::format(formatString, float16ToFloat32(hex::changeEndianess(result, endian)));
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
}
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.float", sizeof(float),
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-05-27 20:49:38 +02:00
|
|
|
float result = 0;
|
|
|
|
std::memcpy(&result, buffer.data(), sizeof(float));
|
2022-03-27 00:01:28 +01:00
|
|
|
|
2022-05-27 20:49:38 +02:00
|
|
|
const auto formatString = style == Style::Hexadecimal ? "{0:a}" : "{0:G}";
|
|
|
|
|
|
|
|
auto value = hex::format(formatString, hex::changeEndianess(result, endian));
|
2022-02-15 21:50:27 +01:00
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
},
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToFloat<float>
|
|
|
|
);
|
2022-02-15 21:50:27 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.double", sizeof(double),
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
double result = 0;
|
|
|
|
std::memcpy(&result, buffer.data(), sizeof(double));
|
|
|
|
|
2022-05-27 20:49:38 +02:00
|
|
|
const auto formatString = style == Style::Hexadecimal ? "{0:a}" : "{0:G}";
|
|
|
|
|
|
|
|
auto value = hex::format(formatString, hex::changeEndianess(result, endian));
|
2022-02-15 21:50:27 +01:00
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
},
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToFloat<double>
|
|
|
|
);
|
2022-02-15 21:50:27 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.long_double", sizeof(long double),
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-05-27 20:49:38 +02:00
|
|
|
long double result = 0;
|
|
|
|
std::memcpy(&result, buffer.data(), sizeof(long double));
|
|
|
|
|
|
|
|
const auto formatString = style == Style::Hexadecimal ? "{0:a}" : "{0:G}";
|
2022-03-27 00:01:28 +01:00
|
|
|
|
2022-05-27 20:49:38 +02:00
|
|
|
auto value = hex::format(formatString, hex::changeEndianess(result, endian));
|
2022-02-15 21:50:27 +01:00
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
},
|
2022-02-17 14:43:04 +01:00
|
|
|
stringToFloat<long double>
|
|
|
|
);
|
2022-02-15 21:50:27 +01:00
|
|
|
|
2022-08-01 13:20:20 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.sleb128", 1, (sizeof(i128) * 8 / 7) + 1,
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
|
|
|
hex::unused(endian);
|
|
|
|
|
|
|
|
auto format = (style == Style::Decimal) ? "{0}{1:d}" : ((style == Style::Hexadecimal) ? "{0}0x{1:X}" : "{0}0o{1:o}");
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
|
2022-08-01 13:20:20 +02:00
|
|
|
auto number = hex::crypt::decodeSleb128(buffer);
|
|
|
|
bool negative = number < 0;
|
2023-10-04 12:00:32 +02:00
|
|
|
auto value = hex::format(format, negative ? "-" : "", negative ? -number : number);
|
2022-08-01 13:20:20 +02:00
|
|
|
|
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
},
|
|
|
|
[](const std::string &value, std::endian endian) -> std::vector<u8> {
|
|
|
|
hex::unused(endian);
|
|
|
|
|
|
|
|
return hex::crypt::encodeSleb128(std::strtoll(value.c_str(), nullptr, 0));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.uleb128", 1, (sizeof(u128) * 8 / 7) + 1,
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
|
|
|
hex::unused(endian);
|
|
|
|
|
|
|
|
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "0o{0:o}");
|
|
|
|
|
|
|
|
auto value = hex::format(format, hex::crypt::decodeUleb128(buffer));
|
|
|
|
|
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
},
|
|
|
|
[](const std::string &value, std::endian endian) -> std::vector<u8> {
|
|
|
|
hex::unused(endian);
|
|
|
|
|
|
|
|
return hex::crypt::encodeUleb128(std::strtoull(value.c_str(), nullptr, 0));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-27 20:49:38 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.bool", sizeof(bool),
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
|
|
|
hex::unused(endian, style);
|
|
|
|
|
|
|
|
std::string value = [buffer] {
|
|
|
|
switch (buffer[0]) {
|
|
|
|
case false:
|
|
|
|
return "false";
|
|
|
|
case true:
|
|
|
|
return "true";
|
|
|
|
default:
|
|
|
|
return "Invalid";
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.ascii", sizeof(char8_t),
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(endian, style);
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
auto value = makePrintable(*reinterpret_cast<char8_t *>(buffer.data()));
|
2023-11-16 22:24:06 +01:00
|
|
|
return [value] { ImGuiExt::TextFormatted("'{0}'", value.c_str()); return value; };
|
2022-02-17 14:43:04 +01:00
|
|
|
},
|
|
|
|
[](const std::string &value, std::endian endian) -> std::vector<u8> {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(endian);
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
if (value.length() > 1) return { };
|
2022-02-15 21:50:27 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
return { u8(value[0]) };
|
|
|
|
}
|
|
|
|
);
|
2022-02-15 21:50:27 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.wide", sizeof(wchar_t),
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(style);
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
wchar_t wideChar = '\x00';
|
|
|
|
std::memcpy(&wideChar, buffer.data(), std::min(sizeof(wchar_t), buffer.size()));
|
2022-02-15 21:50:27 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
auto c = hex::changeEndianess(wideChar, endian);
|
2022-01-15 14:14:53 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("Invalid");
|
2021-08-30 19:12:46 +02:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
auto value = hex::format("{0}", c <= 255 ? makePrintable(c) : converter.to_bytes(c));
|
2023-11-16 22:24:06 +01:00
|
|
|
return [value] { ImGuiExt::TextFormatted("'{0}'", value.c_str()); return value; };
|
2022-02-17 14:43:04 +01:00
|
|
|
},
|
|
|
|
[](const std::string &value, std::endian endian) -> std::vector<u8> {
|
2022-06-03 18:46:38 +02:00
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("Invalid");
|
2021-08-30 19:12:46 +02:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
std::vector<u8> bytes;
|
|
|
|
auto wideString = converter.from_bytes(value.c_str());
|
|
|
|
std::copy(wideString.begin(), wideString.end(), std::back_inserter(bytes));
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
if (endian != std::endian::native)
|
|
|
|
std::reverse(bytes.begin(), bytes.end());
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.utf8", sizeof(char8_t) * 4,
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(endian, style);
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
char utf8Buffer[5] = { 0 };
|
|
|
|
char codepointString[5] = { 0 };
|
|
|
|
u32 codepoint = 0;
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
std::memcpy(utf8Buffer, reinterpret_cast<char8_t *>(buffer.data()), 4);
|
|
|
|
u8 codepointSize = ImTextCharFromUtf8(&codepoint, utf8Buffer, utf8Buffer + 4);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
std::memcpy(codepointString, utf8Buffer, std::min(codepointSize, u8(4)));
|
|
|
|
auto value = hex::format("'{0}' (U+0x{1:04X})",
|
|
|
|
codepoint == 0xFFFD ? "Invalid" : (codepointSize == 1 ? makePrintable(codepointString[0]) : codepointString),
|
|
|
|
codepoint);
|
|
|
|
|
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
}
|
|
|
|
);
|
2021-09-01 02:01:50 +02:00
|
|
|
|
2022-09-28 16:10:40 +02:00
|
|
|
constexpr static auto MaxStringLength = 32;
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.string", 1,
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
2022-07-08 09:21:49 +02:00
|
|
|
hex::unused(buffer, endian, style);
|
2022-03-27 00:01:28 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
auto currSelection = ImHexApi::HexEditor::getSelection();
|
2021-09-01 02:01:50 +02:00
|
|
|
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
std::string value, copyValue;
|
2021-09-01 02:01:50 +02:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
if (currSelection.has_value()) {
|
2022-07-08 09:21:49 +02:00
|
|
|
std::vector<u8> stringBuffer(std::min<size_t>(currSelection->size, 0x1000), 0x00);
|
2022-05-27 20:42:07 +02:00
|
|
|
ImHexApi::Provider::get()->read(currSelection->address, stringBuffer.data(), stringBuffer.size());
|
2021-09-01 02:01:50 +02:00
|
|
|
|
2022-07-08 09:21:49 +02:00
|
|
|
value = copyValue = hex::encodeByteString(stringBuffer);
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2022-08-02 23:07:35 +02:00
|
|
|
if (value.size() > MaxStringLength) {
|
2022-07-08 09:21:49 +02:00
|
|
|
value.resize(MaxStringLength);
|
2022-05-27 20:42:07 +02:00
|
|
|
value += "...";
|
2022-07-08 09:21:49 +02:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
} else {
|
|
|
|
value = "";
|
|
|
|
copyValue = "";
|
|
|
|
}
|
2021-09-01 02:01:50 +02:00
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
return [value, copyValue] { ImGuiExt::TextFormatted("\"{0}\"", value.c_str()); return copyValue; };
|
2022-02-17 14:43:04 +01:00
|
|
|
},
|
|
|
|
[](const std::string &value, std::endian endian) -> std::vector<u8> {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(endian);
|
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
return hex::decodeByteString(value);
|
|
|
|
}
|
|
|
|
);
|
2021-09-01 02:01:50 +02:00
|
|
|
|
2022-09-28 16:10:40 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.string16", 2,
|
|
|
|
[](auto buffer, auto endian, auto style) {
|
|
|
|
hex::unused(buffer, endian, style);
|
|
|
|
|
|
|
|
auto currSelection = ImHexApi::HexEditor::getSelection();
|
|
|
|
|
|
|
|
std::string value, copyValue;
|
|
|
|
|
|
|
|
if (currSelection.has_value()) {
|
|
|
|
std::u16string stringBuffer(std::min<size_t>(currSelection->size, 0x1000), 0x00);
|
|
|
|
ImHexApi::Provider::get()->read(currSelection->address, stringBuffer.data(), stringBuffer.size());
|
|
|
|
|
|
|
|
for (auto &c : stringBuffer)
|
|
|
|
c = hex::changeEndianess(c, endian);
|
|
|
|
|
|
|
|
auto it = std::remove_if(buffer.begin(), buffer.end(),
|
|
|
|
[](auto c) { return c == 0x00; });
|
|
|
|
buffer.erase(it, buffer.end());
|
|
|
|
|
2022-10-10 14:41:24 +02:00
|
|
|
auto string = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>("Invalid").to_bytes(stringBuffer.data());
|
|
|
|
value = copyValue = hex::encodeByteString({ string.begin(), string.end() });
|
2022-09-28 16:10:40 +02:00
|
|
|
|
|
|
|
if (value.size() > MaxStringLength) {
|
|
|
|
value.resize(MaxStringLength);
|
|
|
|
value += "...";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = "";
|
|
|
|
copyValue = "";
|
|
|
|
}
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
return [value, copyValue] { ImGuiExt::TextFormatted("L\"{0}\"", value.c_str()); return copyValue; };
|
2022-09-28 16:10:40 +02:00
|
|
|
},
|
|
|
|
[](const std::string &value, std::endian endian) -> std::vector<u8> {
|
|
|
|
hex::unused(endian);
|
|
|
|
|
|
|
|
return hex::decodeByteString(value);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-01-04 17:55:53 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-01-15 15:03:15 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.time32", sizeof(u32), [](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(style);
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<u32 *>(buffer.data()), endian);
|
2022-01-15 14:14:53 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::string value;
|
|
|
|
try {
|
|
|
|
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", fmt::localtime(endianAdjustedTime));
|
2023-11-10 20:47:08 +01:00
|
|
|
} catch (fmt::format_error &) {
|
2022-01-24 20:53:17 +01:00
|
|
|
value = "Invalid";
|
|
|
|
}
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.time64", sizeof(u64), [](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(style);
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<u64 *>(buffer.data()), endian);
|
2022-01-15 14:14:53 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::string value;
|
|
|
|
try {
|
|
|
|
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", fmt::localtime(endianAdjustedTime));
|
2023-11-10 20:47:08 +01:00
|
|
|
} catch (fmt::format_error &) {
|
2022-01-24 20:53:17 +01:00
|
|
|
value = "Invalid";
|
|
|
|
}
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.time", sizeof(time_t), [](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(style);
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<time_t *>(buffer.data()), endian);
|
2022-01-15 15:03:15 +01:00
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
std::string value;
|
2022-01-15 15:03:15 +01:00
|
|
|
try {
|
|
|
|
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", fmt::localtime(endianAdjustedTime));
|
|
|
|
} catch (fmt::format_error &e) {
|
2021-01-22 18:01:39 +01:00
|
|
|
value = "Invalid";
|
2022-01-15 15:03:15 +01:00
|
|
|
}
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-03-02 23:15:15 +01:00
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
2021-01-22 18:01:39 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-05-27 20:49:38 +02:00
|
|
|
struct DOSDate {
|
|
|
|
unsigned day : 5;
|
|
|
|
unsigned month : 4;
|
|
|
|
unsigned year : 7;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DOSTime {
|
|
|
|
unsigned seconds : 5;
|
|
|
|
unsigned minutes : 6;
|
|
|
|
unsigned hours : 5;
|
|
|
|
};
|
|
|
|
|
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.dos_date", sizeof(DOSDate), [](auto buffer, auto endian, auto style) {
|
|
|
|
hex::unused(style);
|
|
|
|
|
|
|
|
DOSDate date = { };
|
|
|
|
std::memcpy(&date, buffer.data(), sizeof(DOSDate));
|
|
|
|
date = hex::changeEndianess(date, endian);
|
|
|
|
|
|
|
|
auto value = hex::format("{}/{}/{}", date.day, date.month, date.year + 1980);
|
|
|
|
|
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
});
|
|
|
|
|
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.dos_time", sizeof(DOSTime), [](auto buffer, auto endian, auto style) {
|
|
|
|
hex::unused(style);
|
|
|
|
|
|
|
|
DOSTime time = { };
|
|
|
|
std::memcpy(&time, buffer.data(), sizeof(DOSTime));
|
|
|
|
time = hex::changeEndianess(time, endian);
|
|
|
|
|
|
|
|
auto value = hex::format("{:02}:{:02}:{:02}", time.hours, time.minutes, time.seconds * 2);
|
|
|
|
|
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
|
|
|
});
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.guid", sizeof(GUID), [](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(style);
|
|
|
|
|
|
|
|
GUID guid = { };
|
2021-01-22 18:01:39 +01:00
|
|
|
std::memcpy(&guid, buffer.data(), sizeof(GUID));
|
2021-03-03 19:58:22 +01:00
|
|
|
auto value = hex::format("{}{{{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}}}",
|
2022-02-01 22:09:44 +01:00
|
|
|
(hex::changeEndianess(guid.data3, endian) >> 12) <= 5 && ((guid.data4[0] >> 4) >= 8 || (guid.data4[0] >> 4) == 0) ? "" : "Invalid ",
|
|
|
|
hex::changeEndianess(guid.data1, endian),
|
|
|
|
hex::changeEndianess(guid.data2, endian),
|
|
|
|
hex::changeEndianess(guid.data3, endian),
|
|
|
|
guid.data4[0],
|
|
|
|
guid.data4[1],
|
|
|
|
guid.data4[2],
|
|
|
|
guid.data4[3],
|
|
|
|
guid.data4[4],
|
|
|
|
guid.data4[5],
|
|
|
|
guid.data4[6],
|
|
|
|
guid.data4[7]);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-03-02 22:09:38 +01:00
|
|
|
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
2021-01-22 18:01:39 +01:00
|
|
|
});
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.rgba8", sizeof(u32), [](auto buffer, auto endian, auto style) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(style);
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
ImColor value(hex::changeEndianess(*reinterpret_cast<u32 *>(buffer.data()), endian));
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
auto copyValue = hex::format("#{:02X}{:02X}{:02X}{:02X}", u8(0xFF * (value.Value.x)), u8(0xFF * (value.Value.y)), u8(0xFF * (value.Value.z)), u8(0xFF * (value.Value.w)));
|
2021-03-02 22:09:38 +01:00
|
|
|
|
2022-02-17 14:43:04 +01:00
|
|
|
return [value, copyValue] {
|
2023-05-13 17:50:16 +02:00
|
|
|
ImGui::ColorButton("##inspectorColor", value, ImGuiColorEditFlags_None, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
|
2022-02-17 14:43:04 +01:00
|
|
|
return copyValue;
|
2021-01-22 18:01:39 +01:00
|
|
|
};
|
|
|
|
});
|
2022-08-01 13:58:23 +02:00
|
|
|
|
|
|
|
ContentRegistry::DataInspector::add("hex.builtin.inspector.rgb565", sizeof(u16), [](auto buffer, auto endian, auto style) {
|
|
|
|
hex::unused(style);
|
|
|
|
|
|
|
|
auto value = hex::changeEndianess(*reinterpret_cast<u16 *>(buffer.data()), endian);
|
|
|
|
ImColor color((value & 0x1F) << 3, ((value >> 5) & 0x3F) << 2, ((value >> 11) & 0x1F) << 3, 0xFF);
|
|
|
|
|
|
|
|
auto copyValue = hex::format("#{:02X}{:02X}{:02X}", u8(0xFF * (color.Value.x)), u8(0xFF * (color.Value.y)), u8(0xFF * (color.Value.z)), 0xFF);
|
|
|
|
|
|
|
|
return [color, copyValue] {
|
2023-05-13 17:50:16 +02:00
|
|
|
ImGui::ColorButton("##inspectorColor", color, ImGuiColorEditFlags_None, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
|
2022-08-01 13:58:23 +02:00
|
|
|
return copyValue;
|
|
|
|
};
|
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
}
|
2022-02-17 14:43:04 +01:00
|
|
|
// clang-format on
|
2021-01-22 18:01:39 +01:00
|
|
|
|
|
|
|
}
|