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.

decoder.py 8.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 pyasn1 import debug
  8. from pyasn1 import error
  9. from pyasn1.compat import _MISSING
  10. from pyasn1.type import base
  11. from pyasn1.type import char
  12. from pyasn1.type import tag
  13. from pyasn1.type import univ
  14. from pyasn1.type import useful
  15. __all__ = ['decode']
  16. LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_DECODER)
  17. class AbstractScalarPayloadDecoder(object):
  18. def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
  19. return asn1Spec.clone(pyObject)
  20. class BitStringPayloadDecoder(AbstractScalarPayloadDecoder):
  21. def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
  22. return asn1Spec.clone(univ.BitString.fromBinaryString(pyObject))
  23. class SequenceOrSetPayloadDecoder(object):
  24. def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
  25. asn1Value = asn1Spec.clone()
  26. componentsTypes = asn1Spec.componentType
  27. for field in asn1Value:
  28. if field in pyObject:
  29. asn1Value[field] = decodeFun(pyObject[field], componentsTypes[field].asn1Object, **options)
  30. return asn1Value
  31. class SequenceOfOrSetOfPayloadDecoder(object):
  32. def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
  33. asn1Value = asn1Spec.clone()
  34. for pyValue in pyObject:
  35. asn1Value.append(decodeFun(pyValue, asn1Spec.componentType), **options)
  36. return asn1Value
  37. class ChoicePayloadDecoder(object):
  38. def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
  39. asn1Value = asn1Spec.clone()
  40. componentsTypes = asn1Spec.componentType
  41. for field in pyObject:
  42. if field in componentsTypes:
  43. asn1Value[field] = decodeFun(pyObject[field], componentsTypes[field].asn1Object, **options)
  44. break
  45. return asn1Value
  46. TAG_MAP = {
  47. univ.Integer.tagSet: AbstractScalarPayloadDecoder(),
  48. univ.Boolean.tagSet: AbstractScalarPayloadDecoder(),
  49. univ.BitString.tagSet: BitStringPayloadDecoder(),
  50. univ.OctetString.tagSet: AbstractScalarPayloadDecoder(),
  51. univ.Null.tagSet: AbstractScalarPayloadDecoder(),
  52. univ.ObjectIdentifier.tagSet: AbstractScalarPayloadDecoder(),
  53. univ.Enumerated.tagSet: AbstractScalarPayloadDecoder(),
  54. univ.Real.tagSet: AbstractScalarPayloadDecoder(),
  55. univ.Sequence.tagSet: SequenceOrSetPayloadDecoder(), # conflicts with SequenceOf
  56. univ.Set.tagSet: SequenceOrSetPayloadDecoder(), # conflicts with SetOf
  57. univ.Choice.tagSet: ChoicePayloadDecoder(), # conflicts with Any
  58. # character string types
  59. char.UTF8String.tagSet: AbstractScalarPayloadDecoder(),
  60. char.NumericString.tagSet: AbstractScalarPayloadDecoder(),
  61. char.PrintableString.tagSet: AbstractScalarPayloadDecoder(),
  62. char.TeletexString.tagSet: AbstractScalarPayloadDecoder(),
  63. char.VideotexString.tagSet: AbstractScalarPayloadDecoder(),
  64. char.IA5String.tagSet: AbstractScalarPayloadDecoder(),
  65. char.GraphicString.tagSet: AbstractScalarPayloadDecoder(),
  66. char.VisibleString.tagSet: AbstractScalarPayloadDecoder(),
  67. char.GeneralString.tagSet: AbstractScalarPayloadDecoder(),
  68. char.UniversalString.tagSet: AbstractScalarPayloadDecoder(),
  69. char.BMPString.tagSet: AbstractScalarPayloadDecoder(),
  70. # useful types
  71. useful.ObjectDescriptor.tagSet: AbstractScalarPayloadDecoder(),
  72. useful.GeneralizedTime.tagSet: AbstractScalarPayloadDecoder(),
  73. useful.UTCTime.tagSet: AbstractScalarPayloadDecoder()
  74. }
  75. # Put in ambiguous & non-ambiguous types for faster codec lookup
  76. TYPE_MAP = {
  77. univ.Integer.typeId: AbstractScalarPayloadDecoder(),
  78. univ.Boolean.typeId: AbstractScalarPayloadDecoder(),
  79. univ.BitString.typeId: BitStringPayloadDecoder(),
  80. univ.OctetString.typeId: AbstractScalarPayloadDecoder(),
  81. univ.Null.typeId: AbstractScalarPayloadDecoder(),
  82. univ.ObjectIdentifier.typeId: AbstractScalarPayloadDecoder(),
  83. univ.Enumerated.typeId: AbstractScalarPayloadDecoder(),
  84. univ.Real.typeId: AbstractScalarPayloadDecoder(),
  85. # ambiguous base types
  86. univ.Set.typeId: SequenceOrSetPayloadDecoder(),
  87. univ.SetOf.typeId: SequenceOfOrSetOfPayloadDecoder(),
  88. univ.Sequence.typeId: SequenceOrSetPayloadDecoder(),
  89. univ.SequenceOf.typeId: SequenceOfOrSetOfPayloadDecoder(),
  90. univ.Choice.typeId: ChoicePayloadDecoder(),
  91. univ.Any.typeId: AbstractScalarPayloadDecoder(),
  92. # character string types
  93. char.UTF8String.typeId: AbstractScalarPayloadDecoder(),
  94. char.NumericString.typeId: AbstractScalarPayloadDecoder(),
  95. char.PrintableString.typeId: AbstractScalarPayloadDecoder(),
  96. char.TeletexString.typeId: AbstractScalarPayloadDecoder(),
  97. char.VideotexString.typeId: AbstractScalarPayloadDecoder(),
  98. char.IA5String.typeId: AbstractScalarPayloadDecoder(),
  99. char.GraphicString.typeId: AbstractScalarPayloadDecoder(),
  100. char.VisibleString.typeId: AbstractScalarPayloadDecoder(),
  101. char.GeneralString.typeId: AbstractScalarPayloadDecoder(),
  102. char.UniversalString.typeId: AbstractScalarPayloadDecoder(),
  103. char.BMPString.typeId: AbstractScalarPayloadDecoder(),
  104. # useful types
  105. useful.ObjectDescriptor.typeId: AbstractScalarPayloadDecoder(),
  106. useful.GeneralizedTime.typeId: AbstractScalarPayloadDecoder(),
  107. useful.UTCTime.typeId: AbstractScalarPayloadDecoder()
  108. }
  109. # deprecated aliases, https://github.com/pyasn1/pyasn1/issues/9
  110. tagMap = TAG_MAP
  111. typeMap = TYPE_MAP
  112. class SingleItemDecoder(object):
  113. TAG_MAP = TAG_MAP
  114. TYPE_MAP = TYPE_MAP
  115. def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored):
  116. self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP
  117. self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP
  118. def __call__(self, pyObject, asn1Spec, **options):
  119. if LOG:
  120. debug.scope.push(type(pyObject).__name__)
  121. LOG('decoder called at scope %s, working with '
  122. 'type %s' % (debug.scope, type(pyObject).__name__))
  123. if asn1Spec is None or not isinstance(asn1Spec, base.Asn1Item):
  124. raise error.PyAsn1Error(
  125. 'asn1Spec is not valid (should be an instance of an ASN.1 '
  126. 'Item, not %s)' % asn1Spec.__class__.__name__)
  127. try:
  128. valueDecoder = self._typeMap[asn1Spec.typeId]
  129. except KeyError:
  130. # use base type for codec lookup to recover untagged types
  131. baseTagSet = tag.TagSet(asn1Spec.tagSet.baseTag, asn1Spec.tagSet.baseTag)
  132. try:
  133. valueDecoder = self._tagMap[baseTagSet]
  134. except KeyError:
  135. raise error.PyAsn1Error('Unknown ASN.1 tag %s' % asn1Spec.tagSet)
  136. if LOG:
  137. LOG('calling decoder %s on Python type %s '
  138. '<%s>' % (type(valueDecoder).__name__,
  139. type(pyObject).__name__, repr(pyObject)))
  140. value = valueDecoder(pyObject, asn1Spec, self, **options)
  141. if LOG:
  142. LOG('decoder %s produced ASN.1 type %s '
  143. '<%s>' % (type(valueDecoder).__name__,
  144. type(value).__name__, repr(value)))
  145. debug.scope.pop()
  146. return value
  147. class Decoder(object):
  148. SINGLE_ITEM_DECODER = SingleItemDecoder
  149. def __init__(self, **options):
  150. self._singleItemDecoder = self.SINGLE_ITEM_DECODER(**options)
  151. def __call__(self, pyObject, asn1Spec=None, **kwargs):
  152. return self._singleItemDecoder(pyObject, asn1Spec=asn1Spec, **kwargs)
  153. #: Turns Python objects of built-in types into ASN.1 objects.
  154. #:
  155. #: Takes Python objects of built-in types and turns them into a tree of
  156. #: ASN.1 objects (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
  157. #: may be a scalar or an arbitrary nested structure.
  158. #:
  159. #: Parameters
  160. #: ----------
  161. #: pyObject: :py:class:`object`
  162. #: A scalar or nested Python objects
  163. #:
  164. #: Keyword Args
  165. #: ------------
  166. #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  167. #: A pyasn1 type object to act as a template guiding the decoder. It is required
  168. #: for successful interpretation of Python objects mapping into their ASN.1
  169. #: representations.
  170. #:
  171. #: Returns
  172. #: -------
  173. #: : :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  174. #: A scalar or constructed pyasn1 object
  175. #:
  176. #: Raises
  177. #: ------
  178. #: ~pyasn1.error.PyAsn1Error
  179. #: On decoding errors
  180. #:
  181. #: Examples
  182. #: --------
  183. #: Decode native Python object into ASN.1 objects with ASN.1 schema
  184. #:
  185. #: .. code-block:: pycon
  186. #:
  187. #: >>> seq = SequenceOf(componentType=Integer())
  188. #: >>> s, _ = decode([1, 2, 3], asn1Spec=seq)
  189. #: >>> str(s)
  190. #: SequenceOf:
  191. #: 1 2 3
  192. #:
  193. decode = Decoder()