-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwin_kernel32.lua
296 lines (209 loc) · 6.79 KB
/
win_kernel32.lua
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
local ffi = require "ffi"
local C = ffi.C
require "WinBase"
local kernel32 = ffi.load("kernel32");
-- File System Calls
--
ffi.cdef[[
DWORD GetCurrentDirectoryA(DWORD nBufferLength, LPTSTR lpBuffer);
HANDLE CreateFileA(LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
BOOL GetFileInformationByHandle(HANDLE hFile,
PBY_HANDLE_FILE_INFORMATION lpFileInformation);
BOOL GetFileTime(HANDLE hFile,
LPFILETIME lpCreationTime,
LPFILETIME lpLastAccessTime,
LPFILETIME lpLastWriteTime);
BOOL FileTimeToSystemTime(const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime);
BOOL DeleteFileW(LPCTSTR lpFileName);
BOOL MoveFileW(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName);
/*
HFILE WINAPI OpenFile(LPCSTR lpFileName,
LPOFSTRUCT lpReOpenBuff,
UINT uStyle);
*/
]]
ffi.cdef[[
typedef DWORD (*LPTHREAD_START_ROUTINE)(LPVOID lpParameter);
]]
ffi.cdef[[
DWORD GetLastError();
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
BOOL CloseHandle(HANDLE hObject);
HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes,
BOOL bManualReset, BOOL bInitialState, LPCSTR lpName);
HANDLE CreateIoCompletionPort(HANDLE FileHandle,
HANDLE ExistingCompletionPort,
ULONG_PTR CompletionKey,
DWORD NumberOfConcurrentThreads);
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
size_t dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId);
DWORD GetCurrentThreadId(void);
DWORD ResumeThread(HANDLE hThread);
BOOL SwitchToThread(void);
DWORD SuspendThread(HANDLE hThread);
void * GetProcAddress(HMODULE hModule, LPCSTR lpProcName);
BOOL QueryPerformanceFrequency(int64_t *lpFrequency);
BOOL QueryPerformanceCounter(int64_t *lpPerformanceCount);
// DWORD QueueUserAPC(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);
void Sleep(DWORD dwMilliseconds);
DWORD SleepEx(DWORD dwMilliseconds, BOOL bAlertable);
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
HANDLE CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes,
BOOL bManualReset, LPCTSTR lpTimerName);
]]
ffi.cdef[[
typedef struct _OSVERSIONINFO {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[128];
} OSVERSIONINFO, *POSVERSIONINFO;
BOOL GetVersionExA(POSVERSIONINFO pVersionInfo);
]]
OSVERSIONINFO = ffi.typeof("OSVERSIONINFO")
OSVERSIONINFO_mt = {
__new = function(ct)
local obj = ffi.new("OSVERSIONINFO")
obj.dwOSVersionInfoSize = ffi.sizeof("OSVERSIONINFO");
kernel32.GetVersionExA(obj);
return obj;
end,
}
OSVERSIONINFO = ffi.metatype(OSVERSIONINFO, OSVERSIONINFO_mt);
function GetPerformanceFrequency(anum)
anum = anum or ffi.new("int64_t[1]");
local success = ffi.C.QueryPerformanceFrequency(anum)
if success == 0 then
return nil
end
return tonumber(anum[0])
end
function GetPerformanceCounter(anum)
anum = anum or ffi.new("int64_t[1]")
local success = ffi.C.QueryPerformanceCounter(anum)
if success == 0 then
return nil
end
return tonumber(anum[0])
end
function GetCurrentTickTime()
local frequency = 1/GetPerformanceFrequency();
local currentCount = GetPerformanceCounter();
local seconds = currentCount * frequency;
--print(string.format("win_kernel32 - GetCurrentTickTime() - %d\n", seconds));
return seconds;
end
function GetProcAddress(library, funcname)
ffi.load(library)
local paddr = ffi.C.GetProcAddress(ffi.C.GetModuleHandleA(library), funcname)
return paddr
end
function GetCurrentDirectory()
local buffsize = 1024;
local buff = ffi.new("char[1024]");
local err = kernel32.GetCurrentDirectoryA(buffsize, buff);
if err == 0 then return nil end
return ffi.string(buff);
end
local CreateWaitableTimer = function(manualReset, name)
local handle = kernel32.CreateWaitableTimerA(nil, manualReset, name);
end
--[[
WinNls.h
Defined in Kernel32
--]]
ffi.cdef[[
int MultiByteToWideChar(UINT CodePage,
DWORD dwFlags,
LPCSTR lpMultiByteStr, int cbMultiByte,
LPWSTR lpWideCharStr, int cchWideChar);
int WideCharToMultiByte(UINT CodePage,
DWORD dwFlags,
LPCWSTR lpWideCharStr, int cchWideChar,
LPSTR lpMultiByteStr, int cbMultiByte,
LPCSTR lpDefaultChar,
LPBOOL lpUsedDefaultChar);
]]
local CP_ACP = 0 -- default to ANSI code page
local CP_OEMCP = 1 -- default to OEM code page
local CP_MACCP = 2 -- default to MAC code page
local CP_THREAD_ACP = 3 -- current thread's ANSI code page
local CP_SYMBOL = 42 -- SYMBOL translations
local function AnsiToUnicode16L(in_Src)
local nsrcBytes = #in_Src
-- find out how many characters needed
local charsneeded = kernel32.MultiByteToWideChar(CP_ACP, 0, in_Src, nsrcBytes, nil, 0);
--print("charsneeded: ", nsrcBytes, charsneeded);
if charsneeded < 0 then
return nil;
end
local buff = ffi.new("uint16_t[?]", charsneeded+1)
local charswritten = kernel32.MultiByteToWideChar(CP_ACP, 0, in_Src, nsrcBytes, buff, charsneeded)
buff[charswritten] = 0
--print("charswritten: ", charswritten)
return ffi.string(buff, (charswritten*2)+1);
end
local function AnsiToUnicode16L(in_Src, nsrcBytes)
nsrcBytes = nsrcBytes or #in_Src
-- find out how many characters needed
local charsneeded = kernel32.MultiByteToWideChar(CP_ACP, 0, in_Src, nsrcBytes, nil, 0);
--print("charsneeded: ", nsrcBytes, charsneeded);
if charsneeded < 0 then
return nil;
end
local buff = ffi.new("uint16_t[?]", charsneeded+1)
local charswritten = kernel32.MultiByteToWideChar(CP_ACP, 0, in_Src, nsrcBytes, buff, charsneeded)
buff[charswritten] = 0
--print("charswritten: ", charswritten)
return buff;
end
local function Unicode16ToAnsi(in_Src, nsrcBytes)
nsrcBytes = nsrcBytes
local srcShorts = ffi.cast("const uint16_t *", in_Src)
-- find out how many characters needed
local bytesneeded = kernel32.WideCharToMultiByte(CP_ACP, 0, srcShorts, -1, nil, 0, nil, nil);
print("bytesneeded: ", bytesneeded);
if bytesneeded <= 0 then
return nil;
end
local buff = ffi.new("uint8_t[?]", bytesneeded+1)
local byteswritten = kernel32.WideCharToMultiByte(CP_ACP, 0, srcShorts, -1, buff, bytesneeded, nil, nil);
buff[byteswritten] = 0
--print("charswritten: ", byteswritten)
return ffi.string(buff, byteswritten-1);
end
local TEXT = function (quote)
if UNICODE then
return AnsiToUnicode16L(quote);
else
return quote
end
end
return {
Lib = kernel32,
-- Local functions
CreateEvent = kernel32.CreateEventA;
GetLastError = kernel32.GetLastError;
GetPerformanceFrequency = GetPerformanceFrequency,
GetPerformanceCounter = GetPerformanceCounter,
GetCurrentTickTime = GetCurrentTickTime,
GetProcAddress = GetProcAddress,
GetCurrentDirectory = GetCurrentDirectory,
AnsiToUnicode16 = AnsiToUnicode16L,
Unicode16ToAnsi = Unicode16ToAnsi,
TEXT = TEXT,
}