From f0963603bff9ab4b496df290a60696a2941419dc Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 27 Aug 2022 12:41:59 +0200 Subject: [PATCH] patterns: Added bencode pattern --- README.md | 1 + patterns/bencode.hexpat | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 patterns/bencode.hexpat diff --git a/README.md b/README.md index 0b8ea76..e13fce8 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Hex patterns, include patterns and magic files for the use with the ImHex Hex Ed | IP | | `patterns/ip.hexpat` | Ethernet II Frames (IP Packets) | | 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 | ### Scripts diff --git a/patterns/bencode.hexpat b/patterns/bencode.hexpat new file mode 100644 index 0000000..0065c03 --- /dev/null +++ b/patterns/bencode.hexpat @@ -0,0 +1,73 @@ +#pragma MIME application/x-bittorrent + +#include +#include +#include + +namespace bencode { + + struct ASCIIDecimal { + char value[while(std::ctype::isdigit(std::mem::read_unsigned($, 1)))]; + } [[sealed, format("bencode::format_ascii_decimal"), transform("bencode::format_ascii_decimal")]]; + + fn format_ascii_decimal(ASCIIDecimal adsasd) { + return std::string::parse_int(adsasd.value, 10); + }; + + enum Type : u8 { + Integer = 'i', + Dictionary = 'd', + List = 'l', + + String0 = '0', + String1 = '1', + String2 = '2', + String3 = '3', + String4 = '4', + String5 = '5', + String6 = '6', + String7 = '7', + String8 = '8', + String9 = '9' + }; + + struct String { + ASCIIDecimal length; + char separator [[hidden]]; + char value[length]; + } [[sealed, format("bencode::format_string"), transform("bencode::format_string")]]; + + fn format_string(String string) { + return string.value; + }; + + using Bencode; + using Value; + + struct DictionaryEntry { + String key; + Value value; + }; + + struct Value { + Type type; + + if (type == Type::Dictionary) { + DictionaryEntry entry[while(std::mem::read_unsigned($, 1) != 'e')]; + } else if (type == Type::Integer) { + ASCIIDecimal value; + char end; + } else { + $ -= 1; + String value; + } + }; + + struct Bencode { + Value value[while(!std::mem::eof())] [[inline]]; + char end; + }; + +} + +bencode::Bencode bencode @ 0x00;