crc32hash.c 661 B

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