X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pyderasn.py;h=d637053f53f88e6c2aa72a2014d4b6066f41e201;hb=1280a3daf22524df59b7b0379b1d00b54a77f03a;hp=6c14e4619972a8a31e68fdafd8cefbd48f344659;hpb=7af6ff46e2927030706fc4c0df0ddeadfa55dd79;p=pyderasn.git diff --git a/pyderasn.py b/pyderasn.py index 6c14e46..d637053 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -765,6 +765,19 @@ 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) ######################################################################## @@ -2338,7 +2351,6 @@ class Integer(Obj): yield pp -SET01 = frozenset(("0", "1")) BitStringState = namedtuple("BitStringState", ( "version", "specs", @@ -3427,13 +3439,6 @@ ObjectIdentifierState = namedtuple("ObjectIdentifierState", ( ), **NAMEDTUPLE_KWARGS) -def pureint(value): - i = int(value) - if (value[0] in "+- ") or (value[-1] == " "): - raise ValueError("non-pure integer") - return i - - class ObjectIdentifier(Obj): """``OBJECT IDENTIFIER`` OID type @@ -3818,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 @@ -4145,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) @@ -4484,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:])) ) @@ -4495,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:])) ) @@ -4505,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:]))