summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelie <elie>2011-10-10 19:38:58 +0000
committerelie <elie>2011-10-10 19:38:58 +0000
commit40ce65c556156f121cdf1444fa202e007a45197a (patch)
tree2048ac70777abf42cd9b1d2f469a0addf826f9dd
parentd4a4f91da8127a76a6a69cb38ee7cf0f3d1f4b43 (diff)
downloadpyasn1-40ce65c556156f121cdf1444fa202e007a45197a.tar.gz
binary&hex OctetString syntax parser reworked, test case added
-rw-r--r--pyasn1/type/univ.py96
-rw-r--r--test/type/univ.py13
2 files changed, 49 insertions, 60 deletions
diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py
index 6604b83..8aeed84 100644
--- a/pyasn1/type/univ.py
+++ b/pyasn1/type/univ.py
@@ -2,6 +2,7 @@
import operator, sys
from pyasn1.type import base, tag, constraint, namedtype, namedval, tagmap
from pyasn1.codec.ber import eoo
+from pyasn1.compat import octets
from pyasn1 import error
# "Simple" ASN.1 types (yet incomplete)
@@ -258,23 +259,20 @@ class OctetString(base.AbstractSimpleAsn1Item):
tagSet = baseTagSet = tag.initTagSet(
tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x04)
)
+ def __init__(self, value=None, tagSet=None, subtypeSpec=None,
+ binValue=None, hexValue=None):
+ if binValue is not None:
+ value = self.fromBinaryString(binValue)
+ if hexValue is not None:
+ value = self.fromHexString(hexValue)
+ base.AbstractSimpleAsn1Item.__init__(self, value, tagSet, subtypeSpec)
+
if sys.version_info[0] <= 2:
def prettyIn(self, value):
if isinstance(value, str):
return value
else:
return str(value)
- def purePrettyIn(self, value):
- if not isinstance(value, str):
- return str(value)
- elif not value:
- return value
- else:
- r = self.__parseBinHex(value)
- if r is None:
- return value
- else:
- return ''.join([chr(x) for x in r])
else:
def prettyIn(self, value):
if isinstance(value, bytes):
@@ -283,59 +281,39 @@ class OctetString(base.AbstractSimpleAsn1Item):
return bytes(value)
else:
return str(value).encode()
- def purePrettyIn(self, value):
- if isinstance(value, bytes):
- return value
- elif isinstance(value, OctetString):
- return bytes(value)
- elif not value:
- return value.encode()
+
+ def fromBinaryString(self, value):
+ bitNo = 8; byte = 0; r = ()
+ for v in value:
+ if bitNo:
+ bitNo = bitNo - 1
else:
- value = str(value)
- r = self.__parseBinHex(str(value))
- if r is None:
- return value.encode()
- else:
- return bytes(r)
-
- def __parseBinHex(value):
- if value[0] == '\'':
- r = ()
- if value[-2:] == '\'B':
- bitNo = 8; byte = 0
- for v in value[1:-2]:
- if bitNo:
- bitNo = bitNo - 1
- else:
- bitNo = 7
- r = r + (byte,)
- byte = 0
- if v == '0':
- v = 0
- elif v == '1':
- v = 1
- else:
- raise error.PyAsn1Error(
- 'Non-binary OCTET STRING initializer %s' % (v,)
- )
- byte = byte | (v << bitNo)
+ bitNo = 7
r = r + (byte,)
- elif value[-2:] == '\'H':
- p = ()
- for v in value[1:-2]:
- if p:
- r = r + (int(p+v, 16),)
- p = ()
- else:
- p = v
- if p:
- r = r + (int(p+'0', 16),)
+ byte = 0
+ if v == '0':
+ v = 0
+ elif v == '1':
+ v = 1
else:
raise error.PyAsn1Error(
- 'Bad OCTET STRING value notation %s' % value
+ 'Non-binary OCTET STRING initializer %s' % (v,)
)
- return r
-
+ byte = byte | (v << bitNo)
+ return octets.ints2octs(r + (byte,))
+
+ def fromHexString(self, value):
+ r = p = ()
+ for v in value:
+ if p:
+ r = r + (int(p+v, 16),)
+ p = ()
+ else:
+ p = v
+ if p:
+ r = r + (int(p+'0', 16),)
+ return octets.ints2octs(r)
+
def prettyOut(self, value): return repr(value)
if sys.version_info[0] <= 2:
diff --git a/test/type/univ.py b/test/type/univ.py
index bf7de6a..111935a 100644
--- a/test/type/univ.py
+++ b/test/type/univ.py
@@ -1,5 +1,6 @@
+import sys
from pyasn1.type import univ, tag, constraint, namedtype, namedval, error
-from pyasn1.compat.octets import str2octs
+from pyasn1.compat.octets import str2octs, ints2octs
from pyasn1.error import PyAsn1Error
try:
import unittest
@@ -99,6 +100,16 @@ class BitStringTestCase(unittest.TestCase):
assert self.b.clone("'A98A'H")[2] == 1
class OctetStringTestCase(unittest.TestCase):
+ def testInit(self):
+ assert univ.OctetString(str2octs('abcd')) == str2octs('abcd'), '__init__() fails'
+ def testBinStr(self):
+ assert univ.OctetString(binValue="1000010111101110101111000000111011") == ints2octs((133, 238, 188, 14, 192)), 'bin init fails'
+ def testHexStr(self):
+ assert univ.OctetString(hexValue="FA9823C43E43510DE3422") == ints2octs((250, 152, 35, 196, 62, 67, 81, 13, 227, 66, 32)), 'hex init fails'
+ if sys.version_info[0] > 2:
+ def testBytes(self):
+ assert univ.OctetString(b'abcd') == b'abcd','testBytes() fails'
+
def testStr(self):
assert str(univ.OctetString('q')) == 'q', '__str__() fails'
def testSeq(self):