lib_crc8ccitt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /***************************************************************************
  2. * libs/libc/misc/lib_crc8ccitt.c
  3. *
  4. * Public domain implementation: https://3dbrew.org/wiki/CRC-8-CCITT
  5. *
  6. * Copyright (C) 2018 Alan Carvalho de Assis. All rights reserved.
  7. * Author: Alan Carvalho de Assis <acassis@gmail.com>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USe, DATa, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARe, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ***************************************************************************/
  37. /***************************************************************************
  38. * Included Files
  39. ***************************************************************************/
  40. #include <sys/types.h>
  41. #include <stdint.h>
  42. #include <crc8.h>
  43. /***************************************************************************
  44. * Private Data
  45. ***************************************************************************/
  46. static const uint8_t crc8_tab[256] =
  47. {
  48. 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
  49. 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
  50. 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
  51. 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
  52. 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5,
  53. 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
  54. 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85,
  55. 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
  56. 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
  57. 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
  58. 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2,
  59. 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
  60. 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32,
  61. 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
  62. 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
  63. 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
  64. 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c,
  65. 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
  66. 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec,
  67. 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
  68. 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
  69. 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
  70. 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c,
  71. 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
  72. 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b,
  73. 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
  74. 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
  75. 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
  76. 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb,
  77. 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
  78. 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb,
  79. 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
  80. };
  81. /***************************************************************************
  82. * Public Functions
  83. ***************************************************************************/
  84. /***************************************************************************
  85. * Name: crc8ccitt
  86. *
  87. * Description:
  88. * Continue CRC calculation on a part of the buffer.
  89. *
  90. ***************************************************************************/
  91. uint8_t crc8ccittpart(FAR const uint8_t *src, size_t len, uint8_t crc8val)
  92. {
  93. uint8_t *pos = (uint8_t *) src;
  94. uint8_t *end = pos + len;
  95. while (pos < end)
  96. {
  97. crc8val = crc8_tab[crc8val ^ *pos];
  98. pos++;
  99. }
  100. return crc8val;
  101. }
  102. /***************************************************************************
  103. * Name: crc8ccitt
  104. *
  105. * Description:
  106. * Return a 8-bit CRC of the contents of the 'src' buffer, length 'len
  107. * using a CRC with the polynomial x^8+x^2+x^1+1 ("0x07" polynomial).
  108. *
  109. ***************************************************************************/
  110. uint8_t crc8ccitt(FAR const uint8_t *src, size_t len)
  111. {
  112. return crc8ccittpart(src, len, 0);
  113. }