FastCRCsw.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* FastCRC library code is placed under the MIT license
  2. * Copyright (c) 2014,2015,2016 Frank Bosing
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. //
  25. // Thanks to:
  26. // - Catalogue of parametrised CRC algorithms, CRC RevEng
  27. // http://reveng.sourceforge.net/crc-catalogue/
  28. //
  29. // - Danjel McGougan (CRC-Table-Generator)
  30. //
  31. //
  32. // modify from FastCRC library @ 2018/11/20
  33. //
  34. #include "FastCRC.h"
  35. #include "FastCRC_tables.hpp"
  36. // ================= 16-BIT CRC ===================
  37. /** Constructor
  38. */
  39. FastCRC16::FastCRC16(uint16_t seed) { seed_ = seed; }
  40. #define crc_n4(crc, data, table) \
  41. crc ^= data; \
  42. crc = (table[(crc & 0xff) + 0x300]) ^ (table[((crc >> 8) & 0xff) + 0x200]) ^ \
  43. (table[((data >> 16) & 0xff) + 0x100]) ^ (table[data >> 24]);
  44. /** MCRF4XX
  45. * equivalent to _crc_ccitt_update() in crc16.h from avr_libc
  46. * @param data Pointer to Data
  47. * @param datalen Length of Data
  48. * @return CRC value
  49. */
  50. uint16_t FastCRC16::mcrf4xx_calc(const uint8_t *data, uint16_t len) {
  51. uint16_t crc = seed_;
  52. while (((uintptr_t)data & 3) && len) {
  53. crc = (crc >> 8) ^ crc_table_mcrf4xx[(crc & 0xff) ^ *data++];
  54. len--;
  55. }
  56. while (len >= 16) {
  57. len -= 16;
  58. crc_n4(crc, ((uint32_t *)data)[0], crc_table_mcrf4xx);
  59. crc_n4(crc, ((uint32_t *)data)[1], crc_table_mcrf4xx);
  60. crc_n4(crc, ((uint32_t *)data)[2], crc_table_mcrf4xx);
  61. crc_n4(crc, ((uint32_t *)data)[3], crc_table_mcrf4xx);
  62. data += 16;
  63. }
  64. while (len--) {
  65. crc = (crc >> 8) ^ crc_table_mcrf4xx[(crc & 0xff) ^ *data++];
  66. }
  67. // seed = crc;
  68. return crc;
  69. }
  70. // ================= 32-BIT CRC ===================
  71. /** Constructor
  72. */
  73. FastCRC32::FastCRC32(uint32_t seed) { seed_ = seed; }
  74. #define crc_n4d(crc, data, table) \
  75. crc ^= data; \
  76. crc = (table[(crc & 0xff) + 0x300]) ^ (table[((crc >> 8) & 0xff) + 0x200]) ^ \
  77. (table[((crc >> 16) & 0xff) + 0x100]) ^ (table[(crc >> 24) & 0xff]);
  78. #define crcsm_n4d(crc, data, table) \
  79. crc ^= data; \
  80. crc = (crc >> 8) ^ (table[crc & 0xff]); \
  81. crc = (crc >> 8) ^ (table[crc & 0xff]); \
  82. crc = (crc >> 8) ^ (table[crc & 0xff]); \
  83. crc = (crc >> 8) ^ (table[crc & 0xff]);
  84. /** CRC32
  85. * Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3
  86. * @param data Pointer to Data
  87. * @param datalen Length of Data
  88. * @return CRC value
  89. */
  90. #if CRC_BIGTABLES
  91. #define CRC_TABLE_CRC32 crc_table_crc32_big
  92. #else
  93. #define CRC_TABLE_CRC32 crc_table_crc32
  94. #endif
  95. uint32_t FastCRC32::crc32_calc(const uint8_t *data, uint16_t len) {
  96. uint32_t crc = seed_ ^ 0xffffffff;
  97. while (((uintptr_t)data & 3) && len) {
  98. crc = (crc >> 8) ^ CRC_TABLE_CRC32[(crc & 0xff) ^ *data++];
  99. len--;
  100. }
  101. while (len >= 16) {
  102. len -= 16;
  103. #if CRC_BIGTABLES
  104. crc_n4d(crc, ((uint32_t *)data)[0], CRC_TABLE_CRC32);
  105. crc_n4d(crc, ((uint32_t *)data)[1], CRC_TABLE_CRC32);
  106. crc_n4d(crc, ((uint32_t *)data)[2], CRC_TABLE_CRC32);
  107. crc_n4d(crc, ((uint32_t *)data)[3], CRC_TABLE_CRC32);
  108. #else
  109. crcsm_n4d(crc, ((uint32_t *)data)[0], CRC_TABLE_CRC32);
  110. crcsm_n4d(crc, ((uint32_t *)data)[1], CRC_TABLE_CRC32);
  111. crcsm_n4d(crc, ((uint32_t *)data)[2], CRC_TABLE_CRC32);
  112. crcsm_n4d(crc, ((uint32_t *)data)[3], CRC_TABLE_CRC32);
  113. #endif
  114. data += 16;
  115. }
  116. while (len--) {
  117. crc = (crc >> 8) ^ CRC_TABLE_CRC32[(crc & 0xff) ^ *data++];
  118. }
  119. // seed = crc;
  120. crc ^= 0xffffffff;
  121. return crc;
  122. }