| 6 |
| 7 | #include <limits.h> |
| 8 | #include <stdint.h> |
| 9 |
| 10 | #include <type_traits> |
| 11 |
| 12 | #include "base/strings/string_piece.h" |
| 13 | #include "base/strings/string_util.h" |
| 14 | #include "base/strings/utf_string_conversion_utils.h" |
| 15 | #include "base/third_party/icu/icu_utf.h" |
| 16 | #include "build/build_config.h" |
| 17 |
| 18 | namespace base { |
| 19 |
| 20 | namespace { |
| 21 |
| 22 | constexpr int32_t kErrorCodePoint = 0xFFFD; |
| 23 |
| 24 | // Size coefficient ---------------------------------------------------------- |
| 25 | // The maximum number of codeunits in the destination encoding corresponding to |
| 26 | // one codeunit in the source encoding. |
| 27 |
| 28 | template <typename SrcChar, typename DestChar> |
| 29 | struct SizeCoefficient { |
| 30 | static_assert(sizeof(SrcChar) < sizeof(DestChar), |
| 31 | "Default case: from a smaller encoding to the bigger one"); |
| 32 |
| 33 | // ASCII symbols are encoded by one codeunit in all encodings. |
| 34 | static constexpr int value = 1; |
| 35 | }; |
| 36 |
| 37 | template <> |
| 38 | struct SizeCoefficient<char16, char> { |
| 39 | // One UTF-16 codeunit corresponds to at most 3 codeunits in UTF-8. |
| 40 | static constexpr int value = 3; |
| 41 | }; |
| 42 |
| 43 | #if defined(WCHAR_T_IS_UTF32) |
| 44 | template <> |
| 45 | struct SizeCoefficient<wchar_t, char> { |
| 46 | // UTF-8 uses at most 4 codeunits per character. |
| 47 | static constexpr int value = 4; |
| 48 | }; |
| 49 |
| 50 | template <> |
| 51 | struct SizeCoefficient<wchar_t, char16> { |
| 52 | // UTF-16 uses at most 2 codeunits per character. |
| 53 | static constexpr int value = 2; |
| 54 | }; |
| 55 | #endif // defined(WCHAR_T_IS_UTF32) |
| 56 |
| 57 | template <typename SrcChar, typename DestChar> |
| 58 | constexpr int size_coefficient_v = |
| 59 | SizeCoefficient<std::decay_t<SrcChar>, std::decay_t<DestChar>>::value; |
| 60 |
| 61 | // UnicodeAppendUnsafe -------------------------------------------------------- |
| 62 | // Function overloads that write code_point to the output string. Output string |
| 63 | // has to have enough space for the codepoint. |
| 64 |
| 65 | // Convenience typedef that checks whether the passed in type is integral (i.e. |
| 66 | // bool, char, int or their extended versions) and is of the correct size. |
| 67 | template <typename Char, size_t N> |
| 68 | using EnableIfBitsAre = std::enable_if_t<std::is_integral<Char>::value && |
| 69 | CHAR_BIT * sizeof(Char) == N, |
| 70 | bool>; |
| 71 |
| 72 | template <typename Char, EnableIfBitsAre<Char, 8> = true> |
| 73 | void UnicodeAppendUnsafe(Char* out, int32_t* size, uint32_t code_point) { |
| 74 | CBU8_APPEND_UNSAFE(out, *size, code_point); |
| 75 | } |
| 76 |
| 77 | template <typename Char, EnableIfBitsAre<Char, 16> = true> |
| 78 | void UnicodeAppendUnsafe(Char* out, int32_t* size, uint32_t code_point) { |
| 79 | CBU16_APPEND_UNSAFE(out, *size, code_point); |
| 80 | } |
| 81 |
| 82 | template <typename Char, EnableIfBitsAre<Char, 32> = true> |
| 83 | void UnicodeAppendUnsafe(Char* out, int32_t* size, uint32_t code_point) { |
| 84 | out[(*size)++] = code_point; |
| 85 | } |
| 86 |
| 87 | // DoUTFConversion ------------------------------------------------------------ |
| 88 | // Main driver of UTFConversion specialized for different Src encodings. |
| 89 | // dest has to have enough room for the converted text. |
| 90 |
| 91 | template <typename DestChar> |
| 92 | bool DoUTFConversion(const char* src, |
| 93 | int32_t src_len, |
| 94 | DestChar* dest, |
| 95 | int32_t* dest_len) { |
| 96 | bool success = true; |
| 97 |
| 98 | for (int32_t i = 0; i < src_len;) { |
| 99 | int32_t code_point; |
| 100 | CBU8_NEXT(src, i, src_len, code_point); |
| 101 |
| 102 | if (!IsValidCodepoint(code_point)) { |
| 103 | success = false; |
| 104 | code_point = kErrorCodePoint; |
| 105 | } |
| 106 |
| 107 | UnicodeAppendUnsafe(dest, dest_len, code_point); |
| 108 | } |
| 109 |
| 110 | return success; |
| 111 | } |
| 112 |
| 113 | template <typename DestChar> |
| 114 | bool DoUTFConversion(const char16* src, |
| 115 | int32_t src_len, |
| 116 | DestChar* dest, |
| 117 | int32_t* dest_len) { |
| 118 | bool success = true; |
| 119 |
| 120 | auto ConvertSingleChar = [&success](char16 in) -> int32_t { |
| 121 | if (!CBU16_IS_SINGLE(in) || !IsValidCodepoint(in)) { |
| 122 | success = false; |
| 123 | return kErrorCodePoint; |
| 124 | } |
| 125 | return in; |
| 126 | }; |
| 127 |
| 128 | int32_t i = 0; |
| 129 |
| 130 | // Always have another symbol in order to avoid checking boundaries in the |
| 131 | // middle of the surrogate pair. |
| 132 | while (i < src_len - 1) { |
| 133 | int32_t code_point; |
| 134 |
| 135 | if (CBU16_IS_LEAD(src[i]) && CBU16_IS_TRAIL(src[i + 1])) { |
| 136 | code_point = CBU16_GET_SUPPLEMENTARY(src[i], src[i + 1]); |
| 137 | if (!IsValidCodepoint(code_point)) { |
| 138 | code_point = kErrorCodePoint; |
| 139 | success = false; |
| 140 | } |
| 141 | i += 2; |
| 142 | } else { |
| 143 | code_point = ConvertSingleChar(src[i]); |
| 144 | ++i; |
| 145 | } |
| 146 |
| 147 | UnicodeAppendUnsafe(dest, dest_len, code_point); |
| 148 | } |
| 149 |
| 150 | if (i < src_len) |
| 151 | UnicodeAppendUnsafe(dest, dest_len, ConvertSingleChar(src[i])); |
| 152 |
| 153 | return success; |
| 154 | } |
| 155 |
| 156 | #if defined(WCHAR_T_IS_UTF32) |
| 157 |
| 158 | template <typename DestChar> |
| 159 | bool DoUTFConversion(const wchar_t* src, |
| 160 | int32_t src_len, |
| 161 | DestChar* dest, |
| 162 | int32_t* dest_len) { |
| 163 | bool success = true; |
| 164 |
| 165 | for (int32_t i = 0; i < src_len; ++i) { |
| 166 | int32_t code_point = src[i]; |
| 167 |
| 168 | if (!IsValidCodepoint(code_point)) { |
| 169 | success = false; |
| 170 | code_point = kErrorCodePoint; |
| 171 | } |
| 172 |
| 173 | UnicodeAppendUnsafe(dest, dest_len, code_point); |
| 174 | } |
| 175 |
| 176 | return success; |
| 177 | } |
| 178 |
| 179 | #endif // defined(WCHAR_T_IS_UTF32) |
| 180 |
| 181 | // UTFConversion -------------------------------------------------------------- |
| 182 | // Function template for generating all UTF conversions. |
| 183 |
| 184 | template <typename InputString, typename DestString> |
| 185 | bool UTFConversion(const InputString& src_str, DestString* dest_str) { |
| 186 | if (IsStringASCII(src_str)) { |
| 187 | dest_str->assign(src_str.begin(), src_str.end()); |
| 188 | return true; |
| 189 | } |
| 190 |
| 191 | dest_str->resize(src_str.length() * |
| 192 | size_coefficient_v<typename InputString::value_type, |
| 193 | typename DestString::value_type>); |
| 194 |
| 195 | // Empty string is ASCII => it OK to call operator[]. |
| 196 | auto* dest = &(*dest_str)[0]; |
| 197 |
| 198 | // ICU requires 32 bit numbers. |
| 199 | int32_t src_len32 = static_cast<int32_t>(src_str.length()); |
| 200 | int32_t dest_len32 = 0; |
| 201 |
| 202 | bool res = DoUTFConversion(src_str.data(), src_len32, dest, &dest_len32); |
| 203 |
| 204 | dest_str->resize(dest_len32); |
| 205 | dest_str->shrink_to_fit(); |
| 206 |
| 207 | return res; |
| 208 | } |
| 209 |
| 210 | } // namespace |
| 211 |
| 212 | // UTF16 <-> UTF8 -------------------------------------------------------------- |
| 213 |
| 214 | bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { |
| 215 | return UTFConversion(StringPiece(src, src_len), output); |
| 216 | } |
| 217 |
| 218 | string16 UTF8ToUTF16(StringPiece utf8) { |
| 219 | string16 ret; |
| 220 | // Ignore the success flag of this call, it will do the best it can for |
| 221 | // invalid input, which is what we want here. |
| 222 | UTF8ToUTF16(utf8.data(), utf8.size(), &ret); |
| 223 | return ret; |
| 224 | } |
| 225 |
| 226 | bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { |
| 227 | return UTFConversion(StringPiece16(src, src_len), output); |
| 228 | } |
| 229 |
| 230 | std::string UTF16ToUTF8(StringPiece16 utf16) { |
| 231 | std::string ret; |
| 232 | // Ignore the success flag of this call, it will do the best it can for |
| 233 | // invalid input, which is what we want here. |
| 234 | UTF16ToUTF8(utf16.data(), utf16.length(), &ret); |
| 235 | return ret; |
| 236 | } |
| 237 |
| 238 | // UTF-16 <-> Wide ------------------------------------------------------------- |
| 239 |
| 240 | #if defined(WCHAR_T_IS_UTF16) |
| 241 | // When wide == UTF-16 the conversions are a NOP. |
| 242 |
| 243 | bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) { |
| 244 | output->assign(src, src + src_len); |
| 245 | return true; |
| 246 | } |
| 247 |
| 248 | string16 WideToUTF16(WStringPiece wide) { |
| 249 | return string16(wide.begin(), wide.end()); |
| 250 | } |
| 251 |
| 252 | bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output) { |
| 253 | output->assign(src, src + src_len); |
| 254 | return true; |
| 255 | } |
| 256 |
| 257 | std::wstring UTF16ToWide(StringPiece16 utf16) { |
| 258 | return std::wstring(utf16.begin(), utf16.end()); |
| 259 | } |
| 260 |
| 261 | #elif defined(WCHAR_T_IS_UTF32) |
| 262 |
| 263 | bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) { |
| 264 | return UTFConversion(base::WStringPiece(src, src_len), output); |
| 265 | } |
| 266 |
| 267 | string16 WideToUTF16(WStringPiece wide) { |
| 268 | string16 ret; |
| 269 | // Ignore the success flag of this call, it will do the best it can for |
| 270 | // invalid input, which is what we want here. |
| 271 | WideToUTF16(wide.data(), wide.length(), &ret); |
| 272 | return ret; |
| 273 | } |
| 274 |
| 275 | bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output) { |
| 276 | return UTFConversion(StringPiece16(src, src_len), output); |
| 277 | } |
| 278 |
| 279 | std::wstring UTF16ToWide(StringPiece16 utf16) { |
| 280 | std::wstring ret; |
| 281 | // Ignore the success flag of this call, it will do the best it can for |
| 282 | // invalid input, which is what we want here. |
| 283 | UTF16ToWide(utf16.data(), utf16.length(), &ret); |
| 284 | return ret; |
| 285 | } |
| 286 |
| 287 | #endif // defined(WCHAR_T_IS_UTF32) |
| 288 |
| 289 | // UTF-8 <-> Wide -------------------------------------------------------------- |
| 290 |
| 291 | // UTF8ToWide is the same code, regardless of whether wide is 16 or 32 bits |
| 292 |
| 293 | bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { |
| 294 | return UTFConversion(StringPiece(src, src_len), output); |
| 295 | } |
| 296 |
| 297 | std::wstring UTF8ToWide(StringPiece utf8) { |
| 298 | std::wstring ret; |
| 299 | // Ignore the success flag of this call, it will do the best it can for |
| 300 | // invalid input, which is what we want here. |
| 301 | UTF8ToWide(utf8.data(), utf8.length(), &ret); |
| 302 | return ret; |
| 303 | } |
| 304 |
| 305 | #if defined(WCHAR_T_IS_UTF16) |
| 306 | // Easy case since we can use the "utf" versions we already wrote above. |
| 307 |
| 308 | bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) { |
| 309 | return UTF16ToUTF8(as_u16cstr(src), src_len, output); |
| 310 | } |
| 311 |
| 312 | std::string WideToUTF8(WStringPiece wide) { |
| 313 | return UTF16ToUTF8(StringPiece16(as_u16cstr(wide), wide.size())); |
| 314 | } |
| 315 |
| 316 | #elif defined(WCHAR_T_IS_UTF32) |
| 317 |
| 318 | bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) { |
| 319 | return UTFConversion(WStringPiece(src, src_len), output); |
| 320 | } |
| 321 |
| 322 | std::string WideToUTF8(WStringPiece wide) { |
| 323 | std::string ret; |
| 324 | // Ignore the success flag of this call, it will do the best it can for |
| 325 | // invalid input, which is what we want here. |
| 326 | WideToUTF8(wide.data(), wide.length(), &ret); |
| 327 | return ret; |
| 328 | } |
| 329 |
| 330 | #endif // defined(WCHAR_T_IS_UTF32) |
| 331 |
| 332 | string16 ASCIIToUTF16(StringPiece ascii) { |
| 333 | DCHECK(IsStringASCII(ascii)) << ascii; |
| 334 | return string16(ascii.begin(), ascii.end()); |
| 335 | } |
| 336 |
| 337 | std::string UTF16ToASCII(StringPiece16 utf16) { |
| 338 | DCHECK(IsStringASCII(utf16)) << UTF16ToUTF8(utf16); |
| 339 | return std::string(utf16.begin(), utf16.end()); |
| 340 | } |
| 341 |
| 342 | } // namespace base |
| 343 |