]>
git.saurik.com Git - apple/security.git/blob - Security/utilities/src/SecCFWrappers.c
0c33c803aace81329479bd0c535550d3d2392e2a
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.
30 CFGiblisGetSingleton(CFCalendarRef
, SecCFCalendarGetZulu
, zuluCalendar
, ^{
31 *zuluCalendar
= CFCalendarCreateWithIdentifier(kCFAllocatorDefault
, kCFGregorianCalendar
);
32 CFTimeZoneRef tz
= CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault
, 0.0);
33 CFCalendarSetTimeZone(*zuluCalendar
, tz
);
39 void CFStringPerformWithCStringAndLength(CFStringRef inStr
, void(^operation
)(const char *utf8String
, size_t utf8Length
)) {
40 const char *cstr
= CFStringGetCStringPtr(inStr
, kCFStringEncodingUTF8
);
42 operation(cstr
, strlen(cstr
));
44 CFIndex neededLen
= 0;
45 CFRange range
= { 0, CFStringGetLength(inStr
) };
46 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
47 0, FALSE
, NULL
, 0, &neededLen
);
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
);
55 operation(buf
, (size_t)usedLen
);
56 //cc_zero(neededLen, buf);
58 char *buf
= malloc(neededLen
+ 1);
59 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
60 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
61 assert(usedLen
== neededLen
);
63 operation(buf
, (size_t)usedLen
);
64 //cc_zero(neededLen, buf);
70 void CFStringPerformWithCString(CFStringRef inStr
, void(^operation
)(const char *utf8String
)) {
71 const char *cstr
= CFStringGetCStringPtr(inStr
, kCFStringEncodingUTF8
);
75 CFIndex neededLen
= 0;
76 CFRange range
= { 0, CFStringGetLength(inStr
) };
77 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
78 0, FALSE
, NULL
, 0, &neededLen
);
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
);
87 //cc_zero(neededLen, buf);
89 char *buf
= malloc(neededLen
+ 1);
90 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
91 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
92 assert(usedLen
== neededLen
);
95 //cc_zero(neededLen, buf);