5 src/sox.c | 30 ++++++--
6 src/unicode_support.c | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++
7 src/unicode_support.h | 46 ++++++++++++
8 7 files changed, 282 insertions(+), 15 deletions(-)
10 diff --git a/src/effects_i.c b/src/effects_i.c
11 index 685e962..deee444 100644
18 +#include "unicode_support.h"
22 @@ -463,7 +464,7 @@ FILE * lsx_open_input_file(sox_effect_t * effp, char const * filename, sox_bool
23 effp->global_info->global_info->stdin_in_use_by = effp->handler.name;
26 - else if (!(file = fopen(filename, text_mode ? "r" : "rb"))) {
27 + else if (!(file = lsx_fopen(filename, text_mode ? "r" : "rb"))) {
28 lsx_fail("couldn't open file %s: %s", filename, strerror(errno));
31 diff --git a/src/formats.c b/src/formats.c
32 index 724a4cd..a5b65d9 100644
39 +#include "unicode_support.h"
43 @@ -401,7 +402,7 @@ static FILE * xfopen(char const * identifier, char const * mode, lsx_io_type * i
47 - return fopen(identifier, mode);
48 + return lsx_fopen(identifier, mode);
51 /* Hack to rewind pipes (a small amount).
52 @@ -853,8 +854,8 @@ static sox_format_t * open_write(
57 - if (!stat(path, &st) && (st.st_mode & S_IFMT) == S_IFREG &&
59 + if (!lsx_stat(path, &st) && (st.st_mode & S_IFMT) == S_IFREG &&
60 (overwrite_permitted && !overwrite_permitted(path))) {
61 lsx_fail("permission to overwrite `%s' denied", path);
63 @@ -864,7 +865,7 @@ static sox_format_t * open_write(
64 buffer? fmemopen(buffer, buffer_size, "w+b") :
65 buffer_ptr? open_memstream(buffer_ptr, buffer_size_ptr) :
68 + lsx_fopen(path, "w+b");
70 lsx_fail("can't open output file `%s': %s", path, strerror(errno));
72 diff --git a/src/libsox_i.c b/src/libsox_i.c
73 index a5afb8e..f834136 100644
80 +#include "unicode_support.h"
86 static int check_dir(char * buf, size_t buflen, char const * name)
89 - if (!name || stat(name, &st) || (st.st_mode & S_IFMT) != S_IFDIR)
91 + if (!name || lsx_stat(name, &st) || (st.st_mode & S_IFMT) != S_IFDIR)
95 @@ -102,7 +103,7 @@ FILE * lsx_tmpfile(void)
96 fildes = mkstemp(name);
98 lsx_debug(FAKE_MKSTEMP "mkstemp, name=%s (unlinked)", name);
102 lsx_debug(FAKE_MKSTEMP "mkstemp, name=%s (O_TEMPORARY)", name);
104 diff --git a/src/noiseprof.c b/src/noiseprof.c
105 index 8fe6d4f..fd798ca 100644
106 --- a/src/noiseprof.c
107 +++ b/src/noiseprof.c
111 #include "noisered.h"
112 +#include "unicode_support.h"
116 @@ -75,7 +76,7 @@ static int sox_noiseprof_start(sox_effect_t * effp)
117 effp->global_info->global_info->stdout_in_use_by = effp->handler.name;
118 data->output_file = stdout;
120 - else if ((data->output_file = fopen(data->output_filename, "wb")) == NULL) {
121 + else if ((data->output_file = lsx_fopen(data->output_filename, "wb")) == NULL) {
122 lsx_fail("Couldn't open profile file %s: %s", data->output_filename, strerror(errno));
125 diff --git a/src/sox.c b/src/sox.c
126 index fcdac94..ec1dffe 100644
130 #include "soxconfig.h"
133 +#include "unicode_support.h"
137 @@ -238,10 +239,10 @@ static void cleanup(void)
140 if (!success && ofile->ft->io_type == lsx_io_file) { /* If we failed part way through */
141 - struct stat st; /* writing a normal file, remove it. */
142 - if (!stat(ofile->ft->filename, &st) &&
143 + struct _stat st; /* writing a normal file, remove it. */
144 + if (!lsx_stat(ofile->ft->filename, &st) &&
145 (st.st_mode & S_IFMT) == S_IFREG)
146 - unlink(ofile->ft->filename);
147 + lsx_unlink(ofile->ft->filename);
149 sox_close(ofile->ft); /* Assume we can unlink a file before closing it. */
151 @@ -905,7 +906,7 @@ static char * * strtoargv(char * s, int * argc)
153 static void read_user_effects(char const *filename)
155 - FILE *file = fopen(filename, "r");
156 + FILE *file = lsx_fopen(filename, "r");
157 const size_t buffer_size_step = 1024;
158 size_t buffer_size = buffer_size_step;
159 char *s = lsx_malloc(buffer_size); /* buffer for one input line */
160 @@ -2136,7 +2137,7 @@ static void read_comment_file(sox_comments_t * comments, char const * const file
162 size_t text_length = 100;
163 char * text = lsx_malloc(text_length + 1);
164 - FILE * file = fopen(filename, "r");
165 + FILE * file = lsx_fopen(filename, "r");
168 lsx_fail("Cannot open comment file `%s'", filename);
169 @@ -2848,7 +2849,7 @@ static sox_bool cmp_comment_text(char const * c1, char const * c2)
170 return c1 && c2 && !strcasecmp(c1, c2);
173 -int main(int argc, char **argv)
174 +static int sox_main(int argc, char **argv)
178 @@ -3051,3 +3052,20 @@ int main(int argc, char **argv)
183 +int main(int argc, char **argv)
189 + lsx_init_console();
190 + lsx_init_commandline_arguments(&sox_argc, &sox_argv);
192 + exit_code = sox_main(sox_argc, sox_argv);
194 + lsx_uninit_console();
195 + lsx_free_commandline_arguments(&sox_argc, &sox_argv);
199 diff --git a/src/unicode_support.c b/src/unicode_support.c
201 index 0000000..f89aa7b
203 +++ b/src/unicode_support.c
205 +/* Copyright (c) 2004-2015 LoRd_MuldeR <mulder2@gmx.de>
206 + File: unicode_support.c
208 + This file was originally part of a patch included with LameXP,
209 + released under the same license as the original audio tools.
211 + Redistribution and use in source and binary forms, with or without
212 + modification, are permitted provided that the following conditions
215 + - Redistributions of source code must retain the above copyright
216 + notice, this list of conditions and the following disclaimer.
218 + - Redistributions in binary form must reproduce the above copyright
219 + notice, this list of conditions and the following disclaimer in the
220 + documentation and/or other materials provided with the distribution.
222 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
223 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
224 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
225 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
226 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
227 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
228 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
229 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
230 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
231 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
232 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
234 +#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
236 +#include "unicode_support.h"
238 +#include <windows.h>
241 +static UINT g_old_output_cp = ((UINT)-1);
243 +static char *utf16_to_utf8(const wchar_t *input)
246 + int BuffSize = 0, Result = 0;
248 + BuffSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
249 + Buffer = (char*) malloc(sizeof(char) * BuffSize);
252 + Result = WideCharToMultiByte(CP_UTF8, 0, input, -1, Buffer, BuffSize, NULL, NULL);
255 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
258 +static char *utf16_to_ansi(const wchar_t *input)
261 + int BuffSize = 0, Result = 0;
263 + BuffSize = WideCharToMultiByte(CP_ACP, 0, input, -1, NULL, 0, NULL, NULL);
264 + Buffer = (char*) malloc(sizeof(char) * BuffSize);
267 + Result = WideCharToMultiByte(CP_ACP, 0, input, -1, Buffer, BuffSize, NULL, NULL);
270 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
273 +static wchar_t *utf8_to_utf16(const char *input)
276 + int BuffSize = 0, Result = 0;
278 + BuffSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
279 + Buffer = (wchar_t*) malloc(sizeof(wchar_t) * BuffSize);
282 + Result = MultiByteToWideChar(CP_UTF8, 0, input, -1, Buffer, BuffSize);
285 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
288 +void lsx_init_commandline_arguments(int *argc, char ***argv)
293 + szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
295 + if(NULL == szArglist)
297 + fprintf(stderr, "\nFATAL: CommandLineToArgvW failed\n\n");
301 + *argv = (char**) malloc(sizeof(char*) * nArgs);
306 + fprintf(stderr, "\nFATAL: Malloc failed\n\n");
310 + for(i = 0; i < nArgs; i++)
312 + (*argv)[i] = utf16_to_utf8(szArglist[i]);
313 + if(NULL == (*argv)[i])
315 + fprintf(stderr, "\nFATAL: utf16_to_utf8 failed\n\n");
320 + LocalFree(szArglist);
323 +void lsx_free_commandline_arguments(int *argc, char ***argv)
329 + for(i = 0; i < *argc; i++)
331 + if((*argv)[i] != NULL)
342 +FILE *lsx_fopen(const char *filename_utf8, const char *mode_utf8)
345 + wchar_t *filename_utf16 = utf8_to_utf16(filename_utf8);
346 + wchar_t *mode_utf16 = utf8_to_utf16(mode_utf8);
348 + if(filename_utf16 && mode_utf16)
351 + int err = _wfopen_s(&fh, filename_utf16, mode_utf16);
352 + if(err == 0) ret = fh;
355 + if(filename_utf16) free(filename_utf16);
356 + if(mode_utf16) free(mode_utf16);
361 +int lsx_stat(const char *path_utf8, struct _stat *buf)
365 + wchar_t *path_utf16 = utf8_to_utf16(path_utf8);
368 + ret = _wstat(path_utf16, buf);
375 +int lsx_unlink(const char *path_utf8)
379 + wchar_t *path_utf16 = utf8_to_utf16(path_utf8);
382 + ret = _wunlink(path_utf16);
389 +void lsx_init_console(void)
391 + g_old_output_cp = GetConsoleOutputCP();
392 + SetConsoleOutputCP(CP_UTF8);
395 +void lsx_uninit_console(void)
397 + if(g_old_output_cp != ((UINT)-1))
399 + SetConsoleOutputCP(g_old_output_cp);
404 \ No newline at end of file
405 diff --git a/src/unicode_support.h b/src/unicode_support.h
407 index 0000000..6abd0b3
409 +++ b/src/unicode_support.h
411 +/* Copyright (c) 2004-2015 LoRd_MuldeR <mulder2@gmx.de>
412 + File: unicode_support.h
414 + This file was originally part of a patch included with LameXP,
415 + released under the same license as the original audio tools.
417 + Redistribution and use in source and binary forms, with or without
418 + modification, are permitted provided that the following conditions
421 + - Redistributions of source code must retain the above copyright
422 + notice, this list of conditions and the following disclaimer.
424 + - Redistributions in binary form must reproduce the above copyright
425 + notice, this list of conditions and the following disclaimer in the
426 + documentation and/or other materials provided with the distribution.
428 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
429 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
430 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
431 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
432 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
433 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
434 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
435 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
436 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
437 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
438 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
440 +#ifndef UNICODE_SUPPORT_H_INCLUDED
441 +#define UNICODE_SUPPORT_H_INCLUDED
444 +#include <sys/stat.h>
446 +#define WIN_UNICODE 1
448 +void lsx_init_commandline_arguments(int *argc, char ***argv);
449 +void lsx_free_commandline_arguments(int *argc, char ***argv);
450 +FILE *lsx_fopen(const char *filename_utf8, const char *mode_utf8);
451 +int lsx_stat(const char *path_utf8, struct _stat *buf);
452 +int lsx_unlink(const char *path_utf8);
453 +void lsx_init_console(void);
454 +void lsx_uninit_console(void);
457 \ No newline at end of file