X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/72a12576750f52947eb043106ba5c12c0d07decf..b1ab9ed8d0e0f1c3b66d7daa8fd5564444c56195:/libsecurity_transform/lib/Utilities.cpp diff --git a/libsecurity_transform/lib/Utilities.cpp b/libsecurity_transform/lib/Utilities.cpp new file mode 100644 index 00000000..094cc334 --- /dev/null +++ b/libsecurity_transform/lib/Utilities.cpp @@ -0,0 +1,116 @@ +#include "Utilities.h" +#include "SecTransform.h" +#include +#include +#include + +void MyDispatchAsync(dispatch_queue_t queue, void(^block)(void)) +{ + fprintf(stderr, "Running job on queue %p\n", queue); + dispatch_async(queue, block); +} + + + +dispatch_queue_t MyDispatchQueueCreate(const char* name, dispatch_queue_attr_t attr) +{ + dispatch_queue_t result = dispatch_queue_create(name, attr); + // fprintf(stderr, "Created queue %s as %p\n", name, result); + return result; +} + + + +static CFErrorRef CreateErrorRefCore(CFStringRef domain, int errorCode, const char* format, va_list ap) +{ + CFStringRef fmt = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8); + CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, fmt, ap); + va_end(ap); + CFRelease(fmt); + + CFStringRef keys[] = {kCFErrorDescriptionKey}; + CFStringRef values[] = {str}; + + CFErrorRef result = CFErrorCreateWithUserInfoKeysAndValues(NULL, domain, errorCode, (const void**) keys, (const void**) values, 1); + CFRelease(str); + + return result; +} + + + +CFErrorRef CreateGenericErrorRef(CFStringRef domain, int errorCode, const char* format, ...) +{ + va_list ap; + va_start(ap, format); + return CreateErrorRefCore(domain, errorCode, format, ap); +} + + + +CFErrorRef CreateSecTransformErrorRef(int errorCode, const char* format, ...) +{ + // create a CFError in the SecTransform error domain. You can add an explanation, which is cool. + va_list ap; + va_start(ap, format); + + return CreateErrorRefCore(kSecTransformErrorDomain, errorCode, format, ap); +} + + + +CFErrorRef CreateSecTransformErrorRefWithCFType(int errorCode, CFTypeRef message) +{ + CFStringRef keys[] = {kCFErrorLocalizedDescriptionKey}; + CFTypeRef values[] = {message}; + return CFErrorCreateWithUserInfoKeysAndValues(NULL, kSecTransformErrorDomain, errorCode, (const void**) keys, (const void**) values, 1); +} + + + +CFTypeRef gAnnotatedRef = NULL; + +CFTypeRef DebugRetain(const void* owner, CFTypeRef type) +{ + CFTypeRef result = CFRetain(type); + if (type == gAnnotatedRef) + { + fprintf(stderr, "Object %p was retained by object %p, count = %ld\n", type, owner, CFGetRetainCount(type)); + } + + return result; +} + + + +void DebugRelease(const void* owner, CFTypeRef type) +{ + if (type == gAnnotatedRef) + { + fprintf(stderr, "Object %p was released by object %p, count = %ld\n", type, owner, CFGetRetainCount(type) - 1); + } + + CFRelease(type); +} + +// Cribbed from _dispatch_bug and altered a bit +void transforms_bug(size_t line, long val) +{ + static dispatch_once_t pred; + static char os_build[16]; + static void *last_seen; + void *ra = __builtin_return_address(0); + dispatch_once(&pred, ^{ +#ifdef __APPLE__ + int mib[] = { CTL_KERN, KERN_OSVERSION }; + size_t bufsz = sizeof(os_build); + sysctl(mib, 2, os_build, &bufsz, NULL, 0); +#else + os_build[0] = '\0'; +#endif + }); + if (last_seen != ra) { + last_seen = ra; + syslog(LOG_NOTICE, "BUG in SecTransforms: %s - %p - %lu - %lu", os_build, last_seen, (unsigned long)line, val); + } +}