void destroyCertData(CertDataRef pCertData) { if (pCertData->subject) { CFArrayApplyFunction(pCertData->subject, CFRangeMake(0, CFArrayGetCount(pCertData->subject)), arrayDestroyCertName, NULL); CFRelease(pCertData->subject); } if (pCertData->issuer) { CFArrayApplyFunction(pCertData->issuer, CFRangeMake(0, CFArrayGetCount(pCertData->issuer)), arrayDestroyCertName, NULL); CFRelease(pCertData->issuer); } if (pCertData->sha1) CFRelease(pCertData->sha1); if (pCertData->md5) CFRelease(pCertData->md5); if (pCertData->serial) CFRelease(pCertData->serial); free(pCertData); }
//////////////////////////////////////////////////////////// /// Enumerate all elements of a joystick/gamepad controller //////////////////////////////////////////////////////////// bool JoystickSupport::EnumerateElements(CFDictionaryRef dictionary, JoystickDevice *joystick) { CFTypeRef Object; CFTypeID Type; Object = CFDictionaryGetValue(dictionary, CFSTR(kIOHIDElementKey)); if (Object) { Type = CFGetTypeID(Object); if (Type == CFArrayGetTypeID()) { CFRange Range; Range.location = 0; Range.length = CFArrayGetCount((CFArrayRef)Object); CFArrayApplyFunction((CFArrayRef)Object, Range, (CFArrayApplierFunction)JoystickSupport::ElementArrayCallback, (void*)joystick); } else if (Type == CFDictionaryGetTypeID()) { ValidateElement((CFDictionaryRef)Object, joystick); } } return 1; }
static void printObj(CFPropertyListRef obj, struct printContext* c) { CFTypeID typeID = CFGetTypeID(obj); if (typeID == _CFKeyedArchiverUIDGetTypeID()) { unsigned uid = _CFKeyedArchiverUIDGetValue(obj); CFPropertyListRef refObj = CFArrayGetValueAtIndex(c->objs, uid); if (CFEqual(refObj, CFSTR("$null"))) printf("nil"); else if (c->refCount[uid] > 1 && isComplexObj(refObj)) printf("{CF$UID = %u;}", uid); else printObj(refObj, c); } else if (typeID == CFArrayGetTypeID()) { printf("(\n"); ++ c->tabs; CFArrayApplyFunction(obj, CFRangeMake(0, CFArrayGetCount(obj)), (CFArrayApplierFunction)&printObjAsArrayEntry, c); -- c->tabs; for (unsigned i = 0; i < c->tabs; ++ i) printf("\t"); printf(")"); } else if (typeID == CFDictionaryGetTypeID()) { CFStringRef className = CFDictionaryGetValue(obj, CFSTR("$classname")); if (className != NULL) printObjAsClassName(className); else { printf("{\n"); ++ c->tabs; CFIndex dictCount = CFDictionaryGetCount(obj); struct dictionarySorterContext sc; sc.keys = malloc(sizeof(CFStringRef)*dictCount); sc.values = malloc(sizeof(CFPropertyListRef)*dictCount); unsigned* mapping = malloc(sizeof(unsigned)*dictCount); for (unsigned i = 0; i < dictCount; ++ i) mapping[i] = i; CFDictionaryGetKeysAndValues(obj, (const void**)sc.keys, sc.values); qsort_r(mapping, dictCount, sizeof(unsigned), &sc, (int(*)(void*,const void*,const void*))&dictionaryComparer); for (unsigned i = 0; i < dictCount; ++ i) printObjAsDictionaryEntry(sc.keys[mapping[i]], sc.values[mapping[i]], c); free(mapping); free(sc.keys); free(sc.values); -- c->tabs; for (unsigned i = 0; i < c->tabs; ++ i) printf("\t"); printf("}"); } } else if (typeID == CFDataGetTypeID()) printObjAsData(obj); else if (typeID == CFNumberGetTypeID()) printObjAsNumber(obj); else if (typeID == CFStringGetTypeID()) printObjAsString(obj); else if (typeID == CFBooleanGetTypeID()) printf(CFBooleanGetValue(obj) ? "true" : "false"); else if (typeID == CFDateGetTypeID()) printf("/*date*/ %0.09g", CFDateGetAbsoluteTime(obj)); }
static void HIDGetElements (CFTypeRef refElementCurrent, recDevice *pDevice) { CFTypeID type = CFGetTypeID (refElementCurrent); if (type == CFArrayGetTypeID()) /* if element is an array */ { CFRange range = {0, CFArrayGetCount (refElementCurrent)}; /* CountElementsCFArrayHandler called for each array member */ CFArrayApplyFunction (refElementCurrent, range, HIDGetElementsCFArrayHandler, pDevice); } }
static void refCounter(CFPropertyListRef obj, int* refCount) { CFTypeID typeID = CFGetTypeID(obj); if (typeID == _CFKeyedArchiverUIDGetTypeID()) ++ refCount[_CFKeyedArchiverUIDGetValue(obj)]; else if (typeID == CFDictionaryGetTypeID()) { CFDictionaryApplyFunction(obj, (CFDictionaryApplierFunction)&refCounter2, refCount); } else if (typeID == CFArrayGetTypeID()) CFArrayApplyFunction(obj, CFRangeMake(0, CFArrayGetCount(obj)), (CFArrayApplierFunction)&refCounter, refCount); // all other stuff won't affect the refcount. }
CertDataRef createCertDataFromString(const char *description) { CertDataRef pCertData = (CertDataRef)malloc(sizeof(CertData)); pCertData->subject = CFArrayCreateMutable(NULL, 0, NULL); pCertData->issuer = CFArrayCreateMutable(NULL, 0, NULL); pCertData->sha1 = NULL; pCertData->md5 = NULL; pCertData->serial = NULL; CFArrayRef pairs = createDescDataPairs(description); for (int i=0; i<CFArrayGetCount(pairs); i++) if (!arrayParseDescDataPair(CFArrayGetValueAtIndex(pairs, i), pCertData)) { arrayDestroyDescData(pCertData, NULL); CFArrayApplyFunction(pairs, CFRangeMake(0, CFArrayGetCount(pairs)), arrayDestroyDescData, NULL); CFRelease(pairs); return 0; } CFArrayApplyFunction(pairs, CFRangeMake(0, CFArrayGetCount(pairs)), arrayDestroyDescData, NULL); CFRelease(pairs); return pCertData; }
static CFTypeRef GetDictionaryValueUsingKeys( CFDictionaryRef dict, CFArrayRef keysArray, CFTypeID typeID ) { CFArrayApplyFunction( keysArray, CFRangeMake(0, CFArrayGetCount(keysArray)), GetDictionaryValueUsingKeysApplier, &dict ); if ( dict && (CFGetTypeID(dict) != typeID) ) dict = 0; return dict; }
TStringIRep* TSICTStringCreateWithArrayAndFormat(CFArrayRef array, TSITStringFormat format) { CFRetain(array); CFMutableDataRef buffer = CFDataCreateMutable(kCFAllocatorDefault, 0); CFRange all = CFRangeMake(0, CFArrayGetCount(array)); TStringCollectionCallbackContext cx = {buffer, format}; CFArrayApplyFunction(array, all, ArrayBufferAppendCallback, &cx); TStringIRep* rep = TSICTStringCreateWithDataOfTypeAndFormat(buffer, kTSITStringTagList, format); CFRelease(buffer); CFRelease(array); return rep; }
void extractCertificatesFromIdentities(CFTypeRef identityOrArray, CFArrayRef *certificateArrayOut) { if (!identityOrArray || !certificateArrayOut) return; CFIndex cnt = (CFGetTypeID(identityOrArray)==CFArrayGetTypeID())?CFArrayGetCount((CFArrayRef)identityOrArray):1; CFMutableArrayRef certificateArray = CFArrayCreateMutable(kCFAllocatorDefault, cnt, &kCFTypeArrayCallBacks); if (CFGetTypeID(identityOrArray)==CFArrayGetTypeID()) CFArrayApplyFunction((CFArrayRef)identityOrArray, CFRangeMake(0, cnt), extract_certificate_from_identity, certificateArray); else extract_certificate_from_identity(identityOrArray, certificateArray); *certificateArrayOut = certificateArray; }
static SecCertificateSourceRef SecMemoryCertificateSourceCreate( CFArrayRef certificates) { SecMemoryCertificateSourceRef result = (SecMemoryCertificateSourceRef) malloc(sizeof(*result)); result->base.copyParents = SecMemoryCertificateSourceCopyParents; result->base.contains = SecMemoryCertificateSourceContains; CFIndex count = CFArrayGetCount(certificates); result->certificates = CFSetCreateMutable(kCFAllocatorDefault, count, &kCFTypeSetCallBacks); result->subjects = CFDictionaryCreateMutable(kCFAllocatorDefault, count, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFRange range = { 0, count }; CFArrayApplyFunction(certificates, range, SecMemoryCertificateSourceApplierFunction, result); return (SecCertificateSourceRef)result; }
CF_EXPORT void CFNotificationCenterRemoveObserver(CFNotificationCenterRef center, const void *observer, CFStringRef name, const void *object) { if (observer == NULL) { return; } OSSpinLockLock(¢er->lock); CFNotificationObserver ctx = { .observer = observer, .name = name, .object = object, .context = center }; struct __CFNotificationRemove notificationRemove = { .ctx = &ctx, .removed = 0, .more = 0 }; do { CFArrayApplyFunction(center->observers, CFRangeMake(0, CFArrayGetCount(center->observers)), (CFArrayApplierFunction)&removeObserver, ¬ificationRemove); for (int i=notificationRemove.removed-1; i >= 0; i--) { CFArrayRemoveValueAtIndex(center->observers, notificationRemove.removeIdx[i]); } if (notificationRemove.removed < __CFMaxRemove && !notificationRemove.more) { break; } notificationRemove.removed = 0; notificationRemove.more = 0; } while(1); OSSpinLockUnlock(¢er->lock); } CF_EXPORT void CFNotificationCenterRemoveEveryObserver(CFNotificationCenterRef center, const void *observer) { OSSpinLockLock(¢er->lock); CFArrayRemoveAllValues(center->observers); OSSpinLockUnlock(¢er->lock); }
void load(CFBundleRef bundle, Boolean bundleVerbose) { CFArrayRef targets; /* The array of dictionaries representing targets * with a "kick me" sign posted on their backs.*/ if (bundleVerbose) { _verbose = TRUE; } SCLog(_verbose, LOG_DEBUG, CFSTR("load() called")); SCLog(_verbose, LOG_DEBUG, CFSTR(" bundle ID = %@"), CFBundleGetIdentifier(bundle)); /* get the bundle's URL */ myBundleURL = CFBundleCopyBundleURL(bundle); if (!myBundleURL) { return; } /* get the targets */ targets = getTargets(bundle); if (!targets) { /* if nothing to do */ CFRelease(myBundleURL); return; } /* start a kicker for each target */ CFArrayApplyFunction(targets, CFRangeMake(0, CFArrayGetCount(targets)), startKicker, NULL); CFRelease(targets); return; }
/** * Recurses thru the keyboard properties looking for certain keys. * * @remark Yes, this can probably be done in a more efficient way. If you * know how to do this, don't hesitate to let us know! */ static void darwinBruteForcePropertySearch(CFDictionaryRef DictRef, struct KeyboardCacheData *pKeyboardEntry) { CFTypeRef ObjRef; /* * Check for the usage page and usage key we want. */ long lUsage; ObjRef = CFDictionaryGetValue(DictRef, CFSTR(kIOHIDElementUsageKey)); if ( ObjRef && CFGetTypeID(ObjRef) == CFNumberGetTypeID() && CFNumberGetValue((CFNumberRef)ObjRef, kCFNumberLongType, &lUsage)) { switch (lUsage) { case kHIDUsage_KeyboardLeftControl: case kHIDUsage_KeyboardLeftShift: case kHIDUsage_KeyboardLeftAlt: case kHIDUsage_KeyboardLeftGUI: case kHIDUsage_KeyboardRightControl: case kHIDUsage_KeyboardRightShift: case kHIDUsage_KeyboardRightAlt: case kHIDUsage_KeyboardRightGUI: { long lPage; ObjRef = CFDictionaryGetValue(DictRef, CFSTR(kIOHIDElementUsagePageKey)); if ( !ObjRef || CFGetTypeID(ObjRef) != CFNumberGetTypeID() || !CFNumberGetValue((CFNumberRef)ObjRef, kCFNumberLongType, &lPage) || lPage != kHIDPage_KeyboardOrKeypad) break; if (pKeyboardEntry->cCookies >= RT_ELEMENTS(pKeyboardEntry->aCookies)) { AssertMsgFailed(("too many cookies!\n")); break; } /* * Get the cookie and modifier mask. */ long lCookie; ObjRef = CFDictionaryGetValue(DictRef, CFSTR(kIOHIDElementCookieKey)); if ( !ObjRef || CFGetTypeID(ObjRef) != CFNumberGetTypeID() || !CFNumberGetValue((CFNumberRef)ObjRef, kCFNumberLongType, &lCookie)) break; uint32_t fMask; switch (lUsage) { case kHIDUsage_KeyboardLeftControl : fMask = controlKey; break; case kHIDUsage_KeyboardLeftShift : fMask = shiftKey; break; case kHIDUsage_KeyboardLeftAlt : fMask = optionKey; break; case kHIDUsage_KeyboardLeftGUI : fMask = cmdKey; break; case kHIDUsage_KeyboardRightControl: fMask = rightControlKey; break; case kHIDUsage_KeyboardRightShift : fMask = rightShiftKey; break; case kHIDUsage_KeyboardRightAlt : fMask = rightOptionKey; break; case kHIDUsage_KeyboardRightGUI : fMask = kEventKeyModifierRightCmdKeyMask; break; default: AssertMsgFailed(("%ld\n",lUsage)); fMask = 0; break; } /* * If we've got a queue, add the cookie to the queue. */ if (pKeyboardEntry->ppHidQueueInterface) { IOReturn rc = (*pKeyboardEntry->ppHidQueueInterface)->addElement(pKeyboardEntry->ppHidQueueInterface, (IOHIDElementCookie)lCookie, 0); AssertMsg(rc == kIOReturnSuccess, ("rc=%d\n", rc)); #ifdef DEBUG_PRINTF RTPrintf("dbg-add: u=%#lx c=%#lx\n", lUsage, lCookie); #endif } /* * Add the cookie to the keyboard entry. */ pKeyboardEntry->aCookies[pKeyboardEntry->cCookies].Cookie = (IOHIDElementCookie)lCookie; pKeyboardEntry->aCookies[pKeyboardEntry->cCookies].fMask = fMask; ++pKeyboardEntry->cCookies; break; } } } /* * Get the elements key and recursively iterate the elements looking * for they key cookies. */ ObjRef = CFDictionaryGetValue(DictRef, CFSTR(kIOHIDElementKey)); if ( ObjRef && CFGetTypeID(ObjRef) == CFArrayGetTypeID()) { CFArrayRef ArrayObjRef = (CFArrayRef)ObjRef; CFRange Range = {0, CFArrayGetCount(ArrayObjRef)}; CFArrayApplyFunction(ArrayObjRef, Range, darwinBruteForcePropertySearchApplier, pKeyboardEntry); } }
static void HIDGetDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, recDevice *pDevice) { CFMutableDictionaryRef usbProperties = 0; io_registry_entry_t parent1, parent2; /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties */ if ((KERN_SUCCESS == IORegistryEntryGetParentEntry (hidDevice, kIOServicePlane, &parent1)) && (KERN_SUCCESS == IORegistryEntryGetParentEntry (parent1, kIOServicePlane, &parent2)) && (KERN_SUCCESS == IORegistryEntryCreateCFProperties (parent2, &usbProperties, kCFAllocatorDefault, kNilOptions))) { if (usbProperties) { CFTypeRef refCF = 0; /* get device info * try hid dictionary first, if fail then go to usb dictionary */ /* get product name */ refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey)); if (!refCF) refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Product Name")); if (refCF) { if (!CFStringGetCString (refCF, pDevice->product, 256, CFStringGetSystemEncoding ())) SDL_SetError ("CFStringGetCString error retrieving pDevice->product."); } /* get usage page and usage */ refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey)); if (refCF) { if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usagePage)) SDL_SetError ("CFNumberGetValue error retrieving pDevice->usagePage."); refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey)); if (refCF) if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usage)) SDL_SetError ("CFNumberGetValue error retrieving pDevice->usage."); } if (NULL == refCF) /* get top level element HID usage page or usage */ { /* use top level element instead */ CFTypeRef refCFTopElement = 0; refCFTopElement = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey)); { /* refCFTopElement points to an array of element dictionaries */ CFRange range = {0, CFArrayGetCount (refCFTopElement)}; CFArrayApplyFunction (refCFTopElement, range, HIDTopLevelElementHandler, pDevice); } } CFRelease (usbProperties); } else SDL_SetError ("IORegistryEntryCreateCFProperties failed to create usbProperties."); if (kIOReturnSuccess != IOObjectRelease (parent2)) SDL_SetError ("IOObjectRelease error with parent2."); if (kIOReturnSuccess != IOObjectRelease (parent1)) SDL_SetError ("IOObjectRelease error with parent1."); } }
static void printSupportedLanguages(CFStringRef key, struct languagesAndKeys* context) { printLeftCol(key); CFArrayApplyFunction(context->keys, CFRangeMake(0, context->count), (CFArrayApplierFunction)&printTickIfExists, (void*)key); printf("\n"); }
int main (int argc, const char* argv[]) { if (argc == 1) { printf("Usage: LocalizationStatusHelper <bundle-path>\n"); } else { if (chdir(argv[1]) == -1) { printf("Cannot change directory to %s", argv[1]); perror(""); return 0; } DIR* dir = opendir("."); if (dir == NULL) { printf("Cannot open %s for reading", argv[1]); perror(""); return 0; } struct languagesAndKeys sk; sk.languages = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); sk.keys = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); CFMutableSetRef unionKeys = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks); struct dirent* dp; // Scan for the directory. while ((dp = readdir(dir)) != NULL) { if (dp->d_type == DT_DIR) { CFStringRef dirName = CFStringCreateWithCString(NULL, dp->d_name, kCFStringEncodingUTF8); // Check if it's an lproj. if (CFStringHasSuffix(dirName, CFSTR(".lproj"))) { CFMutableSetRef langKeys = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks); // Scan for strings files. chdir(dp->d_name); DIR* subdir = opendir("."); if (subdir != NULL) { struct dirent* dp2; while ((dp2 = readdir(subdir)) != NULL) { // Ignore linked strings files. if (dp2->d_type == DT_REG) { CFStringRef stringsName = CFStringCreateWithCString(NULL, dp2->d_name, kCFStringEncodingUTF8); // Ignore non-strings files. if (CFStringHasSuffix(stringsName, CFSTR(".strings"))) { // Convert to CFURLRef stringsURL = CFURLCreateWithFileSystemPath(NULL, stringsName, kCFURLPOSIXPathStyle, false); CFReadStreamRef stringsStream = CFReadStreamCreateWithFile(NULL, stringsURL); CFRelease(stringsURL); CFReadStreamOpen(stringsStream); CFPropertyListRef strings = CFPropertyListCreateFromStream(NULL, stringsStream, 0, kCFPropertyListImmutable, NULL, NULL); CFReadStreamClose(stringsStream); CFRelease(stringsStream); CFDictionaryApplyFunction(strings, (CFDictionaryApplierFunction)&addKeysToSet, langKeys); CFDictionaryApplyFunction(strings, (CFDictionaryApplierFunction)&addKeysToSet, unionKeys); CFRelease(strings); } CFRelease(stringsName); } } closedir(subdir); } chdir(".."); CFStringRef langCode = CFStringCreateWithSubstring(NULL, dirName, CFRangeMake(0, CFStringGetLength(dirName)-6)); CFArrayAppendValue(sk.languages, langCode); CFArrayAppendValue(sk.keys, langKeys); CFRelease(langKeys); CFRelease(langCode); } CFRelease(dirName); } } closedir(dir); sk.count = CFArrayGetCount(sk.languages); printf("|| *Key* ||"); CFArrayApplyFunction(sk.languages, CFRangeMake(0, sk.count), (CFArrayApplierFunction)&printHeader, NULL); printf("\n"); CFSetApplyFunction(unionKeys, (CFSetApplierFunction)&printSupportedLanguages, &sk); CFRelease(sk.keys); CFRelease(sk.languages); CFRelease(unionKeys); } return 0; }
void joypad::add_hid_elements(CFArrayRef p_array) { CFRange range = { 0, CFArrayGetCount(p_array) }; CFArrayApplyFunction(p_array, range, hid_element_added, this); }
static void printDictionary( const void *key, const void *value, void *context ) { print(CFSTR("%@ -"), key ); CFArrayApplyFunction( value, CFRangeMake(0, CFArrayGetCount(value)), printArray, NULL ); }
// Helper function to construct strings for CFDictionaryApplyFunction in // platform_getmimedescription() // key CFString containing a MIME type. // value CFDictionary containing descriptions and extensions. // context char * where we want our NP_GetMIMETypeDescription compatible output. static void mimetype_dictionary_applier(const void *key, const void *value, void *context) { CFDictionaryRef cf_mimetype_dict = value; CFStringRef cf_mimetype = key; CFIndex cf_length; CFArrayRef cf_extensions; CFStringRef cf_description; CFBooleanRef cf_enabled; char *mimetype = strdupa(""); char *description = strdupa(""); char *extensions = strdup(""); char **result = context; // Here is an example of the output we want: // // "application/example:ext1,ext2:Example MIME Type" // // Verify that we received a CFDictionary object. if (CFGetTypeID(cf_mimetype_dict) != CFDictionaryGetTypeID()) { goto finished; } // Verify that the key is a CFString. if (CFGetTypeID(cf_mimetype) != CFStringGetTypeID()) { goto finished; } // Find the length of the MIME Type, and allocate stack space for it. cf_length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cf_mimetype), kCFStringEncodingUTF8); mimetype = alloca(cf_length + 1); // Extract the string. if (CFStringGetCString(cf_mimetype, mimetype, cf_length + 1, kCFStringEncodingUTF8) != true) { goto finished; } // First we need to check if this type is disabled via WebPluginTypeEnabled. if (CFDictionaryGetValueIfPresent(cf_mimetype_dict, CFSTR("WebPluginTypeEnabled"), (const void **) &cf_enabled)) { // Verify that is a CFBoolean if (CFGetTypeID(cf_enabled) != CFBooleanGetTypeID()) { goto finished; } // Test value. if (CFBooleanGetValue(cf_enabled) == false) { goto finished; } } // Verify we have an empty string. if (!extensions) { goto finished; } // Now we need to lookup the extensions requested by the plugin. if (CFDictionaryGetValueIfPresent(cf_mimetype_dict, CFSTR("WebPluginExtensions"), (const void **) &cf_extensions)) { if (CFGetTypeID(cf_extensions) != CFArrayGetTypeID()) { goto finished; } l_debug("discovered %u extensions defined for mimetype %s", CFArrayGetCount(cf_extensions), mimetype); // Apply a function to every extension listed to concatenate them. CFArrayApplyFunction(cf_extensions, CFRangeMake(0, CFArrayGetCount(cf_extensions)), extension_array_applier, &extensions); } // Now we need the description which is a CFString if (CFDictionaryGetValueIfPresent(cf_mimetype_dict, CFSTR("WebPluginTypeDescription"), (const void **) &cf_description)) { if (CFGetTypeID(cf_description) != CFStringGetTypeID()) { goto finished; } // Find the length of the MIME Type, and allocate stack space for it. cf_length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cf_description), kCFStringEncodingUTF8); description = alloca(cf_length + 1); // Extract the string. if (CFStringGetCString(cf_description, description, cf_length + 1, kCFStringEncodingUTF8) != true) { goto finished; } } // So now we need to assemble the final string. *result = realloc(*result, (*result ? strlen(*result) : 0) + strlen(mimetype) + 1 + strlen(description) + 1 + strlen(extensions) + 1 + 1 + 1); // Verify that worked. if (!*result) { goto finished; } // Create the final string. sprintf(*result, "%s%s%s:%s:%s", *result, strlen(*result) ? ";" : "", mimetype, extensions, description); l_debug("successfully processed mimetype %s", mimetype); finished: free(extensions); return; }
/* Call AddHIDElement() on all elements in an array of IOHIDElementRefs */ static void AddHIDElements(CFArrayRef array, recDevice *pDevice) { const CFRange range = { 0, CFArrayGetCount(array) }; CFArrayApplyFunction(array, range, AddHIDElement, pDevice); }
static void HIDGetDeviceInfo(io_object_t hidDevice, CFMutableDictionaryRef hidProperties, recDevice * pDevice) { CFMutableDictionaryRef usbProperties = 0; io_registry_entry_t parent1, parent2; /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also * get dictionary for USB properties: step up two levels and get CF dictionary for USB properties */ if ((KERN_SUCCESS == IORegistryEntryGetParentEntry(hidDevice, kIOServicePlane, &parent1)) && (KERN_SUCCESS == IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2)) && (KERN_SUCCESS == IORegistryEntryCreateCFProperties(parent2, &usbProperties, kCFAllocatorDefault, kNilOptions))) { if (usbProperties) { CFTypeRef refCF = 0; /* get device info * try hid dictionary first, if fail then go to usb dictionary */ /* get product name */ refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey)); if (!refCF) { refCF = CFDictionaryGetValue(usbProperties, CFSTR("USB Product Name")); } if (refCF) { if (!CFStringGetCString(refCF, pDevice->product, 256, CFStringGetSystemEncoding())) { SDL_SetError("CFStringGetCString error retrieving pDevice->product."); } } /* get usage page and usage */ refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey)); if (refCF) { if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usagePage)) { SDL_SetError("CFNumberGetValue error retrieving pDevice->usagePage."); } refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsageKey)); if (refCF) { if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usage)) { SDL_SetError("CFNumberGetValue error retrieving pDevice->usage."); } } } refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDVendorIDKey)); if (refCF) { if (!CFNumberGetValue(refCF, kCFNumberLongType, &pDevice->guid.data[0])) { SDL_SetError("CFNumberGetValue error retrieving pDevice->guid[0]"); } } refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductIDKey)); if (refCF) { if (!CFNumberGetValue(refCF, kCFNumberLongType, &pDevice->guid.data[8])) { SDL_SetError("CFNumberGetValue error retrieving pDevice->guid[8]"); } } /* Check to make sure we have a vendor and product ID If we don't, use the same algorithm as the Linux code for Bluetooth devices */ { Uint32 *guid32 = (Uint32*)pDevice->guid.data; if (!guid32[0] && !guid32[1]) { const Uint16 BUS_BLUETOOTH = 0x05; Uint16 *guid16 = (Uint16 *)guid32; *guid16++ = BUS_BLUETOOTH; *guid16++ = 0; SDL_strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4); } } /* If we don't have a vendor and product ID this is probably a Bluetooth device */ if (NULL == refCF) { /* get top level element HID usage page or usage */ /* use top level element instead */ CFTypeRef refCFTopElement = 0; refCFTopElement = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDElementKey)); { /* refCFTopElement points to an array of element dictionaries */ CFRange range = { 0, CFArrayGetCount(refCFTopElement) }; CFArrayApplyFunction(refCFTopElement, range, HIDTopLevelElementHandler, pDevice); } } CFRelease(usbProperties); } else { SDL_SetError("IORegistryEntryCreateCFProperties failed to create usbProperties."); } if (kIOReturnSuccess != IOObjectRelease(parent2)) { SDL_SetError("IOObjectRelease error with parent2"); } if (kIOReturnSuccess != IOObjectRelease(parent1)) { SDL_SetError("IOObjectRelease error with parent1"); } } }
int main (int argc, const char* argv[]) { if (argc <= 1) { fprintf(stderr, "Usage: visualize-archived-object <archived-file>\n\n"); return 0; } CFStringRef fileNameAsCFString = CFStringCreateWithCString(NULL, argv[1], 0); CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, fileNameAsCFString, kCFURLPOSIXPathStyle, false); CFRelease(fileNameAsCFString); CFReadStreamRef readStream = CFReadStreamCreateWithFile(NULL, fileURL); CFRelease(fileURL); if (!readStream) { fprintf(stderr, "Error: Cannot open file for reading.\n\n"); return 1; } if (!CFReadStreamOpen(readStream)) { fprintf(stderr, "Error: Cannot open file for reading.\n\n"); CFRelease(readStream); return 2; } CFStringRef errStr = NULL; CFPropertyListRef val = CFPropertyListCreateFromStream(NULL, readStream, 0, kCFPropertyListImmutable, NULL, &errStr); CFReadStreamClose(readStream); CFRelease(readStream); if (errStr != NULL) { CFIndex strlength = CFStringGetLength(errStr)*3+1; char* res_cstring = malloc(strlength); CFStringGetCString(errStr, res_cstring, strlength, 0); fprintf(stderr, "Error: Cannot parse file as serialized property list.\n%s\n\n", res_cstring); CFRelease(errStr); free(res_cstring); if (val != NULL) CFRelease(val); return 3; } if (CFGetTypeID(val) != CFDictionaryGetTypeID()) { fprintf(stderr, "Error: Not a dictionary."); CFRelease(val); return 4; } CFStringRef archiver = CFDictionaryGetValue(val, CFSTR("$archiver")); if (archiver == NULL || !CFEqual(archiver, CFSTR("NSKeyedArchiver"))) { fprintf(stderr, "Error: Not encoded using an NSKeyedArchiver."); CFRelease(val); return 5; } CFArrayRef objs = CFDictionaryGetValue(val, CFSTR("$objects")); if (objs == NULL || CFGetTypeID(objs) != CFArrayGetTypeID()) { fprintf(stderr, "Error: $objects is not an array."); CFRelease(val); return 6; } CFIndex objsCount = CFArrayGetCount(objs); CFDictionaryRef topObj = CFDictionaryGetValue(val, CFSTR("$top")); if (topObj == NULL || CFGetTypeID(topObj) != CFDictionaryGetTypeID()) { CFShow(topObj); fprintf(stderr, "Error: $top is not a dictionary."); CFRelease(val); return 7; } int* refCount = calloc(objsCount, sizeof(int)); CFArrayApplyFunction(objs, CFRangeMake(0, objsCount), (CFArrayApplierFunction)&refCounter, refCount); struct printContext c; c.tabs = 0; c.refCount = refCount; c.objs = objs; printf("%s", "top = "); printObj(topObj, &c); printf(";\n\n"); for (CFIndex i = 0; i < objsCount; ++ i) if (refCount[i] > 1) { CFPropertyListRef refObj = CFArrayGetValueAtIndex(objs, i); if (isComplexObj(refObj)) { printf("/*CF$UID; referenced %u times*/ %ld = ", refCount[i], i); printObj(refObj, &c); printf(";\n\n"); } } free(refCount); CFRelease(val); return 0; }
void InitJoystick() { mach_port_t masterPort = NULL; io_iterator_t hidObjectIterator = NULL; IOReturn result = IOMasterPort (bootstrap_port, &masterPort); if(result != kIOReturnSuccess) return; CFMutableDictionaryRef hidMatchDictionary = IOServiceMatching (kIOHIDDeviceKey); if(!hidMatchDictionary) return; result = IOServiceGetMatchingServices (masterPort, hidMatchDictionary, &hidObjectIterator); if(result != kIOReturnSuccess) return; // find the first joystick/gamepad on the USB for(;;) { IOHIDDeviceInterface **device; io_object_t ioHIDDeviceObject = IOIteratorNext(hidObjectIterator); if(!ioHIDDeviceObject) break; CFMutableDictionaryRef hidProperties = 0; long kresult = IORegistryEntryCreateCFProperties(ioHIDDeviceObject, &hidProperties, kCFAllocatorDefault, kNilOptions); if(kresult == KERN_SUCCESS && hidProperties) { CFTypeRef refCF = 0; refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey)); if(CFGetTypeID(refCF) == CFStringGetTypeID()) { CFIndex bufferSize = CFStringGetLength (refCF) + 1; char * buffer = (char *)malloc (bufferSize); if (buffer) { if (CFStringGetCString (refCF, buffer, bufferSize, CFStringGetSystemEncoding ())) strncpy(gJoystickName, buffer, MaxJoystickNameLen); free(buffer); } } refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey)); long usage, usagePage; CFNumberGetValue (refCF, kCFNumberLongType, &usagePage); refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey)); CFNumberGetValue (refCF, kCFNumberLongType, &usage); if ( (usagePage == kHIDPage_GenericDesktop) && ((usage == kHIDUsage_GD_Joystick || usage == kHIDUsage_GD_GamePad)) ) { CFTypeRef refElementTop = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey)); if (refElementTop) { CFTypeID type = CFGetTypeID (refElementTop); if (type == CFArrayGetTypeID()) /* if element is an array */ { CFRange range = {0, CFArrayGetCount (refElementTop)}; /* CountElementsCFArrayHandler called for each array member */ CFArrayApplyFunction (refElementTop, range, HIDGetElementsCFArrayHandler, NULL); IOCFPlugInInterface ** ppPlugInInterface = NULL; S32 score; IOReturn result = IOCreatePlugInInterfaceForService (ioHIDDeviceObject, kIOHIDDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &ppPlugInInterface, &score); if (result == kIOReturnSuccess) { // Call a method of the intermediate plug-in to create the device interface (*ppPlugInInterface)->QueryInterface (ppPlugInInterface, CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void *) &device); if(device) { result = (*device)->open(device, 0); gController.device = device; gJoystickInit = true; } (*ppPlugInInterface)->Release (ppPlugInInterface); } } } } CFRelease(hidProperties); } IOObjectRelease(ioHIDDeviceObject); if(gJoystickInit) break; } IOObjectRelease (hidObjectIterator); /* release the iterator */ }
SECURITY_STATUS schan_imp_get_session_peer_certificate(schan_imp_session session, HCERTSTORE store, PCCERT_CONTEXT *ret_cert) { struct mac_session* s = (struct mac_session*)session; SECURITY_STATUS ret = SEC_E_OK; PCCERT_CONTEXT cert = NULL; SecCertificateRef mac_cert; CFArrayRef cert_array; int status; CFIndex cnt, i; CFDataRef data; BOOL res; TRACE("(%p/%p, %p)\n", s, s->context, cert); #ifdef HAVE_SSLCOPYPEERCERTIFICATES status = SSLCopyPeerCertificates(s->context, &cert_array); #else status = SSLGetPeerCertificates(s->context, &cert_array); #endif if (status != noErr || !cert_array) { WARN("SSLCopyPeerCertificates failed: %d\n", status); return SEC_E_INTERNAL_ERROR; } cnt = CFArrayGetCount(cert_array); for (i=0; i < cnt; i++) { if (!(mac_cert = (SecCertificateRef)CFArrayGetValueAtIndex(cert_array, i)) || (SecKeychainItemExport(mac_cert, kSecFormatX509Cert, 0, NULL, &data) != noErr)) { WARN("Couldn't extract certificate data\n"); ret = SEC_E_INTERNAL_ERROR; break; } res = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING, CFDataGetBytePtr(data), CFDataGetLength(data), CERT_STORE_ADD_REPLACE_EXISTING, i ? NULL : &cert); CFRelease(data); if (!res) { ret = GetLastError(); WARN("CertAddEncodedCertificateToStore failed: %x\n", ret); break; } } #ifndef HAVE_SSLCOPYPEERCERTIFICATES /* This is why SSLGetPeerCertificates was deprecated */ CFArrayApplyFunction(cert_array, CFRangeMake(0, CFArrayGetCount(cert_array)), schan_imp_cf_release, NULL); #endif CFRelease(cert_array); if (ret != SEC_E_OK) { if(cert) CertFreeCertificateContext(cert); return ret; } *ret_cert = cert; return SEC_E_OK; }
static void HIDGetElementsCFArrayHandler (const void * value, void * parameter) { if (CFGetTypeID (value) != CFDictionaryGetTypeID ()) return; CFTypeRef refElement = CFTypeRef(value); long elementType, usagePage, usage; CFTypeRef refElementType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementTypeKey)); CFTypeRef refUsagePage = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUsagePageKey)); CFTypeRef refUsage = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUsageKey)); bool isButton = false, isAxis = false; ControllerElement *theElement = NULL; if ((refElementType) && (CFNumberGetValue (refElementType, kCFNumberLongType, &elementType))) { /* look at types of interest */ if ((elementType == kIOHIDElementTypeInput_Misc) || (elementType == kIOHIDElementTypeInput_Button) || (elementType == kIOHIDElementTypeInput_Axis)) { if (refUsagePage && CFNumberGetValue (refUsagePage, kCFNumberLongType, &usagePage) && refUsage && CFNumberGetValue (refUsage, kCFNumberLongType, &usage)) { switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */ { case kHIDPage_GenericDesktop: { switch (usage) /* look at usage to determine function */ { case kHIDUsage_GD_X: theElement = gController.axes + AxisX; break; case kHIDUsage_GD_Y: theElement = gController.axes + AxisY; break; case kHIDUsage_GD_Z: theElement = gController.axes + AxisZ; break; case kHIDUsage_GD_Rx: theElement = gController.axes + AxisRx; break; case kHIDUsage_GD_Ry: theElement = gController.axes + AxisRy; break; case kHIDUsage_GD_Rz: theElement = gController.axes + AxisRz; break; case kHIDUsage_GD_Slider: theElement = gController.axes + AxisSlider0; break; case kHIDUsage_GD_Dial: case kHIDUsage_GD_Wheel: break; } } break; case kHIDPage_Button: { ControllerElement e; gController.buttons.push_back(e); theElement = &gController.buttons.last(); } break; default: break; } } } else if (kIOHIDElementTypeCollection == elementType) { CFTypeRef refElementTop = CFDictionaryGetValue ((CFMutableDictionaryRef) refElement, CFSTR(kIOHIDElementKey)); if (refElementTop) { CFTypeID type = CFGetTypeID (refElementTop); if (type == CFArrayGetTypeID()) /* if element is an array */ { CFRange range = {0, CFArrayGetCount (refElementTop)}; /* CountElementsCFArrayHandler called for each array member */ CFArrayApplyFunction (refElementTop, range, HIDGetElementsCFArrayHandler, NULL); } } } } if (theElement) /* add to list */ { long number; CFTypeRef refType; refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementCookieKey)); if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number)) theElement->cookie = (IOHIDElementCookie) number; refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementMinKey)); if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number)) theElement->minValue = number; refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementMaxKey)); if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number)) theElement->maxValue = number; logprintf("Cookie = %d min = %d max = %d", theElement->cookie, theElement->minValue, theElement->maxValue); } }