nss_hash.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (c) 1997-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <nss.h>
  16. /* This is from libc/db/hash/hash_func.c, hash3 is static there */
  17. /*
  18. * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte
  19. * units. On the first time through the loop we get the "leftover bytes"
  20. * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle
  21. * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If
  22. * this routine is heavily used enough, it's worth the ugly coding.
  23. *
  24. * OZ's original sdbm hash
  25. */
  26. uint32_t
  27. __nss_hash (const void *keyarg, size_t len)
  28. {
  29. const unsigned char *key;
  30. size_t loop;
  31. uint32_t h;
  32. #define HASHC h = *key++ + 65599 * h
  33. h = 0;
  34. key = keyarg;
  35. if (len > 0)
  36. {
  37. loop = (len + 8 - 1) >> 3;
  38. switch (len & (8 - 1))
  39. {
  40. case 0:
  41. do
  42. {
  43. HASHC;
  44. /* FALLTHROUGH */
  45. case 7:
  46. HASHC;
  47. /* FALLTHROUGH */
  48. case 6:
  49. HASHC;
  50. /* FALLTHROUGH */
  51. case 5:
  52. HASHC;
  53. /* FALLTHROUGH */
  54. case 4:
  55. HASHC;
  56. /* FALLTHROUGH */
  57. case 3:
  58. HASHC;
  59. /* FALLTHROUGH */
  60. case 2:
  61. HASHC;
  62. /* FALLTHROUGH */
  63. case 1:
  64. HASHC;
  65. }
  66. while (--loop);
  67. }
  68. }
  69. return h;
  70. }
  71. libc_hidden_def (__nss_hash)