Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

char.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import sys
  8. from pyasn1 import error
  9. from pyasn1.type import tag
  10. from pyasn1.type import univ
  11. __all__ = ['NumericString', 'PrintableString', 'TeletexString', 'T61String', 'VideotexString',
  12. 'IA5String', 'GraphicString', 'VisibleString', 'ISO646String',
  13. 'GeneralString', 'UniversalString', 'BMPString', 'UTF8String']
  14. NoValue = univ.NoValue
  15. noValue = univ.noValue
  16. class AbstractCharacterString(univ.OctetString):
  17. """Creates |ASN.1| schema or value object.
  18. |ASN.1| objects are immutable and duck-type Python 2 :class:`unicode` or Python 3 :class:`str`.
  19. When used in octet-stream context, |ASN.1| type assumes "|encoding|" encoding.
  20. Keyword Args
  21. ------------
  22. value: :class:`unicode`, :class:`str`, :class:`bytes` or |ASN.1| object
  23. unicode object (Python 2) or string (Python 3), alternatively string
  24. (Python 2) or bytes (Python 3) representing octet-stream of serialised
  25. unicode string (note `encoding` parameter) or |ASN.1| class instance.
  26. tagSet: :py:class:`~pyasn1.type.tag.TagSet`
  27. Object representing non-default ASN.1 tag(s)
  28. subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
  29. Object representing non-default ASN.1 subtype constraint(s)
  30. encoding: :py:class:`str`
  31. Unicode codec ID to encode/decode :class:`unicode` (Python 2) or
  32. :class:`str` (Python 3) the payload when |ASN.1| object is used
  33. in octet-stream context.
  34. Raises
  35. ------
  36. :py:class:`~pyasn1.error.PyAsn1Error`
  37. On constraint violation or bad initializer.
  38. """
  39. if sys.version_info[0] <= 2:
  40. def __str__(self):
  41. try:
  42. # `str` is Py2 text representation
  43. return self._value.encode(self.encoding)
  44. except UnicodeEncodeError:
  45. raise error.PyAsn1Error(
  46. "Can't encode string '%s' with codec %s" % (self._value, self.encoding)
  47. )
  48. def __unicode__(self):
  49. return unicode(self._value)
  50. def prettyIn(self, value):
  51. try:
  52. if isinstance(value, unicode):
  53. return value
  54. elif isinstance(value, str):
  55. return value.decode(self.encoding)
  56. elif isinstance(value, (tuple, list)):
  57. return self.prettyIn(''.join([chr(x) for x in value]))
  58. elif isinstance(value, univ.OctetString):
  59. return value.asOctets().decode(self.encoding)
  60. else:
  61. return unicode(value)
  62. except (UnicodeDecodeError, LookupError):
  63. raise error.PyAsn1Error(
  64. "Can't decode string '%s' with codec %s" % (value, self.encoding)
  65. )
  66. def asOctets(self, padding=True):
  67. return str(self)
  68. def asNumbers(self, padding=True):
  69. return tuple([ord(x) for x in str(self)])
  70. else:
  71. def __str__(self):
  72. # `unicode` is Py3 text representation
  73. return str(self._value)
  74. def __bytes__(self):
  75. try:
  76. return self._value.encode(self.encoding)
  77. except UnicodeEncodeError:
  78. raise error.PyAsn1Error(
  79. "Can't encode string '%s' with codec %s" % (self._value, self.encoding)
  80. )
  81. def prettyIn(self, value):
  82. try:
  83. if isinstance(value, str):
  84. return value
  85. elif isinstance(value, bytes):
  86. return value.decode(self.encoding)
  87. elif isinstance(value, (tuple, list)):
  88. return self.prettyIn(bytes(value))
  89. elif isinstance(value, univ.OctetString):
  90. return value.asOctets().decode(self.encoding)
  91. else:
  92. return str(value)
  93. except (UnicodeDecodeError, LookupError):
  94. raise error.PyAsn1Error(
  95. "Can't decode string '%s' with codec %s" % (value, self.encoding)
  96. )
  97. def asOctets(self, padding=True):
  98. return bytes(self)
  99. def asNumbers(self, padding=True):
  100. return tuple(bytes(self))
  101. #
  102. # See OctetString.prettyPrint() for the explanation
  103. #
  104. def prettyOut(self, value):
  105. return value
  106. def prettyPrint(self, scope=0):
  107. # first see if subclass has its own .prettyOut()
  108. value = self.prettyOut(self._value)
  109. if value is not self._value:
  110. return value
  111. return AbstractCharacterString.__str__(self)
  112. def __reversed__(self):
  113. return reversed(self._value)
  114. class NumericString(AbstractCharacterString):
  115. __doc__ = AbstractCharacterString.__doc__
  116. #: Set (on class, not on instance) or return a
  117. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  118. #: associated with |ASN.1| type.
  119. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  120. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 18)
  121. )
  122. encoding = 'us-ascii'
  123. # Optimization for faster codec lookup
  124. typeId = AbstractCharacterString.getTypeId()
  125. class PrintableString(AbstractCharacterString):
  126. __doc__ = AbstractCharacterString.__doc__
  127. #: Set (on class, not on instance) or return a
  128. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  129. #: associated with |ASN.1| type.
  130. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  131. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 19)
  132. )
  133. encoding = 'us-ascii'
  134. # Optimization for faster codec lookup
  135. typeId = AbstractCharacterString.getTypeId()
  136. class TeletexString(AbstractCharacterString):
  137. __doc__ = AbstractCharacterString.__doc__
  138. #: Set (on class, not on instance) or return a
  139. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  140. #: associated with |ASN.1| type.
  141. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  142. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 20)
  143. )
  144. encoding = 'iso-8859-1'
  145. # Optimization for faster codec lookup
  146. typeId = AbstractCharacterString.getTypeId()
  147. class T61String(TeletexString):
  148. __doc__ = TeletexString.__doc__
  149. # Optimization for faster codec lookup
  150. typeId = AbstractCharacterString.getTypeId()
  151. class VideotexString(AbstractCharacterString):
  152. __doc__ = AbstractCharacterString.__doc__
  153. #: Set (on class, not on instance) or return a
  154. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  155. #: associated with |ASN.1| type.
  156. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  157. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 21)
  158. )
  159. encoding = 'iso-8859-1'
  160. # Optimization for faster codec lookup
  161. typeId = AbstractCharacterString.getTypeId()
  162. class IA5String(AbstractCharacterString):
  163. __doc__ = AbstractCharacterString.__doc__
  164. #: Set (on class, not on instance) or return a
  165. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  166. #: associated with |ASN.1| type.
  167. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  168. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 22)
  169. )
  170. encoding = 'us-ascii'
  171. # Optimization for faster codec lookup
  172. typeId = AbstractCharacterString.getTypeId()
  173. class GraphicString(AbstractCharacterString):
  174. __doc__ = AbstractCharacterString.__doc__
  175. #: Set (on class, not on instance) or return a
  176. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  177. #: associated with |ASN.1| type.
  178. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  179. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 25)
  180. )
  181. encoding = 'iso-8859-1'
  182. # Optimization for faster codec lookup
  183. typeId = AbstractCharacterString.getTypeId()
  184. class VisibleString(AbstractCharacterString):
  185. __doc__ = AbstractCharacterString.__doc__
  186. #: Set (on class, not on instance) or return a
  187. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  188. #: associated with |ASN.1| type.
  189. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  190. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 26)
  191. )
  192. encoding = 'us-ascii'
  193. # Optimization for faster codec lookup
  194. typeId = AbstractCharacterString.getTypeId()
  195. class ISO646String(VisibleString):
  196. __doc__ = VisibleString.__doc__
  197. # Optimization for faster codec lookup
  198. typeId = AbstractCharacterString.getTypeId()
  199. class GeneralString(AbstractCharacterString):
  200. __doc__ = AbstractCharacterString.__doc__
  201. #: Set (on class, not on instance) or return a
  202. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  203. #: associated with |ASN.1| type.
  204. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  205. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 27)
  206. )
  207. encoding = 'iso-8859-1'
  208. # Optimization for faster codec lookup
  209. typeId = AbstractCharacterString.getTypeId()
  210. class UniversalString(AbstractCharacterString):
  211. __doc__ = AbstractCharacterString.__doc__
  212. #: Set (on class, not on instance) or return a
  213. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  214. #: associated with |ASN.1| type.
  215. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  216. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 28)
  217. )
  218. encoding = "utf-32-be"
  219. # Optimization for faster codec lookup
  220. typeId = AbstractCharacterString.getTypeId()
  221. class BMPString(AbstractCharacterString):
  222. __doc__ = AbstractCharacterString.__doc__
  223. #: Set (on class, not on instance) or return a
  224. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  225. #: associated with |ASN.1| type.
  226. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  227. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 30)
  228. )
  229. encoding = "utf-16-be"
  230. # Optimization for faster codec lookup
  231. typeId = AbstractCharacterString.getTypeId()
  232. class UTF8String(AbstractCharacterString):
  233. __doc__ = AbstractCharacterString.__doc__
  234. #: Set (on class, not on instance) or return a
  235. #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
  236. #: associated with |ASN.1| type.
  237. tagSet = AbstractCharacterString.tagSet.tagImplicitly(
  238. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 12)
  239. )
  240. encoding = "utf-8"
  241. # Optimization for faster codec lookup
  242. typeId = AbstractCharacterString.getTypeId()