00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 # include "config.h"
00025 #endif
00026
00027
00028 #include "stringprep.h"
00029
00030
00031 #include <stdio.h>
00032
00033
00034 #include <stdlib.h>
00035
00036
00037 #include <string.h>
00038
00039
00040 #include "striconv.h"
00041
00042 #ifdef _LIBC
00043 # define HAVE_ICONV 1
00044 # define HAVE_LOCALE_H 1
00045 # define HAVE_LANGINFO_CODESET 1
00046 #endif
00047
00048 #include <locale.h>
00049
00050 #ifdef HAVE_LANGINFO_CODESET
00051 # include <langinfo.h>
00052 #endif
00053
00054 #ifdef _LIBC
00055 # define stringprep_locale_charset() nl_langinfo (CODESET)
00056 #else
00057
00077 const char *
00078 stringprep_locale_charset (void)
00079 {
00080 const char *charset = getenv ("CHARSET");
00081
00082 if (charset && *charset)
00083 return charset;
00084
00085 # ifdef HAVE_LANGINFO_CODESET
00086 charset = nl_langinfo (CODESET);
00087
00088 if (charset && *charset)
00089 return charset;
00090 # endif
00091
00092 return "ASCII";
00093 }
00094 #endif
00095
00108 char *
00109 stringprep_convert (const char *str,
00110 const char *to_codeset, const char *from_codeset)
00111 {
00112 #if HAVE_ICONV
00113 return str_iconv (str, from_codeset, to_codeset);
00114 #else
00115 char *p;
00116 fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
00117 "convert data to UTF-8\n");
00118 p = malloc (strlen (str) + 1);
00119 if (!p)
00120 return NULL;
00121 return strcpy (p, str);
00122 #endif
00123 }
00124
00135 char *
00136 stringprep_locale_to_utf8 (const char *str)
00137 {
00138 return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
00139 }
00140
00151 char *
00152 stringprep_utf8_to_locale (const char *str)
00153 {
00154 return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");
00155 }