Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scapy/cbor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@
CBORF_NULL,
CBORF_UNDEFINED,
CBORF_FLOAT,
CBORF_SEQUENCE,
CBORF_SEQUENCE_OF,
CBORF_ARRAY,
CBORF_ARRAY_OF,
CBORF_MAP,
CBORF_SEMANTIC_TAG,
CBORF_optional,
CBORF_CONDITIONAL,
CBORF_PACKET,
CBORF_BYTE_STRING_PACKET,
)

__all__ = [
Expand Down Expand Up @@ -115,11 +119,15 @@
"CBORF_UNDEFINED",
"CBORF_FLOAT",
# Structured fields
"CBORF_SEQUENCE",
"CBORF_SEQUENCE_OF",
"CBORF_ARRAY",
"CBORF_ARRAY_OF",
"CBORF_MAP",
"CBORF_SEMANTIC_TAG",
# Complex fields
"CBORF_optional",
"CBORF_CONDITIONAL",
"CBORF_PACKET",
"CBORF_BYTE_STRING_PACKET",
]
11 changes: 6 additions & 5 deletions scapy/cbor/cbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,12 @@ def __new__(cls,
'Type[CBOR_Object[Any]]',
super(CBOR_Object_metaclass, cls).__new__(cls, name, bases, dct)
)
try:
c.tag.register_cbor_object(c)
except Exception:
# Some objects may not have tags yet
log_runtime.warning("Failed to register CBOR object %r" % c)
if c.tag is not None:
try:
c.tag.register_cbor_object(c)
except Exception:
# Some objects may not have tags yet
log_runtime.exception("Failed to register CBOR object %r" % c)
return c


Expand Down
10 changes: 7 additions & 3 deletions scapy/cbor/cborcodec.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ def __init__(self,


def CBOR_encode_head(major_type, value):
# type: (int, int) -> bytes
# type: (int, Optional[int]) -> bytes
"""
Encode CBOR initial byte and additional info.
Format: 3 bits major type + 5 bits additional info
"""
if value < 24:
if value is None or value < 24:
# Value fits in 5 bits
if value is None:
value = 0x1f
return chb((major_type << 5) | value)
elif value < 256:
# 1-byte value follows
Expand All @@ -92,7 +94,7 @@ def CBOR_encode_head(major_type, value):


def CBOR_decode_head(s):
# type: (bytes) -> Tuple[int, int, bytes]
# type: (bytes) -> Tuple[int, Optional[int], bytes]
"""
Decode CBOR initial byte and additional info.
Returns: (major_type, value, remaining_bytes)
Expand Down Expand Up @@ -134,6 +136,8 @@ def CBOR_decode_head(s):
"Not enough bytes for 8-byte value", remaining=s)
value = struct.unpack(">Q", s[1:9])[0]
return major_type, value, s[9:]
elif additional_info == 31:
return major_type, None, s[1:]
else:
raise CBOR_Codec_Decoding_Error(
"Invalid additional info: %d" % additional_info, remaining=s)
Expand Down
Loading