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
local_vsnprintf(char * str,size_t size,const char * fmt,va_list va)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 */
utf8_from_wchar(const wchar_t * wstr)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 */
wchar_from_utf8(const char * str)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 */
get_utf8_argv(int * argc,char *** argv)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 = LoadLibraryW(L"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 /* similar to CreateFileW but accepts UTF-8 encoded lpFileName */
CreateFile_utf8(const char * lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)153 HANDLE WINAPI CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
154 {
155 if (!flac_internal_get_utf8_filenames()) {
156 return CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
157 } else {
158 wchar_t *wname;
159 HANDLE handle = INVALID_HANDLE_VALUE;
160
161 if ((wname = wchar_from_utf8(lpFileName)) != NULL) {
162 handle = CreateFileW(wname, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
163 free(wname);
164 }
165
166 return handle;
167 }
168 }
169
170 /* return number of characters in the UTF-8 string */
strlen_utf8(const char * str)171 size_t strlen_utf8(const char *str)
172 {
173 size_t len;
174 len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); /* includes terminating null */
175 if (len != 0)
176 return len-1;
177 else
178 return strlen(str);
179 }
180
181 /* get the console width in characters */
win_get_console_width(void)182 int win_get_console_width(void)
183 {
184 int width = 80;
185 CONSOLE_SCREEN_BUFFER_INFO csbi;
186 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
187 if(hOut != INVALID_HANDLE_VALUE && hOut != NULL)
188 if (GetConsoleScreenBufferInfo(hOut, &csbi) != 0)
189 width = csbi.dwSize.X;
190 return width;
191 }
192
193 /* print functions */
194
wprint_console(FILE * stream,const wchar_t * text,size_t len)195 static int wprint_console(FILE *stream, const wchar_t *text, size_t len)
196 {
197 DWORD out;
198 int ret;
199
200 do {
201 if (stream == stdout) {
202 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
203 if (hOut == INVALID_HANDLE_VALUE || hOut == NULL || GetFileType(hOut) != FILE_TYPE_CHAR)
204 break;
205 if (WriteConsoleW(hOut, text, len, &out, NULL) == 0)
206 return -1;
207 return out;
208 }
209 if (stream == stderr) {
210 HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE);
211 if (hErr == INVALID_HANDLE_VALUE || hErr == NULL || GetFileType(hErr) != FILE_TYPE_CHAR)
212 break;
213 if (WriteConsoleW(hErr, text, len, &out, NULL) == 0)
214 return -1;
215 return out;
216 }
217 } while(0);
218
219 ret = fputws(text, stream);
220 if (ret < 0)
221 return ret;
222 return len;
223 }
224
printf_utf8(const char * format,...)225 int printf_utf8(const char *format, ...)
226 {
227 int ret;
228 va_list argptr;
229 va_start(argptr, format);
230
231 ret = vfprintf_utf8(stdout, format, argptr);
232
233 va_end(argptr);
234
235 return ret;
236 }
237
fprintf_utf8(FILE * stream,const char * format,...)238 int fprintf_utf8(FILE *stream, const char *format, ...)
239 {
240 int ret;
241 va_list argptr;
242 va_start(argptr, format);
243
244 ret = vfprintf_utf8(stream, format, argptr);
245
246 va_end(argptr);
247
248 return ret;
249 }
250
vfprintf_utf8(FILE * stream,const char * format,va_list argptr)251 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
252 {
253 char *utmp = NULL;
254 wchar_t *wout = NULL;
255 int ret = -1;
256
257 do {
258 if (!(utmp = (char *)malloc(UTF8_BUFFER_SIZE))) break;
259 if ((ret = local_vsnprintf(utmp, UTF8_BUFFER_SIZE, format, argptr)) <= 0) break;
260 if (!(wout = wchar_from_utf8(utmp))) {
261 ret = -1;
262 break;
263 }
264 ret = wprint_console(stream, wout, wcslen(wout));
265 } while(0);
266
267 free(utmp);
268 free(wout);
269
270 return ret;
271 }
272