crcsick.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Library: libcrc
  3. * File: src/crcsick.c
  4. * Author: Lammert Bies
  5. *
  6. * This file is licensed under the MIT License as stated below
  7. *
  8. * Copyright (c) 2007-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/crcsick.c contains routines that help in calculating the
  31. * CRC value that is used as dataprotection measure in communications with Sick
  32. * electronic devices.
  33. */
  34. #include <stdlib.h>
  35. #include "checksum.h"
  36. /*
  37. * uint16_t crc_sick( const unsigned char *input_str, size_t num_bytes );
  38. *
  39. * The function crc_sick() calculates the SICK CRC value of an input string in
  40. * one pass.
  41. */
  42. uint16_t crc_sick( const unsigned char *input_str, size_t num_bytes ) {
  43. uint16_t crc;
  44. uint16_t low_byte;
  45. uint16_t high_byte;
  46. uint16_t short_c;
  47. uint16_t short_p;
  48. const unsigned char *ptr;
  49. size_t a;
  50. crc = CRC_START_SICK;
  51. ptr = input_str;
  52. short_p = 0;
  53. if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
  54. short_c = 0x00ff & (uint16_t) *ptr;
  55. if ( crc & 0x8000 ) crc = ( crc << 1 ) ^ CRC_POLY_SICK;
  56. else crc = crc << 1;
  57. crc ^= ( short_c | short_p );
  58. short_p = short_c << 8;
  59. ptr++;
  60. }
  61. low_byte = (crc & 0xff00) >> 8;
  62. high_byte = (crc & 0x00ff) << 8;
  63. crc = low_byte | high_byte;
  64. return crc;
  65. } /* crc_sick */
  66. /*
  67. * uint16_t update_crc_sick( uint16_t crc, unsigned char c, unsigned char prev_byte );
  68. *
  69. * The function update_crc_sick() calculates a new CRC-SICK value based on the
  70. * previous value of the CRC and the next byte of the data to be checked.
  71. */
  72. uint16_t update_crc_sick( uint16_t crc, unsigned char c, unsigned char prev_byte ) {
  73. uint16_t short_c;
  74. uint16_t short_p;
  75. short_c = 0x00ff & (uint16_t) c;
  76. short_p = ( 0x00ff & (uint16_t) prev_byte ) << 8;
  77. if ( crc & 0x8000 ) crc = ( crc << 1 ) ^ CRC_POLY_SICK;
  78. else crc = crc << 1;
  79. crc &= 0xffff;
  80. crc ^= ( short_c | short_p );
  81. return crc;
  82. } /* update_crc_sick */