1 /*
2  * Copyright (C) 2001 Peter Harris <peter.harris@hummingbird.com>
3  * Copyright (C) 2001 Edmund Grimley Evans <edmundo@rano.org>
4  *
5  * Buffer overflow checking added: Josh Coalson, 9/9/2007
6  *
7  * Win32 part rewritten: lvqcl, 2/2/2016
8  *
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.
13  *
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.
18  *
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.
22  */
23 
24 /*
25  * Convert a string between UTF-8 and the locale's charset.
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 
35 #include "share/alloc.h"
36 #include "share/utf8.h"
37 
38 #ifdef _WIN32
39 
40 #include <windows.h>
41 
utf8_encode(const char * from,char ** to)42 int utf8_encode(const char *from, char **to)
43 {
44 	wchar_t *unicode = NULL;
45 	char *utf8 = NULL;
46 	int ret = -1;
47 
48 	do {
49 		int len;
50 
51 		len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, -1, NULL, 0);
52 		if(len == 0) break;
53 		unicode = (wchar_t*) safe_malloc_mul_2op_((size_t)len, sizeof(wchar_t));
54 		if(unicode == NULL) break;
55 		len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, -1, unicode, len);
56 		if(len == 0) break;
57 
58 		len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
59 		if(len == 0) break;
60 		utf8 = (char*) safe_malloc_mul_2op_((size_t)len, sizeof(char));
61 		if(utf8 == NULL) break;
62 		len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len, NULL, NULL);
63 		if(len == 0) break;
64 
65 		ret = 0;
66 
67 	} while(0);
68 
69 	free(unicode);
70 
71 	if(ret == 0) {
72 		*to = utf8;
73 	} else {
74 		free(utf8);
75 		*to = NULL;
76 	}
77 
78 	return ret;
79 }
80 
utf8_decode(const char * from,char ** to)81 int utf8_decode(const char *from, char **to)
82 {
83 	wchar_t *unicode = NULL;
84 	char *acp = NULL;
85 	int ret = -1;
86 
87 	do {
88 		int len;
89 
90 		len = MultiByteToWideChar(CP_UTF8, 0, from, -1, NULL, 0);
91 		if(len == 0) break;
92 		unicode = (wchar_t*) safe_malloc_mul_2op_((size_t)len, sizeof(wchar_t));
93 		if(unicode == NULL) break;
94 		len = MultiByteToWideChar(CP_UTF8, 0, from, -1, unicode, len);
95 		if(len == 0) break;
96 
97 		len = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode, -1, NULL, 0, NULL, NULL);
98 		if(len == 0) break;
99 		acp = (char*) safe_malloc_mul_2op_((size_t)len, sizeof(char));
100 		if(acp == NULL) break;
101 		len = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode, -1, acp, len, NULL, NULL);
102 		if(len == 0) break;
103 
104 		ret = 0;
105 
106 	} while(0);
107 
108 	free(unicode);
109 
110 	if(ret == 0) {
111 		*to = acp;
112 	} else {
113 		free(acp);
114 		*to = NULL;
115 	}
116 
117 	return ret;
118 }
119 
120 #else /* End win32. Rest is for real operating systems */
121 
122 
123 #ifdef HAVE_LANGINFO_CODESET
124 #include <langinfo.h>
125 #endif
126 
127 #include <string.h>
128 
129 #include "share/safe_str.h"
130 #include "iconvert.h"
131 #include "charset.h"
132 
current_charset(void)133 static const char *current_charset(void)
134 {
135   const char *c = 0;
136 #ifdef HAVE_LANGINFO_CODESET
137   c = nl_langinfo(CODESET);
138 #endif
139 
140   if (!c)
141     c = getenv("CHARSET");
142 
143   return c? c : "US-ASCII";
144 }
145 
convert_buffer(const char * fromcode,const char * tocode,const char * from,size_t fromlen,char ** to,size_t * tolen)146 static int convert_buffer(const char *fromcode, const char *tocode,
147 			  const char *from, size_t fromlen,
148 			  char **to, size_t *tolen)
149 {
150   int ret = -1;
151 
152 #ifdef HAVE_ICONV
153   ret = iconvert(fromcode, tocode, from, fromlen, to, tolen);
154   if (ret != -1)
155     return ret;
156 #endif
157 
158 #ifndef HAVE_ICONV /* should be ifdef USE_CHARSET_CONVERT */
159   ret = charset_convert(fromcode, tocode, from, fromlen, to, tolen);
160   if (ret != -1)
161     return ret;
162 #endif
163 
164   return ret;
165 }
166 
convert_string(const char * fromcode,const char * tocode,const char * from,char ** to,char replace)167 static int convert_string(const char *fromcode, const char *tocode,
168 			  const char *from, char **to, char replace)
169 {
170   int ret;
171   size_t fromlen;
172   char *s;
173 
174   fromlen = strlen(from);
175   ret = convert_buffer(fromcode, tocode, from, fromlen, to, 0);
176   if (ret == -2)
177     return -1;
178   if (ret != -1)
179     return ret;
180 
181   s = safe_malloc_add_2op_(fromlen, /*+*/1);
182   if (!s)
183     return -1;
184   snprintf(s, fromlen + 1, "%s", from);
185   *to = s;
186   for (; *s; s++)
187     if (*s & ~0x7f)
188       *s = replace;
189   return 3;
190 }
191 
utf8_encode(const char * from,char ** to)192 int utf8_encode(const char *from, char **to)
193 {
194   return convert_string(current_charset(), "UTF-8", from, to, '#');
195 }
196 
utf8_decode(const char * from,char ** to)197 int utf8_decode(const char *from, char **to)
198 {
199   return convert_string("UTF-8", current_charset(), from, to, '?');
200 }
201 
202 #endif
203