2024-01-28 15:28:55 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
|
|
|
|
#include <wolv/utils/string.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
ImColor entropyMiniMapVisualizer(const std::vector<u8> &data) {
|
2024-01-30 00:00:51 +01:00
|
|
|
std::array<u8, 256> frequencies = { 0 };
|
2024-01-28 15:28:55 +01:00
|
|
|
for (u8 byte : data)
|
2024-01-30 00:00:51 +01:00
|
|
|
frequencies[byte] += 1;
|
2024-01-28 15:28:55 +01:00
|
|
|
|
|
|
|
double entropy = 0.0;
|
|
|
|
for (u32 frequency : frequencies) {
|
|
|
|
if (frequency == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
double probability = static_cast<double>(frequency) / data.size();
|
|
|
|
entropy -= probability * std::log2(probability);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate color
|
|
|
|
ImColor color = ImColor::HSV(0.0F, 0.0F, 1.0F);
|
|
|
|
|
|
|
|
if (entropy > 0.0) {
|
|
|
|
double hue = std::clamp(entropy / 8.0, 0.0, 1.0);
|
|
|
|
color = ImColor::HSV(static_cast<float>(hue) / 0.75F, 0.8F, 1.0F);
|
|
|
|
}
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImColor zerosMiniMapVisualizer(const std::vector<u8> &data) {
|
|
|
|
u32 zerosCount = 0;
|
|
|
|
for (u8 byte : data) {
|
|
|
|
if (byte == 0x00)
|
2024-01-30 00:00:51 +01:00
|
|
|
zerosCount += 1;
|
2024-01-28 15:28:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ImColor::HSV(0.0F, 0.0F, 1.0F - (double(zerosCount) / data.size()));
|
|
|
|
}
|
|
|
|
|
2024-01-30 00:00:51 +01:00
|
|
|
ImColor byteTypeMiniMapVisualizer(const std::vector<u8> &data) {
|
|
|
|
u8 asciiCount = 0;
|
|
|
|
for (u8 byte : data) {
|
|
|
|
if (std::isprint(byte))
|
|
|
|
asciiCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ImColor::HSV(0.5F, 0.5F, (double(asciiCount) / data.size()));
|
|
|
|
}
|
|
|
|
|
2024-01-28 15:28:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerMiniMapVisualizers() {
|
|
|
|
ContentRegistry::HexEditor::addMiniMapVisualizer("hex.builtin.minimap_visualizer.entropy", entropyMiniMapVisualizer);
|
|
|
|
ContentRegistry::HexEditor::addMiniMapVisualizer("hex.builtin.minimap_visualizer.zeros", zerosMiniMapVisualizer);
|
2024-01-30 00:00:51 +01:00
|
|
|
ContentRegistry::HexEditor::addMiniMapVisualizer("hex.builtin.minimap_visualizer.ascii", byteTypeMiniMapVisualizer);
|
2024-01-28 15:28:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|