@@ -262,6 +262,28 @@ def _utf8_length(text: 'str') -> 'int':
262262 return len (text .encode ('utf-8' ))
263263
264264
265+ def _join_names (names : 'list[str]' ) -> 'str' :
266+ """Join NRB record names into zero-terminated name resolution data.
267+
268+ Args:
269+ names: Host names; a :class:`~pcapkit.corekit.fields.strings.DecodedString`
270+ contributes the octets it was decoded from.
271+
272+ Returns:
273+ The name resolution data, as a
274+ :class:`~pcapkit.corekit.fields.strings.DecodedString` when any name
275+ carries octets that its text does not encode back to.
276+
277+ """
278+ names = list (names ) or ['' ]
279+ text = '' .join (f'{ name } \x00 ' for name in names )
280+ raw = b'' .join ((name .raw if isinstance (name , DecodedString ) else name .encode ('utf-8' )) + b'\x00 '
281+ for name in names )
282+ if text .encode ('utf-8' ) == raw :
283+ return text
284+ return DecodedString (text , raw )
285+
286+
265287class PacketDirection (EnumLookup , enum .IntEnum ):
266288 """Packet direction for ``epb_flags`` options.
267289
@@ -1020,6 +1042,10 @@ def pack(self, **kwargs: 'Any') -> 'bytes':
10201042 if self ._ctx is not None :
10211043 self ._byte = self ._ctx .section .byteorder
10221044 packet ['byteorder' ] = self ._byte
1045+ elif self ._type == Enum_BlockType .Section_Header_Block :
1046+ # NOTE: A section header starts its own section, so the byte order
1047+ # is the one :meth:`_make_block_shb` chose rather than the host's.
1048+ packet .setdefault ('byteorder' , self ._byte )
10231049 return self .__header__ .pack (packet )
10241050
10251051 def unpack (self , length : 'Optional[int]' = None , ** kwargs : 'Any' ) -> 'Data_PCAPNG' :
@@ -1600,6 +1626,28 @@ def _make_data(cls, data: 'Data_PCAPNG') -> 'dict[str, Any]': # type: ignore[ov
16001626 'block' : data ,
16011627 }
16021628
1629+ @staticmethod
1630+ def _flags_enum (enum : 'Type[PacketDirection] | Type[PacketReception]' , value : 'int' ,
1631+ option : 'Enum_OptionType' ) -> 'Any' :
1632+ """Look a flags word sub-field up in its enumeration.
1633+
1634+ Args:
1635+ enum: :class:`PacketDirection` or :class:`PacketReception`.
1636+ value: Sub-field value.
1637+ option: Option type, for the error message.
1638+
1639+ Returns:
1640+ The enumeration member.
1641+
1642+ Raises:
1643+ ProtocolError: If the specification assigns ``value`` no meaning.
1644+
1645+ """
1646+ try :
1647+ return enum (value )
1648+ except ValueError :
1649+ raise ProtocolError (f'PCAP-NG: [{ option .name } ] invalid { enum .__name__ } : { value :#b} ' ) from None
1650+
16031651 @classmethod
16041652 def _make_packet_data (cls , block : 'Data_PCAPNG' ) -> 'bytes | ProtocolBase' :
16051653 """Create the packet data of an EPB, SPB or PB from its data model.
@@ -2920,9 +2968,10 @@ def _read_option_epb_flags(self, schema: 'Schema_EPB_FlagsOption', *,
29202968 option = Data_EPB_FlagsOption (
29212969 type = schema .type ,
29222970 length = schema .length ,
2923- direction = PacketDirection ( schema .flags ['direction' ]),
2924- reception = PacketReception ( schema .flags ['reception' ]),
2971+ direction = self . _flags_enum ( PacketDirection , schema .flags ['direction' ], schema . type ),
2972+ reception = self . _flags_enum ( PacketReception , schema .flags ['reception' ], schema . type ),
29252973 fcs_len = schema .flags ['fcs_len' ],
2974+ reserved = schema .flags ['reserved' ],
29262975 crc_error = bool (schema .flags ['crc_error' ]),
29272976 too_long = bool (schema .flags ['too_long' ]),
29282977 too_short = bool (schema .flags ['too_short' ]),
@@ -3378,9 +3427,10 @@ def _read_option_pack_flags(self, schema: 'Schema_PACK_FlagsOption', *,
33783427 option = Data_PACK_FlagsOption (
33793428 type = schema .type ,
33803429 length = schema .length ,
3381- direction = PacketDirection ( schema .flags ['direction' ]),
3382- reception = PacketReception ( schema .flags ['reception' ]),
3430+ direction = self . _flags_enum ( PacketDirection , schema .flags ['direction' ], schema . type ),
3431+ reception = self . _flags_enum ( PacketReception , schema .flags ['reception' ], schema . type ),
33833432 fcs_len = schema .flags ['fcs_len' ],
3433+ reserved = schema .flags ['reserved' ],
33843434 crc_error = bool (schema .flags ['crc_error' ]),
33853435 too_long = bool (schema .flags ['too_long' ]),
33863436 too_short = bool (schema .flags ['too_short' ]),
@@ -3696,7 +3746,10 @@ def _make_block_unknown(self, block: 'Optional[Data_UnknownBlock]' = None, *,
36963746 if block is not None :
36973747 data = block .body
36983748
3699- length = len (data )
3749+ # NOTE: The body is padded to 32 bits, and Block Total Length counts it
3750+ # together with the 12 octets of block type and both length fields.
3751+ data += bytes (- len (data ) % 4 )
3752+ length = len (data ) + 12
37003753
37013754 return Schema_UnknownBlock (
37023755 length = length ,
@@ -3710,6 +3763,7 @@ def _make_block_shb(self, block: 'Optional[Data_SectionHeaderBlock]' = None, *,
37103763 minor_version : 'Optional[int]' = None ,
37113764 section_length : 'int' = - 1 ,
37123765 options : 'Optional[Option | list[Schema_Option | tuple[Enum_OptionType, dict[str, Any]] | bytes]]' = None ,
3766+ byteorder : 'Literal["big", "little"]' = sys .byteorder ,
37133767 ** kwargs : 'Any' ) -> 'Schema_SectionHeaderBlock' :
37143768 """Make PCAP-NG section header block (SHB).
37153769
@@ -3720,13 +3774,16 @@ def _make_block_shb(self, block: 'Optional[Data_SectionHeaderBlock]' = None, *,
37203774 minor_version: Minor version number.
37213775 section_length: Section length.
37223776 options: Block options.
3777+ byteorder: Byte order of the section; a parsed block keeps its own.
37233778 **kwargs: Arbitrary keyword arguments.
37243779
37253780 Returns:
37263781 Constructed block schema.
37273782
37283783 """
3729- self ._byte = sys .byteorder
3784+ if block is not None :
3785+ byteorder = block .byteorder
3786+ self ._byte = byteorder
37303787
37313788 if block is not None :
37323789 major_version = block .version .major
@@ -4958,6 +5015,7 @@ def _make_option_epb_flags(self, type: 'Enum_OptionType', option: 'Optional[Data
49585015 reception_namespace : 'Optional[dict[str, int] | dict[int, str] | Type[StdlibEnum] | Type[AenumEnum]]' = None , # pylint: disable=line-too-long
49595016 reception_reversed : 'bool' = False ,
49605017 fcs_len : 'int' = 0 ,
5018+ reserved : 'int' = 0 ,
49615019 crc_error : 'bool' = False ,
49625020 too_long : 'bool' = False ,
49635021 too_short : 'bool' = False ,
@@ -4981,6 +5039,7 @@ def _make_option_epb_flags(self, type: 'Enum_OptionType', option: 'Optional[Data
49815039 reception_namespace: Namespace of packet reception.
49825040 reception_reversed: Whether to reverse packet reception namespace.
49835041 fcs_len: Length of FCS field, in bytes.
5042+ reserved: Bits 9 to 23 of the flags word, verbatim.
49845043 crc_error: Whether CRC error occurred.
49855044 too_long: Whether packet is too long.
49865045 too_short: Whether packet is too short.
@@ -5006,6 +5065,7 @@ def _make_option_epb_flags(self, type: 'Enum_OptionType', option: 'Optional[Data
50065065 direction_val = option .direction
50075066 reception_val = option .reception
50085067 fcs_len = option .fcs_len
5068+ reserved = option .reserved
50095069 crc_error = option .crc_error
50105070 too_long = option .too_long
50115071 too_short = option .too_short
@@ -5027,6 +5087,7 @@ def _make_option_epb_flags(self, type: 'Enum_OptionType', option: 'Optional[Data
50275087 'direction' : getattr (direction_val , 'value' , direction_val ),
50285088 'reception' : getattr (reception_val , 'value' , reception_val ),
50295089 'fcs_len' : fcs_len ,
5090+ 'reserved' : reserved ,
50305091 'crc_error' : int (crc_error ),
50315092 'too_long' : int (too_long ),
50325093 'too_short' : int (too_short ),
@@ -5198,9 +5259,8 @@ def _make_option_epb_verdict(self, type: 'Enum_OptionType', option: 'Optional[Da
51985259 if self ._type != Enum_BlockType .Enhanced_Packet_Block :
51995260 raise ProtocolError (f'PCAP-NG: [epb_verdict] option must be in Enhanced Packet Block, '
52005261 f'but found in { self ._type } block.' )
5201- if self ._opt [type ] > 0 :
5202- raise ProtocolError (f'PCAP-NG: [epb_verdict] option must be only one, '
5203- f'but { self ._opt [type ] + 1 } found.' )
5262+ # NOTE: No only-one guard: the specification allows ``epb_verdict`` to
5263+ # appear multiple times, and the parser accepts every occurrence.
52045264
52055265 if option is not None :
52065266 verdict_val = option .verdict
@@ -5540,6 +5600,7 @@ def _make_option_pack_flags(self, type: 'Enum_OptionType', option: 'Optional[Dat
55405600 reception_namespace : 'Optional[dict[str, int] | dict[int, str] | Type[StdlibEnum] | Type[AenumEnum]]' = None , # pylint: disable=line-too-long
55415601 reception_reversed : 'bool' = False ,
55425602 fcs_len : 'int' = 0 ,
5603+ reserved : 'int' = 0 ,
55435604 crc_error : 'bool' = False ,
55445605 too_long : 'bool' = False ,
55455606 too_short : 'bool' = False ,
@@ -5563,6 +5624,7 @@ def _make_option_pack_flags(self, type: 'Enum_OptionType', option: 'Optional[Dat
55635624 reception_namespace: Namespace of packet reception.
55645625 reception_reversed: Whether to reverse packet reception namespace.
55655626 fcs_len: Length of FCS field, in bytes.
5627+ reserved: Bits 9 to 23 of the flags word, verbatim.
55665628 crc_error: Whether CRC error occurred.
55675629 too_long: Whether packet is too long.
55685630 too_short: Whether packet is too short.
@@ -5588,6 +5650,7 @@ def _make_option_pack_flags(self, type: 'Enum_OptionType', option: 'Optional[Dat
55885650 direction_val = option .direction
55895651 reception_val = option .reception
55905652 fcs_len = option .fcs_len
5653+ reserved = option .reserved
55915654 crc_error = option .crc_error
55925655 too_long = option .too_long
55935656 too_short = option .too_short
@@ -5609,6 +5672,7 @@ def _make_option_pack_flags(self, type: 'Enum_OptionType', option: 'Optional[Dat
56095672 'direction' : getattr (direction_val , 'value' , direction_val ),
56105673 'reception' : getattr (reception_val , 'value' , reception_val ),
56115674 'fcs_len' : fcs_len ,
5675+ 'reserved' : reserved ,
56125676 'crc_error' : int (crc_error ),
56135677 'too_long' : int (too_long ),
56145678 'too_short' : int (too_short ),
@@ -5809,11 +5873,11 @@ def _make_record_ipv4(self, type: 'Enum_RecordType', record: 'Optional[Data_IPv4
58095873
58105874 if names is None :
58115875 names = []
5812- records = ' \x00 ' . join (names ) + ' \x00 '
5876+ records = _join_names (names )
58135877
58145878 return Schema_IPv4Record (
58155879 type = type ,
5816- length = 4 + len (records ),
5880+ length = 4 + _utf8_length (records ),
58175881 ip = ip ,
58185882 resol = records ,
58195883 )
@@ -5841,11 +5905,11 @@ def _make_record_ipv6(self, type: 'Enum_RecordType', record: 'Optional[Data_IPv6
58415905
58425906 if names is None :
58435907 names = []
5844- records = ' \x00 ' . join (names ) + ' \x00 '
5908+ records = _join_names (names )
58455909
58465910 return Schema_IPv6Record (
58475911 type = type ,
5848- length = 16 + len (records ),
5912+ length = 16 + _utf8_length (records ),
58495913 ip = ip ,
58505914 resol = records ,
58515915 )
0 commit comments