Skip to content

Instantly share code, notes, and snippets.

@miou-gh
Last active April 22, 2017 00:58
Show Gist options
  • Save miou-gh/ed2892a5c40695f7d36bbb6c998d5f60 to your computer and use it in GitHub Desktop.
Save miou-gh/ed2892a5c40695f7d36bbb6c998d5f60 to your computer and use it in GitHub Desktop.
read/write WPE spt files
import bson
import struct
from enum import Enum
class SPT:
def __init__(self):
pass
class Packet:
def __init__(self, name, data):
self.name = name
self.data = data
class State(Enum):
HEAD = 1
NAME = 2
DATA = 3
def read_int(self, bytes, index):
return index + 4, struct.unpack('i', bytes[index:index+4])[0]
def read_str(self, bytes, index, count):
return index + count, struct.unpack(str(count) + 's', bytes[index:index+count])[0]
def read_bytes(self, bytes, index, count):
return index + count, bytearray(struct.unpack(str(count) + 's', bytes[index:index+count])[0])
def deserialize(self, input_bytes):
input_bytes = bytearray(input_bytes)
packets = []
index, capacity, = self.read_int(input_bytes, 0)
for i in range(0, capacity):
index, name_length, = self.read_int(input_bytes, index)
index, name, = self.read_str(input_bytes, index, name_length)
index, data_length, = self.read_int(input_bytes, index)
index, data, = self.read_bytes(input_bytes, index, data_length);
packets.append(Packet(name, data))
pass
return packets
def serialize(self, packet):
bytes = bytearray()
bytes += struct.pack('i', len(packets))
bytes += struct.pack('i', len(packet.name))
bytes += struct.pack(str(len(packet.name)) + 's', packet.name)
bytes += struct.pack('i', len(packet.data))
bytes += struct.pack(str(len(packet.data)) + 's', packet.data)
return bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment