• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2009 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #include "base/strings/utf_string_conversion_utils.h"
11 
12 #include "base/third_party/icu/icu_utf.h"
13 #include "build/build_config.h"
14 
15 namespace base {
16 
17 // CountUnicodeCharacters ------------------------------------------------------
18 
CountUnicodeCharacters(std::string_view text,size_t limit)19 std::optional<size_t> CountUnicodeCharacters(std::string_view text,
20                                              size_t limit) {
21   base_icu::UChar32 unused = 0;
22   size_t count = 0;
23   for (size_t index = 0; count < limit && index < text.size();
24        ++count, ++index) {
25     if (!ReadUnicodeCharacter(text.data(), text.size(), &index, &unused)) {
26       return std::nullopt;
27     }
28   }
29   return count;
30 }
31 
32 // ReadUnicodeCharacter --------------------------------------------------------
33 
ReadUnicodeCharacter(const char * src,size_t src_len,size_t * char_index,base_icu::UChar32 * code_point_out)34 bool ReadUnicodeCharacter(const char* src,
35                           size_t src_len,
36                           size_t* char_index,
37                           base_icu::UChar32* code_point_out) {
38   base_icu::UChar32 code_point;
39   CBU8_NEXT(reinterpret_cast<const uint8_t*>(src), *char_index, src_len,
40             code_point);
41   *code_point_out = code_point;
42 
43   // The ICU macro above moves to the next char, we want to point to the last
44   // char consumed.
45   (*char_index)--;
46 
47   // Validate the decoded value.
48   return IsValidCodepoint(code_point);
49 }
50 
ReadUnicodeCharacter(const char16_t * src,size_t src_len,size_t * char_index,base_icu::UChar32 * code_point)51 bool ReadUnicodeCharacter(const char16_t* src,
52                           size_t src_len,
53                           size_t* char_index,
54                           base_icu::UChar32* code_point) {
55   if (CBU16_IS_SURROGATE(src[*char_index])) {
56     if (!CBU16_IS_SURROGATE_LEAD(src[*char_index]) || !src_len ||
57         *char_index >= src_len - 1 || !CBU16_IS_TRAIL(src[*char_index + 1])) {
58       // Invalid surrogate pair.
59       return false;
60     }
61 
62     // Valid surrogate pair.
63     *code_point = CBU16_GET_SUPPLEMENTARY(src[*char_index],
64                                           src[*char_index + 1]);
65     (*char_index)++;
66   } else {
67     // Not a surrogate, just one 16-bit word.
68     *code_point = src[*char_index];
69   }
70 
71   return IsValidCodepoint(*code_point);
72 }
73 
74 #if defined(WCHAR_T_IS_32_BIT)
ReadUnicodeCharacter(const wchar_t * src,size_t src_len,size_t * char_index,base_icu::UChar32 * code_point)75 bool ReadUnicodeCharacter(const wchar_t* src,
76                           size_t src_len,
77                           size_t* char_index,
78                           base_icu::UChar32* code_point) {
79   // Conversion is easy since the source is 32-bit.
80   *code_point = static_cast<base_icu::UChar32>(src[*char_index]);
81 
82   // Validate the value.
83   return IsValidCodepoint(*code_point);
84 }
85 #endif  // defined(WCHAR_T_IS_32_BIT)
86 
87 // WriteUnicodeCharacter -------------------------------------------------------
88 
WriteUnicodeCharacter(base_icu::UChar32 code_point,std::string * output)89 size_t WriteUnicodeCharacter(base_icu::UChar32 code_point,
90                              std::string* output) {
91   if (code_point >= 0 && code_point <= 0x7f) {
92     // Fast path the common case of one byte.
93     output->push_back(static_cast<char>(code_point));
94     return 1;
95   }
96 
97   // CBU8_APPEND_UNSAFE can append up to 4 bytes.
98   size_t char_offset = output->length();
99   size_t original_char_offset = char_offset;
100   output->resize(char_offset + CBU8_MAX_LENGTH);
101 
102   CBU8_APPEND_UNSAFE(reinterpret_cast<uint8_t*>(output->data()), char_offset,
103                      code_point);
104 
105   // CBU8_APPEND_UNSAFE will advance our pointer past the inserted character, so
106   // it will represent the new length of the string.
107   output->resize(char_offset);
108   return char_offset - original_char_offset;
109 }
110 
WriteUnicodeCharacter(base_icu::UChar32 code_point,std::u16string * output)111 size_t WriteUnicodeCharacter(base_icu::UChar32 code_point,
112                              std::u16string* output) {
113   if (CBU16_LENGTH(code_point) == 1) {
114     // The code point is in the Basic Multilingual Plane (BMP).
115     output->push_back(static_cast<char16_t>(code_point));
116     return 1;
117   }
118   // Non-BMP characters use a double-character encoding.
119   size_t char_offset = output->length();
120   output->resize(char_offset + CBU16_MAX_LENGTH);
121   CBU16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point);
122   return CBU16_MAX_LENGTH;
123 }
124 
125 // Generalized Unicode converter -----------------------------------------------
126 
127 template<typename CHAR>
PrepareForUTF8Output(const CHAR * src,size_t src_len,std::string * output)128 void PrepareForUTF8Output(const CHAR* src,
129                           size_t src_len,
130                           std::string* output) {
131   output->clear();
132   if (src_len == 0)
133     return;
134   if (src[0] < 0x80) {
135     // Assume that the entire input will be ASCII.
136     output->reserve(src_len);
137   } else {
138     // Assume that the entire input is non-ASCII and will have 3 bytes per char.
139     output->reserve(src_len * 3);
140   }
141 }
142 
143 // Instantiate versions we know callers will need.
144 #if !BUILDFLAG(IS_WIN)
145 // wchar_t and char16_t are the same thing on Windows.
146 template void PrepareForUTF8Output(const wchar_t*, size_t, std::string*);
147 #endif
148 template void PrepareForUTF8Output(const char16_t*, size_t, std::string*);
149 
150 template<typename STRING>
PrepareForUTF16Or32Output(const char * src,size_t src_len,STRING * output)151 void PrepareForUTF16Or32Output(const char* src,
152                                size_t src_len,
153                                STRING* output) {
154   output->clear();
155   if (src_len == 0)
156     return;
157   if (static_cast<unsigned char>(src[0]) < 0x80) {
158     // Assume the input is all ASCII, which means 1:1 correspondence.
159     output->reserve(src_len);
160   } else {
161     // Otherwise assume that the UTF-8 sequences will have 2 bytes for each
162     // character.
163     output->reserve(src_len / 2);
164   }
165 }
166 
167 // Instantiate versions we know callers will need.
168 #if !BUILDFLAG(IS_WIN)
169 // std::wstring and std::u16string are the same thing on Windows.
170 template void PrepareForUTF16Or32Output(const char*, size_t, std::wstring*);
171 #endif
172 template void PrepareForUTF16Or32Output(const char*, size_t, std::u16string*);
173 
174 }  // namespace base
175