-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
69 lines (58 loc) · 1.67 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @file utils.cpp
*
* @copyright 2021-2022 extratype
*/
#include "utils.hpp"
namespace chunkdisk
{
DWORD ConvertUTF8(const u8* text, const int size, std::wstring& result)
{
try
{
auto wchar_size = MultiByteToWideChar(
CP_UTF8, 0,
recast<LPCCH>(text), size, nullptr, 0);
if (!wchar_size) return GetLastError();
auto wbuf = std::unique_ptr<WCHAR[]>(new WCHAR[wchar_size + 1]());
wchar_size = MultiByteToWideChar(
CP_UTF8, 0,
recast<LPCCH>(text), size, wbuf.get(), wchar_size + 1);
if (!wchar_size) return GetLastError();
if (wbuf[wchar_size - 1] == '\0') wchar_size -= 1;
result.append(wbuf.get(), wchar_size);
return ERROR_SUCCESS;
}
catch (const std::bad_alloc&)
{
return ERROR_NOT_ENOUGH_MEMORY;
}
}
DWORD GetThreadCount(PDWORD ThreadCount)
{
DWORD Result;
DWORD_PTR ProcessMask, SystemMask;
if (!GetProcessAffinityMask(GetCurrentProcess(), &ProcessMask, &SystemMask)) return GetLastError();
for (Result = 0; 0 != ProcessMask; ProcessMask >>= 1) Result += ProcessMask & 1;
*ThreadCount = Result;
return ERROR_SUCCESS;
}
u64 GetSystemFileTime()
{
u64 ft;
GetSystemTimeAsFileTime(recast<LPFILETIME>(&ft));
return ft;
}
void SetScsiError(SPD_IOCTL_STORAGE_UNIT_STATUS* status, u8 sense_key, u8 asc)
{
status->ScsiStatus = SCSISTAT_CHECK_CONDITION;
status->SenseKey = sense_key;
status->ASC = asc;
}
void SetScsiError(SPD_IOCTL_STORAGE_UNIT_STATUS* status, u8 sense_key, u8 asc, u64 info)
{
SetScsiError(status, sense_key, asc);
status->Information = info;
status->InformationValid = 1;
}
}