idna_name_classify.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Classify a domain name for IDNA purposes.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <inet/net-internal.h>
  17. #include <stdbool.h>
  18. #include <string.h>
  19. #include <wchar.h>
  20. enum idna_name_classification
  21. __idna_name_classify (const char *name)
  22. {
  23. mbstate_t mbs;
  24. memset (&mbs, 0, sizeof (mbs));
  25. const char *p = name;
  26. const char *end = p + strlen (p) + 1;
  27. bool nonascii = false;
  28. bool backslash = false;
  29. while (true)
  30. {
  31. wchar_t wc;
  32. size_t result = mbrtowc (&wc, p, end - p, &mbs);
  33. if (result == 0)
  34. /* NUL terminator was reached. */
  35. break;
  36. else if (result == (size_t) -2)
  37. /* Incomplete trailing multi-byte character. This is an
  38. encoding error becaue we received the full name. */
  39. return idna_name_encoding_error;
  40. else if (result == (size_t) -1)
  41. {
  42. /* Other error, including EILSEQ. */
  43. if (errno == EILSEQ)
  44. return idna_name_encoding_error;
  45. else if (errno == ENOMEM)
  46. return idna_name_memory_error;
  47. else
  48. return idna_name_error;
  49. }
  50. else
  51. {
  52. /* A wide character was decoded. */
  53. p += result;
  54. if (wc == L'\\')
  55. backslash = true;
  56. else if (wc > 127)
  57. nonascii = true;
  58. }
  59. }
  60. if (nonascii)
  61. {
  62. if (backslash)
  63. return idna_name_nonascii_backslash;
  64. else
  65. return idna_name_nonascii;
  66. }
  67. else
  68. return idna_name_ascii;
  69. }