ReactOS 0.4.15-dev-8191-gbc6c731
shimlib.c File Reference
#include <windef.h>
#include <winbase.h>
#include <shimlib.h>
#include <strsafe.h>
#include <ndk/rtlfuncs.h>
Include dependency graph for shimlib.c:

Go to the source code of this file.

Classes

struct  UsedShim
 

Macros

#define WIN32_NO_STATUS
 

Typedefs

typedef struct UsedShim UsedShim
 
typedef struct UsedShimpUsedShim
 

Functions

void ShimLib_Init (HINSTANCE hInstance)
 
void ShimLib_Deinit (VOID)
 
PVOID ShimLib_ShimMalloc (SIZE_T dwSize)
 
void ShimLib_ShimFree (PVOID pData)
 
HINSTANCE ShimLib_Instance (VOID)
 
PCSTR ShimLib_StringNDuplicateA (PCSTR szString, SIZE_T stringLengthIncludingNullTerm)
 
PCSTR ShimLib_StringDuplicateA (PCSTR szString)
 
BOOL ShimLib_StrAEqualsWNC (PCSTR szString, PCWSTR wszString)
 
 _SHMALLOC (".shm$AAA")
 
 _SHMALLOC (".shm$ZZZ")
 
PHOOKAPI WINAPI ShimLib_GetHookAPIs (IN LPCSTR szCommandLine, IN LPCWSTR wszShimName, OUT PDWORD pdwHookCount)
 
BOOL WINAPI ShimLib_NotifyShims (DWORD fdwReason, PVOID ptr)
 
VOID SeiInitDebugSupport (VOID)
 
BOOL WINAPIV SeiDbgPrint (SEI_LOG_LEVEL Level, PCSTR Function, PCSTR Format,...)
 

Variables

ULONG g_ShimEngDebugLevel = 0xffffffff
 
static HINSTANCE g_ShimLib_hInstance
 
static HANDLE g_ShimLib_Heap
 
static PSLIST_HEADER g_UsedShims
 

Macro Definition Documentation

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 8 of file shimlib.c.

Typedef Documentation

◆ pUsedShim

◆ UsedShim

Function Documentation

◆ _SHMALLOC() [1/2]

_SHMALLOC ( ".shm$AAA"  )

Definition at line 103 of file shimlib.c.

103{ 0 };

◆ _SHMALLOC() [2/2]

_SHMALLOC ( ".shm$ZZZ"  )

Definition at line 104 of file shimlib.c.

104{ 0 };

◆ SeiDbgPrint()

BOOL WINAPIV SeiDbgPrint ( SEI_LOG_LEVEL  Level,
PCSTR  Function,
PCSTR  Format,
  ... 
)

Outputs diagnostic info.

Parameters
[in]LevelThe level to log this message with, choose any of [SHIM_ERR, SHIM_WARN, SHIM_INFO].
[in]FunctionNameThe function this log should be attributed to.
[in]FormatThe format string.
...Variable arguments providing additional information.
Returns
Success: TRUE Failure: FALSE.

Definition at line 197 of file shimlib.c.

