BlueBear

crc.c

Mar 11th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. uint32_t crc_table[256];
  7.  
  8. //hash table initialization, call this procedure beforehand we proceed to
  9. //the hashing function
  10. void initTable(void)
  11. {
  12.     uint32_t i = 0;
  13.     int j = 0;
  14.  
  15.     for (i = 0; i < 256; i++)
  16.     {
  17.         uint32_t c = i;
  18.  
  19.         for (j = 0; j < 8; j++)
  20.         {
  21.             c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
  22.         }
  23.  
  24.         crc_table[i] = c;
  25.     }
  26. }
  27.  
  28. //this function generates the CRC checksum for the string we put in
  29. uint32_t makeCrc(uint8_t *buf, size_t len)
  30. {
  31.     uint32_t c = 0xFFFFFFFF;
  32.     size_t i = 0;
  33.  
  34.     for (i = 0; i < len; i++)
  35.     {
  36.         c = crc_table[(c ^ buf[i]) & 0xFF] ^ (c >> 8);
  37.     }
  38.  
  39.     return c ^ 0xFFFFFFFF;
  40. }
  41.  
  42. int main()
  43. {
  44.     //we initialize the string we want to hash
  45.     const char *crcString = "The quick brown fox jumps over the lazy dog";
  46.  
  47.     //fill the hash table
  48.     initTable();
  49.  
  50.     //print out
  51.     printf("CRC hash: %X", makeCrc(crcString, strlen(crcString)));
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment