This repository has been archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BinaryHelper.py
143 lines (108 loc) · 3.78 KB
/
BinaryHelper.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
# /usr/bin/python2
import struct
class BinaryReader:
def __init__(self, base_stream):
self.base_stream = base_stream
def Seek(self, position, seekorigin=0):
return self.base_stream.seek(position, seekorigin)
def Tell(self):
return self.base_stream.tell()
def ReadByte(self):
return self.base_stream.read(1)
def ReadBytes(self, count):
return self.base_stream.read(count)
def ReadChar(self):
return ord(self.base_stream.read(1))
def ReadChars(self, count):
return struct.unpack('%dB', self.base_steam.read(count))
def ReadInt16(self):
return struct.unpack('h', self.base_stream.read(2))[0]
def ReadInt32(self):
return struct.unpack('i', self.base_stream.read(4))[0]
def ReadInt64(self):
return struct.unpack('q', self.base_stream.read(8))[0]
def ReadUInt16(self):
return struct.unpack('H', self.base_stream.read(2))[0]
def ReadUInt32(self):
return struct.unpack('I', self.base_stream.read(4))[0]
def ReadUInt64(self):
return struct.unpack('Q', self.base_stream.read(8))[0]
def ReadFloat(self):
return struct.unpack('f', self.base_stream.read(4))[0]
def ReadBEInt16(self):
return struct.unpack('>h', self.base_stream.read(2))[0]
def ReadBEInt32(self):
return struct.unpack('>i', self.base_stream.read(4))[0]
def ReadBEInt64(self):
return struct.unpack('>q', self.base_stream.read(8))[0]
def ReadBEUInt16(self):
return struct.unpack('>H', self.base_stream.read(2))[0]
def ReadBEUInt32(self):
return struct.unpack('>I', self.base_stream.read(4))[0]
def ReadBEUInt64(self):
return struct.unpack('>Q', self.base_stream.read(8))[0]
def Read7BitEncodedInt(self):
a, b, c = 0, 0, 0
mask = 0x7f
shift = 7
while c < 5:
d = ord(self.ReadByte())
a |= (d & mask) << b
b += shift
if ((d & (mask + 1)) == 0):
break
c += 1
if c >= 5:
raise NameError, ("Too many bytes in 7 bit encoded Int32")
return a
def ReadDotNETString(self):
capacity = self.Read7BitEncodedInt()
if capacity < 0:
raise IOError, ("Invalid binary file (string len < 0)")
if capacity == 0:
return ""
string_buffer = self.ReadBytes(capacity)
return string_buffer
def ReadCString(self):
string = ""
c = True
while (c):
var = self.ReadByte()
if not var:
break
if (var != "\x00"):
string += var
else:
self.Seek(-1, 1)
c = False
break
return string
class BinaryWriter:
def __init__(self, base_stream):
self.base_stream = base_stream
def Seek(self, position, seekorigin=0):
return self.base_stream.seek(position, seekorigin)
def Tell(self):
return self.base_stream.tell()
def WriteByte(self, value):
self.base_stream.write(value[0])
def FillBytes(self, value, count):
if count <= 0:
count = 0
if len(value) <= count:
value += '\x00' * (count - len(value))
self.base_stream.write(value[:count])
def Write7BitEncodedInt(self, value):
if value == 0:
self.WriteByte(chr(value))
while value != 0:
num = (value >> 7) & 0x1ffffff
num2 = value & 0x7f
if num != 0:
num2 = num2 | 0x80
self.WriteByte(chr(num2))
value = num
def WriteDotNETString(self, value):
byteCount = len(value)
self.Write7BitEncodedInt(byteCount)
self.WriteBytes(value, len(value))