~shimon/thd3d8mitigation

ref: 73e1b176f1a04e97cfd83a3fb41835b996960b4a thd3d8mitigation/util.c -rw-r--r-- 3.5 KiB
73e1b176Itoh Shimon 7 months ago
                                                                                
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
#include "thd3d8mitigation.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <mmsystem.h>

#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

HANDLE g_LogFile = NULL;

int myvasprintf_l(char** strp, const char* fmt, _locale_t loc, va_list ap)
{
	va_list ap2;
	int len;

	va_copy(ap2, ap);
	len = _vscprintf_l(fmt, loc, ap);
	va_end(ap2);
	if (len < 0)
		return -1;

	if ((*strp = malloc(len + 1)) == NULL)
		return -1;

	if ((len = _vsnprintf_s_l(*strp, len + 1, _TRUNCATE, fmt, loc, ap)) < 0)
	{
		free(*strp);
		return -1;
	}

	return len;
}

int myvasprintf(char** strp, const char* fmt, va_list ap)
{
	_locale_t loc;
	int ret;

	loc = _get_current_locale();
	ret = myvasprintf_l(strp, fmt, loc, ap);
	_free_locale(loc);

	return ret;
}

int myasprintf(char** strp, const char* fmt, ...)
{
	va_list ap;
	int len;

	va_start(ap, fmt);
	len = myvasprintf(strp, fmt, ap);
	va_end(ap);
	return len;
}

int AllocateErrorMessageA(DWORD code, char** strp)
{
	char* errmsg;
	int len;

	if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL, code, 0, (LPSTR)&errmsg, 0, NULL) == 0)
		return -1;
	len = myasprintf(strp, "%s", errmsg);
	LocalFree(errmsg);
	return len;
}

void VLog(const char* fmt, va_list ap)
{
	char *origmsg = NULL, *msg = NULL;
	DWORD tmp, now;

	if (myvasprintf(&origmsg, fmt, ap) < 0)
	{
		OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": warning: myvasprintf failed.\r\n");
		goto cleanup;
	}

	timeBeginPeriod(1);
	now = timeGetTime();
	timeEndPeriod(1);

	if (myasprintf(&msg, "%s[System time: %010lu][Thread: %010lu]: %s\r\n", LOG_PREFIX, now, GetCurrentThreadId(),
			origmsg) < 0)
	{
		OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": warning: myasprintf failed.\r\n");
		goto cleanup;
	}

	OutputDebugStringA(msg);
	EnterCriticalSection(&g_CS);
	if (g_LogFile != NULL)
		if (!WriteFile(g_LogFile, msg, strlen(msg), &tmp, NULL))
			OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": warning: WriteFile failed.\r\n");
	LeaveCriticalSection(&g_CS);

cleanup:
	free(msg);
	free(origmsg);
}

void Log(const char* fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	VLog(fmt, ap);
	va_end(ap);
}

void VLogWithErrorCode(DWORD err, const char* fmt, va_list ap)
{
	char *origmsg = NULL, *msg = NULL, *errmsg = NULL;
	DWORD tmp, now;

	if (AllocateErrorMessageA(err, &errmsg) < 0)
	{
		OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": warning: AllocateErrorMessage failed.\r\n");
		goto cleanup;
	}

	if (myvasprintf(&origmsg, fmt, ap) < 0)
	{
		OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": warning: myvasprintf failed.\r\n");
		goto cleanup;
	}

	timeBeginPeriod(1);
	now = timeGetTime();
	timeEndPeriod(1);

	if (myasprintf(&msg, "%s[System time: %010lu][Thread: %010lu]: %s: error code: 0x%lx (%s)\r\n", LOG_PREFIX, now,
			GetCurrentThreadId(), origmsg, err, errmsg) < 0)
	{
		OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": warning: myasprintf failed.\r\n");
		goto cleanup;
	}

	OutputDebugStringA(msg);
	EnterCriticalSection(&g_CS);
	if (g_LogFile != NULL)
		if (!WriteFile(g_LogFile, msg, strlen(msg), &tmp, NULL))
			OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": warning: WriteFile failed.\r\n");
	LeaveCriticalSection(&g_CS);

cleanup:
	free(msg);
	free(origmsg);
	free(errmsg);
}

void LogWithErrorCode(DWORD err, const char* fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	VLogWithErrorCode(err, fmt, ap);
	va_end(ap);
}

__declspec(noreturn) void Fatal(int exitcode, const char* fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	VLog(fmt, ap);
	va_end(ap);
	ExitProcess(exitcode);
}