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

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

Powered by Google App Engine
This is Rietveld 408576698