1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/i18n/icu_string_conversions.h"
6*635a8641SAndroid Build Coastguard Worker
7*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
8*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
9*635a8641SAndroid Build Coastguard Worker
10*635a8641SAndroid Build Coastguard Worker #include <memory>
11*635a8641SAndroid Build Coastguard Worker #include <vector>
12*635a8641SAndroid Build Coastguard Worker
13*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_util.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/strings/utf_string_conversions.h"
16*635a8641SAndroid Build Coastguard Worker #include "third_party/icu/source/common/unicode/normalizer2.h"
17*635a8641SAndroid Build Coastguard Worker #include "third_party/icu/source/common/unicode/ucnv.h"
18*635a8641SAndroid Build Coastguard Worker #include "third_party/icu/source/common/unicode/ucnv_cb.h"
19*635a8641SAndroid Build Coastguard Worker #include "third_party/icu/source/common/unicode/ucnv_err.h"
20*635a8641SAndroid Build Coastguard Worker #include "third_party/icu/source/common/unicode/ustring.h"
21*635a8641SAndroid Build Coastguard Worker
22*635a8641SAndroid Build Coastguard Worker namespace base {
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Worker namespace {
25*635a8641SAndroid Build Coastguard Worker // ToUnicodeCallbackSubstitute() is based on UCNV_TO_U_CALLBACK_SUBSTITUTE
26*635a8641SAndroid Build Coastguard Worker // in source/common/ucnv_err.c.
27*635a8641SAndroid Build Coastguard Worker
28*635a8641SAndroid Build Coastguard Worker // Copyright (c) 1995-2006 International Business Machines Corporation
29*635a8641SAndroid Build Coastguard Worker // and others
30*635a8641SAndroid Build Coastguard Worker //
31*635a8641SAndroid Build Coastguard Worker // All rights reserved.
32*635a8641SAndroid Build Coastguard Worker //
33*635a8641SAndroid Build Coastguard Worker
34*635a8641SAndroid Build Coastguard Worker // Permission is hereby granted, free of charge, to any person obtaining a
35*635a8641SAndroid Build Coastguard Worker // copy of this software and associated documentation files (the "Software"),
36*635a8641SAndroid Build Coastguard Worker // to deal in the Software without restriction, including without limitation
37*635a8641SAndroid Build Coastguard Worker // the rights to use, copy, modify, merge, publish, distribute, and/or
38*635a8641SAndroid Build Coastguard Worker // sell copies of the Software, and to permit persons to whom the Software
39*635a8641SAndroid Build Coastguard Worker // is furnished to do so, provided that the above copyright notice(s) and
40*635a8641SAndroid Build Coastguard Worker // this permission notice appear in all copies of the Software and that
41*635a8641SAndroid Build Coastguard Worker // both the above copyright notice(s) and this permission notice appear in
42*635a8641SAndroid Build Coastguard Worker // supporting documentation.
43*635a8641SAndroid Build Coastguard Worker //
44*635a8641SAndroid Build Coastguard Worker // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
45*635a8641SAndroid Build Coastguard Worker // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46*635a8641SAndroid Build Coastguard Worker // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
47*635a8641SAndroid Build Coastguard Worker // OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
48*635a8641SAndroid Build Coastguard Worker // INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
49*635a8641SAndroid Build Coastguard Worker // OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
50*635a8641SAndroid Build Coastguard Worker // OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
51*635a8641SAndroid Build Coastguard Worker // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
52*635a8641SAndroid Build Coastguard Worker // OR PERFORMANCE OF THIS SOFTWARE.
53*635a8641SAndroid Build Coastguard Worker //
54*635a8641SAndroid Build Coastguard Worker // Except as contained in this notice, the name of a copyright holder
55*635a8641SAndroid Build Coastguard Worker // shall not be used in advertising or otherwise to promote the sale, use
56*635a8641SAndroid Build Coastguard Worker // or other dealings in this Software without prior written authorization
57*635a8641SAndroid Build Coastguard Worker // of the copyright holder.
58*635a8641SAndroid Build Coastguard Worker
59*635a8641SAndroid Build Coastguard Worker // ___________________________________________________________________________
60*635a8641SAndroid Build Coastguard Worker //
61*635a8641SAndroid Build Coastguard Worker // All trademarks and registered trademarks mentioned herein are the property
62*635a8641SAndroid Build Coastguard Worker // of their respective owners.
63*635a8641SAndroid Build Coastguard Worker
ToUnicodeCallbackSubstitute(const void * context,UConverterToUnicodeArgs * to_args,const char * code_units,int32_t length,UConverterCallbackReason reason,UErrorCode * err)64*635a8641SAndroid Build Coastguard Worker void ToUnicodeCallbackSubstitute(const void* context,
65*635a8641SAndroid Build Coastguard Worker UConverterToUnicodeArgs *to_args,
66*635a8641SAndroid Build Coastguard Worker const char* code_units,
67*635a8641SAndroid Build Coastguard Worker int32_t length,
68*635a8641SAndroid Build Coastguard Worker UConverterCallbackReason reason,
69*635a8641SAndroid Build Coastguard Worker UErrorCode * err) {
70*635a8641SAndroid Build Coastguard Worker static const UChar kReplacementChar = 0xFFFD;
71*635a8641SAndroid Build Coastguard Worker if (reason <= UCNV_IRREGULAR) {
72*635a8641SAndroid Build Coastguard Worker if (context == nullptr ||
73*635a8641SAndroid Build Coastguard Worker (*(reinterpret_cast<const char*>(context)) == 'i' &&
74*635a8641SAndroid Build Coastguard Worker reason == UCNV_UNASSIGNED)) {
75*635a8641SAndroid Build Coastguard Worker *err = U_ZERO_ERROR;
76*635a8641SAndroid Build Coastguard Worker ucnv_cbToUWriteUChars(to_args, &kReplacementChar, 1, 0, err);
77*635a8641SAndroid Build Coastguard Worker }
78*635a8641SAndroid Build Coastguard Worker // else the caller must have set the error code accordingly.
79*635a8641SAndroid Build Coastguard Worker }
80*635a8641SAndroid Build Coastguard Worker // else ignore the reset, close and clone calls.
81*635a8641SAndroid Build Coastguard Worker }
82*635a8641SAndroid Build Coastguard Worker
ConvertFromUTF16(UConverter * converter,const UChar * uchar_src,int uchar_len,OnStringConversionError::Type on_error,std::string * encoded)83*635a8641SAndroid Build Coastguard Worker bool ConvertFromUTF16(UConverter* converter, const UChar* uchar_src,
84*635a8641SAndroid Build Coastguard Worker int uchar_len, OnStringConversionError::Type on_error,
85*635a8641SAndroid Build Coastguard Worker std::string* encoded) {
86*635a8641SAndroid Build Coastguard Worker int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(uchar_len,
87*635a8641SAndroid Build Coastguard Worker ucnv_getMaxCharSize(converter));
88*635a8641SAndroid Build Coastguard Worker encoded->resize(encoded_max_length);
89*635a8641SAndroid Build Coastguard Worker
90*635a8641SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
91*635a8641SAndroid Build Coastguard Worker
92*635a8641SAndroid Build Coastguard Worker // Setup our error handler.
93*635a8641SAndroid Build Coastguard Worker switch (on_error) {
94*635a8641SAndroid Build Coastguard Worker case OnStringConversionError::FAIL:
95*635a8641SAndroid Build Coastguard Worker ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_STOP, nullptr,
96*635a8641SAndroid Build Coastguard Worker nullptr, nullptr, &status);
97*635a8641SAndroid Build Coastguard Worker break;
98*635a8641SAndroid Build Coastguard Worker case OnStringConversionError::SKIP:
99*635a8641SAndroid Build Coastguard Worker case OnStringConversionError::SUBSTITUTE:
100*635a8641SAndroid Build Coastguard Worker ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SKIP, nullptr,
101*635a8641SAndroid Build Coastguard Worker nullptr, nullptr, &status);
102*635a8641SAndroid Build Coastguard Worker break;
103*635a8641SAndroid Build Coastguard Worker default:
104*635a8641SAndroid Build Coastguard Worker NOTREACHED();
105*635a8641SAndroid Build Coastguard Worker }
106*635a8641SAndroid Build Coastguard Worker
107*635a8641SAndroid Build Coastguard Worker // ucnv_fromUChars returns size not including terminating null
108*635a8641SAndroid Build Coastguard Worker int actual_size = ucnv_fromUChars(converter, &(*encoded)[0],
109*635a8641SAndroid Build Coastguard Worker encoded_max_length, uchar_src, uchar_len, &status);
110*635a8641SAndroid Build Coastguard Worker encoded->resize(actual_size);
111*635a8641SAndroid Build Coastguard Worker ucnv_close(converter);
112*635a8641SAndroid Build Coastguard Worker if (U_SUCCESS(status))
113*635a8641SAndroid Build Coastguard Worker return true;
114*635a8641SAndroid Build Coastguard Worker encoded->clear(); // Make sure the output is empty on error.
115*635a8641SAndroid Build Coastguard Worker return false;
116*635a8641SAndroid Build Coastguard Worker }
117*635a8641SAndroid Build Coastguard Worker
118*635a8641SAndroid Build Coastguard Worker // Set up our error handler for ToUTF-16 converters
SetUpErrorHandlerForToUChars(OnStringConversionError::Type on_error,UConverter * converter,UErrorCode * status)119*635a8641SAndroid Build Coastguard Worker void SetUpErrorHandlerForToUChars(OnStringConversionError::Type on_error,
120*635a8641SAndroid Build Coastguard Worker UConverter* converter, UErrorCode* status) {
121*635a8641SAndroid Build Coastguard Worker switch (on_error) {
122*635a8641SAndroid Build Coastguard Worker case OnStringConversionError::FAIL:
123*635a8641SAndroid Build Coastguard Worker ucnv_setToUCallBack(converter, UCNV_TO_U_CALLBACK_STOP, nullptr, nullptr,
124*635a8641SAndroid Build Coastguard Worker nullptr, status);
125*635a8641SAndroid Build Coastguard Worker break;
126*635a8641SAndroid Build Coastguard Worker case OnStringConversionError::SKIP:
127*635a8641SAndroid Build Coastguard Worker ucnv_setToUCallBack(converter, UCNV_TO_U_CALLBACK_SKIP, nullptr, nullptr,
128*635a8641SAndroid Build Coastguard Worker nullptr, status);
129*635a8641SAndroid Build Coastguard Worker break;
130*635a8641SAndroid Build Coastguard Worker case OnStringConversionError::SUBSTITUTE:
131*635a8641SAndroid Build Coastguard Worker ucnv_setToUCallBack(converter, ToUnicodeCallbackSubstitute, nullptr,
132*635a8641SAndroid Build Coastguard Worker nullptr, nullptr, status);
133*635a8641SAndroid Build Coastguard Worker break;
134*635a8641SAndroid Build Coastguard Worker default:
135*635a8641SAndroid Build Coastguard Worker NOTREACHED();
136*635a8641SAndroid Build Coastguard Worker }
137*635a8641SAndroid Build Coastguard Worker }
138*635a8641SAndroid Build Coastguard Worker
139*635a8641SAndroid Build Coastguard Worker } // namespace
140*635a8641SAndroid Build Coastguard Worker
141*635a8641SAndroid Build Coastguard Worker // Codepage <-> Wide/UTF-16 ---------------------------------------------------
142*635a8641SAndroid Build Coastguard Worker
UTF16ToCodepage(const string16 & utf16,const char * codepage_name,OnStringConversionError::Type on_error,std::string * encoded)143*635a8641SAndroid Build Coastguard Worker bool UTF16ToCodepage(const string16& utf16,
144*635a8641SAndroid Build Coastguard Worker const char* codepage_name,
145*635a8641SAndroid Build Coastguard Worker OnStringConversionError::Type on_error,
146*635a8641SAndroid Build Coastguard Worker std::string* encoded) {
147*635a8641SAndroid Build Coastguard Worker encoded->clear();
148*635a8641SAndroid Build Coastguard Worker
149*635a8641SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
150*635a8641SAndroid Build Coastguard Worker UConverter* converter = ucnv_open(codepage_name, &status);
151*635a8641SAndroid Build Coastguard Worker if (!U_SUCCESS(status))
152*635a8641SAndroid Build Coastguard Worker return false;
153*635a8641SAndroid Build Coastguard Worker
154*635a8641SAndroid Build Coastguard Worker return ConvertFromUTF16(converter, utf16.c_str(),
155*635a8641SAndroid Build Coastguard Worker static_cast<int>(utf16.length()), on_error, encoded);
156*635a8641SAndroid Build Coastguard Worker }
157*635a8641SAndroid Build Coastguard Worker
CodepageToUTF16(const std::string & encoded,const char * codepage_name,OnStringConversionError::Type on_error,string16 * utf16)158*635a8641SAndroid Build Coastguard Worker bool CodepageToUTF16(const std::string& encoded,
159*635a8641SAndroid Build Coastguard Worker const char* codepage_name,
160*635a8641SAndroid Build Coastguard Worker OnStringConversionError::Type on_error,
161*635a8641SAndroid Build Coastguard Worker string16* utf16) {
162*635a8641SAndroid Build Coastguard Worker utf16->clear();
163*635a8641SAndroid Build Coastguard Worker
164*635a8641SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
165*635a8641SAndroid Build Coastguard Worker UConverter* converter = ucnv_open(codepage_name, &status);
166*635a8641SAndroid Build Coastguard Worker if (!U_SUCCESS(status))
167*635a8641SAndroid Build Coastguard Worker return false;
168*635a8641SAndroid Build Coastguard Worker
169*635a8641SAndroid Build Coastguard Worker // Even in the worst case, the maximum length in 2-byte units of UTF-16
170*635a8641SAndroid Build Coastguard Worker // output would be at most the same as the number of bytes in input. There
171*635a8641SAndroid Build Coastguard Worker // is no single-byte encoding in which a character is mapped to a
172*635a8641SAndroid Build Coastguard Worker // non-BMP character requiring two 2-byte units.
173*635a8641SAndroid Build Coastguard Worker //
174*635a8641SAndroid Build Coastguard Worker // Moreover, non-BMP characters in legacy multibyte encodings
175*635a8641SAndroid Build Coastguard Worker // (e.g. EUC-JP, GB18030) take at least 2 bytes. The only exceptions are
176*635a8641SAndroid Build Coastguard Worker // BOCU and SCSU, but we don't care about them.
177*635a8641SAndroid Build Coastguard Worker size_t uchar_max_length = encoded.length() + 1;
178*635a8641SAndroid Build Coastguard Worker
179*635a8641SAndroid Build Coastguard Worker SetUpErrorHandlerForToUChars(on_error, converter, &status);
180*635a8641SAndroid Build Coastguard Worker std::unique_ptr<char16[]> buffer(new char16[uchar_max_length]);
181*635a8641SAndroid Build Coastguard Worker int actual_size = ucnv_toUChars(converter, buffer.get(),
182*635a8641SAndroid Build Coastguard Worker static_cast<int>(uchar_max_length), encoded.data(),
183*635a8641SAndroid Build Coastguard Worker static_cast<int>(encoded.length()), &status);
184*635a8641SAndroid Build Coastguard Worker ucnv_close(converter);
185*635a8641SAndroid Build Coastguard Worker if (!U_SUCCESS(status)) {
186*635a8641SAndroid Build Coastguard Worker utf16->clear(); // Make sure the output is empty on error.
187*635a8641SAndroid Build Coastguard Worker return false;
188*635a8641SAndroid Build Coastguard Worker }
189*635a8641SAndroid Build Coastguard Worker
190*635a8641SAndroid Build Coastguard Worker utf16->assign(buffer.get(), actual_size);
191*635a8641SAndroid Build Coastguard Worker return true;
192*635a8641SAndroid Build Coastguard Worker }
193*635a8641SAndroid Build Coastguard Worker
ConvertToUtf8AndNormalize(const std::string & text,const std::string & charset,std::string * result)194*635a8641SAndroid Build Coastguard Worker bool ConvertToUtf8AndNormalize(const std::string& text,
195*635a8641SAndroid Build Coastguard Worker const std::string& charset,
196*635a8641SAndroid Build Coastguard Worker std::string* result) {
197*635a8641SAndroid Build Coastguard Worker result->clear();
198*635a8641SAndroid Build Coastguard Worker string16 utf16;
199*635a8641SAndroid Build Coastguard Worker if (!CodepageToUTF16(
200*635a8641SAndroid Build Coastguard Worker text, charset.c_str(), OnStringConversionError::FAIL, &utf16))
201*635a8641SAndroid Build Coastguard Worker return false;
202*635a8641SAndroid Build Coastguard Worker
203*635a8641SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
204*635a8641SAndroid Build Coastguard Worker const icu::Normalizer2* normalizer = icu::Normalizer2::getNFCInstance(status);
205*635a8641SAndroid Build Coastguard Worker DCHECK(U_SUCCESS(status));
206*635a8641SAndroid Build Coastguard Worker if (U_FAILURE(status))
207*635a8641SAndroid Build Coastguard Worker return false;
208*635a8641SAndroid Build Coastguard Worker int32_t utf16_length = static_cast<int32_t>(utf16.length());
209*635a8641SAndroid Build Coastguard Worker icu::UnicodeString normalized(utf16.data(), utf16_length);
210*635a8641SAndroid Build Coastguard Worker int32_t normalized_prefix_length =
211*635a8641SAndroid Build Coastguard Worker normalizer->spanQuickCheckYes(normalized, status);
212*635a8641SAndroid Build Coastguard Worker if (normalized_prefix_length < utf16_length) {
213*635a8641SAndroid Build Coastguard Worker icu::UnicodeString un_normalized(normalized, normalized_prefix_length);
214*635a8641SAndroid Build Coastguard Worker normalized.truncate(normalized_prefix_length);
215*635a8641SAndroid Build Coastguard Worker normalizer->normalizeSecondAndAppend(normalized, un_normalized, status);
216*635a8641SAndroid Build Coastguard Worker }
217*635a8641SAndroid Build Coastguard Worker if (U_FAILURE(status))
218*635a8641SAndroid Build Coastguard Worker return false;
219*635a8641SAndroid Build Coastguard Worker normalized.toUTF8String(*result);
220*635a8641SAndroid Build Coastguard Worker return true;
221*635a8641SAndroid Build Coastguard Worker }
222*635a8641SAndroid Build Coastguard Worker
223*635a8641SAndroid Build Coastguard Worker } // namespace base
224