OpenVPN 3 Core Library
Loading...
Searching...
No Matches
cfhelper.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_APPLECRYPTO_CF_CFHELPER_H
13#define OPENVPN_APPLECRYPTO_CF_CFHELPER_H
14
17
18// These methods build on the Wrapper classes for Apple Core Foundation objects
19// defined in cf.hpp. They add additional convenience methods, such as dictionary
20// lookup.
21
22namespace openvpn::CF {
23
24// essentially a vector of void *, used as source for array and dictionary constructors
26
27inline Array array(const SrcList &values)
28{
29 return array((const void **)values.c_data(), values.size());
30}
31
32inline Dict dict(const SrcList &keys, const SrcList &values)
33{
34 return dict((const void **)keys.c_data(), (const void **)values.c_data(), std::min(keys.size(), values.size()));
35}
36
37inline CFTypeRef mutable_dict_new()
38{
39 return CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
40}
41
42inline CFTypeRef mutable_array_new()
43{
44 return CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
45}
46
47// Lookup or create (if absent) an item in a mutable dictionary.
48// Return the item, which will be owned by base.
49template <typename KEY>
50inline CFTypeRef dict_get_create(CFMutableDictionaryRef base,
51 const KEY &key,
52 CFTypeRef (*create_method)())
53{
54 if (base)
55 {
56 String keystr = string(key);
57 CFTypeRef ret = CFDictionaryGetValue(base, keystr()); // try lookup first
58 if (!ret)
59 {
60 // doesn't exist, must create
61 ret = (*create_method)();
62 CFDictionaryAddValue(base, keystr(), ret);
63 CFRelease(ret); // because ret is now owned by base
64 }
65 return ret;
66 }
67 return nullptr;
68}
69
70// lookup a dict in another dict (base) and return or create if absent
71template <typename KEY>
72inline MutableDict dict_get_create_dict(MutableDict &base, const KEY &key)
73{
74 String keystr = string(key);
75 return mutable_dict_cast(dict_get_create(base(), keystr(), mutable_dict_new));
76}
77
78// lookup an array in a dict (base) and return or create if absent
79template <typename KEY>
80inline MutableArray dict_get_create_array(MutableDict &base, const KEY &key)
81{
82 String keystr = string(key);
83 return mutable_array_cast(dict_get_create(base(), keystr(), mutable_array_new));
84}
85
86// lookup an object in a dictionary (DICT should be a Dict or a MutableDict)
87template <typename DICT, typename KEY>
88inline CFTypeRef dict_get_obj(const DICT &dict, const KEY &key)
89{
90 return dict_index(dict, key);
91}
92
93// lookup a string in a dictionary (DICT should be a Dict or a MutableDict)
94template <typename DICT, typename KEY>
95inline std::string dict_get_str(const DICT &dict, const KEY &key)
96{
97 return cppstring(string_cast(dict_index(dict, key)));
98}
99
100// lookup a string in a dictionary (DICT should be a Dict or a MutableDict)
101template <typename DICT, typename KEY>
102inline std::string dict_get_str(const DICT &dict, const KEY &key, const std::string &default_value)
103{
104 String str(string_cast(dict_index(dict, key)));
105 if (str.defined())
106 return cppstring(str());
107 else
108 return default_value;
109}
110
111// lookup an integer in a dictionary (DICT should be a Dict or a MutableDict)
112template <typename DICT, typename KEY>
113inline int dict_get_int(const DICT &dict, const KEY &key, const int default_value)
114{
115 int ret;
116 Number num = number_cast(dict_index(dict, key));
117 if (num.defined() && CFNumberGetValue(num(), kCFNumberIntType, &ret))
118 return ret;
119 else
120 return default_value;
121}
122
123// lookup a boolean in a dictionary (DICT should be a Dict or a MutableDict)
124template <typename DICT, typename KEY>
125inline bool dict_get_bool(const DICT &dict, const KEY &key, const bool default_value)
126{
127 Bool b = bool_cast(dict_index(dict, key));
128 if (b.defined())
129 {
130 if (b() == kCFBooleanTrue)
131 return true;
132 else if (b() == kCFBooleanFalse)
133 return false;
134 }
135 return default_value;
136}
137
138// like CFDictionarySetValue, but no-op if any args are NULL
139inline void dictionarySetValue(CFMutableDictionaryRef theDict, const void *key, const void *value)
140{
141 if (theDict && key && value)
142 CFDictionarySetValue(theDict, key, value);
143}
144
145// like CFArrayAppendValue, but no-op if any args are NULL
146inline void arrayAppendValue(CFMutableArrayRef theArray, const void *value)
147{
148 if (theArray && value)
149 CFArrayAppendValue(theArray, value);
150}
151
152// set a CFTypeRef in a mutable dictionary
153template <typename KEY>
154inline void dict_set_obj(MutableDict &dict, const KEY &key, CFTypeRef value)
155{
156 String keystr = string(key);
157 dictionarySetValue(dict(), keystr(), value);
158}
159
160// set a string in a mutable dictionary
161
162template <typename KEY, typename VALUE>
163inline void dict_set_str(MutableDict &dict, const KEY &key, const VALUE &value)
164{
165 String keystr = string(key);
166 String valstr = string(value);
167 dictionarySetValue(dict(), keystr(), valstr());
168}
169
170// set a number in a mutable dictionary
171
172template <typename KEY>
173inline void dict_set_int(MutableDict &dict, const KEY &key, int value)
174{
175 String keystr = string(key);
176 Number num = number_from_int(value);
177 dictionarySetValue(dict(), keystr(), num());
178}
179
180template <typename KEY>
181inline void dict_set_int32(MutableDict &dict, const KEY &key, SInt32 value)
182{
183 String keystr = string(key);
184 Number num = number_from_int32(value);
185 dictionarySetValue(dict(), keystr(), num());
186}
187
188template <typename KEY>
189inline void dict_set_long_long(MutableDict &dict, const KEY &key, long long value)
190{
191 String keystr = string(key);
192 Number num = number_from_long_long(value);
193 dictionarySetValue(dict(), keystr(), num());
194}
195
196template <typename KEY>
197inline void dict_set_index(MutableDict &dict, const KEY &key, CFIndex value)
198{
199 String keystr = string(key);
200 Number num = number_from_index(value);
201 dictionarySetValue((CFMutableDictionaryRef)dict(), keystr(), num());
202}
203
204// set a boolean in a mutable dictionary
205
206template <typename KEY>
207inline void dict_set_bool(MutableDict &dict, const KEY &key, bool value)
208{
209 String keystr = string(key);
210 CFBooleanRef boolref = value ? kCFBooleanTrue : kCFBooleanFalse;
211 dictionarySetValue(dict(), keystr(), boolref);
212}
213
214// append string to a mutable array
215
216template <typename VALUE>
217inline void array_append_str(MutableArray &array, const VALUE &value)
218{
219 String valstr = string(value);
220 arrayAppendValue(array(), valstr());
221}
222
223// append a number to a mutable array
224
225inline void array_append_int(MutableArray &array, int value)
226{
227 Number num = number_from_int(value);
228 arrayAppendValue(array(), num());
229}
230
231inline void array_append_int32(MutableArray &array, SInt32 value)
232{
233 Number num = number_from_int32(value);
234 arrayAppendValue(array(), num());
235}
236
237inline void array_append_long_long(MutableArray &array, long long value)
238{
239 Number num = number_from_long_long(value);
240 arrayAppendValue(array(), num());
241}
242
243inline void array_append_index(MutableArray &array, CFIndex value)
244{
245 Number num = number_from_index(value);
246 arrayAppendValue(array(), num());
247}
248} // namespace openvpn::CF
249#endif
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1194
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1242
void dict_set_bool(MutableDict &dict, const KEY &key, bool value)
Definition cfhelper.hpp:207
MutableDict dict_get_create_dict(MutableDict &base, const KEY &key)
Definition cfhelper.hpp:72
void dictionarySetValue(CFMutableDictionaryRef theDict, const void *key, const void *value)
Definition cfhelper.hpp:139
BufferAllocatedType< CFTypeRef > SrcList
Definition cfhelper.hpp:25
void dict_set_int(MutableDict &dict, const KEY &key, int value)
Definition cfhelper.hpp:173
int dict_get_int(const DICT &dict, const KEY &key, const int default_value)
Definition cfhelper.hpp:113
CFTypeRef mutable_array_new()
Definition cfhelper.hpp:42
Array array(const void **values, CFIndex numValues)
Definition cf.hpp:271
Number number_from_int(const int n)
Definition cf.hpp:246
void array_append_int32(MutableArray &array, SInt32 value)
Definition cfhelper.hpp:231
void dict_set_str(MutableDict &dict, const KEY &key, const VALUE &value)
Definition cfhelper.hpp:163
void dict_set_index(MutableDict &dict, const KEY &key, CFIndex value)
Definition cfhelper.hpp:197
void array_append_long_long(MutableArray &array, long long value)
Definition cfhelper.hpp:237
void array_append_int(MutableArray &array, int value)
Definition cfhelper.hpp:225
Number number_from_long_long(const long long n)
Definition cf.hpp:256
String string(const char *str)
Definition cf.hpp:221
CFTypeRef dict_get_obj(const DICT &dict, const KEY &key)
Definition cfhelper.hpp:88
void dict_set_int32(MutableDict &dict, const KEY &key, SInt32 value)
Definition cfhelper.hpp:181
void dict_set_obj(MutableDict &dict, const KEY &key, CFTypeRef value)
Definition cfhelper.hpp:154
CFTypeRef dict_get_create(CFMutableDictionaryRef base, const KEY &key, CFTypeRef(*create_method)())
Definition cfhelper.hpp:50
void array_append_str(MutableArray &array, const VALUE &value)
Definition cfhelper.hpp:217
void arrayAppendValue(CFMutableArrayRef theArray, const void *value)
Definition cfhelper.hpp:146
std::string cppstring(CFStringRef str)
Definition cf.hpp:381
Number number_from_index(const CFIndex n)
Definition cf.hpp:261
MutableArray dict_get_create_array(MutableDict &base, const KEY &key)
Definition cfhelper.hpp:80
void dict_set_long_long(MutableDict &dict, const KEY &key, long long value)
Definition cfhelper.hpp:189
Number number_from_int32(const SInt32 n)
Definition cf.hpp:251
Dict dict(const void **keys, const void **values, CFIndex numValues)
Definition cf.hpp:276
bool dict_get_bool(const DICT &dict, const KEY &key, const bool default_value)
Definition cfhelper.hpp:125
void array_append_index(MutableArray &array, CFIndex value)
Definition cfhelper.hpp:243
std::string dict_get_str(const DICT &dict, const KEY &key)
Definition cfhelper.hpp:95
CFTypeRef dict_index(const DICT &dict, const KEY &key)
Definition cf.hpp:360
CFTypeRef mutable_dict_new()
Definition cfhelper.hpp:37
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::string ret