hash.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. /*! \file
  16. * Some parts of this code were copied from BIND-9, which in turn was derived
  17. * from universal hash function libraries of Rice University.
  18. \section license UH Universal Hashing Library
  19. Copyright ((c)) 2002, Rice University
  20. All rights reserved.
  21. Redistribution and use in source and binary forms, with or without
  22. modification, are permitted provided that the following conditions are
  23. met:
  24. * Redistributions of source code must retain the above copyright
  25. notice, this list of conditions and the following disclaimer.
  26. * Redistributions in binary form must reproduce the above
  27. copyright notice, this list of conditions and the following
  28. disclaimer in the documentation and/or other materials provided
  29. with the distribution.
  30. * Neither the name of Rice University (RICE) nor the names of its
  31. contributors may be used to endorse or promote products derived
  32. from this software without specific prior written permission.
  33. This software is provided by RICE and the contributors on an "as is"
  34. basis, without any representations or warranties of any kind, express
  35. or implied including, but not limited to, representations or
  36. warranties of non-infringement, merchantability or fitness for a
  37. particular purpose. In no event shall RICE or contributors be liable
  38. for any direct, indirect, incidental, special, exemplary, or
  39. consequential damages (including, but not limited to, procurement of
  40. substitute goods or services; loss of use, data, or profits; or
  41. business interruption) however caused and on any theory of liability,
  42. whether in contract, strict liability, or tort (including negligence
  43. or otherwise) arising in any way out of the use of this software, even
  44. if advised of the possibility of such damage.
  45. */
  46. #include <cassert>
  47. #include <stdlib.h>
  48. #include <algorithm>
  49. #include <cassert>
  50. #include <string>
  51. #include "config.h"
  52. #include "hash.h"
  53. using namespace std;
  54. namespace isc {
  55. namespace nsas {
  56. // Constructor.
  57. Hash::Hash(uint32_t tablesize, uint32_t maxkeylen, bool randomise) :
  58. tablesize_(tablesize), maxkeylen_(min(maxkeylen, (255 - sizeof(uint16_t))))
  59. {
  60. // (Code taken from BIND-9)
  61. //
  62. // Check to see that we can cope with the maximum key length which, due
  63. // to the limitations, should not be more than 255 in total. The actual
  64. // number of characters in the name that are considered is reduced to
  65. // ensure that the class is taken into account in the hash. (This accounts
  66. // for the "+ sizeof(uint16_t)" in the calculations below.
  67. //
  68. // Overflow check. Since our implementation only does a modulo
  69. // operation at the last stage of hash calculation, the accumulator
  70. // must not overflow.
  71. hash_accum_t overflow_limit =
  72. 1 << (((sizeof(hash_accum_t) - sizeof(hash_random_t))) * 8);
  73. if (overflow_limit < (maxkeylen_ + sizeof(uint16_t) + 1) * 0xff) {
  74. isc_throw(KeyLengthTooLong, "Hash key length too long for Hash class");
  75. }
  76. // Initialize the random number generator with the current time.
  77. // TODO: Use something other than pseudo-random numbers.
  78. union {
  79. unsigned int seed;
  80. time_t curtime;
  81. } init_value;
  82. if (randomise) {
  83. init_value.curtime = time(NULL);
  84. }
  85. else {
  86. init_value.seed = 0;
  87. }
  88. srandom(init_value.seed);
  89. // Fill in the random vector.
  90. randvec_.reserve(maxkeylen_ + sizeof(uint16_t) + 1);
  91. for (uint32_t i = 0; i < (maxkeylen + sizeof(uint16_t) + 1); ++i) {
  92. randvec_.push_back(static_cast<hash_random_t>(random() & 0xffff));
  93. }
  94. assert(sizeof(hash_random_t) == 2); // So that the "& 0xffff" is valid
  95. // Finally, initialize the mapping table for uppercase to lowercase
  96. // characters. A table is used as indexing a table is faster than calling
  97. // the tolower() function.
  98. casemap_.reserve(256);
  99. for (int i = 0; i < 256; ++i) {
  100. casemap_.push_back(i);
  101. }
  102. for (int i = 'A'; i <= 'Z'; ++i) {
  103. casemap_[i] += ('a' - 'A');
  104. }
  105. }
  106. uint32_t Hash::operator()(const HashKey& key, bool ignorecase) const
  107. {
  108. // Calculation as given in BIND-9.
  109. hash_accum_t partial_sum = 0;
  110. uint32_t i = 0; // Used after the end of the loop
  111. // Perform the hashing. If the key length if more than the maximum we set
  112. // up this hash for, ignore the excess.
  113. if (ignorecase) {
  114. for (i = 0; i < min(key.keylen, maxkeylen_); ++i) {
  115. partial_sum += mapLower(key.key[i]) * randvec_[i];
  116. }
  117. } else {
  118. for (i = 0; i < min(key.keylen, maxkeylen_); ++i) {
  119. partial_sum += key.key[i] * randvec_[i];
  120. }
  121. }
  122. // Add the hash of the class code
  123. union {
  124. uint16_t class_code; // Copy of the class code
  125. char bytes[sizeof(uint16_t)]; // Byte equivalent
  126. } convert;
  127. convert.class_code = key.class_code;
  128. for (int j = 0; j < sizeof(uint16_t); ++j, ++i) {
  129. partial_sum += convert.bytes[j] * randvec_[i];
  130. }
  131. // ... and finish up.
  132. partial_sum += randvec_[i];
  133. // Determine the hash value
  134. uint32_t value = partial_sum % prime32_;
  135. // ... and round it to fit the table size
  136. return (value % tablesize_);
  137. }
  138. } // namespace nsas
  139. } // namespace isc