static void outputv(ErlDrvData handle, ErlIOVec *ev) { ErlDrvPort port = (ErlDrvPort) handle; SysIOVec *bin; int i, n, index = 0; unsigned long hash; unsigned long seed; //first piece of the iovec is the seed // printf("ev->size %d\n", ev->size); // printf("ev-vsize %d\n", ev->vsize); //apparently we start counting at 1 round here? bin = &ev->iov[1]; // printf("bin->orig_size %d\n", bin->iov_len); // printf("bin->iov_base %s\n", bin->iov_base); ei_decode_version(bin->iov_base, &index, NULL); ei_decode_ulong(bin->iov_base, &index, &seed); hash = (unsigned int) seed; if (index < bin->iov_len) { hash = MurmurHash2(&bin->iov_base[index], bin->iov_len - index, hash); } // printf("hash %d\n", hash); for (i=2; i<ev->vsize; i++) { bin = &ev->iov[i]; // printf("bin->orig_size %d\n", bin->iov_len); hash = MurmurHash2(bin->iov_base, bin->iov_len, hash); // printf("hashed %d\n", i); } send_hash(port, hash); }
static void BaseUtilTest() { assert(RoundToPowerOf2(0) == 1); assert(RoundToPowerOf2(1) == 1); assert(RoundToPowerOf2(2) == 2); assert(RoundToPowerOf2(3) == 4); assert(RoundToPowerOf2(15) == 16); assert(RoundToPowerOf2((1 << 13) + 1) == (1 << 14)); assert(RoundToPowerOf2(MAX_SIZE_T) == MAX_SIZE_T); assert(MurmurHash2(NULL, 0) == 0x342CE6C); assert(MurmurHash2("test", 4) != MurmurHash2("Test", 4)); }
void BaseUtilTest() { utassert(RoundToPowerOf2(0) == 1); utassert(RoundToPowerOf2(1) == 1); utassert(RoundToPowerOf2(2) == 2); utassert(RoundToPowerOf2(3) == 4); utassert(RoundToPowerOf2(15) == 16); utassert(RoundToPowerOf2((1 << 13) + 1) == (1 << 14)); utassert(RoundToPowerOf2((size_t)-42) == (size_t)-1); utassert(MurmurHash2(NULL, 0) == 0x342CE6C); utassert(MurmurHash2("test", 4) != MurmurHash2("Test", 4)); GeomTest(); }
U32 str_hashfunc(char *str, void *ctxt) { ctxt; if(!str) return 0; return MurmurHash2(str,strlen(str),0); }
StyleRule* HtmlFormatter::FindStyleRule(HtmlTag tag, const char* clazz, size_t clazzLen) { uint32_t classHash = clazz ? MurmurHash2(clazz, clazzLen) : 0; for (size_t i = 0; i < styleRules.size(); i++) { StyleRule& rule = styleRules.at(i); if (tag == rule.tag && classHash == rule.classHash) return &rule; } return nullptr; }
static void switch_smac_rewrite_hash_key_init(uchar *key, switch_mac_addr_t *mac, uint32_t *len, uint32_t *hash) { *len=0; memset(key, 0, ETH_LEN); memcpy(key, mac, ETH_LEN); *len = ETH_LEN; *hash = MurmurHash2(key, *len, 0x98761234); }
static void switch_dmac_rewrite_hash_key_init(uchar *key, switch_mac_addr_t *mac, uint32_t *len, uint32_t *hash) { *len = 0; memset(key, 0, SWITCH_DMAC_REWRITE_HASH_KEY_SIZE); memcpy(key, mac, sizeof(switch_mac_addr_t)); *len = sizeof(switch_mac_addr_t); *hash = MurmurHash2(key, *len, 0x98761234); }
static inline void key2idxs(const uint8_t *key, size_t key_len, idx_t *idxs, int n) { int i; int hash = 0xdeadbeef; for (i = 0; i < NUM_IDXS; i+=2) { hash = MurmurHash2(key, key_len, hash); idxs[i] = hash; if (i + 1 < NUM_IDXS) idxs[i+1] = hash >> 16; } }
static void switch_neighbor_dmac_hash_key_init(uchar *key, switch_handle_t bd_handle, switch_mac_addr_t *mac, uint32_t *len, uint32_t *hash) { *len = 0; memset(key, 0, SWITCH_NEIGHBOR_DMAC_HASH_KEY_SIZE); memcpy(key, &bd_handle, sizeof(switch_handle_t)); *len += sizeof(switch_handle_t); memcpy(key + *len, mac, sizeof(switch_mac_addr_t)); *len += sizeof(switch_mac_addr_t); *hash = MurmurHash2(key, *len, 0x98761234); }
//SUMMARY // Return the cache entry with filename key 'fn' or NULL if no such entry exists //NOTES // This key will uniquely map to one cache entry due to the program flow -- // If this search keys to a cache entry and returns non-NULL, then no cache entry will be added in that line of execution struct cache_entry* hash_search(const char* const fn) { int hash = MurmurHash2(fn, FILENAME_SIZE, HASH_SEED) % max_cache_size; fprintf(stderr, "hash_search: Hash generated for filename %s: %d\n", fn, hash); struct cache_entry* ent = cache_hashtable[hash]; while (ent != NULL && strcmp(ent->filename, fn) != 0) ent = ent->next_collision; return cache_hashtable[hash]; }
ERL_NIF_TERM erlang_murmurhash2_1_impl(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifBinary bin; uint32_t h; if (!check_and_unpack_data(env, argv[0], &bin)) { return enif_make_badarg(env); } h = MurmurHash2(bin.data, bin.size, 0); return enif_make_uint(env, h); }
static HWND FindPrevInstWindow(HANDLE *hMutex) { // create a unique identifier for this executable // (allows independent side-by-side installations) ScopedMem<WCHAR> exePath(GetExePath()); str::ToLower(exePath); uint32_t hash = MurmurHash2(exePath.Get(), str::Len(exePath) * sizeof(WCHAR)); ScopedMem<WCHAR> mapId(str::Format(L"SumatraPDF-%08x", hash)); int retriesLeft = 3; Retry: // use a memory mapping containing a process id as mutex HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(DWORD), mapId); if (!hMap) goto Error; bool hasPrevInst = GetLastError() == ERROR_ALREADY_EXISTS; DWORD *procId = (DWORD *)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)); if (!procId) { CloseHandle(hMap); goto Error; } if (!hasPrevInst) { *procId = GetCurrentProcessId(); UnmapViewOfFile(procId); *hMutex = hMap; return NULL; } // if the mapping already exists, find one window belonging to the original process DWORD prevProcId = *procId; UnmapViewOfFile(procId); CloseHandle(hMap); HWND hwnd = NULL; while ((hwnd = FindWindowEx(HWND_DESKTOP, hwnd, FRAME_CLASS_NAME, NULL)) != NULL) { DWORD wndProcId; GetWindowThreadProcessId(hwnd, &wndProcId); if (wndProcId == prevProcId) { AllowSetForegroundWindow(prevProcId); return hwnd; } } // fall through Error: if (--retriesLeft < 0) return NULL; Sleep(100); goto Retry; }
UINT32 CHashedStringList::SuperFastHash (const TCHAR * data, int len) { return MurmurHash2(data, len*2, 0); /* uint32_t hash = len, tmp; int rem; if (len <= 0 || data == NULL) return 0; rem = len & 3; len >>= 2; / * Main loop * / for (;len > 0; len--) { hash += get16bits (data); tmp = (get16bits (data+2) << 11) ^ hash; hash = (hash << 16) ^ tmp; data += 2*sizeof (uint16_t); hash += hash >> 11; } / * Handle end cases * / switch (rem) { case 3: hash += get16bits (data); hash ^= hash << 16; hash ^= data[sizeof (uint16_t)] << 18; hash += hash >> 11; break; case 2: hash += get16bits (data); hash ^= hash << 11; hash += hash >> 17; break; case 1: hash += *data; hash ^= hash << 10; hash += hash >> 1; } / * Force "avalanching" of final 127 bits * / hash ^= hash << 3; hash += hash >> 5; hash ^= hash << 4; hash += hash >> 17; hash ^= hash << 25; hash += hash >> 6; return hash;*/ }
//SUMMARY // Remove cache entry 'e' from the hash table void hash_remove(struct cache_entry* e) { if (e->prev_collision == NULL) { // This is the first entry in the collision list and the one referenced by the array index int hash = MurmurHash2(e->filename, FILENAME_SIZE, HASH_SEED) % max_cache_size; fprintf(stderr, "hash_remove: Hash generated for filename %s: %d\n", e->filename, hash); if (e->next_collision == NULL) // This hash index is now empty cache_hashtable[hash] = NULL; else cache_hashtable[hash] = e->next_collision; } else { e->prev_collision->next_collision = e->next_collision; if (e->next_collision != NULL) e->next_collision->prev_collision = e->prev_collision; } }
//SUMMARY // Insert cache entry 'e' into the hash table, accounting for collisions void hash_add(struct cache_entry* e) { fprintf(stderr, "hash_add: Called\n"); e->next_collision = NULL; e->prev_collision = NULL; int hash = MurmurHash2(e->filename, FILENAME_SIZE, HASH_SEED) % max_cache_size; if (cache_hashtable[hash] == NULL) { cache_hashtable[hash] = e; } else { struct cache_entry* current = cache_hashtable[hash]; while (current->next_collision != NULL) current = current->next_collision; current->next_collision = e; e->prev_collision = current; } }
void HtmlFormatter::ParseStyleSheet(const char* data, size_t len) { CssPullParser parser(data, len); while (parser.NextRule()) { StyleRule rule = StyleRule::Parse(&parser); const CssSelector* sel; while ((sel = parser.NextSelector()) != nullptr) { if (Tag_NotFound == sel->tag) continue; StyleRule* prevRule = FindStyleRule(sel->tag, sel->clazz, sel->clazzLen); if (prevRule) { prevRule->Merge(rule); } else { rule.tag = sel->tag; rule.classHash = sel->clazz ? MurmurHash2(sel->clazz, sel->clazzLen) : 0; styleRules.Append(rule); } } } }
/* MurmurHash2 functions */ void ring_murmurhash2(void *pPointer) { char *key = NULL; int keylen; int seed = 0; uint32_t out; int ret_type = 0; if (RING_API_PARACOUNT < 2 || RING_API_PARACOUNT > 3) { RING_API_ERROR(RING_API_MISS2PARA); return ; } if (!RING_API_ISSTRING(1)) { RING_API_ERROR("murmurhash2 expects the first parameter to be a string"); return; } if (!RING_API_ISNUMBER(2)) { RING_API_ERROR("murmurhash2 expects the first parameter to be an integer"); return; } key = RING_API_GETSTRING(1); keylen = strlen(key); seed = RING_API_GETNUMBER(2); if (RING_API_PARACOUNT == 3) { if (RING_API_ISNUMBER(3)) { ret_type = RING_API_GETNUMBER(3); if (!is_bool(ret_type)) { RING_API_ERROR("Third parameter should be boolean value\n"); } } else { RING_API_ERROR("murmurhash2 expects the third parameter to be an integer\n"); } } out = MurmurHash2(key, keylen, seed); MH_RETURN_INT(out, ret_type); }
void switch_l3_hash_key_init(uchar *key, switch_handle_t vrf, switch_ip_addr_t *ip_addr, uint32_t *len, uint32_t *hash) { *len = 0; memset(key, 0, SWITCH_L3_HASH_KEY_SIZE); *(unsigned int *)(&key[0]) = (unsigned int)handle_to_id(vrf); key[4] = ip_addr->type; if (ip_addr->type == SWITCH_API_IP_ADDR_V4) { *(unsigned int *)(&key[5]) = ip_addr->ip.v4addr; *len = 9; } else { memcpy(&key[5], ip_addr->ip.v6addr, 4 * sizeof(unsigned int)); *len = 21; } key[*len] = ip_addr->prefix_len; (*len)++; *hash = MurmurHash2(key, *len, 0x98761234); }
static ssize_t murmur2_32_handler(machine_t *machine, request_t *request){ // {{{ uint32_t hash = 0; data_t *key = NULL; murmur_userdata *userdata = (murmur_userdata *)machine->userdata; key = hash_data_find(request, userdata->input); if(key == NULL){ if(userdata->fatal == 0) return machine_pass(machine, request); return error("input key not supplied"); } hash = MurmurHash2(key, 0); request_t r_next[] = { { userdata->output, DATA_UINT32T(hash) }, hash_next(request) }; return machine_pass(machine, r_next); } // }}}
static inline void switch_nhop_hash_key_init(uchar *key, switch_nhop_key_t *nhop_key, uint32_t *len, uint32_t *hash) { *len=0; memset(key, 0, SWITCH_NHOP_HASH_KEY_SIZE); memcpy(key, (uchar *) &nhop_key->intf_handle, sizeof(switch_handle_t)); *len += sizeof(switch_handle_t); key[*len] = nhop_key->ip_addr.type; *len += 4; if(nhop_key->ip_addr.type == SWITCH_API_IP_ADDR_V4) { *(unsigned int *)(&key[*len]) = nhop_key->ip_addr.ip.v4addr; *len += 16; } else { memcpy(&key[*len], nhop_key->ip_addr.ip.v6addr, 16); *len += 16; } key[*len] = nhop_key->ip_addr.prefix_len; *len += 4; *hash = MurmurHash2(key, *len, 0x98761234); }
static uint32_t jsonlite_hash(const uint8_t *data, size_t len) { return MurmurHash2(data, len); }
int main(void) { const unsigned int experiments = 50; const unsigned int hashDataSetTimes = 1000000; const unsigned int hashes = 7; TimeMeasurement measurement[hashes][experiments]; TimeResult results[hashes]; uint32_t testIP = 0; uint32_t testOutput = 0; // INITIALIZATION results[0].identifier = "MurmurHash3"; results[1].identifier = "IPHash 1"; results[2].identifier = "IPHash 2"; results[3].identifier = "MurmurHash2"; results[4].identifier = "SpookyHash"; results[5].identifier = "lookup3"; results[6].identifier = "SuperFastHash"; // EXPERIMENTS for (unsigned int experiment = 0; experiment < experiments; experiment++) { // MURMURHASH3 measurement[0][experiment].start = std::chrono::system_clock::now(); for (unsigned int i = 0; i < hashDataSetTimes; i++) { for (unsigned int ipc = 0; ipc < 100; ipc++) { for (unsigned int octet = 0; octet < 4; octet++) { testIP += octets_dataset[ipc][octet]; testIP = testIP << 8; } MurmurHash3_x86_32(&testIP, 32, 1, &testOutput); } } measurement[0][experiment].end = std::chrono::system_clock::now(); measurement[0][experiment].elapsed_seconds = measurement[0][experiment].end-measurement[0][experiment].start; // IPHASH1 measurement[1][experiment].start = std::chrono::system_clock::now(); for (unsigned int i = 0; i < hashDataSetTimes; i++) { for (unsigned int ipc = 0; ipc < 100; ipc++) { hash1 ( octets_dataset[ipc][0] , octets_dataset[ipc][1] , octets_dataset[ipc][2] , octets_dataset[ipc][3] ); } } measurement[1][experiment].end = std::chrono::system_clock::now(); measurement[1][experiment].elapsed_seconds = measurement[1][experiment].end-measurement[1][experiment].start; // IPHASH2 measurement[2][experiment].start = std::chrono::system_clock::now(); for (unsigned int i = 0; i < hashDataSetTimes; i++) { for (unsigned int ipc = 0; ipc < 100; ipc++) { hash2 ( octets_dataset[ipc][0] , octets_dataset[ipc][1] , octets_dataset[ipc][2] , octets_dataset[ipc][3] ); } } measurement[2][experiment].end = std::chrono::system_clock::now(); measurement[2][experiment].elapsed_seconds = measurement[2][experiment].end-measurement[2][experiment].start; // MURMURHASH2 measurement[3][experiment].start = std::chrono::system_clock::now(); for (unsigned int i = 0; i < hashDataSetTimes; i++) { for (unsigned int ipc = 0; ipc < 100; ipc++) { for (unsigned int octet = 0; octet < 4; octet++) { testIP += octets_dataset[ipc][octet]; testIP = testIP << 8; } MurmurHash2(&testIP, 32, 1); } } measurement[3][experiment].end = std::chrono::system_clock::now(); measurement[3][experiment].elapsed_seconds = measurement[3][experiment].end-measurement[3][experiment].start; // SPOOKYHASH measurement[4][experiment].start = std::chrono::system_clock::now(); for (unsigned int i = 0; i < hashDataSetTimes; i++) { for (unsigned int ipc = 0; ipc < 100; ipc++) { for (unsigned int octet = 0; octet < 4; octet++) { testIP += octets_dataset[ipc][octet]; testIP = testIP << 8; } SpookyHash::Hash32(&testIP, 32, 1); } } measurement[4][experiment].end = std::chrono::system_clock::now(); measurement[4][experiment].elapsed_seconds = measurement[4][experiment].end-measurement[4][experiment].start; // lookup3 measurement[5][experiment].start = std::chrono::system_clock::now(); for (unsigned int i = 0; i < hashDataSetTimes; i++) { for (unsigned int ipc = 0; ipc < 100; ipc++) { for (unsigned int octet = 0; octet < 4; octet++) { testIP += octets_dataset[ipc][octet]; testIP = testIP << 8; } lookup3(&testIP, 32, 1); } } measurement[5][experiment].end = std::chrono::system_clock::now(); measurement[5][experiment].elapsed_seconds = measurement[5][experiment].end-measurement[5][experiment].start; // SuperFastHash measurement[6][experiment].start = std::chrono::system_clock::now(); for (unsigned int i = 0; i < hashDataSetTimes; i++) { for (unsigned int ipc = 0; ipc < 100; ipc++) { for (unsigned int octet = 0; octet < 4; octet++) { testIP += octets_dataset[ipc][octet]; testIP = testIP << 8; } SuperFastHash(&testIP, 32, 1, &testOutput); } } measurement[6][experiment].end = std::chrono::system_clock::now(); measurement[6][experiment].elapsed_seconds = measurement[6][experiment].end-measurement[6][experiment].start; } for (unsigned int hash = 0; hash < hashes; hash++) { for (unsigned int experiment = 0; experiment < experiments; experiment++) { results[hash].elapsed_avg_seconds += measurement[hash][experiment].elapsed_seconds.count(); } results[hash].elapsed_avg_seconds /= experiments; } for (unsigned int mes = 0; mes < hashes; mes++) { printMeasurementResults(&results[mes]); } return 0; }
uint32_t murmur_hash::operator() (const void *key, uint32_t sz, uint32_t seed) const { return MurmurHash2(key, sz, seed); }
size_t protocol_hash::operator()(const protocol_key& pk) const { return MurmurHash2(&pk, sizeof(pk), 0xee6b27eb); }
void MurmurHash2Test::constructor() { /* All should give the same value */ CORRADE_COMPARE(MurmurHash2()("hello"), MurmurHash2()("hello", 5)); CORRADE_COMPARE(MurmurHash2()(std::string("hello")), MurmurHash2()("hello", 5)); }
CError CModuleManager::extract_implementation( unzFile unzip_file, const CInterfaceDefinition &interface_definition, unsigned int &checksum ) { assert( unzip_file ); checksum = 0; string implementation_directory = get_implementation_path( interface_definition ); if( CFileHelper::is_directory( implementation_directory.c_str() ) ) { INTEGRA_TRACE_ERROR << "Can't extract module implementation - target directory already exists: " << implementation_directory; return CError::FAILED; } mkdir( implementation_directory.c_str() ); if( unzGoToFirstFile( unzip_file ) != UNZ_OK ) { INTEGRA_TRACE_ERROR << "Couldn't iterate contents"; return CError::FAILED; } do { unz_file_info file_info; char file_name_buffer[ CStringHelper::string_buffer_length ]; if( unzGetCurrentFileInfo( unzip_file, &file_info, file_name_buffer, CStringHelper::string_buffer_length, NULL, 0, NULL, 0 ) != UNZ_OK ) { INTEGRA_TRACE_ERROR << "Couldn't extract file info"; continue; } string file_name( file_name_buffer ); if( file_name.substr( 0, internal_implementation_directory_name.length() ) != internal_implementation_directory_name ) { /* skip files not in NTG_INTERNAL_IMPLEMENTATION_DIRECTORY_NAME */ continue; } if( file_name.back() == CFileIO::path_separator ) { /* skip directories */ continue; } string relative_file_path = file_name.substr( internal_implementation_directory_name.length() ); CFileHelper::construct_subdirectories( implementation_directory, relative_file_path ); string target_path = implementation_directory + relative_file_path; checksum ^= MurmurHash2( relative_file_path.c_str(), relative_file_path.length(), checksum_seed ); if( unzOpenCurrentFile( unzip_file ) == UNZ_OK ) { FILE *output_file = fopen( target_path.c_str(), "wb" ); if( output_file ) { unsigned char *output_buffer = new unsigned char[ file_info.uncompressed_size ]; if( unzReadCurrentFile( unzip_file, output_buffer, file_info.uncompressed_size ) != file_info.uncompressed_size ) { INTEGRA_TRACE_ERROR << "Error decompressing file: " << file_name; } else { checksum ^= MurmurHash2( output_buffer, file_info.uncompressed_size, checksum_seed ); fwrite( output_buffer, 1, file_info.uncompressed_size, output_file ); } delete[] output_buffer; fclose( output_file ); } else { INTEGRA_TRACE_ERROR << "Couldn't write to implementation file: " << target_path; } unzCloseCurrentFile( unzip_file ); } else { INTEGRA_TRACE_ERROR << "couldn't open zip contents: " << file_name; } } while( unzGoToNextFile( unzip_file ) != UNZ_END_OF_LIST_OF_FILE ); return CError::SUCCESS; }
U32 int_hashfunc(char *key, void *ctxt) { ctxt; return MurmurHash2(key,sizeof(int),0); }
uint32_t ape_hash_str(const void *key, int len) { return MurmurHash2(key, len, _ape_seed) % HACH_TABLE_MAX; }
U32 ptr_hashfunc(char *key, void *ctxt) { ctxt; return MurmurHash2((char*)&key,sizeof(void*),0); }
uint32_t tbl_hash(char* key) { return (uint32_t) MurmurHash2((void*) key, strlen(key), 42); }