strhash.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * strhash.c - FNV string hashing support
  4. *
  5. * Copyright © 2003 Daniel Silverstone <dsilvers@digital-scurf.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <dpkg/string.h>
  23. #define FNV_OFFSET_BASIS 2166136261UL
  24. #define FNV_MIXING_PRIME 16777619UL
  25. /**
  26. * Fowler/Noll/Vo -- FNV-1a simple string hash.
  27. *
  28. * For more info, @see <http://www.isthe.com/chongo/tech/comp/fnv/index.html>.
  29. *
  30. * @param str The string to hash.
  31. *
  32. * @return The hashed value.
  33. */
  34. unsigned int
  35. str_fnv_hash(const char *str)
  36. {
  37. register unsigned int h = FNV_OFFSET_BASIS;
  38. register unsigned int p = FNV_MIXING_PRIME;
  39. while (*str) {
  40. h ^= *str++;
  41. h *= p;
  42. }
  43. return h;
  44. }