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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
#include <IceUtil/StringConverter.h>
#include <IceUtil/MutexPtrLock.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/ScopedArray.h>
#include <IceUtil/StringUtil.h>
#include <IceUtil/Unicode.h>
using namespace IceUtil;
using namespace IceUtilInternal;
using namespace std;
namespace
{
IceUtil::Mutex* processStringConverterMutex = 0;
IceUtil::StringConverterPtr processStringConverter;
IceUtil::WstringConverterPtr processWstringConverter;
class Init
{
public:
Init()
{
processStringConverterMutex = new IceUtil::Mutex;
}
~Init()
{
delete processStringConverterMutex;
processStringConverterMutex = 0;
}
};
Init init;
}
namespace
{
class UTF8BufferI : public UTF8Buffer
{
public:
UTF8BufferI() :
_buffer(0),
_offset(0)
{
}
~UTF8BufferI()
{
free(_buffer);
}
Byte* getMoreBytes(size_t howMany, Byte* firstUnused)
{
if(_buffer == 0)
{
_buffer = (Byte*)malloc(howMany);
}
else
{
assert(firstUnused != 0);
_offset = firstUnused - _buffer;
_buffer = (Byte*)realloc(_buffer, _offset + howMany);
}
if(!_buffer)
{
throw std::bad_alloc();
}
return _buffer + _offset;
}
Byte* getBuffer()
{
return _buffer;
}
void reset()
{
free(_buffer);
_buffer = 0;
_offset = 0;
}
private:
IceUtil::Byte* _buffer;
size_t _offset;
};
}
UnicodeWstringConverter::UnicodeWstringConverter(ConversionFlags flags) :
_conversionFlags(flags)
{
}
Byte*
UnicodeWstringConverter::toUTF8(const wchar_t* sourceStart,
const wchar_t* sourceEnd,
UTF8Buffer& buffer) const
{
//
// The "chunk size" is the maximum of the number of characters in the
// source and 6 (== max bytes necessary to encode one Unicode character).
//
size_t chunkSize = std::max<size_t>(static_cast<size_t>(sourceEnd - sourceStart), 6);
Byte* targetStart = buffer.getMoreBytes(chunkSize, 0);
Byte* targetEnd = targetStart + chunkSize;
ConversionResult result;
while((result =
convertUTFWstringToUTF8(sourceStart, sourceEnd,
targetStart, targetEnd, _conversionFlags))
== targetExhausted)
{
targetStart = buffer.getMoreBytes(chunkSize, targetStart);
targetEnd = targetStart + chunkSize;
}
switch(result)
{
case conversionOK:
break;
case sourceExhausted:
throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "wide string source exhausted");
case sourceIllegal:
throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "wide string source illegal");
default:
{
assert(0);
throw IceUtil::IllegalConversionException(__FILE__, __LINE__);
}
}
return targetStart;
}
void
UnicodeWstringConverter::fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
wstring& target) const
{
if(sourceStart == sourceEnd)
{
target = L"";
return;
}
ConversionResult result =
convertUTF8ToUTFWstring(sourceStart, sourceEnd, target, _conversionFlags);
switch(result)
{
case conversionOK:
break;
case sourceExhausted:
throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "UTF-8 string source exhausted");
case sourceIllegal:
throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "UTF-8 string source illegal");
default:
{
assert(0);
throw IceUtil::IllegalConversionException(__FILE__, __LINE__);
}
}
}
#ifdef _WIN32
WindowsStringConverter::WindowsStringConverter(unsigned int cp) :
_cp(cp)
{
}
Byte*
WindowsStringConverter::toUTF8(const char* sourceStart,
const char* sourceEnd,
UTF8Buffer& buffer) const
{
//
// First convert to UTF-16
//
int sourceSize = static_cast<int>(sourceEnd - sourceStart);
if(sourceSize == 0)
{
return buffer.getMoreBytes(1, 0);
}
int size = 0;
int writtenWchar = 0;
ScopedArray<wchar_t> wbuffer;
//
// The following code pages doesn't support MB_ERR_INVALID_CHARS flag
// see http://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85).aspx
//
DWORD flags =
(_cp == 50220 || _cp == 50221 || _cp == 50222 ||
_cp == 50225 || _cp == 50227 || _cp == 50229 ||
_cp == 65000 || _cp == 42 || (_cp >= 57002 && _cp <= 57011)) ? 0 : MB_ERR_INVALID_CHARS;
do
{
size = size == 0 ? sourceSize + 2 : 2 * size;
wbuffer.reset(new wchar_t[size]);
writtenWchar = MultiByteToWideChar(_cp, flags, sourceStart,
sourceSize, wbuffer.get(), size);
} while(writtenWchar == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
if(writtenWchar == 0)
{
throw IllegalConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString());
}
//
// Then convert this UTF-16 wbuffer into UTF-8
//
return _unicodeWstringConverter.toUTF8(wbuffer.get(), wbuffer.get() + writtenWchar, buffer);
}
void
WindowsStringConverter::fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
string& target) const
{
if(sourceStart == sourceEnd)
{
target = "";
return;
}
if(_cp == CP_UTF8)
{
string tmp(reinterpret_cast<const char*>(sourceStart), sourceEnd - sourceStart);
tmp.swap(target);
return;
}
//
// First convert to wstring (UTF-16)
//
wstring wtarget;
_unicodeWstringConverter.fromUTF8(sourceStart, sourceEnd, wtarget);
//
// WC_ERR_INVALID_CHARS conversion flag is only supported with 65001 (UTF-8) and
// 54936 (GB18030 Simplified Chinese)
//
DWORD flags = (_cp == 65001 || _cp == 54936) ? WC_ERR_INVALID_CHARS : 0;
//
// And then to a multi-byte narrow string
//
int size = 0;
int writtenChar = 0;
ScopedArray<char> buffer;
do
{
size = size == 0 ? static_cast<int>(sourceEnd - sourceStart) + 2 : 2 * size;
buffer.reset(new char[size]);
writtenChar = WideCharToMultiByte(_cp, flags, wtarget.data(), static_cast<int>(wtarget.size()),
buffer.get(), size, 0, 0);
} while(writtenChar == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
if(writtenChar == 0)
{
throw IllegalConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString());
}
target.assign(buffer.get(), writtenChar);
}
#endif
StringConverterPtr
IceUtil::getProcessStringConverter()
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex);
return processStringConverter;
}
void
IceUtil::setProcessStringConverter(const StringConverterPtr& converter)
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex);
processStringConverter = converter;
}
WstringConverterPtr
IceUtil::getProcessWstringConverter()
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex);
return processWstringConverter;
}
void
IceUtil::setProcessWstringConverter(const WstringConverterPtr& converter)
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex);
processWstringConverter = converter;
}
string
IceUtil::wstringToString(const wstring& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter,
IceUtil::ConversionFlags flags)
{
string target;
if(!v.empty())
{
//
// First convert to UTF8 narrow string.
//
if(wConverter)
{
UTF8BufferI buffer;
Byte* last = wConverter->toUTF8(v.data(), v.data() + v.size(), buffer);
target = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer());
}
else
{
size_t size = v.size() * 4 * sizeof(char);
Byte* outBuf = new Byte[size];
Byte* targetStart = outBuf;
Byte* targetEnd = outBuf + size;
const wchar_t* sourceStart = v.data();
ConversionResult cr =
convertUTFWstringToUTF8(
sourceStart, sourceStart + v.size(),
targetStart, targetEnd, flags);
if(cr != conversionOK)
{
delete[] outBuf;
assert(cr == sourceExhausted || cr == sourceIllegal);
throw IllegalConversionException(__FILE__, __LINE__,
cr == sourceExhausted ? "partial character" : "bad encoding");
}
target = string(reinterpret_cast<char*>(outBuf), static_cast<size_t>(targetStart - outBuf));
delete[] outBuf;
}
//
// If narrow string converter is present convert to the native narrow string encoding, otherwise
// native narrow string encoding is UTF8 and we are done.
//
if(converter)
{
string tmp;
converter->fromUTF8(reinterpret_cast<const Byte*>(target.data()),
reinterpret_cast<const Byte*>(target.data() + target.size()), tmp);
tmp.swap(target);
}
}
return target;
}
wstring
IceUtil::stringToWstring(const string& v, const StringConverterPtr& converter,
const WstringConverterPtr& wConverter, IceUtil::ConversionFlags flags)
{
wstring target;
if(!v.empty())
{
//
// If there is a narrow string converter use it to convert to UTF8, otherwise the narrow
// string is already UTF8 encoded.
//
string tmp;
if(converter)
{
UTF8BufferI buffer;
Byte* last = converter->toUTF8(v.data(), v.data() + v.size(), buffer);
tmp = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer());
}
else
{
tmp = v;
}
//
// If there is a wide string converter use fromUTF8 to convert to the wide string native encoding.
//
if(wConverter)
{
wConverter->fromUTF8(reinterpret_cast<const Byte*>(tmp.data()),
reinterpret_cast<const Byte*>(tmp.data() + tmp.size()), target);
}
else
{
const Byte* sourceStart = reinterpret_cast<const Byte*>(tmp.data());
ConversionResult cr =
convertUTF8ToUTFWstring(sourceStart, sourceStart + tmp.size(), target, flags);
if(cr != conversionOK)
{
assert(cr == sourceExhausted || cr == sourceIllegal);
throw IllegalConversionException(__FILE__, __LINE__,
cr == sourceExhausted ? "partial character" : "bad encoding");
}
}
}
return target;
}
string
IceUtil::nativeToUTF8(const string& str, const IceUtil::StringConverterPtr& converter)
{
if(!converter || str.empty())
{
return str;
}
UTF8BufferI buffer;
Byte* last = converter->toUTF8(str.data(), str.data() + str.size(), buffer);
return string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer());
}
string
IceUtil::UTF8ToNative(const string& str, const IceUtil::StringConverterPtr& converter)
{
if(!converter || str.empty())
{
return str;
}
string tmp;
converter->fromUTF8(reinterpret_cast<const Byte*>(str.data()),
reinterpret_cast<const Byte*>(str.data() + str.size()), tmp);
return tmp;
}
|