Source code for pcapkit.protocols.schema.misc.pcap.header

# -*- coding: utf-8 -*-
# mypy: disable-error-code=assignment
"""header schema for global header of PCAP file format"""

from typing import TYPE_CHECKING

from pcapkit.const.reg.linktype import LinkType as Enum_LinkType
from pcapkit.corekit.fields.numbers import EnumField, Int32Field, UInt16Field, UInt32Field
from pcapkit.corekit.fields.strings import BytesField
from pcapkit.protocols.schema.schema import Schema, schema_final
from pcapkit.utilities.exceptions import ProtocolError

__all__ = ['Header']

if TYPE_CHECKING:
    from typing import Any

    from pcapkit.corekit.fields.numbers import NumberField


def magic_number_callback(field: 'NumberField', packet: 'dict[str, Any]') -> 'None':
    """Calculate byte order of PCAP file.

    Args:
        field: Field instance.
        packet: Packet data.

    """
    magic_number = packet['magic_number']
    if magic_number == b'\xd4\xc3\xb2\xa1':
        field._byteorder = 'little'
    elif magic_number == b'\xa1\xb2\xc3\xd4':
        field._byteorder = 'big'
    elif magic_number == b'\x4d\x3c\xb2\xa1':
        field._byteorder = 'little'
    elif magic_number == b'\xa1\xb2\x3c\x4d':
        field._byteorder = 'big'
    else:
        raise ProtocolError('invalid magic number')