Skip to content
Merged
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
10 changes: 2 additions & 8 deletions scapy/asn1/ber.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,8 @@ def do_dec(cls,
):
# type: (...) -> Tuple[ASN1_Object[int], bytes]
l, s, t = cls.check_type_check_len(s)
x = 0
if s:
if s[0] & 0x80: # negative int
x = -1
for c in s:
x <<= 8
x |= c
return cls.asn1_object(x), t
# One pass: shifting a growing integer per octet was quadratic in width.
return cls.asn1_object(int.from_bytes(s, "big", signed=True)), t


class BERcodec_BOOLEAN(BERcodec_INTEGER):
Expand Down
38 changes: 38 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -4376,6 +4376,44 @@ except BER_Decoding_Error:
pass


= Decode an INTEGER with one bulk numeric conversion

from builtins import int as builtin_int
import scapy.asn1.ber as ber_module

class _CountingIntMeta(type):
# The decode path also asks isinstance(tag, int), so the stand-in has to
# answer that the way the real int would.
def __instancecheck__(cls, obj):
return isinstance(obj, builtin_int)

class CountingInt(builtin_int, metaclass=_CountingIntMeta):
calls = 0
@classmethod
def from_bytes(cls, value, byteorder, signed=False):
CountingInt.calls += 1
return builtin_int.from_bytes(value, byteorder, signed=signed)

wide = b"\x02\x08" + b"\x01" * 8
ber_module.int = CountingInt
try:
decoded, remainder = BERcodec_INTEGER.do_dec(wide)
finally:
del ber_module.int

assert decoded.val == builtin_int.from_bytes(b"\x01" * 8, "big", signed=True)
assert remainder == b""
assert CountingInt.calls == 1

= Decode INTEGER boundary values

assert BERcodec_INTEGER.do_dec(b"\x02\x01\x7f")[0].val == 127
assert BERcodec_INTEGER.do_dec(b"\x02\x01\x80")[0].val == -128
assert BERcodec_INTEGER.do_dec(b"\x02\x02\x00\x80")[0].val == 128
assert BERcodec_INTEGER.do_dec(b"\x02\x01\xff")[0].val == -1
assert BERcodec_INTEGER.do_dec(b"\x02\x00")[0].val == 0


= BER tests - 2

a = b'0c\x02\x01\x01\x04\x06public\xa2V\x02\x01\x01\x02\x01\x00\x02\x01\x000K0I\x06\x03+\x06\x010B0@0>0<0:08060402000.0,0*0(0&0$0"0 0\x1e0\x1c0\x1a0\x180\x160\x140\x120\x100\x0e0\x0c0\n0\x080\x060\x040\x020\x00'
Expand Down
Loading