from construct3 import Struct, Sequence, int8, Embedded, Padding, Computed, this, anchor, Container x = Struct( "a" / int8, "b" / int8, Padding(2), "c" / int8, Embedded(Struct( "d" / int8, "e" / int8, "x" / Computed(this.d - this.a), )), "f" / int8, # "g" / anchor, ) #print x.unpack("ABppCDEF") print repr(x.pack(Container(a=1,b=2,c=3,d=4,e=5,f=6))) y = Sequence( int8, int8, Padding(2), int8, Embedded(Sequence( int8, int8, Computed(this[4] - this[0]), )), int8,
from construct3 import Struct, Sequence, int8, Embedded, Padding, Computed, this, anchor, Container x = Struct( "a" / int8, "b" / int8, Padding(2), "c" / int8, Embedded(Struct( "d" / int8, "e" / int8, "x" / Computed(this.d - this.a), )), "f" / int8, # "g" / anchor, ) #print x.unpack("ABppCDEF") print repr(x.pack(Container(a=1, b=2, c=3, d=4, e=5, f=6))) y = Sequence( int8, int8, Padding(2), int8, Embedded(Sequence( int8, int8, Computed(this[4] - this[0]), )), int8, anchor,
ipaddr = byte[4] print ipaddr.unpack("ABCD") # [65, 66, 67, 68] ipaddr = Adapter(byte[4], decode=lambda arr, _: ".".join(map(str, arr)), encode=lambda ipstr, _: map(int, ipstr.split("."))) print ipaddr.unpack("ABCD") # 65.66.67.68 print repr(ipaddr.pack("127.0.0.1")) # '\x7f\x00\x00\x01' mac_addr = Adapter( byte[6], decode=lambda arr, _: "-".join(map("%02x".__mod__, arr)), encode=lambda macstr, _: macstr.replace("-", "").decode("hex")) ethernet_header = Struct("destination" / mac_addr, "source" / mac_addr, "type" / int16ub) print ethernet_header.unpack("123456ABCDEF\x86\xDD") ethernet_header = Struct( "destination" / mac_addr, "source" / mac_addr, "type" / Enum( int16ub, IPv4=0x0800, ARP=0x0806, RARP=0x8035, X25=0x0805, IPX=0x8137, IPv6=0x86DD, )) print ethernet_header.unpack("123456ABCDEF\x86\xDD")
ipv4_header = Struct( +BitStruct( "version" / nibble, "header_length" / Adapter(nibble, decode = lambda obj, _: obj * 4, encode = lambda obj, _: obj / 4), ), "tos" / BitStruct( "precedence" / Bits(3), "minimize_delay" / flag, "high_throuput" / flag, "high_reliability" / flag, "minimize_cost" / flag, Padding(1), ), "total_length" / int16ub, "payload_length" / Computed(this.total_length - this.header_length), int16ub / "identification", +BitStruct( "flags" / Struct( Padding(1), "dont_fragment" / flag, "more_fragments" / flag, ), "frame_offset" / Bits(13), ), "ttl" / int8u, "protocol" / Enum(int8u, ICMP = 1, TCP = 6, UDP = 17), "checksum" / int16ub, "source" / ipaddr, "destination" / ipaddr, "options" / Raw(this.header_length - 20), )
ipaddr = Adapter(byte[4], decode = lambda arr, _: ".".join(map(str, arr)), encode = lambda ipstr, _: map(int, ipstr.split(".")) ) print ipaddr.unpack("ABCD") # 65.66.67.68 print repr(ipaddr.pack("127.0.0.1")) # '\x7f\x00\x00\x01' mac_addr = Adapter(byte[6], decode = lambda arr, _: "-".join(map("%02x".__mod__, arr)), encode = lambda macstr, _: macstr.replace("-", "").decode("hex") ) ethernet_header = Struct( "destination" / mac_addr, "source" / mac_addr, "type" / int16ub ) print ethernet_header.unpack("123456ABCDEF\x86\xDD") ethernet_header = Struct( "destination" / mac_addr, "source" / mac_addr, "type" / Enum(int16ub, IPv4 = 0x0800, ARP = 0x0806, RARP = 0x8035, X25 = 0x0805, IPX = 0x8137, IPv6 = 0x86DD,