def ReadVtableFixups(ClrHeader): VTableFixup = construct.Struct('VTableFixup', MakeRva('RVA'), construct.ULInt16('Count'), construct.FlagsEnum(construct.ULInt16('Type'), COR_VTABLE_32BIT = 0x01, # V-table slots are 32-bits in size. COR_VTABLE_64BIT = 0x02, # V-table slots are 64-bits in size. COR_VTABLE_FROM_UNMANAGED = 0x04, # If set, transition from unmanaged. COR_VTABLE_FROM_UNMANAGED_RETAIN_APPDOMAIN = 0x08, # If set, transition from unmanaged with keeping the current appdomain. COR_VTABLE_CALL_MOST_DERIVED = 0x10, # Call most derived method described by ) ) numFixups = ClrHeader.VTableFixups.Size / VTableFixup.sizeof() VTableFixups = construct.Array(numFixups, VTableFixup) if numFixups == 0: return [] return VTableFixups.parse(idc.GetManyBytes(clrHeader.VTableFixups.VA, VTableFixups.sizeof()))
def convert_fields_to_struct_fields(fields, verbose=False): total_bits = 0 struct_fields = [] for identifier, op, options in fields: s_field = None if op == "&": bits = len(options) description = f"{bits}-bit bitfield" f_enum = enum.IntFlag( identifier, [(op, 1 << i) for (i, op) in enumerate(options)] ) s_field = identifier / construct.FlagsEnum( construct.BitsInteger(bits), f_enum ) elif op == "|": bits = math.ceil(math.log2(len(options))) description = f"{bits}-bit enum" if set(options) == {True, False}: s_field = identifier / construct.Flag else: f_enum = enum.IntEnum( identifier, [(op, 1 << i) for (i, op) in enumerate(options)] ) s_field = identifier / construct.Enum( construct.BitsInteger(bits), f_enum ) struct_fields.append(s_field) if verbose: print(f"{identifier:15}: {description} ({len(options)} options)") assert bits == s_field.sizeof() total_bits += bits if verbose: print(f"Total bits: {total_bits}") return (struct_fields, total_bits)
StatusFlagsFormat = con.FlagsEnum( U64, # Alert flags are allocated at the bottom (from bit 0 upwards) dc_undervoltage=1 << 0, dc_overvoltage=1 << 1, dc_undercurrent=1 << 2, dc_overcurrent=1 << 3, cpu_cold=1 << 4, cpu_overheating=1 << 5, vsi_cold=1 << 6, vsi_overheating=1 << 7, motor_cold=1 << 8, motor_overheating=1 << 9, hardware_lvps_malfunction=1 << 10, hardware_fault=1 << 11, hardware_overload=1 << 12, phase_current_measurement_malfunction=1 << 13, # Non-error flags are allocated at the top (from bit 63 downwards) uavcan_node_up=1 << 56, can_data_link_up=1 << 57, usb_connected=1 << 58, usb_power_supplied=1 << 59, rcpwm_signal_detected=1 << 60, phase_current_agc_high_gain_selected=1 << 61, vsi_modulating=1 << 62, vsi_enabled=1 << 63, )
PKT_GAIN=0x4B) ControlPacketField = c.Enum(c.Byte, **ControlPacketFields) ############################################################################### # Generic Constructs GeneralPacket = c.Struct("START_BYTE" / c.Const(b'\x61'), "LENGTH" / c.Int16ub, "TYPE" / PacketType, "FIELDS" / c.Byte[c.this.LENGTH]) # Table 12 AMBE-3000R Version 2.2 ECMODE_IN = c.FlagsEnum( c.Int16ub, NS_ENABLE=(0x1 << 6), CP_SELECT=(0x1 << 7), CP_ENABLE=(0x1 << 8), ES_ENABLE=(0x1 << 9), DTX_ENABLE=(0x1 << 11), TD_ENABLE=(0x1 << 12), EC_ENABLE=(0x1 << 13), TS_ENABLE=(0x1 << 14), ) # Table 13 AMBE-3000R Version 2.2 ECMODE_OUT = c.FlagsEnum(c.Int16ub, VOICE_ACTIVE=(0x1 << 1), TONE_FRAME=(0x1 << 15)) # Table 14 AMBE-3000R Version 2.2 DCMODE_IN = c.FlagsEnum( c.Int16ub, LOST_FRAME=(0x1 << 2),
import construct as cs import construct_typed as cst import dataclasses import typing as t from . import GalleryItem constr = cs.Struct( "permissions" / cs.FlagsEnum(cs.Int8ul, R=4, W=2, X=1), ) gallery_item = GalleryItem( construct=constr, example_binarys={ "read": bytes([4]), "read_write": bytes([6]), }, )
hardware_version=1, block_size=3200, # sometimes 1970 sometimes 3200 ) SL3_HEADER = dict( version=3, hardware_version=1, block_size=3200, ) ValidFlags = c.FlagsEnum( c.Int16ul, GPSSpeedValid=1 << 1, WaterTemperatureValid=1 << 2, PositionValid=1 << 4, WaterSpeedValid=1 << 6, TrackValid=1 << 7, HeadingValid=1 << 8, AltitudeValid=1 << 9, **{f'Flag{i}': 1 << i for i in (0, 3, 5, 10, 11, 12, 13, 14, 15)}) ChannelType = c.Enum(c.Int16ul, Primary=0, Secondary=1, DownScan=2, Unknown1=3, Unknown2=4, SidescanComposite=5, SidescanStarboard=13, SidescanPort=14)
delta_table.append((-delta) & 0xFFFFFFFF) delta_table.append((delta + (code >> 5))) return delta_table ########################## # # Construct declaration # ########################## # # Sound Flags # gdv_soundflags = construct.BitStruct( construct.Padding(4), construct.FlagsEnum("audio_coding" / construct.BitsInteger(1), PCM = 0x00, DPCM = 0x01), construct.FlagsEnum("sample_width" / construct.BitsInteger(1), BIT_8 = 0x00, BIT_16 = 0x01), construct.FlagsEnum("audio_channels" / construct.BitsInteger(1), MONO = 0x00, STEREO = 0x01), construct.FlagsEnum("audio_present" / construct.BitsInteger(1), AUDIO_NOT_PRESENT = 0x00, AUDIO_PRESENT = 0x01), construct.Padding(8), ) # # Image type # gdv_imagetype = construct.BitStruct( construct.Padding(5), construct.Enum("video_depth" / construct.BitsInteger(3), PIXEL_8_BITS = 0x01, PIXEL_15_BITS = 0x02, PIXEL_16_BITS = 0x03,
ImageNtHeaders = construct.Struct('ImageNtHeaders', construct.Magic(b'PE\0\0'), # Signature ImageFileHeader, ImageOptionalHeader ) ImageCor20Header = construct.Struct('ImageCor20Header', construct.Magic(struct.pack('<L', 0x48)), # Cb construct.ULInt16('MajorRuntimeVersion'), construct.ULInt16('MinorRuntimeVersion'), MakeImageDataDirectory('MetaData'), construct.FlagsEnum(construct.ULInt32('Flags'), COMIMAGE_FLAGS_ILONLY = 0x00000001, COMIMAGE_FLAGS_32BITREQUIRED = 0x00000002, COMIMAGE_FLAGS_IL_LIBRARY = 0x00000004, COMIMAGE_FLAGS_STRONGNAMESIGNED = 0x00000008, COMIMAGE_FLAGS_NATIVE_ENTRYPOINT = 0x00000010, COMIMAGE_FLAGS_TRACKDEBUGDATA = 0x00010000, COMIMAGE_FLAGS_32BITPREFERRED = 0x00020000 ), construct.Union('EntryPoint', construct.ULInt32('EntryPointToken'), MakeRva('EntryPointRVA') ), MakeImageDataDirectory('Resources'), MakeImageDataDirectory('StrongNameSignature'), MakeImageDataDirectory('CodeManagerTable'), MakeImageDataDirectory('VTableFixups'), MakeImageDataDirectory('ExportAddressTableJumps'), MakeImageDataDirectory('ManagedNativeHeader') )