-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfbs.py
90 lines (58 loc) · 1.7 KB
/
fbs.py
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
import struct
def decode_vt_at(data, offset):
vt_len = get_short(data, offset)
tbl_len = get_short(data, offset + 2)
if (vt_len < 0) or (tbl_len < 0):
raise Exception('Invalid virtual table')
if (vt_len % 2):
raise Exception('Invalid virtual table')
vt_nrent = int(vt_len / 2)
if vt_nrent < 3:
raise Exception('Invalid virtual table')
entries = struct.unpack('<' + 'h' * vt_nrent, data[offset: offset + vt_len])[2:]
return {
'vt_len': vt_len,
'tbl_len': tbl_len,
'entries': entries
}
def deref_offset(data, offset, reverse=False):
off = get_int(data, offset)
if reverse:
return offset - off
else:
return offset + off
def get_table_vt(data, offset):
vt_offset = deref_offset(data, offset, True)
return decode_vt_at(data, vt_offset)
def get_int(data, offset):
return struct.unpack('<i', data[offset: offset + 4])[0]
def get_short(data, offset):
return struct.unpack('<h', data[offset: offset + 2])[0]
def get_vector_el(data, offset, el_type, n):
vector_len = get_int(data, offset)
vector_base = offset + 4
if n >= vector_len:
raise Exception('Invalid offset or vector')
el_size = types[el_type]['size']
return vector_base + n * el_size
def get_table_field(data, offset, n):
vt = get_table_vt(data, offset)
e_off = vt['entries'][n]
if e_off == 0:
return None
return offset + e_off
def hex_dump(buf):
ret = ''
for e in buf:
ret += ' {:02X}'.format(e)
return ret
types = {
'int': {
'decode': get_int,
'size': 4
},
'short': {
'decode': get_short,
'size': 2
}
}