3
3
#include " value.h"
4
4
#include " memory.h"
5
5
#include " exception.h"
6
+ #include " ntdll/ntdll.h"
6
7
7
8
std::unordered_map<String, FormatFunctions::Function> FormatFunctions::mFunctions ;
8
9
10
+ static FORMATRESULT formatErrorMsg (HMODULE DLL, const String & errName, DWORD code, char * dest, size_t destCount)
11
+ {
12
+ const NTSTATUS ErrorStatus = code;
13
+ PMESSAGE_RESOURCE_ENTRY Entry;
14
+ NTSTATUS Status = RtlFindMessage (DLL,
15
+ LDR_FORMAT_MESSAGE_FROM_SYSTEM_MESSAGE_TABLE,
16
+ MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
17
+ ErrorStatus,
18
+ &Entry);
19
+ if (!NT_SUCCESS (Status))
20
+ {
21
+ if (destCount < errName.size () + 1 )
22
+ return FORMAT_BUFFER_TOO_SMALL;
23
+ else
24
+ {
25
+ memcpy (dest, errName.c_str (), errName.size () + 1 );
26
+ return FORMAT_SUCCESS;
27
+ }
28
+ }
29
+
30
+ if ((Entry->Flags & MESSAGE_RESOURCE_UNICODE) != 0 )
31
+ {
32
+ String UTF8Description = StringUtils::Utf16ToUtf8 ((const wchar_t *)Entry->Text );
33
+ if (UTF8Description.size () + 3 + errName.size () > destCount)
34
+ return FORMAT_BUFFER_TOO_SMALL;
35
+ else
36
+ {
37
+ sprintf_s (dest, destCount, " %s: %s" , errName.c_str (), UTF8Description.c_str ());
38
+ return FORMAT_SUCCESS;
39
+ }
40
+ }
41
+ else
42
+ {
43
+ 0 ;// printf("%s\n", (const char*)Entry->Text);
44
+ String UTF8Description = StringUtils::LocalCpToUtf8 ((const char *)Entry->Text );
45
+ if (UTF8Description.size () + 3 + errName.size () > destCount)
46
+ return FORMAT_BUFFER_TOO_SMALL;
47
+ else
48
+ {
49
+ sprintf_s (dest, destCount, " %s: %s" , errName.c_str (), UTF8Description.c_str ());
50
+ return FORMAT_SUCCESS;
51
+ }
52
+ }
53
+ }
54
+
9
55
void FormatFunctions::Init ()
10
56
{
11
57
Register (" mem" , [](char * dest, size_t destCount, int argc, char * argv[], duint addr, void * userdata)
@@ -53,28 +99,8 @@ void FormatFunctions::Init()
53
99
#endif // _WIN64
54
100
if (errName.size () == 0 )
55
101
errName = StringUtils::sprintf (" %08X" , DWORD (code));
56
- DWORD success = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL , DWORD (code), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), helpMessage.data (), DWORD (destCount), NULL );
57
- if (success > 0 )
58
- {
59
- String UTF8ErrorMessage = StringUtils::Utf16ToUtf8 (helpMessage.data ());
60
- if (destCount < errName.size () + 3 + UTF8ErrorMessage.size ())
61
- return FORMAT_BUFFER_TOO_SMALL;
62
- else
63
- {
64
- sprintf_s (dest, destCount, " %s: %s" , errName.c_str (), UTF8ErrorMessage.c_str ());
65
- return FORMAT_SUCCESS;
66
- }
67
- }
68
- else
69
- {
70
- if (destCount < errName.size () + 1 )
71
- return FORMAT_BUFFER_TOO_SMALL;
72
- else
73
- {
74
- memcpy (dest, errName.c_str (), errName.size () + 1 );
75
- return FORMAT_SUCCESS;
76
- }
77
- }
102
+
103
+ return formatErrorMsg (GetModuleHandleW (L" kernel32.dll" ), errName, code, dest, destCount);
78
104
});
79
105
80
106
Register (" ntstatus" , [](char * dest, size_t destCount, int argc, char * argv[], duint code, void * userdata)
@@ -83,28 +109,8 @@ void FormatFunctions::Init()
83
109
String errName = NtStatusCodeToName ((unsigned int )code);
84
110
if (errName.size () == 0 )
85
111
errName = StringUtils::sprintf (" %08X" , DWORD (code));
86
- DWORD success = FormatMessageW (FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, GetModuleHandleW (L" ntdll.dll" ), DWORD (code), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), helpMessage.data (), DWORD (destCount), NULL );
87
- if (success > 0 )
88
- {
89
- String UTF8ErrorMessage = StringUtils::Utf16ToUtf8 (helpMessage.data ());
90
- if (destCount < errName.size () + 3 + UTF8ErrorMessage.size ())
91
- return FORMAT_BUFFER_TOO_SMALL;
92
- else
93
- {
94
- sprintf_s (dest, destCount, " %s: %s" , errName.c_str (), UTF8ErrorMessage.c_str ());
95
- return FORMAT_SUCCESS;
96
- }
97
- }
98
- else
99
- {
100
- if (destCount < errName.size () + 1 )
101
- return FORMAT_BUFFER_TOO_SMALL;
102
- else
103
- {
104
- memcpy (dest, errName.c_str (), errName.size () + 1 );
105
- return FORMAT_SUCCESS;
106
- }
107
- }
112
+
113
+ return formatErrorMsg (GetModuleHandleW (L" ntdll.dll" ), errName, code, dest, destCount);
108
114
});
109
115
}
110
116
0 commit comments