From 3e5d6cf88c61fcdd1f5d96cb2ae7ecde0f7ccd25 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 14 Oct 2021 20:28:13 +0200 Subject: [PATCH] patterns: Added padding expressions in bitfields. Closes #326 --- .../libimhex/include/hex/pattern_language/ast_node.hpp | 9 ++++++--- plugins/libimhex/source/pattern_language/parser.cpp | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp index de72e7287..7bb8741f1 100644 --- a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp @@ -1334,11 +1334,14 @@ namespace hex::pl { [](auto &&offset) -> u8 { return static_cast(offset); } }, dynamic_cast(literal)->getValue()); - auto field = new PatternDataBitfieldField(evaluator->dataOffset(), bitOffset, bitSize, evaluator); - field->setVariableName(name); + // If a field is named padding, it was created through a padding expression and only advances the bit position + if (name != "padding") { + auto field = new PatternDataBitfieldField(evaluator->dataOffset(), bitOffset, bitSize, evaluator); + field->setVariableName(name); + fields.push_back(field); + } bitOffset += bitSize; - fields.push_back(field); } evaluator->popScope(); diff --git a/plugins/libimhex/source/pattern_language/parser.cpp b/plugins/libimhex/source/pattern_language/parser.cpp index 5ec25a8bc..6f59ac446 100644 --- a/plugins/libimhex/source/pattern_language/parser.cpp +++ b/plugins/libimhex/source/pattern_language/parser.cpp @@ -986,8 +986,9 @@ namespace hex::pl { if (MATCHES(sequence(IDENTIFIER, OPERATOR_INHERIT))) { auto name = getValue(-2).get(); bitfieldNode->addEntry(name, parseMathematicalExpression()); - } - else if (MATCHES(sequence(SEPARATOR_ENDOFPROGRAM))) + } else if (MATCHES(sequence(VALUETYPE_PADDING, OPERATOR_INHERIT))) { + bitfieldNode->addEntry("padding", parseMathematicalExpression()); + } else if (MATCHES(sequence(SEPARATOR_ENDOFPROGRAM))) throwParseError("unexpected end of program", -2); else throwParseError("invalid bitfield member", 0);