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.
38 #include "share/windows_unicode_filenames.h"
40 /*** FIXME: KLUDGE: export these syms for flac.exe, metaflac.exe, etc. ***/
42 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
43 static wchar_t *wchar_from_utf8(const char *str)
50 if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0)
52 if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL)
54 if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) {
63 static FLAC__bool utf8_filenames = false;
66 FLAC_API void flac_internal_set_utf8_filenames(FLAC__bool flag)
68 utf8_filenames = flag ? true : false;
71 FLAC_API FLAC__bool flac_internal_get_utf8_filenames(void)
73 return utf8_filenames;
78 FLAC_API FILE* flac_internal_fopen_utf8(const char *filename, const char *mode)
80 if (!utf8_filenames) {
81 return fopen(filename, mode);
83 wchar_t *wname = NULL;
84 wchar_t *wmode = NULL;
88 if (!(wname = wchar_from_utf8(filename))) break;
89 if (!(wmode = wchar_from_utf8(mode))) break;
90 f = _wfopen(wname, wmode);
100 FLAC_API int flac_internal_stat64_utf8(const char *path, struct __stat64 *buffer)
102 if (!utf8_filenames) {
103 return _stat64(path, buffer);
108 if (!(wpath = wchar_from_utf8(path))) return -1;
109 ret = _wstat64(wpath, buffer);
116 FLAC_API int flac_internal_chmod_utf8(const char *filename, int pmode)
118 if (!utf8_filenames) {
119 return _chmod(filename, pmode);
124 if (!(wname = wchar_from_utf8(filename))) return -1;
125 ret = _wchmod(wname, pmode);
132 FLAC_API int flac_internal_utime_utf8(const char *filename, struct utimbuf *times)
134 if (!utf8_filenames) {
135 return utime(filename, times);
138 struct __utimbuf64 ut;
141 if (!(wname = wchar_from_utf8(filename))) return -1;
142 ut.actime = times->actime;
143 ut.modtime = times->modtime;
144 ret = _wutime64(wname, &ut);
151 FLAC_API int flac_internal_unlink_utf8(const char *filename)
153 if (!utf8_filenames) {
154 return _unlink(filename);
159 if (!(wname = wchar_from_utf8(filename))) return -1;
160 ret = _wunlink(wname);
167 FLAC_API int flac_internal_rename_utf8(const char *oldname, const char *newname)
169 if (!utf8_filenames) {
170 return rename(oldname, newname);
172 wchar_t *wold = NULL;
173 wchar_t *wnew = NULL;
177 if (!(wold = wchar_from_utf8(oldname))) break;
178 if (!(wnew = wchar_from_utf8(newname))) break;
179 ret = _wrename(wold, wnew);