other-licenses/nsis/Contrib/CityHash/CityHash.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "CityHash.h"
michael@0 7 #include "cityhash/city.h"
michael@0 8 #include <tchar.h>
michael@0 9
michael@0 10 #define MAX_STRLEN 1024
michael@0 11
michael@0 12 typedef struct _stack_t {
michael@0 13 struct _stack_t *next;
michael@0 14 TCHAR text[MAX_STRLEN];
michael@0 15 } stack_t;
michael@0 16
michael@0 17 stack_t **g_stacktop;
michael@0 18 char *g_variables;
michael@0 19
michael@0 20 BOOL APIENTRY DllMain(HMODULE hModule,
michael@0 21 DWORD ul_reason_for_call,
michael@0 22 LPVOID lpReserved)
michael@0 23 {
michael@0 24 switch (ul_reason_for_call)
michael@0 25 {
michael@0 26 case DLL_PROCESS_ATTACH:
michael@0 27 case DLL_THREAD_ATTACH:
michael@0 28 case DLL_THREAD_DETACH:
michael@0 29 case DLL_PROCESS_DETACH:
michael@0 30 break;
michael@0 31 }
michael@0 32 return TRUE;
michael@0 33 }
michael@0 34
michael@0 35 bool popString(TCHAR *result)
michael@0 36 {
michael@0 37 stack_t *th;
michael@0 38 if (!g_stacktop || !*g_stacktop) return false;
michael@0 39 th = (*g_stacktop);
michael@0 40 lstrcpyn(result, th->text, MAX_STRLEN);
michael@0 41 *g_stacktop = th->next;
michael@0 42 GlobalFree((HGLOBAL)th);
michael@0 43 return true;
michael@0 44 }
michael@0 45
michael@0 46 void pushString(const TCHAR *str)
michael@0 47 {
michael@0 48 stack_t *th;
michael@0 49 int strLen = wcslen(str)+1;
michael@0 50 if (!g_stacktop) return;
michael@0 51 th = (stack_t*)GlobalAlloc(GPTR, sizeof(stack_t) + (MAX_STRLEN*sizeof(TCHAR)));
michael@0 52 lstrcpyn(th->text, str, strLen);
michael@0 53 th->next = *g_stacktop;
michael@0 54 *g_stacktop = th;
michael@0 55 }
michael@0 56
michael@0 57 extern "C"
michael@0 58 {
michael@0 59
michael@0 60 void GetCityHash64(HWND hwndParent, int string_size, char *variables, stack_t **stacktop)
michael@0 61 {
michael@0 62 TCHAR hashString[MAX_STRLEN];
michael@0 63 TCHAR hexResult[18] = { _T('\0') };
michael@0 64
michael@0 65 g_stacktop = stacktop;
michael@0 66 g_variables = variables;
michael@0 67
michael@0 68 memset(hashString, 0, sizeof(hashString));
michael@0 69 memset(hexResult, 0, sizeof(hexResult));
michael@0 70
michael@0 71 if (!popString(hashString)) {
michael@0 72 pushString(L"error");
michael@0 73 return;
michael@0 74 }
michael@0 75 uint64 result = CityHash64((const char*)&hashString[0], wcslen(hashString)*sizeof(TCHAR));
michael@0 76 // If the hash happens to work out to less than 16 hash digits it will just
michael@0 77 // use less of the buffer.
michael@0 78 swprintf(hexResult, L"%I64X", result);
michael@0 79 pushString(hexResult);
michael@0 80 }
michael@0 81
michael@0 82 }

mercurial