-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.hexpat
More file actions
320 lines (235 loc) · 12 KB
/
pattern.hexpat
File metadata and controls
320 lines (235 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// ImHex hex pattern file to easily read out the binaries
#pragma endian big
#include <std/sys.pat>
#include <std/mem.pat>
#define ANNOTATION_C color("9600FF")
#define COMPLEX_ANNOTATION_C color("0000FF")
#define VALUE_C color("00FF89")
using INode;
using INodeEntry;
using INodeRootEntry;
using INodeStructureEntry;
using Structure;
using StructureField;
using ComplexDeclaration;
fn read_big_unsigned(u128 address, u128 size) {
u128 iterator = size;
u128 value = 0;
size -= 1;
while (iterator) {
iterator -= 1;
value |= std::mem::read_unsigned(address + size - iterator, 1) << (iterator * 8);
}
return value;
};
fn relative_to_pointer_end(u128 address) {
return $ + 4;
};
enum Type : u16 {
INODE = 0x0000,
NULL = 0x0001,
STRUCTURE = 0x0002,
ARRAY = 0x0004,
STRING = 0x0008,
WSTRING = 0x0010,
BOOL = 0x0020,
CHAR = 0x0040,
WCHAR = 0x0080,
SHORT = 0x0100,
INTEGER = 0x0200,
FLOAT = 0x0400,
LONG = 0x0800,
DOUBLE = 0x1000
};
struct DBInfo {
u32 annotation [[ANNOTATION_C, comment("The magic number telling the implementation that it's a database")]];
std::assert(annotation == 299792458, "Your bin file has to travel at the speed of light to go interstellar");
u32 version [[color("00FFF4"), comment("The version of the database")]];
std::assert(version == 1, "This version of the database is not supported");
u64 creation [[color("FF0000"), comment("The UNIX timestamp representing the creation date")]];
u64 update [[color("FFA200"), comment("The UNIX timestamp representing the last edit date")]];
};
namespace types {
using AnyType;
struct NULL {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading a NULL")]];
std::assert(annotation == Type::NULL, "The annotation is not a NULL");
};
struct Bool {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading a bool")]];
std::assert(annotation == Type::BOOL, "The annotation is not a bool");
bool value [[VALUE_C, comment("The value of the bool")]];
};
struct Char {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading a char")]];
std::assert(annotation == Type::CHAR, "The annotation is not a char");
char value [[VALUE_C, comment("The value of the char")]];
};
struct WChar {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading a wchar")]];
std::assert(annotation == Type::WCHAR, "The annotation is not a wchar");
char16 value [[VALUE_C, comment("The value of the wchar")]];
};
struct Short {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading a short")]];
std::assert(annotation == Type::SHORT, "The annotation is not a short");
u16 value [[VALUE_C, comment("The value of the short")]];
};
struct Integer {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading an integer")]];
std::assert(annotation == Type::INTEGER, "The annotation is not an integer");
u32 value [[VALUE_C, comment("The value of the integer")]];
};
struct Float {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading a float")]];
std::assert(annotation == Type::FLOAT, "The annotation is not a float");
float value [[VALUE_C, comment("The value of the float")]];
};
struct Long {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading a long")]];
std::assert(annotation == Type::LONG, "The annotation is not a long");
u64 value [[VALUE_C, comment("The value of the long")]];
};
struct Double {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading a double")]];
std::assert(annotation == Type::DOUBLE, "The annotation is not a double");
double value [[VALUE_C, comment("The value of the double")]];
};
struct Flags {
u16 annotation[[ANNOTATION_C, comment("The flags which represent the type")]];
std::assert(0x1FF9 & annotation == annotation, "The annotation contains invalid flags");
};
struct Structure {
Type annotation [[COMPLEX_ANNOTATION_C, comment("The annotation to tell the implementation that it's reading a structure")]];
std::assert(annotation == Type::STRUCTURE, "The annotation is not a structure");
Integer length [[comment("The length of the structure")]];
AnyType entries[length.value] [[comment("The entries of the structure")]];
};
struct Array {
Type annotation [[COMPLEX_ANNOTATION_C, comment("The annotation to tell the implementation that it's reading an array")]];
std::assert(annotation == Type::ARRAY, "The annotation is not an array");
Integer length [[comment("The length of the array")]];
AnyType entries[length.value] [[comment("The entries of the array")]];
};
struct String {
Type annotation [[COMPLEX_ANNOTATION_C, comment("The annotation to tell the implementation that it's reading a string")]];
std::assert(annotation == Type::STRING, "The annotation is not a string");
Integer length [[comment("The length of the string")]];
char string[length.value] [[color("FF7600"), comment("The bytes of the string")]];
};
struct WString {
Type annotation [[COMPLEX_ANNOTATION_C, comment("The annotation to tell the implementation that it's reading a wstring")]];
std::assert(annotation == Type::STRING, "The annotation is not a wstring");
Integer length [[comment("The length of the wstring")]];
char16 string[length.value] [[color("FF7600"), comment("The bytes of the wstring")]];
};
struct AnyType {
Type value_annotation [[hidden, no_unique_address]];
if (value_annotation == Type::NULL) {
NULL value [[comment("The value of the entry")]];
} else if (value_annotation == Type::STRUCTURE) {
Structure value [[comment("The value of the entry")]];
} else if (value_annotation == Type::ARRAY) {
Array value [[comment("The value of the entry")]];
} else if (value_annotation == Type::STRING) {
String value [[comment("The value of the entry")]];
} else if (value_annotation == Type::WSTRING) {
WString value [[comment("The value of the entry")]];
} else if (value_annotation == Type::BOOL) {
Bool value [[comment("The value of the entry")]];
} else if (value_annotation == Type::CHAR) {
Char value [[comment("The value of the entry")]];
} else if (value_annotation == Type::WCHAR) {
WChar value [[comment("The value of the entry")]];
} else if (value_annotation == Type::SHORT) {
Short value [[comment("The value of the entry")]];
} else if (value_annotation == Type::INTEGER) {
Integer value [[comment("The value of the entry")]];
} else if (value_annotation == Type::FLOAT) {
Float value [[comment("The value of the entry")]];
} else if (value_annotation == Type::LONG) {
Long value [[comment("The value of the entry")]];
} else if (value_annotation == Type::DOUBLE) {
Double value [[comment("The value of the entry")]];
} else {
std::assert(false, "The value annotation is invalid");
}
};
struct INodePointer {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading an pointer")]];
std::assert(annotation == Type::INTEGER, "The annotation is not an integer");
INode* address : u32 [[VALUE_C, pointer_base("relative_to_pointer_end"), comment("The address of the pointer")]];
};
struct EntryPointer {
Type annotation [[ANNOTATION_C, comment("The annotation to tell the implementation it's reading an pointer")]];
std::assert(annotation == Type::INTEGER, "The annotation is not an integer");
Array* address : u32 [[VALUE_C, pointer_base("relative_to_pointer_end"), comment("The address of the pointer")]];
};
}
struct Structure {
Type annotation [[COMPLEX_ANNOTATION_C, comment("The annotation to tell the implementation that it's reading a structure")]];
std::assert(annotation == Type::STRUCTURE, "The annotation is not a structure");
types::Integer length [[comment("The amount of entries in the structure")]];
StructureField entries[length.value] [[comment("The entries of the structure")]];
};
struct StructureField {
Type key_annotation [[hidden, no_unique_address]];
if (key_annotation == Type::STRING) {
types::String key [[coment("The name of the object field")]];
} else if (key_annotation == Type::WSTRING) {
types::WString key [[coment("The name of the object field")]];
} else {
std::assert(false, "The key is not a string or wstring");
}
u16 value_annotation [[hidden, no_unique_address]];
if (value_annotation != Type::ARRAY && value_annotation != Type::STRUCTURE) {
types::Flags basic_flags [[comment("The type flags of the field")]];
}
ComplexDeclaration complex_declarations[while(read_big_unsigned($, 2) == Type::ARRAY || read_big_unsigned($, 2) == Type::STRUCTURE)] [[comment("The complex declarations of the field")]];
};
struct INode {
Type annotation [[color("FEFF00"), comment("The annotation to tell the implementation that it's reading an INode")]];
std::assert(annotation == Type::INODE, "The annotation is not an INode");
Type key_annotation [[hidden, no_unique_address]];
if (key_annotation == Type::STRUCTURE) {
Structure structure [[comment("A structure definition for the provided entries, if it's left empty it's referring to other INodes")]];
types::Integer length [[comment("The amount of entries in the INode")]];
INodeStructureEntry entries[length.value] [[comment("The entries of the INode")]];
} else if (key_annotation == Type::INTEGER) {
types::Integer length [[comment("The amount of entries in the INode")]];
INodeRootEntry entries[length.value] [[comment("The entries of the INode")]];
} else {
std::assert(false, "The annotation is not a structure or an integer");
}
};
struct INodeEntry {
Type key_annotation [[hidden, no_unique_address]];
if (key_annotation == Type::STRING) {
types::String key [[coment("The name representing the entry")]];
} else if (key_annotation == Type::WSTRING) {
types::WString key [[coment("The name representing the entry")]];
} else if (key_annotation == Type::INTEGER) {
types::Integer key [[coment("The index representing the entry")]];
} else {
std::assert(false, "The key is not a string, wstring or integer");
}
};
struct INodeRootEntry : INodeEntry {
types::INodePointer offset [[comment("The offset of the entry relative to the end of this field")]];
};
struct INodeStructureEntry : INodeEntry {
types::EntryPointer offset [[comment("The offset of the entry relative to the end of this field")]];
};
struct ComplexDeclaration {
Type type_annotation [[hidden, no_unique_address]];
if (type_annotation == Type::ARRAY) {
Type annotation [[COMPLEX_ANNOTATION_C, comment("The annotation to tell the implementation it's reading an array")]];
types::Flags flags [[comment("The type flags of the array")]];
} else if (type_annotation == Type::STRUCTURE) {
Structure structure [[comment("The structure of the object")]];
} else {
std::assert(false, "The type is not a structure or array");
}
};
DBInfo info @ 0x00;
INode root @ $;