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" | |
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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 output->set_length(begin_offset + required_capacity); | 102 output->set_length(begin_offset + required_capacity); |
101 return; | 103 return; |
102 } | 104 } |
103 | 105 |
104 // Output didn't fit, expand | 106 // Output didn't fit, expand |
105 dest_capacity = required_capacity; | 107 dest_capacity = required_capacity; |
106 output->Resize(begin_offset + dest_capacity); | 108 output->Resize(begin_offset + dest_capacity); |
107 } while (true); | 109 } while (true); |
108 } | 110 } |
109 | 111 |
112 static base::LazyInstance<base::Lock>::Leaky | |
113 g_uidna_lock = LAZY_INSTANCE_INITIALIZER; | |
114 | |
110 // Converts the Unicode input representing a hostname to ASCII using IDN rules. | 115 // Converts the Unicode input representing a hostname to ASCII using IDN rules. |
111 // The output must be ASCII, but is represented as wide characters. | 116 // The output must be ASCII, but is represented as wide characters. |
112 // | 117 // |
113 // On success, the output will be filled with the ASCII host name and it will | 118 // 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 | 119 // 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 | 120 // 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. | 121 // the length of the output will be set to the length of the new host name. |
117 // | 122 // |
118 // On error, this will return false. The output in this case is undefined. | 123 // On error, this will return false. The output in this case is undefined. |
124 // TODO(jungshik): use UTF-8/ASCII version of nameToASCII. | |
125 // Change the function signature and callers accordingly to avoid unnecessary | |
126 // conversions in our code. In addition, consider using icu::IDNA's UTF-8/ASCII | |
127 // version with StringByteSink. That way, we can avoid C wrappers and additional | |
128 // string conversion. | |
119 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) { | 129 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) { |
120 DCHECK(output->length() == 0); // Output buffer is assumed empty. | 130 DCHECK(output->length() == 0); // Output buffer is assumed empty. |
131 | |
132 static UIDNA* uidna = NULL; // will be leaked. | |
brettw
2013/09/18 19:39:00
Here you make a lazy lock to protect initializatio
jungshik at Google
2013/09/18 21:50:13
I considered that, but gave it up because it appea
| |
133 { | |
134 UErrorCode err = U_ZERO_ERROR; | |
135 base::AutoLock lock(g_uidna_lock.Get()); | |
136 if (uidna == NULL) { | |
137 // This is the option closest to what we had in the past with IDNA 2003 | |
138 // API and matches what IE 10 does except for BiDi check. | |
139 // IDNA 2003 always checks BiDi. We used to allow unassigned code | |
140 // points. However, with our Unicode DB pretty up to date, we'd not | |
141 // need to turn this on. | |
142 // We didn't use STD3 rules and we continue not to. | |
143 // TODO(jungshik) : Change options as different parties (browsers, | |
144 // registrars, search engines) converge toward a consensus. | |
145 int32_t options = UIDNA_CHECK_BIDI; | |
146 uidna = uidna_openUTS46(options, &err); | |
147 if (U_FAILURE(err)) | |
148 return false; | |
149 } | |
150 } | |
121 while (true) { | 151 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; | 152 UErrorCode err = U_ZERO_ERROR; |
126 int num_converted = uidna_IDNToASCII(src, src_len, output->data(), | 153 UIDNAInfo info = UIDNA_INFO_INITIALIZER; |
127 output->capacity(), | 154 int output_length = uidna_nameToASCII(uidna, src, src_len, output->data(), |
128 UIDNA_ALLOW_UNASSIGNED, NULL, &err); | 155 output->capacity(), &info, &err); |
129 if (err == U_ZERO_ERROR) { | 156 if (U_SUCCESS(err) && info.errors == 0) { |
130 output->set_length(num_converted); | 157 output->set_length(output_length); |
131 return true; | 158 return true; |
132 } | 159 } |
133 if (err != U_BUFFER_OVERFLOW_ERROR) | 160 |
161 // TODO(jungshik): Look at info.errors to handle them case-by-case basis | |
162 // if necessary. | |
163 if (err != U_BUFFER_OVERFLOW_ERROR || info.errors != 0) | |
134 return false; // Unknown error, give up. | 164 return false; // Unknown error, give up. |
135 | 165 |
136 // Not enough room in our buffer, expand. | 166 // Not enough room in our buffer, expand. |
137 output->Resize(output->capacity() * 2); | 167 output->Resize(output_length); |
138 } | 168 } |
139 } | 169 } |
140 | 170 |
141 bool ReadUTFChar(const char* str, int* begin, int length, | 171 bool ReadUTFChar(const char* str, int* begin, int length, |
142 unsigned* code_point_out) { | 172 unsigned* code_point_out) { |
143 int code_point; // Avoids warning when U8_NEXT writes -1 to it. | 173 int code_point; // Avoids warning when U8_NEXT writes -1 to it. |
144 U8_NEXT(str, *begin, length, code_point); | 174 U8_NEXT(str, *begin, length, code_point); |
145 *code_point_out = static_cast<unsigned>(code_point); | 175 *code_point_out = static_cast<unsigned>(code_point); |
146 | 176 |
147 // The ICU macro above moves to the next char, we want to point to the last | 177 // The ICU macro above moves to the next char, we want to point to the last |
(...skipping 27 matching lines...) Expand all Loading... | |
175 | 205 |
176 if (U_IS_UNICODE_CHAR(*code_point)) | 206 if (U_IS_UNICODE_CHAR(*code_point)) |
177 return true; | 207 return true; |
178 | 208 |
179 // Invalid code point. | 209 // Invalid code point. |
180 *code_point = kUnicodeReplacementCharacter; | 210 *code_point = kUnicodeReplacementCharacter; |
181 return false; | 211 return false; |
182 } | 212 } |
183 | 213 |
184 } // namespace url_canon | 214 } // namespace url_canon |
OLD | NEW |