198{
199 char Buffer[512];
200 char* Current = Buffer;
201 const char* LevelStr;
202 size_t Length = sizeof(Buffer);
203 va_list ArgList;
204 HRESULT hr;
205
206 if (g_ShimEngDebugLevel == 0xffffffff)
208
210 return FALSE;
211
212 switch (Level)
213 {
214 case SEI_MSG:
215 LevelStr = "MSG ";
216 break;
217 case SEI_FAIL:
218 LevelStr = "FAIL";
219 break;
220 case SEI_WARN:
221 LevelStr = "WARN";
222 break;
223 case SEI_INFO:
224 LevelStr = "INFO";
225 break;
226 default:
227 LevelStr = "USER";
228 break;
229 }
230
231 if (Function)
232 hr = StringCchPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, "[%s] [%s] ", LevelStr, Function);
233 else
234 hr = StringCchPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, "[%s] ", LevelStr);
235
236 if (!SUCCEEDED(hr))
237 return FALSE;
238
239 va_start(ArgList, Format);
240 hr = StringCchVPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, Format, ArgList);
241 va_end(ArgList);
242 if (!SUCCEEDED(hr))
243 return FALSE;
244
245 DbgPrint("%s", Buffer);
246 return TRUE;
247}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
_In_ CDROM_SCAN_FOR_SPECIAL_INFO _In_ PCDROM_SCAN_FOR_SPECIAL_HANDLER Function
Definition: cdrom.h:1156
Definition: bufpool.h:45
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define DbgPrint
Definition: hal.h:12
#define SUCCEEDED(hr)
Definition: intsafe.h:50
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STRSAFE_NULL_ON_FAILURE
Definition: ntstrsafe.h:34
VOID SeiInitDebugSupport(VOID)
Definition: shimlib.c:165
ULONG g_ShimEngDebugLevel
Definition: shimlib.c:25
@ SEI_FAIL
Definition: shimlib.h:59
@ SEI_MSG
Definition: shimlib.h:58
@ SEI_INFO
Definition: shimlib.h:61
@ SEI_WARN
Definition: shimlib.h:60
HRESULT hr
Definition: shlfolder.c:183
STRSAFEAPI StringCchVPrintfExA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPSTR *ppszDestEnd, size_t *pcchRemaining, STRSAFE_DWORD dwFlags, STRSAFE_LPCSTR pszFormat, va_list argList)
Definition: strsafe.h:650
STRSAFEAPI StringCchPrintfExA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPSTR *ppszDestEnd, size_t *pcchRemaining, STRSAFE_DWORD dwFlags, STRSAFE_LPCSTR pszFormat,...)
Definition: strsafe.h:575
_IRQL_requires_same_ typedef _In_ ULONG _In_ UCHAR Level
Definition: wmitypes.h:56

◆ SeiInitDebugSupport()

VOID SeiInitDebugSupport ( VOID  )

Definition at line 165 of file shimlib.c.

166{
167 static const UNICODE_STRING DebugKey = RTL_CONSTANT_STRING(L"SHIM_DEBUG_LEVEL");
168 UNICODE_STRING DebugValue;
170 ULONG NewLevel = SEI_MSG;
171 WCHAR Buffer[40];
172
173 RtlInitEmptyUnicodeString(&DebugValue, Buffer, sizeof(Buffer));
174
175 Status = RtlQueryEnvironmentVariable_U(NULL, &DebugKey, &DebugValue);
176
177 if (NT_SUCCESS(Status))
178 {
179 if (!NT_SUCCESS(RtlUnicodeStringToInteger(&DebugValue, 10, &NewLevel)))
180 NewLevel = 0;
181 }
182 g_ShimEngDebugLevel = NewLevel;
183}
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Status
Definition: gdiplustypes.h:25
NTSYSAPI NTSTATUS NTAPI RtlQueryEnvironmentVariable_U(_In_opt_ PWSTR Environment, _In_ PCUNICODE_STRING Name, _Out_ PUNICODE_STRING Value)
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToInteger(PUNICODE_STRING String, ULONG Base, PULONG Value)
#define L(x)
Definition: ntvdm.h:50
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
uint32_t ULONG
Definition: typedefs.h:59
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by SeiDbgPrint().

◆ ShimLib_Deinit()

void ShimLib_Deinit ( VOID  )

Definition at line 39 of file shimlib.c.

40{
41 // Is this a good idea?
43}
BOOL WINAPI HeapDestroy(HANDLE hHeap)
Definition: heapmem.c:85
static HANDLE g_ShimLib_Heap
Definition: shimlib.c:27

Referenced by DllMain().

◆ ShimLib_GetHookAPIs()

PHOOKAPI WINAPI ShimLib_GetHookAPIs ( IN LPCSTR  szCommandLine,
IN LPCWSTR  wszShimName,
OUT PDWORD  pdwHookCount 
)

