HashFunctions.cpp 998 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Implementations of hash functions. */
  6. #include "mozilla/HashFunctions.h"
  7. #include "mozilla/Types.h"
  8. #include <string.h>
  9. namespace mozilla {
  10. uint32_t
  11. HashBytes(const void* aBytes, size_t aLength)
  12. {
  13. uint32_t hash = 0;
  14. const char* b = reinterpret_cast<const char*>(aBytes);
  15. /* Walk word by word. */
  16. size_t i = 0;
  17. for (; i < aLength - (aLength % sizeof(size_t)); i += sizeof(size_t)) {
  18. /* Do an explicitly unaligned load of the data. */
  19. size_t data;
  20. memcpy(&data, b + i, sizeof(size_t));
  21. hash = AddToHash(hash, data, sizeof(data));
  22. }
  23. /* Get the remaining bytes. */
  24. for (; i < aLength; i++) {
  25. hash = AddToHash(hash, b[i]);
  26. }
  27. return hash;
  28. }
  29. } /* namespace mozilla */