Handle draft-hoffman-idn-reg-*.txt tables better.
[libidn.git] / examples / example5.c
blob346aea1c2e0b2f12e3cf5d7ad448604125d8bace
1 /* example5.c --- Example TLD checking.
2 * Copyright (C) 2004 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * GNU Libidn 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 /* Get stringprep_locale_charset, etc. */
27 #include <stringprep.h>
29 /* Get idna_to_ascii_8z, etc. */
30 #include <idna.h>
32 /* Get tld_check_4z. */
33 #include <tld.h>
36 * Compiling using libtool and pkg-config is recommended:
38 * $ libtool cc -o example5 example5.c `pkg-config --cflags --libs libidn`
39 * $ ./example5
40 * Input domain encoded as `UTF-8': fooß.no
41 * Read string (length 8): 66 6f 6f c3 9f 2e 6e 6f
42 * ToASCII string (length 8): fooss.no
43 * ToUnicode string: U+0066 U+006f U+006f U+0073 U+0073 U+002e U+006e U+006f
44 * Domain accepted by TLD check
46 * $ ./example5
47 * Input domain encoded as `UTF-8': gr€€n.no
48 * Read string (length 12): 67 72 e2 82 ac e2 82 ac 6e 2e 6e 6f
49 * ToASCII string (length 16): xn--grn-l50aa.no
50 * ToUnicode string: U+0067 U+0072 U+20ac U+20ac U+006e U+002e U+006e U+006f
51 * Domain rejected by TLD check, Unicode position 2
55 int
56 main (int argc, char *argv[])
58 char buf[BUFSIZ];
59 char *p;
60 uint32_t *r;
61 int rc;
62 size_t errpos, i;
64 printf ("Input domain encoded as `%s': ", stringprep_locale_charset ());
65 fflush (stdout);
66 fgets (buf, BUFSIZ, stdin);
67 buf[strlen (buf) - 1] = '\0';
69 printf ("Read string (length %d): ", strlen (buf));
70 for (i = 0; i < strlen (buf); i++)
71 printf ("%02x ", buf[i] & 0xFF);
72 printf ("\n");
74 p = stringprep_locale_to_utf8 (buf);
75 if (p)
77 strcpy (buf, p);
78 free (p);
80 else
81 printf ("Could not convert string to UTF-8, continuing anyway...\n");
83 rc = idna_to_ascii_8z (buf, &p, 0);
84 if (rc != IDNA_SUCCESS)
86 printf ("idna_to_ascii_8z failed (%d)...\n", rc);
87 return 2;
90 printf ("ToASCII string (length %d): %s\n", strlen (p), p);
92 rc = idna_to_unicode_8z4z (p, &r, 0);
93 free (p);
94 if (rc != IDNA_SUCCESS)
96 printf ("idna_to_unicode_8z4z failed (%d)...\n", 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\n", rc);
115 return 2;
118 printf ("Domain accepted by TLD check\n");
120 return 0;