ce188d4d |
1 | /* libFLAC - Free Lossless Audio Codec library |
2 | * Copyright (C) 2013-2016 Xiph.Org Foundation |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * |
8 | * - Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * |
15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
16 | * contributors may be used to endorse or promote products derived from |
17 | * this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #ifdef HAVE_CONFIG_H |
33 | # include <config.h> |
34 | #endif |
35 | |
36 | #include <windows.h> |
37 | #include "share/win_utf8_io.h" |
38 | #include "share/windows_unicode_filenames.h" |
39 | |
40 | #define UTF8_BUFFER_SIZE 32768 |
41 | |
42 | static int local_vsnprintf(char *str, size_t size, const char *fmt, va_list va) |
43 | { |
44 | int rc; |
45 | |
46 | #if defined _MSC_VER |
47 | if (size == 0) |
48 | return 1024; |
49 | rc = vsnprintf_s(str, size, _TRUNCATE, fmt, va); |
50 | if (rc < 0) |
51 | rc = size - 1; |
52 | #elif defined __MINGW32__ |
53 | rc = __mingw_vsnprintf(str, size, fmt, va); |
54 | #else |
55 | rc = vsnprintf(str, size, fmt, va); |
56 | #endif |
57 | |
58 | return rc; |
59 | } |
60 | |
61 | /* convert WCHAR stored Unicode string to UTF-8. Caller is responsible for freeing memory */ |
62 | static char *utf8_from_wchar(const wchar_t *wstr) |
63 | { |
64 | char *utf8str; |
65 | int len; |
66 | |
67 | if (!wstr) |
68 | return NULL; |
69 | if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0) |
70 | return NULL; |
71 | if ((utf8str = (char *)malloc(len)) == NULL) |
72 | return NULL; |
73 | if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) { |
74 | free(utf8str); |
75 | utf8str = NULL; |
76 | } |
77 | |
78 | return utf8str; |
79 | } |
80 | |
81 | /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */ |
82 | static wchar_t *wchar_from_utf8(const char *str) |
83 | { |
84 | wchar_t *widestr; |
85 | int len; |
86 | |
87 | if (!str) |
88 | return NULL; |
89 | if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0) |
90 | return NULL; |
91 | if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL) |
92 | return NULL; |
93 | if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) { |
94 | free(widestr); |
95 | widestr = NULL; |
96 | } |
97 | |
98 | return widestr; |
99 | } |
100 | |
101 | /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */ |
102 | int get_utf8_argv(int *argc, char ***argv) |
103 | { |
104 | typedef int (__cdecl *wgetmainargs_t)(int*, wchar_t***, wchar_t***, int, int*); |
105 | wgetmainargs_t wgetmainargs; |
106 | HMODULE handle; |
107 | int wargc; |
108 | wchar_t **wargv; |
109 | wchar_t **wenv; |
110 | char **utf8argv; |
111 | int ret, i; |
112 | |
113 | if ((handle = LoadLibrary("msvcrt.dll")) == NULL) return 1; |
114 | if ((wgetmainargs = (wgetmainargs_t)GetProcAddress(handle, "__wgetmainargs")) == NULL) { |
115 | FreeLibrary(handle); |
116 | return 1; |
117 | } |
118 | i = 0; |
119 | /* when the 4th argument is 1, __wgetmainargs expands wildcards but also erroneously converts \\?\c:\path\to\file.flac to \\file.flac */ |
120 | if (wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) { |
121 | FreeLibrary(handle); |
122 | return 1; |
123 | } |
124 | if ((utf8argv = (char **)calloc(wargc, sizeof(char*))) == NULL) { |
125 | FreeLibrary(handle); |
126 | return 1; |
127 | } |
128 | |
129 | ret = 0; |
130 | for (i=0; i<wargc; i++) { |
131 | if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) { |
132 | ret = 1; |
133 | break; |
134 | } |
135 | } |
136 | |
137 | FreeLibrary(handle); /* do not free it when wargv or wenv are still in use */ |
138 | |
139 | if (ret == 0) { |
140 | flac_set_utf8_filenames(true); |
141 | *argc = wargc; |
142 | *argv = utf8argv; |
143 | } else { |
144 | for (i=0; i<wargc; i++) |
145 | free(utf8argv[i]); |
146 | free(utf8argv); |
147 | } |
148 | |
149 | return ret; |
150 | } |
151 | |
152 | /* return number of characters in the UTF-8 string */ |
153 | size_t strlen_utf8(const char *str) |
154 | { |
155 | size_t len; |
156 | len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); /* includes terminating null */ |
157 | if (len != 0) |
158 | return len-1; |
159 | else |
160 | return strlen(str); |
161 | } |
162 | |
163 | /* get the console width in characters */ |
164 | int win_get_console_width(void) |
165 | { |
166 | int width = 80; |
167 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
168 | HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); |
169 | if(hOut != INVALID_HANDLE_VALUE && hOut != NULL) |
170 | if (GetConsoleScreenBufferInfo(hOut, &csbi) != 0) |
171 | width = csbi.dwSize.X; |
172 | return width; |
173 | } |
174 | |
175 | /* print functions */ |
176 | |
177 | static int wprint_console(FILE *stream, const wchar_t *text, size_t len) |
178 | { |
179 | DWORD out; |
180 | int ret; |
181 | |
182 | do { |
183 | if (stream == stdout) { |
184 | HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); |
185 | if (hOut == INVALID_HANDLE_VALUE || hOut == NULL || GetFileType(hOut) != FILE_TYPE_CHAR) |
186 | break; |
187 | if (WriteConsoleW(hOut, text, len, &out, NULL) == 0) |
188 | return -1; |
189 | return out; |
190 | } |
191 | if (stream == stderr) { |
192 | HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE); |
193 | if (hErr == INVALID_HANDLE_VALUE || hErr == NULL || GetFileType(hErr) != FILE_TYPE_CHAR) |
194 | break; |
195 | if (WriteConsoleW(hErr, text, len, &out, NULL) == 0) |
196 | return -1; |
197 | return out; |
198 | } |
199 | } while(0); |
200 | |
201 | ret = fputws(text, stream); |
202 | if (ret < 0) |
203 | return ret; |
204 | return len; |
205 | } |
206 | |
207 | int printf_utf8(const char *format, ...) |
208 | { |
209 | int ret; |
210 | va_list argptr; |
211 | va_start(argptr, format); |
212 | |
213 | ret = vfprintf_utf8(stdout, format, argptr); |
214 | |
215 | va_end(argptr); |
216 | |
217 | return ret; |
218 | } |
219 | |
220 | int fprintf_utf8(FILE *stream, const char *format, ...) |
221 | { |
222 | int ret; |
223 | va_list argptr; |
224 | va_start(argptr, format); |
225 | |
226 | ret = vfprintf_utf8(stream, format, argptr); |
227 | |
228 | va_end(argptr); |
229 | |
230 | return ret; |
231 | } |
232 | |
233 | int vfprintf_utf8(FILE *stream, const char *format, va_list argptr) |
234 | { |
235 | char *utmp = NULL; |
236 | wchar_t *wout = NULL; |
237 | int ret = -1; |
238 | |
239 | do { |
240 | if (!(utmp = (char *)malloc(UTF8_BUFFER_SIZE))) break; |
241 | if ((ret = local_vsnprintf(utmp, UTF8_BUFFER_SIZE, format, argptr)) <= 0) break; |
242 | if (!(wout = wchar_from_utf8(utmp))) { |
243 | ret = -1; |
244 | break; |
245 | } |
246 | ret = wprint_console(stream, wout, wcslen(wout)); |
247 | } while(0); |
248 | |
249 | free(utmp); |
250 | free(wout); |
251 | |
252 | return ret; |
253 | } |