-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathHelpers.h
More file actions
110 lines (93 loc) · 2.92 KB
/
Copy pathHelpers.h
File metadata and controls
110 lines (93 loc) · 2.92 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#include <Windows.h>
#include <sddl.h>
#include <string>
#include <functional>
#include "Operation.h"
// helper functions
VOID EnablePrivs() noexcept;
PSID GetSidFromName(const std::wstring & sAccountName);
std::wstring GetNameFromSid(PSID tSid, bool * bMarkAsOrphan = nullptr);
std::wstring GetNameFromSidEx(PSID tSid, bool * bMarkAsOrphan = nullptr);
std::wstring GetDomainNameFromSid(PSID tSid);
std::wstring GenerateAccessMask(DWORD iCurrentMask);
std::wstring GenerateInheritanceFlags(DWORD iCurrentFlags);
HANDLE RegisterFileHandle(HANDLE hFile, const std::wstring & sOperation);
std::wstring GetAntivirusStateDescription();
std::wstring FileTimeToString(FILETIME tFileTime);
std::wstring FileSizeToString(LARGE_INTEGER iFileSize);
std::wstring FileAttributesToString(DWORD iAttributes);
BOOL WriteToFile(const std::wstring & sStringToWrite, HANDLE hFile) noexcept;
VOID InitThreadCom() noexcept;
BOOL IsSidInDomain(PSID pSid, PSID pDomainSid) noexcept;
// helper typedefs
struct SidCompare
{
bool operator()(PSID p1, PSID p2) const
{
const DWORD iLength1 = SidLength(p1);
const DWORD iLength2 = SidLength(p2);
if (iLength1 != iLength2) return iLength1 < iLength2;
return memcmp(p1, p2, iLength1) > 0;
}
};
//
// SmartPointer<>. Custom template for WinAPI resource cleanup.
// Automatically invokes the provided cleanup callable in its destructor.
//
template <typename T>
class SmartPointer final
{
public:
SmartPointer(const SmartPointer&) = delete;
SmartPointer& operator=(const SmartPointer&) = delete;
SmartPointer(std::function<void(T)> cleanup) noexcept : m_cleanup(std::move(cleanup)), m_data(nullptr) {}
SmartPointer(std::function<void(T)> cleanup, T data) noexcept : m_cleanup(std::move(cleanup)), m_data(data) {}
~SmartPointer()
{
Release();
}
SmartPointer(SmartPointer&& src) noexcept
{
m_cleanup = std::move(src.m_cleanup);
m_data = src.m_data;
src.m_data = nullptr;
}
SmartPointer& operator=(SmartPointer&& src) noexcept
{
if (std::addressof(*this) != std::addressof(src))
{
Release();
m_cleanup = std::move(src.m_cleanup);
m_data = src.m_data;
src.m_data = nullptr;
}
return *this;
}
bool IsValid() const noexcept
{
return m_data != nullptr && m_data != INVALID_HANDLE_VALUE;
}
void Release() noexcept
{
if (IsValid())
{
m_cleanup(m_data);
m_data = nullptr;
}
}
T operator=(T lp) noexcept
{
Release();
m_data = lp;
return m_data;
}
operator T() noexcept { return m_data; }
T& operator*() noexcept { return m_data; }
T* operator&() noexcept { return &m_data; }
T operator->() noexcept { return m_data; }
bool operator!() noexcept { return m_data == nullptr; }
private:
std::function<void(T)> m_cleanup;
T m_data;
};