19
19
#include " idna_code_point_map_iterator.hpp"
20
20
#include " punycode.hpp"
21
21
22
- #if !defined(SKYR_DOMAIN_MAX_DOMAIN_LENGTH)
23
- #define SKYR_DOMAIN_MAX_DOMAIN_LENGTH 253
24
- #endif // !defined(SKYR_DOMAIN_MAX_DOMAIN_LENGTH)
25
-
26
- #if !defined(SKYR_DOMAIN_MAX_LABEL_LENGTH)
27
- #define SKYR_DOMAIN_MAX_LABEL_LENGTH 63
28
- #endif // !defined(SKYR_LABEL_MAX_DOMAIN_LENGTH)
29
22
30
23
// / How many labels can be in a domain?
31
24
// / https://www.farsightsecurity.com/blog/txt-record/rrlabel-20171013/
@@ -41,7 +34,7 @@ auto map_code_points(
41
34
U32Range &&domain_name,
42
35
bool use_std3_ascii_rules,
43
36
bool transitional_processing,
44
- static_vector< char32_t , SKYR_DOMAIN_MAX_DOMAIN_LENGTH> *result)
37
+ std::u32string *result)
45
38
-> tl::expected<void, domain_errc> {
46
39
auto range = views::map_code_points (domain_name, use_std3_ascii_rules, transitional_processing);
47
40
auto first = std::cbegin (range);
@@ -50,7 +43,7 @@ auto map_code_points(
50
43
if (!*it) {
51
44
return tl::make_unexpected ((*it).error ());
52
45
}
53
- result->emplace_back ((*it).value ());
46
+ result->push_back ((*it).value ());
54
47
if (result->size () == result->max_size ()) {
55
48
return tl::make_unexpected (domain_errc::invalid_length);
56
49
}
@@ -113,7 +106,7 @@ auto domain_to_ascii(
113
106
using namespace std ::string_view_literals;
114
107
115
108
auto u32domain_name = unicode::views::as_u8 (domain_name) | unicode::transforms::to_u32;
116
- auto mapped_domain_name = static_vector< char32_t , SKYR_DOMAIN_MAX_DOMAIN_LENGTH> {};
109
+ auto mapped_domain_name = std::u32string {};
117
110
auto result = map_code_points (u32domain_name, use_std3_ascii_rules, transitional_processing, &mapped_domain_name);
118
111
if (!result) {
119
112
return tl::make_unexpected (result.error ());
@@ -123,8 +116,7 @@ auto domain_to_ascii(
123
116
return std::u32string_view (std::addressof (*std::begin (label)), ranges::distance (label));
124
117
};
125
118
126
- // / TODO: try this without allocating strings (e.g. for large strings that don't use SBO)
127
- auto labels = static_vector<static_vector<char32_t , SKYR_DOMAIN_MAX_LABEL_LENGTH>, SKYR_DOMAIN_MAX_NUM_LABELS>{};
119
+ auto labels = static_vector<std::u32string, SKYR_DOMAIN_MAX_NUM_LABELS>{};
128
120
for (auto &&label : mapped_domain_name | ranges::views::split (U' .' ) | ranges::views::transform (to_string_view)) {
129
121
if (labels.size () == labels.max_size ()) {
130
122
return tl::make_unexpected (domain_errc::too_many_labels);
@@ -174,15 +166,18 @@ auto domain_to_ascii(
174
166
labels.emplace_back ();
175
167
}
176
168
169
+ constexpr auto max_domain_length = 253 ;
170
+ constexpr auto max_label_length = 63 ;
171
+
177
172
if (verify_dns_length) {
178
173
auto length = mapped_domain_name.size ();
179
- if ((length < 1 ) || (length > 253 )) {
174
+ if ((length < 1 ) || (length > max_domain_length )) {
180
175
return tl::make_unexpected (domain_errc::invalid_length);
181
176
}
182
177
183
178
for (const auto &label : labels) {
184
179
auto label_length = label.size ();
185
- if ((label_length < 1 ) || (label_length > 63 )) {
180
+ if ((label_length < 1 ) || (label_length > max_label_length )) {
186
181
return tl::make_unexpected (domain_errc::invalid_length);
187
182
}
188
183
}
@@ -213,7 +208,7 @@ auto domain_to_u8(std::string_view domain_name, [[maybe_unused]] bool *validatio
213
208
return std::string_view (std::addressof (*std::begin (label)), ranges::distance (label));
214
209
};
215
210
216
- auto labels = static_vector<static_vector< char , SKYR_DOMAIN_MAX_LABEL_LENGTH> , SKYR_DOMAIN_MAX_NUM_LABELS>{};
211
+ auto labels = static_vector<std::string , SKYR_DOMAIN_MAX_NUM_LABELS>{};
217
212
for (auto &&label : domain_name | ranges::views::split (' .' ) | ranges::views::transform (to_string_view)) {
218
213
if (labels.size () == labels.max_size ()) {
219
214
return tl::make_unexpected (domain_errc::too_many_labels);
0 commit comments