libidn: Example 3
1
1 9.3 Example 3
1 =============
1
1 This example demonstrates how the library is used to convert
1 internationalized domain names into ASCII compatible names.
1
1 /* example3.c --- Example ToASCII() code showing how to use Libidn.
1 * Copyright (C) 2002-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 #include <locale.h> /* setlocale() */
1 #include <stringprep.h> /* stringprep_locale_charset() */
1 #include <idna.h> /* idna_to_ascii_lz() */
1
1 /*
1 * Compiling using libtool and pkg-config is recommended:
1 *
1 * $ libtool cc -o example3 example3.c `pkg-config --cflags --libs libidn`
1 * $ ./example3
1 * Input domain encoded as `ISO-8859-1': www.räksmörgåsª.example
1 * Read string (length 23): 77 77 77 2e 72 e4 6b 73 6d f6 72 67 e5 73 aa 2e 65 78 61 6d 70 6c 65
1 * ACE label (length 33): 'www.xn--rksmrgsa-0zap8p.example'
1 * 77 77 77 2e 78 6e 2d 2d 72 6b 73 6d 72 67 73 61 2d 30 7a 61 70 38 70 2e 65 78 61 6d 70 6c 65
1 * $
1 *
1 */
1
1 int
1 main (void)
1 {
1 char buf[BUFSIZ];
1 char *p;
1 int rc;
1 size_t i;
1
1 setlocale (LC_ALL, "");
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 rc = idna_to_ascii_lz (buf, &p, 0);
1 if (rc != IDNA_SUCCESS)
1 {
1 printf ("ToASCII() failed (%d): %s\n", rc, idna_strerror (rc));
1 return EXIT_FAILURE;
1 }
1
1 printf ("ACE label (length %ld): '%s'\n", (long int) strlen (p), p);
1 for (i = 0; i < strlen (p); i++)
1 printf ("%02x ", (unsigned) p[i] & 0xFF);
1 printf ("\n");
1
1 free (p);
1
1 return 0;
1 }
1