| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/i18n/icu_string_conversions.h" | 5 #include "base/i18n/icu_string_conversions.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 150 |
| 151 } // namespace | 151 } // namespace |
| 152 | 152 |
| 153 const char kCodepageLatin1[] = "ISO-8859-1"; | 153 const char kCodepageLatin1[] = "ISO-8859-1"; |
| 154 const char kCodepageUTF8[] = "UTF-8"; | 154 const char kCodepageUTF8[] = "UTF-8"; |
| 155 const char kCodepageUTF16BE[] = "UTF-16BE"; | 155 const char kCodepageUTF16BE[] = "UTF-16BE"; |
| 156 const char kCodepageUTF16LE[] = "UTF-16LE"; | 156 const char kCodepageUTF16LE[] = "UTF-16LE"; |
| 157 | 157 |
| 158 // Codepage <-> Wide/UTF-16 --------------------------------------------------- | 158 // Codepage <-> Wide/UTF-16 --------------------------------------------------- |
| 159 | 159 |
| 160 // Convert a UTF-16 string into the specified codepage_name. If the codepage |
| 161 // isn't found, return false. |
| 162 bool UTF16ToCodepage(const string16& utf16, |
| 163 const char* codepage_name, |
| 164 OnStringConversionError::Type on_error, |
| 165 std::string* encoded) { |
| 166 encoded->clear(); |
| 167 |
| 168 UErrorCode status = U_ZERO_ERROR; |
| 169 UConverter* converter = ucnv_open(codepage_name, &status); |
| 170 if (!U_SUCCESS(status)) |
| 171 return false; |
| 172 |
| 173 return ConvertFromUTF16(converter, utf16.c_str(), |
| 174 static_cast<int>(utf16.length()), on_error, encoded); |
| 175 } |
| 176 |
| 177 bool CodepageToUTF16AndAdjustOffset(const std::string& encoded, |
| 178 const char* codepage_name, |
| 179 OnStringConversionError::Type on_error, |
| 180 string16* utf16, |
| 181 size_t* offset_for_adjustment) { |
| 182 utf16->clear(); |
| 183 |
| 184 UErrorCode status = U_ZERO_ERROR; |
| 185 UConverter* converter = ucnv_open(codepage_name, &status); |
| 186 if (!U_SUCCESS(status)) |
| 187 return false; |
| 188 |
| 189 // Even in the worst case, the maximum length in 2-byte units of UTF-16 |
| 190 // output would be at most the same as the number of bytes in input. There |
| 191 // is no single-byte encoding in which a character is mapped to a |
| 192 // non-BMP character requiring two 2-byte units. |
| 193 // |
| 194 // Moreover, non-BMP characters in legacy multibyte encodings |
| 195 // (e.g. EUC-JP, GB18030) take at least 2 bytes. The only exceptions are |
| 196 // BOCU and SCSU, but we don't care about them. |
| 197 size_t uchar_max_length = encoded.length() + 1; |
| 198 |
| 199 SetUpErrorHandlerForToUChars(on_error, converter, &status); |
| 200 char16* byte_buffer = WriteInto(utf16, uchar_max_length); |
| 201 int byte_buffer_length = static_cast<int>(uchar_max_length); |
| 202 const char* data = encoded.data(); |
| 203 int length = static_cast<int>(encoded.length()); |
| 204 int actual_size = 0; |
| 205 if (offset_for_adjustment) { |
| 206 if (*offset_for_adjustment >= encoded.length()) { |
| 207 *offset_for_adjustment = string16::npos; |
| 208 } else if (*offset_for_adjustment != 0) { |
| 209 // Try to adjust the offset by converting the string in two pieces and |
| 210 // using the length of the first piece as the adjusted offset. |
| 211 actual_size += ucnv_toUChars(converter, byte_buffer, byte_buffer_length, |
| 212 data, static_cast<int>(*offset_for_adjustment), &status); |
| 213 if (U_SUCCESS(status)) { |
| 214 // Conversion succeeded, so update the offset and then fall through to |
| 215 // appending the second half of the string. |
| 216 data += *offset_for_adjustment; |
| 217 length -= *offset_for_adjustment; |
| 218 *offset_for_adjustment = actual_size; |
| 219 byte_buffer += actual_size; |
| 220 byte_buffer_length -= actual_size; |
| 221 } else { |
| 222 // The offset may have been in the middle of an encoding sequence; mark |
| 223 // it as having failed to adjust and then try to convert the entire |
| 224 // string. |
| 225 *offset_for_adjustment = string16::npos; |
| 226 actual_size = 0; |
| 227 ucnv_reset(converter); |
| 228 status = U_ZERO_ERROR; |
| 229 } |
| 230 } |
| 231 } |
| 232 actual_size += ucnv_toUChars(converter, byte_buffer, byte_buffer_length, data, |
| 233 length, &status); |
| 234 ucnv_close(converter); |
| 235 if (!U_SUCCESS(status)) { |
| 236 utf16->clear(); // Make sure the output is empty on error. |
| 237 return false; |
| 238 } |
| 239 |
| 240 utf16->resize(actual_size); |
| 241 return true; |
| 242 } |
| 243 |
| 160 // Convert a wstring into the specified codepage_name. If the codepage | 244 // Convert a wstring into the specified codepage_name. If the codepage |
| 161 // isn't found, return false. | 245 // isn't found, return false. |
| 162 bool WideToCodepage(const std::wstring& wide, | 246 bool WideToCodepage(const std::wstring& wide, |
| 163 const char* codepage_name, | 247 const char* codepage_name, |
| 164 OnStringConversionError::Type on_error, | 248 OnStringConversionError::Type on_error, |
| 165 std::string* encoded) { | 249 std::string* encoded) { |
| 166 #if defined(WCHAR_T_IS_UTF16) | 250 #if defined(WCHAR_T_IS_UTF16) |
| 167 return UTF16ToCodepage(wide, codepage_name, on_error, encoded); | 251 return UTF16ToCodepage(wide, codepage_name, on_error, encoded); |
| 168 #elif defined(WCHAR_T_IS_UTF32) | 252 #elif defined(WCHAR_T_IS_UTF32) |
| 169 encoded->clear(); | 253 encoded->clear(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 181 // and leave room for a NUL terminator. | 265 // and leave room for a NUL terminator. |
| 182 std::vector<UChar> utf16(wide.length() * 2 + 1); | 266 std::vector<UChar> utf16(wide.length() * 2 + 1); |
| 183 u_strFromWCS(&utf16[0], utf16.size(), &utf16_len, | 267 u_strFromWCS(&utf16[0], utf16.size(), &utf16_len, |
| 184 wide.c_str(), wide.length(), &status); | 268 wide.c_str(), wide.length(), &status); |
| 185 DCHECK(U_SUCCESS(status)) << "failed to convert wstring to UChar*"; | 269 DCHECK(U_SUCCESS(status)) << "failed to convert wstring to UChar*"; |
| 186 | 270 |
| 187 return ConvertFromUTF16(converter, &utf16[0], utf16_len, on_error, encoded); | 271 return ConvertFromUTF16(converter, &utf16[0], utf16_len, on_error, encoded); |
| 188 #endif // defined(WCHAR_T_IS_UTF32) | 272 #endif // defined(WCHAR_T_IS_UTF32) |
| 189 } | 273 } |
| 190 | 274 |
| 191 // Convert a UTF-16 string into the specified codepage_name. If the codepage | |
| 192 // isn't found, return false. | |
| 193 bool UTF16ToCodepage(const string16& utf16, | |
| 194 const char* codepage_name, | |
| 195 OnStringConversionError::Type on_error, | |
| 196 std::string* encoded) { | |
| 197 encoded->clear(); | |
| 198 | |
| 199 UErrorCode status = U_ZERO_ERROR; | |
| 200 UConverter* converter = ucnv_open(codepage_name, &status); | |
| 201 if (!U_SUCCESS(status)) | |
| 202 return false; | |
| 203 | |
| 204 return ConvertFromUTF16(converter, utf16.c_str(), | |
| 205 static_cast<int>(utf16.length()), on_error, encoded); | |
| 206 } | |
| 207 | |
| 208 // Converts a string of the given codepage into wstring. | 275 // Converts a string of the given codepage into wstring. |
| 209 // If the codepage isn't found, return false. | 276 // If the codepage isn't found, return false. |
| 210 bool CodepageToWide(const std::string& encoded, | 277 bool CodepageToWideAndAdjustOffset(const std::string& encoded, |
| 211 const char* codepage_name, | 278 const char* codepage_name, |
| 212 OnStringConversionError::Type on_error, | 279 OnStringConversionError::Type on_error, |
| 213 std::wstring* wide) { | 280 std::wstring* wide, |
| 281 size_t* offset_for_adjustment) { |
| 214 #if defined(WCHAR_T_IS_UTF16) | 282 #if defined(WCHAR_T_IS_UTF16) |
| 215 return CodepageToUTF16(encoded, codepage_name, on_error, wide); | 283 return CodepageToUTF16AndAdjustOffset(encoded, codepage_name, on_error, wide, |
| 284 offset_for_adjustment); |
| 216 #elif defined(WCHAR_T_IS_UTF32) | 285 #elif defined(WCHAR_T_IS_UTF32) |
| 217 wide->clear(); | 286 wide->clear(); |
| 218 | 287 |
| 219 UErrorCode status = U_ZERO_ERROR; | 288 UErrorCode status = U_ZERO_ERROR; |
| 220 UConverter* converter = ucnv_open(codepage_name, &status); | 289 UConverter* converter = ucnv_open(codepage_name, &status); |
| 221 if (!U_SUCCESS(status)) | 290 if (!U_SUCCESS(status)) |
| 222 return false; | 291 return false; |
| 223 | 292 |
| 224 // The maximum length in 4 byte unit of UTF-32 output would be | 293 // The maximum length in 4 byte unit of UTF-32 output would be |
| 225 // at most the same as the number of bytes in input. In the worst | 294 // at most the same as the number of bytes in input. In the worst |
| 226 // case of GB18030 (excluding escaped-based encodings like ISO-2022-JP), | 295 // case of GB18030 (excluding escaped-based encodings like ISO-2022-JP), |
| 227 // this can be 4 times larger than actually needed. | 296 // this can be 4 times larger than actually needed. |
| 228 size_t wchar_max_length = encoded.length() + 1; | 297 size_t wchar_max_length = encoded.length() + 1; |
| 229 | 298 |
| 230 // The byte buffer and its length to pass to ucnv_toAlgorithimic. | |
| 231 char* byte_buffer = reinterpret_cast<char*>( | |
| 232 WriteInto(wide, wchar_max_length)); | |
| 233 int byte_buffer_length = static_cast<int>(wchar_max_length) * 4; | |
| 234 | |
| 235 SetUpErrorHandlerForToUChars(on_error, converter, &status); | 299 SetUpErrorHandlerForToUChars(on_error, converter, &status); |
| 236 int actual_size = ucnv_toAlgorithmic(utf32_platform_endian(), | 300 char* byte_buffer = |
| 237 converter, | 301 reinterpret_cast<char*>(WriteInto(wide, wchar_max_length)); |
| 238 byte_buffer, | 302 int byte_buffer_length = static_cast<int>(wchar_max_length) * sizeof(wchar_t); |
| 239 byte_buffer_length, | 303 const char* data = encoded.data(); |
| 240 encoded.data(), | 304 int length = static_cast<int>(encoded.length()); |
| 241 static_cast<int>(encoded.length()), | 305 int actual_size = 0; |
| 242 &status); | 306 if (offset_for_adjustment) { |
| 307 if (*offset_for_adjustment >= encoded.length()) { |
| 308 *offset_for_adjustment = std::wstring::npos; |
| 309 } else if (*offset_for_adjustment != 0) { |
| 310 // Try to adjust the offset by converting the string in two pieces and |
| 311 // using the length of the first piece as the adjusted offset. |
| 312 actual_size += ucnv_toAlgorithmic(utf32_platform_endian(), converter, |
| 313 byte_buffer, byte_buffer_length, data, |
| 314 static_cast<int>(*offset_for_adjustment), &status); |
| 315 if (U_SUCCESS(status)) { |
| 316 // Conversion succeeded, so update the offset and then fall through to |
| 317 // appending the second half of the string. |
| 318 data += *offset_for_adjustment; |
| 319 length -= *offset_for_adjustment; |
| 320 *offset_for_adjustment = actual_size / sizeof(wchar_t); |
| 321 byte_buffer += actual_size; |
| 322 byte_buffer_length -= actual_size; |
| 323 } else { |
| 324 // The offset may have been in the middle of an encoding sequence; mark |
| 325 // it as having failed to adjust and then try to convert the entire |
| 326 // string. |
| 327 *offset_for_adjustment = std::wstring::npos; |
| 328 actual_size = 0; |
| 329 ucnv_reset(converter); |
| 330 status = U_ZERO_ERROR; |
| 331 } |
| 332 } |
| 333 } |
| 334 actual_size += ucnv_toAlgorithmic(utf32_platform_endian(), converter, |
| 335 byte_buffer, byte_buffer_length, data, length, &status); |
| 243 ucnv_close(converter); | 336 ucnv_close(converter); |
| 244 | |
| 245 if (!U_SUCCESS(status)) { | 337 if (!U_SUCCESS(status)) { |
| 246 wide->clear(); // Make sure the output is empty on error. | 338 wide->clear(); // Make sure the output is empty on error. |
| 247 return false; | 339 return false; |
| 248 } | 340 } |
| 249 | 341 |
| 250 // actual_size is # of bytes. | 342 // actual_size is # of bytes. |
| 251 wide->resize(actual_size / 4); | 343 wide->resize(actual_size / sizeof(wchar_t)); |
| 252 return true; | 344 return true; |
| 253 #endif // defined(WCHAR_T_IS_UTF32) | 345 #endif // defined(WCHAR_T_IS_UTF32) |
| 254 } | 346 } |
| 255 | 347 |
| 256 // Converts a string of the given codepage into UTF-16. | |
| 257 // If the codepage isn't found, return false. | |
| 258 bool CodepageToUTF16(const std::string& encoded, | |
| 259 const char* codepage_name, | |
| 260 OnStringConversionError::Type on_error, | |
| 261 string16* utf16) { | |
| 262 utf16->clear(); | |
| 263 | |
| 264 UErrorCode status = U_ZERO_ERROR; | |
| 265 UConverter* converter = ucnv_open(codepage_name, &status); | |
| 266 if (!U_SUCCESS(status)) | |
| 267 return false; | |
| 268 | |
| 269 // Even in the worst case, the maximum length in 2-byte units of UTF-16 | |
| 270 // output would be at most the same as the number of bytes in input. There | |
| 271 // is no single-byte encoding in which a character is mapped to a | |
| 272 // non-BMP character requiring two 2-byte units. | |
| 273 // | |
| 274 // Moreover, non-BMP characters in legacy multibyte encodings | |
| 275 // (e.g. EUC-JP, GB18030) take at least 2 bytes. The only exceptions are | |
| 276 // BOCU and SCSU, but we don't care about them. | |
| 277 size_t uchar_max_length = encoded.length() + 1; | |
| 278 | |
| 279 SetUpErrorHandlerForToUChars(on_error, converter, &status); | |
| 280 int actual_size = ucnv_toUChars(converter, | |
| 281 WriteInto(utf16, uchar_max_length), | |
| 282 static_cast<int>(uchar_max_length), | |
| 283 encoded.data(), | |
| 284 static_cast<int>(encoded.length()), | |
| 285 &status); | |
| 286 ucnv_close(converter); | |
| 287 if (!U_SUCCESS(status)) { | |
| 288 utf16->clear(); // Make sure the output is empty on error. | |
| 289 return false; | |
| 290 } | |
| 291 | |
| 292 utf16->resize(actual_size); | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 296 } // namespace base | 348 } // namespace base |
| OLD | NEW |