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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/nsis/Contrib/CityHash/CityHash.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "CityHash.h"
    1.10 +#include "cityhash/city.h"
    1.11 +#include <tchar.h>
    1.12 +
    1.13 +#define MAX_STRLEN 1024
    1.14 +
    1.15 +typedef struct _stack_t {
    1.16 +  struct _stack_t *next;
    1.17 +  TCHAR text[MAX_STRLEN];
    1.18 +} stack_t;
    1.19 +
    1.20 +stack_t **g_stacktop;
    1.21 +char *g_variables;
    1.22 +
    1.23 +BOOL APIENTRY DllMain(HMODULE hModule,
    1.24 +                      DWORD  ul_reason_for_call,
    1.25 +                      LPVOID lpReserved)
    1.26 +{
    1.27 +  switch (ul_reason_for_call)
    1.28 +  {
    1.29 +  case DLL_PROCESS_ATTACH:
    1.30 +  case DLL_THREAD_ATTACH:
    1.31 +  case DLL_THREAD_DETACH:
    1.32 +  case DLL_PROCESS_DETACH:
    1.33 +    break;
    1.34 +  }
    1.35 +  return TRUE;
    1.36 +}
    1.37 +
    1.38 +bool popString(TCHAR *result)
    1.39 +{
    1.40 +  stack_t *th;
    1.41 +  if (!g_stacktop || !*g_stacktop) return false;
    1.42 +  th = (*g_stacktop);
    1.43 +  lstrcpyn(result, th->text, MAX_STRLEN);
    1.44 +  *g_stacktop = th->next;
    1.45 +  GlobalFree((HGLOBAL)th);
    1.46 +  return true;
    1.47 +}
    1.48 +
    1.49 +void pushString(const TCHAR *str)
    1.50 +{
    1.51 +  stack_t *th;
    1.52 +  int strLen = wcslen(str)+1;
    1.53 +  if (!g_stacktop) return;
    1.54 +  th = (stack_t*)GlobalAlloc(GPTR, sizeof(stack_t) + (MAX_STRLEN*sizeof(TCHAR)));
    1.55 +  lstrcpyn(th->text, str, strLen);
    1.56 +  th->next = *g_stacktop;
    1.57 +  *g_stacktop = th;
    1.58 +}
    1.59 +
    1.60 +extern "C"
    1.61 +{
    1.62 +
    1.63 +void GetCityHash64(HWND hwndParent, int string_size, char *variables, stack_t **stacktop)
    1.64 +{
    1.65 +  TCHAR hashString[MAX_STRLEN];
    1.66 +  TCHAR hexResult[18] = { _T('\0') };
    1.67 +
    1.68 +  g_stacktop = stacktop;
    1.69 +  g_variables = variables;
    1.70 +
    1.71 +  memset(hashString, 0, sizeof(hashString));
    1.72 +  memset(hexResult, 0, sizeof(hexResult));
    1.73 +
    1.74 +  if (!popString(hashString)) {
    1.75 +    pushString(L"error");
    1.76 +    return;
    1.77 +  }
    1.78 +  uint64 result = CityHash64((const char*)&hashString[0], wcslen(hashString)*sizeof(TCHAR));
    1.79 +  // If the hash happens to work out to less than 16 hash digits it will just
    1.80 +  // use less of the buffer.
    1.81 +  swprintf(hexResult, L"%I64X", result);
    1.82 +  pushString(hexResult);
    1.83 +}
    1.84 +
    1.85 +}

mercurial