]> git.saurik.com Git - apple/security.git/blame - OSX/libsecurity_utilities/lib/cfmunge.h
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / cfmunge.h
CommitLineData
b1ab9ed8 1/*
d8f41ccd 2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
b1ab9ed8
A
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
33namespace 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//
41class CFMunge {
42public:
866f8763
A
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) { }
b1ab9ed8
A
48
49protected:
50 char next();
51 bool next(char c);
52
53 bool parameter();
54
55protected:
56 const char *format;
866f8763 57 va_list *args;
b1ab9ed8
A
58 CFAllocatorRef allocator;
59 OSStatus error;
60};
61
62
63//
64// A CFMake is a CFMunge for making CF data structures.
65//
66class CFMake : public CFMunge {
67public:
866f8763 68 CFMake(const char *fmt, va_list *args) : CFMunge(fmt, args) { }
b1ab9ed8
A
69
70 CFTypeRef make();
71 CFDictionaryRef addto(CFMutableDictionaryRef dict);
72
73protected:
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//
88CFTypeRef cfmake(const char *format, ...);
866f8763 89CFTypeRef vcfmake(const char *format, va_list *args);
b1ab9ed8
A
90
91template <class CFType>
92CFType cfmake(const char *format, ...)
93{
94 va_list args;
95 va_start(args, format);
866f8763 96 CFType result = CFType(vcfmake(format, &args));
b1ab9ed8
A
97 va_end(args);
98 return result;
99}
100
101CFDictionaryRef 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//
108bool cfscan(CFTypeRef source, const char *format, ...);
866f8763 109bool vcfscan(CFTypeRef source, const char *format, va_list *args);
b1ab9ed8
A
110
111CFTypeRef cfget(CFTypeRef source, const char *format, ...);
866f8763 112CFTypeRef vcfget(CFTypeRef source, const char *format, va_list *args);
b1ab9ed8
A
113
114template <class CFType>
115CFType cfget(CFTypeRef source, const char *format, ...)
116{
117 va_list args;
118 va_start(args, format);
866f8763 119 CFType result = CFType(vcfget(source, format, &args));
b1ab9ed8
A
120 va_end(args);
121 return (result && CFTraits<CFType>::check(result)) ? result : NULL;
122}
123
124template <class CFType>
125class CFTemp : public CFRef<CFType> {
126public:
127 CFTemp(const char *format, ...)
128 {
129 va_list args;
130 va_start(args, format);
866f8763 131 this->take(CFType(vcfmake(format, &args)));
b1ab9ed8
A
132 va_end(args);
133 }
134};
135
136
137} // end namespace Security
138
139#endif //_H_CFMUNGE