def pack(self): "Serialize an EPB object into packed bytes" #todo make all arg validation look like this (in order, at top) pkt_data_pad = util.block32_pad_bytes(self.pkt_data_captured) pkt_data_captured_len = len(self.pkt_data_captured) pkt_data_captured_pad_len = len(pkt_data_pad) options_bytes = option.pack_all(self.options_lst) block_total_len = ( 4 + # block type 4 + # block total length 4 + # interface id 4 + # timestamp - high 4 + # timestamp - low 4 + # captured packet length 4 + # original packet length pkt_data_captured_pad_len + len(options_bytes) + 4 ) # block total length (timestamp_high, timestamp_low) = util.uint64_split32(self.time_utc_micros) packed_bytes = ( struct.pack(self.head_encoding, self.SPEC_CODE, block_total_len, self.interface_id, timestamp_high, timestamp_low, pkt_data_captured_len, self.pkt_data_orig_len) + pkt_data_pad + options_bytes + struct.pack(self.tail_encoding, block_total_len)) return packed_bytes
def pack(self): #todo needs test "Serialize into packed bytes" #todo validate type_code data_len_orig = len(self.content) data_pad = util.block32_pad_bytes(self.content) packed_bytes = struct.pack('=HH', self.type_code, data_len_orig) + data_pad return packed_bytes
def pack(self): "Serialize into packed bytes" content_len = len(self.content) spec_len = content_len + 4 # spec definition of length includes PEN content_pad = util.block32_pad_bytes(self.content) packed_bytes = struct.pack('=HHL', self.type_code, spec_len, self.pen_val) + content_pad return packed_bytes
def pack(self): #todo needs test "Serialize to packed bytes" content = to_bytes(self.addr_bytes) content_len = len(content) assert content_len == 6 content_pad = util.block32_pad_bytes(content) packed_bytes = struct.pack('=HH', self.type_code, content_len) + content_pad util.assert_block32_length(packed_bytes) #todo add to all return packed_bytes
def pack(self): "Serialize an SPB object into packed bytes" pkt_data_pad = util.block32_pad_bytes(self.pkt_data) original_pkt_len = len(self.pkt_data) pkt_data_pad_len = len(pkt_data_pad) block_total_len = ( 4 + # block type 4 + # block total length 4 + # original packet length pkt_data_pad_len + 4) # block total length block_bytes = (struct.pack(self.head_encoding, self.SPEC_CODE, block_total_len, original_pkt_len) + pkt_data_pad + struct.pack(self.tail_encoding, block_total_len)) return block_bytes
def test_pad_to_block32(): assert to_bytes([]) == util.block32_pad_bytes([]) assert to_bytes([1, 0, 0, 0]) == util.block32_pad_bytes([1]) assert to_bytes([1, 2, 0, 0]) == util.block32_pad_bytes([1, 2]) assert to_bytes([1, 2, 3, 0]) == util.block32_pad_bytes([1, 2, 3]) assert to_bytes([1, 2, 3, 4]) == util.block32_pad_bytes([1, 2, 3, 4]) assert to_bytes([1, 2, 3, 4, 5, 0, 0, 0]) == util.block32_pad_bytes([1, 2, 3, 4, 5]) assert to_bytes([1, 2, 3, 4, 5, 6, 0, 0]) == util.block32_pad_bytes([1, 2, 3, 4, 5, 6]) assert to_bytes([1, 2, 3, 4, 5, 6, 7, 0]) == util.block32_pad_bytes([1, 2, 3, 4, 5, 6, 7]) assert to_bytes([1, 2, 3, 4, 5, 6, 7, 8]) == util.block32_pad_bytes([1, 2, 3, 4, 5, 6, 7, 8]) util.assert_block32_length([]) util.assert_block32_length([1, 2, 3, 4]) util.assert_block32_length([1, 2, 3, 4, 5, 6, 7, 8]) with pytest.raises(AssertionError): util.assert_block32_length([1]) with pytest.raises(AssertionError): util.assert_block32_length([1, 2]) with pytest.raises(AssertionError): util.assert_block32_length([1, 2, 3])
def add_header(type_code, content_len, content): #todo delete content var? "Utility function to prepend an Option's type_code and length field to packed bytes, with 32-bit padding." content_pad = util.block32_pad_bytes(content) packed_bytes = struct.pack('=HH', type_code, content_len) + content_pad return packed_bytes
def uint16_pack(value): packed_bytes = util.block32_pad_bytes(struct.pack('=HHH', UINT16, 2, value)) return packed_bytes
def uint32_pack(value): packed_bytes = util.block32_pad_bytes(struct.pack('=HHL', UINT32, 4, value)) return packed_bytes
def uint8_pack(value): packed_bytes = util.block32_pad_bytes(struct.pack('=HHB', UINT8, 1, value)) return packed_bytes
def string_utf8_pack(str_val): content = to_bytes(str_val) packed_bytes = util.block32_pad_bytes( struct.pack('=HH', STRING_UTF8, len(content)) + content) return packed_bytes
def float64_pack(value): #todo need double_pack/unpack synonym? packed_bytes = util.block32_pad_bytes( struct.pack('=HHd', FLOAT64, 8, value)) return packed_bytes
def float32_pack(value): packed_bytes = util.block32_pad_bytes( struct.pack('=HHf', FLOAT32, 4, value)) return packed_bytes
def int64_pack(value): packed_bytes = util.block32_pad_bytes(struct.pack('=HHq', INT64, 8, value)) return packed_bytes