IDNA.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2023, Simon Wanner <simon@skyrising.xyz>
  3. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibUnicode/ICU.h>
  8. #include <LibUnicode/IDNA.h>
  9. #include <unicode/idna.h>
  10. namespace Unicode::IDNA {
  11. // https://www.unicode.org/reports/tr46/#ToASCII
  12. ErrorOr<String> to_ascii(Utf8View domain_name, ToAsciiOptions const& options)
  13. {
  14. u32 icu_options = UIDNA_DEFAULT;
  15. if (options.check_bidi == CheckBidi::Yes)
  16. icu_options |= UIDNA_CHECK_BIDI;
  17. if (options.check_joiners == CheckJoiners::Yes)
  18. icu_options |= UIDNA_CHECK_CONTEXTJ;
  19. if (options.use_std3_ascii_rules == UseStd3AsciiRules::Yes)
  20. icu_options |= UIDNA_USE_STD3_RULES;
  21. if (options.transitional_processing == TransitionalProcessing::No)
  22. icu_options |= UIDNA_NONTRANSITIONAL_TO_ASCII | UIDNA_NONTRANSITIONAL_TO_UNICODE;
  23. UErrorCode status = U_ZERO_ERROR;
  24. auto idna = adopt_own_if_nonnull(icu::IDNA::createUTS46Instance(icu_options, status));
  25. if (icu_failure(status))
  26. return Error::from_string_literal("Unable to create an IDNA instance");
  27. StringBuilder builder { domain_name.as_string().length() };
  28. icu::StringByteSink sink { &builder };
  29. icu::IDNAInfo info;
  30. idna->nameToASCII_UTF8(icu_string_piece(domain_name.as_string()), sink, info, status);
  31. auto errors = info.getErrors();
  32. if (options.check_hyphens == CheckHyphens::No) {
  33. errors &= ~UIDNA_ERROR_HYPHEN_3_4;
  34. errors &= ~UIDNA_ERROR_LEADING_HYPHEN;
  35. errors &= ~UIDNA_ERROR_TRAILING_HYPHEN;
  36. }
  37. if (options.verify_dns_length == VerifyDnsLength::No) {
  38. errors &= ~UIDNA_ERROR_EMPTY_LABEL;
  39. errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
  40. errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
  41. }
  42. if (icu_failure(status) || errors != 0)
  43. return Error::from_string_literal("Unable to convert domain to ASCII");
  44. return builder.to_string();
  45. }
  46. }