2020-10-13 19:42:30 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
WINTUN_STATUS
|
2020-10-14 08:29:01 +02:00
|
|
|
ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ const VOID **Address, _Out_ DWORD *Size)
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
|
|
|
HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA);
|
|
|
|
if (!FoundResource)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_LAST_ERROR(L"Failed to find resource");
|
2020-10-14 08:29:01 +02:00
|
|
|
*Size = SizeofResource(ResourceModule, FoundResource);
|
|
|
|
if (!*Size)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_LAST_ERROR(L"Failed to size resource");
|
2020-10-13 19:42:30 +02:00
|
|
|
HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource);
|
|
|
|
if (!LoadedResource)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_LAST_ERROR(L"Failed to load resource");
|
2020-10-14 08:29:01 +02:00
|
|
|
*Address = LockResource(LoadedResource);
|
|
|
|
if (!*Address)
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Failed to lock resource");
|
2020-10-13 19:42:30 +02:00
|
|
|
return ERROR_LOCK_FAILED;
|
|
|
|
}
|
2020-10-14 08:29:01 +02:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
WINTUN_STATUS
|
2020-10-15 15:34:31 +02:00
|
|
|
ResourceCopyToFile(_In_z_ const WCHAR *DestinationPath, _In_z_ const WCHAR *ResourceName)
|
2020-10-14 08:29:01 +02:00
|
|
|
{
|
|
|
|
const VOID *LockedResource;
|
|
|
|
DWORD SizeResource;
|
|
|
|
DWORD Result = ResourceGetAddress(ResourceName, &LockedResource, &SizeResource);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
2020-10-15 12:21:55 +02:00
|
|
|
return LOG(WINTUN_LOG_ERR, L"Failed to locate resource"), Result;
|
2020-10-13 19:42:30 +02:00
|
|
|
HANDLE DestinationHandle = CreateFileW(
|
|
|
|
DestinationPath,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
0,
|
|
|
|
SecurityAttributes,
|
|
|
|
CREATE_NEW,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY,
|
|
|
|
NULL);
|
|
|
|
if (DestinationHandle == INVALID_HANDLE_VALUE)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_LAST_ERROR(L"Failed to create file");
|
2020-10-13 19:42:30 +02:00
|
|
|
DWORD BytesWritten;
|
|
|
|
if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL))
|
2020-10-14 13:04:29 +02:00
|
|
|
Result = LOG_LAST_ERROR(L"Failed to write file");
|
2020-10-13 19:42:30 +02:00
|
|
|
if (BytesWritten != SizeResource)
|
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Incomplete write");
|
2020-10-13 19:42:30 +02:00
|
|
|
Result = Result != ERROR_SUCCESS ? Result : ERROR_WRITE_FAULT;
|
|
|
|
}
|
|
|
|
CloseHandle(DestinationHandle);
|
|
|
|
return Result;
|
|
|
|
}
|