1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/strings/utf_string_conversions.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,185 @@ 1.4 +// Copyright (c) 2010 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/strings/utf_string_conversions.h" 1.9 + 1.10 +#include "base/strings/string_piece.h" 1.11 +#include "base/strings/string_util.h" 1.12 +#include "base/strings/utf_string_conversion_utils.h" 1.13 + 1.14 +namespace base { 1.15 + 1.16 +namespace { 1.17 + 1.18 +// Generalized Unicode converter ----------------------------------------------- 1.19 + 1.20 +// Converts the given source Unicode character type to the given destination 1.21 +// Unicode character type as a STL string. The given input buffer and size 1.22 +// determine the source, and the given output STL string will be replaced by 1.23 +// the result. 1.24 +template<typename SRC_CHAR, typename DEST_STRING> 1.25 +bool ConvertUnicode(const SRC_CHAR* src, 1.26 + size_t src_len, 1.27 + DEST_STRING* output) { 1.28 + // ICU requires 32-bit numbers. 1.29 + bool success = true; 1.30 + int32 src_len32 = static_cast<int32>(src_len); 1.31 + for (int32 i = 0; i < src_len32; i++) { 1.32 + uint32 code_point; 1.33 + if (ReadUnicodeCharacter(src, src_len32, &i, &code_point)) { 1.34 + WriteUnicodeCharacter(code_point, output); 1.35 + } else { 1.36 + WriteUnicodeCharacter(0xFFFD, output); 1.37 + success = false; 1.38 + } 1.39 + } 1.40 + 1.41 + return success; 1.42 +} 1.43 + 1.44 +} // namespace 1.45 + 1.46 +// UTF-8 <-> Wide -------------------------------------------------------------- 1.47 + 1.48 +bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) { 1.49 + PrepareForUTF8Output(src, src_len, output); 1.50 + return ConvertUnicode(src, src_len, output); 1.51 +} 1.52 + 1.53 +std::string WideToUTF8(const std::wstring& wide) { 1.54 + std::string ret; 1.55 + // Ignore the success flag of this call, it will do the best it can for 1.56 + // invalid input, which is what we want here. 1.57 + WideToUTF8(wide.data(), wide.length(), &ret); 1.58 + return ret; 1.59 +} 1.60 + 1.61 +bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { 1.62 + PrepareForUTF16Or32Output(src, src_len, output); 1.63 + return ConvertUnicode(src, src_len, output); 1.64 +} 1.65 + 1.66 +std::wstring UTF8ToWide(const StringPiece& utf8) { 1.67 + std::wstring ret; 1.68 + UTF8ToWide(utf8.data(), utf8.length(), &ret); 1.69 + return ret; 1.70 +} 1.71 + 1.72 +// UTF-16 <-> Wide ------------------------------------------------------------- 1.73 + 1.74 +#if defined(WCHAR_T_IS_UTF16) 1.75 + 1.76 +// When wide == UTF-16, then conversions are a NOP. 1.77 +bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) { 1.78 + output->assign(src, src_len); 1.79 + return true; 1.80 +} 1.81 + 1.82 +string16 WideToUTF16(const std::wstring& wide) { 1.83 + return wide; 1.84 +} 1.85 + 1.86 +bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output) { 1.87 + output->assign(src, src_len); 1.88 + return true; 1.89 +} 1.90 + 1.91 +std::wstring UTF16ToWide(const string16& utf16) { 1.92 + return utf16; 1.93 +} 1.94 + 1.95 +#elif defined(WCHAR_T_IS_UTF32) 1.96 + 1.97 +bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) { 1.98 + output->clear(); 1.99 + // Assume that normally we won't have any non-BMP characters so the counts 1.100 + // will be the same. 1.101 + output->reserve(src_len); 1.102 + return ConvertUnicode(src, src_len, output); 1.103 +} 1.104 + 1.105 +string16 WideToUTF16(const std::wstring& wide) { 1.106 + string16 ret; 1.107 + WideToUTF16(wide.data(), wide.length(), &ret); 1.108 + return ret; 1.109 +} 1.110 + 1.111 +bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output) { 1.112 + output->clear(); 1.113 + // Assume that normally we won't have any non-BMP characters so the counts 1.114 + // will be the same. 1.115 + output->reserve(src_len); 1.116 + return ConvertUnicode(src, src_len, output); 1.117 +} 1.118 + 1.119 +std::wstring UTF16ToWide(const string16& utf16) { 1.120 + std::wstring ret; 1.121 + UTF16ToWide(utf16.data(), utf16.length(), &ret); 1.122 + return ret; 1.123 +} 1.124 + 1.125 +#endif // defined(WCHAR_T_IS_UTF32) 1.126 + 1.127 +// UTF16 <-> UTF8 -------------------------------------------------------------- 1.128 + 1.129 +#if defined(WCHAR_T_IS_UTF32) 1.130 + 1.131 +bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { 1.132 + PrepareForUTF16Or32Output(src, src_len, output); 1.133 + return ConvertUnicode(src, src_len, output); 1.134 +} 1.135 + 1.136 +string16 UTF8ToUTF16(const StringPiece& utf8) { 1.137 + string16 ret; 1.138 + // Ignore the success flag of this call, it will do the best it can for 1.139 + // invalid input, which is what we want here. 1.140 + UTF8ToUTF16(utf8.data(), utf8.length(), &ret); 1.141 + return ret; 1.142 +} 1.143 + 1.144 +bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { 1.145 + PrepareForUTF8Output(src, src_len, output); 1.146 + return ConvertUnicode(src, src_len, output); 1.147 +} 1.148 + 1.149 +std::string UTF16ToUTF8(const string16& utf16) { 1.150 + std::string ret; 1.151 + // Ignore the success flag of this call, it will do the best it can for 1.152 + // invalid input, which is what we want here. 1.153 + UTF16ToUTF8(utf16.data(), utf16.length(), &ret); 1.154 + return ret; 1.155 +} 1.156 + 1.157 +#elif defined(WCHAR_T_IS_UTF16) 1.158 +// Easy case since we can use the "wide" versions we already wrote above. 1.159 + 1.160 +bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { 1.161 + return UTF8ToWide(src, src_len, output); 1.162 +} 1.163 + 1.164 +string16 UTF8ToUTF16(const StringPiece& utf8) { 1.165 + return UTF8ToWide(utf8); 1.166 +} 1.167 + 1.168 +bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { 1.169 + return WideToUTF8(src, src_len, output); 1.170 +} 1.171 + 1.172 +std::string UTF16ToUTF8(const string16& utf16) { 1.173 + return WideToUTF8(utf16); 1.174 +} 1.175 + 1.176 +#endif 1.177 + 1.178 +std::wstring ASCIIToWide(const StringPiece& ascii) { 1.179 + DCHECK(IsStringASCII(ascii)) << ascii; 1.180 + return std::wstring(ascii.begin(), ascii.end()); 1.181 +} 1.182 + 1.183 +string16 ASCIIToUTF16(const StringPiece& ascii) { 1.184 + DCHECK(IsStringASCII(ascii)) << ascii; 1.185 + return string16(ascii.begin(), ascii.end()); 1.186 +} 1.187 + 1.188 +} // namespace base