-
Notifications
You must be signed in to change notification settings - Fork 0
/
htl.py
173 lines (158 loc) · 5.71 KB
/
htl.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
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 26 12:08:40 2018
@author: kb1p
"""
import sys
import os
import os.path
import math
import string
import getopt
VER3 = sys.version_info.major >= 3
def message(msg):
sys.stderr.write(msg)
sys.stderr.write(os.linesep)
class Converter(object):
__slots__ = "nCols", "colSize", "delim"
__printableChars = frozenset(string.printable) - frozenset(string.whitespace) | frozenset(" ")
def __init__(self):
self.nCols = 8
self.colSize = 2
self.delim = " "
def bin2text(self, fin, fout, offset, size):
try:
if size < 0:
fin.seek(0, os.SEEK_END)
size = fin.tell() - offset
fin.seek(offset, os.SEEK_SET)
except IOError:
if size < 0:
size = sys.maxsize
addrFmt = "%%0%dx: " % int(math.ceil(math.log(offset + size, 16)))
blockSize = self.nCols * self.colSize
colRange = range(self.nCols)
needMore = size > 0
while needMore:
reqSize = min(blockSize, size)
data = memoryview(fin.read(reqSize))
dataSize = len(data)
if dataSize > 0:
fout.write(addrFmt % offset)
cols = ("".join("%02x" % (x if VER3 else ord(x)) \
for x in data[c * self.colSize:(c + 1) * self.colSize]) \
for c in colRange)
fout.write(" ".join(cols))
if blockSize > dataSize:
# padding
fout.write(" " * (blockSize - dataSize))
fout.write(self.delim)
fout.write("".join(c if c in self.__printableChars else "." \
for c in (map(chr, data) if VER3 else data)))
fout.write("\n")
size -= dataSize
offset += dataSize
needMore = size > 0 and dataSize == reqSize
def text2bin(self, fin, fout):
for line in fin:
# allow partial modification of the binary file
addrSep = line.find(":")
outLen = -1
if addrSep >= 0:
rangeStr = line[0:addrSep]
rangeSep = rangeStr.find("-")
if rangeSep >= 0:
if rangeSep > 0:
addr = int(rangeStr[0:rangeSep].strip(), 16)
else:
addr = fout.tell()
outLen = int(rangeStr[rangeSep + 1:].strip(), 16) - addr + 1
else:
addr = int(rangeStr.strip(), 16)
fout.seek(addr, os.SEEK_SET)
start = addrSep + 1
else:
start = 0
textSep = line.find(self.delim)
data = line[start:textSep] if textSep > 0 else line[start:]
bindata = bytearray()
for chunk in data.split():
if VER3:
blk = bytes(int(chunk[x:x+2], 16) for x in range(0, len(chunk), 2))
else:
blk = "".join(chr(int(chunk[x:x+2], 16)) for x in xrange(0, len(chunk), 2))
bindata += blk
dataLen = len(bindata)
if outLen < 0:
outLen = dataLen
while outLen > 0:
fout.write(bindata if outLen >= dataLen else bindata[0:outLen])
outLen -= dataLen
def parseNumber(strnum):
if strnum.startswith("0x"):
return int(strnum[2:], 16)
elif strnum.startswith("0"):
return int(strnum[1:], 8)
else:
return int(strnum)
def main(args):
showUsage = False
rev = False
conv = Converter()
offset, size = 0, -1
opts, freeargs = getopt.getopt(args[1:], "hrc:w:d:f:s:")
for o, a in opts:
if o == "-r":
rev = True
elif o == "-c":
conv.nCols = parseNumber(a)
elif o == "-w":
conv.colSize = parseNumber(a)
elif o == "-d":
conv.delim = a
elif o == "-f":
offset = parseNumber(a)
elif o == "-s":
size = parseNumber(a)
elif o == "-h":
showUsage = True
nFreeArgs = len(freeargs)
showUsage = showUsage or (nFreeArgs == 0 and rev)
if showUsage:
usage = """Usage:
Dump: %(prog)s [-c columns -w bytes_per_column -d delim_str -f offset -s size] [in-file] [out-file]
Patch: %(prog)s -r [-d delim_str] [in-file] out-file
Arguments in rectangular brackets are optional. When optional files
are not specified, use stdout (for dump) and stdin (for patch).
Numbers can be provided as hex literals (prefixed with '0x'), octal (prefixed with '0')
or decimal (default).
""" % { "prog": args[0] }
message(usage)
return 1
fin, fout = None, None
try:
if rev:
fin = open(freeargs[0], "rt") if nFreeArgs > 1 else sys.stdin
outFn = freeargs[-1]
writeMode = "r+b" if os.path.exists(outFn) else "wb"
fout = open(outFn, writeMode)
conv.text2bin(fin, fout)
else:
fin = open(freeargs[0], "rb") if nFreeArgs > 0 else (sys.stdin.buffer if VER3 else sys.stdin)
fout = open(freeargs[1], "wt") if nFreeArgs > 1 else sys.stdout
conv.bin2text(fin, fout, offset, size)
except IOError as err:
message("I/O error: %s, file: \"%s\"" % (err.strerror, err.filename))
except KeyboardInterrupt:
message("Operation interrupted by user")
finally:
if fout != None:
if fout == sys.stdout:
fout.flush()
else:
fout.close()
if fin not in (None, sys.stdin):
fin.close()
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))