8177951: Charset problem when the name of the sound device contains Chinese character

Reviewed-by: amenkov, serb
This commit is contained in:
Charlie Jiang 2017-08-01 14:18:14 +08:00 committed by Sergey Bylokhov
parent a09f958028
commit fbd95a81a8
7 changed files with 129 additions and 22 deletions

View File

@ -61,6 +61,7 @@ ifeq ($(OPENJDK_TARGET_OS), windows)
-DUSE_PLATFORM_MIDI_IN=TRUE \
-DUSE_PORTS=TRUE
LIBJSOUND_SRC_FILES += \
PLATFORM_API_WinOS_Charset_Util.cpp \
PLATFORM_API_WinOS_MidiIn.cpp \
PLATFORM_API_WinOS_MidiOut.c \
PLATFORM_API_WinOS_Util.c \
@ -190,6 +191,7 @@ ifneq ($(filter jsoundds, $(EXTRA_SOUND_JNI_LIBS)), )
OUTPUT_DIR := $(INSTALL_LIBRARIES_HERE), \
SRC := $(LIBJSOUND_SRC_DIRS), \
INCLUDE_FILES := Utilities.c $(LIBJSOUND_DAUDIOFILES) \
PLATFORM_API_WinOS_Charset_Util.cpp \
PLATFORM_API_WinOS_DirectSound.cpp, \
OPTIMIZATION := LOW, \
CFLAGS := $(CFLAGS_JDKLIB) \

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "PLATFORM_API_WinOS_Charset_Util.h"
#include <cstring>
#ifdef __cplusplus
extern "C" {
#endif
LPSTR UnicodeToUTF8(const LPCWSTR lpUnicodeStr)
{
DWORD dwUTF8Len = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, nullptr, 0, nullptr, nullptr);
LPSTR lpUTF8Str = new CHAR[dwUTF8Len];
memset(lpUTF8Str, 0, sizeof(CHAR) * (dwUTF8Len));
WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, lpUTF8Str, dwUTF8Len, nullptr, nullptr);
return lpUTF8Str;
}
void UnicodeToUTF8AndCopy(LPSTR dest, LPCWSTR src, SIZE_T maxLength) {
LPSTR utf8EncodedName = UnicodeToUTF8(src);
strncpy(dest, utf8EncodedName, maxLength - 1);
delete[] utf8EncodedName;
dest[maxLength - 1] = '\0';
}
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#ifndef PLATFORM_API_WINOS_CHARSET_UTILS_H
#define PLATFORM_API_WINOS_CHARSET_UTILS_H
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif
// NOTE: It's a caller responbility to free the allocated memory using delete[], just like in UnicodeToUTF8AndCopy function
LPSTR _cdecl UnicodeToUTF8(const LPCWSTR lpAnsiStr);
void _cdecl UnicodeToUTF8AndCopy(LPSTR dest, LPCWSTR src, SIZE_T maxLength);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -52,6 +52,9 @@ extern "C" {
}
#endif
/* include to prevent charset problem */
#include "PLATFORM_API_WinOS_Charset_Util.h"
#ifdef USE_DEBUG_SILENCING
#define DEBUG_SILENCING0(p) TRACE0(p)
#define DEBUG_SILENCING1(p1,p2) TRACE1(p1,p2)
@ -227,13 +230,13 @@ INT32 DAUDIO_GetDirectAudioDeviceCount() {
}
BOOL CALLBACK DS_GetDescEnum(LPGUID lpGuid,
LPCSTR lpstrDescription,
LPCSTR lpstrModule,
LPCWSTR lpstrDescription,
LPCWSTR lpstrModule,
DirectAudioDeviceDescription* desc) {
INT32 cacheIndex = findCacheItemByGUID(lpGuid, g_audioDeviceCache[desc->deviceID].isSource);
if (cacheIndex == desc->deviceID) {
strncpy(desc->name, lpstrDescription, DAUDIO_STRING_LENGTH);
UnicodeToUTF8AndCopy(desc->name, lpstrDescription, DAUDIO_STRING_LENGTH);
//strncpy(desc->description, lpstrModule, DAUDIO_STRING_LENGTH);
desc->maxSimulLines = -1;
/* do not continue enumeration */
@ -257,10 +260,10 @@ INT32 DAUDIO_GetDirectAudioDeviceDescription(INT32 mixerIndex, DirectAudioDevice
}
desc->maxSimulLines = 0;
if (g_audioDeviceCache[desc->deviceID].isSource) {
DirectSoundEnumerate((LPDSENUMCALLBACK) DS_GetDescEnum, desc);
DirectSoundEnumerateW((LPDSENUMCALLBACKW) DS_GetDescEnum, desc);
strncpy(desc->description, "DirectSound Playback", DAUDIO_STRING_LENGTH);
} else {
DirectSoundCaptureEnumerate((LPDSENUMCALLBACK) DS_GetDescEnum, desc);
DirectSoundCaptureEnumerateW((LPDSENUMCALLBACKW) DS_GetDescEnum, desc);
strncpy(desc->description, "DirectSound Capture", DAUDIO_STRING_LENGTH);
}

View File

@ -31,6 +31,9 @@ extern "C" {
#include "PLATFORM_API_WinOS_Util.h"
}
/* include to prevent charset problem */
#include "PLATFORM_API_WinOS_Charset_Util.h"
#if USE_PLATFORM_MIDI_IN == TRUE
#ifdef USE_ERROR
@ -248,18 +251,17 @@ INT32 MIDI_IN_GetNumDevices() {
return (INT32) midiInGetNumDevs();
}
INT32 getMidiInCaps(INT32 deviceID, MIDIINCAPS* caps, INT32* err) {
(*err) = midiInGetDevCaps(deviceID, caps, sizeof(MIDIINCAPS));
INT32 getMidiInCaps(INT32 deviceID, MIDIINCAPSW* caps, INT32* err) {
(*err) = midiInGetDevCapsW(deviceID, caps, sizeof(MIDIINCAPS));
return ((*err) == MMSYSERR_NOERROR);
}
INT32 MIDI_IN_GetDeviceName(INT32 deviceID, char *name, UINT32 nameLength) {
MIDIINCAPS midiInCaps;
MIDIINCAPSW midiInCaps;
INT32 err;
if (getMidiInCaps(deviceID, &midiInCaps, &err)) {
strncpy(name, midiInCaps.szPname, nameLength-1);
name[nameLength-1] = 0;
UnicodeToUTF8AndCopy(name, midiInCaps.szPname, nameLength);
return MIDI_SUCCESS;
}
MIDIIN_CHECK_ERROR;
@ -279,7 +281,7 @@ INT32 MIDI_IN_GetDeviceDescription(INT32 deviceID, char *name, UINT32 nameLength
INT32 MIDI_IN_GetDeviceVersion(INT32 deviceID, char *name, UINT32 nameLength) {
MIDIINCAPS midiInCaps;
MIDIINCAPSW midiInCaps;
INT32 err = MIDI_NOT_SUPPORTED;
if (getMidiInCaps(deviceID, &midiInCaps, &err) && (nameLength>7)) {

View File

@ -28,6 +28,9 @@
#include "PLATFORM_API_WinOS_Util.h"
/* include to prevent charset problem */
#include "PLATFORM_API_WinOS_Charset_Util.h"
#if USE_PLATFORM_MIDI_OUT == TRUE
@ -66,24 +69,23 @@ INT32 MIDI_OUT_GetNumDevices() {
}
INT32 getMidiOutCaps(INT32 deviceID, MIDIOUTCAPS* caps, INT32* err) {
INT32 getMidiOutCaps(INT32 deviceID, MIDIOUTCAPSW* caps, INT32* err) {
if (deviceID == 0) {
deviceID = MIDI_MAPPER;
} else {
deviceID--;
}
(*err) = (INT32) midiOutGetDevCaps(deviceID, caps, sizeof(MIDIOUTCAPS));
(*err) = (INT32) midiOutGetDevCapsW(deviceID, caps, sizeof(MIDIOUTCAPS));
return ((*err) == MMSYSERR_NOERROR);
}
INT32 MIDI_OUT_GetDeviceName(INT32 deviceID, char *name, UINT32 nameLength) {
MIDIOUTCAPS midiOutCaps;
MIDIOUTCAPSW midiOutCaps;
INT32 err;
if (getMidiOutCaps(deviceID, &midiOutCaps, &err)) {
strncpy(name, midiOutCaps.szPname, nameLength-1);
name[nameLength-1] = 0;
UnicodeToUTF8AndCopy(name, midiOutCaps.szPname, nameLength);
return MIDI_SUCCESS;
}
MIDIOUT_CHECK_ERROR;
@ -97,7 +99,7 @@ INT32 MIDI_OUT_GetDeviceVendor(INT32 deviceID, char *name, UINT32 nameLength) {
INT32 MIDI_OUT_GetDeviceDescription(INT32 deviceID, char *name, UINT32 nameLength) {
MIDIOUTCAPS midiOutCaps;
MIDIOUTCAPSW midiOutCaps;
char *desc;
INT32 err;
@ -134,7 +136,7 @@ INT32 MIDI_OUT_GetDeviceDescription(INT32 deviceID, char *name, UINT32 nameLengt
INT32 MIDI_OUT_GetDeviceVersion(INT32 deviceID, char *name, UINT32 nameLength) {
MIDIOUTCAPS midiOutCaps;
MIDIOUTCAPSW midiOutCaps;
INT32 err;
if (getMidiOutCaps(deviceID, &midiOutCaps, &err) && nameLength>7) {

View File

@ -38,6 +38,9 @@
#include <mmsystem.h>
#include "Ports.h"
/* include to prevent charset problem */
#include "PLATFORM_API_WinOS_Charset_Util.h"
#if USE_PORTS == TRUE
typedef struct tag_PortControlID PortControlID;
@ -353,10 +356,9 @@ int lineHasControls(HMIXER handle, MIXERLINE* line, MIXERLINECONTROLS* controls)
///// implemented functions of Ports.h
INT32 PORT_GetPortMixerDescription(INT32 mixerIndex, PortMixerDescription* description) {
MIXERCAPS mixerCaps;
if (mixerGetDevCaps(mixerIndex, &mixerCaps, sizeof(MIXERCAPS)) == MMSYSERR_NOERROR) {
strncpy(description->name, mixerCaps.szPname, PORT_STRING_LENGTH-1);
description->name[PORT_STRING_LENGTH-1] = 0;
MIXERCAPSW mixerCaps;
if (mixerGetDevCapsW(mixerIndex, &mixerCaps, sizeof(MIXERCAPS)) == MMSYSERR_NOERROR) {
UnicodeToUTF8AndCopy(description->name, mixerCaps.szPname, PORT_STRING_LENGTH);
sprintf(description->version, "%d.%d", (mixerCaps.vDriverVersion & 0xFF00) >> 8, mixerCaps.vDriverVersion & 0xFF);
strncpy(description->description, "Port Mixer", PORT_STRING_LENGTH-1);
return TRUE;