2 * Copyright (C) 2001 Peter Harris <peter.harris@hummingbird.com>
3 * Copyright (C) 2001 Edmund Grimley Evans <edmundo@rano.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Convert a string between UTF-8 and the locale's charset.
28 #include "identify_encoding.h"
31 /* Thanks to Peter Harris <peter.harris@hummingbird.com> for this win32
38 static unsigned char *make_utf8_string(const wchar_t *unicode)
40 int size = 0, index = 0, out_index = 0;
44 /* first calculate the size of the target string */
61 out = (unsigned char *)malloc(size + 1);
71 out[out_index++] = (unsigned char)c;
74 out[out_index++] = 0xc0 | (c >> 6);
75 out[out_index++] = 0x80 | (c & 0x3f);
77 out[out_index++] = 0xe0 | (c >> 12);
78 out[out_index++] = 0x80 | ((c >> 6) & 0x3f);
79 out[out_index++] = 0x80 | (c & 0x3f);
83 out[out_index] = 0x00;
88 static wchar_t *make_unicode_string(const unsigned char *utf8)
90 int size = 0, index = 0, out_index = 0;
94 /* first calculate the size of the target string */
101 }else if((c & 0xe0) == 0xe0)
111 out = (wchar_t *)malloc((size + 1) * sizeof(wchar_t));
121 out[out_index++] = c;
122 }else if ((c & 0xe0) == 0xe0)
124 out[out_index] = (c & 0x1F) << 12;
126 out[out_index] |= (c & 0x3F) << 6;
128 out[out_index++] |= (c & 0x3F);
130 out[out_index] = (c & 0x3F) << 6;
132 out[out_index++] |= (c & 0x3F);
141 int utf8_encode(const char *from, char **to)
147 wchars = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from,
148 strlen(from), NULL, 0);
152 // fprintf(stderr, "Unicode translation error %d\n", GetLastError());
156 unicode = (wchar_t *)calloc(wchars + 1, sizeof(unsigned short));
159 // fprintf(stderr, "Out of memory processing string to UTF8\n");
163 err = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, strlen(from), unicode, wchars);
167 // fprintf(stderr, "Unicode translation error %d\n", GetLastError());
171 /* On NT-based windows systems, we could use WideCharToMultiByte(), but
172 * MS doesn't actually have a consistent API across win32.
174 *to = (char *)make_utf8_string(unicode);
180 int utf8_decode(const char *from, char **to)
186 identify_encoding_t *cdt;
187 enum identify_encoding_order order;
189 cdt = identify_encoding_open(order);
190 cds = identify_encoding(cdt,(char *)from);
191 if (strcmp(cds,"UTF-8")!=0)
193 identify_encoding_close(cdt);
196 identify_encoding_close(cdt);
199 /* On NT-based windows systems, we could use MultiByteToWideChar(CP_UTF8), but
200 * MS doesn't actually have a consistent API across win32.
202 unicode = (wchar_t *)make_unicode_string((const unsigned char *)from);
205 // fprintf(stderr, "Out of memory processing string from UTF8 to UNICODE16\n");
209 chars = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode, -1, NULL, 0, NULL, NULL);
213 // fprintf(stderr, "Unicode translation error %d\n", GetLastError());
218 *to = (char *)calloc(chars + 1, sizeof(unsigned char));
221 // fprintf(stderr, "Out of memory processing string to local charset\n");
226 err = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode, -1, *to, chars, NULL, NULL);
229 // fprintf(stderr, "Unicode translation error %d\n", GetLastError());
240 #else /* End win32. Rest is for real operating systems */
243 #ifdef HAVE_LANGINFO_CODESET
244 #include <langinfo.h>
247 int iconvert(const char *fromcode, const char *tocode,
248 const char *from, size_t fromlen,
249 char **to, size_t *tolen);
251 static char *current_charset = 0; /* means "US-ASCII" */
253 void convert_set_charset(const char *charset)
257 charset = getenv("CHARSET");
259 #ifdef HAVE_LANGINFO_CODESET
261 charset = nl_langinfo(CODESET);
264 free(current_charset);
266 if (charset && *charset)
267 current_charset = strdup(charset);
270 static int convert_buffer(const char *fromcode, const char *tocode,
271 const char *from, size_t fromlen,
272 char **to, size_t *tolen)
277 ret = iconvert(fromcode, tocode, from, fromlen, to, tolen);
282 #ifndef HAVE_ICONV /* should be ifdef USE_CHARSET_CONVERT */
283 ret = charset_convert(fromcode, tocode, from, fromlen, to, tolen);
291 static int convert_string(const char *fromcode, const char *tocode,
292 const char *from, char **to, char replace)
298 fromlen = strlen(from);
299 ret = convert_buffer(fromcode, tocode, from, fromlen, to, 0);
305 s = malloc(fromlen + 1);
316 int utf8_encode(const char *from, char **to)
320 if (!current_charset)
321 convert_set_charset(0);
322 charset = current_charset ? current_charset : "US-ASCII";
323 return convert_string(charset, "UTF-8", from, to, '#');
326 int utf8_decode(const char *from, char **to)
337 if (!current_charset)
338 convert_set_charset(0);
339 charset = current_charset ? current_charset : "US-ASCII";
340 return convert_string("UTF-8", charset, from, to, '?');