shithub: opus-tools

Download patch

ref: 2081ac9dac4f4830ffb5cbce1024ba270478a77d
parent: 58347273ab4c9e9d30ab8ffd62acbac348f7e0c8
author: Gregory Maxwell <greg@xiph.org>
date: Sat Jul 21 23:48:06 EDT 2012

Cleanups; make win32 unicode support only get used on win32.

Unbreak project files (hopefully).
Also, smarter SSE detection; to avoid breaking on x86 compilers
without SSE support.

--- a/Makefile.am
+++ b/Makefile.am
@@ -4,7 +4,7 @@
 
 SUBDIRS = .
 
-INCLUDES = -I$(top_srcdir)/src
+INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/win32
 
 AM_CFLAGS = $(OGG_CFLAGS) $(Opus_CFLAGS) -DOPUSTOOLS -D_FORTIFY_SOURCE=2 -DHAVE_GETOPT_H -DSPX_RESAMPLE_EXPORT= -DRANDOM_PREFIX=opustools -DOUTSIDE_SPEEX -DFLOATING_POINT
 
@@ -21,6 +21,7 @@
                  src/resample_sse.h \
                  src/speex_resampler.h \
                  src/stack_alloc.h \
+                 win32/unicode_support.h \
                  src/wave_out.h \
                  src/wav_io.h
 
@@ -28,11 +29,11 @@
 
 dist_man_MANS = man/opusenc.1 man/opusdec.1 man/opusinfo.1
 
-opusenc_SOURCES = src/opus_header.c src/opusenc.c src/resample.c src/audio-in.c src/diag_range.c src/lpc.c
+opusenc_SOURCES = src/opus_header.c src/opusenc.c src/resample.c src/audio-in.c src/diag_range.c src/lpc.c win32/unicode_support.c
 opusenc_LDADD = $(OGG_LIBS) $(Opus_LIBS) -lm
 opusenc_MANS = man/opusenc.1
 
-opusdec_SOURCES = src/opus_header.c src/wav_io.c src/wave_out.c src/opusdec.c src/resample.c src/diag_range.c
+opusdec_SOURCES = src/opus_header.c src/wav_io.c src/wave_out.c src/opusdec.c src/resample.c src/diag_range.c win32/unicode_support.c
 opusdec_LDADD = $(OGG_LIBS) $(Opus_LIBS) -lm
 opusdec_MANS = man/opusdec.1
 
--- a/configure.ac
+++ b/configure.ac
@@ -163,11 +163,11 @@
 dnl check for sndio
 HAVE_SNDIO=no
 AC_CHECK_LIB([sndio], [sio_open])
-if x$ac_cv_lib_sndio_sio_open = xyes; then
+if test "x$ac_cv_lib_sndio_sio_open" = "xyes"; then
   HAVE_SNDIO=yes
 fi
 
-if test x$HAVE_OSS != xyes && test x$HAVE_SNDIO != xyes; then
+if test "x$HAVE_OSS" != "xyes" && test "x$HAVE_SNDIO" != "xyes"; then
   AC_MSG_WARN([Audio support not found -- no direct audio output in opusdec])
 fi
 
@@ -184,7 +184,7 @@
 esac
 
 ac_build_stack_protector=no
