forked from ufrisk/MemProcFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oscompatibility.h
215 lines (205 loc) · 9.88 KB
/
oscompatibility.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
// oscompatibility.h : VMM Windows/Linux compatibility layer.
//
// (c) Ulf Frisk, 2021-2023
// Author: Ulf Frisk, [email protected]
//
#ifdef LINUX
#ifndef __OSCOMPATIBILITY_H__
#define __OSCOMPATIBILITY_H__
#include <leechcore.h>
#include <vmmdll.h>
#include <byteswap.h>
#include <ctype.h>
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <sys/eventfd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define LC_LIBRARY_FILETYPE ".so"
typedef void VOID, *PVOID, *LPVOID;
typedef void *HANDLE, **PHANDLE, *HMODULE, *FARPROC;
typedef uint32_t BOOL, *PBOOL;
typedef uint8_t BYTE, *PBYTE, *LPBYTE;
typedef uint8_t UCHAR, *PUCHAR;
typedef char CHAR, *PCHAR, *PSTR, *LPSTR;
typedef const char *LPCSTR;
typedef int16_t SHORT, *PSHORT;
typedef int32_t LONG;
typedef int64_t LONGLONG;
typedef uint16_t WORD, *PWORD, USHORT, *PUSHORT;
typedef uint16_t WCHAR, *PWCHAR, *LPWSTR;
typedef const uint16_t *LPCWSTR;
typedef uint32_t UINT, DWORD, *PDWORD, *LPDWORD, NTSTATUS, ULONG, *PULONG, ULONG32;
typedef long long unsigned int QWORD, *PQWORD, ULONG64, *PULONG64, ULONG_PTR;
typedef uint64_t DWORD64, *PDWORD64, LARGE_INTEGER, *PLARGE_INTEGER, ULONGLONG, FILETIME, *PFILETIME;
typedef size_t SIZE_T, *PSIZE_T;
typedef struct _M128A { ULONGLONG Low; LONGLONG High; } M128A, *PM128A;
typedef void *OVERLAPPED, *LPOVERLAPPED;
typedef struct tdEXCEPTION_RECORD32 { CHAR sz[80]; } EXCEPTION_RECORD32;
typedef struct tdEXCEPTION_RECORD64 { CHAR sz[152]; } EXCEPTION_RECORD64;
typedef struct tdSID { BYTE pb[12]; } SID, *PSID;
typedef DWORD(*PTHREAD_START_ROUTINE)(PVOID);
typedef DWORD(*LPTHREAD_START_ROUTINE)(PVOID);
typedef int(*_CoreCrtNonSecureSearchSortCompareFunction)(void const *, void const *);
#define __forceinline inline __attribute__((always_inline))
#define errno_t int
#define CONST const
#define TRUE 1
#define FALSE 0
#define MAX_PATH 260
#define LMEM_ZEROINIT 0x0040
#define INVALID_HANDLE_VALUE ((HANDLE)-1)
#define STD_INPUT_HANDLE ((DWORD)-10)
#define STD_OUTPUT_HANDLE ((DWORD)-11)
#define GENERIC_WRITE (0x40000000L)
#define GENERIC_READ (0x80000000L)
#define FILE_SHARE_READ (0x00000001L)
#define CREATE_NEW (0x00000001L)
#define OPEN_EXISTING (0x00000003L)
#define FILE_ATTRIBUTE_NORMAL (0x00000080L)
#define STILL_ACTIVE (0x00000103L)
#define CRYPT_STRING_HEX_ANY (0x00000008L)
#define CRYPT_STRING_HEXASCIIADDR (0x00000008L)
#define STILL_ACTIVE (0x00000103L)
#define INVALID_FILE_SIZE (0xffffffffL)
#define _TRUNCATE ((SIZE_T)-1LL)
#define CONSOLE_SCREEN_BUFFER_INFO PVOID // TODO: remove this dummy
#define SOCKET int
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define WSAEWOULDBLOCK 10035L
#define WAIT_OBJECT_0 (0x00000000UL)
#define INFINITE (0xFFFFFFFFUL)
#define MAXIMUM_WAIT_OBJECTS 64
#define SID_MAX_SUB_AUTHORITIES (15)
#define SECURITY_MAX_SID_SIZE (sizeof(SID) - sizeof(DWORD) + (SID_MAX_SUB_AUTHORITIES * sizeof(DWORD)))
#define CP_ACP 0
#define CP_UTF8 65001
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
#define STATUS_END_OF_FILE ((NTSTATUS)0xC0000011L)
#define STATUS_FILE_INVALID ((NTSTATUS)0xC0000098L)
#define STATUS_FILE_SYSTEM_LIMITATION ((NTSTATUS)0xC0000427L)
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010
#define FILE_ATTRIBUTE_COMPRESSED 0x00000800
#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
//-----------------------------------------------------------------------------
// SAL DEFINES BELOW:
//-----------------------------------------------------------------------------
#define _In_
#define _In_z_
#define _Out_
#define _Inout_
#define _Inout_opt_
#define _In_opt_
#define _In_opt_z_
#define _Out_opt_
#define _Check_return_opt_
#define _Frees_ptr_opt_
#define _Post_ptr_invalid_
#define _Printf_format_string_
#define _In_reads_(x)
#define _In_reads_opt_(x)
#define _Out_writes_(x)
#define __bcount(x)
#define _Inout_bytecount_(x)
#define _Inout_count_(x)
#define _Inout_updates_(x)
#define _Inout_updates_opt_(x)
#define _Inout_updates_bytes_(x)
#define _Out_writes_bytes_opt_(x)
#define _Out_writes_opt_(x)
#define _Out_writes_to_(x,y)
#define _Out_writes_z_(x)
#define _Maybenull_
#define _Success_(x)
#define _When_(x,y)
#define _Writable_bytes_(x)
#define UNREFERENCED_PARAMETER(x)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define _byteswap_ushort(v) (bswap_16(v))
#define _byteswap_ulong(v) (bswap_32(v))
#define _byteswap_uint64(v) (bswap_64(v))
#ifndef _rotr
#define _rotr(v,c) ((((DWORD)v) >> ((DWORD)c) | (DWORD)((DWORD)v) << (32 - (DWORD)c)))
#endif /* _rotr */
#define _rotr16(v,c) ((((WORD)v) >> ((WORD)c) | (WORD)((WORD)v) << (16 - (WORD)c)))
#define _rotr64(v,c) ((((QWORD)v) >> ((QWORD)c) | (QWORD)((QWORD)v) << (64 - (QWORD)c)))
#define _rotl64(v,c) ((QWORD)(((QWORD)v) << ((QWORD)c)) | (((QWORD)v) >> (64 - (QWORD)c)))
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#define sprintf_s(s, maxcount, ...) (snprintf(s, maxcount, __VA_ARGS__))
#define strnlen_s(s, maxcount) (strnlen(s, maxcount))
#define strcpy_s(dst, len, src) (strncpy(dst, src, len))
#define strncpy_s(dst, len, src, srclen) (strncpy(dst, src, min((QWORD)(max(1, len)) - 1, (QWORD)(srclen))))
#define strcat_s(dst, len, src) (strcat(dst, src))
#define strncat_s(dst, len, src, srclen) (strncat(dst, src, min((QWORD)(max(1, len)) - 1, (QWORD)(srclen))))
#define _vsnprintf_s(dst, len, cnt, fmt, a) (vsnprintf(dst, min((QWORD)(len), (QWORD)(cnt)), fmt, a))
#define _stricmp(s1, s2) (strcasecmp(s1, s2))
#define _strnicmp(s1, s2, maxcount) (strncasecmp(s1, s2, maxcount))
#define strtok_s(s, d, c) (strtok_r(s, d, c))
#define _snprintf_s(s,l,c,...) (snprintf(s,min((QWORD)(l), (QWORD)(c)),__VA_ARGS__))
#define sscanf_s(s, f, ...) (sscanf(s, f, __VA_ARGS__))
#define SwitchToThread() (sched_yield())
#define ExitThread(dwExitCode) (pthread_exit(dwExitCode))
#define ExitProcess(c) (exit(c ? EXIT_SUCCESS : EXIT_FAILURE))
#define Sleep(dwMilliseconds) (usleep(1000*dwMilliseconds))
#define fopen_s(ppFile, szFile, szAttr) ((*ppFile = fopen(szFile, szAttr)) ? 0 : 1)
#define ZeroMemory(pb, cb) (memset(pb, 0, cb))
#define _ftelli64(f) (ftello(f))
#define _fseeki64(f, o, w) (fseeko(f, o, w))
#define _chsize_s(fd, cb) (ftruncate64(fd, cb))
#define _fileno(f) (fileno(f))
#define InterlockedAdd64(p, v) (__sync_add_and_fetch(p, v))
#define InterlockedIncrement64(p) (__sync_add_and_fetch(p, 1))
#define InterlockedIncrement(p) (__sync_add_and_fetch_4(p, 1))
#define InterlockedDecrement(p) (__sync_sub_and_fetch_4(p, 1))
#define GetCurrentProcess() ((HANDLE)-1)
#define InetNtopA inet_ntop
#define closesocket(s) close(s)
#define LoadLibraryA(s) NULL
#define FreeLibrary(h)
#define GetProcAddress(m,a) NULL
#define GetModuleHandleA(s) NULL
// CRITICAL SECTION
#ifndef _LINUX_DEF_CRITICAL_SECTION
#define _LINUX_DEF_CRITICAL_SECTION
typedef struct tdCRITICAL_SECTION {
pthread_mutex_t mutex;
pthread_mutexattr_t mta;
} CRITICAL_SECTION, *LPCRITICAL_SECTION;
#endif /* _LINUX_DEF_CRITICAL_SECTION */
BOOL InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount);
VOID InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
VOID DeleteCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
VOID EnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
VOID LeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
// SRWLOCK
typedef struct tdSRWLOCK {
uint32_t xchg;
int c;
} SRWLOCK, *PSRWLOCK;
VOID InitializeSRWLock(PSRWLOCK SRWLock);
VOID AcquireSRWLockExclusive(_Inout_ PSRWLOCK SRWLock);
VOID ReleaseSRWLockExclusive(_Inout_ PSRWLOCK SRWLock);
#define AcquireSRWLockShared AcquireSRWLockExclusive
#define ReleaseSRWLockShared ReleaseSRWLockExclusive
HANDLE LocalAlloc(DWORD uFlags, SIZE_T uBytes);
VOID LocalFree(HANDLE hMem);
QWORD GetTickCount64();
#endif /* __OSCOMPATIBILITY_H__ */
#endif /* LINUX */