def PrefixedOffset(sizetype, type, offs=0): return C.FocusedSeq( "content", "_data" / C.Rebuild( C.Struct("size" / C.Rebuild(sizetype, C.len_(this.data) - offs), "data" / C.Bytes(this.size + offs)), lambda obj: {"data": type.build(obj.content, **{ **obj._params, **obj })}), "content" / C.RestreamData(this._data.data, type))
c.Terminated, ) FirmwareOne = c.Struct( "magic" / c.Const(b"TRZR"), "code_length" / c.Rebuild(c.Int32ul, c.len_(c.this.code)), "key_indexes" / c.Int8ul[V1_SIGNATURE_SLOTS], # pylint: disable=E1136 "flags" / c.BitStruct( c.Padding(7), "restore_storage" / c.Flag, ), "reserved" / c.Padding(52), "signatures" / c.Bytes(64)[V1_SIGNATURE_SLOTS], "code" / c.Bytes(c.this.code_length), c.Terminated, "embedded_onev2" / c.RestreamData(c.this.code, c.Optional(FirmwareOneV2)), ) # fmt: on class FirmwareFormat(Enum): TREZOR_ONE = 1 TREZOR_T = 2 TREZOR_ONE_V2 = 3 FirmwareType = NewType("FirmwareType", c.Container) ParsedFirmware = Tuple[FirmwareFormat, FirmwareType]
"content" / c.GreedyRange( c.FocusedSeq( "keyvalue", "terminator" / c.Peek(c.Byte), c.StopIf(c.this.terminator == 0), "keyvalue" / KeyValue, ) ), c.Const(b'\0'), ) PSBT = c.Struct( "magic" / c.Const(b'psbt'), "sep" / c.Const(b'\xff'), "general" / Sequence, "transaction" / c.RestreamData(c.this.general[0].value, Transaction), "inputs" / c.Array(c.len_(c.this.transaction.inputs), Sequence), "outputs" / c.Array(c.len_(c.this.transaction.outputs), Sequence), c.Terminated, ) # key-less format: ValueOnly = c.Prefixed(CompactUint, c.Struct( "type" / c.Byte, "value" / c.GreedyBytes, )) ValueSequence = c.FocusedSeq("content", "content" / c.GreedyRange( c.FocusedSeq("valueonly",