diff --git a/mac/hid.c b/mac/hid.c index 276541b..d55eef0 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -263,16 +263,25 @@ static int32_t get_max_report_length(IOHIDDeviceRef device) static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t *buf, size_t len) { - CFStringRef str = IOHIDDeviceGetProperty(device, prop); + CFStringRef str; + + if (!len) + return 0; - buf[0] = 0x0000; + str = IOHIDDeviceGetProperty(device, prop); + + buf[0] = 0; if (str) { + len --; + + CFIndex str_len = CFStringGetLength(str); CFRange range; range.location = 0; - range.length = len; + range.length = (str_len > len)? len: str_len; CFIndex used_buf_len; - CFStringGetBytes(str, + CFIndex chars_copied; + chars_copied = CFStringGetBytes(str, range, kCFStringEncodingUTF32LE, (char)'?', @@ -280,8 +289,9 @@ static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t (UInt8*)buf, len, &used_buf_len); - buf[len-1] = 0x00000000; - return used_buf_len; + + buf[chars_copied] = 0; + return chars_copied; } else return 0; @@ -290,16 +300,24 @@ static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t static int get_string_property_utf8(IOHIDDeviceRef device, CFStringRef prop, char *buf, size_t len) { - CFStringRef str = IOHIDDeviceGetProperty(device, prop); + CFStringRef str; + if (!len) + return 0; - buf[0] = 0x0000; + str = IOHIDDeviceGetProperty(device, prop); + + buf[0] = 0; if (str) { + len--; + + CFIndex str_len = CFStringGetLength(str); CFRange range; range.location = 0; - range.length = len; + range.length = (str_len > len)? len: str_len; CFIndex used_buf_len; - CFStringGetBytes(str, + CFIndex chars_copied; + chars_copied = CFStringGetBytes(str, range, kCFStringEncodingUTF8, (char)'?', @@ -307,12 +325,12 @@ static int get_string_property_utf8(IOHIDDeviceRef device, CFStringRef prop, cha (UInt8*)buf, len, &used_buf_len); - buf[len-1] = 0x00000000; + + buf[chars_copied] = 0; return used_buf_len; } else return 0; - }