aboutsummaryrefslogtreecommitdiffstats
path: root/api/resource.c
diff options
context:
space:
mode:
authorSimon Rozman <simon@rozman.si>2021-07-28 20:20:09 +0200
committerSimon Rozman <simon@rozman.si>2021-07-28 20:25:27 +0200
commitd675646ab8df227ba39c9d0160d0ba77e8f85479 (patch)
treed6138df6a8b10083de89c644261116abd64b46d8 /api/resource.c
parentexample: resolve signed/unsigned code analysis warning (diff)
downloadwintun-d675646ab8df227ba39c9d0160d0ba77e8f85479.tar.xz
wintun-d675646ab8df227ba39c9d0160d0ba77e8f85479.zip
api: upgrade
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'api/resource.c')
-rw-r--r--api/resource.c53
1 files changed, 48 insertions, 5 deletions
diff --git a/api/resource.c b/api/resource.c
index ce590e1..648f568 100644
--- a/api/resource.c
+++ b/api/resource.c
@@ -7,9 +7,12 @@
#include "main.h"
#include "resource.h"
#include <Windows.h>
+#include <Shlwapi.h>
+#include <NTSecAPI.h>
-_Return_type_success_(return != NULL) _Ret_bytecount_(*Size) const
- void *ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ DWORD *Size)
+_Use_decl_annotations_
+const VOID *
+ResourceGetAddress(LPCWSTR ResourceName, DWORD *Size)
{
HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA);
if (!FoundResource)
@@ -39,11 +42,12 @@ _Return_type_success_(return != NULL) _Ret_bytecount_(*Size) const
return Address;
}
-_Return_type_success_(return != FALSE) BOOL
- ResourceCopyToFile(_In_z_ const WCHAR *DestinationPath, _In_z_ const WCHAR *ResourceName)
+_Use_decl_annotations_
+BOOL
+ResourceCopyToFile(LPCWSTR DestinationPath, LPCWSTR ResourceName)
{
DWORD SizeResource;
- const void *LockedResource = ResourceGetAddress(ResourceName, &SizeResource);
+ const VOID *LockedResource = ResourceGetAddress(ResourceName, &SizeResource);
if (!LockedResource)
{
LOG(WINTUN_LOG_ERR, L"Failed to locate resource %s", ResourceName);
@@ -84,3 +88,42 @@ cleanupDestinationHandle:
CloseHandle(DestinationHandle);
return RET_ERROR(TRUE, LastError);
}
+
+_Return_type_success_(return != FALSE)
+BOOL
+ResourceCreateTemporaryDirectory(_Out_writes_z_(MAX_PATH) LPWSTR RandomTempSubDirectory)
+{
+ WCHAR WindowsDirectory[MAX_PATH];
+ if (!GetWindowsDirectoryW(WindowsDirectory, _countof(WindowsDirectory)))
+ {
+ LOG_LAST_ERROR(L"Failed to get Windows folder");
+ return FALSE;
+ }
+ WCHAR WindowsTempDirectory[MAX_PATH];
+ if (!PathCombineW(WindowsTempDirectory, WindowsDirectory, L"Temp"))
+ {
+ SetLastError(ERROR_BUFFER_OVERFLOW);
+ return FALSE;
+ }
+ UCHAR RandomBytes[32] = { 0 };
+ if (!RtlGenRandom(RandomBytes, sizeof(RandomBytes)))
+ {
+ LOG(WINTUN_LOG_ERR, L"Failed to generate random");
+ SetLastError(ERROR_GEN_FAILURE);
+ return FALSE;
+ }
+ WCHAR RandomSubDirectory[sizeof(RandomBytes) * 2 + 1];
+ for (int i = 0; i < sizeof(RandomBytes); ++i)
+ swprintf_s(&RandomSubDirectory[i * 2], 3, L"%02x", RandomBytes[i]);
+ if (!PathCombineW(RandomTempSubDirectory, WindowsTempDirectory, RandomSubDirectory))
+ {
+ SetLastError(ERROR_BUFFER_OVERFLOW);
+ return FALSE;
+ }
+ if (!CreateDirectoryW(RandomTempSubDirectory, &SecurityAttributes))
+ {
+ LOG_LAST_ERROR(L"Failed to create temporary folder %s", RandomTempSubDirectory);
+ return FALSE;
+ }
+ return TRUE;
+}