michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Implementations of hash functions. */ michael@0: michael@0: #include "mozilla/HashFunctions.h" michael@0: #include "mozilla/Types.h" michael@0: michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: uint32_t michael@0: HashBytes(const void* bytes, size_t length) michael@0: { michael@0: uint32_t hash = 0; michael@0: const char* b = reinterpret_cast(bytes); michael@0: michael@0: /* Walk word by word. */ michael@0: size_t i = 0; michael@0: for (; i < length - (length % sizeof(size_t)); i += sizeof(size_t)) { michael@0: /* Do an explicitly unaligned load of the data. */ michael@0: size_t data; michael@0: memcpy(&data, b + i, sizeof(size_t)); michael@0: michael@0: hash = AddToHash(hash, data, sizeof(data)); michael@0: } michael@0: michael@0: /* Get the remaining bytes. */ michael@0: for (; i < length; i++) michael@0: hash = AddToHash(hash, b[i]); michael@0: michael@0: return hash; michael@0: } michael@0: michael@0: } /* namespace mozilla */