]>
git.saurik.com Git - apple/security.git/blob - utilities/src/SecCFWrappers.c
5 // Created by Michael Brouwer on 11/15/12.
6 // Copyright 2012 Apple Inc. All rights reserved.
9 #include <utilities/SecCFWrappers.h>
11 void CFStringPerformWithCStringAndLength(CFStringRef inStr
, void(^operation
)(const char *utf8String
, size_t utf8Length
)) {
12 const char *cstr
= CFStringGetCStringPtr(inStr
, kCFStringEncodingUTF8
);
14 operation(cstr
, strlen(cstr
));
16 CFIndex neededLen
= 0;
17 CFRange range
= { 0, CFStringGetLength(inStr
) };
18 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
19 0, FALSE
, NULL
, 0, &neededLen
);
21 if (neededLen
< 4096) {
22 char buf
[neededLen
+ 1];
23 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
24 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
25 assert(usedLen
== neededLen
);
27 operation(buf
, (size_t)usedLen
);
28 //cc_zero(neededLen, buf);
30 char *buf
= malloc(neededLen
+ 1);
31 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
32 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
33 assert(usedLen
== neededLen
);
35 operation(buf
, (size_t)usedLen
);
36 //cc_zero(neededLen, buf);
42 void CFStringPerformWithCString(CFStringRef inStr
, void(^operation
)(const char *utf8String
)) {
43 const char *cstr
= CFStringGetCStringPtr(inStr
, kCFStringEncodingUTF8
);
47 CFIndex neededLen
= 0;
48 CFRange range
= { 0, CFStringGetLength(inStr
) };
49 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
50 0, FALSE
, NULL
, 0, &neededLen
);
52 if (neededLen
< 4096) {
53 char buf
[neededLen
+ 1];
54 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
55 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
56 assert(usedLen
== neededLen
);
59 //cc_zero(neededLen, buf);
61 char *buf
= malloc(neededLen
+ 1);
62 CFStringGetBytes(inStr
, range
, kCFStringEncodingUTF8
,
63 0, FALSE
, (UInt8
*)buf
, neededLen
, &usedLen
);
64 assert(usedLen
== neededLen
);
67 //cc_zero(neededLen, buf);