def perf_event_header(): return Embedded(Struct(None, Enum(UNInt32("type"), MMAP = 1, LOST = 2, COMM = 3, EXIT = 4, THROTTLE = 5, UNTHROTTLE = 6, FORK = 7, READ = 8, SAMPLE = 9, MMAP2 = 10, RECORD_AUX = 11, ITRACE_START = 12, LOST_SAMPLES = 13, SWITCH = 14, SWITCH_CPU_WIDE = 15, NAMESPACES = 16, KSYMBOL = 17, BPF_EVENT = 18, CGROUP = 19, HEADER_ATTR = 64, HEADER_EVENT_TYPE = 65, TRACING_DATA = 66, HEADER_BUILD_ID = 67, FINISHED_ROUND = 68, ID_INDEX = 69, AUXTRACE_INFO = 70, AUXTRACE = 71, AUXTRACE_ERROR = 72, THREAD_MAP = 73, CPU_MAP = 74, STAT_CONFIG = 75, STAT = 76, STAT_ROUND = 77, EVENT_UPDATE = 78, TIME_CONV = 79, HEADER_FEATURE = 80, COMPRESSED = 81), Embedded(BitStruct(None, Padding(1), Enum(BitField("cpumode", 7), UNKNOWN = 0, KERNEL = 1, USER = 2, HYPERVISOR = 3, GUEST_KERNEL = 4, GUEST_USER = 5), Flag("ext_reserved"), Flag("exact_ip"), Flag("mmap_data"), Padding(5))), UNInt16("size"), If(has_sample_id_all, Pointer(lambda ctx: ctx.start + ctx.size - 8, UNInt64("end_id"))), Value("attr", lookup_event_attr)))
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 get_mds_parser(self): self.timestamp = Struct("utc_timestamp", SBInt32("day"), UBInt32("sec"), UBInt32("msec")) self.waveform_data = Struct( "wfm", Array(128, Per2048(UBInt16("average_wfm_if_corr_ku"))), Padding(172)) self.mds_record = Struct("mds_record", self.timestamp, SBInt8("quality_indicator"), Padding(3), UBInt32("source_packet_counter"), Padding(8), Array(20, self.waveform_data)) self.mds = Array(self.n_records, self.mds_record) return self.mds
def _Status(self): return BitStruct( 'CameraRunning' / Flag, 'MainImuCalibRunning' / Flag, 'AuxiliaryImuCalibRunning' / Flag, 'AuxiliaryConnected' / Flag, 'GpsRunning' / Flag, 'RemoteGpsRunning' / Flag, 'CamNumber01Error' / Flag, 'CamNumber02Error' / Flag, 'CamNumber03Error' / Flag, 'CamNumber04Error' / Flag, 'CamNumber05Error' / Flag, 'CamNumber06Error' / Flag, 'CamNumber07Error' / Flag, 'CamNumber08Error' / Flag, 'CamNumber09Error' / Flag, 'CamNumber10Error' / Flag, 'CamNumber11Error' / Flag, 'CamNumber12Error' / Flag, 'CamNumber13Error' / Flag, 'CamNumber14Error' / Flag, 'CamNumber15Error' / Flag, 'CamNumber16Error' / Flag, # If new flags are defined, the padding should be adjusted. Padding(10), )
def symbolName(self, name): if name.startswith(b'\x00\x00\x00\x00'): vs = Struct(Padding(4), "value" / Int32ul).parse(name) return CString().parse( self.coff.stringTable[vs.value:]).decode("ascii") else: return String(8, padchar=b"\x00").parse(name).decode("ascii")
def _gen_fmh3_struct(int_type, pointer_type, codepoint_type, addr_mode='rel'): return Struct( "pointer_offset" / (Computed(0) if addr_mode == 'abs' else Tell), "signature" / Const(b'FMH3'), Padding(4), "fonts_count" / Padded(pointer_type.sizeof(), int_type), "fonts_pointers_offset" / pointer_type, "fonts" / Pointer( lambda this: this.fonts_pointers_offset + this.pointer_offset, RepeatUntil( lambda obj, lst, ctx: ctx._index >= ctx.fonts_count - 1, Struct( "pointer" / pointer_type, "data" / Pointer( lambda this: this.pointer + this._.pointer_offset, Struct( "id" / int_type, "advance_width" / Byte, "line_height" / Byte, "box_width" / Byte, "box_height" / Byte, "layout_param_1" / Byte, "layout_param_2_numerator" / Byte, "layout_param_2_denominator" / Byte, Padding(1), "other_params?" / int_type, "tex_size_chars" / int_type, "chars_count" / int_type, "chars_pointer" / pointer_type, "chars" / Pointer( lambda this: this.chars_pointer + this._._. pointer_offset, RepeatUntil( lambda obj, lst, ctx: ctx._index >= ctx. chars_count - 1, Struct( "codepoint" / codepoint_type, "halfwidth" / Flag, Padding(1), "tex_col" / Byte, "tex_row" / Byte, "glyph_x" / Byte, "glyph_width" / Byte, ))), )), ))), )
def _send_standby_request(self): fmt = Struct( 'length' / Const(b'\x08\x00\x00\x00'), 'type' / Const(b'\x1a\x00\x00\x00'), 'dummy' / Padding(8), ) msg = fmt.build({}) self._send_msg(msg, encrypted=True)
def _send_boot_request(self, title_id): fmt = Struct( 'length' / Const(b'\x18\x00\x00\x00'), 'type' / Const(b'\x0a\x00\x00\x00'), 'title_id' / Bytes(16), 'dummy' / Padding(8), ) msg = fmt.build({'title_id': title_id.encode().ljust(16, b'\x00')}) self._send_msg(msg, encrypted=True)
def _send_hello_request(self): fmt = Struct( 'length' / Const(b'\x1c\x00\x00\x00'), 'type' / Const(b'\x70\x63\x63\x6f'), 'version' / Const(b'\x00\x00\x02\x00'), 'dummy' / Padding(16), ) msg = fmt.build({}) self._send_msg(msg)
def test_parse_nested(self): struct = BitStruct("foo", BitField("a", 3), Flag("b"), Padding(3), Nibble("c"), Struct( "bar", Nibble("d"), Bit("e"), )) self.assertEqual( struct.parse(b"\xe1\x1f"), Container(a=7, b=False, bar=Container(d=15, e=1), c=8))
def test_parse(self): struct = BitStruct("foo", BitField("a", 3), Flag("b"), Padding(3), Nibble("c"), BitField("d", 5), ) self.assertEqual(struct.parse(six.b("\xe1\x1f")), Container(a=7, b=False, c=8, d=31))
def create_ending_struct(self): return FocusedSeq( "empty", "_before_start" / Tell, Padding( lambda this: 512 - (this._before_start & 0x1ff), self.block_filler, ), "empty" / Terminated, )
def __get_struct(is_wiiu: bool) -> Struct: if is_wiiu: content_hash = 'sha1' / Padded(0x20, ChecksumRaw(hashlib.sha1)) app_type = ['app_type' / Hex(Bytes(4)), '_unk2' / Bytes(0x3a)] else: content_hash = 'sha256' / ChecksumRaw(hashlib.sha256) app_type = ['_unk2' / Bytes(0x3e)] return InliningStruct( 'signature' / common.signature, '__raw_header_signed__' @ AttributeRawCopy( InlineStruct( 'issuer' / PaddedString(0x40, 'ascii'), 'format_version' / Byte, 'ca_crl_version' / Byte, 'signer_crl_version' / Byte, '_unk1' / Bytes(1), 'system_version' / Hex(Bytes(8)), 'title_id' / common.TitleID, 'title_type' / Hex(Bytes(4)), 'group_id' / Hex(Bytes(2)), * app_type, 'access_rights' / Hex(Bytes(4)), 'title_version' / Int16ub, 'content_count' / Int16ub, 'boot_index' / Int16ub, Padding(2), 'content_info_sha256' / ChecksumValue(hashlib.sha256, this._.content_info))), 'content_info' / ChecksumSourceData( Array( 64, Struct( 'content_index' / Int16ub, 'content_count' / Int16ub, 'contents_sha256' / IfThenElse( this.content_count > 0, ChecksumValue( hashlib.sha256, lambda this: this._. contents[this.content_index:this.content_index + this.content_count], True), ChecksumRaw(hashlib.sha256))))), 'contents' / Array( this.content_count, ChecksumSourceData( Struct( 'id' / Hex(Int32ub), 'index' / Int16ub, # 0x2000: always set (?) # 0x4000: appears in DLC TMDs ("optional"?) # 0x8000: "shared"? 'type' / FlagsEnum(Int16ub, encrypted=0x0001, hashed=0x0002, cfm=0x0004, unk1=0x2000, unk2=0x4000, unk3=0x8000), 'size' / Int64ub, content_hash))), 'certificates' / common.certificates, VerifyOrWriteChecksums, Terminated)
def get_mds_parser(self): self.timestamp = Struct("utc_timestamp", SBInt32("day"), UBInt32("sec"), UBInt32("msec")) self.waveform_data = Struct( "wfm", Array(128, Per2048(UBInt16("average_wfm_if_corr_ku"))), Array(2, Per2048(UBInt16("central_filters_if_corr_ku"))), Array(64, Per8096(UBInt16("average_wfm_if_corr_s"))), Array(2, SBInt16("indexes_of_2_dft_samples")), Per256(SBInt16("delta_offset_fft_filter_units")), Padding(18), Per2048(SBInt16("noise_power_measurement")), OneHundredth(SBInt16("agc_of_noise_power_measurement")), OneHundredthDecibel(UBInt16("reference_power_value")), Padding(10)) self.mds_record = Struct("mds_record", self.timestamp, SBInt8("quality_indicator"), Padding(3), UBInt32("source_packet_counter"), Padding(8), Array(20, self.waveform_data)) self.mds = Array(self.n_records, self.mds_record) return self.mds
def test(self): struct = BitStruct( "bitstruct", BitField("a", 3), Flag("b"), Padding(3), Nibble("c"), BitField("d", 5), ) self.assertEqual(struct.parse(b"\xe1\x1f"), Container(a=7)(b=False)(c=8)(d=31)) self.assertEqual(struct.build(Container(a=7)(b=False)(c=8)(d=31)), b"\xe1\x1f")
def DirEntry(name): return Struct( name, Bytes("name", 8), Bytes("extension", 3), BitStruct("attributes", Flag("unused"), Flag("device"), Flag("archive"), Flag("subDirectory"), Flag("volumeLabel"), Flag("system"), Flag("hidden"), Flag("readonly")), # reserved Padding(10), ULInt16("timeRecorded"), ULInt16("dateRecorded"), ULInt16("firstCluster"), ULInt32("fileSize"))
def PreDataRegion(name): rde = DirEntry("rootdirs") fe = FatEntry("fats") return Struct(name, Embed(BootSector("bootSector")), # the remaining reserved sectors Padding(lambda ctx: (ctx.reservedSectorCount - 1) * ctx.sectorSize), # file allocation tables Array(lambda ctx: (ctx.fatCount), Array(lambda ctx: ctx.sectorsPerFat * ctx.sectorSize / fe.sizeof(), fe)), # root directories Array(lambda ctx: (ctx.rootdirEntryCount*rde.sizeof()) / ctx.sectorSize, rde))
def test_nested(self): struct = BitStruct("bitstruct", BitField("a", 3), Flag("b"), Padding(3), Nibble("c"), Struct( "sub", Nibble("d"), Bit("e"), )) self.assertEqual( struct.parse(b"\xe1\x1f"), Container(a=7)(b=False)(c=8)(sub=Container(d=15)(e=1))) self.assertEqual( struct.build( Container(a=7)(b=False)(c=8)(sub=Container(d=15)(e=1))), b"\xe1\x1f")
def perf_event_header(): return Embedded( Struct( None, Enum(UNInt32("type"), MMAP=1, LOST=2, COMM=3, EXIT=4, THROTTLE=5, UNTHROTTLE=6, FORK=7, READ=8, SAMPLE=9, MMAP2=10, TRACING_DATA=66, FINISHED_ROUND=68, ID_INDEX=69, AUXTRACE_INFO=70, AUXTRACE=71, AUXTRACE_ERROR=72), Embedded( BitStruct( None, Padding(1), Enum(BitField("cpumode", 7), UNKNOWN=0, KERNEL=1, USER=2, HYPERVISOR=3, GUEST_KERNEL=4, GUEST_USER=5), Flag("ext_reserved"), Flag("exact_ip"), Flag("mmap_data"), Padding(5))), UNInt16("size"), If( has_sample_id_all, Pointer(lambda ctx: ctx.start + ctx.size - 8, UNInt64("end_id"))), Value("attr", lookup_event_attr)))
def perf_event(): return Struct( "perf_event", Anchor("start"), perf_event_header(), Anchor("header_end"), Switch( "data", lambda ctx: ctx.type, { "MMAP": mmap(), # noqa E121 "MMAP2": mmap2(), "LOST": Struct("lost", UNInt64("id"), UNInt64("lost"), sample_id()), "COMM": Struct("comm", SNInt32("pid"), SNInt32("tid"), CString("comm"), sample_id()), "EXIT": fork_exit("exit"), "THROTTLE": throttle("throttle"), "UNTHROTTLE": throttle("unthrottle"), "FINISHED_ROUND": Pass, "FORK": fork_exit("fork"), "READ": Embedded( Struct("read_event", SNInt32("pid"), SNInt32("tid"), read_format(), sample_id())), "SAMPLE": event() }), Anchor("end"), Padding(lambda ctx: ctx.size - (ctx.end - ctx.start)))
INDEX_FIELD_ORDER_TYPE_STRUCT = Enum(ULInt16('field_order_type'), ASCENDING=0, DESCENDING=1, _default_='DESCENDING') TABLE_DEFINITION_INDEX_STRUCT = Struct( 'record_table_definition_index', # May be external_filename # if external_filename == 0, no external file index CString('external_filename'), If(lambda x: len(x['external_filename']) == 0, Const(Byte('index_mark'), 1)), CString('name'), Embed( BitStruct('flags', Padding(1), INDEX_TYPE_STRUCT, Padding(2), Flag('NOCASE'), Flag('OPT'), Flag('DUP'))), ULInt16('field_count'), Array( lambda x: x['field_count'], Struct('index_field_propertly', ULInt16('field_number'), INDEX_FIELD_ORDER_TYPE_STRUCT)), ) MEMO_TYPE_STRUCT = Enum(Flag('memo_type'), MEMO=0, BLOB=1) TABLE_DEFINITION_MEMO_STRUCT = Struct( 'record_table_definition_memo', # May be external_filename # if external_filename == 0, no external file index CString('external_filename'),
"unit_ids"/If(lambda ctx: ctx.selected < 0xff, Array( lambda ctx: ctx.selected, "unit_ids"/Int32ul )) ) give_attribute = "give_attribute"/Struct( "player_id"/Byte, "target_id"/Byte, "attribute"/Byte, "amount"/Float32l ) add_attribute = "add_attribute"/Struct( "player_id"/Byte, "attribute"/Byte, Padding(1), "amount"/Float32l ) create = "create"/Struct( Padding(1), "unit_type"/Int16ul, "player_id"/Byte, Padding(1), "x"/Float32l, "y"/Float32l, "z"/Float32l ) ai_interact = "ai_interact"/Struct( Padding(3),
UptimeStatus = Struct( "uptime" / Int32ul ) LastFault = Struct( "time" / Int32ul, "fault" / GreedyString("utf8") ) SystemStats = Struct( "stats" / DictAdapter( GreedyRange( Struct( "name" / PaddedString(8, "utf8"), "high_water_mark" / Int16ul, "rfu" / Padding(4), ) ), key=this.name, value=this.high_water_mark ) ) GarbageCollector = Struct( "bytes_left" / Int16ul ) AppVersion = Struct( "version" / Int16ul )
class Color(FixedObject): """A 32-bit RGB color value. Each component r, g, b has a value between 0 and 1023. However, Colors are considered equal if they have the same 8-bit value. """ classID = 30 _construct = BitStruct( "value", Padding(2), Bits("r", 10), Bits("g", 10), Bits("b", 10), ) _construct_32_rgba = Struct( "", UBInt8("r"), UBInt8("g"), UBInt8("b"), UBInt8("alpha"), ) def __init__(self, value): self.value = value def __eq__(self, other): return (isinstance(other, Color) and self.to_8bit() == other.to_8bit()) def __ne__(self, other): return not self == other @classmethod def from_value(cls, value): return cls((value.r, value.g, value.b)) def to_value(self): (r, g, b) = self.value return Container(r=r, g=g, b=b) @classmethod def from_8bit(self, r, g=None, b=None): if g is None and b is None: rgb = r else: rgb = (r, g, b) return Color((x << 2 for x in rgb)) def to_8bit(self): """Returns value with components between 0-255.""" return tuple(x >> 2 for x in self.value) def __repr__(self): return "%s(%s)" % ( self.__class__.__name__, repr(self.value).strip("()"), ) def to_rgba_array(self): (r, g, b) = self.to_8bit() return array('B', (r, g, b, 255)) def to_argb_array(self): (r, g, b) = self.to_8bit() return bytearray((255, r, g, b))
""" FAT32 Filesystem information sector. This structure is part of the reserved sectors area. """ from construct import Struct, Padding, Bytes, Int32ul # Filesystem information sector for FAT32 FS_INFORMATION_SECTOR = Struct( "fsinfo_sector_signature" / Bytes(4), Padding(480), # reserved "fsinfo_sector_signature" / Bytes(4), "free_data_cluster_count" / Int32ul, "last_allocated_data_cluster" / Int32ul, Padding(12), # reserved "fsinfo_sector_signature" / Bytes(4), )
#!/usr/bin/env python from construct import BitStruct, BitsInteger, Padding, Enum, Flag SCB_ICSR = 0xe000ed04 scb_icsr = BitStruct( "NMIPENDSET" / Flag, Padding(2), "PENDSVSET" / Flag, "PENDSVCLR" / Flag, "PENDSTSET" / Flag, "PENDSTCLR" / Flag, Padding(1), "DEBUG" / Flag, "ISRPENDING" / Flag, "VECTPENDING" / BitsInteger(10), "RETOBASE" / Flag, Padding(2), "VECTACTIVE" / BitsInteger(9) ) SCB_SHCSR = 0xe000ed24 scb_shcsr = BitStruct( Padding(13), "USGFAULTENA" / Flag, "BUSFAULTENA" / Flag, "MEMFAULTENA" / Flag, "SVCALLPENDED"/ Flag, "BUSFAULTPENDED" / Flag, "MEMFAULTPENDED" / Flag, "USGFAULTPENDED" / Flag,
UBInt8("dir"), UBInt8("op")) HELLO = Struct("hello", UBInt32("padding")) CAPS_C = Struct("cells", UBInt16("pci"), UBInt32("cap"), UBInt16("DL_earfcn"), UBInt8("DL_prbs"), UBInt16("UL_earfcn"), UBInt8("UL_prbs")) CAPS_RESPONSE = Struct("caps_response", BitStruct("flags", Padding(30), Bit("ue_measure"), Bit("ue_report")), UBInt32("nof_cells"), Array(lambda ctx: ctx.nof_cells, CAPS_C)) UE_R = Struct("ues", UBInt16("pci"), Bytes("plmn_id", 4), UBInt16("rnti"), UBInt64("imsi")) UE_REPORT_RESPONSE = Struct("ue_report_response", UBInt32("nof_ues"), Array(lambda ctx: ctx.nof_ues, UE_R))
# pylint: disable=invalid-name # Not all actions are defined, not all actions are complete. interact = "interact" / Struct( "player_id" / Byte, Const(b"\x00\x00"), "target_id" / Int32ul, "selected" / Int32ul, "x" / Float32l, "y" / Float32l, If(lambda ctx: ctx.selected < 0xff, Array(lambda ctx: ctx.selected, "unit_ids" / Int32ul))) give_attribute = "give_attribute" / Struct( "player_id" / Byte, "target_id" / Byte, "attribute" / Byte, "amount" / Float32l) add_attribute = "add_attribute" / Struct( "player_id" / Byte, "attribute" / Byte, Padding(1), "amount" / Float32l) ai_interact = "ai_interact" / Struct( Padding(3), "target_id" / Int32ul, "selected" / Byte, Padding(3), "x" / Float32l, "y" / Float32l, If(lambda ctx: ctx.selected < 0xff, Array(lambda ctx: ctx.selected, "unit_ids" / Int32ul))) move = "move" / Struct( "player_id" / Byte, Const(b"\x00\x00"), Padding(4), "selected" / Int32ul, "x" / Float32l, "y" / Float32l, If(lambda ctx: ctx.selected < 0xff, Array(lambda ctx: ctx.selected, "unit_ids" / Int32ul))) ai_move = "ai_move" / Struct( "selected" / Byte, "player_id" / Byte, "player_num" / Byte, Padding(4),
Response = Struct("type" / Enum( Int8ul, OK=0xff, ERR=0xfe, EV=0xfd, SERV=0xfc, )) DAQ = Struct( "odt" / Byte, "daq" / Byte, "data" / GreedyBytes, ) ResourceType = BitStruct( Padding(2), "dbg" / Flag, "pgm" / Flag, "stim" / Flag, "daq" / Flag, Padding(1), "calpag" / Flag, ) AddressGranularity = Enum(BitsInteger(2), BYTE=0b00, WORD=0b01, DWORD=0b10, RESERVED=0b11) ByteOrder = Enum(BitsInteger(1), INTEL=0, MOTOROLA=1)
# Scenario header. scenario_header = "scenario_header" / Struct( "next_uid" / Int32ul, "constant" / Bytes(4), Array(16, "names" / String(256)), Array(16, "player_ids" / Int32ul), Array( 16, "player_data" / Struct( "active" / Int32ul, "human" / Int32ul, "civilization" / Int32ul, "constant" / Int32ul, # 0x04 0x00 0x00 0x00 )), Padding(5), "elapsed_time" / Float32l, "scenario_filename" / PascalString(lengthfield="scenario_filename_length" / Int16ul), If(lambda ctx: ctx._._.version == Version.DE, Padding(64))) # Scenarios have intro text, a bitmap, and cinematics. messages = "messages" / Struct( "instruction_id" / Int32ul, "hints_id" / Int32ul, "victory_id" / Int32ul, "defeat_id" / Int32ul, "history_id" / Int32ul, "scouts_id" / If(lambda ctx: ctx._._.version != Version.AOK, Int32ul), "instructions_length" / Int16ul, "instructions" / Bytes(lambda ctx: ctx.instructions_length),