stringhash.h 2.7 KB

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