1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2013-2022 Xiph.Org Foundation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
38 #include "share/win_utf8_io.h"
40 #define UTF8_BUFFER_SIZE 32768
42 #if !defined(WINAPI_FAMILY_PARTITION)
43 #define WINAPI_FAMILY_PARTITION(x) x
44 #define WINAPI_PARTITION_DESKTOP 1
47 static int local_vsnprintf(char *str, size_t size, const char *fmt, va_list va)
54 rc = vsnprintf_s(str, size, _TRUNCATE, fmt, va);
57 #elif defined __MINGW32__
58 rc = __mingw_vsnprintf(str, size, fmt, va);
60 rc = vsnprintf(str, size, fmt, va);
66 /* convert WCHAR stored Unicode string to UTF-8. Caller is responsible for freeing memory */
67 static char *utf8_from_wchar(const wchar_t *wstr)
74 if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0)
76 if ((utf8str = (char *)malloc(len)) == NULL)
78 if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) {
86 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
87 static wchar_t *wchar_from_utf8(const char *str)
94 if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0)
96 if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL)
98 if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) {
106 /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */
107 int get_utf8_argv(int *argc, char ***argv)
109 typedef int (__cdecl *wgetmainargs_t)(int*, wchar_t***, wchar_t***, int, int*);
110 wgetmainargs_t wgetmainargs;
118 if ((handle = LoadLibraryW(L"msvcrt.dll")) == NULL) return 1;
119 if ((wgetmainargs = (wgetmainargs_t)GetProcAddress(handle, "__wgetmainargs")) == NULL) {
124 /* when the 4th argument is 1, __wgetmainargs expands wildcards but also erroneously converts \\?\c:\path\to\file.flac to \\file.flac */
125 if (wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) {
129 if ((utf8argv = (char **)calloc(wargc, sizeof(char*))) == NULL) {
135 for (i=0; i<wargc; i++) {
136 if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) {
142 FreeLibrary(handle); /* do not free it when wargv or wenv are still in use */
148 for (i=0; i<wargc; i++)
156 /* similar to CreateFileW but accepts UTF-8 encoded lpFileName */
157 HANDLE WINAPI CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
160 HANDLE handle = INVALID_HANDLE_VALUE;
162 if ((wname = wchar_from_utf8(lpFileName)) != NULL) {
163 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
164 handle = CreateFileW(wname, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
165 #else // !WINAPI_PARTITION_DESKTOP
166 CREATEFILE2_EXTENDED_PARAMETERS params;
167 params.dwSize = sizeof(params);
168 params.dwFileAttributes = dwFlagsAndAttributes & 0xFFFF;
169 params.dwFileFlags = dwFlagsAndAttributes & 0xFFF00000;
170 params.dwSecurityQosFlags = dwFlagsAndAttributes & 0x000F0000;
171 params.lpSecurityAttributes = lpSecurityAttributes;
172 params.hTemplateFile = hTemplateFile;
173 handle = CreateFile2(wname, dwDesiredAccess, dwShareMode, dwCreationDisposition, ¶ms);
174 #endif // !WINAPI_PARTITION_DESKTOP
181 /* return number of characters in the UTF-8 string */
182 size_t strlen_utf8(const char *str)
185 len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); /* includes terminating null */
192 /* get the console width in characters */
193 int win_get_console_width(void)
196 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
197 CONSOLE_SCREEN_BUFFER_INFO csbi;
198 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
199 if(hOut != INVALID_HANDLE_VALUE && hOut != NULL)
200 if (GetConsoleScreenBufferInfo(hOut, &csbi) != 0)
201 width = csbi.dwSize.X;
202 #endif // WINAPI_PARTITION_DESKTOP
206 /* print functions */
208 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
209 static int wprint_console(FILE *stream, const wchar_t *text, size_t len)
215 if (stream == stdout) {
216 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
217 if (hOut == INVALID_HANDLE_VALUE || hOut == NULL || GetFileType(hOut) != FILE_TYPE_CHAR)
219 if (WriteConsoleW(hOut, text, len, &out, NULL) == 0)
223 if (stream == stderr) {
224 HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE);
225 if (hErr == INVALID_HANDLE_VALUE || hErr == NULL || GetFileType(hErr) != FILE_TYPE_CHAR)
227 if (WriteConsoleW(hErr, text, len, &out, NULL) == 0)
233 ret = fputws(text, stream);
238 #endif // WINAPI_PARTITION_DESKTOP
240 int printf_utf8(const char *format, ...)
244 va_start(argptr, format);
246 ret = vfprintf_utf8(stdout, format, argptr);
253 int fprintf_utf8(FILE *stream, const char *format, ...)
257 va_start(argptr, format);
259 ret = vfprintf_utf8(stream, format, argptr);
266 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
269 wchar_t *wout = NULL;
273 if (!(utmp = (char *)malloc(UTF8_BUFFER_SIZE))) break;
274 if ((ret = local_vsnprintf(utmp, UTF8_BUFFER_SIZE, format, argptr)) <= 0) break;
275 if (!(wout = wchar_from_utf8(utmp))) {
279 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
280 ret = wprint_console(stream, wout, wcslen(wout));
281 #else // !WINAPI_PARTITION_DESKTOP
282 OutputDebugStringW(wout);
284 #endif // !WINAPI_PARTITION_DESKTOP
295 FILE* fopen_utf8(const char *filename, const char *mode)
297 wchar_t *wname = NULL;
298 wchar_t *wmode = NULL;
302 if (!(wname = wchar_from_utf8(filename))) break;
303 if (!(wmode = wchar_from_utf8(mode))) break;
304 f = _wfopen(wname, wmode);
313 int stat64_utf8(const char *path, struct __stat64 *buffer)
318 if (!(wpath = wchar_from_utf8(path))) return -1;
319 ret = _wstat64(wpath, buffer);
325 int chmod_utf8(const char *filename, int pmode)
330 if (!(wname = wchar_from_utf8(filename))) return -1;
331 ret = _wchmod(wname, pmode);
337 int utime_utf8(const char *filename, struct utimbuf *times)
340 struct __utimbuf64 ut;
343 if (!(wname = wchar_from_utf8(filename))) return -1;
344 ut.actime = times->actime;
345 ut.modtime = times->modtime;
346 ret = _wutime64(wname, &ut);
352 int unlink_utf8(const char *filename)
357 if (!(wname = wchar_from_utf8(filename))) return -1;
358 ret = _wunlink(wname);
364 int rename_utf8(const char *oldname, const char *newname)
366 wchar_t *wold = NULL;
367 wchar_t *wnew = NULL;
371 if (!(wold = wchar_from_utf8(oldname))) break;
372 if (!(wnew = wchar_from_utf8(newname))) break;
373 ret = _wrename(wold, wnew);