Check all values returned from dictionaries.

This commit is contained in:
Steve Checkoway
2006-02-10 09:36:35 +00:00
parent dfa720254d
commit 3f199dbf2a
2 changed files with 19 additions and 13 deletions
+16 -12
View File
@@ -36,6 +36,7 @@ bool HIDDevice::Open( io_object_t device )
IOReturn ret; IOReturn ret;
CFMutableDictionaryRef properties; CFMutableDictionaryRef properties;
kern_return_t result; kern_return_t result;
CFTypeRef object;
result = IORegistryEntryCreateCFProperties( device, &properties, kCFAllocatorDefault, kNilOptions ); result = IORegistryEntryCreateCFProperties( device, &properties, kCFAllocatorDefault, kNilOptions );
if ( result != KERN_SUCCESS || !properties ) if ( result != KERN_SUCCESS || !properties )
@@ -44,14 +45,14 @@ bool HIDDevice::Open( io_object_t device )
return false; return false;
} }
CFStringRef productRef = (CFStringRef)CFDictionaryGetValue( properties, CFSTR(kIOHIDProductKey) ); object = CFDictionaryGetValue( properties, CFSTR(kIOHIDProductKey) );
if( productRef ) if( object && CFGetTypeID(object) == CFStringGetTypeID() )
m_sDescription = CFStringGetCStringPtr( productRef, CFStringGetSystemEncoding() ); m_sDescription = CFStringGetCStringPtr( CFStringRef(object), CFStringGetSystemEncoding() );
if( m_sDescription == "" ) if( m_sDescription == "" )
{ {
CFTypeRef vidRef = (CFTypeRef)CFDictionaryGetValue( properties, CFSTR(kIOHIDVendorIDKey) ); CFTypeRef vidRef = CFDictionaryGetValue( properties, CFSTR(kIOHIDVendorIDKey) );
CFTypeRef pidRef = (CFTypeRef)CFDictionaryGetValue( properties, CFSTR(kIOHIDProductIDKey) ); CFTypeRef pidRef = CFDictionaryGetValue( properties, CFSTR(kIOHIDProductIDKey) );
int vid, pid; int vid, pid;
if( vidRef && !IntValue(vidRef, vid) ) if( vidRef && !IntValue(vidRef, vid) )
@@ -61,13 +62,14 @@ bool HIDDevice::Open( io_object_t device )
m_sDescription = ssprintf( "%04x:%04x", vid, pid ); m_sDescription = ssprintf( "%04x:%04x", vid, pid );
} }
CFArrayRef logicalDevices; object = CFDictionaryGetValue( properties, CFSTR(kIOHIDElementKey) );
if ( !object || CFGetTypeID(object) != CFArrayGetTypeID() )
if ( !(logicalDevices = (CFArrayRef)CFDictionaryGetValue(properties, CFSTR(kIOHIDElementKey))) )
{ {
CFRelease( properties ); CFRelease( properties );
return false; return false;
} }
CFArrayRef logicalDevices = CFArrayRef( object );
CFRange r = { 0, CFArrayGetCount(logicalDevices) }; CFRange r = { 0, CFArrayGetCount(logicalDevices) };
CFArrayApplyFunction( logicalDevices, r, HIDDevice::AddLogicalDevice, this ); CFArrayApplyFunction( logicalDevices, r, HIDDevice::AddLogicalDevice, this );
@@ -177,7 +179,6 @@ void HIDDevice::AddLogicalDevice( const void *value, void *context )
CFDictionaryRef properties = CFDictionaryRef( value ); CFDictionaryRef properties = CFDictionaryRef( value );
HIDDevice *This = (HIDDevice *)context; HIDDevice *This = (HIDDevice *)context;
CFArrayRef elements;
CFTypeRef object; CFTypeRef object;
int usage, usagePage; int usage, usagePage;
CFTypeID numID = CFNumberGetTypeID(); CFTypeID numID = CFNumberGetTypeID();
@@ -192,12 +193,14 @@ void HIDDevice::AddLogicalDevice( const void *value, void *context )
if( !object || CFGetTypeID(object) != numID || !IntValue(object, usage) ) if( !object || CFGetTypeID(object) != numID || !IntValue(object, usage) )
return; return;
if( !(elements = (CFArrayRef)CFDictionaryGetValue( properties, CFSTR(kIOHIDElementKey))) ) object = CFDictionaryGetValue( properties, CFSTR(kIOHIDElementKey) );
if( !object || CFGetTypeID(object) != CFArrayGetTypeID() )
return; return;
if( !This->AddLogicalDevice(usagePage, usage) ) if( !This->AddLogicalDevice(usagePage, usage) )
return; return;
CFArrayRef elements = CFArrayRef( object );
CFRange r = { 0, CFArrayGetCount(elements) }; CFRange r = { 0, CFArrayGetCount(elements) };
CFArrayApplyFunction( elements, r, HIDDevice::AddElement, This ); CFArrayApplyFunction( elements, r, HIDDevice::AddElement, This );
@@ -213,11 +216,12 @@ void HIDDevice::AddElement( const void *value, void *context )
CFTypeRef object; CFTypeRef object;
CFTypeID numID = CFNumberGetTypeID(); CFTypeID numID = CFNumberGetTypeID();
int cookie, usage, usagePage; int cookie, usage, usagePage;
CFArrayRef elements;
// Recursively add elements // Recursively add elements
if( (elements = (CFArrayRef)CFDictionaryGetValue(properties, CFSTR(kIOHIDElementKey))) ) object = CFDictionaryGetValue( properties, CFSTR(kIOHIDElementKey) );
if( object && CFGetTypeID(object) == CFArrayGetTypeID() )
{ {
CFArrayRef elements = CFArrayRef( object );
CFRange r = { 0, CFArrayGetCount(elements) }; CFRange r = { 0, CFArrayGetCount(elements) };
CFArrayApplyFunction( elements, r, AddElement, context ); CFArrayApplyFunction( elements, r, AddElement, context );
+3 -1
View File
@@ -28,8 +28,10 @@ inline void PrintIOErr( IOReturn err, const char *s )
LOG->Warn( "%s - %s(%x,%d)", s, mach_error_string(err), err, err & 0xFFFFFF ); LOG->Warn( "%s - %s(%x,%d)", s, mach_error_string(err), err, err & 0xFFFFFF );
} }
inline Boolean IntValue( const void *o, int &n ) inline Boolean IntValue( CFTypeRef o, int &n )
{ {
if( CFGetTypeID(o) != CFNumberGetTypeID() )
return false;
return CFNumberGetValue( CFNumberRef(o), kCFNumberIntType, &n ); return CFNumberGetValue( CFNumberRef(o), kCFNumberIntType, &n );
} }