| 1 |
#ifndef ___SYSTEM__H___ |
| 2 |
#define ___SYSTEM__H___ |
| 3 |
|
| 4 |
// This should probably be moved to platform.h at some point |
| 5 |
|
| 6 |
#if defined(_M_X64) || defined(_M_AMD64) || defined(__amd64__) |
| 7 |
# define SYSTEM_AMD64 |
| 8 |
#elif defined(_M_IX86) || defined(__i386__) || defined(_X86_) |
| 9 |
# define SYSTEM_X86 |
| 10 |
#elif defined(_M_ARM64) |
| 11 |
# define SYSTEM_ARM64 |
| 12 |
#else |
| 13 |
# error "Unknown architecture!" |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifdef _WIN64 |
| 17 |
# define SYSFMT_HEXPTR _T("0x%016IX") |
| 18 |
#else |
| 19 |
# define SYSFMT_HEXPTR _T("0x%08X") |
| 20 |
#endif |
| 21 |
|
| 22 |
|
| 23 |
// The following ifdef block is the standard way of creating macros which make exporting |
| 24 |
// from a DLL simpler. All files within this DLL are compiled with the SYSTEM_EXPORTS |
| 25 |
// symbol defined on the command line. this symbol should not be defined on any project |
| 26 |
// that uses this DLL. This way any other project whose source files include this file see |
| 27 |
// SYSTEM_API functions as being imported from a DLL, whereas this DLL sees symbols |
| 28 |
// defined with this macro as being exported. |
| 29 |
|
| 30 |
#ifdef SYSTEM_EXPORTS |
| 31 |
# define SYSTEM_API __declspec(dllexport) |
| 32 |
#else |
| 33 |
# define SYSTEM_API __declspec(dllimport) // BUGBUG: This is a plugin, who is going to import the functions directly? |
| 34 |
#endif |
| 35 |
|
| 36 |
#define SYS_ALIGNUP(num, al) ( ((num)+((al)-1)) & ~((al)-1) ) |
| 37 |
#define SYS_UNSAFEALIGNON(num, al) ( (num) % (al) == 0 ? (num) : SYS_ALIGNUP((num), (al)) ) // al CANNOT be 0! |
| 38 |
#define SYS_ALIGNON(num, al) ( (al) ? SYS_UNSAFEALIGNON((num), (al)) : (num) ) |
| 39 |
|
| 40 |
#define NEW_STACK_SIZE 256*256 |
| 41 |
|
| 42 |
// Proc types: |
| 43 |
#define PT_NOTHING 0 |
| 44 |
#define PT_PROC 1 |
| 45 |
#define PT_STRUCT 2 |
| 46 |
#define PT_VTABLEPROC 3 |
| 47 |
|
| 48 |
// Proc results: |
| 49 |
#define PR_OK 0 |
| 50 |
#define PR_ERROR -1 |
| 51 |
#define PR_CALLBACK 1 |
| 52 |
|
| 53 |
// Real world argument types |
| 54 |
#define PAT_VOID 0 |
| 55 |
#define PAT_INT 1 |
| 56 |
#define PAT_LONG 2 |
| 57 |
#define PAT_STRING 3 |
| 58 |
#define PAT_WSTRING 4 |
| 59 |
#define PAT_GUID 5 |
| 60 |
#define PAT_CALLBACK 6 |
| 61 |
#define PAT_REGMEM 7 |
| 62 |
#ifdef _UNICODE |
| 63 |
#define PAT_TSTRING PAT_WSTRING |
| 64 |
#else |
| 65 |
#define PAT_TSTRING PAT_STRING |
| 66 |
#endif |
| 67 |
#define PAT_PTR ( (4==sizeof(void*)) ? PAT_INT : PAT_LONG ) |
| 68 |
#define PAT_ALIGNFLAG 0x8000 // Type is aligned to its natural alignment |
| 69 |
|
| 70 |
// Input/Output Source/Destination |
| 71 |
#define IOT_NONE 0 |
| 72 |
#define IOT_STACK -1 |
| 73 |
#define IOT_REG 1 |
| 74 |
#define IOT_INLINE (__INST_LAST+1) // should replace pointer to inline input |
| 75 |
// #define INLINE_INPUT -> any other value, will contain pointer to input string |
| 76 |
|
| 77 |
// Options |
| 78 |
#define POPT_CDECL 0x1 // (Option & 0x1) == 0x1 -> cdecl, otherwise stdcall |
| 79 |
#define POPT_PERMANENT 0x2 // Permanent proc, will not be destroyed after calling |
| 80 |
#define POPT_ALWRETURN 0x4 // Always return |
| 81 |
#define POPT_NEVERREDEF 0x8 // Never redefine |
| 82 |
#define POPT_GENSTACK 0x10 // Use general stack (non temporary for callback) |
| 83 |
#define POPT_ERROR 0x20 // Call GetLastError after proc and push it to stack |
| 84 |
#define POPT_UNLOAD 0x40 // unload dll after call |
| 85 |
#define POPT_CLONE 0x80 // Callback clone |
| 86 |
#define POPT_SYNTAX2 0x100 // "?2" syntax mode (direct callback ids and aligned uppercased types) |
| 87 |
|
| 88 |
// Proc argument (ProcParameter) options |
| 89 |
#define PAO_PTRFLAG -1 // Could be changed to 0x80000000 if we need to support "*&iN" |
| 90 |
#define PAO_ARRBASE 1 |
| 91 |
#define ParamOptionIsPointer(opt) ( (opt) < 0 ) |
| 92 |
|
| 93 |
typedef struct |
| 94 |
{ |
| 95 |
int Type; // Can be ORed with PAT_ALIGNFLAG to request alignment in structs |
| 96 |
int Option; // PAO_PTRFLAG -> Pointer, PAO_ARRBASE-... -> Special+PAO_ARRBASE |
| 97 |
INT_PTR Value; // it can hold any pointer sized value |
| 98 |
#ifndef _WIN64 |
| 99 |
int _value; // Upper 32 bits of Value when type is 64 bit (2 pushes) |
| 100 |
#endif |
| 101 |
int Size; // Value real size (should be either 1 or 2 (the number of pushes)) |
| 102 |
int Output; |
| 103 |
INT_PTR Input; |
| 104 |
HGLOBAL allocatedBlock; // block allocated for passing string, wstring or guid param |
| 105 |
} ProcParameter; |
| 106 |
|
| 107 |
#define ParamIsSimple(par) ( (par).Option == 0 ) |
| 108 |
#define ParamIsPointer(par) ParamOptionIsPointer((par).Option) |
| 109 |
#define ParamIsArray(par) ( (par).Option > 0 ) // AKA special |
| 110 |
#define GetParamArrayTypeSize GetSpecialParamTypeSize |
| 111 |
#ifdef POPT_SYNTAX2 |
| 112 |
#define GetParamType(par) ( (BYTE) (par).Type ) |
| 113 |
#else |
| 114 |
#define GetParamType(par) ( (par).Type ) |
| 115 |
#endif |
| 116 |
|
| 117 |
// Our single proc (Since the user will free proc with GlobalFree, |
| 118 |
// I've declared all variables as statics) |
| 119 |
typedef struct tag_SystemProc SystemProc; |
| 120 |
struct tag_SystemProc |
| 121 |
{ |
| 122 |
int ProcType; |
| 123 |
int ProcResult; |
| 124 |
TCHAR DllName[1024]; |
| 125 |
TCHAR ProcName[1024]; |
| 126 |
HMODULE Dll; |
| 127 |
HANDLE Proc; |
| 128 |
int Options; |
| 129 |
int ParamCount; |
| 130 |
ProcParameter Params[100]; // I hope nobody will use more than 100 params |
| 131 |
|
| 132 |
// Callback specific |
| 133 |
int CallbackIndex; |
| 134 |
int ArgsSize; |
| 135 |
// Clone of current element (used for multi-level callbacks) |
| 136 |
SystemProc *Clone; |
| 137 |
}; |
| 138 |
|
| 139 |
typedef struct tag_CallbackThunk CallbackThunk; |
| 140 |
struct tag_CallbackThunk |
| 141 |
{ |
| 142 |
#ifdef SYSTEM_X86 |
| 143 |
/* |
| 144 |
#pragma pack(push,1) |
| 145 |
char mov_eax_imm; |
| 146 |
int sysprocptr; |
| 147 |
char reljmp_imm; |
| 148 |
int realprocaddr; |
| 149 |
#pragma pack(pop) |
| 150 |
*/ |
| 151 |
char asm_code[10]; |
| 152 |
#elif defined(SYSTEM_AMD64) || defined(SYSTEM_ARM64) |
| 153 |
char asm_code[BUGBUG64(1)]; // TODO: BUGBUG64 |
| 154 |
#else |
| 155 |
#error "Asm thunk not implemented for this architecture!" |
| 156 |
#endif |
| 157 |
|
| 158 |
CallbackThunk* pNext; |
| 159 |
}; |
| 160 |
|
| 161 |
// Free() only knows about pNext in CallbackThunk, it does not know anything about the assembly, that is where this helper comes in... |
| 162 |
#ifdef SYSTEM_X86 |
| 163 |
# define GetAssociatedSysProcFromCallbackThunkPtr(pCbT) ( (SystemProc*) *(unsigned int*) (((char*)(pCbT))+1) ) |
| 164 |
#elif defined(SYSTEM_AMD64) || defined(SYSTEM_ARM64) |
| 165 |
# define GetAssociatedSysProcFromCallbackThunkPtr(pCbT) BUGBUG64(NULL) |
| 166 |
#else |
| 167 |
# error "GetAssociatedSysProcFromCallbackThunkPtr not defined for the current architecture!" |
| 168 |
#endif |
| 169 |
|
| 170 |
|
| 171 |
extern const int ParamSizeByType[]; // Size of every parameter type (*4 bytes) |
| 172 |
|
| 173 |
extern HANDLE CreateCallback(SystemProc *cbproc); |
| 174 |
extern SystemProc* PrepareProc(BOOL NeedForCall); |
| 175 |
extern void ParamAllocate(SystemProc *proc); |
| 176 |
extern void ParamsDeAllocate(SystemProc *proc); |
| 177 |
extern void ParamsIn(SystemProc *proc); |
| 178 |
extern void ParamsOut(SystemProc *proc); |
| 179 |
#ifdef SYSTEM_AMD64 |
| 180 |
extern SystemProc* CallProc2(SystemProc *proc, UINT_PTR ParamCount); |
| 181 |
#define CallProc(p) CallProc2((p), (p)->ParamCount) // ParamCount is passed as a parameter so CallProc2 can determine the required stack size without a function call |
| 182 |
#else //! SYSTEM_AMD64 |
| 183 |
extern SystemProc* CallProc(SystemProc *proc); |
| 184 |
#endif //~ SYSTEM_AMD64 |
| 185 |
#ifndef SYSTEM_NOCALLBACKS |
| 186 |
extern SystemProc* CallBack(SystemProc *proc); |
| 187 |
extern SystemProc* RealCallBack(); |
| 188 |
#endif |
| 189 |
extern void CallStruct(SystemProc *proc); |
| 190 |
|
| 191 |
#ifdef _UNICODE |
| 192 |
# define STRSET2CH(str, c1, c2) ( *(DWORD*)(str) = ((c1)|(c2)<<16) ) |
| 193 |
#else |
| 194 |
# define STRSET2CH(str, c1, c2) ( *(WORD*)(str) = ((c1)|(c2)<<8) ) |
| 195 |
#endif |
| 196 |
|
| 197 |
#endif |