api: opencode bin2hex in same manner as random directory creation
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
62f21f2fdd
commit
26c5478042
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <bcrypt.h>
|
#include <bcrypt.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
static BOOL HasInitialized = FALSE;
|
static BOOL HasInitialized = FALSE;
|
||||||
static CRITICAL_SECTION Initializing;
|
static CRITICAL_SECTION Initializing;
|
||||||
@ -34,17 +35,6 @@ NormalizeStringAlloc(_In_ NORM_FORM NormForm, _In_z_ const WCHAR *Source)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
Bin2Hex(_In_bytecount_(Size) const void *Source, size_t Size, _Out_capcount_(Size * 2) WCHAR *Destination)
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < Size; ++i)
|
|
||||||
{
|
|
||||||
static const WCHAR nibble[] = L"0123456789ABCDEF";
|
|
||||||
*(Destination++) = nibble[(((unsigned char *)Source)[i] & 0xf0) >> 4];
|
|
||||||
*(Destination++) = nibble[(((unsigned char *)Source)[i] & 0x0f)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static WINTUN_STATUS
|
static WINTUN_STATUS
|
||||||
NamespaceRuntimeInit(void)
|
NamespaceRuntimeInit(void)
|
||||||
{
|
{
|
||||||
@ -134,10 +124,10 @@ NamespaceTakeMutex(_In_z_ const WCHAR *Pool)
|
|||||||
if (!BCRYPT_SUCCESS(BCryptFinishHash(Sha256, Hash, sizeof(Hash), 0)))
|
if (!BCRYPT_SUCCESS(BCryptFinishHash(Sha256, Hash, sizeof(Hash), 0)))
|
||||||
goto cleanupPoolNorm;
|
goto cleanupPoolNorm;
|
||||||
static const WCHAR MutexNamePrefix[] = L"Wintun\\Wintun-Name-Mutex-";
|
static const WCHAR MutexNamePrefix[] = L"Wintun\\Wintun-Name-Mutex-";
|
||||||
WCHAR MutexName[_countof(MutexNamePrefix) /*<= incl. terminator*/ + sizeof(Hash) * 2];
|
WCHAR MutexName[_countof(MutexNamePrefix) + sizeof(Hash) * 2];
|
||||||
memcpy(MutexName, MutexNamePrefix, sizeof(MutexNamePrefix) - sizeof(WCHAR));
|
memcpy(MutexName, MutexNamePrefix, sizeof(MutexNamePrefix));
|
||||||
Bin2Hex(Hash, sizeof(Hash), MutexName + _countof(MutexNamePrefix) - 1);
|
for (size_t i = 0; i < sizeof(Hash); ++i)
|
||||||
MutexName[_countof(MutexName) - 1] = 0;
|
swprintf_s(&MutexName[_countof(MutexNamePrefix) - 1 + i * 2], 3, L"%02x", Hash[i]);
|
||||||
Mutex = CreateMutexW(&SecurityAttributes, FALSE, MutexName);
|
Mutex = CreateMutexW(&SecurityAttributes, FALSE, MutexName);
|
||||||
if (!Mutex)
|
if (!Mutex)
|
||||||
goto cleanupPoolNorm;
|
goto cleanupPoolNorm;
|
||||||
|
Loading…
Reference in New Issue
Block a user