Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
to_ascii.cpp
Go to the documentation of this file.
1#include "ada/idna/to_ascii.h"
2
3#include <algorithm>
4#include <cstdint>
5#include <cstring>
6#include <ranges>
7
8#include "ada/idna/mapping.h"
10#include "ada/idna/punycode.h"
12#include "ada/idna/validity.h"
13
14#ifdef ADA_USE_SIMDUTF
15#include "simdutf.h"
16#endif
17
18namespace ada::idna {
19
20bool constexpr is_ascii(std::u32string_view view) {
21 for (uint32_t c : view) {
22 if (c >= 0x80) {
23 return false;
24 }
25 }
26 return true;
27}
28
29bool constexpr is_ascii(std::string_view view) {
30 for (uint8_t c : view) {
31 if (c >= 0x80) {
32 return false;
33 }
34 }
35 return true;
36}
37
38constexpr static uint8_t is_forbidden_domain_code_point_table[] = {
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
50
51static_assert(sizeof(is_forbidden_domain_code_point_table) == 256);
52
53inline bool is_forbidden_domain_code_point(const char c) noexcept {
54 return is_forbidden_domain_code_point_table[uint8_t(c)];
55}
56
57bool contains_forbidden_domain_code_point(std::string_view view) {
58 return std::ranges::any_of(view, is_forbidden_domain_code_point);
59}
60
61// Per the WHATWG URL "domain to ASCII" algorithm, when beStrict is false and
62// the input domain is an ASCII string, the result is the input lowercased,
63// regardless of the outcome of Unicode ToASCII.
64//
65// See https://url.spec.whatwg.org/#concept-domain-to-ascii
66static void from_ascii_to_ascii(std::string_view ut8_string, std::string& out) {
67 out.assign(ut8_string);
68 ascii_map(out.data(), out.size());
69}
70
71// Append ASCII code units from a UTF-32 label (all values < 0x80).
72static void append_ascii_label(std::string& out, std::u32string_view label) {
73 const size_t old = out.size();
74 out.resize(old + label.size());
75 char* dest = out.data() + old;
76 for (char32_t c : label) {
77 *dest++ = static_cast<char>(c);
78 }
79}
80
81// True if label begins with "xn--" (already lowercased by mapping).
82static bool is_ace_prefix(std::u32string_view label) noexcept {
83 return label.size() >= 4 && label[0] == U'x' && label[1] == U'n' &&
84 label[2] == U'-' && label[3] == U'-';
85}
86
87// RFC 5893: a Bidi domain name contains at least one RTL label. CheckBidi then
88// applies to every label in the domain, so this must be known before the first
89// label is validated. ACE labels are decoded; a decode failure is left for the
90// conversion pass, which rejects the domain.
91static bool mapped_domain_is_bidi(std::u32string_view mapped) {
92 const char32_t* p = mapped.data();
93 const char32_t* const end = p + mapped.size();
94 std::string puny;
95 std::u32string decoded;
96 while (p < end) {
97 const char32_t* begin = p;
98 while (p < end && *p != U'.') {
99 ++p;
100 }
101 const std::u32string_view label(begin, static_cast<size_t>(p - begin));
102 if (p < end) {
103 ++p;
104 }
105 if (label.empty()) {
106 continue;
107 }
108 if (is_ace_prefix(label)) {
109 bool ace_ascii = true;
110 for (char32_t c : label) {
111 if (c >= 0x80) {
112 ace_ascii = false;
113 break;
114 }
115 }
116 if (!ace_ascii) {
117 continue;
118 }
119 puny.resize(label.size() - 4);
120 for (size_t i = 0; i < puny.size(); i++) {
121 puny[i] = static_cast<char>(label[i + 4]);
122 }
123 decoded.clear();
124 if (punycode_to_utf32(puny, decoded) && label_contains_rtl(decoded)) {
125 return true;
126 }
127 } else if (label_contains_rtl(label)) {
128 return true;
129 }
130 }
131 return false;
132}
133
134[[nodiscard]] bool to_ascii(std::string_view ut8_string, std::string& out) {
135 out.clear();
136 // The ASCII path is a single same-size copy, so it needs no length bound.
137 // Checking first would reject the (longer) ASCII output of a non-ASCII input
138 // that was accepted, breaking to_ascii(to_ascii(x)) == to_ascii(x).
139 if (is_ascii(ut8_string)) {
140 from_ascii_to_ascii(ut8_string, out);
141 return true;
142 }
143 if (ut8_string.size() > max_domain_input_bytes) {
144 return false;
145 }
146
147#ifdef ADA_USE_SIMDUTF
148 size_t utf32_length =
149 simdutf::utf32_length_from_utf8(ut8_string.data(), ut8_string.size());
150 if (utf32_length == 0 && !ut8_string.empty()) {
151 return false;
152 }
153 std::u32string working(utf32_length, U'\0');
154 size_t actual_utf32_length = simdutf::convert_utf8_to_utf32(
155 ut8_string.data(), ut8_string.size(), working.data());
156#else
157 size_t utf32_length =
158 ada::idna::utf32_length_from_utf8(ut8_string.data(), ut8_string.size());
159 if (utf32_length == 0 && !ut8_string.empty()) {
160 return false;
161 }
162 std::u32string working(utf32_length, U'\0');
163 size_t actual_utf32_length = ada::idna::utf8_to_utf32(
164 ut8_string.data(), ut8_string.size(), working.data());
165#endif
166 if (actual_utf32_length == 0 || actual_utf32_length != utf32_length) {
167 return false;
168 }
169 working.resize(actual_utf32_length);
170
171 // Map into a second buffer with exact sizing (no growth reallocs).
172 std::u32string mapped;
173 if (!ada::idna::map(working, mapped)) {
174 return false;
175 }
176 // Drop UTF-32 input; reuse `working` as scratch for ACE validation below.
177 working.clear();
178
179 // Skip NFC when already normalized (ASCII is a fast subset of this check).
180 if (!is_ascii(mapped) && !is_already_nfc(mapped)) {
181 if (!normalize(mapped)) {
182 return false;
183 }
184 }
185
186 // CheckBidi applies to every label once any label is RTL (RFC 5893).
187 const bool bidi_domain = mapped_domain_is_bidi(mapped);
188
189 // Estimate ASCII output size (punycode may expand non-ASCII labels).
190 out.reserve(mapped.size() + 8);
191
192 // Walk labels with a single pointer scan (no repeated string::find).
193 const char32_t* p = mapped.data();
194 const char32_t* const end = p + mapped.size();
195 std::u32string post_map;
196
197 while (p < end) {
198 const char32_t* label_begin = p;
199 while (p < end && *p != U'.') {
200 ++p;
201 }
202 const size_t label_size = static_cast<size_t>(p - label_begin);
203 std::u32string_view label_view(label_begin, label_size);
204 const bool is_last_label = (p == end);
205 if (p < end) {
206 ++p; // skip dot
207 }
208
209 if (label_size == 0) {
210 // empty label
211 } else if (is_ace_prefix(label_view)) {
212 for (char32_t c : label_view) {
213 if (c >= 0x80) {
214 out.clear();
215 return false;
216 }
217 }
218 append_ascii_label(out, label_view);
219 std::string_view puny_segment_ascii(
220 out.data() + (out.size() - label_size) + 4, label_size - 4);
221 working.clear();
222 if (!ada::idna::punycode_to_utf32(puny_segment_ascii, working)) {
223 out.clear();
224 return false;
225 }
226 if (is_ascii(working)) {
227 out.clear();
228 return false;
229 }
230 if (!ada::idna::map(working, post_map) || working != post_map) {
231 out.clear();
232 return false;
233 }
234 // Mapping must be stable; NFC must not change the mapped form either.
235 if (!is_ascii(post_map) && !is_already_nfc(post_map)) {
236 if (!normalize(post_map) || post_map != working) {
237 out.clear();
238 return false;
239 }
240 }
241 if (post_map.empty() || !check_label(post_map, bidi_domain)) {
242 out.clear();
243 return false;
244 }
245 } else if (is_ascii(label_view)) {
246 // All-ASCII domains never reach here (they return above). An ASCII label
247 // in a Bidi domain must still satisfy RFC 5893 rules 1, 5, and 6.
248 if (bidi_domain && !check_label(label_view, true)) {
249 out.clear();
250 return false;
251 }
252 append_ascii_label(out, label_view);
253 } else {
254 if (!check_label(label_view, bidi_domain)) {
255 out.clear();
256 return false;
257 }
258 out.append("xn--");
259 if (!ada::idna::utf32_to_punycode(label_view, out)) {
260 out.clear();
261 return false;
262 }
263 }
264 if (!is_last_label) {
265 out.push_back('.');
266 }
267 }
268 return true;
269}
270
271std::string to_ascii(std::string_view ut8_string) {
272 std::string out;
273 if (!to_ascii(ut8_string, out)) {
274 return {};
275 }
276 return out;
277}
278} // namespace ada::idna
static bool is_ace_prefix(std::u32string_view label) noexcept
void ascii_map(char *input, size_t length)
bool punycode_to_utf32(std::string_view input, std::u32string &out)
size_t utf32_length_from_utf8(const char *buf, size_t len)
Definition ada_idna.cpp:142
static bool check_label(const std::u32string_view label, bool bidi_domain)
static constexpr uint8_t is_forbidden_domain_code_point_table[]
bool constexpr is_ascii(std::u32string_view view)
static void from_ascii_to_ascii(std::string_view ut8_string, std::string &out)
bool utf32_to_punycode(std::u32string_view input, std::string &out)
static bool mapped_domain_is_bidi(std::u32string_view mapped)
std::string to_ascii(std::string_view ut8_string)
bool normalize(std::u32string &input)
bool is_forbidden_domain_code_point(const char c) noexcept
bool is_already_nfc(std::u32string_view input) noexcept
bool contains_forbidden_domain_code_point(std::string_view ascii_string)
std::u32string map(std::u32string_view input)
size_t utf8_to_utf32(const char *buf, size_t len, char32_t *utf32_output)
Definition ada_idna.cpp:32
static void append_ascii_label(std::string &out, std::u32string_view label)
constexpr size_t max_domain_input_bytes
Definition limits.h:12
static bool label_contains_rtl(std::u32string_view label) noexcept