OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // ICU integration functions. | 5 // ICU integration functions. |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/lazy_instance.h" | |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/synchronization/lock.h" | |
jungshik at Google
2013/09/19 16:09:40
oops. I'll remove this line in both files.
| |
11 #include "third_party/icu/source/common/unicode/ucnv.h" | 13 #include "third_party/icu/source/common/unicode/ucnv.h" |
12 #include "third_party/icu/source/common/unicode/ucnv_cb.h" | 14 #include "third_party/icu/source/common/unicode/ucnv_cb.h" |
13 #include "third_party/icu/source/common/unicode/uidna.h" | 15 #include "third_party/icu/source/common/unicode/uidna.h" |
14 #include "url/url_canon_icu.h" | 16 #include "url/url_canon_icu.h" |
15 #include "url/url_canon_internal.h" // for _itoa_s | 17 #include "url/url_canon_internal.h" // for _itoa_s |
16 | 18 |
17 namespace url_canon { | 19 namespace url_canon { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 ucnv_setFromUCallBack(converter_, old_callback_, old_context_, 0, 0, &err); | 66 ucnv_setFromUCallBack(converter_, old_callback_, old_context_, 0, 0, &err); |
65 } | 67 } |
66 | 68 |
67 private: | 69 private: |
68 UConverter* converter_; | 70 UConverter* converter_; |
69 | 71 |
70 UConverterFromUCallback old_callback_; | 72 UConverterFromUCallback old_callback_; |
71 const void* old_context_; | 73 const void* old_context_; |
72 }; | 74 }; |
73 | 75 |
76 struct uidna_wrapper { | |
77 uidna_wrapper() { | |
78 UErrorCode err = U_ZERO_ERROR; | |
79 // This is the option closest to what we had in the past with IDNA 2003 | |
80 // API and matches what IE 10 does except for BiDi check. | |
81 // IDNA 2003 always checks BiDi. We used to allow unassigned code | |
82 // points. However, with our Unicode DB pretty up to date, we'd not | |
83 // need to turn this on. | |
84 // We didn't use STD3 rules and we continue not to. | |
85 // TODO(jungshik) : Change options as different parties (browsers, | |
86 // registrars, search engines) converge toward a consensus. | |
87 int32_t options = UIDNA_CHECK_BIDI; | |
88 value = uidna_openUTS46(options, &err); | |
89 if (U_FAILURE(err)) | |
90 value = NULL; | |
91 } | |
92 | |
93 UIDNA* value; | |
94 }; | |
95 | |
74 } // namespace | 96 } // namespace |
75 | 97 |
76 ICUCharsetConverter::ICUCharsetConverter(UConverter* converter) | 98 ICUCharsetConverter::ICUCharsetConverter(UConverter* converter) |
77 : converter_(converter) { | 99 : converter_(converter) { |
78 } | 100 } |
79 | 101 |
80 ICUCharsetConverter::~ICUCharsetConverter() { | 102 ICUCharsetConverter::~ICUCharsetConverter() { |
81 } | 103 } |
82 | 104 |
83 void ICUCharsetConverter::ConvertFromUTF16(const base::char16* input, | 105 void ICUCharsetConverter::ConvertFromUTF16(const base::char16* input, |
(...skipping 16 matching lines...) Expand all Loading... | |
100 output->set_length(begin_offset + required_capacity); | 122 output->set_length(begin_offset + required_capacity); |
101 return; | 123 return; |
102 } | 124 } |
103 | 125 |
104 // Output didn't fit, expand | 126 // Output didn't fit, expand |
105 dest_capacity = required_capacity; | 127 dest_capacity = required_capacity; |
106 output->Resize(begin_offset + dest_capacity); | 128 output->Resize(begin_offset + dest_capacity); |
107 } while (true); | 129 } while (true); |
108 } | 130 } |
109 | 131 |
132 static base::LazyInstance<uidna_wrapper>::Leaky | |
133 g_uidna = LAZY_INSTANCE_INITIALIZER; | |
134 | |
110 // Converts the Unicode input representing a hostname to ASCII using IDN rules. | 135 // Converts the Unicode input representing a hostname to ASCII using IDN rules. |
111 // The output must be ASCII, but is represented as wide characters. | 136 // The output must be ASCII, but is represented as wide characters. |
112 // | 137 // |
113 // On success, the output will be filled with the ASCII host name and it will | 138 // On success, the output will be filled with the ASCII host name and it will |
114 // return true. Unlike most other canonicalization functions, this assumes that | 139 // return true. Unlike most other canonicalization functions, this assumes that |
115 // the output is empty. The beginning of the host will be at offset 0, and | 140 // the output is empty. The beginning of the host will be at offset 0, and |
116 // the length of the output will be set to the length of the new host name. | 141 // the length of the output will be set to the length of the new host name. |
117 // | 142 // |
118 // On error, this will return false. The output in this case is undefined. | 143 // On error, this will return false. The output in this case is undefined. |
144 // TODO(jungshik): use UTF-8/ASCII version of nameToASCII. | |
145 // Change the function signature and callers accordingly to avoid unnecessary | |
146 // conversions in our code. In addition, consider using icu::IDNA's UTF-8/ASCII | |
147 // version with StringByteSink. That way, we can avoid C wrappers and additional | |
148 // string conversion. | |
119 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) { | 149 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) { |
120 DCHECK(output->length() == 0); // Output buffer is assumed empty. | 150 DCHECK(output->length() == 0); // Output buffer is assumed empty. |
151 | |
152 UIDNA* uidna = g_uidna.Get().value; | |
153 DCHECK(uidna != NULL); | |
121 while (true) { | 154 while (true) { |
122 // Use ALLOW_UNASSIGNED to be more tolerant of hostnames that violate | |
123 // the spec (which do exist). This does not present any risk and is a | |
124 // little more future proof. | |
125 UErrorCode err = U_ZERO_ERROR; | 155 UErrorCode err = U_ZERO_ERROR; |
126 int num_converted = uidna_IDNToASCII(src, src_len, output->data(), | 156 UIDNAInfo info = UIDNA_INFO_INITIALIZER; |
127 output->capacity(), | 157 int output_length = uidna_nameToASCII(uidna, src, src_len, output->data(), |
128 UIDNA_ALLOW_UNASSIGNED, NULL, &err); | 158 output->capacity(), &info, &err); |
129 if (err == U_ZERO_ERROR) { | 159 if (U_SUCCESS(err) && info.errors == 0) { |
130 output->set_length(num_converted); | 160 output->set_length(output_length); |
131 return true; | 161 return true; |
132 } | 162 } |
133 if (err != U_BUFFER_OVERFLOW_ERROR) | 163 |
164 // TODO(jungshik): Look at info.errors to handle them case-by-case basis | |
165 // if necessary. | |
166 if (err != U_BUFFER_OVERFLOW_ERROR || info.errors != 0) | |
134 return false; // Unknown error, give up. | 167 return false; // Unknown error, give up. |
135 | 168 |
136 // Not enough room in our buffer, expand. | 169 // Not enough room in our buffer, expand. |
137 output->Resize(output->capacity() * 2); | 170 output->Resize(output_length); |
138 } | 171 } |
139 } | 172 } |
140 | 173 |
141 bool ReadUTFChar(const char* str, int* begin, int length, | 174 bool ReadUTFChar(const char* str, int* begin, int length, |
142 unsigned* code_point_out) { | 175 unsigned* code_point_out) { |
143 int code_point; // Avoids warning when U8_NEXT writes -1 to it. | 176 int code_point; // Avoids warning when U8_NEXT writes -1 to it. |
144 U8_NEXT(str, *begin, length, code_point); | 177 U8_NEXT(str, *begin, length, code_point); |
145 *code_point_out = static_cast<unsigned>(code_point); | 178 *code_point_out = static_cast<unsigned>(code_point); |
146 | 179 |
147 // The ICU macro above moves to the next char, we want to point to the last | 180 // The ICU macro above moves to the next char, we want to point to the last |
(...skipping 27 matching lines...) Expand all Loading... | |
175 | 208 |
176 if (U_IS_UNICODE_CHAR(*code_point)) | 209 if (U_IS_UNICODE_CHAR(*code_point)) |
177 return true; | 210 return true; |
178 | 211 |
179 // Invalid code point. | 212 // Invalid code point. |
180 *code_point = kUnicodeReplacementCharacter; | 213 *code_point = kUnicodeReplacementCharacter; |
181 return false; | 214 return false; |
182 } | 215 } |
183 | 216 |
184 } // namespace url_canon | 217 } // namespace url_canon |
OLD | NEW |