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