X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pyderasn.py;h=d637053f53f88e6c2aa72a2014d4b6066f41e201;hb=1280a3daf22524df59b7b0379b1d00b54a77f03a;hp=0d3a6637840115c15fdcf90bc898f095f0c640f7;hpb=517f9ab0bb42c9d89fade2aa69ec5da9501efc0a;p=pyderasn.git diff --git a/pyderasn.py b/pyderasn.py index 0d3a663..d637053 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -1,5 +1,6 @@ #!/usr/bin/env python # coding: utf-8 +# cython: language_level=3 # PyDERASN -- Python ASN.1 DER/BER codec with abstract structures # Copyright (C) 2017-2020 Sergey Matveev # @@ -763,6 +764,20 @@ EOC = b"\x00\x00" EOC_LEN = len(EOC) LENINDEF = b"\x80" # length indefinite mark LENINDEF_PP_CHAR = "I" if PY2 else "∞" +NAMEDTUPLE_KWARGS = {} if PY2 else {"module": __name__} +SET01 = frozenset(("0", "1")) +DECIMAL_SIGNS = ".," + + +def pureint(value): + i = int(value) + if (value[0] in "+- ") or (value[-1] == " "): + raise ValueError("non-pure integer") + return i + +def fractions2float(fractions_raw): + pureint(fractions_raw) + return float("0." + fractions_raw) ######################################################################## @@ -1479,7 +1494,7 @@ PP = namedtuple("PP", ( "lenindef", "ber_encoded", "bered", -)) +), **NAMEDTUPLE_KWARGS) def _pp( @@ -1739,7 +1754,7 @@ BooleanState = namedtuple("BooleanState", ( "expl_lenindef", "lenindef", "ber_encoded", -)) +), **NAMEDTUPLE_KWARGS) class Boolean(Obj): @@ -1983,7 +1998,7 @@ IntegerState = namedtuple("IntegerState", ( "expl_lenindef", "lenindef", "ber_encoded", -)) +), **NAMEDTUPLE_KWARGS) class Integer(Obj): @@ -2336,7 +2351,6 @@ class Integer(Obj): yield pp -SET01 = frozenset(("0", "1")) BitStringState = namedtuple("BitStringState", ( "version", "specs", @@ -2353,7 +2367,7 @@ BitStringState = namedtuple("BitStringState", ( "ber_encoded", "tag_constructed", "defined", -)) +), **NAMEDTUPLE_KWARGS) class BitString(Obj): @@ -2864,7 +2878,7 @@ OctetStringState = namedtuple("OctetStringState", ( "ber_encoded", "tag_constructed", "defined", -)) +), **NAMEDTUPLE_KWARGS) class OctetString(Obj): @@ -3252,7 +3266,7 @@ NullState = namedtuple("NullState", ( "expl_lenindef", "lenindef", "ber_encoded", -)) +), **NAMEDTUPLE_KWARGS) class Null(Obj): @@ -3422,14 +3436,7 @@ ObjectIdentifierState = namedtuple("ObjectIdentifierState", ( "lenindef", "ber_encoded", "defines", -)) - - -def pureint(value): - i = int(value) - if (value[0] in "+- ") or (value[-1] == " "): - raise ValueError("non-pure integer") - return i +), **NAMEDTUPLE_KWARGS) class ObjectIdentifier(Obj): @@ -3816,7 +3823,7 @@ class Enumerated(Integer): def escape_control_unicode(c): - if unicat(c).startswith("C"): + if unicat(c)[0] == "C": c = repr(c).lstrip("u").strip("'") return c @@ -4009,6 +4016,7 @@ class NumericString(AllowableCharsMixin, CommonString): PrintableStringState = namedtuple( "PrintableStringState", OctetStringState._fields + ("allowable_chars",), + **NAMEDTUPLE_KWARGS ) @@ -4142,11 +4150,6 @@ LEN_YYYYMMDDHHMMSSDMZ = len("YYYYMMDDHHMMSSDMZ") LEN_YYYYMMDDHHMMSSZ = len("YYYYMMDDHHMMSSZ") -def fractions2float(fractions_raw): - pureint(fractions_raw) - return float("0." + fractions_raw) - - class VisibleString(CommonString): __slots__ = () tag_default = tag_encode(26) @@ -4154,7 +4157,11 @@ class VisibleString(CommonString): asn1_type_name = "VisibleString" -UTCTimeState = namedtuple("UTCTimeState", OctetStringState._fields + ("ber_raw",)) +UTCTimeState = namedtuple( + "UTCTimeState", + OctetStringState._fields + ("ber_raw",), + **NAMEDTUPLE_KWARGS +) class UTCTime(VisibleString): @@ -4477,8 +4484,7 @@ class GeneralizedTime(UTCTime): break if len(value) == 0: return offset, decoded - decimal_signs = ".," - if value[0] in decimal_signs: + if value[0] in DECIMAL_SIGNS: return offset, ( decoded + timedelta(seconds=3600 * fractions2float(value[1:])) ) @@ -4488,7 +4494,7 @@ class GeneralizedTime(UTCTime): value = value[2:] if len(value) == 0: return offset, decoded - if value[0] in decimal_signs: + if value[0] in DECIMAL_SIGNS: return offset, ( decoded + timedelta(seconds=60 * fractions2float(value[1:])) ) @@ -4498,7 +4504,7 @@ class GeneralizedTime(UTCTime): value = value[2:] if len(value) == 0: return offset, decoded - if value[0] not in decimal_signs: + if value[0] not in DECIMAL_SIGNS: raise ValueError("invalid format after seconds") return offset, ( decoded + timedelta(microseconds=10**6 * fractions2float(value[1:])) @@ -4598,7 +4604,7 @@ ChoiceState = namedtuple("ChoiceState", ( "expl_lenindef", "lenindef", "ber_encoded", -)) +), **NAMEDTUPLE_KWARGS) class Choice(Obj): @@ -4916,7 +4922,7 @@ AnyState = namedtuple("AnyState", ( "lenindef", "ber_encoded", "defined", -)) +), **NAMEDTUPLE_KWARGS) class Any(Obj): @@ -5195,7 +5201,7 @@ SequenceState = namedtuple("SequenceState", ( "expl_lenindef", "lenindef", "ber_encoded", -)) +), **NAMEDTUPLE_KWARGS) class Sequence(Obj): @@ -5860,7 +5866,7 @@ SequenceOfState = namedtuple("SequenceOfState", ( "expl_lenindef", "lenindef", "ber_encoded", -)) +), **NAMEDTUPLE_KWARGS) class SequenceOf(Obj):