fork(4) download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4.  
  5. uint32_t crc_table[256];
  6.  
  7. /* Run this function previously */
  8. void make_crc_table(void) {
  9. for (uint32_t i = 0; i < 256; i++) {
  10. uint32_t c = i;
  11. for (int j = 0; j < 8; j++) {
  12. c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
  13. }
  14. crc_table[i] = c;
  15. }
  16. }
  17.  
  18. uint32_t crc32_wikipedia(const uint8_t *buf, size_t len) {
  19. uint32_t c = 0xFFFFFFFF;
  20. for (size_t i = 0; i < len; i++) {
  21. c = crc_table[(c ^ buf[i]) & 0xFF] ^ (c >> 8);
  22. }
  23. return c ^ 0xFFFFFFFF;
  24. }
  25.  
  26.  
  27.  
  28. const uint32_t Polynomial = 0xEDB88320;
  29.  
  30. uint32_t crc32_bitwise(const void* data, size_t length, uint32_t previousCrc32 = 0) {
  31. uint32_t crc = ~previousCrc32; // same as previousCrc32 ^ 0xFFFFFFFF
  32. unsigned char* current = (unsigned char*) data;
  33. while (length--) {
  34. crc ^= *current++;
  35. for (unsigned int j = 0; j < 8; j++)
  36. if (crc & 1) crc = (crc >> 1) ^ Polynomial; else crc = crc >> 1;
  37.  
  38. }
  39. return ~crc; // same as crc ^ 0xFFFFFFFF
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. make_crc_table();
  46. printf("%08x\n", crc32_wikipedia((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26));
  47. printf("%08x\n", crc32_bitwise("abcdefghijklmnopqrstuvwxyz", 26, 0));
  48. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
4c2750bd
4c2750bd