Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit cast from void* to corresponding type #430

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions linux/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions mac/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
24 changes: 17 additions & 7 deletions windows/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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. */
Expand Down