stringhash.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef __LINUX_STRINGHASH_H
  2. #define __LINUX_STRINGHASH_H
  3. #include <linux/compiler.h> /* For __pure */
  4. #include <linux/types.h> /* For u32, u64 */
  5. /*
  6. * Routines for hashing strings of bytes to a 32-bit hash value.
  7. *
  8. * These hash functions are NOT GUARANTEED STABLE between kernel
  9. * versions, architectures, or even repeated boots of the same kernel.
  10. * (E.g. they may depend on boot-time hardware detection or be
  11. * deliberately randomized.)
  12. *
  13. * They are also not intended to be secure against collisions caused by
  14. * malicious inputs; much slower hash functions are required for that.
  15. *
  16. * They are optimized for pathname components, meaning short strings.
  17. * Even if a majority of files have longer names, the dynamic profile of
  18. * pathname components skews short due to short directory names.
  19. * (E.g. /usr/lib/libsesquipedalianism.so.3.141.)
  20. */
  21. /*
  22. * Version 1: one byte at a time. Example of use:
  23. *
  24. * unsigned long hash = init_name_hash;
  25. * while (*p)
  26. * hash = partial_name_hash(tolower(*p++), hash);
  27. * hash = end_name_hash(hash);
  28. *
  29. * Although this is designed for bytes, fs/hfsplus/unicode.c
  30. * abuses it to hash 16-bit values.
  31. */
  32. /* Hash courtesy of the R5 hash in reiserfs modulo sign bits */
  33. #define init_name_hash(salt) (unsigned long)(salt)
  34. /* partial hash update function. Assume roughly 4 bits per character */
  35. static inline unsigned long
  36. partial_name_hash(unsigned long c, unsigned long prevhash)
  37. {
  38. return (prevhash + (c << 4) + (c >> 4)) * 11;
  39. }
  40. /*
  41. * Finally: cut down the number of bits to a int value (and try to avoid
  42. * losing bits)
  43. */
  44. static inline unsigned long end_name_hash(unsigned long hash)
  45. {
  46. return (unsigned int)hash;
  47. }
  48. /*
  49. * Version 2: One word (32 or 64 bits) at a time.
  50. * If CONFIG_DCACHE_WORD_ACCESS is defined (meaning <asm/word-at-a-time.h>
  51. * exists, which describes major Linux platforms like x86 and ARM), then
  52. * this computes a different hash function much faster.
  53. *
  54. * If not set, this falls back to a wrapper around the preceding.
  55. */
  56. extern unsigned int __pure full_name_hash(const void *salt, const char *, unsigned int);
  57. /*
  58. * A hash_len is a u64 with the hash of a string in the low
  59. * half and the length in the high half.
  60. */
  61. #define hashlen_hash(hashlen) ((u32)(hashlen))
  62. #define hashlen_len(hashlen) ((u32)((hashlen) >> 32))
  63. #define hashlen_create(hash, len) ((u64)(len)<<32 | (u32)(hash))
  64. /* Return the "hash_len" (hash and length) of a null-terminated string */
  65. extern u64 __pure hashlen_string(const void *salt, const char *name);
  66. #endif /* __LINUX_STRINGHASH_H */