chiark / gitweb /
Apply https://sourceware.org/git/?p=glibc.git;a=commit;h=d5dd6189d506068ed11c8bfa1e1e...
[eglibc.git] / libidn / toutf8.c
1 /* toutf8.c --- Convert strings from system locale into UTF-8.
2  * Copyright (C) 2002, 2003, 2004, 2005  Simon Josefsson
3  *
4  * This file is part of GNU Libidn.
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 /* Get prototypes. */
27 #include "stringprep.h"
28
29 /* Get fprintf. */
30 #include <stdio.h>
31
32 /* Get getenv. */
33 #include <stdlib.h>
34
35 /* Get strlen. */
36 #include <string.h>
37
38 /* Get __OPTION_EGLIBC_LOCALE_CODE.  */
39 #ifdef _LIBC
40 # include <gnu/option-groups.h>
41 #endif
42
43 /* Get iconv_string. */
44 #include "iconvme.h"
45
46 #ifdef _LIBC
47 # define HAVE_ICONV 1
48 # define LOCALE_WORKS 1
49 #endif
50
51 #if LOCALE_WORKS
52 # include <langinfo.h>
53 # include <locale.h>
54 #endif
55
56 #ifdef _LIBC
57 # if __OPTION_EGLIBC_LOCALE_CODE
58 #  define stringprep_locale_charset() nl_langinfo (CODESET)
59 # else
60 #  define stringprep_locale_charset() "ANSI_X3.4-1968"
61 # endif
62 #else
63 /**
64  * stringprep_locale_charset - return charset used in current locale
65  *
66  * Find out current locale charset.  The function respect the CHARSET
67  * environment variable, but typically uses nl_langinfo(CODESET) when
68  * it is supported.  It fall back on "ASCII" if CHARSET isn't set and
69  * nl_langinfo isn't supported or return anything.
70  *
71  * Note that this function return the application's locale's preferred
72  * charset (or thread's locale's preffered charset, if your system
73  * support thread-specific locales).  It does not return what the
74  * system may be using.  Thus, if you receive data from external
75  * sources you cannot in general use this function to guess what
76  * charset it is encoded in.  Use stringprep_convert from the external
77  * representation into the charset returned by this function, to have
78  * data in the locale encoding.
79  *
80  * Return value: Return the character set used by the current locale.
81  *   It will never return NULL, but use "ASCII" as a fallback.
82  **/
83 const char *
84 stringprep_locale_charset (void)
85 {
86   const char *charset = getenv ("CHARSET");     /* flawfinder: ignore */
87
88   if (charset && *charset)
89     return charset;
90
91 # ifdef LOCALE_WORKS
92   charset = nl_langinfo (CODESET);
93
94   if (charset && *charset)
95     return charset;
96 # endif
97
98   return "ASCII";
99 }
100 #endif
101
102 /**
103  * stringprep_convert - encode string using new character set
104  * @str: input zero-terminated string.
105  * @to_codeset: name of destination character set.
106  * @from_codeset: name of origin character set, as used by @str.
107  *
108  * Convert the string from one character set to another using the
109  * system's iconv() function.
110  *
111  * Return value: Returns newly allocated zero-terminated string which
112  *   is @str transcoded into to_codeset.
113  **/
114 char *
115 stringprep_convert (const char *str,
116                     const char *to_codeset, const char *from_codeset)
117 {
118 #if HAVE_ICONV
119   return iconv_string (str, from_codeset, to_codeset);
120 #else
121   char *p;
122   fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
123            "convert data to UTF-8\n");
124   p = malloc (strlen (str) + 1);
125   if (!p)
126     return NULL;
127   return strcpy (p, str);
128 #endif
129 }
130
131 /**
132  * stringprep_locale_to_utf8 - convert locale encoded string to UTF-8
133  * @str: input zero terminated string.
134  *
135  * Convert string encoded in the locale's character set into UTF-8 by
136  * using stringprep_convert().
137  *
138  * Return value: Returns newly allocated zero-terminated string which
139  *   is @str transcoded into UTF-8.
140  **/
141 char *
142 stringprep_locale_to_utf8 (const char *str)
143 {
144   return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
145 }
146
147 /**
148  * stringprep_utf8_to_locale - encode UTF-8 string to locale encoding
149  * @str: input zero terminated string.
150  *
151  * Convert string encoded in UTF-8 into the locale's character set by
152  * using stringprep_convert().
153  *
154  * Return value: Returns newly allocated zero-terminated string which
155  *   is @str transcoded into the locale's character set.
156  **/
157 char *
158 stringprep_utf8_to_locale (const char *str)
159 {
160   return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");
161 }