decoder.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
  5. # License: http://pyasn1.sf.net/license.html
  6. #
  7. from pyasn1.type import base, univ, char, useful
  8. from pyasn1 import debug, error
  9. __all__ = ['decode']
  10. class AbstractScalarDecoder(object):
  11. def __call__(self, pyObject, asn1Spec, decoderFunc=None):
  12. return asn1Spec.clone(pyObject)
  13. class BitStringDecoder(AbstractScalarDecoder):
  14. def __call__(self, pyObject, asn1Spec, decoderFunc=None):
  15. return asn1Spec.clone(univ.BitString.fromBinaryString(pyObject))
  16. class SequenceOrSetDecoder(object):
  17. def __call__(self, pyObject, asn1Spec, decoderFunc):
  18. asn1Value = asn1Spec.clone()
  19. componentsTypes = asn1Spec.getComponentType()
  20. for field in asn1Value:
  21. if field in pyObject:
  22. asn1Value[field] = decoderFunc(pyObject[field], componentsTypes[field].getType())
  23. return asn1Value
  24. class SequenceOfOrSetOfDecoder(object):
  25. def __call__(self, pyObject, asn1Spec, decoderFunc):
  26. asn1Value = asn1Spec.clone()
  27. for pyValue in pyObject:
  28. asn1Value.append(decoderFunc(pyValue, asn1Spec.getComponentType()))
  29. return asn1Value
  30. class ChoiceDecoder(object):
  31. def __call__(self, pyObject, asn1Spec, decoderFunc):
  32. asn1Value = asn1Spec.clone()
  33. componentsTypes = asn1Spec.getComponentType()
  34. for field in pyObject:
  35. if field in componentsTypes:
  36. asn1Value[field] = decoderFunc(pyObject[field], componentsTypes[field].getType())
  37. break
  38. return asn1Value
  39. tagMap = {
  40. univ.Integer.tagSet.getBaseTag(): AbstractScalarDecoder(),
  41. univ.Boolean.tagSet.getBaseTag(): AbstractScalarDecoder(),
  42. univ.BitString.tagSet.getBaseTag(): BitStringDecoder(),
  43. univ.OctetString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  44. univ.Null.tagSet.getBaseTag(): AbstractScalarDecoder(),
  45. univ.ObjectIdentifier.tagSet.getBaseTag(): AbstractScalarDecoder(),
  46. univ.Enumerated.tagSet.getBaseTag(): AbstractScalarDecoder(),
  47. univ.Real.tagSet.getBaseTag(): AbstractScalarDecoder(),
  48. univ.Sequence.tagSet.getBaseTag(): SequenceOrSetDecoder(), # conflicts with SequenceOf
  49. univ.Set.tagSet.getBaseTag(): SequenceOrSetDecoder(), # conflicts with SetOf
  50. univ.Choice.tagSet.getBaseTag(): ChoiceDecoder(), # conflicts with Any
  51. # character string types
  52. char.UTF8String.tagSet.getBaseTag(): AbstractScalarDecoder(),
  53. char.NumericString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  54. char.PrintableString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  55. char.TeletexString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  56. char.VideotexString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  57. char.IA5String.tagSet.getBaseTag(): AbstractScalarDecoder(),
  58. char.GraphicString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  59. char.VisibleString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  60. char.GeneralString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  61. char.UniversalString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  62. char.BMPString.tagSet.getBaseTag(): AbstractScalarDecoder(),
  63. # useful types
  64. useful.ObjectDescriptor.tagSet.getBaseTag(): AbstractScalarDecoder(),
  65. useful.GeneralizedTime.tagSet.getBaseTag(): AbstractScalarDecoder(),
  66. useful.UTCTime.tagSet.getBaseTag(): AbstractScalarDecoder()
  67. }
  68. # Type-to-codec map for ambiguous ASN.1 types
  69. typeMap = {
  70. univ.Set.typeId: SequenceOrSetDecoder(),
  71. univ.SetOf.typeId: SequenceOfOrSetOfDecoder(),
  72. univ.Sequence.typeId: SequenceOrSetDecoder(),
  73. univ.SequenceOf.typeId: SequenceOfOrSetOfDecoder(),
  74. univ.Choice.typeId: ChoiceDecoder(),
  75. univ.Any.typeId: AbstractScalarDecoder()
  76. }
  77. class Decoder(object):
  78. # noinspection PyDefaultArgument
  79. def __init__(self, tagMap, typeMap):
  80. self.__tagMap = tagMap
  81. self.__typeMap = typeMap
  82. def __call__(self, pyObject, asn1Spec):
  83. if debug.logger & debug.flagDecoder:
  84. debug.scope.push(type(pyObject).__name__)
  85. debug.logger('decoder called at scope %s, working with type %s' % (debug.scope, type(pyObject).__name__))
  86. if asn1Spec is None or not isinstance(asn1Spec, base.Asn1Item):
  87. raise error.PyAsn1Error('asn1Spec is not valid (should be an instance of an ASN.1 Item, not %s)' % asn1Spec.__class__.__name__)
  88. if asn1Spec.typeId is not None and asn1Spec.typeId in self.__typeMap:
  89. valueDecoder = self.__typeMap[asn1Spec.typeId]
  90. elif asn1Spec.tagSet.getBaseTag() in self.__tagMap:
  91. valueDecoder = self.__tagMap[asn1Spec.tagSet.getBaseTag()]
  92. else:
  93. raise error.PyAsn1Error('Unknown ASN.1 tag %s' % asn1Spec.tagSet)
  94. if debug.logger & debug.flagDecoder:
  95. debug.logger('calling decoder %s on Python type %s <%s>' % (type(valueDecoder).__name__, type(pyObject).__name__, repr(pyObject)))
  96. value = valueDecoder(pyObject, asn1Spec, self)
  97. if debug.logger & debug.flagDecoder:
  98. debug.logger('decoder %s produced ASN.1 type %s <%s>' % (type(valueDecoder).__name__, type(value).__name__, repr(value)))
  99. debug.scope.pop()
  100. return value
  101. #: Turns Python objects of built-in types into ASN.1 objects.
  102. #:
  103. #: Takes Python objects of built-in types and turns them into a tree of
  104. #: ASN.1 objects (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
  105. #: may be a scalar or an arbitrary nested structure.
  106. #:
  107. #: Parameters
  108. #: ----------
  109. #: pyObject: :py:class:`object`
  110. #: A scalar or nested Python objects
  111. #:
  112. #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  113. #: A pyasn1 type object to act as a template guiding the decoder. It is required
  114. #: for successful interpretation of Python objects mapping into their ASN.1
  115. #: representations.
  116. #:
  117. #: Returns
  118. #: -------
  119. #: : :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  120. #: A scalar or constructed pyasn1 object
  121. #:
  122. #: Raises
  123. #: ------
  124. #: : :py:class:`pyasn1.error.PyAsn1Error`
  125. #: On decoding errors
  126. decode = Decoder(tagMap, typeMap)