tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CityHash.cpp (1984B)


      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/. */
      5 
      6 #include "CityHash.h"
      7 #include "cityhash/city.h"
      8 #include <tchar.h>
      9 
     10 #define MAX_STRLEN 1024
     11 
     12 typedef struct _stack_t {
     13  struct _stack_t *next;
     14  TCHAR text[MAX_STRLEN];
     15 } stack_t;
     16 
     17 stack_t **g_stacktop;
     18 char *g_variables;
     19 
     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 }
     34 
     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 }
     45 
     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 }
     56 
     57 extern "C"
     58 {
     59 
     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') };
     64 
     65  g_stacktop = stacktop;
     66  g_variables = variables;
     67 
     68  memset(hashString, 0, sizeof(hashString));
     69  memset(hexResult, 0, sizeof(hexResult));
     70 
     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 }
     81 
     82 }