Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.20 KB  |  hits: 138  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Converting a CFStringRef to char *
  2. SecStaticCodeRef staticCode;
  3. CFDictionaryRef information;
  4. SecCSFlags flags = kSecCSInternalInformation
  5.             | kSecCSSigningInformation
  6.             | kSecCSRequirementInformation
  7.             | kSecCSInternalInformation;    
  8. CFURLRef pathURL = NULL;
  9. CFStringRef pathStr = NULL;
  10. CFStringRef uniqueid;
  11. char* str = NULL;
  12. CFIndex length;
  13.  
  14.  
  15. pathStr = CFStringCreateWithCString(kCFAllocatorDefault,  
  16.                                     filename, kCFStringEncodingUTF8);    
  17. pathURL = CFURLCreateWithString(kCFAllocatorDefault, pathStr, NULL);
  18. SecStaticCodeCreateWithPath(pathURL, kSecCSDefaultFlags, &staticCode);
  19. SecCodeCopySigningInformation(staticCode, flags, &information);      
  20.  
  21. uniqueid = (CFStringRef) CFDictionaryGetValue(information, kSecCodeInfoUnique);
  22.  
  23. // how do I convert it here to char *?
  24. length = CFStringGetLength(uniqueid);
  25. str = (char *)malloc( length + 1 );
  26. CFStringGetCString(uniqueid, str, length, kCFStringEncodingUTF8);
  27.  
  28. printf("hash of signature is %sn", str);
  29.  
  30. CFRelease(information);
  31. CFRelease(staticCode);
  32.        
  33. char * MYCFStringCopyUTF8String(CFStringRef aString) {
  34.   if (aString == NULL) {
  35.     return NULL;
  36.   }
  37.  
  38.   CFIndex length = CFStringGetLength(aString);
  39.   CFIndex maxSize =
  40.   CFStringGetMaximumSizeForEncoding(length,
  41.                                     kCFStringEncodingUTF8);
  42.   char *buffer = (char *)malloc(maxSize);
  43.   if (CFStringGetCString(aString, buffer, maxSize,
  44.                          kCFStringEncodingUTF8)) {
  45.     return buffer;
  46.   }
  47.   return NULL;
  48. }
  49.        
  50. #include <CoreFoundation/CoreFoundation.h>
  51.  
  52.  
  53. CFStringRef str;
  54. //Removed CFRange
  55. const char *bytes; //This is where the conversion result will end up.
  56.  
  57.  
  58.  
  59. str = CFSTR("You Want this String!n"); //Changed this part
  60.  
  61. bytes = CFStringGetCStringPtr(str, kCFStringEncodingMacRoman);
  62.  
  63.  
  64.  
  65. if (bytes == NULL)
  66. {
  67.     //Adding in getting the size
  68.     CFIndex stringLengthIndex = CFStringGetLength(str);
  69.  
  70.     //Converted index (signed long ) to int
  71.     char localBuffer[(int) stringLengthIndex];
  72.  
  73.     Boolean success;
  74.  
  75.     success = CFStringGetCString(str, localBuffer, stringLengthIndex, kCFStringEncodingMacRoman);
  76.  
  77. }
  78. //At this point the "bytes" variable should contain your C string copied from the provided CFStringRef