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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.codec.cer import decoder
  8. from pyasn1.type import univ
  9. __all__ = ['decode', 'StreamingDecoder']
  10. class BitStringPayloadDecoder(decoder.BitStringPayloadDecoder):
  11. supportConstructedForm = False
  12. class OctetStringPayloadDecoder(decoder.OctetStringPayloadDecoder):
  13. supportConstructedForm = False
  14. # TODO: prohibit non-canonical encoding
  15. RealPayloadDecoder = decoder.RealPayloadDecoder
  16. TAG_MAP = decoder.TAG_MAP.copy()
  17. TAG_MAP.update(
  18. {univ.BitString.tagSet: BitStringPayloadDecoder(),
  19. univ.OctetString.tagSet: OctetStringPayloadDecoder(),
  20. univ.Real.tagSet: RealPayloadDecoder()}
  21. )
  22. TYPE_MAP = decoder.TYPE_MAP.copy()
  23. # deprecated aliases, https://github.com/pyasn1/pyasn1/issues/9
  24. tagMap = TAG_MAP
  25. typeMap = TYPE_MAP
  26. # Put in non-ambiguous types for faster codec lookup
  27. for typeDecoder in TAG_MAP.values():
  28. if typeDecoder.protoComponent is not None:
  29. typeId = typeDecoder.protoComponent.__class__.typeId
  30. if typeId is not None and typeId not in TYPE_MAP:
  31. TYPE_MAP[typeId] = typeDecoder
  32. class SingleItemDecoder(decoder.SingleItemDecoder):
  33. __doc__ = decoder.SingleItemDecoder.__doc__
  34. TAG_MAP = TAG_MAP
  35. TYPE_MAP = TYPE_MAP
  36. supportIndefLength = False
  37. class StreamingDecoder(decoder.StreamingDecoder):
  38. __doc__ = decoder.StreamingDecoder.__doc__
  39. SINGLE_ITEM_DECODER = SingleItemDecoder
  40. class Decoder(decoder.Decoder):
  41. __doc__ = decoder.Decoder.__doc__
  42. STREAMING_DECODER = StreamingDecoder
  43. #: Turns DER octet stream into an ASN.1 object.
  44. #:
  45. #: Takes DER octet-stream and decode it into an ASN.1 object
  46. #: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
  47. #: may be a scalar or an arbitrary nested structure.
  48. #:
  49. #: Parameters
  50. #: ----------
  51. #: substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
  52. #: DER octet-stream
  53. #:
  54. #: Keyword Args
  55. #: ------------
  56. #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  57. #: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
  58. #: being decoded, *asn1Spec* may or may not be required. Most common reason for
  59. #: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
  60. #:
  61. #: Returns
  62. #: -------
  63. #: : :py:class:`tuple`
  64. #: A tuple of pyasn1 object recovered from DER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  65. #: and the unprocessed trailing portion of the *substrate* (may be empty)
  66. #:
  67. #: Raises
  68. #: ------
  69. #: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError
  70. #: On decoding errors
  71. #:
  72. #: Examples
  73. #: --------
  74. #: Decode DER serialisation without ASN.1 schema
  75. #:
  76. #: .. code-block:: pycon
  77. #:
  78. #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
  79. #: >>> str(s)
  80. #: SequenceOf:
  81. #: 1 2 3
  82. #:
  83. #: Decode DER serialisation with ASN.1 schema
  84. #:
  85. #: .. code-block:: pycon
  86. #:
  87. #: >>> seq = SequenceOf(componentType=Integer())
  88. #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq)
  89. #: >>> str(s)
  90. #: SequenceOf:
  91. #: 1 2 3
  92. #:
  93. decode = Decoder()