forked from Blizzard/s2protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoders.py
320 lines (265 loc) · 9.91 KB
/
decoders.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Copyright (c) 2013 Blizzard Entertainment
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import struct
class TruncatedError(Exception):
pass
class CorruptedError(Exception):
pass
class BitPackedBuffer:
def __init__(self, contents, endian='big'):
self._data = contents or []
self._used = 0
self._next = None
self._nextbits = 0
self._bigendian = (endian == 'big')
def __str__(self):
return 'buffer(%02x/%d,[%d]=%s)' % (
self._nextbits and self._next or 0, self._nextbits,
self._used, '%02x' % (ord(self._data[self._used]),) if (self._used < len(self._data)) else '--')
def done(self):
return self._nextbits == 0 and self._used >= len(self._data)
def used_bits(self):
return self._used * 8 - self._nextbits
def byte_align(self):
self._nextbits = 0
def read_aligned_bytes(self, bytes):
self.byte_align()
data = self._data[self._used:self._used + bytes]
self._used += bytes
if len(data) != bytes:
raise TruncatedError(self)
return data
def read_bits(self, bits):
result = 0
resultbits = 0
while resultbits != bits:
if self._nextbits == 0:
if self.done():
raise TruncatedError(self)
self._next = ord(self._data[self._used])
self._used += 1
self._nextbits = 8
copybits = min(bits - resultbits, self._nextbits)
copy = (self._next & ((1 << copybits) - 1))
if self._bigendian:
result |= copy << (bits - resultbits - copybits)
else:
result |= copy << resultbits
self._next >>= copybits
self._nextbits -= copybits
resultbits += copybits
return result
def read_unaligned_bytes(self, bytes):
return ''.join([chr(self.read_bits(8)) for i in xrange(bytes)])
class BitPackedDecoder:
def __init__(self, contents, typeinfos):
self._buffer = BitPackedBuffer(contents)
self._typeinfos = typeinfos
def __str__(self):
return self._buffer.__str__()
def instance(self, typeid):
if typeid >= len(self._typeinfos):
raise CorruptedError(self)
typeinfo = self._typeinfos[typeid]
return getattr(self, typeinfo[0])(*typeinfo[1])
def byte_align(self):
self._buffer.byte_align()
def done(self):
return self._buffer.done()
def used_bits(self):
return self._buffer.used_bits()
def _array(self, bounds, typeid):
length = self._int(bounds)
return [self.instance(typeid) for i in xrange(length)]
def _bitarray(self, bounds):
length = self._int(bounds)
return (length, self._buffer.read_bits(length))
def _blob(self, bounds):
length = self._int(bounds)
result = self._buffer.read_aligned_bytes(length)
return result
def _bool(self):
return self._int((0, 1)) != 0
def _choice(self, bounds, fields):
tag = self._int(bounds)
if tag not in fields:
raise CorruptedError(self)
field = fields[tag]
return {field[0]: self.instance(field[1])}
def _fourcc(self):
return self._buffer.read_unaligned_bytes(4)
def _int(self, bounds):
return bounds[0] + self._buffer.read_bits(bounds[1])
def _null(self):
return None
def _optional(self, typeid):
exists = self._bool()
return self.instance(typeid) if exists else None
def _real32(self):
return struct.unpack('>f', self._buffer.read_unaligned_bytes(4))
def _real64(self):
return struct.unpack('>d', self._buffer.read_unaligned_bytes(8))
def _struct(self, fields):
result = {}
for field in fields:
if field[0] == '__parent':
parent = self.instance(field[1])
if isinstance(parent, dict):
result.update(parent)
elif len(fields) == 1:
result = parent
else:
result[field[0]] = parent
else:
result[field[0]] = self.instance(field[1])
return result
class VersionedDecoder:
def __init__(self, contents, typeinfos):
self._buffer = BitPackedBuffer(contents)
self._typeinfos = typeinfos
def __str__(self):
return self._buffer.__str__()
def instance(self, typeid):
if typeid >= len(self._typeinfos):
raise CorruptedError(self)
typeinfo = self._typeinfos[typeid]
return getattr(self, typeinfo[0])(*typeinfo[1])
def byte_align(self):
self._buffer.byte_align()
def done(self):
return self._buffer.done()
def used_bits(self):
return self._buffer.used_bits()
def _expect_skip(self, expected):
if self._buffer.read_bits(8) != expected:
raise CorruptedError(self)
def _vint(self):
b = self._buffer.read_bits(8)
negative = b & 1
result = (b >> 1) & 0x3f
bits = 6
while (b & 0x80) != 0:
b = self._buffer.read_bits(8)
result |= (b & 0x7f) << bits
bits += 7
return -result if negative else result
def _array(self, bounds, typeid):
self._expect_skip(0)
length = self._vint()
return [self.instance(typeid) for i in xrange(length)]
def _bitarray(self, bounds):
self._expect_skip(1)
length = self._vint()
return (length, self._buffer.read_aligned_bytes((length + 7) / 8))
def _blob(self, bounds):
self._expect_skip(2)
length = self._vint()
return self._buffer.read_aligned_bytes(length)
def _bool(self):
self._expect_skip(6)
return self._buffer.read_bits(8) != 0
def _choice(self, bounds, fields):
self._expect_skip(3)
tag = self._vint()
if tag not in fields:
self._skip_instance()
return {}
field = fields[tag]
return {field[0]: self.instance(field[1])}
def _fourcc(self):
self._expect_skip(7)
return self._buffer.read_aligned_bytes(4)
def _int(self, bounds):
self._expect_skip(9)
return self._vint()
def _null(self):
return None
def _optional(self, typeid):
self._expect_skip(4)
exists = self._buffer.read_bits(8) != 0
return self.instance(typeid) if exists else None
def _real32(self):
self._expect_skip(7)
return struct.unpack('>f', self._buffer.read_aligned_bytes(4))
def _real64(self):
self._expect_skip(8)
return struct.unpack('>d', self._buffer.read_aligned_bytes(8))
def _struct(self, fields):
self._expect_skip(5)
result = {}
field_index = 0
length = self._vint()
for i in xrange(length):
tag = self._vint()
while field_index < len(fields):
field = fields[field_index]
if tag == field[2]:
if field[0] == '__parent':
parent = self.instance(field[1])
if isinstance(parent, dict):
result.update(parent)
elif len(fields) == 1:
result = parent
else:
result[field[0]] = parent
else:
result[field[0]] = self.instance(field[1])
field_index += 1
break
elif tag < field[2]:
self._skip_instance()
break
else:
field_index += 1
else:
self._skip_instance()
return result
def _skip_instance(self):
skip = self._buffer.read_bits(8)
if skip == 0: # array
length = self._vint()
for i in xrange(length):
self._skip_instance()
elif skip == 1: # bitblob
length = self._vint()
self._buffer.read_aligned_bytes((length + 7) / 8)
elif skip == 2: # blob
length = self._vint()
self._buffer.read_aligned_bytes(length)
elif skip == 3: # choice
tag = self._vint()
self._skip_instance()
elif skip == 4: # optional
exists = self._buffer.read_bits(8) != 0
if exists:
self._skip_instance()
elif skip == 5: # struct
length = self._vint()
for i in xrange(length):
tag = self._vint()
self._skip_instance()
elif skip == 6: # u8
self._buffer.read_aligned_bytes(1)
elif skip == 7: # u32
self._buffer.read_aligned_bytes(4)
elif skip == 8: # u64
self._buffer.read_aligned_bytes(8)
elif skip == 9: # vint
self._vint()