2 * Copyright (C) 2001 Peter Harris <peter.harris@hummingbird.com>
3 * Copyright (C) 2001 Edmund Grimley Evans <edmundo@rano.org>
5 * Buffer overflow checking added: Josh Coalson, 9/9/2007
7 * Win32 part rewritten: lvqcl, 2/2/2016
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Convert a string between UTF-8 and the locale's charset.
34 #include "share/alloc.h"
35 #include "share/utf8.h"
41 int utf8_encode(const char *from, char **to)
43 wchar_t *unicode = NULL;
50 len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, -1, NULL, 0);
53 unicode = (wchar_t*) safe_malloc_mul_2op_((size_t)len, sizeof(wchar_t));
54 if(unicode == NULL) break;
56 len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, -1, unicode, len);
59 len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
62 utf8 = (char*) safe_malloc_mul_2op_((size_t)len, sizeof(char));
63 if(utf8 == NULL) break;
65 len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len, NULL, NULL);
84 int utf8_decode(const char *from, char **to)
86 wchar_t *unicode = NULL;
93 len = MultiByteToWideChar(CP_UTF8, 0, from, -1, NULL, 0);
96 unicode = (wchar_t*) safe_malloc_mul_2op_((size_t)len, sizeof(wchar_t));
97 if(unicode == NULL) break;
99 len = MultiByteToWideChar(CP_UTF8, 0, from, -1, unicode, len);
102 len = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode, -1, NULL, 0, NULL, NULL);
105 acp = (char*) safe_malloc_mul_2op_((size_t)len, sizeof(char));
106 if(acp == NULL) break;
108 len = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode, -1, acp, len, NULL, NULL);
127 #else /* End win32. Rest is for real operating systems */
130 #ifdef HAVE_LANGINFO_CODESET
131 #include <langinfo.h>
136 #include "share/safe_str.h"
137 #include "iconvert.h"
140 static const char *current_charset(void)
143 #ifdef HAVE_LANGINFO_CODESET
144 c = nl_langinfo(CODESET);
148 c = getenv("CHARSET");
150 return c? c : "US-ASCII";
153 static int convert_buffer(const char *fromcode, const char *tocode,
154 const char *from, size_t fromlen,
155 char **to, size_t *tolen)
160 ret = iconvert(fromcode, tocode, from, fromlen, to, tolen);
165 #ifndef HAVE_ICONV /* should be ifdef USE_CHARSET_CONVERT */
166 ret = charset_convert(fromcode, tocode, from, fromlen, to, tolen);
174 static int convert_string(const char *fromcode, const char *tocode,
175 const char *from, char **to, char replace)
181 fromlen = strlen(from);
182 ret = convert_buffer(fromcode, tocode, from, fromlen, to, 0);
188 s = safe_malloc_add_2op_(fromlen, /*+*/1);
191 safe_strncpy(s, from, fromlen + 1);
199 int utf8_encode(const char *from, char **to)
201 return convert_string(current_charset(), "UTF-8", from, to, '#');
204 int utf8_decode(const char *from, char **to)
206 return convert_string("UTF-8", current_charset(), from, to, '?');