-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpacking.py
62 lines (51 loc) · 1.82 KB
/
packing.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
import struct
import zlib
def buffer_read(format, data):
unpacked = struct.unpack_from(format, data)
size = struct.calcsize(format)
return data[size:], unpacked[0] # [size:] means skip ahead size amount of bytes
def readAIOHeader(data):
packetsize, = struct.unpack("I", data[:3]+"\x00") # read first 3 bytes. this is the packet size
compression, = struct.unpack("B", data[3]) # the last byte is the compression type. 0=none, 1=zlib
return packetsize, compression # after that you do tcp.recv(packetsize) and check if decompression is needed
def makeAIOPacket(data, compression=0):
if compression == 1:
data = zlib.compress(data)
finaldata = struct.pack("I", len(data))[:3] # strip the 4th byte off of it
finaldata += struct.pack("B", compression) # compression type
return finaldata + data
def packString8(string):
string = string[:255]
l = len(string)
buf = struct.pack("B%ds"%l, l, string)
return buf
def packString16(string):
string = string[:65535]
l = len(string)
buf = struct.pack("H%ds"%l, l, string)
return buf
def unpackString8(data):
l, = struct.unpack_from("B", data)
string, = struct.unpack_from("%ds"%l, data[1:])
return data[struct.calcsize("B%ds"%l):], string[:l]
def unpackString16(data):
l, = struct.unpack_from("H", data)
string, = struct.unpack_from("%ds"%l, data[2:])
return data[struct.calcsize("H%ds"%l):], string[:l]
def versionToInt(ver):
v = ver.split(".")
major = v[0]
minor = v[1]
patch = v[2] if len(v) > 2 else "0"
try:
return int(major+minor+patch)
except:
return int(major+minor+"0")
def versionToStr(ver):
major = ver[1]
minor = ver[0]
if len(ver) > 2:
patch = ver[2]
else:
return major+"."+minor
return major+"."+minor+"."+patch