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.

xormasker.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. try:
  27. # use Cython implementation of XorMasker validator if available
  28. from wsaccel.xormask import XorMaskerNull
  29. # noinspection PyUnresolvedReferences
  30. from wsaccel.xormask import createXorMasker
  31. create_xor_masker = createXorMasker
  32. except ImportError:
  33. # fallback to pure Python implementation (this is faster on PyPy than above!)
  34. # http://stackoverflow.com/questions/15014310/python3-xrange-lack-hurts
  35. try:
  36. # noinspection PyUnresolvedReferences
  37. xrange
  38. except NameError:
  39. # Python 3
  40. # noinspection PyShadowingBuiltins
  41. xrange = range
  42. from array import array
  43. class XorMaskerNull(object):
  44. __slots__ = ('_ptr',)
  45. # noinspection PyUnusedLocal
  46. def __init__(self, mask=None):
  47. self._ptr = 0
  48. def pointer(self):
  49. return self._ptr
  50. def reset(self):
  51. self._ptr = 0
  52. def process(self, data):
  53. self._ptr += len(data)
  54. return data
  55. class XorMaskerSimple(object):
  56. __slots__ = ('_ptr', '_msk')
  57. def __init__(self, mask):
  58. assert len(mask) == 4
  59. self._ptr = 0
  60. self._msk = array('B', mask)
  61. def pointer(self):
  62. return self._ptr
  63. def reset(self):
  64. self._ptr = 0
  65. def process(self, data):
  66. dlen = len(data)
  67. payload = array('B', data)
  68. for k in xrange(dlen):
  69. payload[k] ^= self._msk[self._ptr & 3]
  70. self._ptr += 1
  71. return payload.tobytes()
  72. class XorMaskerShifted1(object):
  73. __slots__ = ('_ptr', '_mskarray')
  74. def __init__(self, mask):
  75. assert len(mask) == 4
  76. self._ptr = 0
  77. self._mskarray = [array('B'), array('B'), array('B'), array('B')]
  78. for j in xrange(4):
  79. self._mskarray[0].append(mask[j & 3])
  80. self._mskarray[1].append(mask[(j + 1) & 3])
  81. self._mskarray[2].append(mask[(j + 2) & 3])
  82. self._mskarray[3].append(mask[(j + 3) & 3])
  83. def pointer(self):
  84. return self._ptr
  85. def reset(self):
  86. self._ptr = 0
  87. def process(self, data):
  88. dlen = len(data)
  89. payload = array('B', data)
  90. msk = self._mskarray[self._ptr & 3]
  91. for k in xrange(dlen):
  92. payload[k] ^= msk[k & 3]
  93. self._ptr += dlen
  94. return payload.tobytes()
  95. def create_xor_masker(mask, length=None):
  96. if length is None or length < 128:
  97. return XorMaskerSimple(mask)
  98. else:
  99. return XorMaskerShifted1(mask)