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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 #include <inet/net-internal.h>
25 enum idna_name_classification
26 __idna_name_classify (const char *name
)
29 memset (&mbs
, 0, sizeof (mbs
));
31 const char *end
= p
+ strlen (p
) + 1;
32 bool nonascii
= false;
33 bool backslash
= false;
37 size_t result
= mbrtowc (&wc
, p
, end
- p
, &mbs
);
39 /* NUL terminator was reached. */
41 else if (result
== (size_t) -2)
42 /* Incomplete trailing multi-byte character. This is an
43 encoding error becaue we received the full name. */
44 return idna_name_encoding_error
;
45 else if (result
== (size_t) -1)
47 /* Other error, including EILSEQ. */
49 return idna_name_encoding_error
;
50 else if (errno
== ENOMEM
)
51 return idna_name_memory_error
;
53 return idna_name_error
;
57 /* A wide character was decoded. */
69 return idna_name_nonascii_backslash
;
71 return idna_name_nonascii
;
74 return idna_name_ascii
;