Definition at line 111 of file shimlib.c.

112{
113 PSHIMREG ps = &_shim_start;
114 ps++;
115 for (; ps < &_shim_end; ps++)
116 {
117 if (ps->GetHookAPIs != NULL && ps->ShimName != NULL)
118 {
119 if (ShimLib_StrAEqualsWNC(ps->ShimName, wszShimName))
120 {
122 shim->pShim = ps;
123#if (WINVER > _WIN32_WINNT_WS03)
124 shim->bInitCalled = FALSE;
125#endif
127
128 return ps->GetHookAPIs(SHIM_NOTIFY_ATTACH, szCommandLine, pdwHookCount);
129 }
130 }
131 }
132 return NULL;
133}
static PSLIST_HEADER g_UsedShims
Definition: shimlib.c:28
PVOID ShimLib_ShimMalloc(SIZE_T dwSize)
Definition: shimlib.c:45
struct UsedShim * pUsedShim
BOOL ShimLib_StrAEqualsWNC(PCSTR szString, PCWSTR wszString)
Definition: shimlib.c:72
#define SHIM_NOTIFY_ATTACH
Definition: shimlib.h:50
SLIST_ENTRY Entry
Definition: shimlib.c:17
PSHIMREG pShim
Definition: shimlib.c:18
PCSTR ShimName
Definition: shimlib.h:88
_PVGetHookAPIs GetHookAPIs
Definition: shimlib.h:86
NTSYSAPI PSLIST_ENTRY NTAPI RtlInterlockedPushEntrySList(_Inout_ PSLIST_HEADER ListHead, _Inout_ __drv_aliasesMem PSLIST_ENTRY ListEntry)
Definition: slist.c:226

Referenced by GetHookAPIs().

◆ ShimLib_Init()

void ShimLib_Init ( HINSTANCE  hInstance)

Definition at line 30 of file shimlib.c.

31{
33 g_ShimLib_Heap = HeapCreate(0, 0x10000, 0);
34
37}
HINSTANCE hInstance
Definition: charmap.c:19
HANDLE WINAPI HeapCreate(DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize)
Definition: heapmem.c:45
static HINSTANCE g_ShimLib_hInstance
Definition: shimlib.c:26
NTSYSAPI VOID NTAPI RtlInitializeSListHead(_Out_ PSLIST_HEADER ListHead)
Definition: slist.c:25
union _SLIST_HEADER * PSLIST_HEADER

Referenced by DllMain().

◆ ShimLib_Instance()

◆ ShimLib_NotifyShims()

BOOL WINAPI ShimLib_NotifyShims ( DWORD  fdwReason,
PVOID  ptr 
)

Definition at line 136 of file shimlib.c.

137{
139
140 if (fdwReason < SHIM_REASON_INIT)
141 fdwReason += (SHIM_REASON_INIT - SHIM_NOTIFY_ATTACH);
142
143 while (pEntry)
144 {
146 _PVNotify Notify = pUsed->pShim->Notify;
147#if (WINVER > _WIN32_WINNT_WS03)
148 if (pUsed->bInitCalled && fdwReason == SHIM_REASON_INIT)
149 Notify = NULL;
150#endif
151 if (Notify)
152 Notify(fdwReason, ptr);
153#if (WINVER > _WIN32_WINNT_WS03)
154 if (fdwReason == SHIM_REASON_INIT)
155 pUsed->bInitCalled = TRUE;
156#endif
157
158 pEntry = pEntry->Next;
159 }
160
161 return TRUE;
162}
BOOL WINAPI SHIM_OBJ_NAME() Notify(DWORD fdwReason, PVOID ptr)
PLIST_ENTRY pEntry
Definition: fxioqueue.cpp:4484
static PVOID ptr
Definition: dispmode.c:27
#define SHIM_REASON_INIT
Definition: shimlib.h:45
BOOL(WINAPI * _PVNotify)(DWORD, PVOID)
Definition: shimlib.h:82
base of all file and directory entries
Definition: entries.h:83
_PVNotify Notify
Definition: shimlib.h:87
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
_Must_inspect_result_ NTSYSAPI PSLIST_ENTRY NTAPI RtlFirstEntrySList(_In_ const SLIST_HEADER *ListHead)
Definition: slist.c:51
#define PSLIST_ENTRY
Definition: rtltypes.h:134