-if test $ac_cv_c_compiler_gnu = yes && test $on_x86 = yes; then
+if test "x$ac_cv_c_compiler_gnu" = "xyes" && test "x$on_x86" = "xyes"; then
 AC_ARG_ENABLE(stack-protector,
      AS_HELP_STRING([--disable-stack-protector],[Disable compiler stack hardening]),
      [
--- a/src/opusdec.c
+++ b/src/opusdec.c
@@ -49,12 +49,18 @@
 #include <opus_multistream.h>
 #include <ogg/ogg.h>
 
-#if defined WIN32 || defined _WIN32
-#include "wave_out.h"
+#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
+# include "unicode_support.h"
+# include "wave_out.h"
 /* We need the following two to set stdout to binary */
-#include <io.h>
-#include <fcntl.h>
+# include <io.h>
+# include <fcntl.h>
+# define I64FORMAT "I64d"
+#else
+# define I64FORMAT "lld"
+# define fopen_utf8(_x,_y) fopen((_x),(_y))
 #endif
+
 #include <math.h>
 
 #ifdef HAVE_LRINTF
@@ -63,39 +69,30 @@
 # define float2int(flt) ((int)(floor(.5+flt)))
 #endif
 
-#ifdef _WIN32
-#define I64FORMAT "I64d"
-#else
-#define I64FORMAT "lld"
-#endif
-
 #if defined HAVE_LIBSNDIO
-#include <sndio.h>
-
+# include <sndio.h>
 #elif defined HAVE_SYS_SOUNDCARD_H || defined HAVE_MACHINE_SOUNDCARD_H || HAVE_SOUNDCARD_H
-#ifdef HAVE_SYS_SOUNDCARD_H
-# include <sys/soundcard.h>
-#elif HAVE_MACHINE_SOUNDCARD_H
-# include <machine/soundcard.h>
-#else
-# include <soundcard.h>
-#endif
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-
+# ifdef HAVE_SYS_SOUNDCARD_H
+#  include <sys/soundcard.h>
+# elif HAVE_MACHINE_SOUNDCARD_H
+#  include <machine/soundcard.h>
+# else
+#  include <soundcard.h>
+# endif
+# include <sys/types.h>
+# include <sys/stat.h>
+# include <fcntl.h>
+# include <sys/ioctl.h>
 #elif defined HAVE_SYS_AUDIOIO_H
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/audioio.h>
-#ifndef AUDIO_ENCODING_SLINEAR
-#define AUDIO_ENCODING_SLINEAR AUDIO_ENCODING_LINEAR /* Solaris */
+# include <sys/types.h>
+# include <fcntl.h>
+# include <sys/ioctl.h>
+# include <sys/audioio.h>
+# ifndef AUDIO_ENCODING_SLINEAR
+#  define AUDIO_ENCODING_SLINEAR AUDIO_ENCODING_LINEAR /* Solaris */
+# endif
 #endif
 
-#endif
-
 #include <string.h>
 #include "wav_io.h"
 #include "opus_header.h"
@@ -103,8 +100,6 @@
 #include "speex_resampler.h"
 #include "stack_alloc.h"
 
-#include "unicode_support.h"
-
 #define MINI(_a,_b)      ((_a)<(_b)?(_a):(_b))
 #define MAXI(_a,_b)      ((_a)>(_b)?(_a):(_b))
 #define CLAMPI(_a,_b,_c) (MAXI(_a,MINI(_b,_c)))
@@ -605,7 +600,11 @@
    return sampout;
 }
 
+#ifdef WIN_UNICODE
 static int opusdec_main(int argc, char **argv)
+#else
+int main(int argc, char **argv)
+#endif
 {
    int c;
    int option_index = 0;
@@ -886,7 +885,6 @@
                   last_spin++;
                }
 
-
                if (ret<0)
                {
                   fprintf (stderr, "Decoding error: %s\n", opus_strerror(ret));
@@ -953,8 +951,10 @@
          }
       }
       if (feof(fin)) {
-         fprintf(stderr, "Complete.\n");
-         fflush(stderr);
+         if(!quiet) {
+           fprintf(stderr, "Complete.\n");
+           fflush(stderr);
+         }
          break;
       }
    }
@@ -1006,6 +1006,7 @@
    return 0;
 }
 
+#ifdef WIN_UNICODE
 int main( int argc, char **argv )
 {
   int my_argc;
@@ -1018,3 +1019,4 @@
 
   return exit_code;
 }
+#endif
--- a/src/opusdec.vcxproj
+++ b/src/opusdec.vcxproj
@@ -27,7 +27,7 @@
     <ClCompile Include="opusdec.c" />
     <ClCompile Include="opus_header.c" />
     <ClCompile Include="resample.c" />
