1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // Module Name:
4
+ //
5
+ // KexComm.h
6
+ //
7
+ // Abstract:
8
+ //
9
+ // VxKex common header file. Every project which is a part of VxKex must
10
+ // include this file unless there is a very good reason not to.
11
+ //
12
+ // Author:
13
+ //
14
+ // vxiiduu (30-Sep-2022)
15
+ //
16
+ // Environment:
17
+ //
18
+ // Refer to included header files for information about their contents.
19
+ //
20
+ // Revision History:
21
+ //
22
+ // vxiiduu 30-Sep-2022 Initial creation.
23
+ //
24
+ ///////////////////////////////////////////////////////////////////////////////
25
+
26
+ #pragma once
27
+
28
+ #if !defined(KEX_ARCH_X64 ) && !defined(KEX_ARCH_X86 )
29
+ # if defined(_M_X64 )
30
+ # define KEX_ARCH_X64
31
+ # elif defined(_M_IX86 )
32
+ # define KEX_ARCH_X86
33
+ # else
34
+ # error Invalid CPU architecture for VxKex. Only x86 and x64 are supported.
35
+ # endif
36
+ #endif
37
+
38
+ #ifndef KEX_ENV_NATIVE
39
+ # define KEX_ENV_WIN32
40
+ #endif
41
+
42
+ #if defined(KEX_ENV_NATIVE ) && defined(KEX_ENV_WIN32 )
43
+ # error Only one of the KEX_ENV_ macros may be defined.
44
+ #endif
45
+
46
+ // Define Unicode macros.
47
+ // __FILEW__ and __FUNCTIONW__ are already defined somewhere inside the windows
48
+ // headers.
49
+ #define CONCAT (a ,b ) a##b
50
+ #define _L (str ) CONCAT(L,str)
51
+ #define __DATEW__ _L(__DATE__)
52
+ #define __TIMEW__ _L(__TIME__)
53
+ #define __TIMESTAMPW__ _L(__TIMESTAMP__)
54
+ #define __FUNCDNAMEW__ _L(__FUNCDNAME__)
55
+ #define __FUNCSIGW__ _L(__FUNCSIG__)
56
+
57
+ //
58
+ // Define convenience macros so we don't need #ifdef in the middle of code.
59
+ //
60
+ #ifdef _DEBUG
61
+ # define KexIsDebugBuild TRUE
62
+ # define KexIsReleaseBuild FALSE
63
+ #else
64
+ # define KexIsDebugBuild FALSE
65
+ # define KexIsReleaseBuild TRUE
66
+ #endif
67
+
68
+ #ifdef KEX_ARCH_X64
69
+ # define KexIs32BitBuild FALSE
70
+ # define KexIs64BitBuild TRUE
71
+ #else
72
+ # define KexIs32BitBuild TRUE
73
+ # define KexIs64BitBuild FALSE
74
+ #endif
75
+
76
+ #pragma region Header Includes
77
+ // these two must be included before other headers (esp. KexStrSafe.h), or
78
+ // undefined symbol errors can occur
79
+ # include <KexComp.h>
80
+ # include <KexLnk.h>
81
+
82
+ # ifdef KEX_ENV_NATIVE
83
+ //
84
+ // We can't use any of this stuff in a native environment,
85
+ // so we disable it to avoid mistakes/accidentally calling
86
+ // unusable APIs.
87
+ //
88
+ # define NOGDICAPMASKS
89
+ # define NOVIRTUALKEYCODES
90
+ # define NOWINMESSAGES
91
+ # define NOWINSTYLES
92
+ # define NOSYSMETRICS
93
+ # define NOMENUS
94
+ # define NOICONS
95
+ # define NOKEYSTATES
96
+ # define NOSYSCOMMANDS
97
+ # define NORASTEROPS
98
+ # define NOSHOWWINDOW
99
+ # define NOATOM
100
+ # define NOCLIPBOARD
101
+ # define NOCOLOR
102
+ # define NOCTLMGR
103
+ # define NODRAWTEXT
104
+ # define NOGDI
105
+ # define NOKERNEL
106
+ # define NOUSER
107
+ # define NONLS
108
+ # define NOMB
109
+ # define NOMEMMGR
110
+ # define NOMSG
111
+ # define NOOPENFILE
112
+ # define NOSCROLL
113
+ # define NOTEXTMETRIC
114
+ # define NOWH
115
+ # define NOWINOFFSETS
116
+ # define NOCOMM
117
+ # define NOKANJI
118
+ # define NOHELP
119
+ # define NOPROFILER
120
+ # define NODEFERWINDOWPOS
121
+ # define NOMCX
122
+ # define NOCRYPT
123
+ # define NOMETAFILE
124
+ # define NOSERVICE
125
+ # define NOSOUND
126
+ # endif
127
+
128
+ # define COBJMACROS
129
+ # define CINTERFACE
130
+
131
+ # define STRICT
132
+ # define WIN32_NO_STATUS
133
+
134
+ # ifdef __INTELLISENSE__
135
+ # undef __cplusplus
136
+ # endif
137
+
138
+ // attempt to make vxkex build with newer sdk's as well.
139
+ // no guarantees. if you install the win10 sdk and it doesn't build, tough luck.
140
+ // don't ask me to make shit build on your windows 11 with newest SDK.
141
+ # define _WIN32_WINNT 0x0601
142
+
143
+ # include <Windows.h>
144
+ # include <NtDll.h>
145
+ # include <StdArg.h>
146
+
147
+ # ifdef KEX_TARGET_TYPE_SYS
148
+ # include <NtKrnl.h>
149
+ # endif
150
+
151
+ # ifdef __INTELLISENSE__
152
+ # undef NULL
153
+ # define NULL 0
154
+ # endif
155
+
156
+ # if defined(KEX_ENV_WIN32 ) && !defined(NOUSER )
157
+ // Generally speaking we import from SHLWAPI only for the Path*
158
+ // functions. In the future we will dogfood our own PathCch* functions
159
+ // and avoid importing from SHLWAPI altogether.
160
+
161
+ // SHLWAPI string functions are EXTREMELY slow. Do not use them, ever.
162
+ # define NO_SHLWAPI_STRFCNS
163
+
164
+ // Ok, we'll make an exception for StrFormatByteSizeW -_-
165
+ # define StrFormatByteSize StrFormatByteSizeW
166
+ PWSTR StrFormatByteSizeW (LONGLONG qdw , PWSTR pszBuf , UINT cchBuf );
167
+
168
+ # define NO_SHLWAPI_ISOS
169
+ # define NO_SHLWAPI_STREAM
170
+ # define NO_SHLWAPI_HTTP
171
+ # define NO_SHLWAPI_GDI
172
+ # define NO_SHLWAPI_STOPWATCH
173
+ # pragma comment(lib, "shlwapi.lib")
174
+ # include <Shlwapi.h>
175
+ # endif
176
+
177
+ // Some bullshit warning about a "deprecated" function in intrin.h
178
+ // get rid of the stupid warning
179
+ # pragma warning(push)
180
+ # pragma warning(disable:4995)
181
+ # include <Intrin.h>
182
+ # pragma warning(pop)
183
+
184
+ # include <KexTypes.h>
185
+ # include <KexVer.h>
186
+
187
+ # if defined(KEX_ENV_WIN32 ) && !defined(KEX_TARGET_TYPE_LIB )
188
+ # include <KexGui.h>
189
+ # include <KexW32ML.h>
190
+ # include <KxCfgHlp.h>
191
+ # endif
192
+
193
+ # include <KexAssert.h>
194
+
195
+ # ifdef KEX_TARGET_TYPE_SYS
196
+ # include <NtStrSafe.h>
197
+ # else
198
+ # include <KexStrSafe.h>
199
+ # include <KexPathCch.h>
200
+ # include <SafeAlloc.h>
201
+ # endif
202
+ #pragma endregion
203
+
204
+ #pragma region Extended Language Constructs
205
+ # define try __try
206
+ # define except __except
207
+ # define finally __finally
208
+ # define leave __leave
209
+ # define throw (Status ) RtlRaiseStatus(Status)
210
+ # define asm __asm
211
+
212
+ # ifndef until
213
+ # define until (Condition ) while (!(Condition))
214
+ # endif
215
+
216
+ # define unless (Condition ) if (!(Condition))
217
+ # define ReturnAddress _ReturnAddress
218
+
219
+ # define PopulationCount16 __popcnt16
220
+ # define PopulationCount __popcnt
221
+ # define PopulationCount64 __popcnt64
222
+ #pragma endregion
223
+
224
+ #pragma region Convenience Macros
225
+ //
226
+ // Convert a HRESULT code to a Win32 error code.
227
+ // Note that not all HRESULT codes can be mapped to a valid Win32 error code.
228
+ //
229
+ # define WIN32_FROM_HRESULT (x ) (HRESULT_CODE(x))
230
+
231
+ # define LODWORD (ull ) ((ULONG) (ull))
232
+ # define HIDWORD (ull ) ((ULONG) ((ULONGLONG) (ull) >> 32))
233
+
234
+ //
235
+ // Convert a relative virtual address (e.g. as found in PE image files) to a
236
+ // real virtual address which can be read, written, dereferenced etc.
237
+ // VA_TO_RVA does the opposite.
238
+ //
239
+ # define RVA_TO_VA (base , rva ) ((PVOID) (((PBYTE) (base)) + (rva)))
240
+ # define VA_TO_RVA (base , va ) ((ULONG_PTR) (((PBYTE) (va)) - ((PBYTE) (base))))
241
+
242
+ //
243
+ // Convert a boolean to a string which can be displayed to the user.
244
+ //
245
+ # define BOOLEAN_AS_STRING (b ) ((b) ? L"TRUE" : L"FALSE")
246
+
247
+ //
248
+ // Lookup an entry in a static entry table with bounds clamping.
249
+ //
250
+ # define ARRAY_LOOKUP_BOUNDS_CHECKED (Array , Index ) Array[max(0, min((Index), ARRAYSIZE(Array) - 1))]
251
+
252
+ //
253
+ // Convert a byte count to a character count (assuming a character is
254
+ // 2 bytes long, as it always is when dealing with windows unicode strings)
255
+ // and vice versa.
256
+ //
257
+ # define CB_TO_CCH (Cb ) ((Cb) >> 1)
258
+ # define CCH_TO_CB (Cch ) ((Cch) << 1)
259
+
260
+ //
261
+ // Check that a kernel handle is not NULL or INVALID_HANDLE_VALUE.
262
+ // Keep in mind that NtCurrentProcess() will not be valid if checked with
263
+ // this macro, so you will have to special-case it in any code that uses
264
+ // process handles.
265
+ //
266
+ # define VALID_HANDLE (Handle ) \
267
+ (((Handle) != NULL) && \
268
+ ((Handle) != INVALID_HANDLE_VALUE) && \
269
+ !(((ULONG_PTR) (Handle)) & 3) && \
270
+ (((ULONG_PTR) (Handle)) <= ULONG_MAX))
271
+
272
+ # define KexDebugCheckpoint () if (KexIsDebugBuild && NtCurrentPeb()->BeingDebugged) __debugbreak()
273
+
274
+ # define InterlockedIncrement16 _InterlockedIncrement16
275
+ # define InterlockedDecrement16 _InterlockedDecrement16
276
+ #pragma endregion
0 commit comments