Referenced by NotifyShims().

◆ ShimLib_ShimFree()

void ShimLib_ShimFree ( PVOID  pData)

Definition at line 50 of file shimlib.c.

51{
53}
#define HeapFree(x, y, z)
Definition: compat.h:735
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1830

Referenced by FreeLibrary(), and InitIgnoreFreeLibrary().

◆ ShimLib_ShimMalloc()

PVOID ShimLib_ShimMalloc ( SIZE_T  dwSize)

Definition at line 45 of file shimlib.c.

46{
48}
#define HeapAlloc
Definition: compat.h:733
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56

Referenced by FreeLibrary(), InitIgnoreFreeLibrary(), ShimLib_GetHookAPIs(), ShimLib_Init(), and ShimLib_StringNDuplicateA().

◆ ShimLib_StrAEqualsWNC()

BOOL ShimLib_StrAEqualsWNC ( PCSTR  szString,
PCWSTR  wszString 
)

Definition at line 72 of file shimlib.c.

73{
74 while (toupper(*szString) == towupper(*wszString))
75 {
76 if (!*szString)
77 return TRUE;
78
79 szString++; wszString++;
80 }
81 return FALSE;
82}
int toupper(int c)
Definition: utclib.c:881
#define towupper(c)
Definition: wctype.h:99

Referenced by ShimLib_GetHookAPIs().

◆ ShimLib_StringDuplicateA()

PCSTR ShimLib_StringDuplicateA ( PCSTR  szString)

Definition at line 67 of file shimlib.c.

68{
69 return ShimLib_StringNDuplicateA(szString, lstrlenA(szString) + 1);
70}
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
PCSTR ShimLib_StringNDuplicateA(PCSTR szString, SIZE_T stringLengthIncludingNullTerm)
Definition: shimlib.c:60

Referenced by InitIgnoreFreeLibrary().

◆ ShimLib_StringNDuplicateA()

PCSTR ShimLib_StringNDuplicateA ( PCSTR  szString,
SIZE_T  stringLengthIncludingNullTerm 
)

Definition at line 60 of file shimlib.c.

61{
62 PSTR NewString = ShimLib_ShimMalloc(stringLengthIncludingNullTerm);
63 StringCchCopyA(NewString, stringLengthIncludingNullTerm, szString);
64 return NewString;
65}
STRSAFEAPI StringCchCopyA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:145
char * PSTR
Definition: typedefs.h:51

Referenced by InitIgnoreFreeLibrary(), and ShimLib_StringDuplicateA().

Variable Documentation

◆ g_ShimEngDebugLevel

ULONG g_ShimEngDebugLevel = 0xffffffff

Definition at line 25 of file shimlib.c.

Referenced by SeiDbgPrint(), and SeiInitDebugSupport().

◆ g_ShimLib_Heap

HANDLE g_ShimLib_Heap
static

Definition at line 27 of file shimlib.c.

Referenced by ShimLib_Deinit(), ShimLib_Init(), ShimLib_ShimFree(), and ShimLib_ShimMalloc().

◆ g_ShimLib_hInstance

HINSTANCE g_ShimLib_hInstance
static

Definition at line 26 of file shimlib.c.

Referenced by ShimLib_Init(), and ShimLib_Instance().

◆ g_UsedShims

PSLIST_HEADER g_UsedShims
static

Definition at line 28 of file shimlib.c.

Referenced by ShimLib_GetHookAPIs(), ShimLib_Init(), and ShimLib_NotifyShims().