Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: url/url_canon_icu.cc

Issue 23642003: Support IDNA 2008 (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 // A wrapper to use LazyInstance<>::Leaky with ICU's UIDNA, a C pointer to
76 // UTS46/IDNA 2008 handling object opened with |uidna_openUTS46|.
77 //
78 // We now switch to UTS46 with BiDiCheck to migrate to IDNA 2008 with
79 // the backward compatibility in mind. What it means :
80 //
81 // 1. Use the up-to-date Unicode data.
82 // 2. Define a case folding/mapping with the up-to-date Unicode data as
83 // in IDNA 2003.
84 // 3. Use transitional mechanism for 4 deviation characters (sharp-s,
85 // final sigma, ZWJ and ZWNJ) for now.
86 // 4. Continue to allow symbols and punctuations.
87 // 5. Apply new BiDi check rules more permissive than the IDNA 2003 BiDI rules.
88 // 6. Do not apply STD3 rules
89 // 7. Do not allow unassigned code points.
90 //
91 // It also closely matches what IE 10 does except for the BiDi check (
92 // http://goo.gl/3XBhqw ).
93 // See http://http://unicode.org/reports/tr46/ and references therein
94 // for more details.
95 struct UIDNAWrapper {
96 UIDNAWrapper() {
97 UErrorCode err = U_ZERO_ERROR;
98 // TODO(jungshik) : Change options as different parties (browsers,
99 // registrars, search engines) converge toward a consensus.
100 value = uidna_openUTS46(UIDNA_CHECK_BIDI, &err);
101 if (U_FAILURE(err))
102 value = NULL;
103 }
104
105 UIDNA* value;
106 };
107
74 } // namespace 108 } // namespace
75 109
76 ICUCharsetConverter::ICUCharsetConverter(UConverter* converter) 110 ICUCharsetConverter::ICUCharsetConverter(UConverter* converter)
77 : converter_(converter) { 111 : converter_(converter) {
78 } 112 }
79 113
80 ICUCharsetConverter::~ICUCharsetConverter() { 114 ICUCharsetConverter::~ICUCharsetConverter() {
81 } 115 }
82 116
83 void ICUCharsetConverter::ConvertFromUTF16(const base::char16* input, 117 void ICUCharsetConverter::ConvertFromUTF16(const base::char16* input,
(...skipping 16 matching lines...) Expand all
100 output->set_length(begin_offset + required_capacity); 134 output->set_length(begin_offset + required_capacity);
101 return; 135 return;
102 } 136 }
103 137
104 // Output didn't fit, expand 138 // Output didn't fit, expand
105 dest_capacity = required_capacity; 139 dest_capacity = required_capacity;
106 output->Resize(begin_offset + dest_capacity); 140 output->Resize(begin_offset + dest_capacity);
107 } while (true); 141 } while (true);
108 } 142 }
109 143
144 static base::LazyInstance<UIDNAWrapper>::Leaky
145 g_uidna = LAZY_INSTANCE_INITIALIZER;
146
110 // Converts the Unicode input representing a hostname to ASCII using IDN rules. 147 // Converts the Unicode input representing a hostname to ASCII using IDN rules.
111 // The output must be ASCII, but is represented as wide characters. 148 // The output must be ASCII, but is represented as wide characters.
112 // 149 //
113 // On success, the output will be filled with the ASCII host name and it will 150 // 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 151 // 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 152 // 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. 153 // the length of the output will be set to the length of the new host name.
117 // 154 //
118 // On error, this will return false. The output in this case is undefined. 155 // On error, this will return false. The output in this case is undefined.
156 // TODO(jungshik): use UTF-8/ASCII version of nameToASCII.
157 // Change the function signature and callers accordingly to avoid unnecessary
158 // conversions in our code. In addition, consider using icu::IDNA's UTF-8/ASCII
159 // version with StringByteSink. That way, we can avoid C wrappers and additional
160 // string conversion.
119 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) { 161 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) {
120 DCHECK(output->length() == 0); // Output buffer is assumed empty. 162 DCHECK(output->length() == 0); // Output buffer is assumed empty.
163
164 UIDNA* uidna = g_uidna.Get().value;
165 DCHECK(uidna != NULL);
121 while (true) { 166 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; 167 UErrorCode err = U_ZERO_ERROR;
126 int num_converted = uidna_IDNToASCII(src, src_len, output->data(), 168 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
127 output->capacity(), 169 int output_length = uidna_nameToASCII(uidna, src, src_len, output->data(),
128 UIDNA_ALLOW_UNASSIGNED, NULL, &err); 170 output->capacity(), &info, &err);
129 if (err == U_ZERO_ERROR) { 171 if (U_SUCCESS(err) && info.errors == 0) {
130 output->set_length(num_converted); 172 output->set_length(output_length);
131 return true; 173 return true;
132 } 174 }
133 if (err != U_BUFFER_OVERFLOW_ERROR) 175
176 // TODO(jungshik): Look at info.errors to handle them case-by-case basis
177 // if necessary.
178 if (err != U_BUFFER_OVERFLOW_ERROR || info.errors != 0)
134 return false; // Unknown error, give up. 179 return false; // Unknown error, give up.
135 180
136 // Not enough room in our buffer, expand. 181 // Not enough room in our buffer, expand.
137 output->Resize(output->capacity() * 2); 182 output->Resize(output_length);
138 } 183 }
139 } 184 }
140 185
141 bool ReadUTFChar(const char* str, int* begin, int length, 186 bool ReadUTFChar(const char* str, int* begin, int length,
142 unsigned* code_point_out) { 187 unsigned* code_point_out) {
143 int code_point; // Avoids warning when U8_NEXT writes -1 to it. 188 int code_point; // Avoids warning when U8_NEXT writes -1 to it.
144 U8_NEXT(str, *begin, length, code_point); 189 U8_NEXT(str, *begin, length, code_point);
145 *code_point_out = static_cast<unsigned>(code_point); 190 *code_point_out = static_cast<unsigned>(code_point);
146 191
147 // The ICU macro above moves to the next char, we want to point to the last 192 // The ICU macro above moves to the next char, we want to point to the last
(...skipping 27 matching lines...) Expand all
175 220
176 if (U_IS_UNICODE_CHAR(*code_point)) 221 if (U_IS_UNICODE_CHAR(*code_point))
177 return true; 222 return true;
178 223
179 // Invalid code point. 224 // Invalid code point.
180 *code_point = kUnicodeReplacementCharacter; 225 *code_point = kUnicodeReplacementCharacter;
181 return false; 226 return false;
182 } 227 }
183 228
184 } // namespace url_canon 229 } // namespace url_canon
OLDNEW
« net/base/net_util.cc ('K') | « net/base/net_util.cc ('k') | url/url_canon_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698