_crcfunpy.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #-----------------------------------------------------------------------------
  2. # Low level CRC functions for use by crcmod. This version is implemented in
  3. # Python for a couple of reasons. 1) Provide a reference implememtation.
  4. # 2) Provide a version that can be used on systems where a C compiler is not
  5. # available for building extension modules.
  6. #
  7. # Copyright (c) 2009 Raymond L. Buvel
  8. # Copyright (c) 2010 Craig McQueen
  9. #
  10. # Permission is hereby granted, free of charge, to any person obtaining a copy
  11. # of this software and associated documentation files (the "Software"), to deal
  12. # in the Software without restriction, including without limitation the rights
  13. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. # copies of the Software, and to permit persons to whom the Software is
  15. # furnished to do so, subject to the following conditions:
  16. #
  17. # The above copyright notice and this permission notice shall be included in
  18. # all copies or substantial portions of the Software.
  19. #
  20. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. # SOFTWARE.
  27. #-----------------------------------------------------------------------------
  28. def _get_buffer_view(in_obj):
  29. if isinstance(in_obj, str):
  30. raise TypeError('Unicode-objects must be encoded before calculating a CRC')
  31. mv = memoryview(in_obj)
  32. if mv.ndim > 1:
  33. raise BufferError('Buffer must be single dimension')
  34. return mv
  35. def _crc8(data, crc, table):
  36. mv = _get_buffer_view(data)
  37. crc = crc & 0xFF
  38. for x in mv.tobytes():
  39. crc = table[x ^ crc]
  40. return crc
  41. def _crc8r(data, crc, table):
  42. mv = _get_buffer_view(data)
  43. crc = crc & 0xFF
  44. for x in mv.tobytes():
  45. crc = table[x ^ crc]
  46. return crc
  47. def _crc16(data, crc, table):
  48. mv = _get_buffer_view(data)
  49. crc = crc & 0xFFFF
  50. for x in mv.tobytes():
  51. crc = table[x ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
  52. return crc
  53. def _crc16r(data, crc, table):
  54. mv = _get_buffer_view(data)
  55. crc = crc & 0xFFFF
  56. for x in mv.tobytes():
  57. crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
  58. return crc
  59. def _crc24(data, crc, table):
  60. mv = _get_buffer_view(data)
  61. crc = crc & 0xFFFFFF
  62. for x in mv.tobytes():
  63. crc = table[x ^ (crc>>16 & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
  64. return crc
  65. def _crc24r(data, crc, table):
  66. mv = _get_buffer_view(data)
  67. crc = crc & 0xFFFFFF
  68. for x in mv.tobytes():
  69. crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
  70. return crc
  71. def _crc32(data, crc, table):
  72. mv = _get_buffer_view(data)
  73. crc = crc & 0xFFFFFFFF
  74. for x in mv.tobytes():
  75. crc = table[x ^ ((crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00)
  76. return crc
  77. def _crc32r(data, crc, table):
  78. mv = _get_buffer_view(data)
  79. crc = crc & 0xFFFFFFFF
  80. for x in mv.tobytes():
  81. crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
  82. return crc
  83. def _crc64(data, crc, table):
  84. mv = _get_buffer_view(data)
  85. crc = crc & 0xFFFFFFFFFFFFFFFF
  86. for x in mv.tobytes():
  87. crc = table[x ^ ((crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00)
  88. return crc
  89. def _crc64r(data, crc, table):
  90. mv = _get_buffer_view(data)
  91. crc = crc & 0xFFFFFFFFFFFFFFFF
  92. for x in mv.tobytes():
  93. crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
  94. return crc