2020-10-13 19:42:30 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
2021-01-30 16:45:26 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2020-10-13 19:42:30 +02:00
|
|
|
*/
|
|
|
|
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "logger.h"
|
2021-07-28 13:50:40 +02:00
|
|
|
#include "main.h"
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "resource.h"
|
|
|
|
#include <Windows.h>
|
2020-10-13 19:42:30 +02:00
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != NULL) _Ret_bytecount_(*Size) const
|
|
|
|
void *ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ DWORD *Size)
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
|
|
|
HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA);
|
|
|
|
if (!FoundResource)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to find resource %s", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-14 08:29:01 +02:00
|
|
|
*Size = SizeofResource(ResourceModule, FoundResource);
|
|
|
|
if (!*Size)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to query resource %s size", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-13 19:42:30 +02:00
|
|
|
HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource);
|
|
|
|
if (!LoadedResource)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to load resource %s", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
BYTE *Address = LockResource(LoadedResource);
|
|
|
|
if (!Address)
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Failed to lock resource %s", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(ERROR_LOCK_FAILED);
|
|
|
|
return NULL;
|
2020-10-13 19:42:30 +02:00
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
return Address;
|
2020-10-14 08:29:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != FALSE) BOOL
|
|
|
|
ResourceCopyToFile(_In_z_ const WCHAR *DestinationPath, _In_z_ const WCHAR *ResourceName)
|
2020-10-14 08:29:01 +02:00
|
|
|
{
|
|
|
|
DWORD SizeResource;
|
2020-11-03 12:29:34 +01:00
|
|
|
const void *LockedResource = ResourceGetAddress(ResourceName, &SizeResource);
|
|
|
|
if (!LockedResource)
|
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Failed to locate resource %s", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2020-10-13 19:42:30 +02:00
|
|
|
HANDLE DestinationHandle = CreateFileW(
|
|
|
|
DestinationPath,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
0,
|
2020-10-30 13:26:36 +01:00
|
|
|
&SecurityAttributes,
|
2020-10-13 19:42:30 +02:00
|
|
|
CREATE_NEW,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY,
|
|
|
|
NULL);
|
|
|
|
if (DestinationHandle == INVALID_HANDLE_VALUE)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to create file %s", DestinationPath);
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2020-10-13 19:42:30 +02:00
|
|
|
DWORD BytesWritten;
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-10-13 19:42:30 +02:00
|
|
|
if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL))
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LastError = LOG_LAST_ERROR(L"Failed to write file %s", DestinationPath);
|
2020-11-03 12:29:34 +01:00
|
|
|
goto cleanupDestinationHandle;
|
|
|
|
}
|
2020-10-13 19:42:30 +02:00
|
|
|
if (BytesWritten != SizeResource)
|
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG(WINTUN_LOG_ERR,
|
|
|
|
L"Incomplete write to %s (written: %u, expected: %u)",
|
|
|
|
DestinationPath,
|
|
|
|
BytesWritten,
|
|
|
|
SizeResource);
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_WRITE_FAULT;
|
|
|
|
goto cleanupDestinationHandle;
|
2020-10-13 19:42:30 +02:00
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_SUCCESS;
|
|
|
|
cleanupDestinationHandle:
|
2020-10-13 19:42:30 +02:00
|
|
|
CloseHandle(DestinationHandle);
|
2020-11-03 12:29:34 +01:00
|
|
|
return RET_ERROR(TRUE, LastError);
|
2020-10-13 19:42:30 +02:00
|
|
|
}
|