checksum.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @file :libcrc_sample.c
  3. * @author :wangwang105(wang rong wen)
  4. * @brief Header file for checksum calculations.
  5. *
  6. * This file contains the function declarations for various checksum
  7. * calculations used in the project.
  8. * @version :0.1
  9. * @date :2025-01-09 14:17:41
  10. *
  11. * @copyright:Copyright (c) 2025 wangwang105(wang rong wen)).AllRightsReserved.
  12. */
  13. /*
  14. * Library: libcrc
  15. * File: include/checksum.h
  16. * Author: Lammert Bies
  17. *
  18. * This file is licensed under the MIT License as stated below
  19. *
  20. * Copyright (c) 1999-2018 Lammert Bies
  21. *
  22. * Permission is hereby granted, free of charge, to any person obtaining a copy
  23. * of this software and associated documentation files (the "Software"), to deal
  24. * in the Software without restriction, including without limitation the rights
  25. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  26. * copies of the Software, and to permit persons to whom the Software is
  27. * furnished to do so, subject to the following conditions:
  28. *
  29. * The above copyright notice and this permission notice shall be included in all
  30. * copies or substantial portions of the Software.
  31. *
  32. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  35. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  36. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  37. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  38. * SOFTWARE.
  39. *
  40. * Description
  41. * -----------
  42. * The headerfile include/checksum.h contains the definitions and prototypes
  43. * for routines that can be used to calculate several kinds of checksums.
  44. */
  45. #ifndef DEF_LIBCRC_CHECKSUM_H
  46. #define DEF_LIBCRC_CHECKSUM_H
  47. #include <stddef.h>
  48. #include <stdint.h>
  49. #include <rtconfig.h>
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /*
  54. * #define CRC_POLY_xxxx
  55. *
  56. * The constants of the form CRC_POLY_xxxx define the polynomials for some well
  57. * known CRC calculations.
  58. */
  59. #define CRC_POLY_16 0xA001
  60. #define CRC_POLY_32 0xEDB88320ul
  61. #define CRC_POLY_64 0x42F0E1EBA9EA3693ull
  62. #define CRC_POLY_CCITT 0x1021
  63. #define CRC_POLY_DNP 0xA6BC
  64. #define CRC_POLY_KERMIT 0x8408
  65. #define CRC_POLY_SICK 0x8005
  66. /*
  67. * #define CRC_START_xxxx
  68. *
  69. * The constants of the form CRC_START_xxxx define the values that are used for
  70. * initialization of a CRC value for common used calculation methods.
  71. */
  72. #define CRC_START_8 0x00
  73. #define CRC_START_16 0x0000
  74. #define CRC_START_MODBUS 0xFFFF
  75. #define CRC_START_XMODEM 0x0000
  76. #define CRC_START_CCITT_1D0F 0x1D0F
  77. #define CRC_START_CCITT_FFFF 0xFFFF
  78. #define CRC_START_KERMIT 0x0000
  79. #define CRC_START_SICK 0x0000
  80. #define CRC_START_DNP 0x0000
  81. #define CRC_START_32 0xFFFFFFFFul
  82. #define CRC_START_64_ECMA 0x0000000000000000ull
  83. #define CRC_START_64_WE 0xFFFFFFFFFFFFFFFFull
  84. /*
  85. * Prototype list of global functions
  86. */
  87. #ifdef PKG_USING_CHECKSUM_NMEA
  88. unsigned char * checksum_NMEA( const unsigned char *input_str, unsigned char *result );
  89. #endif
  90. #ifdef LIBCRC_USING_CRC8
  91. uint8_t crc_8( const unsigned char *input_str, size_t num_bytes );
  92. #endif
  93. #ifdef LIBCRC_USING_CRC16
  94. uint16_t crc_16( const unsigned char *input_str, size_t num_bytes );
  95. #endif
  96. #ifdef LIBCRC_USING_CRC32
  97. uint32_t crc_32( const unsigned char *input_str, size_t num_bytes );
  98. #endif
  99. #ifdef LIBCRC_USING_CRC64_ECMA
  100. uint64_t crc_64_ecma( const unsigned char *input_str, size_t num_bytes );
  101. #endif
  102. #ifdef LIBCRC_USING_CRC64_WE
  103. uint64_t crc_64_we( const unsigned char *input_str, size_t num_bytes );
  104. #endif
  105. #ifdef LIBCRC_USING_CRC_CCITT_1D0F
  106. uint16_t crc_ccitt_1d0f( const unsigned char *input_str, size_t num_bytes );
  107. #endif
  108. #ifdef LIBCRC_USING_CRC_CCITT_FFFF
  109. uint16_t crc_ccitt_ffff( const unsigned char *input_str, size_t num_bytes );
  110. #endif
  111. #ifdef LIBCRC_USING_CRC_DNP
  112. uint16_t crc_dnp( const unsigned char *input_str, size_t num_bytes );
  113. #endif
  114. #ifdef LIBCRC_USING_CRC_KERMIT
  115. uint16_t crc_kermit( const unsigned char *input_str, size_t num_bytes );
  116. #endif
  117. #ifdef LIBCRC_USING_CRC_MODBUS
  118. uint16_t crc_modbus( const unsigned char *input_str, size_t num_bytes );
  119. #endif
  120. #ifdef LIBCRC_USING_CRC_SICK
  121. uint16_t crc_sick( const unsigned char *input_str, size_t num_bytes );
  122. #endif
  123. #ifdef LIBCRC_USING_CRC_XMODEM
  124. uint16_t crc_xmodem( const unsigned char *input_str, size_t num_bytes );
  125. #endif
  126. #ifdef LIBCRC_USING_CRC8
  127. uint8_t update_crc_8( uint8_t crc, unsigned char c );
  128. #endif
  129. #ifdef LIBCRC_USING_CRC16
  130. uint16_t update_crc_16( uint16_t crc, unsigned char c );
  131. #endif
  132. #ifdef LIBCRC_USING_CRC32
  133. uint32_t update_crc_32( uint32_t crc, unsigned char c );
  134. #endif
  135. #if defined(LIBCRC_USING_CRC64_ECMA) || defined(LIBCRC_USING_CRC64_WE)
  136. uint64_t update_crc_64_ecma( uint64_t crc, unsigned char c );
  137. #endif
  138. #if defined(LIBCRC_USING_CRC_CCITT_1D0F) || defined(LIBCRC_USING_CRC_CCITT_FFFF) || defined(LIBCRC_USING_CRC_XMODEM)
  139. uint16_t update_crc_ccitt( uint16_t crc, unsigned char c );
  140. #endif
  141. #ifdef LIBCRC_USING_CRC_DNP
  142. uint16_t update_crc_dnp( uint16_t crc, unsigned char c );
  143. #endif
  144. #ifdef LIBCRC_USING_CRC_KERMIT
  145. uint16_t update_crc_kermit( uint16_t crc, unsigned char c );
  146. #endif
  147. #ifdef LIBCRC_USING_CRC_SICK
  148. uint16_t update_crc_sick( uint16_t crc, unsigned char c, unsigned char prev_byte );
  149. #endif
  150. /*
  151. * Global CRC lookup tables
  152. */
  153. extern const uint32_t crc_tab32[];
  154. extern const uint64_t crc_tab64[];
  155. #ifdef __cplusplus
  156. }// Extern C
  157. #endif
  158. #endif // DEF_LIBCRC_CHECKSUM_H