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: #include "CityHash.h" michael@0: #include "cityhash/city.h" michael@0: #include michael@0: michael@0: #define MAX_STRLEN 1024 michael@0: michael@0: typedef struct _stack_t { michael@0: struct _stack_t *next; michael@0: TCHAR text[MAX_STRLEN]; michael@0: } stack_t; michael@0: michael@0: stack_t **g_stacktop; michael@0: char *g_variables; michael@0: michael@0: BOOL APIENTRY DllMain(HMODULE hModule, michael@0: DWORD ul_reason_for_call, michael@0: LPVOID lpReserved) michael@0: { michael@0: switch (ul_reason_for_call) michael@0: { michael@0: case DLL_PROCESS_ATTACH: michael@0: case DLL_THREAD_ATTACH: michael@0: case DLL_THREAD_DETACH: michael@0: case DLL_PROCESS_DETACH: michael@0: break; michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: bool popString(TCHAR *result) michael@0: { michael@0: stack_t *th; michael@0: if (!g_stacktop || !*g_stacktop) return false; michael@0: th = (*g_stacktop); michael@0: lstrcpyn(result, th->text, MAX_STRLEN); michael@0: *g_stacktop = th->next; michael@0: GlobalFree((HGLOBAL)th); michael@0: return true; michael@0: } michael@0: michael@0: void pushString(const TCHAR *str) michael@0: { michael@0: stack_t *th; michael@0: int strLen = wcslen(str)+1; michael@0: if (!g_stacktop) return; michael@0: th = (stack_t*)GlobalAlloc(GPTR, sizeof(stack_t) + (MAX_STRLEN*sizeof(TCHAR))); michael@0: lstrcpyn(th->text, str, strLen); michael@0: th->next = *g_stacktop; michael@0: *g_stacktop = th; michael@0: } michael@0: michael@0: extern "C" michael@0: { michael@0: michael@0: void GetCityHash64(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) michael@0: { michael@0: TCHAR hashString[MAX_STRLEN]; michael@0: TCHAR hexResult[18] = { _T('\0') }; michael@0: michael@0: g_stacktop = stacktop; michael@0: g_variables = variables; michael@0: michael@0: memset(hashString, 0, sizeof(hashString)); michael@0: memset(hexResult, 0, sizeof(hexResult)); michael@0: michael@0: if (!popString(hashString)) { michael@0: pushString(L"error"); michael@0: return; michael@0: } michael@0: uint64 result = CityHash64((const char*)&hashString[0], wcslen(hashString)*sizeof(TCHAR)); michael@0: // If the hash happens to work out to less than 16 hash digits it will just michael@0: // use less of the buffer. michael@0: swprintf(hexResult, L"%I64X", result); michael@0: pushString(hexResult); michael@0: } michael@0: michael@0: }