_crcfunpy.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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) 2004 Raymond L. Buvel
  8. #
  9. # Permission is hereby granted, free of charge, to any person obtaining a copy
  10. # of this software and associated documentation files (the "Software"), to deal
  11. # in the Software without restriction, including without limitation the rights
  12. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. # copies of the Software, and to permit persons to whom the Software is
  14. # furnished to do so, subject to the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be included in
  17. # all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. # SOFTWARE.
  26. #-----------------------------------------------------------------------------
  27. def _crc8(data, crc, table):
  28. crc = crc & 0xFF
  29. for x in data:
  30. crc = table[ord(x) ^ crc]
  31. return crc
  32. def _crc8r(data, crc, table):
  33. crc = crc & 0xFF
  34. for x in data:
  35. crc = table[ord(x) ^ crc]
  36. return crc
  37. def _crc16(data, crc, table):
  38. crc = crc & 0xFFFF
  39. for x in data:
  40. crc = table[ord(x) ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
  41. return crc
  42. def _crc16r(data, crc, table):
  43. crc = crc & 0xFFFF
  44. for x in data:
  45. crc = table[ord(x) ^ (crc & 0xFF)] ^ (crc >> 8)
  46. return crc
  47. def _crc24(data, crc, table):
  48. crc = crc & 0xFFFFFF
  49. for x in data:
  50. crc = table[ord(x) ^ (int(crc>>16) & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
  51. return crc
  52. def _crc24r(data, crc, table):
  53. crc = crc & 0xFFFFFF
  54. for x in data:
  55. crc = table[ord(x) ^ int(crc & 0xFF)] ^ (crc >> 8)
  56. return crc
  57. def _crc32(data, crc, table):
  58. crc = crc & 0xFFFFFFFFL
  59. for x in data:
  60. crc = table[ord(x) ^ (int(crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00L)
  61. return crc
  62. def _crc32r(data, crc, table):
  63. crc = crc & 0xFFFFFFFFL
  64. for x in data:
  65. crc = table[ord(x) ^ int(crc & 0xFFL)] ^ (crc >> 8)
  66. return crc
  67. def _crc64(data, crc, table):
  68. crc = crc & 0xFFFFFFFFFFFFFFFFL
  69. for x in data:
  70. crc = table[ord(x) ^ (int(crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00L)
  71. return crc
  72. def _crc64r(data, crc, table):
  73. crc = crc & 0xFFFFFFFFFFFFFFFFL
  74. for x in data:
  75. crc = table[ord(x) ^ int(crc & 0xFFL)] ^ (crc >> 8)
  76. return crc