USB string descriptors are not UTF8, so it is not safe to treat them as such.

Add AndroidRuntime::NewStringLatin1() to convert non-UTF8 strings to Java strings.

Bug: 17427781
Change-Id: I7df1d4e94a7beebc8b1a74c0c0a163b794025ae8
This commit is contained in:
Mike Lockwood
2014-09-08 16:15:21 -07:00
committed by Michael Wright
parent 3afd6e3fc6
commit 9ee5e7e17f
3 changed files with 22 additions and 5 deletions

View File

@@ -925,6 +925,20 @@ char* AndroidRuntime::toSlashClassName(const char* className)
return result;
}
/** Create a Java string from an ASCII or Latin-1 string */
jstring AndroidRuntime::NewStringLatin1(JNIEnv* env, const char* bytes) {
if (!bytes) return NULL;
int length = strlen(bytes);
jchar* buffer = (jchar *)alloca(length * sizeof(jchar));
if (!buffer) return NULL;
jchar* chp = buffer;
for (int i = 0; i < length; i++) {
*chp++ = *bytes++;
}
return env->NewString(buffer, length);
}
/*
* Start the Android runtime. This involves starting the virtual machine
* and calling the "static void main(String[] args)" method in the class

View File

@@ -114,6 +114,9 @@ public:
/** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
static char* toSlashClassName(const char* className);
/** Create a Java string from an ASCII or Latin-1 string */
static jstring NewStringLatin1(JNIEnv* env, const char* bytes);
private:
static int startReg(JNIEnv* env);
void addOption(const char* optionString, void* extra_info = NULL);

View File

@@ -77,9 +77,9 @@ static int usb_device_added(const char *devname, void* client_data) {
char *serial = usb_device_get_serial(device);
jstring deviceName = env->NewStringUTF(devname);
jstring manufacturerName = env->NewStringUTF(manufacturer);
jstring productName = env->NewStringUTF(product);
jstring serialNumber = env->NewStringUTF(serial);
jstring manufacturerName = AndroidRuntime::NewStringLatin1(env, manufacturer);
jstring productName = AndroidRuntime::NewStringLatin1(env, product);
jstring serialNumber = AndroidRuntime::NewStringLatin1(env, serial);
jboolean result = env->CallBooleanMethod(thiz, method_beginUsbDeviceAdded,
deviceName, usb_device_get_vendor_id(device), usb_device_get_product_id(device),
@@ -102,7 +102,7 @@ static int usb_device_added(const char *devname, void* client_data) {
if (desc->bDescriptorType == USB_DT_CONFIG) {
struct usb_config_descriptor *config = (struct usb_config_descriptor *)desc;
char *name = usb_device_get_string(device, config->iConfiguration);
jstring configName = env->NewStringUTF(name);
jstring configName = AndroidRuntime::NewStringLatin1(env, name);
env->CallVoidMethod(thiz, method_addUsbConfiguration,
config->bConfigurationValue, configName, config->bmAttributes,
@@ -113,7 +113,7 @@ static int usb_device_added(const char *devname, void* client_data) {
} else if (desc->bDescriptorType == USB_DT_INTERFACE) {
struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
char *name = usb_device_get_string(device, interface->iInterface);
jstring interfaceName = env->NewStringUTF(name);
jstring interfaceName = AndroidRuntime::NewStringLatin1(env, name);
env->CallVoidMethod(thiz, method_addUsbInterface,
interface->bInterfaceNumber, interfaceName, interface->bAlternateSetting,