windows: Set maximum buffer for USB strings to 0xFFF wide characters

Limit the size of the buffer passed to hid_get_manufacturer_string(),
hid_get_product_string(), and hid_get_serial_number_string() because
calling the underlying win32 functions with larger buffers causes them
to fail.
This commit is contained in:
Austin Mullins 2016-06-19 23:22:08 -05:00 committed by Alan Ott
parent ae15b65ed9
commit 901512e88b

View File

@ -36,6 +36,10 @@ typedef LONG NTSTATUS;
#define _wcsdup wcsdup
#endif
/* The maximum number of characters that can be passed into the
HidD_Get*String() functions without it failing.*/
#define MAX_STRING_WCHARS 0xFFF
/*#define HIDAPI_USE_DDK*/
#ifdef __cplusplus
@ -62,6 +66,9 @@ extern "C" {
#include "hidapi.h"
#undef MIN
#define MIN(x,y) ((x) < (y)? (x): (y))
#ifdef _MSC_VER
/* Thanks Microsoft, but I know how to use strncpy(). */
#pragma warning(disable:4996)
@ -811,7 +818,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev
{
BOOL res;
res = HidD_GetManufacturerString(dev->device_handle, string, sizeof(wchar_t) * maxlen);
res = HidD_GetManufacturerString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS));
if (!res) {
register_error(dev, "HidD_GetManufacturerString");
return -1;
@ -824,7 +831,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wch
{
BOOL res;
res = HidD_GetProductString(dev->device_handle, string, sizeof(wchar_t) * maxlen);
res = HidD_GetProductString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS));
if (!res) {
register_error(dev, "HidD_GetProductString");
return -1;
@ -837,7 +844,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *de
{
BOOL res;
res = HidD_GetSerialNumberString(dev->device_handle, string, sizeof(wchar_t) * maxlen);
res = HidD_GetSerialNumberString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS));
if (!res) {
register_error(dev, "HidD_GetSerialNumberString");
return -1;
@ -850,7 +857,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int
{
BOOL res;
res = HidD_GetIndexedString(dev->device_handle, string_index, string, sizeof(wchar_t) * maxlen);
res = HidD_GetIndexedString(dev->device_handle, string_index, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS));
if (!res) {
register_error(dev, "HidD_GetIndexedString");
return -1;