From b0d8b8186114c886135a866fc2eae88d245d1f85 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 28 Aug 2022 13:51:58 +0200 Subject: [PATCH] patterns: Added protobuf pattern --- README.md | 1 + patterns/protobuf.hexpat | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 patterns/protobuf.hexpat diff --git a/README.md b/README.md index e13fce8..6639558 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Hex patterns, include patterns and magic files for the use with the ImHex Hex Ed | UF2 | | `patterns/uf2.hexpat` | [USB Flashing Format](https://github.com/microsoft/uf2) | | File System | | `patterns/fs.hexpat` | Drive File System | | Bencode | `application/x-bittorrent` | `patterns/bencode.hexpat` | Bencode encoding, used by Torrent files | +| Protobuf | | `patterns/protobuf.hexpat` | Google Protobuf encoding | ### Scripts diff --git a/patterns/protobuf.hexpat b/patterns/protobuf.hexpat new file mode 100644 index 0000000..8b8e341 --- /dev/null +++ b/patterns/protobuf.hexpat @@ -0,0 +1,66 @@ +#include + +#include + +struct ZigZag32 { + u32 value; +} [[sealed, format("format_zigzag32")]]; + +fn format_zigzag32(ZigZag32 zigzag) { + return s32((zigzag.value << 1) ^ (zigzag.value >> 31)); +}; + +struct ZigZag64 { + u64 value; +} [[sealed, format("format_zigzag64")]]; + +fn format_zigzag64(ZigZag64 zigzag) { + return s64((zigzag.value << 1) ^ (zigzag.value >> 63)); +}; + +enum WireType : u8 { + Varint = 0, + _64Bit = 1, + LengthDelimited = 2, + StartGroup = 3, + EndGroup = 4, + _32Bit = 5 +}; + +bitfield Key { + field_number : 5; + wire_type : 3; +} [[left_to_right]]; + +union _64Bit { + u64 fixed64; + ZigZag64 sfixed64; + double dbl; +}; + +union _32Bit { + u32 fixed32; + ZigZag32 sfixed32; + float flt; +}; + +struct LengthDelimited { + type::LEB128 length; + char data[length]; +}; + + +struct Entry { + Key key; + + if (key.wire_type == WireType::Varint) + type::LEB128 value; + else if (key.wire_type == WireType::_64Bit) + _64Bit value; + else if (key.wire_type == WireType::LengthDelimited) + LengthDelimited value; + else if (key.wire_type == WireType::_32Bit) + _32Bit value; +}; + +Entry entries[while(!std::mem::eof())] @ 0x00; \ No newline at end of file