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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 error
  8. from pyasn1.codec.cer import encoder
  9. from pyasn1.type import univ
  10. __all__ = ['Encoder', 'encode']
  11. class SetEncoder(encoder.SetEncoder):
  12. @staticmethod
  13. def _componentSortKey(componentAndType):
  14. """Sort SET components by tag
  15. Sort depending on the actual Choice value (dynamic sort)
  16. """
  17. component, asn1Spec = componentAndType
  18. if asn1Spec is None:
  19. compType = component
  20. else:
  21. compType = asn1Spec
  22. if compType.typeId == univ.Choice.typeId and not compType.tagSet:
  23. if asn1Spec is None:
  24. return component.getComponent().tagSet
  25. else:
  26. # TODO: move out of sorting key function
  27. names = [namedType.name for namedType in asn1Spec.componentType.namedTypes
  28. if namedType.name in component]
  29. if len(names) != 1:
  30. raise error.PyAsn1Error(
  31. '%s components for Choice at %r' % (len(names) and 'Multiple ' or 'None ', component))
  32. # TODO: support nested CHOICE ordering
  33. return asn1Spec[names[0]].tagSet
  34. else:
  35. return compType.tagSet
  36. TAG_MAP = encoder.TAG_MAP.copy()
  37. TAG_MAP.update({
  38. # Set & SetOf have same tags
  39. univ.Set.tagSet: SetEncoder()
  40. })
  41. TYPE_MAP = encoder.TYPE_MAP.copy()
  42. TYPE_MAP.update({
  43. # Set & SetOf have same tags
  44. univ.Set.typeId: SetEncoder()
  45. })
  46. # deprecated aliases, https://github.com/pyasn1/pyasn1/issues/9
  47. tagMap = TAG_MAP
  48. typeMap = TYPE_MAP
  49. class SingleItemEncoder(encoder.SingleItemEncoder):
  50. fixedDefLengthMode = True
  51. fixedChunkSize = 0
  52. TAG_MAP = TAG_MAP
  53. TYPE_MAP = TYPE_MAP
  54. class Encoder(encoder.Encoder):
  55. SINGLE_ITEM_ENCODER = SingleItemEncoder
  56. #: Turns ASN.1 object into DER octet stream.
  57. #:
  58. #: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  59. #: walks all its components recursively and produces a DER octet stream.
  60. #:
  61. #: Parameters
  62. #: ----------
  63. #: value: either a Python or pyasn1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  64. #: A Python or pyasn1 object to encode. If Python object is given, `asnSpec`
  65. #: parameter is required to guide the encoding process.
  66. #:
  67. #: Keyword Args
  68. #: ------------
  69. #: asn1Spec:
  70. #: Optional ASN.1 schema or value object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  71. #:
  72. #: Returns
  73. #: -------
  74. #: : :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
  75. #: Given ASN.1 object encoded into BER octet-stream
  76. #:
  77. #: Raises
  78. #: ------
  79. #: ~pyasn1.error.PyAsn1Error
  80. #: On encoding errors
  81. #:
  82. #: Examples
  83. #: --------
  84. #: Encode Python value into DER with ASN.1 schema
  85. #:
  86. #: .. code-block:: pycon
  87. #:
  88. #: >>> seq = SequenceOf(componentType=Integer())
  89. #: >>> encode([1, 2, 3], asn1Spec=seq)
  90. #: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
  91. #:
  92. #: Encode ASN.1 value object into DER
  93. #:
  94. #: .. code-block:: pycon
  95. #:
  96. #: >>> seq = SequenceOf(componentType=Integer())
  97. #: >>> seq.extend([1, 2, 3])
  98. #: >>> encode(seq)
  99. #: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
  100. #:
  101. encode = Encoder()