]> git.saurik.com Git - apple/security.git/blob - libsecurity_transform/lib/Utilities.cpp
Security-55178.0.1.tar.gz
[apple/security.git] / libsecurity_transform / lib / Utilities.cpp
1 #include "Utilities.h"
2 #include "SecTransform.h"
3 #include <sys/sysctl.h>
4 #include <syslog.h>
5 #include <dispatch/dispatch.h>
6
7 void MyDispatchAsync(dispatch_queue_t queue, void(^block)(void))
8 {
9 fprintf(stderr, "Running job on queue %p\n", queue);
10 dispatch_async(queue, block);
11 }
12
13
14
15 dispatch_queue_t MyDispatchQueueCreate(const char* name, dispatch_queue_attr_t attr)
16 {
17 dispatch_queue_t result = dispatch_queue_create(name, attr);
18 // fprintf(stderr, "Created queue %s as %p\n", name, result);
19 return result;
20 }
21
22
23
24 static CFErrorRef CreateErrorRefCore(CFStringRef domain, int errorCode, const char* format, va_list ap)
25 {
26 CFStringRef fmt = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
27 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, fmt, ap);
28 va_end(ap);
29 CFRelease(fmt);
30
31 CFStringRef keys[] = {kCFErrorDescriptionKey};
32 CFStringRef values[] = {str};
33
34 CFErrorRef result = CFErrorCreateWithUserInfoKeysAndValues(NULL, domain, errorCode, (const void**) keys, (const void**) values, 1);
35 CFRelease(str);
36
37 return result;
38 }
39
40
41
42 CFErrorRef CreateGenericErrorRef(CFStringRef domain, int errorCode, const char* format, ...)
43 {
44 va_list ap;
45 va_start(ap, format);
46 return CreateErrorRefCore(domain, errorCode, format, ap);
47 }
48
49
50
51 CFErrorRef CreateSecTransformErrorRef(int errorCode, const char* format, ...)
52 {
53 // create a CFError in the SecTransform error domain. You can add an explanation, which is cool.
54 va_list ap;
55 va_start(ap, format);
56
57 return CreateErrorRefCore(kSecTransformErrorDomain, errorCode, format, ap);
58 }
59
60
61
62 CFErrorRef CreateSecTransformErrorRefWithCFType(int errorCode, CFTypeRef message)
63 {
64 CFStringRef keys[] = {kCFErrorLocalizedDescriptionKey};
65 CFTypeRef values[] = {message};
66 return CFErrorCreateWithUserInfoKeysAndValues(NULL, kSecTransformErrorDomain, errorCode, (const void**) keys, (const void**) values, 1);
67 }
68
69
70
71 CFTypeRef gAnnotatedRef = NULL;
72
73 CFTypeRef DebugRetain(const void* owner, CFTypeRef type)
74 {
75 CFTypeRef result = CFRetain(type);
76 if (type == gAnnotatedRef)
77 {
78 fprintf(stderr, "Object %p was retained by object %p, count = %ld\n", type, owner, CFGetRetainCount(type));
79 }
80
81 return result;
82 }
83
84
85
86 void DebugRelease(const void* owner, CFTypeRef type)
87 {
88 if (type == gAnnotatedRef)
89 {
90 fprintf(stderr, "Object %p was released by object %p, count = %ld\n", type, owner, CFGetRetainCount(type) - 1);
91 }
92
93 CFRelease(type);
94 }
95
96 // Cribbed from _dispatch_bug and altered a bit
97 void transforms_bug(size_t line, long val)
98 {
99 static dispatch_once_t pred;
100 static char os_build[16];
101 static void *last_seen;
102 void *ra = __builtin_return_address(0);
103 dispatch_once(&pred, ^{
104 #ifdef __APPLE__
105 int mib[] = { CTL_KERN, KERN_OSVERSION };
106 size_t bufsz = sizeof(os_build);
107 sysctl(mib, 2, os_build, &bufsz, NULL, 0);
108 #else
109 os_build[0] = '\0';
110 #endif
111 });
112 if (last_seen != ra) {
113 last_seen = ra;
114 syslog(LOG_NOTICE, "BUG in SecTransforms: %s - %p - %lu - %lu", os_build, last_seen, (unsigned long)line, val);
115 }
116 }