crc32.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * \file
  3. *
  4. * \brief 32-bit CRC implementation.
  5. *
  6. * Copyright (C) 2014-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include "crc32.h"
  47. #include <status_codes.h>
  48. /**
  49. * Convenience typedef for words.
  50. *
  51. * \note This type has an architecture dependent size, and is used to optimize
  52. * the CRC algorithm with regards to the number of databus accesses.
  53. */
  54. typedef unsigned int word_t;
  55. /** Polynomial for 32-bit CRC in IEEE 802.3. */
  56. #define CRC32_POLYNOMIAL 0xEDB88320UL
  57. /** Convenience macro for inverting the CRC. */
  58. #define COMPLEMENT_CRC(c) ((c) ^ 0xffffffffUL)
  59. /** Convenience macro for size of a word. */
  60. #define WORD_SIZE (sizeof(word_t))
  61. /** Bitmask for word-aligning an address. */
  62. #define WORD_ALIGNMENT_MASK ~((uintptr_t)WORD_SIZE - 1)
  63. /**
  64. * \internal
  65. * \brief Recalculate 32-bit CRC for bytes within a word
  66. *
  67. * \param[in] data Data to recalculate for.
  68. * \param[in] crc Initial/current CRC value.
  69. * \param[in] bytes Number of data bytes in word.
  70. *
  71. * \return New CRC value.
  72. *
  73. * \attention This implementation assumes a little-endian architecture.
  74. */
  75. static inline crc32_t _crc32_recalculate_bytes_helper(word_t data,
  76. crc32_t crc, uint_fast8_t bytes)
  77. {
  78. uint_fast8_t bit;
  79. crc ^= data;
  80. for (bit = 8 * bytes; bit > 0; bit--) {
  81. if (crc & 1) {
  82. crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
  83. } else {
  84. crc >>= 1;
  85. }
  86. }
  87. return crc;
  88. }
  89. /**
  90. * \brief Recalculate 32-bit CRC for another block
  91. *
  92. * This function recalculates the CRC according to the polynomial
  93. * \ref CRC32_POLYNOMIAL for the specified data block and initial CRC value.
  94. *
  95. * To reduce the number of databus accesses and thus speed up the calculation,
  96. * the algorithm is tuned to work with words as much as possible.
  97. *
  98. * \param[in] data Address of data.
  99. * \param[in] length Length of data.
  100. * \param[in,out] crc Address of variable containing current CRC, and to store
  101. * recalculated CRC in.
  102. *
  103. * \return Status of calculation.
  104. * \retval STATUS_OK if calculation succeeded.
  105. * \retval <other> if calculation failed or could not be started.
  106. *
  107. * \note To calculate the CRC of multiple blocks, use \ref crc32_calculate()
  108. * first, then this function for the following blocks.
  109. *
  110. * \attention This implementation assumes a little-endian architecture.
  111. */
  112. enum status_code crc32_recalculate(const void *data, size_t length, crc32_t *crc)
  113. {
  114. const word_t *word_ptr =
  115. (word_t *)((uintptr_t)data & WORD_ALIGNMENT_MASK);
  116. size_t temp_length;
  117. crc32_t temp_crc = COMPLEMENT_CRC(*crc);
  118. word_t word;
  119. // Calculate for initial bytes to get word-aligned
  120. if (length < WORD_SIZE) {
  121. temp_length = length;
  122. } else {
  123. temp_length = ~WORD_ALIGNMENT_MASK & (WORD_SIZE - (uintptr_t)data);
  124. }
  125. if (temp_length) {
  126. length -= temp_length;
  127. word = *(word_ptr++);
  128. word >>= 8 * (WORD_SIZE - temp_length);
  129. temp_crc = _crc32_recalculate_bytes_helper(word, temp_crc, temp_length);
  130. }
  131. // Calculate for whole words, if any
  132. temp_length = length & WORD_ALIGNMENT_MASK;
  133. if (temp_length) {
  134. length -= temp_length;
  135. temp_length /= WORD_SIZE;
  136. while (temp_length--) {
  137. word = *(word_ptr++);
  138. temp_crc = _crc32_recalculate_bytes_helper(word, temp_crc, WORD_SIZE);
  139. }
  140. }
  141. // Calculate for tailing bytes
  142. if (length) {
  143. word = *word_ptr;
  144. word &= 0xffffffffUL >> (8 * (WORD_SIZE - length));
  145. temp_crc = _crc32_recalculate_bytes_helper(word, temp_crc, length);
  146. }
  147. *crc = COMPLEMENT_CRC(temp_crc);
  148. return STATUS_OK;
  149. }