1 /* example5.c --- Example TLD checking.
2 * Copyright (C) 2004, 2005, 2006, 2007 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /* Get stringprep_locale_charset, etc. */
26 #include <stringprep.h>
28 /* Get idna_to_ascii_8z, etc. */
31 /* Get tld_check_4z. */
35 * Compiling using libtool and pkg-config is recommended:
37 * $ libtool cc -o example5 example5.c `pkg-config --cflags --libs libidn`
39 * Input domain encoded as `UTF-8': fooß.no
40 * Read string (length 8): 66 6f 6f c3 9f 2e 6e 6f
41 * ToASCII string (length 8): fooss.no
42 * ToUnicode string: U+0066 U+006f U+006f U+0073 U+0073 U+002e U+006e U+006f
43 * Domain accepted by TLD check
46 * Input domain encoded as `UTF-8': gr€€n.no
47 * Read string (length 12): 67 72 e2 82 ac e2 82 ac 6e 2e 6e 6f
48 * ToASCII string (length 16): xn--grn-l50aa.no
49 * ToUnicode string: U+0067 U+0072 U+20ac U+20ac U+006e U+002e U+006e U+006f
50 * Domain rejected by TLD check, Unicode position 2
55 main (int argc
, char *argv
[])
63 printf ("Input domain encoded as `%s': ", stringprep_locale_charset ());
65 fgets (buf
, BUFSIZ
, stdin
);
66 buf
[strlen (buf
) - 1] = '\0';
68 printf ("Read string (length %d): ", strlen (buf
));
69 for (i
= 0; i
< strlen (buf
); i
++)
70 printf ("%02x ", buf
[i
] & 0xFF);
73 p
= stringprep_locale_to_utf8 (buf
);
80 printf ("Could not convert string to UTF-8, continuing anyway...\n");
82 rc
= idna_to_ascii_8z (buf
, &p
, 0);
83 if (rc
!= IDNA_SUCCESS
)
85 printf ("idna_to_ascii_8z failed (%d): %s\n", rc
, idna_strerror (rc
));
89 printf ("ToASCII string (length %d): %s\n", strlen (p
), p
);
91 rc
= idna_to_unicode_8z4z (p
, &r
, 0);
93 if (rc
!= IDNA_SUCCESS
)
95 printf ("idna_to_unicode_8z4z failed (%d): %s\n",
96 rc
, idna_strerror (rc
));
100 printf ("ToUnicode string: ");
101 for (i
= 0; r
[i
]; i
++)
102 printf ("U+%04x ", r
[i
]);
105 rc
= tld_check_4z (r
, &errpos
, NULL
);
107 if (rc
== TLD_INVALID
)
109 printf ("Domain rejected by TLD check, Unicode position %d\n", errpos
);
112 else if (rc
!= TLD_SUCCESS
)
114 printf ("tld_check_4z() failed (%d): %s\n", rc
, tld_strerror (rc
));
118 printf ("Domain accepted by TLD check\n");