-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtil.cpp
200 lines (174 loc) · 6.39 KB
/
StringUtil.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
/******************************************************************************
* StringUtil.cpp
*****************************************************************************/
/******************************************************************************
* Includes
*****************************************************************************/
#include "stdafx.h"
#include "StringUtil.h"
/******************************************************************************
* Internal Functions Declarations
*****************************************************************************/
static char HexDigitValue(char cDigit);
/******************************************************************************
* CStringWToMultiByte Function
*****************************************************************************/
static CStringA CStringWToMultiByte(CStringW strUnicode, UINT ulCodePage)
{
CStringA sRet;
int nSize = WideCharToMultiByte(ulCodePage, 0, strUnicode, strUnicode.GetLength()+1, NULL, NULL, NULL, NULL);
if (nSize > 0)
{
WideCharToMultiByte(ulCodePage, 0, strUnicode, strUnicode.GetLength()+1, sRet.GetBuffer(nSize + 1), nSize + 1, NULL, NULL);
sRet.ReleaseBuffer();
}
return sRet;
}
/******************************************************************************
* CStringMultiByteToW Function
*****************************************************************************/
static CStringW CStringMultiByteToW(CStringA strMultiByte, UINT ulCodePage)
{
CStringW strRet;
int nUnicodeSize = MultiByteToWideChar(ulCodePage, 0, strMultiByte, strMultiByte.GetLength()+1, NULL, NULL);
if (nUnicodeSize > 0)
{
MultiByteToWideChar(ulCodePage, 0, strMultiByte, strMultiByte.GetLength()+1, strRet.GetBuffer(nUnicodeSize + 1), nUnicodeSize + 1);
strRet.ReleaseBuffer();
}
return strRet;
}
/******************************************************************************
* CStringWToUTF8 Function
*****************************************************************************/
CStringA CStringWToUTF8(CStringW strUnicode)
{
return CStringWToMultiByte(strUnicode, CP_UTF8);
}
/******************************************************************************
* CStringUTF8ToW Function
*****************************************************************************/
CStringW CStringUTF8ToW(CStringA strUTF8)
{
return CStringMultiByteToW(strUTF8, CP_UTF8);
}
/******************************************************************************
* CStringWToA Function
*****************************************************************************/
CStringA CStringWToA(CStringW strUnicode)
{
return CStringWToMultiByte(strUnicode, CP_ACP);
}
/******************************************************************************
* CStringAToW Function
*****************************************************************************/
CStringW CStringAToW(CStringA strAnsi)
{
return CStringMultiByteToW(strAnsi, CP_ACP);
}
/******************************************************************************
* CStringUTF8ToA Function
*****************************************************************************/
CStringA CStringUTF8ToA(CStringA strUtf8)
{
return CStringWToA(CStringUTF8ToW(strUtf8));
}
/******************************************************************************
* GUIDToCStringW Function
*****************************************************************************/
CStringW GUIDToCStringW(REFGUID tGuid)
{
CStringW strClsid;
StringFromGUID2(tGuid, strClsid.GetBuffer(GUID_STRING_SIZE), GUID_STRING_SIZE);
strClsid.ReleaseBuffer();
return strClsid;
}
/******************************************************************************
* GUIDToCStringA Function
*****************************************************************************/
CStringA GUIDToCStringA(REFGUID tGuid)
{
return CStringWToA(GUIDToCStringW(tGuid));
}
/******************************************************************************
* HexStringToBufferA Function
*****************************************************************************/
bool HexStringToBufferA(CStringA& strHex, OUT BYTE * acBuffer, IN size_t cbBufferSize)
{
/* Make sure the hex string is even */
if (0 != (strHex.GetLength() % 2))
{
return false;
}
/* Make sure the buffer is big enough */
size_t cbHexNeededBufferSize = strHex.GetLength() / 2;
if (cbBufferSize < cbHexNeededBufferSize)
{
return false;
}
LPCSTR pszHex = strHex.GetBuffer();
bool bRet = true;
for (size_t i = 0; i < cbHexNeededBufferSize; i++)
{
char cHigh = HexDigitValue(pszHex[i * 2]);
char cLow = HexDigitValue(pszHex[(i * 2) + 1]);
if ((cHigh < 0) || (cLow < 0))
{
bRet = false;
break;
}
acBuffer[i] = (cHigh << 4) | cLow;
}
strHex.ReleaseBuffer();
return bRet;
}
/******************************************************************************
* BufferToHexString Function
* Note: Based on zframe_strhex from 0MQ.
*****************************************************************************/
bool BufferToHexStringA(BYTE * pcBuffer, size_t cbBufferSize, OUT char * acHexString, IN size_t ccHexStringSize)
{
static const char acHexChars[] = "0123456789ABCDEF";
if (ccHexStringSize < ((cbBufferSize * 2) + 1))
{
return false;
}
for (size_t i = 0; i < cbBufferSize; i++)
{
acHexString[i * 2] = acHexChars[pcBuffer[i] >> 4];
acHexString[(i * 2) + 1] = acHexChars[pcBuffer[i] & 0x0f];
}
acHexString[cbBufferSize * 2] = '\0';
return true;
}
bool BufferToHexStringA(BYTE * pcBuffer, size_t cbBufferSize, OUT CStringA& strHex)
{
size_t ccHexStringLength = cbBufferSize * 2;
bool bRet = BufferToHexStringA(pcBuffer, cbBufferSize, strHex.GetBuffer(ccHexStringLength), ccHexStringLength + 1);
strHex.ReleaseBuffer();
return bRet;
}
/******************************************************************************
* Internal Functions Implementations
*****************************************************************************/
/******************************************************************************
* HexDigitValue Function
* Taken from UniCodeMultiByteTools.cpp (DS_CalcHexDigitValue)
*****************************************************************************/
static char HexDigitValue(char cDigit)
{
char cDigitValue = -1;
if (cDigit >= '0' && cDigit <= '9')
{
cDigitValue = cDigit - '0';
}
else if (cDigit >= 'a' && cDigit <= 'f')
{
cDigitValue = 10 + cDigit - 'a';
}
else if (cDigit >= 'A' && cDigit <= 'F')
{
cDigitValue = 10 + cDigit - 'A';
}
return cDigitValue;
}