crc32.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  3. *
  4. * Based on busybox code:
  5. * CRC32 table fill function
  6. * Copyright (C) 2006 by Rob Sullivan <cogito.ergo.cogito@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #ifndef __BB_CRC32_H
  18. #define __BB_CRC32_H
  19. static inline void
  20. crc32_filltable(uint32_t *crc_table)
  21. {
  22. uint32_t polynomial = 0xedb88320;
  23. uint32_t c;
  24. int i, j;
  25. for (i = 0; i < 256; i++) {
  26. c = i;
  27. for (j = 8; j; j--)
  28. c = (c&1) ? ((c >> 1) ^ polynomial) : (c >> 1);
  29. *crc_table++ = c;
  30. }
  31. }
  32. static inline uint32_t
  33. crc32_block(uint32_t val, const void *buf, unsigned len, uint32_t *crc_table)
  34. {
  35. const void *end = (uint8_t*)buf + len;
  36. while (buf != end) {
  37. val = crc_table[(uint8_t)val ^ *(uint8_t*)buf] ^ (val >> 8);
  38. buf = (uint8_t*)buf + 1;
  39. }
  40. return val;
  41. }
  42. #endif