1*600f14f4SXin Li /*
2*600f14f4SXin Li * Copyright (C) 2001 Peter Harris <[email protected]>
3*600f14f4SXin Li * Copyright (C) 2001 Edmund Grimley Evans <[email protected]>
4*600f14f4SXin Li *
5*600f14f4SXin Li * Buffer overflow checking added: Josh Coalson, 9/9/2007
6*600f14f4SXin Li *
7*600f14f4SXin Li * Win32 part rewritten: lvqcl, 2/2/2016
8*600f14f4SXin Li *
9*600f14f4SXin Li * This program is free software; you can redistribute it and/or modify
10*600f14f4SXin Li * it under the terms of the GNU General Public License as published by
11*600f14f4SXin Li * the Free Software Foundation; either version 2 of the License, or
12*600f14f4SXin Li * (at your option) any later version.
13*600f14f4SXin Li *
14*600f14f4SXin Li * This program is distributed in the hope that it will be useful,
15*600f14f4SXin Li * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*600f14f4SXin Li * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17*600f14f4SXin Li * GNU General Public License for more details.
18*600f14f4SXin Li *
19*600f14f4SXin Li * You should have received a copy of the GNU General Public License along
20*600f14f4SXin Li * with this program; if not, write to the Free Software Foundation, Inc.,
21*600f14f4SXin Li * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*600f14f4SXin Li */
23*600f14f4SXin Li
24*600f14f4SXin Li /*
25*600f14f4SXin Li * Convert a string between UTF-8 and the locale's charset.
26*600f14f4SXin Li */
27*600f14f4SXin Li
28*600f14f4SXin Li #ifdef HAVE_CONFIG_H
29*600f14f4SXin Li # include <config.h>
30*600f14f4SXin Li #endif
31*600f14f4SXin Li
32*600f14f4SXin Li #include <stdio.h>
33*600f14f4SXin Li #include <stdlib.h>
34*600f14f4SXin Li
35*600f14f4SXin Li #include "share/alloc.h"
36*600f14f4SXin Li #include "share/utf8.h"
37*600f14f4SXin Li
38*600f14f4SXin Li #ifdef _WIN32
39*600f14f4SXin Li
40*600f14f4SXin Li #include <windows.h>
41*600f14f4SXin Li
utf8_encode(const char * from,char ** to)42*600f14f4SXin Li int utf8_encode(const char *from, char **to)
43*600f14f4SXin Li {
44*600f14f4SXin Li wchar_t *unicode = NULL;
45*600f14f4SXin Li char *utf8 = NULL;
46*600f14f4SXin Li int ret = -1;
47*600f14f4SXin Li
48*600f14f4SXin Li do {
49*600f14f4SXin Li int len;
50*600f14f4SXin Li
51*600f14f4SXin Li len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, -1, NULL, 0);
52*600f14f4SXin Li if(len == 0) break;
53*600f14f4SXin Li unicode = (wchar_t*) safe_malloc_mul_2op_((size_t)len, sizeof(wchar_t));
54*600f14f4SXin Li if(unicode == NULL) break;
55*600f14f4SXin Li len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, -1, unicode, len);
56*600f14f4SXin Li if(len == 0) break;
57*600f14f4SXin Li
58*600f14f4SXin Li len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
59*600f14f4SXin Li if(len == 0) break;
60*600f14f4SXin Li utf8 = (char*) safe_malloc_mul_2op_((size_t)len, sizeof(char));
61*600f14f4SXin Li if(utf8 == NULL) break;
62*600f14f4SXin Li len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len, NULL, NULL);
63*600f14f4SXin Li if(len == 0) break;
64*600f14f4SXin Li
65*600f14f4SXin Li ret = 0;
66*600f14f4SXin Li
67*600f14f4SXin Li } while(0);
68*600f14f4SXin Li
69*600f14f4SXin Li free(unicode);
70*600f14f4SXin Li
71*600f14f4SXin Li if(ret == 0) {
72*600f14f4SXin Li *to = utf8;
73*600f14f4SXin Li } else {
74*600f14f4SXin Li free(utf8);
75*600f14f4SXin Li *to = NULL;
76*600f14f4SXin Li }
77*600f14f4SXin Li
78*600f14f4SXin Li return ret;
79*600f14f4SXin Li }
80*600f14f4SXin Li
utf8_decode(const char * from,char ** to)81*600f14f4SXin Li int utf8_decode(const char *from, char **to)
82*600f14f4SXin Li {
83*600f14f4SXin Li wchar_t *unicode = NULL;
84*600f14f4SXin Li char *acp = NULL;
85*600f14f4SXin Li int ret = -1;
86*600f14f4SXin Li
87*600f14f4SXin Li do {
88*600f14f4SXin Li int len;
89*600f14f4SXin Li
90*600f14f4SXin Li len = MultiByteToWideChar(CP_UTF8, 0, from, -1, NULL, 0);
91*600f14f4SXin Li if(len == 0) break;
92*600f14f4SXin Li unicode = (wchar_t*) safe_malloc_mul_2op_((size_t)len, sizeof(wchar_t));
93*600f14f4SXin Li if(unicode == NULL) break;
94*600f14f4SXin Li len = MultiByteToWideChar(CP_UTF8, 0, from, -1, unicode, len);
95*600f14f4SXin Li if(len == 0) break;
96*600f14f4SXin Li
97*600f14f4SXin Li len = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode, -1, NULL, 0, NULL, NULL);
98*600f14f4SXin Li if(len == 0) break;
99*600f14f4SXin Li acp = (char*) safe_malloc_mul_2op_((size_t)len, sizeof(char));
100*600f14f4SXin Li if(acp == NULL) break;
101*600f14f4SXin Li len = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode, -1, acp, len, NULL, NULL);
102*600f14f4SXin Li if(len == 0) break;
103*600f14f4SXin Li
104*600f14f4SXin Li ret = 0;
105*600f14f4SXin Li
106*600f14f4SXin Li } while(0);
107*600f14f4SXin Li
108*600f14f4SXin Li free(unicode);
109*600f14f4SXin Li
110*600f14f4SXin Li if(ret == 0) {
111*600f14f4SXin Li *to = acp;
112*600f14f4SXin Li } else {
113*600f14f4SXin Li free(acp);
114*600f14f4SXin Li *to = NULL;
115*600f14f4SXin Li }
116*600f14f4SXin Li
117*600f14f4SXin Li return ret;
118*600f14f4SXin Li }
119*600f14f4SXin Li
120*600f14f4SXin Li #else /* End win32. Rest is for real operating systems */
121*600f14f4SXin Li
122*600f14f4SXin Li
123*600f14f4SXin Li #ifdef HAVE_LANGINFO_CODESET
124*600f14f4SXin Li #include <langinfo.h>
125*600f14f4SXin Li #endif
126*600f14f4SXin Li
127*600f14f4SXin Li #include <string.h>
128*600f14f4SXin Li
129*600f14f4SXin Li #include "share/safe_str.h"
130*600f14f4SXin Li #include "iconvert.h"
131*600f14f4SXin Li #include "charset.h"
132*600f14f4SXin Li
current_charset(void)133*600f14f4SXin Li static const char *current_charset(void)
134*600f14f4SXin Li {
135*600f14f4SXin Li const char *c = 0;
136*600f14f4SXin Li #ifdef HAVE_LANGINFO_CODESET
137*600f14f4SXin Li c = nl_langinfo(CODESET);
138*600f14f4SXin Li #endif
139*600f14f4SXin Li
140*600f14f4SXin Li if (!c)
141*600f14f4SXin Li c = getenv("CHARSET");
142*600f14f4SXin Li
143*600f14f4SXin Li return c? c : "US-ASCII";
144*600f14f4SXin Li }
145*600f14f4SXin Li
convert_buffer(const char * fromcode,const char * tocode,const char * from,size_t fromlen,char ** to,size_t * tolen)146*600f14f4SXin Li static int convert_buffer(const char *fromcode, const char *tocode,
147*600f14f4SXin Li const char *from, size_t fromlen,
148*600f14f4SXin Li char **to, size_t *tolen)
149*600f14f4SXin Li {
150*600f14f4SXin Li int ret = -1;
151*600f14f4SXin Li
152*600f14f4SXin Li #ifdef HAVE_ICONV
153*600f14f4SXin Li ret = iconvert(fromcode, tocode, from, fromlen, to, tolen);
154*600f14f4SXin Li if (ret != -1)
155*600f14f4SXin Li return ret;
156*600f14f4SXin Li #endif
157*600f14f4SXin Li
158*600f14f4SXin Li #ifndef HAVE_ICONV /* should be ifdef USE_CHARSET_CONVERT */
159*600f14f4SXin Li ret = charset_convert(fromcode, tocode, from, fromlen, to, tolen);
160*600f14f4SXin Li if (ret != -1)
161*600f14f4SXin Li return ret;
162*600f14f4SXin Li #endif
163*600f14f4SXin Li
164*600f14f4SXin Li return ret;
165*600f14f4SXin Li }
166*600f14f4SXin Li
convert_string(const char * fromcode,const char * tocode,const char * from,char ** to,char replace)167*600f14f4SXin Li static int convert_string(const char *fromcode, const char *tocode,
168*600f14f4SXin Li const char *from, char **to, char replace)
169*600f14f4SXin Li {
170*600f14f4SXin Li int ret;
171*600f14f4SXin Li size_t fromlen;
172*600f14f4SXin Li char *s;
173*600f14f4SXin Li
174*600f14f4SXin Li fromlen = strlen(from);
175*600f14f4SXin Li ret = convert_buffer(fromcode, tocode, from, fromlen, to, 0);
176*600f14f4SXin Li if (ret == -2)
177*600f14f4SXin Li return -1;
178*600f14f4SXin Li if (ret != -1)
179*600f14f4SXin Li return ret;
180*600f14f4SXin Li
181*600f14f4SXin Li s = safe_malloc_add_2op_(fromlen, /*+*/1);
182*600f14f4SXin Li if (!s)
183*600f14f4SXin Li return -1;
184*600f14f4SXin Li snprintf(s, fromlen + 1, "%s", from);
185*600f14f4SXin Li *to = s;
186*600f14f4SXin Li for (; *s; s++)
187*600f14f4SXin Li if (*s & ~0x7f)
188*600f14f4SXin Li *s = replace;
189*600f14f4SXin Li return 3;
190*600f14f4SXin Li }
191*600f14f4SXin Li
utf8_encode(const char * from,char ** to)192*600f14f4SXin Li int utf8_encode(const char *from, char **to)
193*600f14f4SXin Li {
194*600f14f4SXin Li return convert_string(current_charset(), "UTF-8", from, to, '#');
195*600f14f4SXin Li }
196*600f14f4SXin Li
utf8_decode(const char * from,char ** to)197*600f14f4SXin Li int utf8_decode(const char *from, char **to)
198*600f14f4SXin Li {
199*600f14f4SXin Li return convert_string("UTF-8", current_charset(), from, to, '?');
200*600f14f4SXin Li }
201*600f14f4SXin Li
202*600f14f4SXin Li #endif
203