Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2018, The University of Memphis |
| 4 | * |
| 5 | * This file is part of PSync. |
| 6 | * See AUTHORS.md for complete list of PSync authors and contributors. |
| 7 | * |
| 8 | * PSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * murmurHash3 was written by Austin Appleby, and is placed in the public |
| 20 | * domain. The author hereby disclaims copyright to this source code. |
| 21 | * https://github.com/aappleby/smhasher/blob/master/src/murmurHash3.cpp |
| 22 | **/ |
| 23 | |
| 24 | #include "util.hpp" |
| 25 | |
| 26 | #include <ndn-cxx/util/backports.hpp> |
| 27 | |
| 28 | #include <string> |
| 29 | |
| 30 | namespace psync { |
| 31 | |
| 32 | static uint32_t |
| 33 | ROTL32 ( uint32_t x, int8_t r ) |
| 34 | { |
| 35 | return (x << r) | (x >> (32 - r)); |
| 36 | } |
| 37 | |
| 38 | uint32_t |
| 39 | murmurHash3(uint32_t nHashSeed, const std::vector<unsigned char>& vDataToHash) |
| 40 | { |
| 41 | uint32_t h1 = nHashSeed; |
| 42 | const uint32_t c1 = 0xcc9e2d51; |
| 43 | const uint32_t c2 = 0x1b873593; |
| 44 | |
| 45 | const size_t nblocks = vDataToHash.size() / 4; |
| 46 | |
| 47 | //---------- |
| 48 | // body |
| 49 | const uint32_t * blocks = (const uint32_t *)(&vDataToHash[0] + nblocks*4); |
| 50 | |
| 51 | for (size_t i = -nblocks; i; i++) { |
| 52 | uint32_t k1 = blocks[i]; |
| 53 | |
| 54 | k1 *= c1; |
| 55 | k1 = ROTL32(k1,15); |
| 56 | k1 *= c2; |
| 57 | |
| 58 | h1 ^= k1; |
| 59 | h1 = ROTL32(h1,13); |
| 60 | h1 = h1*5+0xe6546b64; |
| 61 | } |
| 62 | |
| 63 | //---------- |
| 64 | // tail |
| 65 | const uint8_t * tail = (const uint8_t*)(&vDataToHash[0] + nblocks*4); |
| 66 | |
| 67 | uint32_t k1 = 0; |
| 68 | |
| 69 | switch (vDataToHash.size() & 3) { |
| 70 | case 3: |
| 71 | k1 ^= tail[2] << 16; |
| 72 | NDN_CXX_FALLTHROUGH; |
| 73 | |
| 74 | case 2: |
| 75 | k1 ^= tail[1] << 8; |
| 76 | NDN_CXX_FALLTHROUGH; |
| 77 | |
| 78 | case 1: |
| 79 | k1 ^= tail[0]; |
| 80 | k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; |
| 81 | } |
| 82 | |
| 83 | //---------- |
| 84 | // finalization |
| 85 | h1 ^= vDataToHash.size(); |
| 86 | h1 ^= h1 >> 16; |
| 87 | h1 *= 0x85ebca6b; |
| 88 | h1 ^= h1 >> 13; |
| 89 | h1 *= 0xc2b2ae35; |
| 90 | h1 ^= h1 >> 16; |
| 91 | |
| 92 | return h1; |
| 93 | } |
| 94 | |
| 95 | uint32_t |
| 96 | murmurHash3(uint32_t nHashSeed, const std::string& str) |
| 97 | { |
| 98 | return murmurHash3(nHashSeed, std::vector<unsigned char>(str.begin(), str.end())); |
| 99 | } |
| 100 | |
| 101 | uint32_t |
| 102 | murmurHash3(uint32_t nHashSeed, uint32_t value) |
| 103 | { |
| 104 | return murmurHash3(nHashSeed, |
| 105 | std::vector<unsigned char>((unsigned char*)&value, |
| 106 | (unsigned char*)&value + sizeof(uint32_t))); |
| 107 | } |
| 108 | |
| 109 | } // namespace psync |