-    <ClCompile Include="unicode_support.c" />
+    <ClCompile Include="..\win32\unicode_support.c" />
     <ClCompile Include="wave_out.c" />
     <ClCompile Include="wav_io.c" />
   </ItemGroup>
@@ -44,7 +44,7 @@
     <ClInclude Include="resample_sse.h" />
     <ClInclude Include="speex_resampler.h" />
     <ClInclude Include="stack_alloc.h" />
-    <ClInclude Include="unicode_support.h" />
+    <ClInclude Include="..\win32\unicode_support.h" />
     <ClInclude Include="wave_out.h" />
     <ClInclude Include="wav_io.h" />
   </ItemGroup>
@@ -169,14 +169,9 @@
       <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;HAVE_CONFIG_H;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
       <WarningLevel>Level3</WarningLevel>
-      <AdditionalIncludeDirectories>..\..\libogg-1.3.0\include;..\..\opus-git\include;..\win32;..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <AdditionalIncludeDirectories>..\..\libogg\include;..\..\opus\include;..\win32;..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
-      <Optimization>Full</Optimization>
-      <InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
-      <OmitFramePointers>true</OmitFramePointers>
-      <FunctionLevelLinking>true</FunctionLevelLinking>
-      <EnableEnhancedInstructionSet>NotSet</EnableEnhancedInstructionSet>
     </ClCompile>
     <Link>
       <TargetMachine>MachineX86</TargetMachine>
@@ -184,10 +179,8 @@
       <SubSystem>Console</SubSystem>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>F:\DeLpHi\EncodePointerLib\Release\EncodePointer.lib;celt.lib;opus.lib;silk_common.lib;silk_float.lib;libogg_static.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories>$(OutDir);D:\SVN\Tools\opus-git\Release;D:\SVN\Tools\libogg-1.3.0\win32\VS2010\Win32\Release</AdditionalLibraryDirectories>
-      <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
-      <MinimumRequiredVersion>5.0</MinimumRequiredVersion>
+      <AdditionalDependencies>celt.lib;opus.lib;silk_common.lib;silk_float.lib;libogg_static.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalLibraryDirectories>$(OutDir)</AdditionalLibraryDirectories>
     </Link>
     <PreBuildEvent>
       <Command>"$(ProjectDir)..\win32\genversion.bat" "$(ProjectDir)..\win32\version.h" VERSION</Command>
--- a/src/opusdec.vcxproj.filters
+++ b/src/opusdec.vcxproj.filters
@@ -41,9 +41,6 @@
     <ClCompile Include="..\share\getopt1.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="unicode_support.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="lpc.h">
@@ -86,7 +83,6 @@
       <Filter>Header Files</Filter>
     </ClInclude>
     <ClInclude Include="..\win32\config.h">
-    <ClInclude Include="unicode_support.h">
       <Filter>Header Files</Filter>
     </ClInclude>
   </ItemGroup>
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -46,10 +46,15 @@
 #define snprintf _snprintf
 #endif
 
-#ifdef _WIN32
-#define I64FORMAT "I64d"
+#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
+# include "unicode_support.h"
+/* We need the following two to set stdout to binary */
+# include <io.h>
+# include <fcntl.h>
+# define I64FORMAT "I64d"
 #else
-#define I64FORMAT "lld"
+# define I64FORMAT "lld"
+# define fopen_utf8(_x,_y) fopen((_x),(_y))
 #endif
 
 #include <opus.h>
@@ -61,12 +66,6 @@
 #include "opusenc.h"
 #include "diag_range.h"
 
-#if defined WIN32 || defined _WIN32
-/* We need the following two to set stdout to binary */
-#include <io.h>
-#include <fcntl.h>
-#endif
-
 #ifdef VALGRIND
 #include <valgrind/memcheck.h>
 #define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
@@ -76,7 +75,6 @@
 #define VG_CHECK(x,y)
 #endif
 
