将Objective-C代码转换为plain C

时间:2012-01-20 14:37:49

标签: objective-c c macos

我正在使用以下代码来验证应用程序是否已签名。它位于Objective-C中,它基于Professional Cocoa Application Security上的代码。

OSStatus secError = noErr; 
// retrieve this process's code object 
SecCodeRef myCode; 
secError = SecCodeCopySelf(kSecCSDefaultFlags, &myCode); 
if (noErr != secError) 
{
    NSLog(@"unable to retrieve code object, security error %d", secError); 
    return -1;
}

// validate the process's identity, using the internal requirements 
secError = SecCodeCheckValidity(myCode, kSecCSDefaultFlags, NULL); 
switch (secError) 
{
    case noErr: 
        NSLog(@"this process has a valid signature"); 
        break;
    case errSecCSUnsigned: 
        NSLog(@"this process executable is unsigned"); 
        break;
    case errSecCSSignatureFailed:
    case errSecCSGuestInvalid:
        NSLog(@"this process has an invalid signature");
        break; 
    default:
        NSLog(@"error %d validating signature", secError); 
        break;
}

// get the static code object, representing the executable on disk 
SecStaticCodeRef fileCode; 
secError = SecCodeCopyStaticCode(myCode, kSecCSDefaultFlags, &fileCode); 
if (noErr != secError) 
{
    NSLog(@"unable to get static code object, security error %d", secError); 
    CFRelease(myCode); 
    return -1;
}

//some basic information about the code signature 
NSDictionary *signingInfo = nil; 

secError = SecCodeCopySigningInformation(fileCode, kSecCSDefaultFlags, &signingInfo);
if (noErr != secError) 
{ 
    if(secError == errSecCSSignatureFailed)
        NSLog(@"invalid signature");
    else
        NSLog(@"cannot get signing information, security error %d", secError);
} 
else 
{
    NSLog(@"signing info: %@", signingInfo); 
    [signingInfo release];
}

CFRelease(myCode); 
CFRelease(fileCode); 

我需要将它转换为普通C,所以我也可以在我用C编写的应用程序上使用它。其中一个问题是NSDictionary *signingInfo = nil;我尝试使用CFDictionaryRef *signingInfo = NULL;来解决它似乎不起作用。

任何人都有机会将此代码翻译成C?

谢谢!

2 个答案:

答案 0 :(得分:3)

CFDictionaryRef已经是一个指针。因此,您应该使用CFDictionaryRef而不是CFDictionaryRef*

答案 1 :(得分:3)

您是否尝试过使用CFDictionaryRef signingInfo = NULL;而没有额外*?核心基础ref已经是一个指针。 CFDictionaryRef免费桥接到NSDictionary *。然后[signingInfo release];可以转换为CFRelease(signingInfo)。您还应该用其他东西替换NSLog。