crc32.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * \file
  3. *
  4. * \brief 32-bit CRC header.
  5. *
  6. * Copyright (C) 2013-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. #ifndef CRC32_H
  47. #define CRC32_H
  48. #include <compiler.h>
  49. /**
  50. * \defgroup common_services_crc32 CRC-32 calculation service
  51. *
  52. * See \ref common_services_crc32_quickstart.
  53. *
  54. * This service enables the user to calculate 32-bit CRC using the polynomial
  55. * defined in the IEEE 802.3 standard, with support for multiple data blocks
  56. * of arbitrary sizes, and any alignment in memory.
  57. *
  58. * @{
  59. */
  60. //! Type to contain 32-bit CRC.
  61. typedef uint32_t crc32_t;
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. enum status_code crc32_recalculate(const void *data, size_t length, crc32_t *crc);
  66. #ifdef __cplusplus
  67. }
  68. #endif
  69. /**
  70. * \brief Calculate 32-bit CRC for initial block
  71. *
  72. * This function calculates the CRC for the specified data block, which may be
  73. * first of an arbitrary number of blocks.
  74. *
  75. * The actual calculation is done in \ref crc32_recalculate(), while this
  76. * function just sets up the initial CRC value.
  77. *
  78. * \param[in] data Address of data.
  79. * \param[in] length Length of data.
  80. * \param[out] crc Address of variable to store the calculated CRC in.
  81. *
  82. * \return Status of calculation.
  83. * \retval STATUS_OK if calculation succeeded.
  84. * \retval <other> if calculation failed somehow.
  85. *
  86. * \note To calculate the CRC of multiple blocks, use this function first, then
  87. * \ref crc32_recalculate() for the following blocks.
  88. */
  89. static inline enum status_code crc32_calculate(const void *data, size_t length,
  90. crc32_t *crc)
  91. {
  92. *crc = 0;
  93. return crc32_recalculate(data, length, crc);
  94. }
  95. /** @} */
  96. /**
  97. * \page common_services_crc32_quickstart Quick Start Guide for CRC-32
  98. *
  99. * To use this service, the user must supply a \ref crc32_t "container" variable
  100. * for the CRC and call \ref crc32_calculate() with the parameters for the first
  101. * block in the dataset. For subsequent blocks, \ref crc32_recalculate() must be
  102. * used.
  103. *
  104. * \note The user may also initialize the container with a known CRC value and
  105. * use that as the "seed" for \ref crc32_recalculate().
  106. *
  107. *
  108. * \section common_services_crc32_quickstart_code Example Code
  109. *
  110. \code
  111. uint8_t block1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  112. uint8_t block2[6] = {0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
  113. crc32_t my_crc;
  114. crc32_calculate(block1, sizeof(block1), &my_crc);
  115. crc32_recalculate(block2, sizeof(block2), &my_crc);
  116. \endcode
  117. *
  118. */
  119. #endif // CRC32_H