crc32hash.c 702 B

123456789101112131415161718192021222324252627282930313233
  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. #include <string.h>
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <stdlib.h>
  10. static unsigned int crc32(unsigned char const *p, unsigned int len)
  11. {
  12. int i;
  13. unsigned int crc = 0;
  14. while (len--) {
  15. crc ^= *p++;
  16. for (i = 0; i < 8; i++)
  17. crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
  18. }
  19. return crc;
  20. }
  21. int main(int argc, char **argv) {
  22. unsigned int result;
  23. if (argc != 2) {
  24. printf("no string passed as argument\n");
  25. return -1;
  26. }
  27. result = crc32((unsigned char const *)argv[1], strlen(argv[1]));
  28. printf("0x%x\n", result);
  29. return 0;
  30. }