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.

integer.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. import sys
  8. import platform
  9. from pyasn1.compat.octets import oct2int, null, ensureString
  10. implementation = platform.python_implementation()
  11. if sys.version_info[0] < 3:
  12. from binascii import a2b_hex, b2a_hex
  13. def from_bytes(octets, signed=False):
  14. if not octets:
  15. return 0
  16. value = long(b2a_hex(ensureString(octets)), 16)
  17. if signed and oct2int(octets[0]) & 0x80:
  18. return value - (1 << len(octets) * 8)
  19. return value
  20. def to_bytes(value, signed=False, length=0):
  21. if value < 0:
  22. if signed:
  23. bits = bitLength(value)
  24. # two's complement form
  25. maxValue = 1 << bits
  26. valueToEncode = (value + maxValue) % maxValue
  27. else:
  28. raise OverflowError('can\'t convert negative int to unsigned')
  29. elif value == 0 and length == 0:
  30. return null
  31. else:
  32. bits = 0
  33. valueToEncode = value
  34. hexValue = hex(valueToEncode)[2:]
  35. if hexValue.endswith('L'):
  36. hexValue = hexValue[:-1]
  37. if len(hexValue) & 1:
  38. hexValue = '0' + hexValue
  39. # padding may be needed for two's complement encoding
  40. if value != valueToEncode or length:
  41. hexLength = len(hexValue) * 4
  42. padLength = max(length, bits)
  43. if padLength > hexLength:
  44. hexValue = '00' * ((padLength - hexLength - 1) // 8 + 1) + hexValue
  45. elif length and hexLength - length > 7:
  46. raise OverflowError('int too big to convert')
  47. firstOctet = int(hexValue[:2], 16)
  48. if signed:
  49. if firstOctet & 0x80:
  50. if value >= 0:
  51. hexValue = '00' + hexValue
  52. elif value < 0:
  53. hexValue = 'ff' + hexValue
  54. octets_value = a2b_hex(hexValue)
  55. return octets_value
  56. def bitLength(number):
  57. # bits in unsigned number
  58. hexValue = hex(abs(number))
  59. bits = len(hexValue) - 2
  60. if hexValue.endswith('L'):
  61. bits -= 1
  62. if bits & 1:
  63. bits += 1
  64. bits *= 4
  65. # TODO: strip lhs zeros
  66. return bits
  67. else:
  68. def from_bytes(octets, signed=False):
  69. return int.from_bytes(bytes(octets), 'big', signed=signed)
  70. def to_bytes(value, signed=False, length=0):
  71. length = max(value.bit_length(), length)
  72. if signed and length % 8 == 0:
  73. length += 1
  74. return value.to_bytes(length // 8 + (length % 8 and 1 or 0), 'big', signed=signed)
  75. def bitLength(number):
  76. return int(number).bit_length()