diff --git a/libusb/hid.c b/libusb/hid.c index 3c6d877f..aaf3231d 100644 --- a/libusb/hid.c +++ b/libusb/hid.c @@ -182,7 +182,7 @@ static int return_data(hid_device *dev, unsigned char *data, size_t length); static hid_device *new_hid_device(void) { - hid_device *dev = calloc(1, sizeof(hid_device)); + hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device)); dev->blocking = 1; pthread_mutex_init(&dev->mutex, NULL); @@ -430,7 +430,7 @@ static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx) Skip over the first character (2-bytes). */ len -= 2; - str = malloc((len / 2 + 1) * sizeof(wchar_t)); + str = (wchar_t*) malloc((len / 2 + 1) * sizeof(wchar_t)); int i; for (i = 0; i < len / 2; i++) { str[i] = buf[i * 2 + 2] | (buf[i * 2 + 3] << 8); @@ -563,7 +563,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, struct hid_device_info *tmp; /* VID/PID match. Create the record. */ - tmp = calloc(1, sizeof(struct hid_device_info)); + tmp = (struct hid_device_info*) calloc(1, sizeof(struct hid_device_info)); if (cur_dev) { cur_dev->next = tmp; } @@ -736,8 +736,8 @@ static void read_callback(struct libusb_transfer *transfer) if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { - struct input_report *rpt = malloc(sizeof(*rpt)); - rpt->data = malloc(transfer->actual_length); + struct input_report *rpt = (struct input_report*) malloc(sizeof(*rpt)); + rpt->data = (uint8_t *) malloc(transfer->actual_length); memcpy(rpt->data, transfer->buffer, transfer->actual_length); rpt->len = transfer->actual_length; rpt->next = NULL; @@ -799,11 +799,11 @@ static void read_callback(struct libusb_transfer *transfer) static void *read_thread(void *param) { hid_device *dev = param; - unsigned char *buf; + uint8_t *buf; const size_t length = dev->input_ep_max_packet_size; /* Set up the transfer object. */ - buf = malloc(length); + buf = (uint8_t*) malloc(length); dev->transfer = libusb_alloc_transfer(0); libusb_fill_interrupt_transfer(dev->transfer, dev->device_handle, diff --git a/linux/hid.c b/linux/hid.c index 56dac0fa..93beda04 100644 --- a/linux/hid.c +++ b/linux/hid.c @@ -103,7 +103,7 @@ static __u32 detect_kernel_version(void) static hid_device *new_hid_device(void) { - hid_device *dev = calloc(1, sizeof(hid_device)); + hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device)); dev->device_handle = -1; dev->blocking = 1; dev->uses_numbered_reports = 0; @@ -122,7 +122,7 @@ static wchar_t *utf8_to_wchar_t(const char *utf8) if ((size_t) -1 == wlen) { return wcsdup(L""); } - ret = calloc(wlen+1, sizeof(wchar_t)); + ret = (wchar_t*) calloc(wlen+1, sizeof(wchar_t)); mbstowcs(ret, utf8, wlen+1); ret[wlen] = 0x0000; } @@ -461,7 +461,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, struct hid_device_info *tmp; /* VID/PID match. Create the record. */ - tmp = malloc(sizeof(struct hid_device_info)); + tmp = (struct hid_device_info*) malloc(sizeof(struct hid_device_info)); if (cur_dev) { cur_dev->next = tmp; } diff --git a/mac/hid.c b/mac/hid.c index e0756a15..722d3d58 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -124,7 +124,7 @@ struct hid_device_ { static hid_device *new_hid_device(void) { - hid_device *dev = calloc(1, sizeof(hid_device)); + hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device)); dev->device_handle = NULL; dev->blocking = 1; dev->uses_numbered_reports = 0; @@ -281,7 +281,7 @@ static int get_product_string(IOHIDDeviceRef device, wchar_t *buf, size_t len) static wchar_t *dup_wcs(const wchar_t *s) { size_t len = wcslen(s); - wchar_t *ret = malloc((len+1)*sizeof(wchar_t)); + wchar_t *ret = (wchar_t*) malloc((len+1)*sizeof(wchar_t)); wcscpy(ret, s); return ret; @@ -410,7 +410,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, /* Convert the list into a C array so we can iterate easily. */ num_devices = CFSetGetCount(device_set); - IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef)); + IOHIDDeviceRef *device_array = (IOHIDDeviceRef*) calloc(num_devices, sizeof(IOHIDDeviceRef)); CFSetGetValues(device_set, (const void **) device_array); /* Iterate over each device, making an entry for it. */ @@ -437,7 +437,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, io_string_t path; /* VID/PID match. Create the record. */ - tmp = malloc(sizeof(struct hid_device_info)); + tmp = (struct hid_device_info*) malloc(sizeof(struct hid_device_info)); if (cur_dev) { cur_dev->next = tmp; } @@ -561,8 +561,8 @@ static void hid_report_callback(void *context, IOReturn result, void *sender, hid_device *dev = context; /* Make a new Input Report object */ - rpt = calloc(1, sizeof(struct input_report)); - rpt->data = calloc(1, report_length); + rpt = (input_report*) calloc(1, sizeof(struct input_report)); + rpt->data = (uint8_t*) calloc(1, report_length); memcpy(rpt->data, report, report_length); rpt->len = report_length; rpt->next = NULL; @@ -710,7 +710,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) /* Create the buffers for receiving data */ dev->max_input_report_len = (CFIndex) get_max_report_length(dev->device_handle); - dev->input_report_buf = calloc(dev->max_input_report_len, sizeof(uint8_t)); + dev->input_report_buf = (uint8_t*) calloc(dev->max_input_report_len, sizeof(uint8_t)); /* Create the Run Loop Mode for this device. printing the reference seems to work. */ diff --git a/windows/hid.c b/windows/hid.c index 86810d7e..33fbe48c 100755 --- a/windows/hid.c +++ b/windows/hid.c @@ -225,10 +225,10 @@ static int lookup_functions() } #endif -static HANDLE open_device(const char *path, BOOL enumerate) +static HANDLE open_device(const char *path, BOOL open_rw) { HANDLE handle; - DWORD desired_access = (enumerate)? 0: (GENERIC_WRITE | GENERIC_READ); + DWORD desired_access = (open_rw)? (GENERIC_WRITE | GENERIC_READ): 0; DWORD share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE; handle = CreateFileA(path, @@ -370,7 +370,7 @@ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned shor //wprintf(L"HandleName: %s\n", device_interface_detail_data->DevicePath); /* Open a handle to the device */ - write_handle = open_device(device_interface_detail_data->DevicePath, TRUE); + write_handle = open_device(device_interface_detail_data->DevicePath, FALSE); /* Check validity of write_handle. */ if (write_handle == INVALID_HANDLE_VALUE) { @@ -566,13 +566,23 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path) dev = new_hid_device(); /* Open a handle to the device */ - dev->device_handle = open_device(path, FALSE); + dev->device_handle = open_device(path, TRUE); /* Check validity of write_handle. */ if (dev->device_handle == INVALID_HANDLE_VALUE) { - /* Unable to open the device. */ - register_error(dev, "CreateFile"); - goto err; + /* System devices, such as keyboards and mice, cannot be opened in + read-write mode, because the system takes exclusive control over + them. This is to prevent keyloggers. However, feature reports + can still be sent and received. Retry opening the device, but + without read/write access. */ + dev->device_handle = open_device(path, FALSE); + + /* Check the validity of the limited device_handle. */ + if (dev->device_handle == INVALID_HANDLE_VALUE) { + /* Unable to open the device, even without read-write mode. */ + register_error(dev, "CreateFile"); + goto err; + } } /* Set the Input Report buffer size to 64 reports. */