crc32.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * crc32 calculation routines.
  3. *
  4. * Copyright (c) 2005 by Andrew de Quincey <adq_dvb@lidskialf.net>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #ifndef _UCSI_CRC32_H
  21. #define _UCSI_CRC32_H 1
  22. #include <stdint.h>
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #define CRC32_INIT (~0)
  28. extern uint32_t crc32tbl[];
  29. /**
  30. * Calculate a CRC32 over a piece of data.
  31. *
  32. * @param crc Current CRC value (use CRC32_INIT for first call).
  33. * @param buf Buffer to calculate over.
  34. * @param len Number of bytes.
  35. * @return Calculated CRC.
  36. */
  37. static inline uint32_t crc32(uint32_t crc, uint8_t* buf, size_t len)
  38. {
  39. size_t i;
  40. for (i=0; i< len; i++) {
  41. crc = (crc << 8) ^ crc32tbl[((crc >> 24) ^ buf[i]) & 0xff];
  42. }
  43. return crc;
  44. }
  45. #ifdef __cplusplus
  46. }
  47. #endif
  48. #endif