forked from Ankermgmt/ankermake-m5-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamtypes.py
221 lines (161 loc) · 4.45 KB
/
amtypes.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
## ------------------------------------------
## Generated by Transwarp
##
## THIS FILE IS AUTOMATICALLY GENERATED.
## DO NOT EDIT. ALL CHANGES WILL BE LOST.
## ------------------------------------------
import struct
import enum
from dataclasses import dataclass, field
import socket
def _assert_equal(value, expected):
if value != expected:
raise ValueError(f"expected {expected} but found {value}")
class Zeroes:
@classmethod
def parse(cls, p, num):
body = p[:num]
_assert_equal(set(body) - {0}, set())
return body, p[num:]
def pack(self, num):
return b"\x00" * num
class Bytes(bytes):
@classmethod
def parse(cls, p, size):
return p[:size], p[size:]
def pack(self, size):
return self
class String(Bytes):
@classmethod
def parse(cls, p, size):
body, p = super().parse(p, size)
_assert_equal(body[-1], 0)
return body[:-1].decode(), p
def pack(self, size):
return self[:size-1].ljust(size, '\x00').encode()
class Array:
@classmethod
def parse(cls, p, elem, num):
res = []
for _ in range(num):
item, p = elem.parse(p)
res.append(item)
return res, p
def pack(self, cls, num):
return b"".join(cls.pack(e) for e in self)
class IPv4(str):
@classmethod
def parse(cls, p):
addr = p[:4][::-1]
return cls(socket.inet_ntoa(addr)), p[4:]
def pack(self):
return socket.inet_aton(self)[::-1]
class Magic(bytes):
@classmethod
def parse(cls, p, size, expected):
v, p = p[:size], p[size:]
_assert_equal(v, expected)
return cls(v), p
def pack(self, size, expected):
return self
class Tail(bytes):
@classmethod
def parse(cls, p):
return cls(p), b""
def pack(self):
if isinstance(self, bytes):
return self
else:
return self.pack()
class IntType(int):
pass
class i8be(IntType):
size = 1
@classmethod
def parse(cls, p):
return cls(struct.unpack(">b", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack(">b", self)
class i8le(IntType):
size = 1
@classmethod
def parse(cls, p):
return cls(struct.unpack("<b", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack("<b", self)
i8 = i8be
class u8be(IntType):
size = 1
@classmethod
def parse(cls, p):
return cls(struct.unpack(">B", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack(">B", self)
class u8le(IntType):
size = 1
@classmethod
def parse(cls, p):
return cls(struct.unpack("<B", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack("<B", self)
u8 = u8be
class i16be(IntType):
size = 2
@classmethod
def parse(cls, p):
return cls(struct.unpack(">h", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack(">h", self)
class i16le(IntType):
size = 2
@classmethod
def parse(cls, p):
return cls(struct.unpack("<h", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack("<h", self)
i16 = i16be
class u16be(IntType):
size = 2
@classmethod
def parse(cls, p):
return cls(struct.unpack(">H", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack(">H", self)
class u16le(IntType):
size = 2
@classmethod
def parse(cls, p):
return cls(struct.unpack("<H", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack("<H", self)
u16 = u16be
class i32be(IntType):
size = 4
@classmethod
def parse(cls, p):
return cls(struct.unpack(">i", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack(">i", self)
class i32le(IntType):
size = 4
@classmethod
def parse(cls, p):
return cls(struct.unpack("<i", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack("<i", self)
i32 = i32be
class u32be(IntType):
size = 4
@classmethod
def parse(cls, p):
return cls(struct.unpack(">I", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack(">I", self)
class u32le(IntType):
size = 4
@classmethod
def parse(cls, p):
return cls(struct.unpack("<I", p[:cls.size])[0]), p[cls.size:]
def pack(self):
return struct.pack("<I", self)
u32 = u32be