1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2013-2016 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.
37 #include "share/windows_unicode_filenames.h"
39 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
40 static wchar_t *wchar_from_utf8(const char *str)
47 if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0)
49 if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL)
51 if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) {
60 static FLAC__bool utf8_filenames = false;
63 void flac_internal_set_utf8_filenames(FLAC__bool flag)
65 utf8_filenames = flag ? true : false;
68 FLAC__bool flac_internal_get_utf8_filenames(void)
70 return utf8_filenames;
75 FILE* flac_internal_fopen_utf8(const char *filename, const char *mode)
77 if (!utf8_filenames) {
78 return fopen(filename, mode);
80 wchar_t *wname = NULL;
81 wchar_t *wmode = NULL;
85 if (!(wname = wchar_from_utf8(filename))) break;
86 if (!(wmode = wchar_from_utf8(mode))) break;
87 f = _wfopen(wname, wmode);
97 int flac_internal_stat64_utf8(const char *path, struct __stat64 *buffer)
99 if (!utf8_filenames) {
100 return _stat64(path, buffer);
105 if (!(wpath = wchar_from_utf8(path))) return -1;
106 ret = _wstat64(wpath, buffer);
113 int flac_internal_chmod_utf8(const char *filename, int pmode)
115 if (!utf8_filenames) {
116 return _chmod(filename, pmode);
121 if (!(wname = wchar_from_utf8(filename))) return -1;
122 ret = _wchmod(wname, pmode);
129 int flac_internal_utime_utf8(const char *filename, struct utimbuf *times)
131 if (!utf8_filenames) {
132 return utime(filename, times);
135 struct __utimbuf64 ut;
138 if (!(wname = wchar_from_utf8(filename))) return -1;
139 ut.actime = times->actime;
140 ut.modtime = times->modtime;
141 ret = _wutime64(wname, &ut);
148 int flac_internal_unlink_utf8(const char *filename)
150 if (!utf8_filenames) {
151 return _unlink(filename);
156 if (!(wname = wchar_from_utf8(filename))) return -1;
157 ret = _wunlink(wname);
164 int flac_internal_rename_utf8(const char *oldname, const char *newname)
166 if (!utf8_filenames) {
167 return rename(oldname, newname);
169 wchar_t *wold = NULL;
170 wchar_t *wnew = NULL;
174 if (!(wold = wchar_from_utf8(oldname))) break;
175 if (!(wnew = wchar_from_utf8(newname))) break;
176 ret = _wrename(wold, wnew);
186 HANDLE WINAPI flac_internal_CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
188 #if _MSC_VER > 1900 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
190 HANDLE handle = INVALID_HANDLE_VALUE;
192 if ((wname = wchar_from_utf8(lpFileName)) != NULL) {
194 handle = CreateFile2(wname, dwDesiredAccess, dwShareMode, CREATE_ALWAYS, NULL);
200 if (!utf8_filenames) {
201 return CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
204 HANDLE handle = INVALID_HANDLE_VALUE;
206 if ((wname = wchar_from_utf8(lpFileName)) != NULL) {
207 handle = CreateFileW(wname, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);