summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/WindowsStringConverter.cpp
blob: 6ecd6dd24905ed857434f73c32b99e191babde7e (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/StringConverter.h>

#ifdef _WIN32

#include <IceUtil/StringUtil.h>


using namespace Ice;
using namespace std;

namespace
{
//
// Converts to/from UTF-8 using MultiByteToWideChar and WideCharToMultiByte
//
class WindowsStringConverter : public StringConverter
{
public:

    explicit WindowsStringConverter(unsigned int);

    virtual Byte* toUTF8(const char*, const char*, UTF8Buffer&) const;

    virtual void fromUTF8(const Byte*, const Byte*, string& target) const;

private:
    unsigned int _cp;
    WstringConverterPtr _unicodeConverter;

};

WindowsStringConverter::WindowsStringConverter(unsigned int cp) :
    _cp(cp),
    _unicodeConverter(createUnicodeWstringConverter())
{
}

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 writtenWchar = 0;
    wstring 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
    {
        wbuffer.resize(wbuffer.size() == 0 ? sourceSize + 2 : 2 * wbuffer.size());
        writtenWchar = MultiByteToWideChar(_cp, flags, sourceStart, sourceSize,
                                           const_cast<wchar_t*>(wbuffer.data()), static_cast<int>(wbuffer.size()));
    } while(writtenWchar == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);

    if(writtenWchar == 0)
    {
        throw IllegalConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString());
    }

    wbuffer.resize(static_cast<size_t>(writtenWchar));

    //
    // Then convert this UTF-16 wbuffer into UTF-8
    //
    return _unicodeConverter->toUTF8(wbuffer.data(), wbuffer.data() + wbuffer.size(), 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;
    _unicodeConverter->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 writtenChar = -1;
    do
    {
        target.resize(writtenChar == -1 ?
                      std::max<size_t>(sourceEnd - sourceStart + 2, target.size()) :
                      2 * target.size());

        writtenChar = WideCharToMultiByte(_cp, flags, wtarget.data(), static_cast<int>(wtarget.size()),
                                          const_cast<char*>(target.data()), static_cast<int>(target.size()),
                                          0, 0);
    } while(writtenChar == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);

    if(writtenChar == 0)
    {
        throw IllegalConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString());
    }

    target.resize(static_cast<size_t>(writtenChar));
}
}

StringConverterPtr
Ice::createWindowsStringConverter(unsigned int cp)
{
    return ICE_MAKE_SHARED(WindowsStringConverter, cp);
}
#endif