-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTypes.py
More file actions
191 lines (135 loc) · 3.79 KB
/
Copy pathDataTypes.py
File metadata and controls
191 lines (135 loc) · 3.79 KB
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
import struct
import json
class DataType:
pattern = ""
length = 0
def __init__(self, value=None):
self.value = value
def setValue(self, value):
self.value = value
def getValue(self):
return self.value
def pack(self):
return struct.pack(f">{self.pattern}", self.value)
def unpack(self, value):
data = b""
for i in range(self.length):
data += bytes([value.pop(0)])
self.value, = struct.unpack(f">{self.pattern}", data)
return self.value
class Boolean(DataType):
def pack(self):
return b"\x01" if self.value else b"\x00"
def unpack(self, input):
self.value = bytearray(b"\x01")[0] == input.pop(0)
return self.value, input
class Byte(DataType):
pattern = "b"
length = 1
class UnsignedByte(DataType):
pattern = "B"
length = 1
class Short(DataType):
pattern = "h"
length = 2
class UnsignedShort(DataType):
pattern = "H"
length = 2
class Int(DataType):
pattern = "i"
length = 4
class UnsignedInt(DataType):
pattern = "I"
length = 4
class Long(DataType):
pattern = "q"
length = 8
class UnsignedLong(DataType):
pattern = "Q"
length = 8
class Float(DataType):
pattern = "f"
length = 4
class Double(DataType):
pattern = "d"
length = 8
class VarInt(DataType):
def pack(self):
data = self.value
""" Pack the var int """
ordinal = b''
while True:
byte = data & 0x7F
data >>= 7
ordinal += struct.pack('B', byte | (0x80 if data > 0 else 0))
if data == 0:
break
if len(ordinal) > 5:
raise ValueError(f"{self.value} is out of the range of a VarInt")
return ordinal
def unpack(self, input):
""" Unpack the varint """
CONTINUE_BIT = bytearray(b'\x80')
SEGMENT_BITS = bytearray(b'\x7F')
data = 0
position = 0
while True:
ordinal = input.pop(0)
byte = ordinal
if (byte & CONTINUE_BIT[0]) == 0:
break
data |= (byte & SEGMENT_BITS[0]) << position
position += 7
if (position >= 32):
raise Exception("VarInt too long!")
self.value = data
return data, input
class VarLong(DataType):
def pack(self):
data = self.value
""" Pack the var int """
ordinal = b''
while True:
byte = data & 0x7F
data >>= 7
ordinal += struct.pack('B', byte | (0x80 if data > 0 else 0))
if data == 0:
break
if len(ordinal) > 7:
raise ValueError(f"{self.value} is out of the range of a VarLong")
return ordinal
def unpack(self, value):
""" Unpack the varint """
data = 0
for i in range(5):
ordinal = value.pop(0)
if len(ordinal) == 0:
break
byte = ord(ordinal)
data |= (byte & 0x7F) << 7 * i
if not byte & 0x80:
break
self.value = data
return data
class String(DataType):
def pack(self):
byte = self.value.encode("utf-8")
return VarInt(len(byte)).pack() + byte
def unpack(self, value):
leng, bytearray = VarInt().unpack(value)
nex = b""
for i in range(leng):
nex += bytes([value.pop()])
self.value = nex.decode("utf-8")[::-1]
return self.value
class Json(DataType):
def pack(self):
string = json.dumps(self.value)
return String(string).pack()
def unpack(self):
string = String().unpack(self.value)
return json.loads(string)
class Chat(String):
pass
class Identifier(String):
pass