]> git.saurik.com Git - apple/security.git/blob - Security/utilities/src/SecCFWrappers.c
Security-57031.30.12.tar.gz
[apple/security.git] / Security / utilities / src / SecCFWrappers.c
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. Must be serialized since it is really a CFMutableCalendarRef
29 // <rdar://problem/16372688> CFCalendarDecomposeAbsoluteTime is not thread safe
30 //
31 static dispatch_queue_t fqueue_cf;
32 static CFCalendarRef sZuluCalendar = NULL;
33
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);
38 });
39 return fqueue_cf;
40 }
41
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);
48 if (tz)
49 CFRelease(tz);
50 });
51 return sZuluCalendar;
52 }
53
54 void SecCFCalendarDoWithZuluCalendar(void(^action)(CFCalendarRef zuluCalendar)) {
55 dispatch_sync(SecCFCalendarGetZuluQueue(), ^{
56 action(SecCFCalendarGetZulu());
57 });
58 }
59
60 void CFStringPerformWithCStringAndLength(CFStringRef inStr, void(^operation)(const char *utf8String, size_t utf8Length)) {
61 const char *cstr = CFStringGetCStringPtr(inStr, kCFStringEncodingUTF8);
62 if (cstr) {
63 operation(cstr, strlen(cstr));
64 } else {
65 CFIndex neededLen = 0;
66 CFRange range = { 0, CFStringGetLength(inStr) };
67 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8,
68 0, FALSE, NULL, 0, &neededLen);
69 CFIndex usedLen = 0;
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);
75 buf[usedLen] = 0;
76 operation(buf, (size_t)usedLen);
77 //cc_zero(neededLen, buf);
78 } else {
79 char *buf = malloc(neededLen + 1);
80 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8,
81 0, FALSE, (UInt8 *)buf, neededLen, &usedLen);
82 assert(usedLen == neededLen);
83 buf[usedLen] = 0;
84 operation(buf, (size_t)usedLen);
85 //cc_zero(neededLen, buf);
86 free(buf);
87 }
88 }
89 }
90
91 void CFStringPerformWithCString(CFStringRef inStr, void(^operation)(const char *utf8String)) {
92 const char *cstr = CFStringGetCStringPtr(inStr, kCFStringEncodingUTF8);
93 if (cstr) {
94 operation(cstr);
95 } else {
96 CFIndex neededLen = 0;
97 CFRange range = { 0, CFStringGetLength(inStr) };
98 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8,
99 0, FALSE, NULL, 0, &neededLen);
100 CFIndex usedLen = 0;
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);
106 buf[usedLen] = 0;
107 operation(buf);
108 //cc_zero(neededLen, buf);
109 } else {
110 char *buf = malloc(neededLen + 1);
111 CFStringGetBytes(inStr, range, kCFStringEncodingUTF8,
112 0, FALSE, (UInt8 *)buf, neededLen, &usedLen);
113 assert(usedLen == neededLen);
114 buf[usedLen] = 0;
115 operation(buf);
116 //cc_zero(neededLen, buf);
117 free(buf);
118 }
119 }
120 }