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