-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignature2bytegenerator.py
224 lines (197 loc) · 7.91 KB
/
signature2bytegenerator.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
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
"""Converts a PRONOM signature sequence to byte sequences."""
import random
class Sig2ByteGenerator:
"""Converts PRONOM syntax to bytes for writing to file."""
def __init__(self):
self.component_list = []
self.open_syntax = ["{", "(", "[", "?", "*"]
self.fillbyte = -1
def __del__(self):
del self.component_list[:]
@staticmethod
def int_list_from_sequence(bytes_):
"""Convert bytes to a list."""
return list(bytes.fromhex(bytes_))
def set_fillbyte(self, fillvalue):
"""Set the fill-byte for the class instance."""
if not isinstance(fillvalue, int):
self.fillbyte = "Random"
return
if fillvalue < 0 or fillvalue > 255:
self.fillbyte = "Random"
return
self.fillbyte = fillvalue
return
def check_syntax(self, signature):
"""If a signature component appears more than once in a
signature it should error.
"""
for i in self.open_syntax:
if signature.find(i) > -1:
return True
return False
def create_bytes(self, number):
"""Create bytes for a signature sequence."""
for _ in range(int(number)):
if self.fillbyte == "Random":
self.component_list.append(
hex(random.randint(0, 255))
.replace("0x", "")
.zfill(2)
.replace("L", "")
)
else:
self.component_list.append(
hex(self.fillbyte).replace("0x", "").zfill(2).replace("L", "")
)
return True
def process_curly(self, syn):
"""Process curly bracket syntax from PRONOM."""
syn = syn.replace("{", "")
syn = syn.replace("}", "")
if syn.find("-") == -1:
self.create_bytes(int(syn))
else:
new_str = syn.split("-")
if new_str[1] == "*":
val = int(new_str[0])
self.create_bytes(val + 10)
else:
val = (int(new_str[0]) + int(new_str[1])) / 2
self.create_bytes(val)
def process_square(self, syn):
"""Process square bracket syntax from PRONOM."""
syn = syn.replace("[", "")
syn = syn.replace("]", "")
# convert to ints and find mean value in range
if syn.find(":") > -1:
self.sqr_colon(syn)
# convert to ints and -1 so don't equal hex in not clause
elif syn.find("!") > -1:
self.sqr_not(syn)
# TODO: Copy and paste from container work... make submodule
def process_mask(self, syn, inverted=False):
"""Process mask syntax from PRONOM."""
syn = syn.replace("[", "")
syn = syn.replace("]", "")
val = 0
# negate first, else, mask...
if "!&" in syn and inverted is True:
syn = syn.replace("!&", "")
byte = int(syn, 16)
mask = byte & 0
val = mask
elif "&" in syn and inverted is False:
syn = syn.replace("&", "")
byte = int(syn, 16)
mask = byte & 255
val = mask
self.component_list.append(hex(val).replace("0x", "").zfill(2).replace("L", ""))
def sqr_colon(self, syn):
"""Process colon syntax in square bracket syntax from PRONOM."""
# convert to ints and find mean value in range
if syn.find(":") > -1:
new_str = syn.split(":")
val = (int(new_str[0], 16) + int(new_str[1], 16)) / 2
hex_ = hex(int(val)).replace("0x", "").zfill(2).replace("L", "")
# this is a hack to solve issue #8 we've never come across it before
# but it could conceivably happen again... depends how large the value
# is following a colon and if the hex representation is odd numbered
if len(hex_) % 2 != 0:
hex_ = (
hex(int(val))
.replace("0x", "")
.zfill(len(hex_) + 1)
.replace("L", "")
)
self.component_list.append(hex_)
def sqr_not(self, syn):
"""Process negated square bracket syntax from PRONOM."""
syn = syn.replace("!", "")
sequence = self.int_list_from_sequence(syn)
idx = 0
for _ in sequence: # this function could be seriously busted - check
if sequence[idx] == 0:
sequence[idx] = sequence[idx] + 1
else:
sequence[idx] = sequence[idx] - 1
self.component_list.append(
hex(sequence[idx]).replace("0x", "").zfill(2).replace("L", "")
)
idx += 1
def process_thesis(self, syn):
"""Process parenthesis syntax from PRONOM."""
syn = syn.replace("(", "").replace(")", "")
index = syn.find("|")
syn = syn[0:index]
if syn.find("[") == -1:
sequence = self.int_list_from_sequence(syn)
for item in sequence:
self.component_list.append(
hex(item).replace("0x", "").zfill(2).replace("L", "")
)
else:
self.process_square(syn)
def detailed_check(self, signature):
"""Perform a more detailed check of PRONOM syntax."""
index = 0
if len(signature) > 0:
check_byte = signature[0]
if check_byte == "{":
index = signature.find("}")
syn = signature[0 : index + 1]
self.process_curly(syn)
elif check_byte == "[":
# if we have a bytemask.
check_inverted = signature[1:3]
if check_inverted == "!&":
index = signature.find("]")
syn = signature[1 : index + 1]
self.process_mask(syn, True)
return signature[index + 1 :]
check_mask = signature[1:2]
if check_mask == "&":
index = signature.find("]")
syn = signature[0 : index + 1]
self.process_mask(syn)
return signature[index + 1 :]
# bytemask work ends.
index = signature.find("]")
syn = signature[0 : index + 1]
self.process_square(syn)
elif check_byte == "?":
syn = signature[0:index]
index = 1
self.create_bytes(1)
elif check_byte == "(":
index = signature.find(")")
syn = signature[0 : index + 1]
self.process_thesis(syn)
elif check_byte == "*":
self.create_bytes(20)
return signature[index + 1 :]
def process_signature(self, signature):
"""Process a signature provided by the caller."""
if signature != "":
if self.check_syntax(signature) is True:
i = 0
for item in signature:
if not item.isalnum(): # are all alphanumeric
element = signature[0:i]
if element != "": # may not be anything to append e.g. '??ab'
self.component_list.append(element)
signature = self.detailed_check(signature[i:])
break
i += 1
self.process_signature(signature)
else:
self.component_list.append(signature)
def map_signature(self, bofoffset, signature, eofoffset, fillvalue=-1):
"""Map a signature from PRONON."""
self.set_fillbyte(fillvalue)
if bofoffset != "null":
self.create_bytes(int(bofoffset)) # dangerous? need to check type?
self.process_signature(signature)
if eofoffset != "null":
self.create_bytes(int(eofoffset))
return self.component_list