- Converting a CFStringRef to char *
- SecStaticCodeRef staticCode;
- CFDictionaryRef information;
- SecCSFlags flags = kSecCSInternalInformation
- | kSecCSSigningInformation
- | kSecCSRequirementInformation
- | kSecCSInternalInformation;
- CFURLRef pathURL = NULL;
- CFStringRef pathStr = NULL;
- CFStringRef uniqueid;
- char* str = NULL;
- CFIndex length;
- pathStr = CFStringCreateWithCString(kCFAllocatorDefault,
- filename, kCFStringEncodingUTF8);
- pathURL = CFURLCreateWithString(kCFAllocatorDefault, pathStr, NULL);
- SecStaticCodeCreateWithPath(pathURL, kSecCSDefaultFlags, &staticCode);
- SecCodeCopySigningInformation(staticCode, flags, &information);
- uniqueid = (CFStringRef) CFDictionaryGetValue(information, kSecCodeInfoUnique);
- // how do I convert it here to char *?
- length = CFStringGetLength(uniqueid);
- str = (char *)malloc( length + 1 );
- CFStringGetCString(uniqueid, str, length, kCFStringEncodingUTF8);
- printf("hash of signature is %sn", str);
- CFRelease(information);
- CFRelease(staticCode);
- char * MYCFStringCopyUTF8String(CFStringRef aString) {
- if (aString == NULL) {
- return NULL;
- }
- CFIndex length = CFStringGetLength(aString);
- CFIndex maxSize =
- CFStringGetMaximumSizeForEncoding(length,
- kCFStringEncodingUTF8);
- char *buffer = (char *)malloc(maxSize);
- if (CFStringGetCString(aString, buffer, maxSize,
- kCFStringEncodingUTF8)) {
- return buffer;
- }
- return NULL;
- }
- #include <CoreFoundation/CoreFoundation.h>
- CFStringRef str;
- //Removed CFRange
- const char *bytes; //This is where the conversion result will end up.
- str = CFSTR("You Want this String!n"); //Changed this part
- bytes = CFStringGetCStringPtr(str, kCFStringEncodingMacRoman);
- if (bytes == NULL)
- {
- //Adding in getting the size
- CFIndex stringLengthIndex = CFStringGetLength(str);
- //Converted index (signed long ) to int
- char localBuffer[(int) stringLengthIndex];
- Boolean success;
- success = CFStringGetCString(str, localBuffer, stringLengthIndex, kCFStringEncodingMacRoman);
- }
- //At this point the "bytes" variable should contain your C string copied from the provided CFStringRef