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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Convert a string between UTF-8 and the locale's charset.
33 #include "share/alloc.h"
40 /* Thanks to Peter Harris <peter.harris@hummingbird.com> for this win32
47 static unsigned char *make_utf8_string(const wchar_t *unicode)
50 int index = 0, out_index = 0;
54 /* first calculate the size of the target string */
59 } else if(c < 0x0800) {
64 if(size+n < size) /* overflow check */
70 out = safe_malloc_add_2op_(size, /*+*/1);
79 out[out_index++] = (unsigned char)c;
80 } else if(c < 0x800) {
81 out[out_index++] = 0xc0 | (c >> 6);
82 out[out_index++] = 0x80 | (c & 0x3f);
84 out[out_index++] = 0xe0 | (c >> 12);
85 out[out_index++] = 0x80 | ((c >> 6) & 0x3f);
86 out[out_index++] = 0x80 | (c & 0x3f);
90 out[out_index] = 0x00;
95 static wchar_t *make_unicode_string(const unsigned char *utf8)
98 int index = 0, out_index = 0;
102 /* first calculate the size of the target string */
105 if((c & 0x80) == 0) {
107 } else if((c & 0xe0) == 0xe0) {
112 if(size + 1 == 0) /* overflow check */
118 if(size + 1 == 0) /* overflow check */
120 out = safe_malloc_mul_2op_(size+1, /*times*/sizeof(wchar_t));
128 if((c & 0x80) == 0) {
129 out[out_index++] = c;
130 } else if((c & 0xe0) == 0xe0) {
131 out[out_index] = (c & 0x1F) << 12;
133 out[out_index] |= (c & 0x3F) << 6;
135 out[out_index++] |= (c & 0x3F);
137 out[out_index] = (c & 0x3F) << 6;
139 out[out_index++] |= (c & 0x3F);
148 int utf8_encode(const char *from, char **to)
153 wchars = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from,
154 strlen(from), NULL, 0);
158 fprintf(stderr, "Unicode translation error %d\n", (int)GetLastError());
162 if(wchars < 0) /* underflow check */
165 unicode = safe_calloc_((size_t)wchars + 1, sizeof(unsigned short));
168 fprintf(stderr, "Out of memory processing string to UTF8\n");
172 err = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from,
173 strlen(from), unicode, wchars);
177 fprintf(stderr, "Unicode translation error %d\n", (int)GetLastError());
181 /* On NT-based windows systems, we could use WideCharToMultiByte(), but
182 * MS doesn't actually have a consistent API across win32.
184 *to = make_utf8_string(unicode);
190 int utf8_decode(const char *from, char **to)
195 /* On NT-based windows systems, we could use MultiByteToWideChar(CP_UTF8), but
196 * MS doesn't actually have a consistent API across win32.
198 unicode = make_unicode_string(from);
201 fprintf(stderr, "Out of memory processing string from UTF8 to UNICODE16\n");
205 chars = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
206 -1, NULL, 0, NULL, NULL);
208 if(chars < 0) /* underflow check */
213 fprintf(stderr, "Unicode translation error %d\n", (int)GetLastError());
218 *to = safe_calloc_((size_t)chars + 1, sizeof(unsigned char));
221 fprintf(stderr, "Out of memory processing string to local charset\n");
226 err = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
227 -1, *to, chars, NULL, NULL);
230 fprintf(stderr, "Unicode translation error %d\n", (int)GetLastError());
241 #else /* End win32. Rest is for real operating systems */
244 #ifdef HAVE_LANGINFO_CODESET
245 #include <langinfo.h>
248 #include "iconvert.h"
250 static const char *current_charset(void)
253 #ifdef HAVE_LANGINFO_CODESET
254 c = nl_langinfo(CODESET);
258 c = getenv("CHARSET");
260 return c? c : "US-ASCII";
263 static int convert_buffer(const char *fromcode, const char *tocode,
264 const char *from, size_t fromlen,
265 char **to, size_t *tolen)
270 ret = iconvert(fromcode, tocode, from, fromlen, to, tolen);
275 #ifndef HAVE_ICONV /* should be ifdef USE_CHARSET_CONVERT */
276 ret = charset_convert(fromcode, tocode, from, fromlen, to, tolen);
284 static int convert_string(const char *fromcode, const char *tocode,
285 const char *from, char **to, char replace)
291 fromlen = strlen(from);
292 ret = convert_buffer(fromcode, tocode, from, fromlen, to, 0);
298 s = safe_malloc_add_2op_(fromlen, /*+*/1);
309 int utf8_encode(const char *from, char **to)
311 return convert_string(current_charset(), "UTF-8", from, to, '#');
314 int utf8_decode(const char *from, char **to)
316 return convert_string("UTF-8", current_charset(), from, to, '?');