2020-11-10 21:31:04 +01:00
|
|
|
#include <random>
|
|
|
|
#include "views/view_pattern.hpp"
|
|
|
|
|
|
|
|
#include "utils.hpp"
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
ViewPattern::ViewPattern(std::vector<Highlight> &highlights) : View(), m_highlights(highlights) {
|
|
|
|
this->m_buffer = new char[0xFFFFFF];
|
|
|
|
std::memset(this->m_buffer, 0x00, 0xFFFFFF);
|
|
|
|
}
|
|
|
|
ViewPattern::~ViewPattern() {
|
|
|
|
delete[] this->m_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewPattern::createMenu() {
|
|
|
|
if (ImGui::BeginMenu("File")) {
|
|
|
|
if (ImGui::MenuItem("Load pattern...")) {
|
2020-11-12 21:20:51 +01:00
|
|
|
this->m_fileBrowser.SetTitle("Open Hex Pattern");
|
|
|
|
this->m_fileBrowser.SetTypeFilters({ ".hexpat" });
|
|
|
|
this->m_fileBrowser.Open();
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:22:55 +01:00
|
|
|
if (ImGui::BeginMenu("View")) {
|
2020-11-10 21:31:04 +01:00
|
|
|
ImGui::MenuItem("Pattern View", "", &this->m_windowOpen);
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewPattern::createView() {
|
|
|
|
if (!this->m_windowOpen)
|
|
|
|
return;
|
|
|
|
|
2020-11-11 10:47:02 +01:00
|
|
|
if (ImGui::Begin("Pattern", &this->m_windowOpen, ImGuiWindowFlags_None)) {
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-11 10:47:02 +01:00
|
|
|
auto size = ImGui::GetWindowSize();
|
|
|
|
size.y -= 50;
|
|
|
|
ImGui::InputTextMultiline("Pattern", this->m_buffer, 0xFFFF, size, ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CallbackEdit,
|
|
|
|
[](ImGuiInputTextCallbackData* data) -> int {
|
|
|
|
auto _this = static_cast<ViewPattern*>(data->UserData);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-11 10:47:02 +01:00
|
|
|
_this->parsePattern(data->Buf);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-11 10:47:02 +01:00
|
|
|
return 0;
|
|
|
|
}, this
|
|
|
|
);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-11 10:47:02 +01:00
|
|
|
ImGui::PopStyleVar(2);
|
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
ImGui::End();
|
2020-11-12 21:20:51 +01:00
|
|
|
|
|
|
|
this->m_fileBrowser.Display();
|
|
|
|
|
|
|
|
if (this->m_fileBrowser.HasSelected()) {
|
|
|
|
FILE *file = fopen(this->m_fileBrowser.GetSelected().string().c_str(), "rb");
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size_t size = ftell(file);
|
|
|
|
rewind(file);
|
|
|
|
|
2020-11-13 12:07:30 +01:00
|
|
|
if (size > 0xFF'FFFF) {
|
|
|
|
fclose(file);
|
2020-11-12 21:20:51 +01:00
|
|
|
return;
|
2020-11-13 12:07:30 +01:00
|
|
|
}
|
2020-11-12 21:20:51 +01:00
|
|
|
|
|
|
|
fread(this->m_buffer, size, 1, file);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
this->parsePattern(this->m_buffer);
|
2020-11-13 11:15:07 +01:00
|
|
|
this->m_fileBrowser.ClearSelected();
|
2020-11-12 21:20:51 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-13 12:07:05 +01:00
|
|
|
void ViewPattern::setHighlight(u64 offset, std::string name, lang::Token::TypeToken::Type type, size_t size, u32 color) {
|
2020-11-10 21:31:04 +01:00
|
|
|
if (color == 0)
|
|
|
|
color = std::mt19937(std::random_device()())();
|
|
|
|
|
|
|
|
color &= ~0xFF00'0000;
|
|
|
|
color |= 0x5000'0000;
|
|
|
|
|
2020-11-13 12:07:05 +01:00
|
|
|
VariableType varType = { 0 };
|
|
|
|
|
|
|
|
switch (static_cast<u32>(type) & 0x0F) {
|
|
|
|
default:
|
|
|
|
case 0: varType.kind = VariableType::Kind::Unsigned; break;
|
|
|
|
case 1: varType.kind = VariableType::Kind::Signed; break;
|
|
|
|
case 2: varType.kind = VariableType::Kind::FloatingPoint; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
varType.size = size;
|
|
|
|
|
|
|
|
this->m_highlights.emplace_back(offset, varType, color, name);
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<std::derived_from<lang::ASTNode> T>
|
|
|
|
static std::vector<T*> findNodes(const lang::ASTNode::Type type, const std::vector<lang::ASTNode*> &nodes) {
|
|
|
|
std::vector<T*> result;
|
|
|
|
|
|
|
|
for (const auto & node : nodes)
|
|
|
|
if (node->getType() == type)
|
|
|
|
result.push_back(static_cast<T*>(node));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewPattern::parsePattern(char *buffer) {
|
|
|
|
static hex::lang::Lexer lexer;
|
|
|
|
static hex::lang::Parser parser;
|
|
|
|
|
|
|
|
this->m_highlights.clear();
|
|
|
|
|
|
|
|
auto [lexResult, tokens] = lexer.lex(buffer);
|
|
|
|
|
|
|
|
if (lexResult.failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto [parseResult, ast] = parser.parse(tokens);
|
|
|
|
if (parseResult.failed()) {
|
|
|
|
for(auto &node : ast) delete node;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &varNode : findNodes<lang::ASTNodeVariableDecl>(lang::ASTNode::Type::VariableDecl, ast)) {
|
|
|
|
if (!varNode->getOffset().has_value())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
u64 offset = varNode->getOffset().value();
|
|
|
|
if (varNode->getVariableType() != lang::Token::TypeToken::Type::CustomType) {
|
2020-11-13 12:07:05 +01:00
|
|
|
size_t size = static_cast<u32>(varNode->getVariableType()) >> 4 * varNode->getArraySize();
|
|
|
|
this->setHighlight(offset, varNode->getVariableName(), varNode->getVariableType(), size);
|
2020-11-10 21:31:04 +01:00
|
|
|
} else {
|
|
|
|
for (auto &structNode : findNodes<lang::ASTNodeStruct>(lang::ASTNode::Type::Struct, ast))
|
2020-11-13 11:15:34 +01:00
|
|
|
if (varNode->getCustomVariableTypeName() == structNode->getName()) {
|
|
|
|
for (u32 i = 0; i < varNode->getArraySize(); i++) {
|
2020-11-13 11:37:43 +01:00
|
|
|
std::string name = varNode->getVariableName();
|
|
|
|
if (varNode->getArraySize() > 1)
|
|
|
|
name += "[" + std::to_string(varNode->getArraySize()) + "]";
|
|
|
|
|
|
|
|
if (size_t size = this->highlightStruct(ast, structNode, offset, name); size == -1)
|
2020-11-13 11:15:34 +01:00
|
|
|
this->m_highlights.clear();
|
|
|
|
else
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &usingNode : findNodes<lang::ASTNodeTypeDecl>(lang::ASTNode::Type::TypeDecl, ast)) {
|
|
|
|
if (varNode->getCustomVariableTypeName() == usingNode->getTypeName()) {
|
|
|
|
for (u32 i = 0; i < varNode->getArraySize(); i++) {
|
2020-11-13 11:37:43 +01:00
|
|
|
std::string name = varNode->getVariableName();
|
|
|
|
if (varNode->getArraySize() > 1)
|
|
|
|
name += "[" + std::to_string(varNode->getArraySize()) + "]";
|
|
|
|
|
|
|
|
if (size_t size = this->highlightUsingDecls(ast, usingNode, varNode, offset, name); size == -1)
|
2020-11-13 11:15:34 +01:00
|
|
|
this->m_highlights.clear();
|
|
|
|
else
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto &node : ast) delete node;
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:37:43 +01:00
|
|
|
s32 ViewPattern::highlightUsingDecls(std::vector<lang::ASTNode*> &ast, lang::ASTNodeTypeDecl* currTypeDeclNode, lang::ASTNodeVariableDecl* currVarDecl, u64 offset, std::string name) {
|
2020-11-13 11:15:34 +01:00
|
|
|
u64 startOffset = offset;
|
|
|
|
|
2020-11-10 21:31:04 +01:00
|
|
|
if (currTypeDeclNode->getAssignedType() != lang::Token::TypeToken::Type::CustomType) {
|
2020-11-13 00:42:29 +01:00
|
|
|
size_t size = (static_cast<u32>(currTypeDeclNode->getAssignedType()) >> 4);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-13 12:07:05 +01:00
|
|
|
this->setHighlight(offset, name, currTypeDeclNode->getAssignedType(), size);
|
2020-11-10 21:31:04 +01:00
|
|
|
offset += size;
|
|
|
|
} else {
|
|
|
|
bool foundType = false;
|
|
|
|
for (auto &structNode : findNodes<lang::ASTNodeStruct>(lang::ASTNode::Type::Struct, ast))
|
|
|
|
if (structNode->getName() == currTypeDeclNode->getAssignedCustomTypeName()) {
|
2020-11-13 11:15:34 +01:00
|
|
|
for (size_t i = 0; i < currVarDecl->getArraySize(); i++) {
|
2020-11-13 11:37:43 +01:00
|
|
|
size_t size = this->highlightStruct(ast, structNode, offset, name);
|
2020-11-13 11:15:34 +01:00
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:04 +01:00
|
|
|
foundType = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:15:34 +01:00
|
|
|
for (auto &typeDeclNode : findNodes<lang::ASTNodeTypeDecl>(lang::ASTNode::Type::TypeDecl, ast)) {
|
2020-11-10 21:31:04 +01:00
|
|
|
if (typeDeclNode->getTypeName() == currTypeDeclNode->getAssignedCustomTypeName()) {
|
2020-11-13 11:15:34 +01:00
|
|
|
for (size_t i = 0; i < currVarDecl->getArraySize(); i++) {
|
2020-11-13 11:37:43 +01:00
|
|
|
size_t size = this->highlightUsingDecls(ast, typeDeclNode, currVarDecl, offset, name);
|
2020-11-13 11:15:34 +01:00
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:04 +01:00
|
|
|
foundType = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-11-13 11:15:34 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
|
|
|
|
if (!foundType)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:15:34 +01:00
|
|
|
return offset - startOffset;
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:37:43 +01:00
|
|
|
s32 ViewPattern::highlightStruct(std::vector<lang::ASTNode*> &ast, lang::ASTNodeStruct* currStructNode, u64 offset, std::string name) {
|
2020-11-10 21:31:04 +01:00
|
|
|
u64 startOffset = offset;
|
|
|
|
|
|
|
|
for (auto &node : currStructNode->getNodes()) {
|
|
|
|
auto var = static_cast<lang::ASTNodeVariableDecl*>(node);
|
|
|
|
|
|
|
|
if (var->getVariableType() != lang::Token::TypeToken::Type::CustomType) {
|
2020-11-13 11:37:43 +01:00
|
|
|
size_t size = (static_cast<u32>(var->getVariableType()) >> 4);
|
|
|
|
for (size_t i = 0; i < var->getArraySize(); i++) {
|
|
|
|
std::string memberName = name + "." + var->getVariableName();
|
|
|
|
if (var->getArraySize() > 1)
|
|
|
|
memberName += "[" + std::to_string(i) + "]";
|
|
|
|
|
2020-11-13 12:07:05 +01:00
|
|
|
this->setHighlight(offset, memberName, var->getVariableType(), size);
|
2020-11-13 11:37:43 +01:00
|
|
|
offset += size;
|
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
} else {
|
|
|
|
bool foundType = false;
|
2020-11-13 11:37:43 +01:00
|
|
|
for (auto &structNode : findNodes<lang::ASTNodeStruct>(lang::ASTNode::Type::Struct, ast)) {
|
2020-11-10 21:31:04 +01:00
|
|
|
if (structNode->getName() == var->getCustomVariableTypeName()) {
|
2020-11-12 23:57:43 +01:00
|
|
|
for (size_t i = 0; i < var->getArraySize(); i++) {
|
2020-11-13 11:37:43 +01:00
|
|
|
std::string memberName = name + "." + var->getVariableName();
|
|
|
|
if (var->getArraySize() > 1)
|
|
|
|
memberName += "[" + std::to_string(i) + "]";
|
|
|
|
|
|
|
|
size_t size = this->highlightStruct(ast, structNode, offset, memberName);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-12 23:57:43 +01:00
|
|
|
if (size == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
offset += size;
|
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
|
|
|
|
foundType = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-11-13 11:37:43 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-13 11:15:34 +01:00
|
|
|
for (auto &typeDeclNode : findNodes<lang::ASTNodeTypeDecl>(lang::ASTNode::Type::TypeDecl, ast)) {
|
2020-11-10 21:31:04 +01:00
|
|
|
if (typeDeclNode->getTypeName() == var->getCustomVariableTypeName()) {
|
2020-11-13 00:42:29 +01:00
|
|
|
for (size_t i = 0; i < var->getArraySize(); i++) {
|
2020-11-13 11:37:43 +01:00
|
|
|
std::string memberName = name + "." + var->getVariableName();
|
|
|
|
if (var->getArraySize() > 1)
|
|
|
|
memberName += "[" + std::to_string(i) + "]";
|
|
|
|
|
|
|
|
size_t size = this->highlightUsingDecls(ast, typeDeclNode, var, offset, memberName);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-13 00:42:29 +01:00
|
|
|
if (size == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
offset += size;
|
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
|
|
|
|
foundType = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-11-13 11:15:34 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
|
|
|
|
if (!foundType)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset - startOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|