-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
148 lines (126 loc) · 4.09 KB
/
parser.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
import os
import binascii
import struct
import ctypes
class packetuint:
@staticmethod
def put(n,bytes,offset=0):
n = ctypes.c_int32(n).value
bytes = list(bytes)
if n<0 or n >= (1<<21):
bytes.insert(0+offset,(0x80 | (n&0x7f)))
bytes.insert(offset+1,(0x80 | ((n >> 7)&0x7f)))
bytes.insert(offset+2,(0x80 | ((n >> 14)&0x7f)))
bytes.insert(offset+3,(n >> 21)&0xff)
bytes = bytearray(bytes)
return bytes,4
elif n<(1<<7):
bytes.insert(offset,n)
bytes = bytearray(bytes)
return bytes,1
elif n < (1<<14):
bytes.insert(offset,(0x80 | (n & 0x7f)))
bytes.insert(offset+1,n>>7)
bytes = bytearray(bytes)
return bytes,2
else:
bytes.insert(offset,(0x80 | (n & 0x7f)))
bytes.insert(offset+1,(0x80 | ((n>>7) & 0x7f)))
bytes.insert(offset+2,n>>14)
bytes = bytearray(bytes)
return bytes,3
@staticmethod
def replace(n,bytes,offset=0):
bytes = list(bytes)
bytes, removed = packetuint.remove(bytes,offset)
bytes = bytearray(bytes)
bytes,len = packetuint.put(n,bytes,offset)
return bytes,len,removed
@staticmethod
def remove(bytes,offset=0):
c=0
for c in range(packetuint.get(bytes,offset)[1]):
bytes.pop(offset)
return bytes, c+1
@staticmethod
def get(bytes,offset=0):
i = 1
n = bytes[0+offset]
if n & 0x80:
n += (bytes[1+offset] << 7) - 0x80
i=2
if n & (1<<14):
n += (bytes[2+offset] << 14) - (1<<14)
i=3
if n & (1<<21):
n += (bytes[3+offset] << 21) - (1<<21)
i=4
if n & (1<<28):
n |= 0xF0000000
return n,i
@staticmethod
def getcoord(bytes,offset=0):
coord, i = packetuint.get(bytes,offset)
return ctypes.c_int32(coord).value/16,i
@staticmethod
def getcoords(bytes,offset=0):
x,ix = packetuint.getcoord(bytes,offset)
y,iy = packetuint.getcoord(bytes,offset+ix)
z,iz = packetuint.getcoord(bytes,offset+ix+iy)
return (x,y,z,ix+iy+iz)
class packetint:
@staticmethod
def remove(bytes,offset=0):
bytes = list(bytes)
c=0
for c in range(packetint.get(bytes,offset)[1]):
bytes.pop(offset)
bytes = bytearray(bytes)
return bytes, c+1
@staticmethod
def replace(n,bytes,offset=0):
bytes = list(bytes)
bytes, removed = packetint.remove(bytes,offset)
bytes = bytearray(bytes)
bytes,len = packetint.put(n,bytes,offset)
return bytes,len,removed
@staticmethod
def put(n,bytes,offset=0):
n = ctypes.c_int32(n).value
bytes = list(bytes)
if n <128 and n > -127:
bytes.insert(0+offset,n & 0xff)
bytes = bytearray(bytes)
return bytes,1
elif n<0x8000 and n>=-0x8000:
bytes.insert(0+offset,0x80)
bytes.insert(1+offset,n & 0xff)
bytes.insert(2+offset,n>>8 & 0xff)
bytes = bytearray(bytes)
return bytes, 3
else:
bytes.insert(0+offset,0x81)
bytes.insert(1+offset,n & 0xff)
bytes.insert(2+offset,n>>8 & 0xff)
bytes.insert(3+offset,n>>16 & 0xff)
bytes.insert(4+offset,n>>24 & 0xff)
bytes = bytearray(bytes)
return bytes, 5
@staticmethod
def get(bytes,offset=0):
c = bytes[0+offset]
i = 1
if c == 0x80:
n = bytes[1+offset]
n |= bytes[2+offset] << 8
i = 3
return ctypes.c_short(n).value
elif c == 0x81:
n = bytes[1+offset]
n = bytes[2+offset]<<8
n = bytes[3+offset]<<16
n = bytes[4+offset]<<24
i = 5
return ctypes.c_int32(n).value
else:
return struct.unpack("b",c.to_bytes(1,'little'))[0],i