Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
ada_idna.h
Go to the documentation of this file.
1/* auto-generated from the idna component. Do not edit! */
2/* begin file include/idna.h */
3#ifndef ADA_IDNA_H
4#define ADA_IDNA_H
5
6/* begin file include/ada/idna/unicode_transcoding.h */
7#ifndef ADA_IDNA_UNICODE_TRANSCODING_H
8#define ADA_IDNA_UNICODE_TRANSCODING_H
9
10#include <string>
11#include <string_view>
12
13namespace ada::idna {
14
15size_t utf8_to_utf32(const char* buf, size_t len, char32_t* utf32_output);
16
17size_t utf8_length_from_utf32(const char32_t* buf, size_t len);
18
19size_t utf32_length_from_utf8(const char* buf, size_t len);
20
21size_t utf32_to_utf8(const char32_t* buf, size_t len, char* utf8_output);
22
23} // namespace ada::idna
24
25#endif // ADA_IDNA_UNICODE_TRANSCODING_H
26/* end file include/ada/idna/unicode_transcoding.h */
27/* begin file include/ada/idna/mapping.h */
28#ifndef ADA_IDNA_MAPPING_H
29#define ADA_IDNA_MAPPING_H
30
31#include <string>
32#include <string_view>
33
34namespace ada::idna {
35
36// If the input is ascii, then the mapping is just -> lower case.
37void ascii_map(char* input, size_t length);
38// Map the characters according to IDNA, returning the empty string on error.
39std::u32string map(std::u32string_view input);
40// Map into an existing buffer (cleared on entry). Returns false if any code
41// point is disallowed. Reusing the buffer avoids repeated heap allocations
42// when called in a loop over multiple labels.
43bool map(std::u32string_view input, std::u32string& out);
44
45} // namespace ada::idna
46
47#endif
48/* end file include/ada/idna/mapping.h */
49/* begin file include/ada/idna/normalization.h */
50#ifndef ADA_IDNA_NORMALIZATION_H
51#define ADA_IDNA_NORMALIZATION_H
52
53#include <string>
54#include <string_view>
55
56namespace ada::idna {
57
58// Returns true if `input` is already in Unicode Normalization Form C.
59// Requires that internal tables have been loaded (call ensure via normalize
60// or map first, or this returns false if tables are unavailable).
61[[nodiscard]] bool is_already_nfc(std::u32string_view input) noexcept;
62
63// Normalize the characters according to IDNA (Unicode Normalization Form C).
64// Returns false if the internal Unicode tables could not be loaded; in that
65// case `input` is left unchanged. Skips work when the string is already NFC.
66[[nodiscard]] bool normalize(std::u32string& input);
67
68} // namespace ada::idna
69#endif
70/* end file include/ada/idna/normalization.h */
71/* begin file include/ada/idna/punycode.h */
72#ifndef ADA_IDNA_PUNYCODE_H
73#define ADA_IDNA_PUNYCODE_H
74
75#include <string>
76#include <string_view>
77
78namespace ada::idna {
79
80bool punycode_to_utf32(std::string_view input, std::u32string& out);
81bool verify_punycode(std::string_view input);
82bool utf32_to_punycode(std::u32string_view input, std::string& out);
83
84} // namespace ada::idna
85
86#endif // ADA_IDNA_PUNYCODE_H
87/* end file include/ada/idna/punycode.h */
88/* begin file include/ada/idna/validity.h */
89#ifndef ADA_IDNA_VALIDITY_H
90#define ADA_IDNA_VALIDITY_H
91
92#include <string>
93#include <string_view>
94
95namespace ada::idna {
96
100bool is_label_valid(std::u32string_view label);
101
102} // namespace ada::idna
103
104#endif // ADA_IDNA_VALIDITY_H
105/* end file include/ada/idna/validity.h */
106/* begin file include/ada/idna/to_ascii.h */
107#ifndef ADA_IDNA_TO_ASCII_H
108#define ADA_IDNA_TO_ASCII_H
109
110#include <string>
111#include <string_view>
112
113/* begin file include/ada/idna/limits.h */
114#ifndef ADA_IDNA_LIMITS_H
115#define ADA_IDNA_LIMITS_H
116
117#include <cstddef>
118
119namespace ada::idna {
120
121// Maximum accepted UTF-8 domain length for non-ASCII to_ascii input and for
122// to_unicode input.
123// Bounds heap growth under untrusted input (DoS resistance). DNS wire limits
124// are smaller; this allows long Unicode labels used in URL tests/fixtures.
125inline constexpr size_t max_domain_input_bytes = 16384;
126
127} // namespace ada::idna
128
129#endif // ADA_IDNA_LIMITS_H
130/* end file include/ada/idna/limits.h */
131
132namespace ada::idna {
133
134// Converts a domain (e.g., www.google.com) possibly containing international
135// characters to an ascii domain (with punycode). It will not do percent
136// decoding: percent decoding should be done prior to calling this function. We
137// do not remove tabs and spaces, they should have been removed prior to calling
138// this function. We also do not trim control characters. We also assume that
139// the input is not empty. We return "" on error. Non-ASCII inputs longer than
140// max_domain_input_bytes are rejected; ASCII inputs of any length are accepted
141// (they are only lowercased).
142//
143// This function may accept or even produce invalid domains (WHATWG carve-outs).
144std::string to_ascii(std::string_view ut8_string);
145
146// Same as to_ascii, but writes into `out` and returns false on error without
147// relying on empty-string ambiguity.
148[[nodiscard]] bool to_ascii(std::string_view ut8_string, std::string& out);
149
150// Returns true if the string contains a forbidden code point according to the
151// WHATGL URL specification:
152// https://url.spec.whatwg.org/#forbidden-domain-code-point
153bool contains_forbidden_domain_code_point(std::string_view ascii_string);
154
155bool constexpr is_ascii(std::u32string_view view);
156bool constexpr is_ascii(std::string_view view);
157
158} // namespace ada::idna
159
160#endif // ADA_IDNA_TO_ASCII_H
161/* end file include/ada/idna/to_ascii.h */
162/* begin file include/ada/idna/to_unicode.h */
163#ifndef ADA_IDNA_TO_UNICODE_H
164#define ADA_IDNA_TO_UNICODE_H
165
166#include <string>
167#include <string_view>
168
169
170namespace ada::idna {
171
172// UTS #46 ToUnicode. Never fails per the standard: on step failure the original
173// label is kept. Inputs longer than max_domain_input_bytes are returned
174// unchanged as a safety measure under untrusted input.
175std::string to_unicode(std::string_view input);
176
177// Writes into `out`. Returns false only if the input exceeds
178// max_domain_input_bytes (out is left empty). Otherwise always returns true
179// (ToUnicode does not fail).
180[[nodiscard]] bool to_unicode(std::string_view input, std::string& out);
181
182} // namespace ada::idna
183
184#endif // ADA_IDNA_TO_UNICODE_H
185/* end file include/ada/idna/to_unicode.h */
186/* begin file include/ada/idna/identifier.h */
187#ifndef ADA_IDNA_IDENTIFIER_H
188#define ADA_IDNA_IDENTIFIER_H
189
190#include <string>
191#include <string_view>
192
193namespace ada::idna {
194
195// Verify if it is valid name code point given a Unicode code point and a
196// boolean first: If first is true return the result of checking if code point
197// is contained in the IdentifierStart set of code points. Otherwise return the
198// result of checking if code point is contained in the IdentifierPart set of
199// code points. Returns false if the input is empty or the code point is not
200// valid. There is minimal Unicode error handling: the input should be valid
201// UTF-8. https://urlpattern.spec.whatwg.org/#is-a-valid-name-code-point
202bool valid_name_code_point(char32_t code_point, bool first);
203
204} // namespace ada::idna
205
206#endif
207/* end file include/ada/idna/identifier.h */
208
209#endif
210/* end file include/idna.h */
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
size_t utf32_to_utf8(const char32_t *buf, size_t len, char *utf8_output)
Definition ada_idna.cpp:151
bool constexpr is_ascii(std::u32string_view view)
bool utf32_to_punycode(std::u32string_view input, std::string &out)
std::string to_ascii(std::string_view ut8_string)
std::string to_unicode(std::string_view input)
bool normalize(std::u32string &input)
bool valid_name_code_point(char32_t code_point, bool first)
size_t utf8_length_from_utf32(const char32_t *buf, size_t len)
Definition ada_idna.cpp:129
bool is_already_nfc(std::u32string_view input) noexcept
bool is_label_valid(std::u32string_view label)
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
constexpr size_t max_domain_input_bytes
Definition limits.h:12
bool verify_punycode(std::string_view input)