]> git.saurik.com Git - apple/security.git/blob - Keychain/DLDBListCFPref.h
ba74d7a8392dab34a9cdf4978638e4556263cbc5
[apple/security.git] / Keychain / DLDBListCFPref.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * DLDBListCFPref.h
21 */
22 #ifndef _SECURITY_DLDBLISTCFPREF_H_
23 #define _SECURITY_DLDBLISTCFPREF_H_
24
25 #include <Security/cfutilities.h>
26 #include <CoreFoundation/CFDictionary.h>
27 #include <CoreFoundation/CFPreferences.h>
28 #include <Security/DLDBList.h>
29 #include <Security/cssmdb.h>
30 #include <stdexcept>
31 #include <CoreFoundation/CFNumber.h>
32 #include <CoreFoundation/CFDate.h>
33
34
35 namespace Security
36 {
37
38 class DLDbListCFPref : public CssmClient::DLDbList
39 {
40 public:
41 DLDbListCFPref(CFStringRef theDLDbListKey=NULL,CFStringRef prefsDomain=NULL);
42 ~DLDbListCFPref();
43
44 void save();
45 CssmClient::DLDbList& list() { return *this; } // eventually, it should check mod dates of CFPrefs file, etc.
46
47 static DLDbIdentifier cfDictionaryRefToDLDbIdentifier(CFDictionaryRef theDict);
48 static CFDictionaryRef dlDbIdentifierToCFDictionaryRef(const DLDbIdentifier& dldbIdentifier);
49 bool revert(bool force);
50 void clearDefaultKeychain();
51
52 static string ExpandTildesInPath(const string &inPath);
53 static string StripPathStuff(const string &inPath);
54 static string AbbreviatedPath(const string &inPath);
55 static string HomeDir();
56
57 private:
58 // Private member variables
59 CFStringRef mPrefsDomain;
60 CFStringRef mDLDbListKey;
61
62 // Private member functions
63 void loadOrCreate();
64
65 CFAbsoluteTime mPrefsTimeStamp;
66 };
67
68 class CCFValue
69 {
70 public:
71 template <class T>
72 T cfref() const { return reinterpret_cast<T>(CFTypeRef(mRef)); }
73
74 CCFValue() {}
75 CCFValue(CFTypeRef ref) : mRef(ref) {}
76 CCFValue &operator =(CFTypeRef ref) { mRef = ref; return *this; }
77
78 CCFValue &operator = (bool value)
79 {
80 mRef = value?kCFBooleanTrue:kCFBooleanFalse;
81 return *this;
82 }
83
84 /*
85 CCFValue &operator = (const string &value) { string(value); return *this; }
86
87 void string(const string &value, CFStringEncoding encoding=kCFStringEncodingMacRoman)
88 {
89 mRef = CFStringCreate();
90 CFStringRef CFStringCreateWithBytes(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean isExternalRepresentation);
91 if (!mRef) throw std::bad_alloc;
92 CFRelease(mRef);
93 }
94 */
95
96 bool hasValue() const { return mRef; }
97
98 operator bool() const
99 {
100 if (!mRef) return false;
101 if (::CFGetTypeID(mRef) != ::CFBooleanGetTypeID())
102 throw std::logic_error("wrong type in property list");
103
104 return ::CFBooleanGetValue(cfref<CFBooleanRef>());
105 }
106
107 operator sint32() const
108 {
109 if (!mRef) return 0;
110 if (::CFGetTypeID(mRef) != ::CFNumberGetTypeID())
111 throw std::logic_error("wrong type in property list");
112
113 sint32 val;
114 ::CFNumberGetValue(cfref<CFNumberRef>(),kCFNumberSInt32Type,&val);
115 return val;
116 }
117
118 operator uint32() const { return uint32(sint32(*this)); }
119
120 operator const string() const { return getString(); }
121
122 const string getString(CFStringEncoding encoding=kCFStringEncodingMacRoman) const
123 {
124 if (!mRef)
125 throw std::logic_error("missing string in property list");
126 if (::CFGetTypeID(mRef) != ::CFStringGetTypeID())
127 throw std::logic_error("wrong type in property list");
128
129 const char *tmpStr=::CFStringGetCStringPtr(cfref<CFStringRef>(),encoding);
130 if (tmpStr == NULL)
131 {
132 CFIndex maxLen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfref<CFStringRef>()), encoding);
133 auto_array<char> buffer(maxLen + 1);
134
135 if (!::CFStringGetCString(cfref<CFStringRef>(),buffer.get(),maxLen + 1,encoding))
136 throw std::logic_error("could not convert string from property list");
137
138 tmpStr=buffer.get();
139 return string(tmpStr?tmpStr:"");
140 }
141 return string(tmpStr?tmpStr:"");
142 }
143 private:
144 CFCopyRef<CFTypeRef>mRef;
145 };
146
147 } // end namespace Security
148
149 #endif /* !_SECURITY_DLDBLISTCFPREF_H_ */