xref: /linux/tools/pcmcia/crc32hash.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* crc32hash.c - derived from linux/lib/crc32.c, GNU GPL v2 */
3 /* Usage example:
4 $ ./crc32hash "Dual Speed"
5 */
6 
7 #include <string.h>
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <stdlib.h>
11 
12 static unsigned int crc32(unsigned char const *p, unsigned int len)
13 {
14 	int i;
15 	unsigned int crc = 0;
16 	while (len--) {
17 		crc ^= *p++;
18 		for (i = 0; i < 8; i++)
19 			crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
20 	}
21 	return crc;
22 }
23 
24 int main(int argc, char **argv) {
25 	unsigned int result;
26 	if (argc != 2) {
27 		printf("no string passed as argument\n");
28 		return -1;
29 	}
30 	result = crc32((unsigned char const *)argv[1], strlen(argv[1]));
31 	printf("0x%x\n", result);
32 	return 0;
33 }
34