-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.py
168 lines (125 loc) · 4.37 KB
/
binary.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
from struct import *
class BinaryStream:
def __init__(self, base_stream: bytes = b""):
self.base_stream = base_stream
self.offset = 0
def ReadByte(self):
if self.offset + length > len(self.base_stream):
print(f"{self.offset=} {length=}")
raise Exception("End of bytes reached")
ret = self.base_stream[self.offset:self.offset+1]
self.offset += 1
def ReadBytes(self, length):
if self.offset + length > len(self.base_stream):
print(f"{self.offset=} {length=}")
raise Exception("End of bytes reached")
ret = self.base_stream[self.offset:self.offset+length]
self.offset += length
return ret
def ReadByteArray(self):
length = self.ReadInt32()
if length > 0:
ret = self.ReadBytes(length)
return ret
def ReadChar(self):
return self.unpack('b')
def ReadUChar(self):
return self.unpack('B')
def ReadBool(self):
return self.unpack('?')
def ReadUInt8(self):
return self.unpack('B', 1)
def ReadInt8(self):
return self.unpack('b', 1)
def ReadInt16(self):
return self.unpack('h', 2)
def ReadUInt16(self):
return self.unpack('H', 2)
def ReadInt32(self):
return self.unpack('i', 4)
def ReadUInt32(self):
return self.unpack('I', 4)
def ReadInt64(self):
return self.unpack('q', 8)
def ReadUInt64(self):
return self.unpack('Q', 8)
def ReadFloat(self):
return self.unpack('f', 4)
def ReadSingle(self):
return self.unpack('f', 4)
def ReadDouble(self):
return self.unpack('d', 8)
def ReadVector3(self):
return [self.ReadFloat(), self.ReadFloat(), self.ReadFloat()]
def ReadColor(self):
return [self.ReadUInt8(), self.ReadUInt8(), self.ReadUInt8()]
def ReadString(self):
length = self.ReadUInt16()
#print(length)
if length > 0:
return self.unpack(str(length) + 's', length).decode("utf-8")
return ""
def ReadStrings(self):
list_length = self.ReadUInt16()
#print(length)
ret = []
for i in range(list_length):
length = self.ReadUInt16()
ret.append(self.unpack(str(length) + 's', length).decode("utf-8"))
return ret
def ReadStringSingleByteLength(self):
length = int.from_bytes(self.ReadByte(), "little")
print(length)
return self.unpack(str(length) + 's', length).decode("utf-8")
def WriteByteArray(self, value: bytes):
self.WriteInt32(len(value))
self.WriteBytes(value)
def WriteBytes(self, value):
self.base_stream += value
def WriteChar(self, value):
self.pack('c', value)
def WriteUChar(self, value):
self.pack('C', value)
def WriteBool(self, value):
self.pack('?', value)
def WriteUInt8(self, value):
self.pack('B', value)
def WriteInt8(self, value):
self.pack('b', value)
def WriteInt16(self, value):
self.pack('h', value)
def WriteUInt16(self, value):
self.pack('H', value)
def WriteInt32(self, value):
self.pack('i', value)
def WriteUInt32(self, value):
self.pack('I', value)
def WriteInt64(self, value):
self.pack('q', value)
def WriteUInt64(self, value):
self.pack('Q', value)
def WriteFloat(self, value):
self.pack('f', value)
def WriteDouble(self, value):
self.pack('d', value)
def WriteVector3(self, value):
[self.WriteFloat(i) for i in value]
def WriteColor(self, value):
[self.WriteUInt8(i) for i in value]
def WriteString(self, value):
length = len(value)
self.WriteUInt16(length)
self.pack(str(length) + 's', value.encode("utf-8"))
def WriteStrings(self, value: list):
length = len(value)
self.WriteUInt16(length)
for string in value:
self.WriteString(string)
def WriteStringSingleByteLength(self, value):
length = len(value)
self.WriteUInt8(length)
self.pack(str(length) + 's', value.encode("utf-8"))
def pack(self, fmt, data):
return self.WriteBytes(pack(fmt, data))
def unpack(self, fmt, length = 1):
return unpack(fmt, self.ReadBytes(length))[0]