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
26 /* Get stringprep_locale_charset, etc. */
27 #include <stringprep.h>
29 /* Get idna_to_ascii_8z, etc. */
32 /* Get tld_check_4z. */
36 * Compiling using libtool and pkg-config is recommended:
38 * $ libtool cc -o example5 example5.c `pkg-config --cflags --libs libidn`
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
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
56 main (int argc
, char *argv
[])
64 printf ("Input domain encoded as `%s': ", stringprep_locale_charset ());
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);
74 p
= stringprep_locale_to_utf8 (buf
);
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): %s\n", rc
, idna_strerror (rc
));
90 printf ("ToASCII string (length %d): %s\n", strlen (p
), p
);
92 rc
= idna_to_unicode_8z4z (p
, &r
, 0);
94 if (rc
!= IDNA_SUCCESS
)
96 printf ("idna_to_unicode_8z4z failed (%d): %s\n",
97 rc
, idna_strerror (rc
));
101 printf ("ToUnicode string: ");
102 for (i
= 0; r
[i
]; i
++)
103 printf ("U+%04x ", r
[i
]);
106 rc
= tld_check_4z (r
, &errpos
, NULL
);
108 if (rc
== TLD_INVALID
)
110 printf ("Domain rejected by TLD check, Unicode position %d\n", errpos
);
113 else if (rc
!= TLD_SUCCESS
)
115 printf ("tld_check_4z() failed (%d): %s\n", rc
, tld_strerror (rc
));
119 printf ("Domain accepted by TLD check\n");