2 * Copyright (c) 2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <utilities/SecCFWrappers.h>
28 // Global sigleton Zulu time. Must be serialized since it is really a CFMutableCalendarRef
29 // <rdar://problem/16372688> CFCalendarDecomposeAbsoluteTime is not thread safe
31 static dispatch_queue_t fqueue_cf
;
32 static CFCalendarRef sZuluCalendar
= NULL
;
34 static dispatch_queue_t
SecCFCalendarGetZuluQueue() {
35 static dispatch_once_t onceToken
;
36 dispatch_once(&onceToken
, ^{
37 fqueue_cf
= dispatch_queue_create("ZuluCalendar", DISPATCH_QUEUE_SERIAL
);
42 static CFCalendarRef
SecCFCalendarGetZulu() {
43 static dispatch_once_t onceToken
;
44 dispatch_once(&onceToken
, ^{
45 sZuluCalendar
= CFCalendarCreateWithIdentifier(kCFAllocatorDefault
, kCFGregorianCalendar
);
46 CFTimeZoneRef tz
= CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault
, 0.0);
47 CFCalendarSetTimeZone(sZuluCalendar
, tz
);
54 void SecCFCalendarDoWithZuluCalendar(void(^action
)(CFCalendarRef zuluCalendar
)) {
55 dispatch_sync(SecCFCalendarGetZuluQueue(), ^{
56 action(SecCFCalendarGetZulu());
60 void CFStringPerformWithCStringAndLength(CFStringRef inStr
, void(^operation
)(const char *utf8String
, size_t utf8Length
)) {
61 const char *cstr
= CFStringGetCStringPtr(inStr
, kCFStringEncodingUTF8
);
63 operation(cstr
, strlen(cstr
));
65 CFIndex neededLen
= 0;
66 CFRange range
= { 0, CFStringGetLength(inStr
) };
67 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
68 0, FALSE
, NULL
, 0, &neededLen
);
70 if (neededLen
< 4096) {
71 char buf
[neededLen
+ 1];
72 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
73 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
74 assert(usedLen
== neededLen
);
76 operation(buf
, (size_t)usedLen
);
77 //cc_zero(neededLen, buf);
79 char *buf
= malloc(neededLen
+ 1);
80 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
81 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
82 assert(usedLen
== neededLen
);
84 operation(buf
, (size_t)usedLen
);
85 //cc_zero(neededLen, buf);
91 void CFStringPerformWithCString(CFStringRef inStr
, void(^operation
)(const char *utf8String
)) {
92 const char *cstr
= CFStringGetCStringPtr(inStr
, kCFStringEncodingUTF8
);
96 CFIndex neededLen
= 0;
97 CFRange range
= { 0, CFStringGetLength(inStr
) };
98 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
99 0, FALSE
, NULL
, 0, &neededLen
);
101 if (neededLen
< 4096) {
102 char buf
[neededLen
+ 1];
103 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
104 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
105 assert(usedLen
== neededLen
);
108 //cc_zero(neededLen, buf);
110 char *buf
= malloc(neededLen
+ 1);
111 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
112 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
113 assert(usedLen
== neededLen
);
116 //cc_zero(neededLen, buf);