0x8d import struct struct.pack('>L', 154) struct.pack('>L', 141) struct.unpack('>2L', data[16:24]) struct.unpack('>16x2L6x', data) from construct import Struct, Magic, UBInt32, Const, String # adapted from code at https://github.com/construct fmt = Struct('png', Magic(b'\x89PNG\r\n\x1a\n'), UBInt32('length'), Const(String('type', 4), b'IHDR'), UBInt32('width'), UBInt32('height') ) data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR' + \ b'\x00\x00\x00\x9a\x00\x00\x00\x8d\x08\x02\x00\x00\x00\xc0' result = fmt.parse(data) print(result) print(result.width, result.height) import binascii valid_png_header = b'\x89PNG\r\n\x1a\n' print(binascii.hexlify(valid_png_header))
# -*- coding: utf-8 -*- """ constructBytes.py Created on Sat Apr 13 11:28:18 2019 @author: madhu """ from construct import Struct, Magic, UBInt32, Const, String # adapted from code at https://github.com/construct fmt = Struct('png', Magic(b'\x89PNG\r\n\x1a\n'), UBInt32('length'), Const(String('type', 4), b'IHDR'), UBInt32('width'), UBInt32('height')) data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR' + \ b'\x00\x00\x00\x9a\x00\x00\x00\x8d\x08\x02\x00\x00\x00\xc0' result = fmt.parse(data) print(result) print(result.width, result.height)
UBInt32("cumulative_drops"), EpochTimeStampAdapter(UBInt32("timestamp_seconds")), UBInt32("timestamp_microseconds"), HexDumpAdapter(Field("data", lambda ctx: ctx.included_length)), # 24 being the static length of the packet_record header Padding(lambda ctx: ctx.record_length - ctx.included_length - 24), ) datalink_type = Enum( UBInt32("datalink"), IEEE802dot3=0, IEEE802dot4=1, IEEE802dot5=2, IEEE802dot6=3, ETHERNET=4, HDLC=5, CHARSYNC=6, IBMCHANNEL=7, FDDI=8, OTHER=9, UNASSIGNED=10, ) snoop_file = Struct( "snoop", Magic("snoop\x00\x00\x00"), UBInt32("version"), # snoop v1 is deprecated datalink_type, OptionalGreedyRange(packet_record), )
Array( lambda ctx: num_attr(ctx), Struct("f_attr", perf_event_attr, perf_file_section("ids", id_array)))) perf_event_types = Struct("perf_file_attr", Anchor("here"), Padding(lambda ctx: ctx._.size)) perf_data = TunnelAdapter(Bytes("perf_data", lambda ctx: ctx.size), GreedyRange(perf_event())) #OnDemand(Bytes("perf_data", lambda ctx: ctx.size)) perf_file = Struct( "perf_file_header", # no support for version 1 Magic("PERFILE2"), UNInt64("size"), UNInt64("attr_size"), perf_file_section("attrs", perf_file_attr), perf_file_section("data", perf_data), perf_file_section("event_types", perf_event_types), # little endian Embedded( BitStruct(None, Flag("nrcpus"), Flag("arch"), Flag("version"), Flag("osrelease"), Flag("hostname"), Flag("build_id"), Flag("tracing_data"), Flag("reserved"), Flag("branch_stack"), Flag("numa_topology"), Flag("cpu_topology"), Flag("event_desc"), Flag("cmdline"), Flag("total_mem"), Flag("cpuid"), Flag("cpudesc"), Padding(6), Flag("group_desc"), Flag("pmu_mappings"), Padding(256 - 3 * 8))),
from construct import (Struct, Magic, UBInt8, UBInt16, UBInt32, Embed, Enum, Array, Field, BFloat32, Switch, If, PascalString, Debugger, Probe) packet_versions = ('DSI-Streamer-v.0.7.15',) _header = Struct('embedded', Magic('@ABCD'), # bytes 0-5 Enum(UBInt8('type'), # byte 5 NULL=0, EEG_DATA=1, EVENT=5 ), UBInt16('payload_length'), # bytes 6-7 UBInt32('number'), # bytes 8-11 ) _event = Struct('embedded', Enum(UBInt32('event_code'), # bytes 12-15 VERSION=1, DATA_START=2, DATA_STOP=3, SENSOR_MAP=9, DATA_RATE=10 ), UBInt32('sending_node'), # byes 16-19 If(lambda ctx: ctx.payload_length > 8, # Message data is optional # message_length: bytes 20-23, message: bytes 24+ PascalString('message', length_field=UBInt32('message_length'), encoding='ascii') )
BFloat64("z")) orientation = Struct("orientation", BFloat32("rotation"), BFloat32("pitch")) # TODO: this must be replaced with 'slot' (see below) # Notchian item packing (slot data) items = Struct( "items", SBInt16("primary"), If( lambda context: context["primary"] >= 0, Embed( Struct( "item_information", UBInt8("count"), UBInt16("secondary"), Magic("\xff\xff"), )), ), ) Speed = namedtuple('speed', 'x y z') class Slot(object): def __init__(self, item_id=-1, count=1, damage=0, nbt=None): self.item_id = item_id self.count = count self.damage = damage # TODO: Implement packing/unpacking of gzipped NBT data self.nbt = nbt
# encoding: utf-8 """Parser for SUNP BURN files, commonly distributed as SPHOSTBRN""" __author__ = "Martin Heistermann" __license__ = "GPL3" __email__ = "code at mheistermann.de" import construct as c from construct import Struct, Magic, ULInt32, Padding, Aligned from construct import Pointer, Array, MetaArray, String from construct import Field, StaticField, OnDemand, Bytes from construct import Adapter, Value burnhdr1 = Struct( "burnhdr1", Magic(b"SUNP BURN HDR 1"), Padding(1, strict=True), Array( 9, Bytes( "unk", 16 ) # mostly only first 4 bytes used, exception 12 bytes at [burnhdr+0x74], e.g. behind unk[6] )) def _xor(s, key=0x7A): return bytes((x ^ key) for x in s) class XorObfuscation(Adapter): def _encode(self, obj, ctx):
position = Struct("position", BFloat64("x"), BFloat64("y"), BFloat64("stance"), BFloat64("z") ) orientation = Struct("orientation", BFloat32("rotation"), BFloat32("pitch")) # Notchian item packing items = Struct("items", SBInt16("primary"), If(lambda context: context["primary"] >= 0, Embed(Struct("item_information", UBInt8("count"), UBInt16("secondary"), Magic("\xff\xff"), )), ), ) Metadata = namedtuple("Metadata", "type value") metadata_types = ["byte", "short", "int", "float", "string", "slot", "coords"] # Metadata adaptor. class MetadataAdapter(Adapter): def _decode(self, obj, context): d = {} for m in obj.data: d[m.id.second] = Metadata(metadata_types[m.id.first], m.value)