Merge "adb: win32: Unicode USB device names"

This commit is contained in:
Elliott Hughes 2015-11-13 16:41:31 +00:00 committed by Gerrit Code Review
commit 76aefd5f9b

View file

@ -55,7 +55,7 @@ struct usb_handle {
ADBAPIHANDLE adb_write_pipe; ADBAPIHANDLE adb_write_pipe;
/// Interface name /// Interface name
char* interface_name; wchar_t* interface_name;
/// Mask for determining when to use zero length packets /// Mask for determining when to use zero length packets
unsigned zero_mask; unsigned zero_mask;
@ -74,11 +74,11 @@ static usb_handle handle_list = {
ADB_MUTEX_DEFINE( usb_lock ); ADB_MUTEX_DEFINE( usb_lock );
/// Checks if there is opened usb handle in handle_list for this device. /// Checks if there is opened usb handle in handle_list for this device.
int known_device(const char* dev_name); int known_device(const wchar_t* dev_name);
/// Checks if there is opened usb handle in handle_list for this device. /// Checks if there is opened usb handle in handle_list for this device.
/// usb_lock mutex must be held before calling this routine. /// usb_lock mutex must be held before calling this routine.
int known_device_locked(const char* dev_name); int known_device_locked(const wchar_t* dev_name);
/// Registers opened usb handle (adds it to handle_list). /// Registers opened usb handle (adds it to handle_list).
int register_new_device(usb_handle* handle); int register_new_device(usb_handle* handle);
@ -118,7 +118,7 @@ void usb_kick(usb_handle* handle);
/// Closes opened usb handle /// Closes opened usb handle
int usb_close(usb_handle* handle); int usb_close(usb_handle* handle);
int known_device_locked(const char* dev_name) { int known_device_locked(const wchar_t* dev_name) {
usb_handle* usb; usb_handle* usb;
if (NULL != dev_name) { if (NULL != dev_name) {
@ -126,7 +126,7 @@ int known_device_locked(const char* dev_name) {
for(usb = handle_list.next; usb != &handle_list; usb = usb->next) { for(usb = handle_list.next; usb != &handle_list; usb = usb->next) {
// In Windows names are not case sensetive! // In Windows names are not case sensetive!
if((NULL != usb->interface_name) && if((NULL != usb->interface_name) &&
(0 == stricmp(usb->interface_name, dev_name))) { (0 == wcsicmp(usb->interface_name, dev_name))) {
return 1; return 1;
} }
} }
@ -135,7 +135,7 @@ int known_device_locked(const char* dev_name) {
return 0; return 0;
} }
int known_device(const char* dev_name) { int known_device(const wchar_t* dev_name) {
int ret = 0; int ret = 0;
if (NULL != dev_name) { if (NULL != dev_name) {
@ -316,17 +316,16 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
AdbGetInterfaceName(ret->adb_interface, AdbGetInterfaceName(ret->adb_interface,
NULL, NULL,
&name_len, &name_len,
true); false);
if (0 == name_len) { if (0 == name_len) {
D("AdbGetInterfaceName returned name length of zero: %s", D("AdbGetInterfaceName returned name length of zero: %s",
SystemErrorCodeToString(GetLastError()).c_str()); SystemErrorCodeToString(GetLastError()).c_str());
goto fail; goto fail;
} }
ret->interface_name = (char*)malloc(name_len); ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0]));
if (NULL == ret->interface_name) { if (NULL == ret->interface_name) {
D("Could not allocate %lu bytes for interface_name: %s", name_len, D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno));
strerror(errno));
goto fail; goto fail;
} }
@ -334,7 +333,7 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
if (!AdbGetInterfaceName(ret->adb_interface, if (!AdbGetInterfaceName(ret->adb_interface,
ret->interface_name, ret->interface_name,
&name_len, &name_len,
true)) { false)) {
D("AdbGetInterfaceName failed: %s", D("AdbGetInterfaceName failed: %s",
SystemErrorCodeToString(GetLastError()).c_str()); SystemErrorCodeToString(GetLastError()).c_str());
goto fail; goto fail;
@ -581,12 +580,10 @@ int recognized_device(usb_handle* handle) {
} }
void find_devices() { void find_devices() {
usb_handle* handle = NULL; usb_handle* handle = NULL;
char entry_buffer[2048]; char entry_buffer[2048];
char interf_name[2048];
AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
unsigned long entry_buffer_size = sizeof(entry_buffer); unsigned long entry_buffer_size = sizeof(entry_buffer);
char* copy_name;
// Enumerate all present and active interfaces. // Enumerate all present and active interfaces.
ADBAPIHANDLE enum_handle = ADBAPIHANDLE enum_handle =
@ -599,25 +596,21 @@ void find_devices() {
} }
while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
// TODO: FIXME - temp hack converting wchar_t into char.
// It would be better to change AdbNextInterface so it will return
// interface name as single char string.
const wchar_t* wchar_name = next_interface->device_name;
for(copy_name = interf_name;
L'\0' != *wchar_name;
wchar_name++, copy_name++) {
*copy_name = (char)(*wchar_name);
}
*copy_name = '\0';
// Lets see if we already have this device in the list // Lets see if we already have this device in the list
if (!known_device(interf_name)) { if (!known_device(next_interface->device_name)) {
// This seems to be a new device. Open it! // This seems to be a new device. Open it!
handle = do_usb_open(next_interface->device_name); handle = do_usb_open(next_interface->device_name);
if (NULL != handle) { if (NULL != handle) {
// Lets see if this interface (device) belongs to us // Lets see if this interface (device) belongs to us
if (recognized_device(handle)) { if (recognized_device(handle)) {
D("adding a new device %s", interf_name); D("adding a new device %ls", next_interface->device_name);
// We don't request a wchar_t string from AdbGetSerialNumber() because of a bug in
// adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString, bytes_written) where the
// last parameter should be (str_len * sizeof(wchar_t)). The bug reads 2 bytes past the
// end of a stack buffer in the best case, and in the unlikely case of a long serial
// number, it will read 2 bytes past the end of a heap allocation. This doesn't affect the
// resulting string, but we should avoid the bad reads in the first place.
char serial_number[512]; char serial_number[512];
unsigned long serial_number_len = sizeof(serial_number); unsigned long serial_number_len = sizeof(serial_number);
if (AdbGetSerialNumber(handle->adb_interface, if (AdbGetSerialNumber(handle->adb_interface,
@ -628,7 +621,7 @@ void find_devices() {
if (register_new_device(handle)) { if (register_new_device(handle)) {
register_usb_transport(handle, serial_number, NULL, 1); register_usb_transport(handle, serial_number, NULL, 1);
} else { } else {
D("register_new_device failed for %s", interf_name); D("register_new_device failed for %ls", next_interface->device_name);
usb_cleanup_handle(handle); usb_cleanup_handle(handle);
free(handle); free(handle);
} }