Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

encoder.py 8.4KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  5. # License: https://pyasn1.readthedocs.io/en/latest/license.html
  6. #
  7. from collections import OrderedDict
  8. from pyasn1 import debug
  9. from pyasn1 import error
  10. from pyasn1.compat import _MISSING
  11. from pyasn1.type import base
  12. from pyasn1.type import char
  13. from pyasn1.type import tag
  14. from pyasn1.type import univ
  15. from pyasn1.type import useful
  16. __all__ = ['encode']
  17. LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_ENCODER)
  18. class AbstractItemEncoder(object):
  19. def encode(self, value, encodeFun, **options):
  20. raise error.PyAsn1Error('Not implemented')
  21. class BooleanEncoder(AbstractItemEncoder):
  22. def encode(self, value, encodeFun, **options):
  23. return bool(value)
  24. class IntegerEncoder(AbstractItemEncoder):
  25. def encode(self, value, encodeFun, **options):
  26. return int(value)
  27. class BitStringEncoder(AbstractItemEncoder):
  28. def encode(self, value, encodeFun, **options):
  29. return str(value)
  30. class OctetStringEncoder(AbstractItemEncoder):
  31. def encode(self, value, encodeFun, **options):
  32. return value.asOctets()
  33. class TextStringEncoder(AbstractItemEncoder):
  34. def encode(self, value, encodeFun, **options):
  35. return str(value)
  36. class NullEncoder(AbstractItemEncoder):
  37. def encode(self, value, encodeFun, **options):
  38. return None
  39. class ObjectIdentifierEncoder(AbstractItemEncoder):
  40. def encode(self, value, encodeFun, **options):
  41. return str(value)
  42. class RealEncoder(AbstractItemEncoder):
  43. def encode(self, value, encodeFun, **options):
  44. return float(value)
  45. class SetEncoder(AbstractItemEncoder):
  46. protoDict = dict
  47. def encode(self, value, encodeFun, **options):
  48. inconsistency = value.isInconsistent
  49. if inconsistency:
  50. raise inconsistency
  51. namedTypes = value.componentType
  52. substrate = self.protoDict()
  53. for idx, (key, subValue) in enumerate(value.items()):
  54. if namedTypes and namedTypes[idx].isOptional and not value[idx].isValue:
  55. continue
  56. substrate[key] = encodeFun(subValue, **options)
  57. return substrate
  58. class SequenceEncoder(SetEncoder):
  59. protoDict = OrderedDict
  60. class SequenceOfEncoder(AbstractItemEncoder):
  61. def encode(self, value, encodeFun, **options):
  62. inconsistency = value.isInconsistent
  63. if inconsistency:
  64. raise inconsistency
  65. return [encodeFun(x, **options) for x in value]
  66. class ChoiceEncoder(SequenceEncoder):
  67. pass
  68. class AnyEncoder(AbstractItemEncoder):
  69. def encode(self, value, encodeFun, **options):
  70. return value.asOctets()
  71. TAG_MAP = {
  72. univ.Boolean.tagSet: BooleanEncoder(),
  73. univ.Integer.tagSet: IntegerEncoder(),
  74. univ.BitString.tagSet: BitStringEncoder(),
  75. univ.OctetString.tagSet: OctetStringEncoder(),
  76. univ.Null.tagSet: NullEncoder(),
  77. univ.ObjectIdentifier.tagSet: ObjectIdentifierEncoder(),
  78. univ.Enumerated.tagSet: IntegerEncoder(),
  79. univ.Real.tagSet: RealEncoder(),
  80. # Sequence & Set have same tags as SequenceOf & SetOf
  81. univ.SequenceOf.tagSet: SequenceOfEncoder(),
  82. univ.SetOf.tagSet: SequenceOfEncoder(),
  83. univ.Choice.tagSet: ChoiceEncoder(),
  84. # character string types
  85. char.UTF8String.tagSet: TextStringEncoder(),
  86. char.NumericString.tagSet: TextStringEncoder(),
  87. char.PrintableString.tagSet: TextStringEncoder(),
  88. char.TeletexString.tagSet: TextStringEncoder(),
  89. char.VideotexString.tagSet: TextStringEncoder(),
  90. char.IA5String.tagSet: TextStringEncoder(),
  91. char.GraphicString.tagSet: TextStringEncoder(),
  92. char.VisibleString.tagSet: TextStringEncoder(),
  93. char.GeneralString.tagSet: TextStringEncoder(),
  94. char.UniversalString.tagSet: TextStringEncoder(),
  95. char.BMPString.tagSet: TextStringEncoder(),
  96. # useful types
  97. useful.ObjectDescriptor.tagSet: OctetStringEncoder(),
  98. useful.GeneralizedTime.tagSet: OctetStringEncoder(),
  99. useful.UTCTime.tagSet: OctetStringEncoder()
  100. }
  101. # Put in ambiguous & non-ambiguous types for faster codec lookup
  102. TYPE_MAP = {
  103. univ.Boolean.typeId: BooleanEncoder(),
  104. univ.Integer.typeId: IntegerEncoder(),
  105. univ.BitString.typeId: BitStringEncoder(),
  106. univ.OctetString.typeId: OctetStringEncoder(),
  107. univ.Null.typeId: NullEncoder(),
  108. univ.ObjectIdentifier.typeId: ObjectIdentifierEncoder(),
  109. univ.Enumerated.typeId: IntegerEncoder(),
  110. univ.Real.typeId: RealEncoder(),
  111. # Sequence & Set have same tags as SequenceOf & SetOf
  112. univ.Set.typeId: SetEncoder(),
  113. univ.SetOf.typeId: SequenceOfEncoder(),
  114. univ.Sequence.typeId: SequenceEncoder(),
  115. univ.SequenceOf.typeId: SequenceOfEncoder(),
  116. univ.Choice.typeId: ChoiceEncoder(),
  117. univ.Any.typeId: AnyEncoder(),
  118. # character string types
  119. char.UTF8String.typeId: OctetStringEncoder(),
  120. char.NumericString.typeId: OctetStringEncoder(),
  121. char.PrintableString.typeId: OctetStringEncoder(),
  122. char.TeletexString.typeId: OctetStringEncoder(),
  123. char.VideotexString.typeId: OctetStringEncoder(),
  124. char.IA5String.typeId: OctetStringEncoder(),
  125. char.GraphicString.typeId: OctetStringEncoder(),
  126. char.VisibleString.typeId: OctetStringEncoder(),
  127. char.GeneralString.typeId: OctetStringEncoder(),
  128. char.UniversalString.typeId: OctetStringEncoder(),
  129. char.BMPString.typeId: OctetStringEncoder(),
  130. # useful types
  131. useful.ObjectDescriptor.typeId: OctetStringEncoder(),
  132. useful.GeneralizedTime.typeId: OctetStringEncoder(),
  133. useful.UTCTime.typeId: OctetStringEncoder()
  134. }
  135. # deprecated aliases, https://github.com/pyasn1/pyasn1/issues/9
  136. tagMap = TAG_MAP
  137. typeMap = TYPE_MAP
  138. class SingleItemEncoder(object):
  139. TAG_MAP = TAG_MAP
  140. TYPE_MAP = TYPE_MAP
  141. def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored):
  142. self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP
  143. self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP
  144. def __call__(self, value, **options):
  145. if not isinstance(value, base.Asn1Item):
  146. raise error.PyAsn1Error(
  147. 'value is not valid (should be an instance of an ASN.1 Item)')
  148. if LOG:
  149. debug.scope.push(type(value).__name__)
  150. LOG('encoder called for type %s '
  151. '<%s>' % (type(value).__name__, value.prettyPrint()))
  152. tagSet = value.tagSet
  153. try:
  154. concreteEncoder = self._typeMap[value.typeId]
  155. except KeyError:
  156. # use base type for codec lookup to recover untagged types
  157. baseTagSet = tag.TagSet(
  158. value.tagSet.baseTag, value.tagSet.baseTag)
  159. try:
  160. concreteEncoder = self._tagMap[baseTagSet]
  161. except KeyError:
  162. raise error.PyAsn1Error('No encoder for %s' % (value,))
  163. if LOG:
  164. LOG('using value codec %s chosen by '
  165. '%s' % (concreteEncoder.__class__.__name__, tagSet))
  166. pyObject = concreteEncoder.encode(value, self, **options)
  167. if LOG:
  168. LOG('encoder %s produced: '
  169. '%s' % (type(concreteEncoder).__name__, repr(pyObject)))
  170. debug.scope.pop()
  171. return pyObject
  172. class Encoder(object):
  173. SINGLE_ITEM_ENCODER = SingleItemEncoder
  174. def __init__(self, **options):
  175. self._singleItemEncoder = self.SINGLE_ITEM_ENCODER(**options)
  176. def __call__(self, pyObject, asn1Spec=None, **options):
  177. return self._singleItemEncoder(
  178. pyObject, asn1Spec=asn1Spec, **options)
  179. #: Turns ASN.1 object into a Python built-in type object(s).
  180. #:
  181. #: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  182. #: walks all its components recursively and produces a Python built-in type or a tree
  183. #: of those.
  184. #:
  185. #: One exception is that instead of :py:class:`dict`, the :py:class:`OrderedDict`
  186. #: is used to preserve ordering of the components in ASN.1 SEQUENCE.
  187. #:
  188. #: Parameters
  189. #: ----------
  190. # asn1Value: any pyasn1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  191. #: pyasn1 object to encode (or a tree of them)
  192. #:
  193. #: Returns
  194. #: -------
  195. #: : :py:class:`object`
  196. #: Python built-in type instance (or a tree of them)
  197. #:
  198. #: Raises
  199. #: ------
  200. #: ~pyasn1.error.PyAsn1Error
  201. #: On encoding errors
  202. #:
  203. #: Examples
  204. #: --------
  205. #: Encode ASN.1 value object into native Python types
  206. #:
  207. #: .. code-block:: pycon
  208. #:
  209. #: >>> seq = SequenceOf(componentType=Integer())
  210. #: >>> seq.extend([1, 2, 3])
  211. #: >>> encode(seq)
  212. #: [1, 2, 3]
  213. #:
  214. encode = SingleItemEncoder()