crc32.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * \file stdout
  3. * Functions and types for CRC checks.
  4. *
  5. * Generated on Tue Jun 30 21:35:13 2009,
  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 <stdint.h>
  18. #include "crc32.h"
  19. /**
  20. * Reflect all bits of a \a data word of \a data_len bytes.
  21. *
  22. * \param data The data word to be reflected.
  23. * \param data_len The width of \a data expressed in number of bits.
  24. * \return The reflected data.
  25. *****************************************************************************/
  26. long crc_reflect(long data, size_t data_len)
  27. {
  28. unsigned int i;
  29. long ret;
  30. ret = data & 0x01;
  31. for (i = 1; i < data_len; i++)
  32. {
  33. data >>= 1;
  34. ret = (ret << 1) | (data & 0x01);
  35. }
  36. return ret;
  37. }
  38. /**
  39. * Update the crc value with new data.
  40. *
  41. * \param crc The current crc value.
  42. * \param data Pointer to a buffer of \a data_len bytes.
  43. * \param data_len Number of bytes in the \a data buffer.
  44. * \return The updated crc value.
  45. *****************************************************************************/
  46. crc_t crc_update(crc_t crc, const unsigned char *data, size_t data_len)
  47. {
  48. unsigned int i;
  49. uint8_t bit;
  50. unsigned char c;
  51. while (data_len--) {
  52. c = *data++;
  53. for (i = 0x01; i & 0xff; i <<= 1) {
  54. bit = ((crc & 0x80000000) ? 1 : 0);
  55. if (c & i) {
  56. bit ^= 1;
  57. }
  58. crc <<= 1;
  59. if (bit) {
  60. crc ^= 0x04c11db7;
  61. }
  62. }
  63. crc &= 0xffffffff;
  64. }
  65. return crc & 0xffffffff;
  66. }