1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2013-2016  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/windows_unicode_filenames.h"
39 
40 /*** FIXME: KLUDGE: export these syms for flac.exe, metaflac.exe, etc. ***/
41 
42 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
wchar_from_utf8(const char * str)43 static wchar_t *wchar_from_utf8(const char *str)
44 {
45 	wchar_t *widestr;
46 	int len;
47 
48 	if (!str)
49 		return NULL;
50 	if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0)
51 		return NULL;
52 	if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL)
53 		return NULL;
54 	if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) {
55 		free(widestr);
56 		widestr = NULL;
57 	}
58 
59 	return widestr;
60 }
61 
62 
63 static FLAC__bool utf8_filenames = false;
64 
65 
flac_internal_set_utf8_filenames(FLAC__bool flag)66 FLAC_API void flac_internal_set_utf8_filenames(FLAC__bool flag)
67 {
68 	utf8_filenames = flag ? true : false;
69 }
70 
flac_internal_get_utf8_filenames(void)71 FLAC_API FLAC__bool flac_internal_get_utf8_filenames(void)
72 {
73 	return utf8_filenames;
74 }
75 
76 /* file functions */
77 
flac_internal_fopen_utf8(const char * filename,const char * mode)78 FLAC_API FILE* flac_internal_fopen_utf8(const char *filename, const char *mode)
79 {
80 	if (!utf8_filenames) {
81 		return fopen(filename, mode);
82 	} else {
83 		wchar_t *wname = NULL;
84 		wchar_t *wmode = NULL;
85 		FILE *f = NULL;
86 
87 		do {
88 			if (!(wname = wchar_from_utf8(filename))) break;
89 			if (!(wmode = wchar_from_utf8(mode))) break;
90 			f = _wfopen(wname, wmode);
91 		} while(0);
92 
93 		free(wname);
94 		free(wmode);
95 
96 		return f;
97 	}
98 }
99 
flac_internal_stat64_utf8(const char * path,struct __stat64 * buffer)100 FLAC_API int flac_internal_stat64_utf8(const char *path, struct __stat64 *buffer)
101 {
102 	if (!utf8_filenames) {
103 		return _stat64(path, buffer);
104 	} else {
105 		wchar_t *wpath;
106 		int ret;
107 
108 		if (!(wpath = wchar_from_utf8(path))) return -1;
109 		ret = _wstat64(wpath, buffer);
110 		free(wpath);
111 
112 		return ret;
113 	}
114 }
115 
flac_internal_chmod_utf8(const char * filename,int pmode)116 FLAC_API int flac_internal_chmod_utf8(const char *filename, int pmode)
117 {
118 	if (!utf8_filenames) {
119 		return _chmod(filename, pmode);
120 	} else {
121 		wchar_t *wname;
122 		int ret;
123 
124 		if (!(wname = wchar_from_utf8(filename))) return -1;
125 		ret = _wchmod(wname, pmode);
126 		free(wname);
127 
128 		return ret;
129 	}
130 }
131 
flac_internal_utime_utf8(const char * filename,struct utimbuf * times)132 FLAC_API int flac_internal_utime_utf8(const char *filename, struct utimbuf *times)
133 {
134 	if (!utf8_filenames) {
135 		return utime(filename, times);
136 	} else {
137 		wchar_t *wname;
138 		struct __utimbuf64 ut;
139 		int ret;
140 
141 		if (!(wname = wchar_from_utf8(filename))) return -1;
142 		ut.actime = times->actime;
143 		ut.modtime = times->modtime;
144 		ret = _wutime64(wname, &ut);
145 		free(wname);
146 
147 		return ret;
148 	}
149 }
150 
flac_internal_unlink_utf8(const char * filename)151 FLAC_API int flac_internal_unlink_utf8(const char *filename)
152 {
153 	if (!utf8_filenames) {
154 		return _unlink(filename);
155 	} else {
156 		wchar_t *wname;
157 		int ret;
158 
159 		if (!(wname = wchar_from_utf8(filename))) return -1;
160 		ret = _wunlink(wname);
161 		free(wname);
162 
163 		return ret;
164 	}
165 }
166 
flac_internal_rename_utf8(const char * oldname,const char * newname)167 FLAC_API int flac_internal_rename_utf8(const char *oldname, const char *newname)
168 {
169 	if (!utf8_filenames) {
170 		return rename(oldname, newname);
171 	} else {
172 		wchar_t *wold = NULL;
173 		wchar_t *wnew = NULL;
174 		int ret = -1;
175 
176 		do {
177 			if (!(wold = wchar_from_utf8(oldname))) break;
178 			if (!(wnew = wchar_from_utf8(newname))) break;
179 			ret = _wrename(wold, wnew);
180 		} while(0);
181 
182 		free(wold);
183 		free(wnew);
184 
185 		return ret;
186 	}
187 }
188