static CFStringRef date_string(CFDateRef date) { CFLocaleRef locale = CFLocaleCopyCurrent(); CFDateFormatterRef formatter = CFDateFormatterCreate(kCFAllocatorDefault, locale, kCFDateFormatterFullStyle, kCFDateFormatterFullStyle); CFStringRef result = CFDateFormatterCreateStringWithDate(kCFAllocatorDefault, formatter, date); RELEASE_IF_NOT_NULL(formatter); RELEASE_IF_NOT_NULL(locale); return result; }
void logger::addLog(CFStringRef _log, bool _addTimeStamp){ if (_addTimeStamp){ CFAbsoluteTime timeNow = CFAbsoluteTimeGetCurrent(); CFDateRef d = CFDateCreate(kCFAllocatorDefault, timeNow); CFLocaleRef currentLocale = CFLocaleCopyCurrent(); CFDateFormatterRef dateFormatter = CFDateFormatterCreate(NULL, currentLocale, kCFDateFormatterShortStyle, kCFDateFormatterMediumStyle); CFStringRef t = CFDateFormatterCreateStringWithDate(kCFAllocatorDefault, dateFormatter, d); CFMutableStringRef tmpString = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, t); CFStringAppend(tmpString, CFSTR("\t")); CFStringAppend(tmpString, _log); CFArrayAppendValue(logArray, tmpString); CFRelease(d); CFRelease(currentLocale); CFRelease(dateFormatter); CFRelease(t); } else { CFArrayAppendValue(logArray, _log); } needUpdate = true; if (CFArrayGetCount(logArray)>MAX_LOG) CFArrayRemoveValueAtIndex(logArray, 0); }
/* TODO: Use the shared version of this function in print_cert.c. */ static void print_line(CFStringRef line) { UInt8 buf[256]; CFRange range = { .location = 0 }; range.length = CFStringGetLength(line); while (range.length > 0) { CFIndex bytesUsed = 0; CFIndex converted = CFStringGetBytes(line, range, kCFStringEncodingUTF8, 0, false, buf, sizeof(buf), &bytesUsed); fwrite(buf, 1, bytesUsed, stdout); range.length -= converted; range.location += converted; } fputc('\n', stdout); } static void printPlist(CFArrayRef plist, CFIndex indent, CFIndex maxWidth) { CFIndex count = CFArrayGetCount(plist); CFIndex ix; for (ix = 0; ix < count ; ++ix) { CFDictionaryRef prop = (CFDictionaryRef)CFArrayGetValueAtIndex(plist, ix); CFStringRef pType = (CFStringRef)CFDictionaryGetValue(prop, kSecPropertyKeyType); CFStringRef label = (CFStringRef)CFDictionaryGetValue(prop, kSecPropertyKeyLabel); CFStringRef llabel = (CFStringRef)CFDictionaryGetValue(prop, kSecPropertyKeyLocalizedLabel); CFTypeRef value = (CFTypeRef)CFDictionaryGetValue(prop, kSecPropertyKeyValue); bool isSection = CFEqual(pType, kSecPropertyTypeSection); CFMutableStringRef line = CFStringCreateMutable(NULL, 0); CFIndex jx = 0; for (jx = 0; jx < indent; ++jx) { CFStringAppend(line, CFSTR(" ")); } if (llabel) { CFStringAppend(line, llabel); if (!isSection) { for (jx = CFStringGetLength(llabel) + indent * 4; jx < maxWidth; ++jx) { CFStringAppend(line, CFSTR(" ")); } CFStringAppend(line, CFSTR(" : ")); } } if (CFEqual(pType, kSecPropertyTypeWarning)) { CFStringAppend(line, CFSTR("*WARNING* ")); CFStringAppend(line, (CFStringRef)value); } else if (CFEqual(pType, kSecPropertyTypeError)) { CFStringAppend(line, CFSTR("*ERROR* ")); CFStringAppend(line, (CFStringRef)value); } else if (CFEqual(pType, kSecPropertyTypeSuccess)) { CFStringAppend(line, CFSTR("*OK* ")); CFStringAppend(line, (CFStringRef)value); } else if (CFEqual(pType, kSecPropertyTypeTitle)) { CFStringAppend(line, CFSTR("*")); CFStringAppend(line, (CFStringRef)value); CFStringAppend(line, CFSTR("*")); } else if (CFEqual(pType, kSecPropertyTypeSection)) { } else if (CFEqual(pType, kSecPropertyTypeData)) { CFDataRef data = (CFDataRef)value; CFIndex length = CFDataGetLength(data); if (length > 20) CFStringAppendFormat(line, NULL, CFSTR("[%" PRIdCFIndex " bytes] "), length); const UInt8 *bytes = CFDataGetBytePtr(data); for (jx = 0; jx < length; ++jx) { if (jx == 0) CFStringAppendFormat(line, NULL, CFSTR("%02X"), bytes[jx]); else if (jx < 15 || length <= 20) CFStringAppendFormat(line, NULL, CFSTR(" %02X"), bytes[jx]); else { CFStringAppend(line, CFSTR(" ...")); break; } } } else if (CFEqual(pType, kSecPropertyTypeString)) { CFStringAppend(line, (CFStringRef)value); } else if (CFEqual(pType, kSecPropertyTypeDate)) { CFLocaleRef lc = CFLocaleCopyCurrent(); CFDateFormatterRef df = CFDateFormatterCreate(NULL, lc, kCFDateFormatterFullStyle, kCFDateFormatterFullStyle); //CFTimeZoneRef tz = CFTimeZoneCreateWithName(NULL, CFSTR("GMT"), false); //CFDateFormatterSetProperty(df, kCFDateFormatterTimeZone, tz); //CFRelease(tz); CFDateRef date = (CFDateRef)value; CFStringRef ds = CFDateFormatterCreateStringWithDate(NULL, df, date); CFStringAppend(line, ds); CFRelease(ds); CFRelease(df); CFRelease(lc); } else if (CFEqual(pType, kSecPropertyTypeURL)) { CFURLRef url = (CFURLRef)value; CFStringAppend(line, CFSTR("<")); CFStringAppend(line, CFURLGetString(url)); CFStringAppend(line, CFSTR(">")); } else { CFStringAppendFormat(line, NULL, CFSTR("*unknown type %@* = %@"), pType, value); } if (!isSection || label) print_line(line); CFRelease(line); if (isSection) { printPlist((CFArrayRef)value, indent + 1, maxWidth); } } }
static void printPlist(CFArrayRef plist, CFIndex indent, CFIndex maxWidth) { CFIndex count = CFArrayGetCount(plist); CFIndex ix; for (ix = 0; ix < count ; ++ix) { CFDictionaryRef prop = (CFDictionaryRef)CFArrayGetValueAtIndex(plist, ix); CFStringRef pType = (CFStringRef)CFDictionaryGetValue(prop, kSecPropertyKeyType); CFStringRef label = (CFStringRef)CFDictionaryGetValue(prop, kSecPropertyKeyLabel); CFStringRef llabel = (CFStringRef)CFDictionaryGetValue(prop, kSecPropertyKeyLocalizedLabel); CFTypeRef value = (CFTypeRef)CFDictionaryGetValue(prop, kSecPropertyKeyValue); bool isSection = CFEqual(pType, kSecPropertyTypeSection); CFMutableStringRef line = CFStringCreateMutable(NULL, 0); CFIndex jx = 0; for (jx = 0; jx < indent; ++jx) { CFStringAppend(line, CFSTR(" ")); } if (llabel) { CFStringAppend(line, llabel); if (!isSection) { for (jx = CFStringGetLength(llabel) + indent * 4; jx < maxWidth; ++jx) { CFStringAppend(line, CFSTR(" ")); } CFStringAppend(line, CFSTR(" : ")); } } if (CFEqual(pType, kSecPropertyTypeWarning)) { CFStringAppend(line, CFSTR("*WARNING* ")); CFStringAppend(line, (CFStringRef)value); } else if (CFEqual(pType, kSecPropertyTypeError)) { CFStringAppend(line, CFSTR("*ERROR* ")); CFStringAppend(line, (CFStringRef)value); } else if (CFEqual(pType, kSecPropertyTypeSuccess)) { CFStringAppend(line, CFSTR("*OK* ")); CFStringAppend(line, (CFStringRef)value); } else if (CFEqual(pType, kSecPropertyTypeTitle)) { CFStringAppend(line, CFSTR("*")); CFStringAppend(line, (CFStringRef)value); CFStringAppend(line, CFSTR("*")); } else if (CFEqual(pType, kSecPropertyTypeSection)) { } else if (CFEqual(pType, kSecPropertyTypeData)) { CFDataRef data = (CFDataRef)value; CFIndex length = CFDataGetLength(data); if (length > 20) CFStringAppendFormat(line, NULL, CFSTR("[%d bytes] "), length); const UInt8 *bytes = CFDataGetBytePtr(data); for (jx = 0; jx < length; ++jx) { if (jx == 0) CFStringAppendFormat(line, NULL, CFSTR("%02X"), bytes[jx]); else if (jx < 15 || length <= 20) CFStringAppendFormat(line, NULL, CFSTR(" %02X"), bytes[jx]); else { CFStringAppend(line, CFSTR(" ...")); break; } } } else if (CFEqual(pType, kSecPropertyTypeString)) { CFStringAppend(line, (CFStringRef)value); } else if (CFEqual(pType, kSecPropertyTypeDate)) { CFDateRef date = (CFDateRef)value; CFLocaleRef lc = CFLocaleCopyCurrent(); CFDateFormatterRef df = CFDateFormatterCreate(NULL, lc, kCFDateFormatterMediumStyle, kCFDateFormatterLongStyle); CFStringRef ds; if (df) { CFTimeZoneRef tz = CFTimeZoneCreateWithTimeIntervalFromGMT(NULL, 0.0); CFDateFormatterSetProperty(df, kCFDateFormatterTimeZone, tz); CFRelease(tz); ds = CFDateFormatterCreateStringWithDate(NULL, df, date); CFRelease(df); } else { ds = CFStringCreateWithFormat(NULL, NULL, CFSTR("%g"), CFDateGetAbsoluteTime(date)); } CFStringAppend(line, ds); CFRelease(ds); CFRelease(lc); } else if (CFEqual(pType, kSecPropertyTypeURL)) { CFURLRef url = (CFURLRef)value; CFStringAppend(line, CFSTR("<")); CFStringAppend(line, CFURLGetString(url)); CFStringAppend(line, CFSTR(">")); } else { CFStringAppendFormat(line, NULL, CFSTR("*unknown type %@* = %@"), pType, value); } if (!isSection || label) print_line(line); CFRelease(line); if (isSection) { printPlist((CFArrayRef)value, indent + 1, maxWidth); } } }
/* MyPrintDirectoryListing prints a FTP directory entry, represented by a CFDictionary as returned by CFFTPCreateParsedResourceListing, as a single line of text, much like you'd get from "ls -l". */ static void MyPrintDirectoryListing(CFDictionaryRef dictionary) { CFDateRef cfModDate; CFNumberRef cfType, cfMode, cfSize; CFStringRef cfOwner, cfName, cfLink, cfGroup; char owner[256], group[256], name[256]; char permString[12], link[1024]; SInt64 size; SInt32 mode, type; assert(dictionary != NULL); /* You should not assume that the directory entry dictionary will contain all the possible keys. Most of the time it will, however, depending on the FTP server, some of the keys may be missing. */ cfType = CFDictionaryGetValue(dictionary, kCFFTPResourceType); if (cfType) { assert(CFGetTypeID(cfType) == CFNumberGetTypeID()); CFNumberGetValue(cfType, kCFNumberSInt32Type, &type); cfMode = CFDictionaryGetValue(dictionary, kCFFTPResourceMode); if (cfMode) { assert(CFGetTypeID(cfMode) == CFNumberGetTypeID()); CFNumberGetValue(cfMode, kCFNumberSInt32Type, &mode); /* Converts inode status information into a symbolic string */ strmode(mode + DTTOIF(type), permString); fprintf(stderr, "%s ", permString); } } cfOwner = CFDictionaryGetValue(dictionary, kCFFTPResourceOwner); if (cfOwner) { assert(CFGetTypeID(cfOwner) == CFStringGetTypeID()); CFStringGetCString(cfOwner, owner, sizeof(owner), kCFStringEncodingASCII); fprintf(stderr, "%9s", owner); } cfGroup = CFDictionaryGetValue(dictionary, kCFFTPResourceGroup); if (cfGroup) { assert(CFGetTypeID(cfGroup) == CFStringGetTypeID()); CFStringGetCString(cfGroup, group, sizeof(group), kCFStringEncodingASCII); fprintf(stderr, "%9s", group); } cfSize = CFDictionaryGetValue(dictionary, kCFFTPResourceSize); if (cfSize) { assert(CFGetTypeID(cfSize) == CFNumberGetTypeID()); CFNumberGetValue(cfSize, kCFNumberSInt64Type, &size); fprintf(stderr, "%9lld ", size); } cfModDate = CFDictionaryGetValue(dictionary, kCFFTPResourceModDate); if (cfModDate) { CFLocaleRef locale; CFDateFormatterRef formatDate; CFDateFormatterRef formatTime; CFStringRef cfDate; CFStringRef cfTime; char date[256]; char time[256]; assert(CFGetTypeID(cfModDate) == CFDateGetTypeID()); locale = CFLocaleCopyCurrent(); assert(locale != NULL); formatDate = CFDateFormatterCreate(kCFAllocatorDefault, locale, kCFDateFormatterShortStyle, kCFDateFormatterNoStyle ); assert(formatDate != NULL); formatTime = CFDateFormatterCreate(kCFAllocatorDefault, locale, kCFDateFormatterNoStyle, kCFDateFormatterShortStyle); assert(formatTime != NULL); cfDate = CFDateFormatterCreateStringWithDate(kCFAllocatorDefault, formatDate, cfModDate); assert(cfDate != NULL); cfTime = CFDateFormatterCreateStringWithDate(kCFAllocatorDefault, formatTime, cfModDate); assert(cfTime != NULL); CFStringGetCString(cfDate, date, sizeof(date), kCFStringEncodingUTF8); CFStringGetCString(cfTime, time, sizeof(time), kCFStringEncodingUTF8); fprintf(stderr, "%10s %5s ", date, time); CFRelease(cfTime); CFRelease(cfDate); CFRelease(formatTime); CFRelease(formatDate); CFRelease(locale); } /* Note that this sample assumes UTF-8 since that's what the Mac OS X FTP server returns, however, some servers may use a different encoding. */ cfName = CFDictionaryGetValue(dictionary, kCFFTPResourceName); if (cfName) { assert(CFGetTypeID(cfName) == CFStringGetTypeID()); CFStringGetCString(cfName, name, sizeof(name), kCFStringEncodingUTF8); fprintf(stderr, "%s", name); cfLink = CFDictionaryGetValue(dictionary, kCFFTPResourceLink); if (cfLink) { assert(CFGetTypeID(cfLink) == CFStringGetTypeID()); CFStringGetCString(cfLink, link, sizeof(link), kCFStringEncodingUTF8); if (strlen(link) > 0) fprintf(stderr, " -> %s", link); } } fprintf(stderr, "\n"); }