| 16 | const int r = 24; |
| 17 | |
| 18 | uint32_t MurmurHash2(std::ifstream&& file_stream, std::size_t buffer_size, std::function<bool(char)> filter_out) |
| 19 | { |
| 20 | auto* buffer = new char[buffer_size]; |
| 21 | char data[4]; |
| 22 | |
| 23 | int read = 0; |
| 24 | uint32_t size = 0; |
| 25 | |
| 26 | // We need the size without the filtered out characters before actually calculating the hash, |
| 27 | // to setup the initial value for the hash. |
| 28 | do { |
| 29 | file_stream.read(buffer, buffer_size); |
| 30 | read = file_stream.gcount(); |
| 31 | for (int i = 0; i < read; i++) { |
| 32 | if (!filter_out(buffer[i])) |
| 33 | size += 1; |
| 34 | } |
| 35 | } while (!file_stream.eof()); |
| 36 | |
| 37 | file_stream.clear(); |
| 38 | file_stream.seekg(0, file_stream.beg); |
| 39 | |
| 40 | int index = 0; |
| 41 | |
| 42 | // This forces a seed of 1. |
| 43 | IncrementalHashInfo info{ (uint32_t)1 ^ size, (uint32_t)size }; |
| 44 | do { |
| 45 | file_stream.read(buffer, buffer_size); |
| 46 | read = file_stream.gcount(); |
| 47 | for (int i = 0; i < read; i++) { |
| 48 | char c = buffer[i]; |
| 49 | |
| 50 | if (filter_out(c)) |
| 51 | continue; |
| 52 | |
| 53 | data[index] = c; |
| 54 | index = (index + 1) % 4; |
| 55 | |
| 56 | // Mix 4 bytes at a time into the hash |
| 57 | if (index == 0) |
| 58 | FourBytes_MurmurHash2((unsigned char*)&data, info); |
| 59 | } |
| 60 | } while (!file_stream.eof()); |
| 61 | |
| 62 | // Do one last bit shuffle in the hash |
| 63 | FourBytes_MurmurHash2((unsigned char*)&data, info); |
| 64 | |
| 65 | delete[] buffer; |
| 66 | |
| 67 | file_stream.close(); |
| 68 | return info.h; |
| 69 | } |
| 70 | |
| 71 | void FourBytes_MurmurHash2(const unsigned char* data, IncrementalHashInfo& prev) |
| 72 | { |
no test coverage detected