-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryFile.py
41 lines (30 loc) · 843 Bytes
/
BinaryFile.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
import codecs
class BinaryFile:
def __init__(self, path):
self.file = open(path, 'rb')
def seek(self, offset):
self.file.seek(offset)
def read(self, size, offset=None, output_format='h', big_endian=True):
block = []
# if offset provided, seek
if offset:
self.file.seek(offset)
for i in range(size):
byte = self.file.read(1)
byte = byte.hex()
block.append(byte)
# reverse block for little endian translation and convert to string
if big_endian:
block = reversed(block)
# convert array to string
block = ''.join(block)
# if dec_output flag set, convert to decimal int
if output_format == 'd':
return int(block, 16)
else: # output_format == 'h'
return block
def close(self):
self.file.close()
@staticmethod
def decode_ansi(text):
return str(codecs.decode(text, "hex"), 'ansi')