crc16.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Library: libcrc
  3. * File: src/crc16.c
  4. * Author: Lammert Bies
  5. *
  6. * This file is licensed under the MIT License as stated below
  7. *
  8. * Copyright (c) 1999-2016 Lammert Bies
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. *
  28. * Description
  29. * -----------
  30. * The source file src/crc16.c contains routines which calculate the common
  31. * CRC16 cyclic redundancy check values for an incomming byte string.
  32. */
  33. #include <stdbool.h>
  34. #include <stdlib.h>
  35. #include "checksum.h"
  36. static void init_crc16_tab( void );
  37. static bool crc_tab16_init = false;
  38. static uint16_t crc_tab16[256];
  39. /*
  40. * uint16_t crc_16( const unsigned char *input_str, size_t num_bytes );
  41. *
  42. * The function crc_16() calculates the 16 bits CRC16 in one pass for a byte
  43. * string of which the beginning has been passed to the function. The number of
  44. * bytes to check is also a parameter. The number of the bytes in the string is
  45. * limited by the constant SIZE_MAX.
  46. */
  47. uint16_t crc_16( const unsigned char *input_str, size_t num_bytes ) {
  48. uint16_t crc;
  49. uint16_t tmp;
  50. uint16_t short_c;
  51. const unsigned char *ptr;
  52. size_t a;
  53. if ( ! crc_tab16_init ) init_crc16_tab();
  54. crc = CRC_START_16;
  55. ptr = input_str;
  56. if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
  57. short_c = 0x00ff & (uint16_t) *ptr;
  58. tmp = crc ^ short_c;
  59. crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];
  60. ptr++;
  61. }
  62. return crc;
  63. } /* crc_16 */
  64. /*
  65. * uint16_t crc_modbus( const unsigned char *input_str, size_t num_bytes );
  66. *
  67. * The function crc_modbus() calculates the 16 bits Modbus CRC in one pass for
  68. * a byte string of which the beginning has been passed to the function. The
  69. * number of bytes to check is also a parameter.
  70. */
  71. uint16_t crc_modbus( const unsigned char *input_str, size_t num_bytes ) {
  72. uint16_t crc;
  73. uint16_t tmp;
  74. uint16_t short_c;
  75. const unsigned char *ptr;
  76. size_t a;
  77. if ( ! crc_tab16_init ) init_crc16_tab();
  78. crc = CRC_START_MODBUS;
  79. ptr = input_str;
  80. if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
  81. short_c = 0x00ff & (uint16_t) *ptr;
  82. tmp = crc ^ short_c;
  83. crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];
  84. ptr++;
  85. }
  86. return crc;
  87. } /* crc_modbus */
  88. /*
  89. * uint16_t update_crc_16( uint16_t crc, unsigned char c );
  90. *
  91. * The function update_crc_16() calculates a new CRC-16 value based on the
  92. * previous value of the CRC and the next byte of data to be checked.
  93. */
  94. uint16_t update_crc_16( uint16_t crc, unsigned char c ) {
  95. uint16_t tmp;
  96. uint16_t short_c;
  97. short_c = 0x00ff & (uint16_t) c;
  98. if ( ! crc_tab16_init ) init_crc16_tab();
  99. tmp = crc ^ short_c;
  100. crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];
  101. return crc;
  102. } /* update_crc_16 */
  103. /*
  104. * static void init_crc16_tab( void );
  105. *
  106. * For optimal performance uses the CRC16 routine a lookup table with values
  107. * that can be used directly in the XOR arithmetic in the algorithm. This
  108. * lookup table is calculated by the init_crc16_tab() routine, the first time
  109. * the CRC function is called.
  110. */
  111. static void init_crc16_tab( void ) {
  112. uint16_t i;
  113. uint16_t j;
  114. uint16_t crc;
  115. uint16_t c;
  116. for (i=0; i<256; i++) {
  117. crc = 0;
  118. c = i;
  119. for (j=0; j<8; j++) {
  120. if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ CRC_POLY_16;
  121. else crc = crc >> 1;
  122. c = c >> 1;
  123. }
  124. crc_tab16[i] = crc;
  125. }
  126. crc_tab16_init = true;
  127. } /* init_crc16_tab */