crckrmit.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Library: libcrc
  3. * File: src/crckrmit.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/crckrmit.c contains routines which calculate the CRC
  31. * Kermit cyclic redundancy check value for an incomming byte string.
  32. */
  33. #include <stdbool.h>
  34. #include <stdlib.h>
  35. #include "checksum.h"
  36. static void init_crc_tab( void );
  37. static bool crc_tab_init = false;
  38. static uint16_t crc_tab[256];
  39. /*
  40. * uint16_t crc_kermit( const unsigned char *input_str, size_t num_bytes );
  41. *
  42. * The function crc_kermit() calculates the 16 bits Kermit CRC in one pass for
  43. * a byte string of which the beginning has been passed to the function. The
  44. * number of bytes to check is also a parameter.
  45. */
  46. uint16_t crc_kermit( const unsigned char *input_str, size_t num_bytes ) {
  47. uint16_t crc;
  48. uint16_t tmp;
  49. uint16_t short_c;
  50. uint16_t low_byte;
  51. uint16_t high_byte;
  52. const unsigned char *ptr;
  53. size_t a;
  54. if ( ! crc_tab_init ) init_crc_tab();
  55. crc = CRC_START_KERMIT;
  56. ptr = input_str;
  57. if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
  58. short_c = 0x00ff & (uint16_t) *ptr;
  59. tmp = crc ^ short_c;
  60. crc = (crc >> 8) ^ crc_tab[ tmp & 0xff ];
  61. ptr++;
  62. }
  63. low_byte = (crc & 0xff00) >> 8;
  64. high_byte = (crc & 0x00ff) << 8;
  65. crc = low_byte | high_byte;
  66. return crc;
  67. } /* crc_kermit */
  68. /*
  69. * uint16_t update_crc_kermit( uint16_t crc, unsigned char c );
  70. *
  71. * The function update_crc_kermit() calculates a new CRC Kermit value based on
  72. * the previous value of the CRC and the next byte of data to be checked.
  73. */
  74. uint16_t update_crc_kermit( uint16_t crc, unsigned char c ) {
  75. uint16_t tmp;
  76. uint16_t short_c;
  77. short_c = 0x00ff & (uint16_t) c;
  78. if ( ! crc_tab_init ) init_crc_tab();
  79. tmp = crc ^ short_c;
  80. crc = (crc >> 8) ^ crc_tab[ tmp & 0xff ];
  81. return crc;
  82. } /* update_crc_kermit */
  83. /*
  84. * static void init_crc_tab( void );
  85. *
  86. * For optimal performance, the CRC Kermit routine uses a lookup table with
  87. * values that can be used directly in the XOR arithmetic in the algorithm.
  88. * This lookup table is calculated by the init_crc_tab() routine, the first
  89. * time the CRC function is called.
  90. */
  91. static void init_crc_tab( void ) {
  92. uint16_t i;
  93. uint16_t j;
  94. uint16_t crc;
  95. uint16_t c;
  96. for (i=0; i<256; i++) {
  97. crc = 0;
  98. c = i;
  99. for (j=0; j<8; j++) {
  100. if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ CRC_POLY_KERMIT;
  101. else crc = crc >> 1;
  102. c = c >> 1;
  103. }
  104. crc_tab[i] = crc;
  105. }
  106. crc_tab_init = true;
  107. } /* init_crc_tab */