-
Notifications
You must be signed in to change notification settings - Fork 1
/
inspecfiler.cpp
456 lines (357 loc) · 8.83 KB
/
inspecfiler.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// IntroFiler.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
//
#include "pch.h"
#include <cstdio>
#include <cstdlib>
#include <string>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include "Array.h"
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef float f32;
typedef double f64;
enum class ExprValueType {
eInvalid,
eMetadata,
eDataBlock,
eData,
eU8,
eS8,
eU16,
eS16,
eU32,
eS32,
eU64,
eS64,
};
enum ErrorCode {
eOk = 0,
eIndexOutOfBounds,
eWrongType,
};
struct MetaData;
struct DataBlock;
struct ExpressionValue {
ExprValueType type = ExprValueType::eInvalid;
union {
MetaData* metadata;
DataBlock* datablock;
struct {
u8* ptr;
u64 size;
} data;
u8 value_u8;
s8 value_s8;
u16 value_u16;
s16 value_s16;
u32 value_u32;
s32 value_s32;
u64 value_u64;
s64 value_s64;
};
ErrorCode dereference(u64 index, u8* ptr) {
if (type == ExprValueType::eData) {
if (index > data.size) {
return ErrorCode::eIndexOutOfBounds;
}
*ptr = data.ptr[index];
return ErrorCode::eOk;
}
return ErrorCode::eWrongType;
}
};
enum class ExprSelectorType {
eInvalid,
eStaticValue, // -> value
eMetadata, // index -> node
eDataBlock, // -> node
eNode, // node, data -> node
ePrevNode, // -> node
eNextNode, // -> node
eParentNode, // -> node
eSize, // node -> u64
eOffset, // node -> u64
eData, // node -> data
eDereference, // data, index -> u8
eValue, // node -> type
eOperator, // type, value, value -> value
};
enum class OperatorType {
eAdd,
eSub,
eMul,
eDiv,
eMod,
eEq,
};
// value[u8](12) -> 12
// metadata[0].parent[this.parent['section']['']] = 'www'
// type coersion: signed -> unsigned, smaller -> larger
struct ExpressionSelector {
ExprSelectorType type;
ExpressionValue static_value;
ExpressionSelector *arg1, *arg2;
ExprValueType expr_type;
ErrorCode evaluate(ExpressionValue* result) {
switch (type) {
default:
return ErrorCode::eWrongType;
case ExprSelectorType::eStaticValue:
*result = static_value;
return ErrorCode::eOk;
case ExprSelectorType::eMetadata:
case ExprSelectorType::eDataBlock:
case ExprSelectorType::eNode:
case ExprSelectorType::ePrevNode:
case ExprSelectorType::eNextNode:
case ExprSelectorType::eParentNode:
case ExprSelectorType::eSize:
case ExprSelectorType::eOffset:
case ExprSelectorType::eData:
case ExprSelectorType::eDereference:
case ExprSelectorType::eValue:
case ExprSelectorType::eOperator:
return ErrorCode::eWrongType;
}
}
ExprValueType get_type() {
switch (type) {
default:
return ExprValueType::eInvalid;
case ExprSelectorType::eStaticValue:
return static_value.type;
case ExprSelectorType::eMetadata:
return ExprValueType::eMetadata;
case ExprSelectorType::eDataBlock:
return ExprValueType::eDataBlock;
case ExprSelectorType::eNode:
if (arg1) return arg1->get_type();
else return ExprValueType::eInvalid;
case ExprSelectorType::ePrevNode:
case ExprSelectorType::eNextNode:
case ExprSelectorType::eParentNode:
if (arg1) return arg1->get_type();
else return ExprValueType::eDataBlock;
case ExprSelectorType::eSize:
case ExprSelectorType::eOffset:
return ExprValueType::eU64;
case ExprSelectorType::eData:
return ExprValueType::eData;
case ExprSelectorType::eDereference:
return ExprValueType::eU8;
case ExprSelectorType::eValue:
return expr_type;
case ExprSelectorType::eOperator:
return expr_type;
}
}
};
struct GeneralExpression {
ExpressionValue value;
ExpressionSelector selector;
};
/*
DataMatcher
Type
Static-Data
Generic-Data
String
null-terminating -> bool
size -> General Expression
type -> ASCII, UTF-8, ...
offset -> General Expression
size -> General Expression
optional -> bool
data/size
uvalue
svalue
Expression-Access
data -> u8*
data[i] -> u8
value_u8 -> u8
value_s8 -> s8
value_u16 -> u16
value_s16 -> s16
value_u32 -> u32
value_s32 -> s32
value_u64 -> u64
value_s64 -> s64
size -> u64
offset -> u64
Hidden-Metadata
%
Visible-Metadata
$
Data
*/
struct DataSource {
unsigned char* data;
size_t size;
};
DataSource* load_file(const char* file) {
DataSource* datasource = nullptr;
FILE *f = fopen(file, "rb");
if (f) {
datasource = new DataSource();
fseek(f, 0, SEEK_END);
datasource->size = ftell(f);
fseek(f, 0, SEEK_SET);
datasource->data = (unsigned char*)malloc(datasource->size);
fread(datasource->data, datasource->size, 1, f);
fclose(f);
}
return datasource;
}
struct Interpretation {
u32 id;
//string name
//how to convert from and to
bool dynamic_size;
};
enum class DataBlockType {
eInvalid,
eStatic,
eValue,
};
struct FileFormat;
struct DataBlock {
u32 id;
//string selector
DataBlockType type;
Endianess endianess;
u64 offset = 0;
u64 size = 0;
//data - interpretation
bool optional = false;
u8 *static_data = nullptr;
FileFormat* root_format;
u32 parent;
DynArray<u32> child_blocks;
DataBlock(FileFormat* format) :
id(0), type(DataBlockType::eInvalid),
endianess(Endianess::eUndefined),
offset(0), size(0), optional(false),
static_data(nullptr),
root_format(format), parent(0), child_blocks() {}
void init_static_check(u8 *ptr, u64 offset, u64 size, bool optional = false) {
this->type = DataBlockType::eStatic;
this->offset = offset;
this->size = size;
this->optional = optional;
this->static_data = ptr;
}
void init_value(u64 offset, u64 size, bool optional = false) {
this->type = DataBlockType::eValue;
this->offset = offset;
this->size = size;
this->optional = optional;
}
void add_child_block(DataBlock* child_block) {
child_blocks.push_back(child_block->id);
child_block->parent = id;
}
};
struct MetaData {
//string selector
};
u8 elf_header[4] = { 0x7F, 0x45, 0x4C, 0x46 };
u8 elf_1_byte = 0x01;
u8 elf_2_byte = 0x02;
struct FileFormat {
IdArray<DataBlock> data_blocks;
u32 root_block;
DataBlock* create_block() {
return &data_blocks[data_blocks.insert(DataBlock(this))];
}
};
FileFormat* setup_elf_datadefinition() {
FileFormat* format = new FileFormat();
DataBlock* block = format->create_block();
block->init_static_check(elf_header, 0, 4);
format->root_block = block->id;
{
DataBlock* sub_def = format->create_block();
sub_def->init_static_check(&elf_1_byte, 4, 1, true);
format->data_blocks[format->root_block].add_child_block(sub_def);
sub_def = format->create_block();
sub_def->init_static_check(&elf_1_byte, 5, 1, true);
format->data_blocks[format->root_block].add_child_block(sub_def);
sub_def = format->create_block();
sub_def->init_static_check(&elf_2_byte, 5, 1, true);
format->data_blocks[format->root_block].add_child_block(sub_def);
sub_def = format->create_block();
sub_def->init_static_check(&elf_1_byte, 6, 1);
format->data_blocks[format->root_block].add_child_block(sub_def);
//entry
sub_def = format->create_block();
sub_def->init_value(0x18, 4);
format->data_blocks[format->root_block].add_child_block(sub_def);
//flags
sub_def = format->create_block();
sub_def->init_value(0x1C, 4);
format->data_blocks[format->root_block].add_child_block(sub_def);
//section header start
sub_def = format->create_block();
sub_def->init_value(0x20, 4);
format->data_blocks[format->root_block].add_child_block(sub_def);
}
{
DataBlock* sub_def = format->create_block();
sub_def->init_static_check(&elf_2_byte, 4, 1, true);
format->data_blocks[format->root_block].add_child_block(sub_def);
}
return format;
}
bool parse_source_with_definition (FileFormat* format, u32 data_id, DataSource* data) {
bool matched = true;
DataBlock* def = &format->data_blocks[data_id];
switch (def->type) {
case DataBlockType::eStatic: {
u64 off = def->offset;
for (u64 i = 0; i < def->size; i++) {
if (data->data[off + i] != def->static_data[i]) {
printf("Static Data Missmatch 0x%" PRIx8 " != 0x%" PRIx8 "\n", data->data[off + i], def->static_data[i]);
}
}
printf("Static Data Matched\n");
}break;
case DataBlockType::eValue: {
u64 off = def->offset;
printf("Value 0x");
for (u64 i = 0; i < def->size; i++) {
printf("%02" PRIx8, data->data[off + i]);
}
printf("\n");
}break;
}
if (matched) {
for (u32 child_id : def->child_blocks) {
parse_source_with_definition(format, child_id, data);
}
}
else {
if (def->optional) {
}
else {
}
}
return true;
}
int main() {
const char* filename = "fibrec.elf";
DataSource* source = load_file(filename);
if (!source) {
printf("Failed to load file %s\n", filename);
}
printf("Loaded file %s of size: %zu Bytes\n", filename, source->size);
printf("%" PRIu8 "\n", source->data[4]);
FileFormat* format = setup_elf_datadefinition();
parse_source_with_definition(format, format->root_block, source);
}