blob: 44f8beea726040a332c3c9002ea2f1fa0a1f9512 [file] [log] [blame]
Randy Dunlapd29693b2006-05-21 20:57:42 -07001/* crc32hash.c - derived from linux/lib/crc32.c, GNU GPL v2 */
2/* Usage example:
3$ ./crc32hash "Dual Speed"
4*/
5
6#include <string.h>
7#include <stdio.h>
8#include <ctype.h>
9#include <stdlib.h>
10
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -070011static unsigned int crc32(unsigned char const *p, unsigned int len)
Randy Dunlapd29693b2006-05-21 20:57:42 -070012{
13 int i;
14 unsigned int crc = 0;
15 while (len--) {
16 crc ^= *p++;
17 for (i = 0; i < 8; i++)
18 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
19 }
20 return crc;
21}
22
23int main(int argc, char **argv) {
24 unsigned int result;
25 if (argc != 2) {
26 printf("no string passed as argument\n");
27 return -1;
28 }
Randy Dunlapffab10e2008-08-12 15:09:08 -070029 result = crc32((unsigned char const *)argv[1], strlen(argv[1]));
Randy Dunlapd29693b2006-05-21 20:57:42 -070030 printf("0x%x\n", result);
31 return 0;
32}