Skip to content

Add generic codec base class combining shared ASN.1/CBOR codec logic#29

Draft
Copilot wants to merge 2 commits into
masterfrom
copilot/create-generic-codec-class
Draft

Add generic codec base class combining shared ASN.1/CBOR codec logic#29
Copilot wants to merge 2 commits into
masterfrom
copilot/create-generic-codec-class

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 17, 2026

BERcodec_Object and CBORcodec_Object duplicated the same metaclass registration pattern, check_string, dec (safe-mode skeleton), and safedec. This extracts those identical parts into a shared base in scapy/libs/codec.py.

New: scapy/libs/codec.py

  • GenericCodec_metaclass — handles c.tag.register(c.codec, c) once; exposes _handle_registration_error hook for format-specific logging
  • GenericCodecObject[_K] — provides the shared methods:
    • check_string — parameterised by _decoding_error_class
    • dec — parameterised by _generic_error_classes + _decoding_error_object_class; covers CBOR fully, overridable for BER's BadTag recovery
    • safedec — was 100% identical in both; now lives in one place
    • enc — abstract stub

scapy/asn1/ber.py

  • BERcodec_metaclass → inherits GenericCodec_metaclass; overrides _handle_registration_error with warning()
  • BERcodec_Object → inherits GenericCodecObject; removes check_string + safedec; retains dec override for BER_BadTag_Decoding_Error recovery path

scapy/cbor/cborcodec.py

  • CBORcodec_metaclass → inherits GenericCodec_metaclass; overrides _handle_registration_error with log_runtime.error()
  • CBORcodec_Object → inherits GenericCodecObject; removes check_string, dec, and safedec entirely — all three are now fully inherited

Usage pattern for new codec formats

from scapy.libs.codec import GenericCodec_metaclass, GenericCodecObject

class MyCodec_metaclass(GenericCodec_metaclass):
    @classmethod
    def _handle_registration_error(cls, c, exc):
        log_runtime.warning("Failed to register %r" % c)

class MyCodecObject(GenericCodecObject[_K], metaclass=MyCodec_metaclass):
    codec = My_Codecs.MY_FORMAT
    tag   = My_Tags.SOME_TAG
    _decoding_error_class        = MyDecodingError
    _generic_error_classes       = (MyDecodingError, MyError)
    _decoding_error_object_class = MY_DECODING_ERROR

    @classmethod
    def do_dec(cls, s, context=None, safe=False):
        ...  # format-specific logic only

    @classmethod
    def enc(cls, s):
        ...  # format-specific logic only

💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title [WIP] Add generic codec class for ASN1 and CBOR Add generic codec base class combining shared ASN.1/CBOR codec logic Mar 17, 2026
Copilot AI requested a review from polybassa March 17, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants