hash_key.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include <cstring>
  15. #include <config.h>
  16. #include "hash_key.h"
  17. namespace isc {
  18. namespace nsas {
  19. /// Hash Equality Function
  20. bool HashKey::operator==(const isc::nsas::HashKey& other) {
  21. // Check key lengths
  22. if (other.keylen == keylen) {
  23. // ... and classes
  24. if (other.class_code == class_code) {
  25. // ... before the expensive operation. This involves a
  26. // byte-by-byte comparison, doing a case-independent match.
  27. // memcmp() doesn't work (exact match) nor does strcmp or its
  28. // variation (stops on the first null byte).
  29. //
  30. // TODO: Use a lookup table to map upper to lower case (for speed)
  31. for (int i = 0; i < other.keylen; ++i) {
  32. if (tolower(static_cast<unsigned char>(other.key[i])) !=
  33. tolower(static_cast<unsigned char>(key[i]))) {
  34. return false; // Mismatch
  35. }
  36. }
  37. return true; // All bytes matched
  38. }
  39. }
  40. return false; // Key length or class did not match
  41. }
  42. } // namespace nsas
  43. } // namespace isc