-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcsaori_util.cpp
306 lines (274 loc) · 8.14 KB
/
csaori_util.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//setlocale//
#ifdef _MSC_VER
#if _MSC_VER >= 1400
#pragma setlocale("japanese")
#endif
#endif
//setlocale end//
/*
* csaori_base.cpp
*/
#ifdef _MSC_VER
#pragma warning( disable : 4786 )
#pragma warning( disable : 4996 )
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <locale>
#include <process.h>
#include "csaori_base.h"
//////////WINDOWS DEFINE///////////////////////////
#ifdef _WINDOWS
#ifdef _DEBUG
#include <crtdbg.h>
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#endif
///////////////////////////////////////////////////
//------------------------------------------------------------------------------
// Utility functions
//------------------------------------------------------------------------------
namespace SAORI_FUNC{
template<> string_t numToString<unsigned char>(unsigned char num){
char_t buf[32];
swprintf(buf,L"%u",num);
return buf;
}
template<> string_t numToString<unsigned int>(unsigned int num){
char_t buf[32];
swprintf(buf,L"%u",num);
return buf;
}
template<> string_t numToString<unsigned long>(unsigned long num){
char_t buf[32];
swprintf(buf,L"%u",num);
return buf;
}
std::string UnicodeToMultiByte(const wchar_t *Source, unsigned int CodePage, DWORD Flags)
{
if ( Source && *Source ) {
if (int Len = ::WideCharToMultiByte(CodePage, Flags, Source, (int)wcslen(Source), NULL, 0, NULL, NULL)) {
std::vector<char> Dest(Len);
if (Len = ::WideCharToMultiByte(CodePage, Flags, Source, (int)wcslen(Source), &Dest[0], static_cast<int>(Dest.size()), NULL, NULL)) {
return std::string(Dest.begin(), Dest.begin() + Len);
}
}
}
return "";
}
std::wstring MultiByteToUnicode(const char* Source, unsigned int CodePage, DWORD Flags)
{
if ( Source && *Source ) {
if (int Len = ::MultiByteToWideChar(CodePage, Flags, Source, (int)strlen(Source), NULL, 0)) {
std::vector<wchar_t> Dest(Len);
if (Len = ::MultiByteToWideChar(CodePage, 0, Source, (int)strlen(Source), &Dest[0], static_cast<int>(Dest.size()))) {
return std::wstring(Dest.begin(), Dest.begin() + Len);
}
}
}
return L"";
}
std::wstring CodePagetoString(unsigned int cset){
switch(cset){
case CP_SJIS:
return L"Shift_JIS";
case CP_ISO2022JP:
return L"ISO-2022-JP";
case CP_EUCJP:
return L"EUC-JP";
case CP_UTF8:
return L"UTF-8";
}
return L"unknown charset";
}
UINT StringtoCodePage(const char *str)
{
if ( str && *str ) {
if ( strnicmp(str,"shift_jis",9) == 0 ) {
return CP_SJIS;
}
if ( strnicmp(str,"x-sjis",6) == 0 ) {
return CP_SJIS;
}
if ( strnicmp(str,"iso-2022-jp",11) == 0 ) {
return CP_ISO2022JP;
}
if ( strnicmp(str,"euc-jp",6) == 0 ) {
return CP_EUCJP;
}
if ( strnicmp(str,"x-euc-jp",8) == 0 ) {
return CP_EUCJP;
}
if ( strnicmp(str,"utf-8",5) == 0 ) {
return CP_UTF8;
}
}
return CP_SJIS;
}
string_t getResultString(int rc)
{
const char_t *p;
switch(rc) {
case 200:
p = L"OK"; break;
case 204:
p = L"No Content"; break;
case 210:
p = L"Break"; break; // SSTP Break (SSTP session aborted)
case 300:
p = L"Communicate"; break; // (obsolete)
case 311:
p = L"Not Enough"; break; // SHIORI/2.4 TEACH...need more info.
case 312:
p = L"Advice"; break; // SHIORI/2.4 TEACH...drop recent reference
case 400:
p = L"Bad Request"; break;
case 401:
p = L"Unauthorized"; break;
case 403:
p = L"Forbidden"; break;
case 404:
p = L"Not Found"; break;
case 405:
p = L"Method Not Allowed"; break;
case 406:
p = L"Not Acceptable"; break;
case 408:
p = L"Request Timeout"; break;
case 409:
p = L"Conflict"; break;
case 420:
p = L"Refuse"; break; // SSTP refused by ghost
case 500:
p = L"Internal Server Error"; break;
case 501:
p = L"Not Implemented"; break;
case 503:
p = L"Service Unavailable"; break;
case 510:
p = L"Not Local IP"; break; // SSTP from Non-Local IP
case 511:
p = L"In Black List"; break; // SSTP from "Black-Listed" IP
case 512:
p = L"Invisible"; break; // SSTP inivisible (not proceed)
default:
p = L"Unknown Error";
}
return string_t(p);
}
// cut one line
// return ... first position of next line or string::npos if not found.
string_t::size_type getLine(string_t &sl, const string_t &src, string_t::size_type tpos)
{
string_t::size_type len = src.size();
if (tpos == string_t::npos || tpos >= len) {
sl = L"";
return string_t::npos;
}
string_t::size_type ppos = src.find_first_of(L"\r\n", tpos);
if (ppos == string_t::npos) {
sl = src.substr(tpos);
return ppos;
}
sl = src.substr(tpos, ppos - tpos);
ppos++;
if (ppos >= len) return string_t::npos;
if (src[ppos - 1] == '\r' && src[ppos] == '\n') {
ppos++;
if (ppos >= len) return string_t::npos;
}
return ppos;
}
string_t replaceAll(string_t string, const string_t &find, const string_t &replace) {
string_t::size_type found = std::string::npos;
while((found = string.find(find, found)) != std::string::npos) {
string.replace(found, find.length(), replace);
found += replace.size();
}
return string;
}
std::string replaceAll(std::string string, const std::string &find, const std::string &replace) {
std::string::size_type found = std::string::npos;
while((found = string.find(find, found)) != std::string::npos) {
string.replace(found, find.length(), replace);
found += replace.size();
}
return string;
}
typedef struct tagMessageBoxInfo {
void *hwnd;
string_t message;
string_t title;
unsigned int flags;
} MessageBoxInfo;
static void __cdecl AsyncMessageBoxProc(void *p)
{
MessageBoxInfo *pInf = reinterpret_cast<MessageBoxInfo*>(p);
::MessageBoxW((HWND)pInf->hwnd,pInf->message.c_str(),pInf->title.c_str(),pInf->flags);
delete pInf;
}
void AsyncMessageBox(void *hwnd,char_t *message,char_t *title,unsigned int flags)
{
MessageBoxInfo *pMsg = new MessageBoxInfo;
pMsg->hwnd = hwnd;
pMsg->title = title ? title : L"";
pMsg->message = message ? message : L"";
pMsg->flags = flags;
_beginthread(AsyncMessageBoxProc,0,pMsg);
}
CCriticalSection::CCriticalSection(void) : init(false)
{
//Vista以降のメモリリークとマルチスレッドのパフォーマンス対策
typedef BOOL (WINAPI *FInitializeCriticalSectionEx)(LPCRITICAL_SECTION lpCriticalSection,DWORD dwSpinCount,DWORD Flags);
typedef BOOL (WINAPI *FInitializeCriticalSectionAndSpinCount)(LPCRITICAL_SECTION lpCriticalSection,DWORD dwSpinCoun);
static FInitializeCriticalSectionEx SInitializeCriticalSectionEx = NULL;
static FInitializeCriticalSectionAndSpinCount SInitializeCriticalSectionAndSpinCount = NULL;
if ( ! init ) {
OSVERSIONINFO inf;
inf.dwOSVersionInfoSize = sizeof(inf);
::GetVersionEx(&inf);
init = true;
if ( inf.dwMajorVersion >= 6 ) {
SInitializeCriticalSectionEx = reinterpret_cast<FInitializeCriticalSectionEx>(
::GetProcAddress(::GetModuleHandleA("kernel32"),"InitializeCriticalSectionEx") );
}
if ( inf.dwMajorVersion >= 5 ) {
SInitializeCriticalSectionAndSpinCount = reinterpret_cast<FInitializeCriticalSectionAndSpinCount>(
::GetProcAddress(::GetModuleHandleA("kernel32"),"InitializeCriticalSectionAndSpinCount") );
}
}
BOOL result;
if ( SInitializeCriticalSectionEx ) {
result = SInitializeCriticalSectionEx(&c,1500,0x01000000 /*CRITICAL_SECTION_NO_DEBUG_INFO*/);
}
else if ( SInitializeCriticalSectionAndSpinCount ) {
result = SInitializeCriticalSectionAndSpinCount(&c,1500);
}
else {
::InitializeCriticalSection(&c);
result = 1;
}
init = (result != 0);
}
CCriticalSection::~CCriticalSection()
{
if ( init ) {
::DeleteCriticalSection(&c);
}
}
void CCriticalSection::Enter(void)
{
if ( init ) {
::EnterCriticalSection(&c);
}
}
void CCriticalSection::Leave(void)
{
if ( init ) {
::LeaveCriticalSection(&c);
}
}
}