//
//  main.c
//  secure-notes-exporter
//
//  Created by Greg Hurrell on 5/9/14.
//  Copyright (c) 2014 Greg Hurrell. All rights reserved.
//

#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>

void printItem(const void *value, void *context) {
    CFDictionaryRef dictionary = value;
    CFNumberRef itemType = CFDictionaryGetValue(dictionary, kSecAttrType);
    CFNumberRef noteType = (CFNumberRef)context;
    if (!itemType || !CFEqual(itemType, noteType)) {
        // not a note
        return;
    }

    CFShow(noteType);
}

int main(int argc, const char * argv[])
{
    // create query
    CFStringRef keys[] = { kSecReturnAttributes, kSecMatchLimit, kSecClass };
    CFTypeRef values[] = { kCFBooleanTrue, kSecMatchLimitAll, kSecClassGenericPassword };
    CFDictionaryRef query = CFDictionaryCreate(
        kCFAllocatorDefault,
        (const void **)keys,
        (const void **)values,
        3,
        &kCFTypeDictionaryKeyCallBacks,
        &kCFTypeDictionaryValueCallBacks
    );

    // get search results
    CFArrayRef items = nil;
    OSStatus status = SecItemCopyMatching(query, (CFTypeRef *)&items);
    CFRelease(query);
    assert(status == 0);

    // iterate over "Secure Note" items
    CFRange range = CFRangeMake(0, CFArrayGetCount(items));
    SInt32 note = 'note';
    CFNumberRef noteRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &note);
    CFArrayApplyFunction(items, range, printItem, (void *)noteRef);
    CFRelease(noteRef);
    CFRelease(items);
    return 0;
}


//
//  main.c
//  secure-notes-exporter
//
//  Created by Greg Hurrell on 5/9/14.
//  Copyright (c) 2014 Greg Hurrell. All rights reserved.
//

#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>

int main(int argc, const char * argv[])
{
    // create query
    CFStringRef keys[] = { kSecReturnAttributes, kSecMatchLimit, kSecClass };
    CFTypeRef values[] = { kCFBooleanTrue, kSecMatchLimitAll, kSecClassGenericPassword };
    CFDictionaryRef query = CFDictionaryCreate(
        kCFAllocatorDefault,
        (const void **)keys,
        (const void **)values,
        3,
        &kCFTypeDictionaryKeyCallBacks,
        &kCFTypeDictionaryValueCallBacks
    );

    // get search results
    CFArrayRef items = nil;
    OSStatus status = SecItemCopyMatching(query, (CFTypeRef *)&items);
    CFRelease(query);
    assert(status == 0);
    CFShow(items);
    CFRelease(items);
    return 0;
}


--- 
+++ 
@@ -8,6 +8,18 @@
 
 #include <CoreFoundation/CoreFoundation.h>
 #include <Security/Security.h>
+
+void printItem(const void *value, void *context) {
+    CFDictionaryRef dictionary = value;
+    CFNumberRef itemType = CFDictionaryGetValue(dictionary, kSecAttrType);
+    CFNumberRef noteType = (CFNumberRef)context;
+    if (!itemType || !CFEqual(itemType, noteType)) {
+        // not a note
+        return;
+    }
+
+    CFShow(noteType);
+}
 
 int main(int argc, const char * argv[])
 {
@@ -28,7 +40,13 @@
     OSStatus status = SecItemCopyMatching(query, (CFTypeRef *)&items);
     CFRelease(query);
     assert(status == 0);
-    CFShow(items);
+
+    // iterate over "Secure Note" items
+    CFRange range = CFRangeMake(0, CFArrayGetCount(items));
+    SInt32 note = 'note';
+    CFNumberRef noteRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &note);
+    CFArrayApplyFunction(items, range, printItem, (void *)noteRef);
+    CFRelease(noteRef);
     CFRelease(items);
     return 0;
 }
Print something about "Secure Notes" while iterating

I couldn't find a constant for 'note' and there are probably more
idiomatic ways to do this, but this is what I have.

Signed-off-by: Greg Hurrell <79e2475f81a6317276bf7cbb3958b20d289b78df@hurrell.net>
