aboutsummaryrefslogtreecommitdiffstats
path: root/api/resource.c
diff options
context:
space:
mode:
authorSimon Rozman <simon@rozman.si>2020-10-13 19:42:30 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2020-10-30 16:50:59 +0100
commitc324d07ffb6c7dc2a59ad968a6d9322af1a446e2 (patch)
tree36fb32ed73b6ceb1154f99af888cbbb7288ede2f /api/resource.c
parentapi: introduce logging (diff)
downloadwintun-c324d07ffb6c7dc2a59ad968a6d9322af1a446e2.tar.xz
wintun-c324d07ffb6c7dc2a59ad968a6d9322af1a446e2.zip
api: add driver management
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'api/resource.c')
-rw-r--r--api/resource.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/api/resource.c b/api/resource.c
new file mode 100644
index 0000000..b25b96d
--- /dev/null
+++ b/api/resource.c
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
+ */
+
+#include "pch.h"
+
+WINTUN_STATUS
+CopyResource(
+ _In_z_ const WCHAR *DestinationPath,
+ _In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes,
+ _In_z_ const WCHAR *ResourceName)
+{
+ HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA);
+ if (!FoundResource)
+ return WINTUN_LOGGER_LAST_ERROR(L"Failed to find resource");
+ DWORD SizeResource = SizeofResource(ResourceModule, FoundResource);
+ if (!SizeResource)
+ return WINTUN_LOGGER_LAST_ERROR(L"Failed to size resource");
+ HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource);
+ if (!LoadedResource)
+ return WINTUN_LOGGER_LAST_ERROR(L"Failed to load resource");
+ LPVOID LockedResource = LockResource(LoadedResource);
+ if (!LockedResource)
+ {
+ WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to lock resource");
+ return ERROR_LOCK_FAILED;
+ }
+ HANDLE DestinationHandle = CreateFileW(
+ DestinationPath,
+ GENERIC_WRITE,
+ 0,
+ SecurityAttributes,
+ CREATE_NEW,
+ FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY,
+ NULL);
+ if (DestinationHandle == INVALID_HANDLE_VALUE)
+ return WINTUN_LOGGER_LAST_ERROR(L"Failed to create file");
+ DWORD BytesWritten;
+ DWORD Result = ERROR_SUCCESS;
+ if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL))
+ Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to write file");
+ if (BytesWritten != SizeResource)
+ {
+ WINTUN_LOGGER(WINTUN_LOG_ERR, L"Incomplete write");
+ Result = Result != ERROR_SUCCESS ? Result : ERROR_WRITE_FAULT;
+ }
+ CloseHandle(DestinationHandle);
+ return Result;
+}