-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrid2bt.py
192 lines (152 loc) · 4.98 KB
/
trid2bt.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
"""
Create a .bt Binary Template for 010 Editor from a TrID's definition
"""
PROGRAM_VER = "0.61b"
import sys
import xml.etree.ElementTree as ET
import random
import argparse
import string
def header_intro():
"""
Show some credits.
"""
print
print "TrID2bt v%s - (C) 2016 Marco Pontello" % (PROGRAM_VER)
print
def hex2bytes(string):
"""
Convert a list of HEX values to a bytes sequence
"""
bytes = []
for i in range(0, len(string), 2):
bytes.append(chr(int(string[i:i+2], 16)))
return "".join(bytes)
class TrIDDefLite():
"""
Define the bare minimum needed for a TrID definition.
"""
def __init__(self):
self.filetype= ""
self.ext= ""
self.patterns= []
self.strings= []
def __str__(self):
text = "FileType: '%s', Ext: '%s', Patterns: %d, Strings: %d" % \
(self.filetype, self.ext,
len(self.patterns), len(self.strings))
return text
def load_trid_def(filename):
"""
Parse a TrID's XML definition and return a TrIDDefLite object
"""
triddef = TrIDDefLite()
try:
trid = ET.parse(filename).getroot()
triddef.filetype = trid.find("Info/FileType").text
#consider only the first extension
triddef.ext = trid.find("Info/Ext")
if triddef.ext.text:
triddef.ext = triddef.ext.text.split("/")[0]
else:
triddef.ext = ""
elist = trid.findall("FrontBlock/Pattern")
for pat in elist:
for patdata in pat.getchildren():
ppos = 0
bytes = ""
if patdata.tag == "Pos":
ppos = int(patdata.text)
elif patdata.tag == "Bytes":
pbytes = hex2bytes(patdata.text)
triddef.patterns.append( (ppos, pbytes) )
elist = trid.findall("GlobalStrings/String")
for ele in elist:
#decodes the zeros-bytes
triddef.strings.append(ele.text.replace("'", "\x00").upper())
except IOError:
print "* Error: unable to access TrID's definition."
except ET.ParseError as detail:
print "* Error: XML -", detail
except:
print "* Error:", sys.exc_info()[0]
return triddef
def bytes2c(buff):
"""
Convert a byte buffer to a 'c style' literal constant
"""
text = []
printables = string.digits + string.letters
for c in buff:
if c in printables:
text.append(c)
else:
text.append("\\x"+hex(ord(c))[2:])
return "".join(text)
def writebt(triddef, btname):
"""
Create a binary template using the patterns from the given
TrID definition.
"""
text = ("// TrID2bt v%s - 010 Editor Binary Template generator\n\n" %
(PROGRAM_VER) )
text += 'Printf( "TrID filetype: %s\\n");\n' % (triddef.filetype)
text += "local int errs = 0;\n\n"
#evaluate patterns
patnum = 0
for pat in triddef.patterns:
pos = pat[0]
patbytes = pat[1]
patnum += 1
text += "FSeek(%d);\n" % (pos)
text += "char patt%d[%d] <bgcolor=cAqua>;\n" % (patnum, len(patbytes))
text += 'if (patt%d != "%s") {\n' % (patnum, bytes2c(patbytes))
text += ' errs++;\n'
text += ' Printf( "patt%d doesn''t match!\\n");\n' % (patnum)
text += ' if (errs==1) SetCursorPos(%d);\n' % (pos)
text += '}\n'
text += "\n"
text += 'Printf( "Marked %%d pattern(s)\\n", %d );\n' % (len(triddef.patterns))
text += 'if (errs > 0) Printf( "*** Wrong matches: %d ***", errs );\n'
print "Writing file '%s'" % (btname)
try:
f = open(btname, "w")
f.write(text)
f.close()
except:
print "* Error: unable to write file!"
def get_cmdline():
"""
Evaluate command line parameters, usage & help.
"""
parser = argparse.ArgumentParser(
description="Create fake files from a TrID's definition",
prefix_chars='-',
version = "TrID2Files/Python v" + PROGRAM_VER)
parser.add_argument("filename", action="store",
help = "TrID's defintion")
parser.add_argument("tempname", action="store", default="new.bt",
nargs="?", help="binary template to create")
res = parser.parse_args()
param = {}
param["filename"] = res.filename
param["btname"] = res.tempname
return param
def main():
header_intro()
params = get_cmdline()
filename = params["filename"]
btname = params["btname"]
print "Reading TrID's definition '%s'..." % (filename)
triddef = load_trid_def(filename)
if len(triddef.patterns) == 0:
print "Not enough info available."
sys.exit(1)
print "FileType :", triddef.filetype
print "Extension:", triddef.ext
print "Patterns :", len(triddef.patterns)
print "Strings :", len(triddef.strings)
writebt(triddef, btname)
print "Done."
if __name__ == "__main__":
main()