You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. /*
  2. * Library: libcrc
  3. * Git: https://github.com/lammertb/libcrc
  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. * Copyright (c) 2020 Thomas A. Early, N7TAE
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. *
  29. * Description
  30. * -----------
  31. * The source file contains routines which calculate the CCITT CRC
  32. * values for an incomming byte string.
  33. */
  34. #include <stdlib.h>
  35. #include <stdint.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include "crc.h"
  39. #define CRC_POLY_16 0x5935u
  40. #define CRC_START_16 0xFFFFu
  41. CCRC::CCRC()
  42. {
  43. for (uint16_t i=0; i<256; i++)
  44. {
  45. uint16_t crc = 0;
  46. uint16_t c = i << 8;
  47. for (uint16_t j=0; j<8; j++)
  48. {
  49. if ( (crc ^ c) & 0x8000 )
  50. crc = ( crc << 1 ) ^ CRC_POLY_16;
  51. else
  52. crc = crc << 1;
  53. c = c << 1;
  54. }
  55. crc_tab16[i] = crc;
  56. }
  57. }
  58. uint16_t CCRC::CalcCRC( const uint8_t *input_str, size_t num_bytes ) const
  59. {
  60. uint16_t crc = CRC_START_16;
  61. if ( input_str )
  62. for (size_t a=0; a<num_bytes; a++)
  63. {
  64. crc = (crc << 8) ^ crc_tab16[ ((crc >> 8) ^ uint16_t(input_str[a])) & 0x00FF ];
  65. }
  66. return crc;
  67. }