stringhash.h 2.6 KB

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