]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/cfmunge.h
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / cfmunge.h
1 /*
2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 //
24 // CoreFoundation building and parsing functions
25 //
26 #ifndef _H_CFMUNGE
27 #define _H_CFMUNGE
28
29 #include <security_utilities/cfutilities.h>
30 #include <CoreFoundation/CoreFoundation.h>
31 #include <cstdarg>
32
33 namespace Security {
34
35
36 //
37 // Common interface to Mungers.
38 // A CFMunge provides a one-pass, non-resettable scan through a format string,
39 // performing various actions on the way.
40 //
41 class CFMunge {
42 public:
43 // Initialize a CFMunge. We start out with the default CFAllocator, and
44 // we do not throw errors.
45 // CFMunge consumes the va_list, the caller should call va_copy if necessary.
46 CFMunge(const char *fmt, va_list *args)
47 : format(fmt), args(args), allocator(NULL), error(errSecSuccess) { }
48
49 protected:
50 char next();
51 bool next(char c);
52
53 bool parameter();
54
55 protected:
56 const char *format;
57 va_list *args;
58 CFAllocatorRef allocator;
59 OSStatus error;
60 };
61
62
63 //
64 // A CFMake is a CFMunge for making CF data structures.
65 //
66 class CFMake : public CFMunge {
67 public:
68 CFMake(const char *fmt, va_list *args) : CFMunge(fmt, args) { }
69
70 CFTypeRef make();
71 CFDictionaryRef addto(CFMutableDictionaryRef dict);
72
73 protected:
74 CFTypeRef makedictionary();
75 CFTypeRef makearray();
76 CFTypeRef makenumber();
77 CFTypeRef makestring();
78 CFTypeRef makeformat();
79 CFTypeRef makespecial();
80
81 CFDictionaryRef add(CFMutableDictionaryRef dict);
82 };
83
84
85 //
86 // Make a CF object following a general recipe
87 //
88 CFTypeRef cfmake(const char *format, ...);
89 CFTypeRef vcfmake(const char *format, va_list *args);
90
91 template <class CFType>
92 CFType cfmake(const char *format, ...)
93 {
94 va_list args;
95 va_start(args, format);
96 CFType result = CFType(vcfmake(format, &args));
97 va_end(args);
98 return result;
99 }
100
101 CFDictionaryRef cfadd(CFMutableDictionaryRef dict, const char *format, ...);
102
103
104 //
105 // Parse out parts of a CF object following a general recipe.
106 // Cfscan returns false on error; cfget throws.
107 //
108 bool cfscan(CFTypeRef source, const char *format, ...);
109 bool vcfscan(CFTypeRef source, const char *format, va_list *args);
110
111 CFTypeRef cfget(CFTypeRef source, const char *format, ...);
112 CFTypeRef vcfget(CFTypeRef source, const char *format, va_list *args);
113
114 template <class CFType>
115 CFType cfget(CFTypeRef source, const char *format, ...)
116 {
117 va_list args;
118 va_start(args, format);
119 CFType result = CFType(vcfget(source, format, &args));
120 va_end(args);
121 return (result && CFTraits<CFType>::check(result)) ? result : NULL;
122 }
123
124 template <class CFType>
125 class CFTemp : public CFRef<CFType> {
126 public:
127 CFTemp(const char *format, ...)
128 {
129 va_list args;
130 va_start(args, format);
131 this->take(CFType(vcfmake(format, &args)));
132 va_end(args);
133 }
134 };
135
136
137 } // end namespace Security
138
139 #endif //_H_CFMUNGE