2023-06-15 19:57:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Simon Wanner <simon@skyrising.xyz>
|
2024-06-18 16:11:40 +00:00
|
|
|
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
2023-06-15 19:57:13 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-06-23 13:14:27 +00:00
|
|
|
#include <LibUnicode/ICU.h>
|
2023-06-15 19:57:13 +00:00
|
|
|
#include <LibUnicode/IDNA.h>
|
|
|
|
|
2024-06-18 16:11:40 +00:00
|
|
|
#include <unicode/idna.h>
|
2023-06-15 19:57:13 +00:00
|
|
|
|
|
|
|
namespace Unicode::IDNA {
|
|
|
|
|
|
|
|
// https://www.unicode.org/reports/tr46/#ToASCII
|
|
|
|
ErrorOr<String> to_ascii(Utf8View domain_name, ToAsciiOptions const& options)
|
|
|
|
{
|
2024-06-18 16:11:40 +00:00
|
|
|
u32 icu_options = UIDNA_DEFAULT;
|
2023-06-15 19:57:13 +00:00
|
|
|
|
2024-06-18 16:11:40 +00:00
|
|
|
if (options.check_bidi == CheckBidi::Yes)
|
|
|
|
icu_options |= UIDNA_CHECK_BIDI;
|
|
|
|
if (options.check_joiners == CheckJoiners::Yes)
|
|
|
|
icu_options |= UIDNA_CHECK_CONTEXTJ;
|
|
|
|
if (options.use_std3_ascii_rules == UseStd3AsciiRules::Yes)
|
|
|
|
icu_options |= UIDNA_USE_STD3_RULES;
|
|
|
|
if (options.transitional_processing == TransitionalProcessing::No)
|
|
|
|
icu_options |= UIDNA_NONTRANSITIONAL_TO_ASCII | UIDNA_NONTRANSITIONAL_TO_UNICODE;
|
2023-06-15 19:57:13 +00:00
|
|
|
|
2024-06-18 16:11:40 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2023-06-15 19:57:13 +00:00
|
|
|
|
2024-06-18 16:11:40 +00:00
|
|
|
auto idna = adopt_own_if_nonnull(icu::IDNA::createUTS46Instance(icu_options, status));
|
2024-06-23 13:14:27 +00:00
|
|
|
if (icu_failure(status))
|
2024-06-18 16:11:40 +00:00
|
|
|
return Error::from_string_literal("Unable to create an IDNA instance");
|
2023-06-15 19:57:13 +00:00
|
|
|
|
2024-06-18 16:11:40 +00:00
|
|
|
StringBuilder builder { domain_name.as_string().length() };
|
|
|
|
icu::StringByteSink sink { &builder };
|
2023-06-15 19:57:13 +00:00
|
|
|
|
2024-06-18 16:11:40 +00:00
|
|
|
icu::IDNAInfo info;
|
2024-06-23 13:14:27 +00:00
|
|
|
idna->nameToASCII_UTF8(icu_string_piece(domain_name.as_string()), sink, info, status);
|
2023-06-15 19:57:13 +00:00
|
|
|
|
2024-06-18 16:11:40 +00:00
|
|
|
auto errors = info.getErrors();
|
|
|
|
|
|
|
|
if (options.check_hyphens == CheckHyphens::No) {
|
|
|
|
errors &= ~UIDNA_ERROR_HYPHEN_3_4;
|
|
|
|
errors &= ~UIDNA_ERROR_LEADING_HYPHEN;
|
|
|
|
errors &= ~UIDNA_ERROR_TRAILING_HYPHEN;
|
|
|
|
}
|
|
|
|
if (options.verify_dns_length == VerifyDnsLength::No) {
|
|
|
|
errors &= ~UIDNA_ERROR_EMPTY_LABEL;
|
|
|
|
errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
|
|
|
|
errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
|
2023-06-15 19:57:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 13:14:27 +00:00
|
|
|
if (icu_failure(status) || errors != 0)
|
2024-06-18 16:11:40 +00:00
|
|
|
return Error::from_string_literal("Unable to convert domain to ASCII");
|
2023-06-15 19:57:13 +00:00
|
|
|
|
2024-06-18 16:11:40 +00:00
|
|
|
return builder.to_string();
|
2023-06-15 19:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|