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
|
/*
* Copyright (C) 2007-2009 Torch Mobile, Inc. All rights reserved.
* Copyright (C) 2010-2012 Patrick Gansterer <paroga@paroga.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* This library is distributed in the hope that i will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "TextCodecWin.h"
#include "COMPtr.h"
#include <mlang.h>
#include <windows.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringHash.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
struct CharsetInfo {
CString m_name;
String m_friendlyName;
UINT m_codePage;
Vector<CString> m_aliases;
};
class LanguageManager {
private:
LanguageManager();
friend LanguageManager& languageManager();
};
// Usage: a lookup table used to get CharsetInfo with code page ID.
// Key: code page ID. Value: charset information.
static HashMap<UINT, CString>& codePageCharsets()
{
static HashMap<UINT, CString> cc;
return cc;
}
static HashMap<String, CharsetInfo>& knownCharsets()
{
static HashMap<String, CharsetInfo> kc;
return kc;
}
// Usage: a map that stores charsets that are supported by system. Sorted by name.
// Key: charset. Value: code page ID.
typedef HashSet<String> CharsetSet;
static CharsetSet& supportedCharsets()
{
static CharsetSet sl;
return sl;
}
static LanguageManager& languageManager()
{
static LanguageManager lm;
return lm;
}
LanguageManager::LanguageManager()
{
COMPtr<IMultiLanguage> multiLanguage;
if (FAILED(::CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_INPROC_SERVER, IID_IMultiLanguage, reinterpret_cast<LPVOID*>(&multiLanguage))))
return;
COMPtr<IEnumCodePage> enumInterface;
if (FAILED(multiLanguage->EnumCodePages(MIMECONTF_BROWSER, &enumInterface)))
return;
MIMECPINFO cpInfo;
ULONG ccpInfo;
while (SUCCEEDED(enumInterface->Next(1, &cpInfo, &ccpInfo)) && ccpInfo) {
if (!IsValidCodePage(cpInfo.uiCodePage))
continue;
HashMap<UINT, CString>::iterator i = codePageCharsets().find(cpInfo.uiCodePage);
CString name(String(cpInfo.wszWebCharset).latin1());
if (i == codePageCharsets().end()) {
CharsetInfo info;
info.m_codePage = cpInfo.uiCodePage;
knownCharsets().set(name.data(), info);
i = codePageCharsets().set(cpInfo.uiCodePage, name).iterator;
}
if (i != codePageCharsets().end()) {
HashMap<String, CharsetInfo>::iterator j = knownCharsets().find(String(i->value.data(), i->value.length()));
ASSERT(j != knownCharsets().end());
CharsetInfo& info = j->value;
info.m_name = i->value.data();
info.m_friendlyName = cpInfo.wszDescription;
info.m_aliases.append(name);
info.m_aliases.append(String(cpInfo.wszHeaderCharset).latin1());
info.m_aliases.append(String(cpInfo.wszBodyCharset).latin1());
String cpName = "cp" + String::number(cpInfo.uiCodePage);
info.m_aliases.append(cpName.latin1());
supportedCharsets().add(i->value.data());
}
}
}
static UINT getCodePage(const char* name)
{
// Explicitly use a "const" reference to fix the silly VS build error
// saying "==" is not found for const_iterator and iterator
const HashMap<String, CharsetInfo>& charsets = knownCharsets();
HashMap<String, CharsetInfo>::const_iterator i = charsets.find(name);
return i == charsets.end() ? CP_ACP : i->value.m_codePage;
}
static PassOwnPtr<TextCodec> newTextCodecWin(const TextEncoding& encoding, const void*)
{
return adoptPtr(new TextCodecWin(getCodePage(encoding.name())));
}
TextCodecWin::TextCodecWin(UINT codePage)
: m_codePage(codePage)
{
}
TextCodecWin::~TextCodecWin()
{
}
void TextCodecWin::registerExtendedEncodingNames(EncodingNameRegistrar registrar)
{
languageManager();
for (CharsetSet::iterator i = supportedCharsets().begin(); i != supportedCharsets().end(); ++i) {
HashMap<String, CharsetInfo>::iterator j = knownCharsets().find(*i);
if (j != knownCharsets().end()) {
registrar(j->value.m_name.data(), j->value.m_name.data());
for (Vector<CString>::const_iterator alias = j->value.m_aliases.begin(); alias != j->value.m_aliases.end(); ++alias)
registrar(alias->data(), j->value.m_name.data());
}
}
}
void TextCodecWin::registerExtendedCodecs(TextCodecRegistrar registrar)
{
languageManager();
for (CharsetSet::iterator i = supportedCharsets().begin(); i != supportedCharsets().end(); ++i) {
HashMap<String, CharsetInfo>::iterator j = knownCharsets().find(*i);
if (j != knownCharsets().end())
registrar(j->value.m_name.data(), newTextCodecWin, 0);
}
}
static DWORD getCodePageFlags(UINT codePage)
{
if (codePage == 42) // Symbol
return 0;
// Microsoft says the flag must be 0 for the following code pages
if (codePage > 50000) {
if ((codePage >= 50220 && codePage <= 50222)
|| codePage == 50225
|| codePage == 50227
|| codePage == 50229
|| codePage == 52936
|| codePage == 54936
|| (codePage >= 57002 && codePage <= 57001)
|| codePage == 65000 // UTF-7
)
return 0;
}
return MB_PRECOMPOSED | MB_ERR_INVALID_CHARS;
}
static inline const char* findFirstNonAsciiCharacter(const char* bytes, size_t length)
{
for (const char* bytesEnd = bytes + length; bytes < bytesEnd; ++bytes) {
if (*bytes & 0x80)
break;
}
return bytes;
}
static void decodeInternal(Vector<UChar, 8192>& result, UINT codePage, const char* bytes, size_t length, size_t* left)
{
*left = length;
if (!bytes || !length)
return;
DWORD flags = getCodePageFlags(codePage);
int testLength = length;
int untestedLength = length;
for (;;) {
int resultLength = MultiByteToWideChar(codePage, flags, bytes, testLength, 0, 0);
if (resultLength > 0) {
int oldSize = result.size();
result.resize(oldSize + resultLength);
MultiByteToWideChar(codePage, flags, bytes, testLength, result.data() + oldSize, resultLength);
if (testLength == untestedLength) {
*left = length - testLength;
break;
}
untestedLength -= testLength;
length -= testLength;
bytes += testLength;
} else {
untestedLength = testLength - 1;
if (!untestedLength) {
*left = length;
break;
}
}
testLength = (untestedLength + 1) / 2;
}
}
String TextCodecWin::decode(const char* bytes, size_t length, bool flush, bool stopOnError, bool& sawError)
{
if (!m_decodeBuffer.isEmpty()) {
m_decodeBuffer.append(bytes, length);
bytes = m_decodeBuffer.data();
length = m_decodeBuffer.size();
}
size_t left;
Vector<UChar, 8192> result;
for (;;) {
decodeInternal(result, m_codePage, bytes, length, &left);
if (!left)
break;
if (!flush && left < 16)
break;
result.append(L'?');
sawError = true;
if (stopOnError)
return String::adopt(result);
if (left == 1)
break;
bytes += length - left + 1;
length = left - 1;
}
if (left && !flush) {
if (m_decodeBuffer.isEmpty())
m_decodeBuffer.append(bytes + length - left, left);
else {
memmove(m_decodeBuffer.data(), bytes + length - left, left);
m_decodeBuffer.resize(left);
}
} else
m_decodeBuffer.clear();
return String::adopt(result);
}
CString TextCodecWin::encode(const UChar* characters, size_t length, UnencodableHandling)
{
if (!characters || !length)
return CString();
int resultLength = WideCharToMultiByte(m_codePage, WC_COMPOSITECHECK, characters, length, 0, 0, 0, 0);
// FIXME: We need to implement UnencodableHandling: QuestionMarksForUnencodables, EntitiesForUnencodables, and URLEncodedEntitiesForUnencodables.
if (resultLength <= 0)
return "?";
char* characterBuffer;
CString result = CString::newUninitialized(resultLength, characterBuffer);
WideCharToMultiByte(m_codePage, WC_COMPOSITECHECK, characters, length, characterBuffer, resultLength, 0, 0);
return result;
}
void TextCodecWin::enumerateSupportedEncodings(EncodingReceiver& receiver)
{
languageManager();
for (CharsetSet::iterator i = supportedCharsets().begin(); i != supportedCharsets().end(); ++i) {
HashMap<String, CharsetInfo>::iterator j = knownCharsets().find(*i);
if (j != knownCharsets().end() && !receiver.receive(j->value.m_name.data(), j->value.m_friendlyName.charactersWithNullTermination().data(), j->value.m_codePage))
break;
}
}
} // namespace WebCore
|