-#include "unicode_support.h"
 
 static void comment_init(char **comments, int* length, const char *vendor_string);
 static void comment_add(char **comments, int* length, char *tag, char *val);
@@ -172,7 +170,11 @@
   if(seconds>0)fprintf(stderr," %0.4g second%s",seconds,seconds!=1?"s":"");
 }
 
+#ifdef WIN_UNICODE
 static int opusenc_main(int argc, char **argv)
+#else
+int main(int argc, char **argv)
+#endif
 {
   static const input_format raw_format = {NULL, 0, raw_open, wav_close, "raw",N_("RAW file reader")};
   int option_index=0;
@@ -909,7 +911,7 @@
           coded_seconds/wall_time,
           estbitrate/1000.);
         fprintf(stderr,"%s",sbuf);
-        fflush(stderr); //Required for real-time progress updates in GUI's!
+        fflush(stderr);
         last_spin_len=strlen(sbuf);
         last_spin=stop_time;
       }
@@ -1031,6 +1033,7 @@
 #undef readint
 #undef writeint
 
+#ifdef WIN_UNICODE
 int main( int argc, char **argv )
 {
   int my_argc;
@@ -1043,3 +1046,4 @@
 
   return exit_code;
 }
+#endif
--- a/src/opusenc.vcxproj
+++ b/src/opusenc.vcxproj
@@ -27,7 +27,7 @@
     <ClCompile Include="opusenc.c" />
     <ClCompile Include="opus_header.c" />
     <ClCompile Include="resample.c" />
-    <ClCompile Include="unicode_support.c" />
+    <ClCompile Include="..\win32\unicode_support.c" />
     <ClCompile Include="wave_out.c" />
     <ClCompile Include="wav_io.c" />
   </ItemGroup>
@@ -44,7 +44,7 @@
     <ClInclude Include="resample_sse.h" />
     <ClInclude Include="speex_resampler.h" />
     <ClInclude Include="stack_alloc.h" />
-    <ClInclude Include="unicode_support.h" />
+    <ClInclude Include="..\win32\unicode_support.h" />
     <ClInclude Include="wave_out.h" />
     <ClInclude Include="wav_io.h" />
   </ItemGroup>
@@ -180,10 +180,8 @@
       <SubSystem>Console</SubSystem>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>F:\DeLpHi\EncodePointerLib\Release\EncodePointer.lib;celt.lib;opus.lib;silk_common.lib;silk_float.lib;libogg_static.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories>$(OutDir);D:\SVN\Tools\opus-git\Release;D:\SVN\Tools\libogg-1.3.0\win32\VS2010\Win32\Release</AdditionalLibraryDirectories>
-      <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
-      <MinimumRequiredVersion>5.0</MinimumRequiredVersion>
+      <AdditionalDependencies>celt.lib;opus.lib;silk_common.lib;silk_float.lib;libogg_static.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalLibraryDirectories>$(OutDir)</AdditionalLibraryDirectories>
     </Link>
     <PreBuildEvent>
       <Command>"$(ProjectDir)..\win32\genversion.bat" "$(ProjectDir)..\win32\version.h" VERSION</Command>
--- a/src/opusenc.vcxproj.filters
+++ b/src/opusenc.vcxproj.filters
@@ -41,7 +41,7 @@
     <ClCompile Include="..\share\getopt1.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="unicode_support.c">
+    <ClCompile Include="..\win32\unicode_support.c">
       <Filter>Source Files</Filter>
     </ClCompile>
   </ItemGroup>
@@ -86,7 +86,7 @@
       <Filter>Header Files</Filter>
     </ClInclude>
     <ClInclude Include="..\win32\config.h">
-    <ClInclude Include="unicode_support.h">
+    <ClInclude Include="..\win32\unicode_support.h">
       <Filter>Header Files</Filter>
     </ClInclude>
   </ItemGroup>
--- a/src/opusinfo.vcxproj
+++ b/src/opusinfo.vcxproj
@@ -168,7 +168,7 @@
       <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;HAVE_CONFIG_H;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
       <WarningLevel>Level3</WarningLevel>
