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

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial