1/* 2 * Copyright (c) 2012,2014 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 25#include <utilities/SecCFWrappers.h> 26 27// 28// Global sigleton Zulu time. 29// 30CFGiblisGetSingleton(CFCalendarRef, SecCFCalendarGetZulu, zuluCalendar, ^{ 31 *zuluCalendar = CFCalendarCreateWithIdentifier(kCFAllocatorDefault, kCFGregorianCalendar); 32 CFTimeZoneRef tz = CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0.0); 33 CFCalendarSetTimeZone(*zuluCalendar, tz); 34 CFReleaseSafe(tz); 35}) 36 37 38 39void CFStringPerformWithCStringAndLength(CFStringRef inStr, void(^operation)(const char *utf8String, size_t utf8Length)) { 40 const char *cstr = CFStringGetCStringPtr(inStr, kCFStringEncodingUTF8); 41 if (cstr) { 42 operation(cstr, strlen(cstr)); 43 } else { 44 CFIndex neededLen = 0; 45 CFRange range = { 0, CFStringGetLength(inStr) }; 46 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8, 47 0, FALSE, NULL, 0, &neededLen); 48 CFIndex usedLen = 0; 49 if (neededLen < 4096) { 50 char buf[neededLen + 1]; 51 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8, 52 0, FALSE, (UInt8 *)buf, neededLen, &usedLen); 53 assert(usedLen == neededLen); 54 buf[usedLen] = 0; 55 operation(buf, (size_t)usedLen); 56 //cc_zero(neededLen, buf); 57 } else { 58 char *buf = malloc(neededLen + 1); 59 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8, 60 0, FALSE, (UInt8 *)buf, neededLen, &usedLen); 61 assert(usedLen == neededLen); 62 buf[usedLen] = 0; 63 operation(buf, (size_t)usedLen); 64 //cc_zero(neededLen, buf); 65 free(buf); 66 } 67 } 68} 69 70void CFStringPerformWithCString(CFStringRef inStr, void(^operation)(const char *utf8String)) { 71 const char *cstr = CFStringGetCStringPtr(inStr, kCFStringEncodingUTF8); 72 if (cstr) { 73 operation(cstr); 74 } else { 75 CFIndex neededLen = 0; 76 CFRange range = { 0, CFStringGetLength(inStr) }; 77 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8, 78 0, FALSE, NULL, 0, &neededLen); 79 CFIndex usedLen = 0; 80 if (neededLen < 4096) { 81 char buf[neededLen + 1]; 82 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8, 83 0, FALSE, (UInt8 *)buf, neededLen, &usedLen); 84 assert(usedLen == neededLen); 85 buf[usedLen] = 0; 86 operation(buf); 87 //cc_zero(neededLen, buf); 88 } else { 89 char *buf = malloc(neededLen + 1); 90 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8, 91 0, FALSE, (UInt8 *)buf, neededLen, &usedLen); 92 assert(usedLen == neededLen); 93 buf[usedLen] = 0; 94 operation(buf); 95 //cc_zero(neededLen, buf); 96 free(buf); 97 } 98 } 99} 100