crc32.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * \file crc32.c
  3. * Functions and types for CRC checks.
  4. *
  5. * Generated on Mon Feb 21 23:02:07 2011,
  6. * by pycrc v0.7.1, http://www.tty1.net/pycrc/
  7. * using the configuration:
  8. * Width = 32
  9. * Poly = 0x04c11db7
  10. * XorIn = 0xffffffff
  11. * ReflectIn = True
  12. * XorOut = 0xffffffff
  13. * ReflectOut = True
  14. * Algorithm = bit-by-bit-fast
  15. * Direct = True
  16. *****************************************************************************/
  17. #include "crc32.h"
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <stdbool.h>
  21. /**
  22. * Reflect all bits of a \a data word of \a data_len bytes.
  23. *
  24. * \param data The data word to be reflected.
  25. * \param data_len The width of \a data expressed in number of bits.
  26. * \return The reflected data.
  27. ******************************************************************************/
  28. uint32_t crc_reflect( uint32_t data, size_t data_len )
  29. {
  30. unsigned int i;
  31. uint32_t ret;
  32. ret = data & 0x01;
  33. for ( i = 1; i < data_len; i++ )
  34. {
  35. data >>= 1;
  36. ret = ( ret << 1 ) | ( data & 0x01 );
  37. }
  38. return ret;
  39. }
  40. /**
  41. * Update the crc value with new data.
  42. *
  43. * \param crc The current crc value.
  44. * \param data Pointer to a buffer of \a data_len bytes.
  45. * \param data_len Number of bytes in the \a data buffer.
  46. * \return The updated crc value.
  47. *****************************************************************************/
  48. uint32_t crc32_update( uint32_t crc, const unsigned char data )
  49. {
  50. unsigned int i;
  51. uint32_t bit;
  52. unsigned char c;
  53. c = data;
  54. for ( i = 0x01; i & 0xff; i <<= 1 )
  55. {
  56. bit = crc & 0x80000000;
  57. if ( c & i )
  58. {
  59. bit = !bit;
  60. }
  61. crc <<= 1;
  62. if ( bit )
  63. {
  64. crc ^= 0x04c11db7;
  65. }
  66. }
  67. return crc & 0xffffffff;
  68. }