2020import enum
2121import io
2222import math
23- import os
2423import platform
2524import re
2625import struct
@@ -1237,6 +1236,12 @@ def __post_init__(self, file: 'Optional[IO[bytes] | bytes]' = None, length: 'Opt
12371236 self ._data = self .pack (** kwargs )
12381237 #: io.BytesIO: Source packet stream.
12391238 self ._file = io .BytesIO (self ._data )
1239+
1240+ # NOTE: The block just built is parsed again below on this same
1241+ # instance, and the parse counts each option afresh against its
1242+ # "only one" guard -- so the count left by the make pass must not
1243+ # carry over into it.
1244+ self ._opt = collections .Counter ()
12401245 else :
12411246 _read = True
12421247 #: io.BytesIO: Source packet stream.
@@ -1572,6 +1577,34 @@ def _make_data(cls, data: 'Data_PCAPNG') -> 'dict[str, Any]': # type: ignore[ov
15721577 'block' : data ,
15731578 }
15741579
1580+ @classmethod
1581+ def _make_packet_data (cls , block : 'Data_PCAPNG' ) -> 'bytes | ProtocolBase' :
1582+ """Create the packet data of an EPB, SPB or PB from its data model.
1583+
1584+ Args:
1585+ block: Block data model.
1586+
1587+ Returns:
1588+ The captured packet octets the block was parsed from, as
1589+ :attr:`~pcapkit.protocols.data.protocol.Protocol.packet` holds them,
1590+ cut to the block's ``captured_len``; or, for a data model that
1591+ carries none, the payload rebuilt from its decoded next layer.
1592+
1593+ Note:
1594+ The cut is what keeps a block with ``captured_len`` of zero intact:
1595+ its ``packet`` can hold octets past the payload (:issue:`1275`), and
1596+ writing those back would grow the block.
1597+
1598+ The fallback is lossy. A next layer rebuilt from its own data model
1599+ drops anything it did not decode, such as Ethernet pad octets, so
1600+ the block it yields can be shorter than its ``captured_len`` says.
1601+
1602+ """
1603+ packet = block .get ('packet' )
1604+ if isinstance (packet , bytes ):
1605+ return packet [:block .captured_len ]
1606+ return cls ._make_payload (block )
1607+
15751608 def _make_timestamp (self , timestamp : 'Optional[float | Decimal | dt_type | int]' = None , * ,
15761609 interface_id : 'int' = 0 ) -> 'tuple[int, int]' :
15771610 """Make timestamp.
@@ -3492,6 +3525,10 @@ def _read_secrets_tls(self, schema: 'Schema_TLSKeyLog', *,
34923525 secrets = Data_TLSKeyLog (
34933526 entries = schema .entries ,
34943527 )
3528+ # NOTE: the log text is kept as well, since :attr:`entries` holds
3529+ # neither its comments nor the order of its lines, and a rebuild writes
3530+ # it back verbatim.
3531+ secrets .__update__ (data = schema .data )
34953532 return secrets
34963533
34973534 def _read_secrets_wireguard (self , schema : 'Schema_WireGuardKeyLog' , * ,
@@ -3509,6 +3546,10 @@ def _read_secrets_wireguard(self, schema: 'Schema_WireGuardKeyLog', *,
35093546 secrets = Data_WireGuardKeyLog (
35103547 entries = schema .entries ,
35113548 )
3549+ # NOTE: the log text is kept as well, since :attr:`entries` holds
3550+ # neither its comments nor the order of its lines, and a rebuild writes
3551+ # it back verbatim.
3552+ secrets .__update__ (data = schema .data )
35123553 return secrets
35133554
35143555 def _read_secrets_zigbee_nwk (self , schema : 'Schema_ZigBeeNWKKey' , * ,
@@ -3757,6 +3798,7 @@ def _make_block_epb(self, block: 'Optional[Data_EnhancedPacketBlock]' = None, *,
37573798 timestamp = block .timestamp_epoch
37583799 captured_len = block .captured_len
37593800 original_len = block .original_len
3801+ packet_data = self ._make_packet_data (block )
37603802 options = block .options
37613803
37623804 timestamp_high , timestamp_low = self ._make_timestamp (timestamp , interface_id = interface_id )
@@ -3805,6 +3847,7 @@ def _make_block_spb(self, block: 'Optional[Data_SimplePacketBlock]' = None, *,
38053847 """
38063848 if block is not None :
38073849 original_len = block .original_len
3850+ packet_data = self ._make_packet_data (block )
38083851
38093852 if original_len is None :
38103853 original_len = len (packet_data )
@@ -3878,6 +3921,9 @@ def _make_block_isb(self, block: 'Optional[Data_InterfaceStatisticsBlock]' = Non
38783921 options = block .options
38793922
38803923 timestamp_high , timestamp_low = self ._make_timestamp (timestamp , interface_id = interface_id )
3924+ # NOTE: ``isb_starttime`` and ``isb_endtime`` are scaled by the
3925+ # resolution of this block's interface, as on the parse path.
3926+ self ._isb_interface_id = interface_id # pylint: disable=attribute-defined-outside-init
38813927
38823928 if options is not None :
38833929 options_value , total_length = self ._make_pcapng_options (options , namespace = 'isb' )
@@ -4094,6 +4140,7 @@ def _make_block_packet(self, block: 'Optional[Data_PacketBlock]' = None, *,
40944140 timestamp = block .timestamp_epoch
40954141 captured_len = block .captured_len
40964142 original_len = block .original_len
4143+ packet_data = self ._make_packet_data (block )
40974144 options = block .options
40984145
40994146 timestamp_high , timestamp_low = self ._make_timestamp (timestamp , interface_id = interface_id )
@@ -4427,7 +4474,7 @@ def _make_option_if_ipv6(self, type: 'Enum_OptionType', option: 'Optional[Data_I
44274474
44284475 return Schema_IF_IPv6AddrOption (
44294476 type = type ,
4430- length = 8 ,
4477+ length = 17 ,
44314478 interface = interface ,
44324479 )
44334480
@@ -5774,7 +5821,10 @@ def _make_secrets_tls(self, type: 'Enum_SecretsType', secrets: 'Optional[Data_TL
57745821
57755822 Args:
57765823 type: Secrets type.
5777- secrets: Secrets data model.
5824+ secrets: Secrets data model. The key log text it was parsed from,
5825+ if any, is written back verbatim; otherwise its entries are.
5826+ On a parsed model the text wins, so an edit to its
5827+ ``entries`` alone is silently ignored.
57785828 entries: TLS Key Log entries.
57795829 **kwargs: Arbitrary keyword arguments.
57805830
@@ -5785,15 +5835,20 @@ def _make_secrets_tls(self, type: 'Enum_SecretsType', secrets: 'Optional[Data_TL
57855835 from pcapkit import __version__
57865836
57875837 if secrets is not None :
5838+ text = secrets .get ('data' )
5839+ if isinstance (text , str ):
5840+ return Schema_TLSKeyLog (
5841+ data = text ,
5842+ )
57885843 entries = secrets .entries
57895844
57905845 if entries is None :
57915846 entries = {}
57925847
5793- data = [f'# generated by PyPCAPKit v{ __version__ } at { datetime . datetime . now (). isoformat () } { os . sep } ' ] # type: list[str]
5848+ data = [f'# generated by PyPCAPKit v{ __version__ } \n ' ] # type: list[str]
57945849 for label , entry in entries .items ():
57955850 for k , v in entry .items (multi = True ):
5796- data .append (f'{ label .name } { k .hex ()} { v .hex ()} { os . sep } ' )
5851+ data .append (f'{ label .name } { k .hex ()} { v .hex ()} \n ' )
57975852
57985853 return Schema_TLSKeyLog (
57995854 data = '' .join (data ),
@@ -5806,7 +5861,10 @@ def _make_secrets_wireguard(self, type: 'Enum_SecretsType', secrets: 'Optional[D
58065861
58075862 Args:
58085863 type: Secrets type.
5809- secrets: Secrets data model.
5864+ secrets: Secrets data model. The key log text it was parsed from,
5865+ if any, is written back verbatim; otherwise its entries are.
5866+ On a parsed model the text wins, so an edit to its
5867+ ``entries`` alone is silently ignored.
58105868 entries: WireGuard Key Log entries.
58115869 **kwargs: Arbitrary keyword arguments.
58125870
@@ -5817,14 +5875,19 @@ def _make_secrets_wireguard(self, type: 'Enum_SecretsType', secrets: 'Optional[D
58175875 from pcapkit import __version__
58185876
58195877 if secrets is not None :
5878+ text = secrets .get ('data' )
5879+ if isinstance (text , str ):
5880+ return Schema_WireGuardKeyLog (
5881+ data = text ,
5882+ )
58205883 entries = secrets .entries
58215884
58225885 if entries is None :
58235886 entries = OrderedMultiDict ()
58245887
5825- data = [f'# generated by PyPCAPKit v{ __version__ } at { datetime . datetime . now (). isoformat () } { os . sep } ' ] # type: list[str]
5888+ data = [f'# generated by PyPCAPKit v{ __version__ } \n ' ] # type: list[str]
58265889 for label , value in entries .items (multi = True ):
5827- data .append (f'{ label .name } = { base64 .b64encode (value ).decode ()} { os . sep } ' )
5890+ data .append (f'{ label .name } = { base64 .b64encode (value ).decode ()} \n ' )
58285891
58295892 return Schema_WireGuardKeyLog (
58305893 data = '' .join (data ),
0 commit comments