shithub: libopusenc

Download patch

ref: bdf3369e85901c3d446786c8d42e1962111278d8
parent: 802597c5e662615180948a72a2351c8bd39bb7d2
author: Jean-Marc Valin <jmvalin@jmvalin.ca>
date: Fri Dec 29 08:47:13 EST 2017

Add Windows UTF8 support

--- a/Makefile.am
+++ b/Makefile.am
@@ -16,7 +16,8 @@
 		 src/picture.h \
 		 src/resample_sse.h \
 		 src/speex_resampler.h \
-		 src/stack_alloc.h
+		 src/stack_alloc.h \
+		 src/unicode_support.h
 
 libopusenc_la_SOURCES = \
 	src/ogg_packer.c \
@@ -23,7 +24,8 @@
 	src/opus_header.c \
 	src/opusenc.c \
 	src/picture.c \
-	src/resample.c
+	src/resample.c \
+	src/unicode_support.c
 libopusenc_la_LIBADD = $(DEPS_LIBS) $(lrintf_lib)
 libopusenc_la_LDFLAGS = -no-undefined \
  -version-info @OP_LT_CURRENT@:@OP_LT_REVISION@:@OP_LT_AGE@
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -42,6 +42,7 @@
 #include "speex_resampler.h"
 #include "picture.h"
 #include "ogg_packer.h"
+#include "unicode_support.h"
 
 /* Bump this when we change the ABI. */
 #define OPE_ABI_VERSION 0
@@ -234,7 +235,7 @@
   if (enc == NULL || (error && *error)) {
     return NULL;
   }
-  obj->file = fopen(path, "wb");
+  obj->file = _ope_fopen(path, "wb");
   if (!obj->file) {
     if (error) *error = OPE_CANNOT_OPEN;
     ope_encoder_destroy(enc);
@@ -695,7 +696,7 @@
   int ret;
   struct StdioObject *obj;
   if (!(obj = malloc(sizeof(*obj)))) return OPE_ALLOC_FAIL;
-  obj->file = fopen(path, "wb");
+  obj->file = _ope_fopen(path, "wb");
   if (!obj->file) {
     free(obj);
     /* By trying to open the file first, we can recover if we can't open it. */
--- a/src/picture.c
+++ b/src/picture.c
@@ -33,6 +33,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "picture.h"
+#include "unicode_support.h"
 
 static const char BASE64_TABLE[64]={
   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
@@ -262,7 +263,7 @@
   }
 
   if (description == NULL) description = "";
-  picture_file=fopen(filename,"rb");
+  picture_file=_ope_fopen(filename,"rb");
   /*Buffer size: 8 static 4-byte fields plus 2 dynamic fields, plus the
      file/URL data.
     We reserve at least 10 bytes for the media type, in case we still need to
--- /dev/null
+++ b/src/unicode_support.c
@@ -1,0 +1,80 @@
+/* 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.
+*/
+#include "unicode_support.h"
+
+#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
+
+
+#include <windows.h>
+#include <io.h>
+
+static UINT g_old_output_cp = ((UINT)-1);
+
+static 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;
+}
+
+FILE *_ope_fopen(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;
+}
+
+#else
+
+#include <stdio.h>
+
+FILE *_ope_fopen(const char *filename_utf8, const char *mode_utf8) {
+  return fopen(filename_utf8, mode_utf8);
+}
+
+#endif
--- /dev/null
+++ b/src/unicode_support.h
@@ -1,0 +1,39 @@
+/* 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>
+
+#define WIN_UNICODE 1
+
+FILE *_ope_fopen(const char *filename_utf8, const char *mode_utf8);
+
+#endif