]> Cypherpunks.ru repositories - pyderasn.git/commitdiff
BitString ''H notation support
authorSergey Matveev <stargrave@stargrave.org>
Sun, 20 May 2018 09:34:15 +0000 (12:34 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 20 May 2018 09:34:15 +0000 (12:34 +0300)
pyderasn.py
tests/test_pyderasn.py

index 0986475e85766d203da55f6e09a6720bed12e66b..b3ee318ae7f03cecbe120f8173e4de1a580e8eb6 100755 (executable)
@@ -1831,6 +1831,8 @@ class BitString(Obj):
     >>> b.bit_len
     88
 
+    >>> BitString("'0A3B5F291CD'H")
+    BIT STRING 44 bits 0a3b5f291cd0
     >>> b = BitString("'010110000000'B")
     BIT STRING 12 bits 5800
     >>> b.bit_len
@@ -1919,21 +1921,25 @@ class BitString(Obj):
         if isinstance(value, (string_types, binary_type)):
             if (
                     isinstance(value, string_types) and
-                    value.startswith("'") and
-                    value.endswith("'B")
+                    value.startswith("'")
             ):
-                value = value[1:-2]
-                if not set(value) <= set(("0", "1")):
-                    raise ValueError("B's coding contains unacceptable chars")
-                return self._bits2octets(value)
+                if value.endswith("'B"):
+                    value = value[1:-2]
+                    if not set(value) <= set(("0", "1")):
+                        raise ValueError("B's coding contains unacceptable chars")
+                    return self._bits2octets(value)
+                elif value.endswith("'H"):
+                    value = value[1:-2]
+                    return (
+                        len(value) * 4,
+                        hexdec(value + ("" if len(value) % 2 == 0 else "0")),
+                    )
+                else:
+                    raise InvalidValueType((self.__class__, string_types, binary_type))
             elif isinstance(value, binary_type):
                 return (len(value) * 8, value)
             else:
-                raise InvalidValueType((
-                    self.__class__,
-                    string_types,
-                    binary_type,
-                ))
+                raise InvalidValueType((self.__class__, string_types, binary_type))
         if isinstance(value, tuple):
             if (
                     len(value) == 2 and
index 62954d78762a162f901f33a649b957b0c8063e6b..a07c495ebd5d838219b6b2be69cc011911f734cf 100644 (file)
@@ -1519,8 +1519,7 @@ class TestBitString(CommonMixin, TestCase):
             self.assertEqual(len(encoded), obj.tlvlen)
 
     def test_x690_vector(self):
-        vector_payload = hexdec("0A3B5F291CD0")
-        vector = BitString((len(vector_payload) * 8 - 4, vector_payload))
+        vector = BitString("'0A3B5F291CD'H")
         obj, tail = BitString().decode(hexdec("0307040A3B5F291CD0"))
         self.assertSequenceEqual(tail, b"")
         self.assertEqual(obj, vector)