def nfc_cmd(cmd): return Struct( 'nfc_msg', OneOf(Byte('start'), (NFC_STX, )), OneOf(Byte('device'), NFC_RESPONSE_SET), OneOf(Byte('command'), NFC_COMMAND_SET), UBInt16('len'), String('data', lambda ctx: ctx['len']) if cmd is None else cmd, OneOf(Byte('stop'), (NFC_ETX, )), Embed(crc))
def setUp(self): self.cons = Struct( 'foo', Enum(Byte("a_enum"), ALFA=1, BETA=2), Switch( 'switch', lambda ctx: ctx.a_enum, { 'ALFA': Embed(Struct('struct_alfa', Byte('byte_alfa'))), 'BETA': Embed(Struct('struct_beta', Byte('byte_beta'))), }))
def _parse(self, stream, context): l = VLQ('').parse_stream(stream) c = {} for x in range(l): junk1 = Byte('').parse_stream(stream) junk2 = Byte('').parse_stream(stream) junk3 = BFloat32('').parse_stream(stream) junk4 = Byte('').parse_stream(stream) junk5 = StarByteArray('').parse_stream(stream) return c
def client_context_update(name='client_context'): return Struct( name, VLQ('length'), Peek(Byte('a')), If( lambda ctx: ctx['a'] == 0, Struct('junk', Padding(1), Peek(Byte('b')), If(lambda ctx: ctx['b'] == 0, Padding(1)), VLQ('extra_length'))), If(lambda ctx: ctx['a'] > 8, Struct('junk2', VLQ('extra_length'))), VLQ('subpackets'), Array(lambda ctx: ctx.subpackets, Variant('subpacket')))
def entity_create(name='entity_create'): return Struct( name, GreedyRange( Struct('entity', Byte('entity_type'), VLQ('payload_size'), String('payload', lambda ctx: ctx.payload_size), VLQ('entity_id'))))
def swap_in_container(name='swap_in_container'): return Struct( name, VLQ('entity_id'), # Where are we putting stuff star_string('item_name'), VLQ('count'), Byte('variant_type'), StarByteArray('item_description'), VLQ('slot'))
def warp_action(name='warp_action'): return Struct( name, Byte('warp_type'), Switch('warp_action_type', lambda ctx: ctx['warp_type'], { 1: LazyBound('next', lambda: WARP_WORLD), 2: HexAdapter(Field('uuid', 16)), 3: SBInt32('alias') }, default=Pass))
def test_overwrite(self): s = Struct("s", Byte("a"), Byte("a"), allow_overwrite = True ) self.assertEqual(s.parse(b"\x01\x02").a, 2) s = Struct("s", Byte("a"), Embedded(Struct("b", Byte("a"), allow_overwrite = True )), ) self.assertEqual(s.parse(b"\x01\x02").a, 2) s = Struct("s", Embedded(Struct("b", Byte("a"), )), Byte("a"), allow_overwrite = True ) self.assertEqual(s.parse(b"\x01\x02").a, 2)
def _parse(self, stream, context): x = Byte("").parse_stream(stream) if x == 0: return None elif x == 1: return star_string().parse_stream(stream) elif x == 2: return None elif x == 3: flag = Flag("").parse_stream(stream) return Field("", 16).parse_stream(stream).encode("hex") elif x == 4: return star_string().parse_stream(stream)
def _parse(self, stream, context): x = Byte("").parse_stream(stream) if x == 1: return None elif x == 2: return BFloat64("").parse_stream(stream) elif x == 3: return Flag("").parse_stream(stream) elif x == 4: return SignedVLQ("").parse_stream(stream) elif x == 5: return star_string().parse_stream(stream) elif x == 6: return VariantVariant("").parse_stream(stream) elif x == 7: return DictVariant("").parse_stream(stream)
def parse_header(data): """ split up header information (using construct) """ mavlink_header = Struct('header', Const(Byte('magic'), MAVLINK_MAGIC), Byte('plength'), Byte('sequence'), Byte('sysid'), Byte('compid'), Byte('msgid'), ) return mavlink_header.parse(data[0:6])
def Fat16Header(name): return Struct(name, Bytes("jumpInstruction", 3), Bytes("creatingSystemId", 8), ULInt16("sectorSize"), Byte("sectorsPerCluster"), ULInt16("reservedSectorCount"), Byte("fatCount"), ULInt16("rootdirEntryCount"), ULInt16("sectorCount_small"), Byte("mediaId"), ULInt16("sectorsPerFat"), ULInt16("sectorsPerTrack"), ULInt16("sideCount"), ULInt32("hiddenSectorCount"), ULInt32("sectorCount_large"), Byte("physicalDriveNumber"), Byte("currentHead"), Byte("extendedBootSignature"), Bytes("volumeId", 4), Bytes("volumeLabel", 11), Const(Bytes("fsType", 8), "FAT16 "), Bytes("bootCode", 448), Const(Bytes("bootSectorSignature", 2), "\x55\xaa"))
def test_no_overwrite(self): s = Struct( "s", Byte("a"), Byte("a"), ) self.assertRaises(OverwriteError, s.parse, six.b("\x01\x02")) s = Struct("s", Byte("a"), Embedded(Struct( "b", Byte("a"), )), allow_overwrite=True) self.assertRaises(OverwriteError, s.parse, six.b("\x01\x02")) s = Struct( "s", Embedded(Struct("b", Byte("a"), allow_overwrite=True)), Byte("a"), ) self.assertRaises(OverwriteError, s.parse, six.b("\x01\x02"))
def start_packet(name='interim_packet'): return Struct(name, Byte('id'), SignedVLQ('payload_size'))
def warp_toalias_write(name='warp_toalias_write'): return Struct(name, Byte('warp_type'), SBInt32('alias'))
def update_tile_protection_writer(name='update_tile_protection_writer'): return Struct(name, UBInt16('dungeon_id'), Byte('is_protected'))
def spawn_entity(name='spawn_entity'): return Struct( name, GreedyRange( Struct('entity', Byte('entity_type'), VLQ('payload_size'), String('payload', lambda ctx: ctx.payload_size))))
def connection(name='connection'): return Struct(name, GreedyRange(Byte('compressed_data')))
def swap_in_container_result(name='swap_in_container_result'): return Struct(name, star_string('item_name'), VLQ('count'), Byte('variant_type'), GreedyRange(StarByteArray('item_description')))
def give_item(name='give_item'): return Struct(name, star_string('name'), VLQ('count'), Byte('variant_type'), star_string('description'))
def warp_touniqueworld_write(name='warp_touniqueworld_write'): return Struct(name, Byte('warp_type'), Byte('world_type'), star_string('unique_world_name'), Byte('has_position'))
def warp_toplayerworld_write(name='warp_toplayerworld_write'): return Struct(name, Byte('warp_type'), Byte('world_type'), HexAdapter(Field('uuid', 16)), Byte('has_position'))
def warp_toplayer_write(name='warp_toplayer_write'): return Struct(name, Byte('warp_type'), HexAdapter(Field('uuid', 16)))
from construct import Byte, Bytes, Embed, Enum, IfThenElse, Peek, String, Struct, Switch, UBInt32, ULInt16, ULInt32 from .tpspage import PAGE_HEADER_STRUCT from .utils import check_value record_encoding = None RECORD_TYPE = Enum( Byte('type'), NULL=None, DATA=0xF3, METADATA=0xF6, TABLE_DEFINITION=0xFA, TABLE_NAME=0xFE, _default_='INDEX', ) DATA_RECORD_DATA = Struct('field_data', UBInt32('record_number'), Bytes('data', lambda ctx: ctx['data_size'] - 9)) METADATA_RECORD_DATA = Struct('field_metadata', Byte('metadata_type'), ULInt32('metadata_record_count'), ULInt32('metadata_record_last_access')) TABLE_DEFINITION_RECORD_DATA = Struct( 'table_definition', Bytes('table_definition_bytes', lambda ctx: ctx['data_size'] - 5)) INDEX_RECORD_DATA = Struct('field_index', Bytes('data', lambda ctx: ctx['data_size'] - 10), ULInt32('record_number'))
def byte(self): return Byte.parse(self.read(1))
def client_disconnect_request(name='client_disconnect_request'): return Struct(name, Byte('data'))
""" What : Internet Group Management Protocol, Version 2 How : http://www.ietf.org/rfc/rfc2236.txt Who : jesse @ housejunkie . ca """ from binascii import unhexlify from construct import Byte, Enum, Struct, UBInt16 from construct.protocols.layer3.ipv4 import IpAddress igmp_type = Enum( Byte("igmp_type"), MEMBERSHIP_QUERY=0x11, MEMBERSHIP_REPORT_V1=0x12, MEMBERSHIP_REPORT_V2=0x16, LEAVE_GROUP=0x17, ) igmpv2_header = Struct( "igmpv2_header", igmp_type, Byte("max_resp_time"), UBInt16("checksum"), IpAddress("group_address"), ) if __name__ == '__main__': capture = unhexlify(b"1600FA01EFFFFFFD") print(igmpv2_header.parse(capture))
def chat_received(name='chat_received'): return Struct( name, Enum(Byte('mode'), CHANNEL=0, BROADCAST=1, WHISPER=2, COMMAND_RESULT=3), star_string('channel'), UBInt32('client_id'), star_string('name'), star_string('message'))
""" TPS File Table """ from construct import Array, BitField, BitStruct, Byte, Const, CString, Embed, Enum, Flag, If, Padding, Struct, ULInt16 from .tpsrecord import TpsRecordsList FIELD_TYPE_STRUCT = Enum( Byte('type'), BYTE=0x1, SHORT=0x2, USHORT=0x3, # date format 0xYYYYMMDD DATE=0x4, # time format 0xHHMMSSHS TIME=0x5, LONG=0x6, ULONG=0x7, FLOAT=0x8, DOUBLE=0x9, DECIMAL=0x0A, STRING=0x12, CSTRING=0x13, PSTRING=0x14, # compound data structure GROUP=0x16, # LIKE (inherited data type) ) TABLE_DEFINITION_FIELD_STRUCT = Struct(
def chat_sent(name='chat_sent'): return Struct(name, star_string('message'), Enum(Byte('send_mode'), BROADCAST=0, LOCAL=1, PARTY=2))