| 1 |
#ifdef _WIN32 |
| 2 |
/* libFLAC - Free Lossless Audio Codec library |
| 3 |
* Copyright (C) 2013-2025 Xiph.Org Foundation |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* - Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* - Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 |
* contributors may be used to endorse or promote products derived from |
| 18 |
* this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#ifdef HAVE_CONFIG_H |
| 34 |
# include <config.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include <io.h> |
| 38 |
#include <windows.h> |
| 39 |
#include "flac_share_win_utf8_io.h" |
| 40 |
|
| 41 |
#define UTF8_BUFFER_SIZE 32768 |
| 42 |
|
| 43 |
/* detect whether it is Windows APP (UWP) or standard Win32 envionment */ |
| 44 |
#ifdef WINAPI_FAMILY_PARTITION |
| 45 |
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 46 |
#define FLAC_WINDOWS_APP 1 |
| 47 |
#else |
| 48 |
#define FLAC_WINDOWS_APP 0 |
| 49 |
#endif |
| 50 |
#else |
| 51 |
#define FLAC_WINDOWS_APP 0 |
| 52 |
#endif |
| 53 |
|
| 54 |
static int local_vsnprintf(char *str, size_t size, const char *fmt, va_list va) |
| 55 |
{ |
| 56 |
int rc; |
| 57 |
|
| 58 |
#if defined _MSC_VER |
| 59 |
if (size == 0) |
| 60 |
return 1024; |
| 61 |
rc = vsnprintf_s(str, size, _TRUNCATE, fmt, va); |
| 62 |
if (rc < 0) |
| 63 |
rc = size - 1; |
| 64 |
#elif defined __MINGW32__ |
| 65 |
rc = __mingw_vsnprintf(str, size, fmt, va); |
| 66 |
#else |
| 67 |
rc = vsnprintf(str, size, fmt, va); |
| 68 |
#endif |
| 69 |
|
| 70 |
return rc; |
| 71 |
} |
| 72 |
|
| 73 |
/* convert WCHAR stored Unicode string to UTF-8. Caller is responsible for freeing memory */ |
| 74 |
static char *utf8_from_wchar(const wchar_t *wstr) |
| 75 |
{ |
| 76 |
char *utf8str; |
| 77 |
int len; |
| 78 |
|
| 79 |
if (!wstr) |
| 80 |
return NULL; |
| 81 |
if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0) |
| 82 |
return NULL; |
| 83 |
if ((utf8str = (char *)malloc(len)) == NULL) |
| 84 |
return NULL; |
| 85 |
if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) { |
| 86 |
free(utf8str); |
| 87 |
utf8str = NULL; |
| 88 |
} |
| 89 |
|
| 90 |
return utf8str; |
| 91 |
} |
| 92 |
|
| 93 |
/* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */ |
| 94 |
static wchar_t *wchar_from_utf8(const char *str) |
| 95 |
{ |
| 96 |
wchar_t *widestr; |
| 97 |
int len; |
| 98 |
|
| 99 |
if (!str) |
| 100 |
return NULL; |
| 101 |
if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0) |
| 102 |
return NULL; |
| 103 |
if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL) |
| 104 |
return NULL; |
| 105 |
if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) { |
| 106 |
free(widestr); |
| 107 |
widestr = NULL; |
| 108 |
} |
| 109 |
|
| 110 |
return widestr; |
| 111 |
} |
| 112 |
|
| 113 |
/* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */ |
| 114 |
int get_utf8_argv(int *argc, char ***argv) |
| 115 |
{ |
| 116 |
#if !FLAC_WINDOWS_APP |
| 117 |
typedef int (__cdecl *wgetmainargs_t)(int*, wchar_t***, wchar_t***, int, int*); |
| 118 |
wgetmainargs_t wgetmainargs; |
| 119 |
HMODULE handle; |
| 120 |
#endif // !FLAC_WINDOWS_APP |
| 121 |
int wargc; |
| 122 |
wchar_t **wargv; |
| 123 |
wchar_t **wenv; |
| 124 |
char **utf8argv; |
| 125 |
int ret, i; |
| 126 |
|
| 127 |
#if FLAC_WINDOWS_APP |
| 128 |
wargc = __argc; |
| 129 |
wargv = __wargv; |
| 130 |
wenv = _wenviron; |
| 131 |
#else // !FLAC_WINDOWS_APP |
| 132 |
if ((handle = LoadLibraryW(L"msvcrt.dll")) == NULL) return 1; |
| 133 |
if ((wgetmainargs = (wgetmainargs_t)GetProcAddress(handle, "__wgetmainargs")) == NULL) { |
| 134 |
FreeLibrary(handle); |
| 135 |
return 1; |
| 136 |
} |
| 137 |
i = 0; |
| 138 |
/* when the 4th argument is 1, __wgetmainargs expands wildcards but also erroneously converts \\?\c:\path\to\file.flac to \\file.flac */ |
| 139 |
if (wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) { |
| 140 |
FreeLibrary(handle); |
| 141 |
return 1; |
| 142 |
} |
| 143 |
#endif // !FLAC_WINDOWS_APP |
| 144 |
if ((utf8argv = (char **)calloc(wargc, sizeof(char*))) == NULL) { |
| 145 |
#if !FLAC_WINDOWS_APP |
| 146 |
FreeLibrary(handle); |
| 147 |
#endif // !FLAC_WINDOWS_APP |
| 148 |
return 1; |
| 149 |
} |
| 150 |
|
| 151 |
ret = 0; |
| 152 |
for (i=0; i<wargc; i++) { |
| 153 |
if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) { |
| 154 |
ret = 1; |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
#if !FLAC_WINDOWS_APP |
| 160 |
FreeLibrary(handle); /* do not free it when wargv or wenv are still in use */ |
| 161 |
#endif // !FLAC_WINDOWS_APP |
| 162 |
|
| 163 |
if (ret == 0) { |
| 164 |
*argc = wargc; |
| 165 |
*argv = utf8argv; |
| 166 |
} else { |
| 167 |
for (i=0; i<wargc; i++) |
| 168 |
free(utf8argv[i]); |
| 169 |
free(utf8argv); |
| 170 |
} |
| 171 |
|
| 172 |
return ret; |
| 173 |
} |
| 174 |
|
| 175 |
/* similar to CreateFileW but accepts UTF-8 encoded lpFileName */ |
| 176 |
HANDLE WINAPI CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) |
| 177 |
{ |
| 178 |
wchar_t *wname; |
| 179 |
HANDLE handle = INVALID_HANDLE_VALUE; |
| 180 |
|
| 181 |
if ((wname = wchar_from_utf8(lpFileName)) != NULL) { |
| 182 |
#if !FLAC_WINDOWS_APP |
| 183 |
handle = CreateFileW(wname, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); |
| 184 |
#else // FLAC_WINDOWS_APP |
| 185 |
CREATEFILE2_EXTENDED_PARAMETERS params; |
| 186 |
params.dwSize = sizeof(params); |
| 187 |
params.dwFileAttributes = dwFlagsAndAttributes & 0xFFFF; |
| 188 |
params.dwFileFlags = dwFlagsAndAttributes & 0xFFF00000; |
| 189 |
params.dwSecurityQosFlags = dwFlagsAndAttributes & 0x000F0000; |
| 190 |
params.lpSecurityAttributes = lpSecurityAttributes; |
| 191 |
params.hTemplateFile = hTemplateFile; |
| 192 |
handle = CreateFile2(wname, dwDesiredAccess, dwShareMode, dwCreationDisposition, ¶ms); |
| 193 |
#endif // FLAC_WINDOWS_APP |
| 194 |
free(wname); |
| 195 |
} |
| 196 |
|
| 197 |
return handle; |
| 198 |
} |
| 199 |
|
| 200 |
/* return number of characters in the UTF-8 string */ |
| 201 |
size_t strlen_utf8(const char *str) |
| 202 |
{ |
| 203 |
size_t len; |
| 204 |
len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); /* includes terminating null */ |
| 205 |
if (len != 0) |
| 206 |
return len-1; |
| 207 |
else |
| 208 |
return strlen(str); |
| 209 |
} |
| 210 |
|
| 211 |
/* get the console width in characters */ |
| 212 |
int win_get_console_width(void) |
| 213 |
{ |
| 214 |
int width = 80; |
| 215 |
#if !FLAC_WINDOWS_APP |
| 216 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 217 |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 218 |
if(hOut != INVALID_HANDLE_VALUE && hOut != NULL) |
| 219 |
if (GetConsoleScreenBufferInfo(hOut, &csbi) != 0) |
| 220 |
width = csbi.dwSize.X; |
| 221 |
#endif // !FLAC_WINDOWS_APP |
| 222 |
return width; |
| 223 |
} |
| 224 |
|
| 225 |
/* print functions */ |
| 226 |
|
| 227 |
#if !FLAC_WINDOWS_APP |
| 228 |
static int wprint_console(FILE *stream, const wchar_t *text, size_t len) |
| 229 |
{ |
| 230 |
DWORD out; |
| 231 |
int ret; |
| 232 |
|
| 233 |
do { |
| 234 |
if (stream == stdout) { |
| 235 |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 236 |
if (hOut == INVALID_HANDLE_VALUE || hOut == NULL || GetFileType(hOut) != FILE_TYPE_CHAR) |
| 237 |
break; |
| 238 |
if (WriteConsoleW(hOut, text, len, &out, NULL) == 0) |
| 239 |
return -1; |
| 240 |
return out; |
| 241 |
} |
| 242 |
if (stream == stderr) { |
| 243 |
HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE); |
| 244 |
if (hErr == INVALID_HANDLE_VALUE || hErr == NULL || GetFileType(hErr) != FILE_TYPE_CHAR) |
| 245 |
break; |
| 246 |
if (WriteConsoleW(hErr, text, len, &out, NULL) == 0) |
| 247 |
return -1; |
| 248 |
return out; |
| 249 |
} |
| 250 |
} while(0); |
| 251 |
|
| 252 |
ret = fputws(text, stream); |
| 253 |
if (ret < 0) |
| 254 |
return ret; |
| 255 |
return len; |
| 256 |
} |
| 257 |
#endif // !FLAC_WINDOWS_APP |
| 258 |
|
| 259 |
int printf_utf8(const char *format, ...) |
| 260 |
{ |
| 261 |
int ret; |
| 262 |
va_list argptr; |
| 263 |
va_start(argptr, format); |
| 264 |
|
| 265 |
ret = vfprintf_utf8(stdout, format, argptr); |
| 266 |
|
| 267 |
va_end(argptr); |
| 268 |
|
| 269 |
return ret; |
| 270 |
} |
| 271 |
|
| 272 |
int fprintf_utf8(FILE *stream, const char *format, ...) |
| 273 |
{ |
| 274 |
int ret; |
| 275 |
va_list argptr; |
| 276 |
va_start(argptr, format); |
| 277 |
|
| 278 |
ret = vfprintf_utf8(stream, format, argptr); |
| 279 |
|
| 280 |
va_end(argptr); |
| 281 |
|
| 282 |
return ret; |
| 283 |
} |
| 284 |
|
| 285 |
int vfprintf_utf8(FILE *stream, const char *format, va_list argptr) |
| 286 |
{ |
| 287 |
char *utmp = NULL; |
| 288 |
wchar_t *wout = NULL; |
| 289 |
int ret = -1; |
| 290 |
|
| 291 |
do { |
| 292 |
if (!(utmp = (char *)malloc(UTF8_BUFFER_SIZE))) break; |
| 293 |
if ((ret = local_vsnprintf(utmp, UTF8_BUFFER_SIZE, format, argptr)) <= 0) break; |
| 294 |
if (!(wout = wchar_from_utf8(utmp))) { |
| 295 |
ret = -1; |
| 296 |
break; |
| 297 |
} |
| 298 |
#if !FLAC_WINDOWS_APP |
| 299 |
ret = wprint_console(stream, wout, wcslen(wout)); |
| 300 |
#else // FLAC_WINDOWS_APP |
| 301 |
OutputDebugStringW(wout); |
| 302 |
ret = 0; |
| 303 |
#endif // FLAC_WINDOWS_APP |
| 304 |
} while(0); |
| 305 |
|
| 306 |
free(utmp); |
| 307 |
free(wout); |
| 308 |
|
| 309 |
return ret; |
| 310 |
} |
| 311 |
|
| 312 |
/* file functions */ |
| 313 |
|
| 314 |
FILE* fopen_utf8(const char *filename, const char *mode) |
| 315 |
{ |
| 316 |
wchar_t *wname = NULL; |
| 317 |
wchar_t *wmode = NULL; |
| 318 |
FILE *f = NULL; |
| 319 |
|
| 320 |
do { |
| 321 |
if (!(wname = wchar_from_utf8(filename))) break; |
| 322 |
if (!(wmode = wchar_from_utf8(mode))) break; |
| 323 |
f = _wfopen(wname, wmode); |
| 324 |
} while(0); |
| 325 |
|
| 326 |
free(wname); |
| 327 |
free(wmode); |
| 328 |
|
| 329 |
return f; |
| 330 |
} |
| 331 |
|
| 332 |
int stat64_utf8(const char *path, struct __stat64 *buffer) |
| 333 |
{ |
| 334 |
wchar_t *wpath; |
| 335 |
int ret; |
| 336 |
|
| 337 |
if (!(wpath = wchar_from_utf8(path))) return -1; |
| 338 |
ret = _wstat64(wpath, buffer); |
| 339 |
free(wpath); |
| 340 |
|
| 341 |
return ret; |
| 342 |
} |
| 343 |
|
| 344 |
int chmod_utf8(const char *filename, int pmode) |
| 345 |
{ |
| 346 |
wchar_t *wname; |
| 347 |
int ret; |
| 348 |
|
| 349 |
if (!(wname = wchar_from_utf8(filename))) return -1; |
| 350 |
ret = _wchmod(wname, pmode); |
| 351 |
free(wname); |
| 352 |
|
| 353 |
return ret; |
| 354 |
} |
| 355 |
|
| 356 |
int utime_utf8(const char *filename, struct utimbuf *times) |
| 357 |
{ |
| 358 |
wchar_t *wname; |
| 359 |
struct __utimbuf64 ut; |
| 360 |
int ret; |
| 361 |
|
| 362 |
if (!(wname = wchar_from_utf8(filename))) return -1; |
| 363 |
ut.actime = times->actime; |
| 364 |
ut.modtime = times->modtime; |
| 365 |
ret = _wutime64(wname, &ut); |
| 366 |
free(wname); |
| 367 |
|
| 368 |
return ret; |
| 369 |
} |
| 370 |
|
| 371 |
int unlink_utf8(const char *filename) |
| 372 |
{ |
| 373 |
wchar_t *wname; |
| 374 |
int ret; |
| 375 |
|
| 376 |
if (!(wname = wchar_from_utf8(filename))) return -1; |
| 377 |
ret = _wunlink(wname); |
| 378 |
free(wname); |
| 379 |
|
| 380 |
return ret; |
| 381 |
} |
| 382 |
|
| 383 |
int rename_utf8(const char *oldname, const char *newname) |
| 384 |
{ |
| 385 |
wchar_t *wold = NULL; |
| 386 |
wchar_t *wnew = NULL; |
| 387 |
int ret = -1; |
| 388 |
|
| 389 |
do { |
| 390 |
if (!(wold = wchar_from_utf8(oldname))) break; |
| 391 |
if (!(wnew = wchar_from_utf8(newname))) break; |
| 392 |
ret = _wrename(wold, wnew); |
| 393 |
} while(0); |
| 394 |
|
| 395 |
free(wold); |
| 396 |
free(wnew); |
| 397 |
|
| 398 |
return ret; |
| 399 |
} |
| 400 |
#endif |