Update, sync with libidn.texi.
[libidn.git] / examples / example5.c
blobdca8826523a8e95dfe4ec59199792c2740fe1c86
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/>.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 /* Get stringprep_locale_charset, etc. */
26 #include <stringprep.h>
28 /* Get idna_to_ascii_8z, etc. */
29 #include <idna.h>
31 /* Get tld_check_4z. */
32 #include <tld.h>
35 * Compiling using libtool and pkg-config is recommended:
37 * $ libtool cc -o example5 example5.c `pkg-config --cflags --libs libidn`
38 * $ ./example5
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
45 * $ ./example5
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
54 int
55 main (int argc, char *argv[])
57 char buf[BUFSIZ];
58 char *p;
59 uint32_t *r;
60 int rc;
61 size_t errpos, i;
63 printf ("Input domain encoded as `%s': ", stringprep_locale_charset ());
64 fflush (stdout);
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);
71 printf ("\n");
73 p = stringprep_locale_to_utf8 (buf);
74 if (p)
76 strcpy (buf, p);
77 free (p);
79 else
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));
86 return 2;
89 printf ("ToASCII string (length %d): %s\n", strlen (p), p);
91 rc = idna_to_unicode_8z4z (p, &r, 0);
92 free (p);
93 if (rc != IDNA_SUCCESS)
95 printf ("idna_to_unicode_8z4z failed (%d): %s\n",
96 rc, idna_strerror (rc));
97 return 2;
100 printf ("ToUnicode string: ");
101 for (i = 0; r[i]; i++)
102 printf ("U+%04x ", r[i]);
103 printf ("\n");
105 rc = tld_check_4z (r, &errpos, NULL);
106 free (r);
107 if (rc == TLD_INVALID)
109 printf ("Domain rejected by TLD check, Unicode position %d\n", errpos);
110 return 1;
112 else if (rc != TLD_SUCCESS)
114 printf ("tld_check_4z() failed (%d): %s\n", rc, tld_strerror (rc));
115 return 2;
118 printf ("Domain accepted by TLD check\n");
120 return 0;