]> git.saurik.com Git - apple/security.git/blob - keychain/tpctl/tpctl-objc.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / tpctl / tpctl-objc.m
1
2 #import "tpctl-objc.h"
3
4 @implementation TPCTLObjectiveC
5
6 + (BOOL)catchNSException:(void(^)(void))block error:(NSError**)error {
7 @try {
8 block();
9 return true;
10 }
11 @catch(NSException* exception) {
12 if(error) {
13 NSMutableDictionary* ui = exception.userInfo ? [exception.userInfo mutableCopy] : [NSMutableDictionary dictionary];
14 if(exception.reason) {
15 ui[NSLocalizedDescriptionKey] = exception.reason;
16 }
17 *error = [NSError errorWithDomain:exception.name code:0 userInfo:ui];
18 }
19 return false;
20 }
21 }
22
23 + (NSString* _Nullable)jsonSerialize:(id)something error:(NSError**)error {
24 @try {
25 NSError* localError = nil;
26 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:something options:(NSJSONWritingPrettyPrinted | NSJSONWritingSortedKeys) error:&localError];
27 if(!jsonData || localError) {
28 if(error) {
29 *error = localError;
30 }
31 return nil;
32 }
33
34 NSString* utf8String = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
35 if(!utf8String) {
36 if(error) {
37 *error = [NSError errorWithDomain:@"text" code:0 userInfo:@{NSLocalizedDescriptionKey: @"JSON data could not be decoded as UTF8"}];
38 }
39 return nil;
40 }
41 return utf8String;
42 }
43 @catch(NSException* exception) {
44 if(error) {
45 NSMutableDictionary* ui = exception.userInfo ? [exception.userInfo mutableCopy] : [NSMutableDictionary dictionary];
46 if(exception.reason) {
47 ui[NSLocalizedDescriptionKey] = exception.reason;
48 }
49 *error = [NSError errorWithDomain:exception.name code:0 userInfo:ui];
50 }
51 return nil;
52 }
53 }
54
55 @end