[flac-dev] One tiny Windows Unicode patch

Janne Hyvärinen cse at sci.fi
Sat Apr 20 01:23:35 PDT 2013


I have been doing some heavy testing with the new FLAC version, and I 
found that CreateFile function in grabbag had been left out of UTF-8 
treatment at some point. This causes re-encoding an existing flac to the 
same name to break the file if it contains non-ascii characters. 
Attached patch fixes this.

-------------- next part --------------
diff --git a/include/share/win_utf8_io.h b/include/share/win_utf8_io.h
index b48e85e..b689db0 100644
--- a/include/share/win_utf8_io.h
+++ b/include/share/win_utf8_io.h
@@ -10,7 +10,7 @@ extern "C" {
 #include <stdio.h>
 #include <sys/stat.h>
 #include <stdarg.h>
-
+#include <windows.h>
 
 int get_utf8_argv(int *argc, char ***argv);
 
@@ -25,6 +25,7 @@ int chmod_utf8(const char *filename, int pmode);
 int utime_utf8(const char *filename, struct utimbuf *times);
 int unlink_utf8(const char *filename);
 int rename_utf8(const char *oldname, const char *newname);
+HANDLE WINAPI CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
 
 #ifdef __cplusplus
 } /* extern "C" */
diff --git a/src/share/grabbag/file.c b/src/share/grabbag/file.c
index dd2880c..a3706f1 100644
--- a/src/share/grabbag/file.c
+++ b/src/share/grabbag/file.c
@@ -127,8 +127,8 @@ FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
 	BY_HANDLE_FILE_INFORMATION info1, info2;
 	HANDLE h1, h2;
 	BOOL ok = 1;
-	h1 = CreateFile(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
-	h2 = CreateFile(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+	h1 = CreateFile_utf8(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+	h2 = CreateFile_utf8(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 	if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
 		ok = 0;
 	ok &= GetFileInformationByHandle(h1, &info1);
diff --git a/src/share/win_utf8_io/win_utf8_io.c b/src/share/win_utf8_io/win_utf8_io.c
index d7d1dbd..d2288d9 100644
--- a/src/share/win_utf8_io/win_utf8_io.c
+++ b/src/share/win_utf8_io/win_utf8_io.c
@@ -262,3 +262,16 @@ int rename_utf8(const char *oldname, const char *newname)
 
 	return ret;
 }
+
+HANDLE WINAPI CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
+{
+	wchar_t *wname;
+	HANDLE handle = INVALID_HANDLE_VALUE;
+
+	if ((wname = wchar_from_utf8(lpFileName)) != NULL) {
+		handle = CreateFileW(wname, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+		free(wname);
+	}
+
+	return handle;
+}


More information about the flac-dev mailing list