Source code for pcapkit.corekit.schema.entries
# -*- coding: utf-8 -*-
"""Whole List Entries
========================
.. module:: pcapkit.corekit.schema.entries
:mod:`pcapkit.corekit.schema.entries` refuses a list length that leaves part of a
fixed-width entry, for the lists of SCTP's parameters, causes and chunks, of HIP's
parameters and of IPv6-Route's Source Route alike. It is one of the
:mod:`pcapkit.corekit.schema` helpers shared across the schema layer, not part of
the header schema framework in :mod:`pcapkit.protocols.schema`; see
:ref:`utilities-or-corekit`.
.. versionadded:: 1.10.0
"""
from typing import TYPE_CHECKING
from pcapkit.utilities.exceptions import ProtocolError
if TYPE_CHECKING:
from typing import Any, Callable
__all__ = ['whole_entries']
[docs]
def whole_entries(length: 'Callable[[dict[str, Any]], int]', width: 'int',
message: 'str') -> 'Callable[[dict[str, Any]], int]':
"""Refuse a list length that leaves part of a fixed-width entry.
Args:
length: Callback computing the length of a list of ``width``-octet
entries from the packet data.
width: Octets of each entry.
message: What the refusal says, led by the protocol's alias, as a
:meth:`str.format_map` template the packet data fills in.
Returns:
A callback returning that length, which raises
:exc:`~pcapkit.utilities.exceptions.ProtocolError` where it is no whole
number of entries: a structural refusal (:ref:`rfc-conformance`), quiet
under a next-layer fallback (:issue:`2588`). A length of zero or below,
as a pack resolves the field before it has one, passes.
.. versionadded:: 1.10.0
"""
def callback(pkt: 'dict[str, Any]') -> 'int':
value = length(pkt)
if value > 0 and value % width:
raise ProtocolError(f'{message.format_map(pkt)}, not a whole number of {width}-octet entries')
return value
return callback