Some progress on packing (pack() and unpack raw)

This commit is contained in:
ChillerDragon 2023-03-26 11:19:47 +02:00
parent e22530743c
commit a55817e8ab
4 changed files with 100 additions and 3 deletions

View file

@ -0,0 +1,23 @@
from twnet_parser.packet import *
from twnet_parser.messages7.system.map_change import MsgMapChange
def test_parse_7_real_map_change():
msg = MsgMapChange()
msg.unpack(
b'BlmapChill\x00' \
b'\xde\xcf\xaa\xee\x0b' \
b'\x8b\xbe\x8a\x01' \
b'\x08' \
b'\xa8\x15' \
b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28')
assert msg.message_name == 'map_change'
assert msg.name == 'BlmapChill'
assert msg.crc == -1592087519
assert msg.size == 1134475
assert msg.chunks_per_request == 8
assert msg.chunk_size == 1384
# TODO: fix sha unpack
# assert msg.sha256 == b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28'

View file

@ -1,6 +1,6 @@
from twnet_parser.packer import *
def test_unpack_ints_and_strings():
def test_unpack_ints_and_strings() -> None:
u = Unpacker(b'\x01\x02\x03\x01foo\x00bar\x00')
assert u.get_int() == 1
assert u.get_int() == 2
@ -8,3 +8,60 @@ def test_unpack_ints_and_strings():
assert u.get_int() == 1
assert u.get_str() == 'foo'
assert u.get_str() == 'bar'
def test_simple_repack() -> None:
data: bytes = pack_str('hello world')
assert data == b'hello world\x00'
data += pack_int(22)
u = Unpacker(data)
assert u.get_str() == 'hello world'
assert u.get_int() == 22
def test_non_ascii_repack() -> None:
data: bytes = pack_str('💩')
assert data == '💩'.encode('utf-8') + b'\x00'
u = Unpacker(data)
assert u.get_str() == '💩'
def test_multi_repack() -> None:
strs: list[str] = [
'foo',
'bar',
'baz',
'',
'yeeeeeeeeeeeeeeeeeee' \
'eeeeeeeeeeeeeeeeeeee' \
'eeeeeeeeeeeeeeeeeeee' \
'eeeeeeeeeeeeeeeeeeee' \
'eeeeeeeeeeeeeeeeeeee' \
'eeeeeeeeeeeeeeeeeeee' \
'eeeppiiiiiiiiiiiiiii',
'a b c d e f',
'nameless tee',
'(1)nameless tee',
'[D](1)nameless t']
ints: list[int] = [
0,
111111111,
222222222,
649010,
-1,
-19882,
29299]
# pack
data: bytes = b''
for string in strs:
data += pack_str(string)
for num in ints:
data += pack_int(num)
# unpack
u = Unpacker(data)
for string in strs:
assert u.get_str() == string
for num in ints:
assert u.get_int() == num

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python
from twnet_parser.pretty_print import PrettyPrint
from twnet_parser.packer import Unpacker
from twnet_parser.packer import Unpacker, pack_str, pack_int
class MsgMapChange(PrettyPrint):
def __init__(
@ -30,10 +30,15 @@ class MsgMapChange(PrettyPrint):
self.crc = unpacker.get_int()
self.size = unpacker.get_int()
self.chunks_per_request = unpacker.get_int()
self.sha256 = unpacker.get_raw()
return True
def pack(self) -> bytes:
return b'todo'
return pack_str(self.name) + \
pack_int(self.crc) + \
pack_int(self.size) + \
pack_int(self.chunks_per_request) + \
self.sha256
# msg = MsgMapChange()
# msg.unpack(
@ -44,4 +49,5 @@ class MsgMapChange(PrettyPrint):
# b'\xa8\x15' \
# b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28')
#
# print(msg.pack())
# print(msg)

View file

@ -25,6 +25,14 @@ class Unpacker():
def data(self) -> bytes:
return self._data[self.idx:]
def get_raw(self, size: int = -1) -> bytes:
if size == -1:
return self.data()
end: int = self.idx + size
data: bytes = self._data[self.idx:end]
self.idx = end
return data
def get_int(self) -> int:
sign = (self.byte() >> 6) & 1
res = self.byte() & 0x3F
@ -81,6 +89,9 @@ def pack_int(num: int) -> bytes:
num >>= 7 # discard 7 bits
return bytes(res)
def pack_str(data: str) -> bytes:
return data.encode('utf-8') + b'\x00'
# TODO: optimize performance and benchmark in tests
def unpack_int(data: bytes) -> int:
sign = (data[0] >> 6) & 1