-      <AdditionalIncludeDirectories>..\..\libogg-1.3.0\include;..\..\opus-git\include;..\win32;..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <AdditionalIncludeDirectories>..\..\libogg\include;..\..\opus\include;..\win32;..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
       <FloatingPointModel>Fast</FloatingPointModel>
@@ -180,7 +180,7 @@
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <AdditionalDependencies>celt.lib;opus.lib;silk_common.lib;silk_float.lib;libogg_static.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories>$(OutDir);D:\SVN\Tools\opus-git\Release;D:\SVN\Tools\libogg-1.3.0\win32\VS2010\Win32\Release</AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>$(OutDir)</AdditionalLibraryDirectories>
     </Link>
     <PreBuildEvent>
       <Command>"$(ProjectDir)..\win32\genversion.bat" "$(ProjectDir)..\win32\version.h" VERSION</Command>
--- a/src/resample.c
+++ b/src/resample.c
@@ -95,7 +95,7 @@
 #define NULL 0
 #endif
 
-#if defined(FLOATING_POINT) && (defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__amd64__))
+#if defined(FLOATING_POINT) && defined(__SSE__)
 #include "resample_sse.h"
 #endif
 
--- a/src/unicode_support.c
+++ /dev/null
@@ -1,148 +1,0 @@
-#include "unicode_support.h"
-
-#include <windows.h>
-#include <io.h>
-
-char *utf16_to_utf8(const wchar_t *input)
-{
-	char *Buffer;
-	int BuffSize = 0, Result = 0;
-
-	BuffSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
-	Buffer = (char*) malloc(sizeof(char) * BuffSize);
-	if(Buffer)
-	{
-		Result = WideCharToMultiByte(CP_UTF8, 0, input, -1, Buffer, BuffSize, NULL, NULL);
-	}
-
-	return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
-}
-
-char *utf16_to_ansi(const wchar_t *input)
-{
-	char *Buffer;
-	int BuffSize = 0, Result = 0;
-
-	BuffSize = WideCharToMultiByte(CP_ACP, 0, input, -1, NULL, 0, NULL, NULL);
-	Buffer = (char*) malloc(sizeof(char) * BuffSize);
-	if(Buffer)
-	{
-		Result = WideCharToMultiByte(CP_ACP, 0, input, -1, Buffer, BuffSize, NULL, NULL);
-	}
-
-	return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
-}
-
-wchar_t *utf8_to_utf16(const char *input)
-{
-	wchar_t *Buffer;
-	int BuffSize = 0, Result = 0;
-
-	BuffSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
-	Buffer = (wchar_t*) malloc(sizeof(wchar_t) * BuffSize);
-	if(Buffer)
-	{
-		Result = MultiByteToWideChar(CP_UTF8, 0, input, -1, Buffer, BuffSize);
-	}
-
-	return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
-}
-
-void init_commandline_arguments_utf8(int *argc, char ***argv)
-{
-	int i, nArgs;
-	LPWSTR *szArglist;
-
-	szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
-
-	if(NULL == szArglist)
-	{
-		fprintf(stderr, "\nFATAL: CommandLineToArgvW failed\n\n");
-		exit(-1);
-	}
-
-	*argv = (char**) malloc(sizeof(char*) * nArgs);
-	*argc = nArgs;
-
-	if(NULL == *argv)
-	{
-		fprintf(stderr, "\nFATAL: Malloc failed\n\n");
-		exit(-1);
-	}
-	
-	for(i = 0; i < nArgs; i++)
-	{
-		(*argv)[i] = utf16_to_utf8(szArglist[i]);
-		if(NULL == (*argv)[i])
-		{
-			fprintf(stderr, "\nFATAL: utf16_to_utf8 failed\n\n");
-			exit(-1);
-		}
-	}
-
-	LocalFree(szArglist);
-}
-
-void free_commandline_arguments_utf8(int *argc, char ***argv)
-{
-	int i = 0;
-	
-	if(*argv != NULL)
-	{
-		for(i = 0; i < *argc; i++)
-		{
-			if((*argv)[i] != NULL)
-			{
-				free((*argv)[i]);
-				(*argv)[i] = NULL;
-			}
-		}
-		free(*argv);
-		*argv = NULL;
-	}
-}
-
-FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8)
-{
-	FILE *ret = NULL;
-	wchar_t *filename_utf16 = utf8_to_utf16(filename_utf8);
-	wchar_t *mode_utf16 = utf8_to_utf16(mode_utf8);
-	
-	if(filename_utf16 && mode_utf16)
-	{
-		ret = _wfopen(filename_utf16, mode_utf16);
-	}
-
-	if(filename_utf16) free(filename_utf16);
-	if(mode_utf16) free(mode_utf16);
-
-	return ret;
-}
-
-int stat_utf8(const char *path_utf8, struct _stat *buf)
-{
-	int ret = -1;
-	
-	wchar_t *path_utf16 = utf8_to_utf16(path_utf8);
-	if(path_utf16)
-	{
-		ret = _wstat(path_utf16, buf);
-		free(path_utf16);
-	}
-	
-	return ret;
-}
-
-int unlink_utf8(const char *path_utf8)
-{
-	int ret = -1;
-	
-	wchar_t *path_utf16 = utf8_to_utf16(path_utf8);
-	if(path_utf16)
-	{
-		ret = _wunlink(path_utf16);
-		free(path_utf16);
-	}
-	
-	return ret;
-}
--- a/src/unicode_support.h
+++ /dev/null
@@ -1,16 +1,0 @@
-#ifndef UNICODE_SUPPORT_H_INCLUDED
-#define UNICODE_SUPPORT_H_INCLUDED
-
-#include <stdio.h>
-#include <sys/stat.h>
-
-char *utf16_to_utf8(const wchar_t *input);
-char *utf16_to_ansi(const wchar_t *input);
-wchar_t *utf8_to_utf16(const char *input);
-void init_commandline_arguments_utf8(int *argc, char ***argv);
-void free_commandline_arguments_utf8(int *argc, char ***argv);
-FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8);
-int stat_utf8(const char *path_utf8, struct _stat *buf);
-int unlink_utf8(const char *path_utf8);
-
-#endif
\ No newline at end of file
--- /dev/null
+++ b/win32/unicode_support.c
@@ -1,0 +1,181 @@
+/* Copyright (c) 2004-2012 LoRd_MuldeR <mulder2@gmx.de>
+   File: unicode_support.c
+
+   This file was originally part of a patch included with LameXP,
+   released under the same license as the original audio tools.
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
+
+#include "unicode_support.h"
+
+#include <windows.h>
+#include <io.h>
+
+char *utf16_to_utf8(const wchar_t *input)
+{
+	char *Buffer;
+	int BuffSize = 0, Result = 0;
+
+	BuffSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
+	Buffer = (char*) malloc(sizeof(char) * BuffSize);
+	if(Buffer)
+	{
+		Result = WideCharToMultiByte(CP_UTF8, 0, input, -1, Buffer, BuffSize, NULL, NULL);
+	}
+
+	return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
+}
+
+char *utf16_to_ansi(const wchar_t *input)
+{
+	char *Buffer;
+	int BuffSize = 0, Result = 0;
+
+	BuffSize = WideCharToMultiByte(CP_ACP, 0, input, -1, NULL, 0, NULL, NULL);
+	Buffer = (char*) malloc(sizeof(char) * BuffSize);
+	if(Buffer)
+	{
+		Result = WideCharToMultiByte(CP_ACP, 0, input, -1, Buffer, BuffSize, NULL, NULL);
+	}
+
+	return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
+}
+
+wchar_t *utf8_to_utf16(const char *input)
+{
+	wchar_t *Buffer;
+	int BuffSize = 0, Result = 0;
+
+	BuffSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
+	Buffer = (wchar_t*) malloc(sizeof(wchar_t) * BuffSize);
+	if(Buffer)
+	{
+		Result = MultiByteToWideChar(CP_UTF8, 0, input, -1, Buffer, BuffSize);
+	}
+
+	return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
+}
+
+void init_commandline_arguments_utf8(int *argc, char ***argv)
+{
+	int i, nArgs;
+	LPWSTR *szArglist;
+
+	szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
+
+	if(NULL == szArglist)
+	{
+		fprintf(stderr, "\nFATAL: CommandLineToArgvW failed\n\n");
+		exit(-1);
+	}
+
+	*argv = (char**) malloc(sizeof(char*) * nArgs);
+	*argc = nArgs;
+
+	if(NULL == *argv)
+	{
+		fprintf(stderr, "\nFATAL: Malloc failed\n\n");
+		exit(-1);
+	}
+	
+	for(i = 0; i < nArgs; i++)
+	{
+		(*argv)[i] = utf16_to_utf8(szArglist[i]);
+		if(NULL == (*argv)[i])
+		{
+			fprintf(stderr, "\nFATAL: utf16_to_utf8 failed\n\n");
+			exit(-1);
+		}
+	}
+
+	LocalFree(szArglist);
+}
+
+void free_commandline_arguments_utf8(int *argc, char ***argv)
+{
+	int i = 0;
+	
+	if(*argv != NULL)
+	{
+		for(i = 0; i < *argc; i++)
+		{
+			if((*argv)[i] != NULL)
+			{
+				free((*argv)[i]);
+				(*argv)[i] = NULL;
+			}
+		}
+		free(*argv);
+		*argv = NULL;
+	}
+}
+
+FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8)
+{
+	FILE *ret = NULL;
+	wchar_t *filename_utf16 = utf8_to_utf16(filename_utf8);
+	wchar_t *mode_utf16 = utf8_to_utf16(mode_utf8);
+	
+	if(filename_utf16 && mode_utf16)
+	{
+		ret = _wfopen(filename_utf16, mode_utf16);
+	}
+
+	if(filename_utf16) free(filename_utf16);
+	if(mode_utf16) free(mode_utf16);
+
+	return ret;
+}
+
+int stat_utf8(const char *path_utf8, struct _stat *buf)
+{
+	int ret = -1;
+	
+	wchar_t *path_utf16 = utf8_to_utf16(path_utf8);
+	if(path_utf16)
+	{
+		ret = _wstat(path_utf16, buf);
+		free(path_utf16);
+	}
+	
+	return ret;
+}
+
+int unlink_utf8(const char *path_utf8)
+{
+	int ret = -1;
+	
+	wchar_t *path_utf16 = utf8_to_utf16(path_utf8);
+	if(path_utf16)
+	{
+		ret = _wunlink(path_utf16);
+		free(path_utf16);
+	}
+	
+	return ret;
+}
+
+#endif
\ No newline at end of file
--- /dev/null
+++ b/win32/unicode_support.h
@@ -1,0 +1,47 @@
+/* Copyright (c) 2004-2012 LoRd_MuldeR <mulder2@gmx.de>
+   File: unicode_support.h
+
+   This file was originally part of a patch included with LameXP,
+   released under the same license as the original audio tools.
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+#ifndef UNICODE_SUPPORT_H_INCLUDED
+#define UNICODE_SUPPORT_H_INCLUDED
+
+#include <stdio.h>
+#include <sys/stat.h>
+
+#define WIN_UNICODE 1
+
+char *utf16_to_utf8(const wchar_t *input);
+char *utf16_to_ansi(const wchar_t *input);
+wchar_t *utf8_to_utf16(const char *input);
+void init_commandline_arguments_utf8(int *argc, char ***argv);
+void free_commandline_arguments_utf8(int *argc, char ***argv);
+FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8);
+int stat_utf8(const char *path_utf8, struct _stat *buf);
+int unlink_utf8(const char *path_utf8);
+
+#endif
\ No newline at end of file