def unpack(packed_bytes): "Deserialize an EPB object from packed bytes" util.assert_type_bytes(packed_bytes) (block_type, block_total_len, interface_id, timestamp_high, timestamp_low, pkt_data_captured_len, pkt_data_orig_len) = struct.unpack(EnhancedPacketBlock.head_encoding, packed_bytes[:28]) (block_total_len_end, ) = struct.unpack( EnhancedPacketBlock.tail_encoding, packed_bytes[-4:]) time_utc_micros = util.uint64_join32(timestamp_high, timestamp_low) assert block_type == EnhancedPacketBlock.SPEC_CODE #todo verify block type & all fields, all fns assert block_total_len == block_total_len_end == len(packed_bytes) assert pkt_data_captured_len <= pkt_data_orig_len pkt_data_captured_pad_len = util.block32_ceil_num_bytes( pkt_data_captured_len) block_bytes_stripped = packed_bytes[28:-4] pkt_data = block_bytes_stripped[:pkt_data_captured_len] options_bytes = block_bytes_stripped[pkt_data_captured_pad_len:] options_lst = option.unpack_all( EnhancedPacketBlock.UNPACK_DISPATCH_TABLE, options_bytes) result_obj = EnhancedPacketBlock(interface_id, pkt_data, pkt_data_orig_len, options_lst, timestamp=time_utc_micros) return result_obj
def test_custom_option_value(): #todo include standalone value pack/unpack #todo include pack/unpack mixed with regular options def assert_custom_option_value_codec(pen, content): csc = option.CustomStringCopyable(pen, content) csc_unpacked = option.CustomStringCopyable.unpack(csc.pack()) assert csc_unpacked == csc cbc = option.CustomBinaryCopyable(pen, content) cbc_unpacked = option.CustomBinaryCopyable.unpack(cbc.pack()) assert cbc_unpacked == cbc csnc = option.CustomStringNonCopyable(pen, content) csnc_unpacked = option.CustomStringNonCopyable.unpack(csnc.pack()) assert csnc_unpacked == csnc cbnc = option.CustomBinaryNonCopyable(pen, content) cbnc_unpacked = option.CustomBinaryNonCopyable.unpack(cbnc.pack()) assert cbnc_unpacked == cbnc assert_custom_option_value_codec(pen.BROCADE_PEN, '') assert_custom_option_value_codec(pen.BROCADE_PEN, 'a') assert_custom_option_value_codec(pen.BROCADE_PEN, 'go') assert_custom_option_value_codec(pen.BROCADE_PEN, 'ray') assert_custom_option_value_codec(pen.BROCADE_PEN, 'Doh!') assert_custom_option_value_codec(pen.BROCADE_PEN, 'How do you like me now?') unpack_dispatch_table = util.dict_merge_all([ option.Comment.dispatch_entry(), option.CustomStringCopyable.dispatch_entry(), option.CustomBinaryCopyable.dispatch_entry(), option.CustomStringNonCopyable.dispatch_entry(), option.CustomBinaryNonCopyable.dispatch_entry() ]) opts_lst = [ option.Comment("five"), option.CustomStringCopyable(pen.BROCADE_PEN, "yo"), option.Comment("six"), option.CustomBinaryCopyable(pen.BROCADE_PEN, '10011010'), option.Comment("seventy-seven"), option.CustomStringNonCopyable(pen.BROCADE_PEN, "don't copy me"), option.Comment("eight"), option.CustomBinaryNonCopyable(pen.BROCADE_PEN, 'fin'), option.Comment("Agent 009") ] packed_bytes = option.pack_all(opts_lst) opts_lst_unpacked = option.unpack_all(unpack_dispatch_table, packed_bytes) assert opts_lst == opts_lst_unpacked
def unpack(packed_bytes): #todo verify block type & all fields "Deserialize a CBC object from packed bytes" util.assert_type_bytes(packed_bytes) (block_type, block_total_len, pen_val) = struct.unpack(CustomBlock.head_encoding, packed_bytes[:12]) (block_total_len_end, ) = struct.unpack(CustomBlock.tail_encoding, packed_bytes[-4:]) assert (block_type == CustomBlockCopyable.SPEC_CODE) assert pen_val == pcapng.pen.BROCADE_PEN assert block_total_len == block_total_len_end == len(packed_bytes) block_bytes_stripped = packed_bytes[12:-4] (content_bytes, options_bytes ) = util.block32_lv_bytes_unpack_rolling(block_bytes_stripped) options_lst = option.unpack_all(CustomBlock.UNPACK_DISPATCH_TABLE, options_bytes) result_obj = CustomBlockCopyable(pen_val, content_bytes, options_lst) return result_obj
def unpack(block_bytes): #todo verify block type & all fields "Deserialize a IDB object from packed bytes" util.assert_type_bytes(block_bytes) (block_type, block_total_len, link_type, reserved, snaplen) = struct.unpack(InterfaceDescBlock.block_head_encoding, block_bytes[:16]) (block_total_len_end, ) = struct.unpack( InterfaceDescBlock.block_tail_encoding, block_bytes[-4:]) assert block_type == InterfaceDescBlock.SPEC_CODE assert block_total_len == block_total_len_end == len(block_bytes) assert reserved == 0 assert snaplen == 0 options_bytes = block_bytes[16:-4] options_lst = option.unpack_all( InterfaceDescBlock.UNPACK_DISPATCH_TABLE, options_bytes) result_obj = InterfaceDescBlock(link_type, options_lst) return result_obj
def unpack(block_bytes): #todo verify block type & all fields "Deserialize a SHB object from packed bytes" util.assert_type_bytes(block_bytes) (block_type, block_total_len, byte_order_magic, major_version, minor_version, section_len) = struct.unpack(SectionHeaderBlock.block_head_encoding, block_bytes[:24]) (block_total_len_end, ) = struct.unpack( SectionHeaderBlock.block_tail_encoding, block_bytes[-4:]) assert block_type == SectionHeaderBlock.SPEC_CODE assert byte_order_magic == BYTE_ORDER_MAGIC assert major_version == SectionHeaderBlock.MAJOR_VERSION assert minor_version == SectionHeaderBlock.MINOR_VERSION assert block_total_len == block_total_len_end == len(block_bytes) # section_len currently ignored options_bytes = block_bytes[24:-4] options_lst = option.unpack_all( SectionHeaderBlock.UNPACK_DISPATCH_TABLE, options_bytes) result_obj = SectionHeaderBlock(options_lst) return result_obj