hash.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <common/hash.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #define CRC32C_POLY_BE 0x1EDC6F41
  20. static inline uint32_t __attribute__ ((pure)) crc32_be_8(uint8_t inputbyte, uint32_t inputcrc)
  21. {
  22. unsigned i;
  23. uint32_t crc;
  24. crc = inputcrc ^ (inputbyte << 24);
  25. for (i = 0; i < 8; i++)
  26. crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32C_POLY_BE : 0);
  27. return crc;
  28. }
  29. uint32_t crc32_be(uint32_t input, uint32_t inputcrc)
  30. {
  31. uint8_t *p = (uint8_t *)&input;
  32. uint32_t crc = inputcrc;
  33. crc = crc32_be_8(p[0], crc);
  34. crc = crc32_be_8(p[1], crc);
  35. crc = crc32_be_8(p[2], crc);
  36. crc = crc32_be_8(p[3], crc);
  37. return crc;
  38. }
  39. uint32_t crc32_string(char *str, uint32_t inputcrc)
  40. {
  41. uint32_t hash = inputcrc;
  42. size_t len = strlen(str);
  43. unsigned i;
  44. for (i = 0; i < len; i++)
  45. {
  46. hash = crc32_be_8((uint8_t)str[i], hash);
  47. }
  48. return hash;
  49. }