crc16.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * \file stdout
  3. * Functions and types for CRC checks.
  4. *
  5. * Generated on Tue Jun 30 23:02:59 2009,
  6. * by pycrc v0.7.1, http://www.tty1.net/pycrc/
  7. * using the configuration:
  8. * Width = 16
  9. * Poly = 0x8005
  10. * XorIn = 0x0000
  11. * ReflectIn = True
  12. * XorOut = 0x0000
  13. * ReflectOut = True
  14. * Algorithm = bit-by-bit-fast
  15. * Direct = True
  16. *****************************************************************************/
  17. #include <stdint.h>
  18. #include "crc16.h"
  19. /**
  20. * Update the crc value with new data.
  21. *
  22. * \param crc The current crc value.
  23. * \param data Pointer to a buffer of \a data_len bytes.
  24. * \param data_len Number of bytes in the \a data buffer.
  25. * \return The updated crc value.
  26. *****************************************************************************/
  27. crc_t crc16_update(crc_t crc, const unsigned char *data, size_t data_len)
  28. {
  29. unsigned int i;
  30. uint8_t bit;
  31. unsigned char c;
  32. while (data_len--) {
  33. c = *data++;
  34. for (i = 0x01; i & 0xff; i <<= 1) {
  35. bit = (crc & 0x8000 ? 1 : 0);
  36. if (c & i) {
  37. bit ^= 1;
  38. }
  39. crc <<= 1;
  40. if (bit) {
  41. crc ^= 0x8005;
  42. }
  43. }
  44. crc &= 0xffff;
  45. }
  46. return crc & 0xffff;
  47. }