OSDN Git Service

[flac] Update FLAC to 1.4.1
[timidity41/timidity41.git] / FLAC / src / share / win_utf8_io / win_utf8_io.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2013-2022  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 <io.h>
37 #include <windows.h>
38 #include "share/win_utf8_io.h"
39
40 #define UTF8_BUFFER_SIZE 32768
41
42 #if !defined(WINAPI_FAMILY_PARTITION)
43 #define WINAPI_FAMILY_PARTITION(x) x
44 #define WINAPI_PARTITION_DESKTOP 1
45 #endif
46
47 static int local_vsnprintf(char *str, size_t size, const char *fmt, va_list va)
48 {
49         int rc;
50
51 #if defined _MSC_VER
52         if (size == 0)
53                 return 1024;
54         rc = vsnprintf_s(str, size, _TRUNCATE, fmt, va);
55         if (rc < 0)
56                 rc = size - 1;
57 #elif defined __MINGW32__
58         rc = __mingw_vsnprintf(str, size, fmt, va);
59 #else
60         rc = vsnprintf(str, size, fmt, va);
61 #endif
62
63         return rc;
64 }
65
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)
68 {
69         char *utf8str;
70         int len;
71
72         if (!wstr)
73                 return NULL;
74         if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0)
75                 return NULL;
76         if ((utf8str = (char *)malloc(len)) == NULL)
77                 return NULL;
78         if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) {
79                 free(utf8str);
80                 utf8str = NULL;
81         }
82
83         return utf8str;
84 }
85
86 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
87 static wchar_t *wchar_from_utf8(const char *str)
88 {
89         wchar_t *widestr;
90         int len;
91
92         if (!str)
93                 return NULL;
94         if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0)
95                 return NULL;
96         if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL)
97                 return NULL;
98         if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) {
99                 free(widestr);
100                 widestr = NULL;
101         }
102
103         return widestr;
104 }
105
106 /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */
107 int get_utf8_argv(int *argc, char ***argv)
108 {
109         typedef int (__cdecl *wgetmainargs_t)(int*, wchar_t***, wchar_t***, int, int*);
110         wgetmainargs_t wgetmainargs;
111         HMODULE handle;
112         int wargc;
113         wchar_t **wargv;
114         wchar_t **wenv;
115         char **utf8argv;
116         int ret, i;
117
118         if ((handle = LoadLibraryW(L"msvcrt.dll")) == NULL) return 1;
119         if ((wgetmainargs = (wgetmainargs_t)GetProcAddress(handle, "__wgetmainargs")) == NULL) {
120                 FreeLibrary(handle);
121                 return 1;
122         }
123         i = 0;
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) {
126                 FreeLibrary(handle);
127                 return 1;
128         }
129         if ((utf8argv = (char **)calloc(wargc, sizeof(char*))) == NULL) {
130                 FreeLibrary(handle);
131                 return 1;
132         }
133
134         ret = 0;
135         for (i=0; i<wargc; i++) {
136                 if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) {
137                         ret = 1;
138                         break;
139                 }
140         }
141
142         FreeLibrary(handle); /* do not free it when wargv or wenv are still in use */
143
144         if (ret == 0) {
145                 *argc = wargc;
146                 *argv = utf8argv;
147         } else {
148                 for (i=0; i<wargc; i++)
149                         free(utf8argv[i]);
150                 free(utf8argv);
151         }
152
153         return ret;
154 }
155
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)
158 {
159         wchar_t *wname;
160         HANDLE handle = INVALID_HANDLE_VALUE;
161
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, &params);
174 #endif // !WINAPI_PARTITION_DESKTOP
175                 free(wname);
176         }
177
178         return handle;
179 }
180
181 /* return number of characters in the UTF-8 string */
182 size_t strlen_utf8(const char *str)
183 {
184         size_t len;
185         len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); /* includes terminating null */
186         if (len != 0)
187                 return len-1;
188         else
189                 return strlen(str);
190 }
191
192 /* get the console width in characters */
193 int win_get_console_width(void)
194 {
195         int width = 80;
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
203         return width;
204 }
205
206 /* print functions */
207
208 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
209 static int wprint_console(FILE *stream, const wchar_t *text, size_t len)
210 {
211         DWORD out;
212         int ret;
213
214         do {
215                 if (stream == stdout) {
216                         HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
217                         if (hOut == INVALID_HANDLE_VALUE || hOut == NULL || GetFileType(hOut) != FILE_TYPE_CHAR)
218                                 break;
219                         if (WriteConsoleW(hOut, text, len, &out, NULL) == 0)
220                                 return -1;
221                         return out;
222                 }
223                 if (stream == stderr) {
224                         HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE);
225                         if (hErr == INVALID_HANDLE_VALUE || hErr == NULL || GetFileType(hErr) != FILE_TYPE_CHAR)
226                                 break;
227                         if (WriteConsoleW(hErr, text, len, &out, NULL) == 0)
228                                 return -1;
229                         return out;
230                 }
231         } while(0);
232
233         ret = fputws(text, stream);
234         if (ret < 0)
235                 return ret;
236         return len;
237 }
238 #endif // WINAPI_PARTITION_DESKTOP
239
240 int printf_utf8(const char *format, ...)
241 {
242         int ret;
243         va_list argptr;
244         va_start(argptr, format);
245
246         ret = vfprintf_utf8(stdout, format, argptr);
247
248         va_end(argptr);
249
250         return ret;
251 }
252
253 int fprintf_utf8(FILE *stream, const char *format, ...)
254 {
255         int ret;
256         va_list argptr;
257         va_start(argptr, format);
258
259         ret = vfprintf_utf8(stream, format, argptr);
260
261         va_end(argptr);
262
263         return ret;
264 }
265
266 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
267 {
268         char *utmp = NULL;
269         wchar_t *wout = NULL;
270         int ret = -1;
271
272         do {
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))) {
276                         ret = -1;
277                         break;
278                 }
279 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
280                 ret = wprint_console(stream, wout, wcslen(wout));
281 #else // !WINAPI_PARTITION_DESKTOP
282                 OutputDebugStringW(wout);
283                 ret = 0;
284 #endif // !WINAPI_PARTITION_DESKTOP
285         } while(0);
286
287         free(utmp);
288         free(wout);
289
290         return ret;
291 }
292
293 /* file functions */
294
295 FILE* fopen_utf8(const char *filename, const char *mode)
296 {
297         wchar_t *wname = NULL;
298         wchar_t *wmode = NULL;
299         FILE *f = NULL;
300
301         do {
302                 if (!(wname = wchar_from_utf8(filename))) break;
303                 if (!(wmode = wchar_from_utf8(mode))) break;
304                 f = _wfopen(wname, wmode);
305         } while(0);
306
307         free(wname);
308         free(wmode);
309
310         return f;
311 }
312
313 int stat64_utf8(const char *path, struct __stat64 *buffer)
314 {
315         wchar_t *wpath;
316         int ret;
317
318         if (!(wpath = wchar_from_utf8(path))) return -1;
319         ret = _wstat64(wpath, buffer);
320         free(wpath);
321
322         return ret;
323 }
324
325 int chmod_utf8(const char *filename, int pmode)
326 {
327         wchar_t *wname;
328         int ret;
329
330         if (!(wname = wchar_from_utf8(filename))) return -1;
331         ret = _wchmod(wname, pmode);
332         free(wname);
333
334         return ret;
335 }
336
337 int utime_utf8(const char *filename, struct utimbuf *times)
338 {
339         wchar_t *wname;
340         struct __utimbuf64 ut;
341         int ret;
342
343         if (!(wname = wchar_from_utf8(filename))) return -1;
344         ut.actime = times->actime;
345         ut.modtime = times->modtime;
346         ret = _wutime64(wname, &ut);
347         free(wname);
348
349         return ret;
350 }
351
352 int unlink_utf8(const char *filename)
353 {
354         wchar_t *wname;
355         int ret;
356
357         if (!(wname = wchar_from_utf8(filename))) return -1;
358         ret = _wunlink(wname);
359         free(wname);
360
361         return ret;
362 }
363
364 int rename_utf8(const char *oldname, const char *newname)
365 {
366         wchar_t *wold = NULL;
367         wchar_t *wnew = NULL;
368         int ret = -1;
369
370         do {
371                 if (!(wold = wchar_from_utf8(oldname))) break;
372                 if (!(wnew = wchar_from_utf8(newname))) break;
373                 ret = _wrename(wold, wnew);
374         } while(0);
375
376         free(wold);
377         free(wnew);
378
379         return ret;
380 }