-
Notifications
You must be signed in to change notification settings - Fork 151
/
Base.h
477 lines (382 loc) · 10.5 KB
/
Base.h
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#pragma once
#define UMDF_USING_NTSTATUS
#include <Windows.h>
#include "StringI.h"
#include <set>
#include <map>
#include <vector>
#include <winioctl.h>
#include <winscard.h>
#include <wincrypt.h>
#include "km.h"
#define LDR_IS_DATAFILE(handle) (((ULONG_PTR)(handle)) & (ULONG_PTR)1)
#define LDR_IS_IMAGEMAPPING(handle) (((ULONG_PTR)(handle)) & (ULONG_PTR)2)
#define LDR_IS_RESOURCE(handle) (LDR_IS_IMAGEMAPPING(handle) || LDR_IS_DATAFILE(handle))
//计算数组长度
#define ArraySize(Array) _countof(Array)
//生成版本号
#define MakeVersion(v1,v2,v3,v4) (UINT64)(((UINT64)(v4))|((UINT64)v3<<16)|((UINT64)v2<<32)|((UINT64)v1<<48))
#define MakeMiniVersion(v1,v2) (DWORD)(v2|(v1<<16))
#define __StrA(x) #x
#define __StrW(x) _T(__StrA(x))
#define DISM_MSG_PROGRESS 38008 //进度信息wParam为当前完成百分比
#ifdef ENABLE_BACKUP_RESTORE
#if ENABLE_BACKUP_RESTORE >=2
//该模式根据实际情况动态调整
extern DWORD REG_OPTION;
#else
#define REG_OPTION REG_OPTION_BACKUP_RESTORE
#endif
#else
#define REG_OPTION 0
#endif
_Success_(return == ERROR_SUCCESS)
typedef DWORD ( __stdcall *BaseCallBack)(
_In_ DWORD MessageId,
_In_opt_ WPARAM wParam,
_In_opt_ LPARAM lParam,
_In_opt_ PVOID UserData
);
void FindPath(
_In_ CStringI FilePath,
_In_z_ LPCWSTR SubStr,
_Out_ std::set<CStringI>& FindList,
_In_ DWORD cchRootPath
);
_Check_return_
LSTATUS RunCmd(
_In_opt_z_ LPCWSTR FilePath,
_In_ CString CmdString,
_Out_opt_ CString* pOutString
);
_Check_return_
LSTATUS RunCmd(
_In_opt_z_ LPCWSTR FilePath,
_In_ CString CmdString,
_In_ bool Async = false
);
_Check_return_
LSTATUS RunCmd(
_In_opt_z_ LPCWSTR FilePath,
_In_ CString CmdString,
_In_opt_ BaseCallBack CallBack,
_In_opt_ LPVOID UserData
);
_Check_return_ _Success_(return == STATUS_SUCCESS)
NTSTATUS QuerySymbolicLinkObject(
_In_z_ LPCWSTR LinkName,
_Out_ CString& LinkTarget
);
#if _MSC_VER >1500
_Check_return_ _Success_(return == S_OK)
HRESULT IsoCreateFileByPath(
_In_z_ LPCWSTR pIsoPath,
_In_z_ LPCWSTR pSrcDir,
_In_opt_z_ LPCWSTR VolumeName,
_In_opt_ BaseCallBack callBack,
_In_opt_ LPVOID pUserData
);
#endif
_Check_return_
LSTATUS Base642Binary(
_Out_ CStringA& Binary,
_In_NLS_string_(cchString) LPCSTR Base64String,
_In_ DWORD cchString
);
_Check_return_
LSTATUS Base642Binary(
_Out_ CStringA& Binary,
_In_NLS_string_(cchString) LPCWSTR Base64String,
_In_ DWORD cchString = -1
);
_Check_return_
LSTATUS Binary2Base64(
_In_reads_bytes_(ccbSrc) const void* Src,
_In_ DWORD ccbSrc,
_Out_ CString& Base64String
);
_Check_return_
LSTATUS Base642Binary(
_Out_writes_bytes_to_(ccbBinary, ccbBinary) BYTE* pBinary,
_Inout_ DWORD& ccbBinary,
_In_NLS_string_(cchString) LPCWSTR Base64String,
_In_ DWORD cchString = -1
);
void ReverseBinary(
_Inout_updates_bytes_(ccbBinary) BYTE* pBinary,
_In_ DWORD ccbBinary
);
_Check_return_
BOOL PathIsSameVolume(
_In_z_ LPCWSTR Path1,
_In_z_ LPCWSTR Path2
);
_Check_return_ _Success_(return == STATUS_SUCCESS)
NTSTATUS AdjustPrivilege(
_In_ ULONG Privilege,
_In_ BOOL Enable = TRUE
);
_Check_return_
LSTATUS GetHashByFilePath(
_In_z_ LPCWSTR FilePath,
_In_ ALG_ID Algid,
_Out_writes_bytes_(cbHashData) BYTE* pHashData,
_In_
_When_(Algid == CALG_MD5, _In_range_(>= , 16))
_When_(Algid == CALG_SHA1, _In_range_(>= , 20))
_When_(Algid == CALG_SHA_256, _In_range_(>= , 32)) DWORD cbHashData
);
_Check_return_
LSTATUS GetHashByData(
_In_reads_bytes_(cbData) LPCBYTE pData,
_In_ DWORD cbData,
_In_ ALG_ID Algid,
_Out_writes_bytes_(cbHashData) BYTE* pHashData,
_In_
_When_(Algid == CALG_MD5, _In_range_(>= , 16))
_When_(Algid == CALG_SHA1, _In_range_(>= , 20))
_When_(Algid == CALG_SHA_256, _In_range_(>= , 32)) DWORD cbHashData
);
_Check_return_
LSTATUS GetMd5ByFilePath(
_In_z_ LPCWSTR FilePath,
_Out_writes_bytes_(16) BYTE FileMd5[16]
);
_Check_return_
LSTATUS GetMd5ByData(
_In_reads_bytes_(cbData) LPCBYTE pData,
_In_ DWORD cbData,
_Out_writes_bytes_(16) BYTE FileMd5[16]
);
_Check_return_
LSTATUS GetSha1ByFilePath(
_In_z_ LPCWSTR FilePath,
_Out_writes_bytes_(20) BYTE FileSha1[20]
);
void PathCat(
_In_ CString& Path,
_In_opt_z_ LPCWSTR Append
);
CString PathCat(
_In_z_ LPCWSTR Path,
_In_opt_z_ LPCWSTR Append
);
_Check_return_
LSTATUS CreateFileByData(
_In_z_ LPCWSTR FilePath,
_In_z_ LPCWSTR lpName,
_In_z_ LPCWSTR lpType,
_In_ HMODULE hModule
);
_Check_return_
LSTATUS CreateFileByData(
_In_z_ LPCWSTR FilePath,
_In_reads_bytes_(ccbData) const void* pData,
_In_ DWORD ccbData
);
_Check_return_
LSTATUS CreateFileByZipData(
_In_z_ LPCWSTR FilePath,
_In_reads_bytes_(ccbData) const void* Data,
_In_ DWORD ccbData
);
_Check_return_
LSTATUS CreateRoot(
_In_z_ LPCWSTR FilePath
);
_Check_return_ _Success_(return != INVALID_HANDLE_VALUE)
HANDLE OpenDriver(
_In_z_ LPCWSTR DriverPath,
_In_ DWORD dwDesiredAccess = GENERIC_READ
);
_Check_return_
LSTATUS GetDriverLayout(
_In_ HANDLE hDevice,
_Out_ CStringA& Buffer/*DRIVE_LAYOUT_INFORMATION_EX+ n * PARTITION_INFORMATION_EX*/
);
_Check_return_
LSTATUS GetDriverLayout(
_In_z_ LPCWSTR DriverPath,
_Out_ CStringA& Buffer
);
_Check_return_ _Success_(return == -1)
int GetPartitionDisk(
_In_ HANDLE hDevice
);
_Check_return_
LSTATUS GetPartitionInfomation(
_In_ HANDLE hDevice,
_Out_ PARTITION_INFORMATION_EX& PartitionInfo
);
_Check_return_ _Success_(return == -1)
int GetPartition(
_In_ HANDLE hDevice
);
_Check_return_
LSTATUS GetVhdVolumeFilePath(
_In_ HANDLE hDevice,
_Out_ CString& VHDFilePath
);
_Check_return_
LSTATUS GetVhdVolumeFilePath(
_In_z_ LPCWSTR hDevicePath,
_Out_ CString& VHDFilePath
);
_Check_return_
LSTATUS GetDiskCount(
_Out_ std::vector<CString>& pszDevicePath
);
_Check_return_ _Success_(return != nullptr)
byte* IsProcExists(
_In_ byte* pBase,
_In_z_ LPCSTR ProcName
);
_Check_return_
bool IsProcExists(
_In_ HANDLE hFile,
_In_z_ LPCSTR ProcName
);
_Check_return_ _Success_(return == 0)
NTSTATUS CompressBuffer(
_In_reads_bytes_(cbSrc) const void* Src,
_In_ DWORD cbSrc,
_Out_writes_bytes_to_(*pcbDst, *pcbDst) void* Dst,
_Inout_ DWORD* pcbDst
);
_Check_return_ _Success_(return == 0)
NTSTATUS DecompressBuffer(
_In_reads_bytes_(cbSrc) const void* Src,
_In_ DWORD cbSrc,
_Out_writes_bytes_to_(*pcbDst, *pcbDst) void* Dst,
_Inout_ DWORD* pcbDst
);
_Check_return_ _Success_(return == S_OK)
HRESULT NtPath2DosPath(
_In_z_ LPCWSTR NtPath,
_Out_ CString& DosPath
);
_Check_return_ _Success_(return)
BOOL LoadString_s(
_In_z_ LPCWSTR FilePath,
_In_ int Index,
_Out_ CStringW& String
);
_Check_return_ _Success_(return)
BOOL LoadString_s(
_In_z_ LPCWSTR FilePath,
_In_ int Index,
_Outptr_result_z_ LPBSTR pString
);
//HRESULT LoadString(HMODULE hModule,DWORD Index,CString& String);
#define IMAGE_FIRST_DIRECTORY(ntheader) (IMAGE_DATA_DIRECTORY*)((byte*)IMAGE_FIRST_SECTION(ntheader)-sizeof(IMAGE_DATA_DIRECTORY)*IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
_Check_return_ _Success_(return != PROCESSOR_ARCHITECTURE_UNKNOWN)
DWORD GetFileArchitecture(
_In_z_ LPCWSTR FilePath
);
//void GetCtlCode(DWORD Code, DWORD& DeviceType, DWORD& Function, DWORD& Method, DWORD& Access);
_Check_return_ _Success_(return != nullptr)
PVOID64 GetProcAddressEx(
_In_ HANDLE hProc,
_In_ HMODULE hModule,
_In_z_ LPCSTR lpProcName
);
//GetLongPathNameW的安全封装
_Check_return_
LSTATUS GetLongPathName_s(
_In_ LPCWSTR lpszShortPath,
_Out_ CStringW& lpszLongPath
);
//ExpandEnvironmentStringsW的安全封装
_Check_return_
LSTATUS __fastcall ExpandEnvironmentStrings_s(
_In_ LPCWSTR lpszShortPath,
_Out_ CStringW& lpszLongPath
);
//GetVolumePathNameW的安全封装
_Check_return_
LSTATUS __fastcall GetVolumePathName_s(
_In_z_ LPCWSTR lpszFileName,
_Out_ CStringW& lpszLongPath
);
//GetModuleFileNameW的安全封装
LSTATUS __fastcall GetModuleFileName_s(
_In_opt_ HMODULE hModule,
_Out_ CStringW& szModulePath
);
//IsReparseTagNameSurrogate可以使用此函数代替下面函数功能
//bool IsSimpleReparesPoint(DWORD dwReserved0);
_Check_return_ _Success_(return <= 0xF)
byte Char2Hex(
_In_ wchar_t ch
);
CString Binary2String(
_In_reads_bytes_(cbData) const byte* pBinaryData,
_In_ DWORD cbData
);
CStringA HexString2Binary(
_In_z_ LPCWSTR HexString
);
CStringA HexString2Binary(
_In_z_ LPCSTR HexString
);
HRESULT HresultFromBool(
_In_ BOOL bSuccess = FALSE
);
HRESULT HresultFromBoolFalse();
//安全版GetLastError,必定返回一个错误代码。
_Ret_
LSTATUS GetLastError_s(
_In_ BOOL bSuccess = FALSE
);
LSTATUS GetLastErrorFromBoolFalse();
//检测是否是兼容模式
_Check_return_ _Success_(return !=-1)
BOOL IsCompatibilityMode();
//获取2位小版本号,比如6.1
DWORD DirectGetOsMinVersion();
//获取3位版本号,最后一位必定为0,比如6.1.7601.0
UINT64 DirectGetOsVersion();
//LocalFree释放空间
_Check_return_
_CRTALLOCATOR LPCWSTR __cdecl FormatLongString(_In_z_ LPCWSTR _Format, ...);
LSTATUS __fastcall ModuleAddRef(
_In_ HMODULE hModule
);
namespace internal
{
template<class T>
class range_based_for_container_array
{
private:
T* pBegin;
T* pEnd;
public:
__forceinline __fastcall range_based_for_container_array(T* __pBegin, T* __pEnd)
: pBegin(__pBegin)
, pEnd(__pEnd)
{
}
__forceinline T* begin()
{
return pBegin;
}
__forceinline T* end()
{
return pEnd;
}
};
}
template<class T>
static
__forceinline
internal::range_based_for_container_array<T> __fastcall range_based_for(T* pData, size_t Count)
{
return internal::range_based_for_container_array<T>(pData, pData + Count);
}
template<class T>
static
__forceinline
internal::range_based_for_container_array<T> __fastcall range_based_for(T* pBegin, T* pEnd)
{
return internal::range_based_for_container_array<T>(pBegin, pEnd);
}