Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* crc32hash.c - derived from linux/lib/crc32.c, GNU GPL v2 */
0003 /* Usage example:
0004 $ ./crc32hash "Dual Speed"
0005 */
0006 
0007 #include <string.h>
0008 #include <stdio.h>
0009 #include <ctype.h>
0010 #include <stdlib.h>
0011 
0012 static unsigned int crc32(unsigned char const *p, unsigned int len)
0013 {
0014     int i;
0015     unsigned int crc = 0;
0016     while (len--) {
0017         crc ^= *p++;
0018         for (i = 0; i < 8; i++)
0019             crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
0020     }
0021     return crc;
0022 }
0023 
0024 int main(int argc, char **argv) {
0025     unsigned int result;
0026     if (argc != 2) {
0027         printf("no string passed as argument\n");
0028         return -1;
0029     }
0030     result = crc32((unsigned char const *)argv[1], strlen(argv[1]));
0031     printf("0x%x\n", result);
0032     return 0;
0033 }