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