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