0
  B   ˆl–Ü4ý('ÔËm
eAãb¸ËŠbÑ¿�þc–Q¬à7Ï
@ùÊ„ið´çÉ_ŠI|¥�+Wé3ž¯ÉÈ ?÷     # -*- coding: utf-8 -*-
# Common utility functions
# SPDX-License-Identifier: BSD-2-Clause

from __future__ import print_function

import struct
import ntp.poly
from ntp.util import slicedata


internetPrefix = (1, 3, 6, 1)  # Used by the prefix option of OID headers
prefixCount = len(internetPrefix)

# ==========================================================================
#
# Callables fall into the following categories:
#   Data type encoder/decoders
#   Packet classes/encoders
#   Packet body decoders
#   Glue/Utility/Misc functions
#
# To encode a packet, create an instance of that packet type's class, then
# call the encode() method.
#
# To decode a packet, call the decode_packet function, which will select
# the correct decoder for that packet type.
#
# ==========================================================================

# ==========================================================================
#
# Error classes
#
# ==========================================================================


class ParseError(Exception):
    def __init__(self, message, header=None, packetData="", remainingData=""):
        Exception.__init__(self)
        self.message = message
        self.header = header
        self.packetData = packetData
        self.remainingData = remainingData


class ParseDataLengthError(ParseError):
    pass


class ParseVersionError(ParseError):
    pass


class ParsePDUTypeError(ParseError):
    pass


# ==========================================================================
#
# Packet encoders / decoders
#
#   Encoders are class methods which output a string version of the
#   packet, ready for transmission.
#
#   Decoders take just the body sliced away from everything else,
#   and a dict containing the already parsed information from the header.
#   They return the relevant class instance for the packet in question,
#   they do not return extra data as they are never supposed to receive it.
#
#   Decoders are not meant to be called directly by external code,
#   only by decode_packet().
#
# ==========================================================================


class AgentXPDU:
    def __init__(self, pduType, bigEndian, sID, tactID, pktID,
                 hascontext=False, context=None):
        self.pduType = pduType
        self.bigEndian = bigEndian
        self.sessionID = sID
        self.transactionID = tactID
        self.packetID = pktID
        self.context = context
        self._hascontext = hascontext

    def packetVars(self):
        "Assembles a list of class variables that it is desirable to print"
        pktvars = {}
        names = dir(self)
        names.remove("context")
        for vname in names:
            if vname[0] == "_":  # ignores specials, and what we want ignored
                continue
            var = getattr(self, vname)
            if callable(var):  # ignores methods
                # *might* have to rethink this if contents get classified
                continue
            pktvars[vname] = var
        if self._hascontext:
            # context is always present, not always used
            pktvars["context"] = self.context
        return pktvars

    def __repr__(self):
        s = self.__class__.__name__ + "("
        v = []
        myvars = self.packetVars()
        keys = list(myvars.keys())
        keys.sort()  # they will always be in the same order: testable
        for name in keys:
            value = myvars[name]
            v.append("%s=%s" % (name, repr(value)))
        s += ", ".join(v) + ")"
        return s

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return False
        if self.pduType != other.pduType:
            return False
        if self.bigEndian != other.bigEndian:
            return False
        if self.sessionID != other.sessionID:
            return False
        if self.transactionID != other.transactionID:
            return False
        if self.packetID != other.packetID:
   