libidn: Example 5
1
1 9.5 Example 5
1 =============
1
1 This example demonstrates how the library is used to check a string for
1 invalid characters within a specific TLD.
1
1 /* example5.c --- Example TLD checking.
1 * Copyright (C) 2004-2016 Simon Josefsson
1 *
1 * This file is part of GNU Libidn.
1 *
1 * This program is free software: you can redistribute it and/or modify
1 * it under the terms of the GNU General Public License as published by
1 * the Free Software Foundation, either version 3 of the License, or
1 * (at your option) any later version.
1 *
1 * This program is distributed in the hope that it will be useful,
1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1 * GNU General Public License for more details.
1 *
1 * You should have received a copy of the GNU General Public License
1 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1 *
1 */
1
1 #include <stdio.h>
1 #include <stdlib.h>
1 #include <string.h>
1
1 /* Get stringprep_locale_charset, etc. */
1 #include <stringprep.h>
1
1 /* Get idna_to_ascii_8z, etc. */
1 #include <idna.h>
1
1 /* Get tld_check_4z. */
1 #include <tld.h>
1
1 /*
1 * Compiling using libtool and pkg-config is recommended:
1 *
1 * $ libtool cc -o example5 example5.c `pkg-config --cflags --libs libidn`
1 * $ ./example5
1 * Input domain encoded as `UTF-8': fooß.no
1 * Read string (length 8): 66 6f 6f c3 9f 2e 6e 6f
1 * ToASCII string (length 8): fooss.no
1 * ToUnicode string: U+0066 U+006f U+006f U+0073 U+0073 U+002e U+006e U+006f
1 * Domain accepted by TLD check
1 *
1 * $ ./example5
1 * Input domain encoded as `UTF-8': gr€€n.no
1 * Read string (length 12): 67 72 e2 82 ac e2 82 ac 6e 2e 6e 6f
1 * ToASCII string (length 16): xn--grn-l50aa.no
1 * ToUnicode string: U+0067 U+0072 U+20ac U+20ac U+006e U+002e U+006e U+006f
1 * Domain rejected by TLD check, Unicode position 2
1 *
1 */
1
1 int
1 main (void)
1 {
1 char buf[BUFSIZ];
1 char *p;
1 uint32_t *r;
1 int rc;
1 size_t errpos, i;
1
1 printf ("Input domain encoded as `%s': ", stringprep_locale_charset ());
1 fflush (stdout);
1 if (!fgets (buf, BUFSIZ, stdin))
1 perror ("fgets");
1 buf[strlen (buf) - 1] = '\0';
1
1 printf ("Read string (length %ld): ", (long int) strlen (buf));
1 for (i = 0; i < strlen (buf); i++)
1 printf ("%02x ", (unsigned) buf[i] & 0xFF);
1 printf ("\n");
1
1 p = stringprep_locale_to_utf8 (buf);
1 if (p)
1 {
1 strcpy (buf, p);
1 free (p);
1 }
1 else
1 printf ("Could not convert string to UTF-8, continuing anyway...\n");
1
1 rc = idna_to_ascii_8z (buf, &p, 0);
1 if (rc != IDNA_SUCCESS)
1 {
1 printf ("idna_to_ascii_8z failed (%d): %s\n", rc, idna_strerror (rc));
1 return 2;
1 }
1
1 printf ("ToASCII string (length %ld): %s\n", (long int) strlen (p), p);
1
1 rc = idna_to_unicode_8z4z (p, &r, 0);
1 free (p);
1 if (rc != IDNA_SUCCESS)
1 {
1 printf ("idna_to_unicode_8z4z failed (%d): %s\n",
1 rc, idna_strerror (rc));
1 return 2;
1 }
1
1 printf ("ToUnicode string: ");
1 for (i = 0; r[i]; i++)
1 printf ("U+%04x ", r[i]);
1 printf ("\n");
1
1 rc = tld_check_4z (r, &errpos, NULL);
1 free (r);
1 if (rc == TLD_INVALID)
1 {
1 printf ("Domain rejected by TLD check, Unicode position %ld\n", (long int) errpos);
1 return 1;
1 }
1 else if (rc != TLD_SUCCESS)
1 {
1 printf ("tld_check_4z() failed (%d): %s\n", rc, tld_strerror (rc));
1 return 2;
1 }
1
1 printf ("Domain accepted by TLD check\n");
1
1 return 0;
1 }
1