libidn 1.41
toutf8.c
Go to the documentation of this file.
1/* toutf8.c --- Convert strings from system locale into UTF-8.
2 Copyright (C) 2002-2022 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 either:
8
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version.
12
13 or
14
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version.
18
19 or both in parallel, as here.
20
21 GNU Libidn is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
25
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <https://www.gnu.org/licenses/>. */
29
30#ifdef HAVE_CONFIG_H
31# include "config.h"
32#endif
33
34/* Get prototypes. */
35#include "stringprep.h"
36
37/* Get fprintf. */
38#include <stdio.h>
39
40/* Get getenv. */
41#include <stdlib.h>
42
43/* Get strlen. */
44#include <string.h>
45
46/* Get iconv_string. */
47#include "striconv.h"
48
49#ifdef _LIBC
50# define HAVE_ICONV 1
51# define HAVE_LOCALE_H 1
52# define HAVE_LANGINFO_CODESET 1
53#endif
54
55#include <locale.h>
56
57#ifdef HAVE_LANGINFO_CODESET
58# include <langinfo.h>
59#endif
60
61#ifdef _LIBC
62# define stringprep_locale_charset() nl_langinfo (CODESET)
63#else
84const char *
86{
87 const char *charset = getenv ("CHARSET"); /* flawfinder: ignore */
88
89 if (charset && *charset)
90 return charset;
91
92# ifdef HAVE_LANGINFO_CODESET
93 charset = nl_langinfo (CODESET);
94
95 if (charset && *charset)
96 return charset;
97# endif
98
99 return "ASCII";
100}
101#endif
102
115char *
116stringprep_convert (const char *str,
117 const char *to_codeset, const char *from_codeset)
118{
119#if HAVE_ICONV
120 return str_iconv (str, from_codeset, to_codeset);
121#else
122 char *p;
123 fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
124 "convert data to UTF-8\n");
125 p = malloc (strlen (str) + 1);
126 if (!p)
127 return NULL;
128 return strcpy (p, str);
129#endif
130}
131
142char *
144{
145 return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
146}
147
158char *
160{
161 return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");
162}
char * stringprep_convert(const char *str, const char *to_codeset, const char *from_codeset)
Definition: toutf8.c:116
const char * stringprep_locale_charset(void)
Definition: toutf8.c:85
char * stringprep_locale_to_utf8(const char *str)
Definition: toutf8.c:143
char * stringprep_utf8_to_locale(const char *str)
Definition: toutf8.c:159