]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmwalkers.h
Security-164.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / cssmwalkers.h
1 /*
2 * Copyright (c) 2000-2001 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 // cssmwalkers - walkers for standard CSSM datatypes and wrappers
21 //
22 #ifndef _H_CSSMWALKERS
23 #define _H_CSSMWALKERS
24
25 #include <Security/walkers.h>
26
27
28 namespace Security {
29 namespace DataWalkers {
30
31
32 //
33 // The full set of walkers for CssmData in all its forms.
34 //
35 template <class Action>
36 void walk(Action &operate, CssmData &data)
37 {
38 operate(data);
39 operate.blob(data.Data, data.Length);
40 }
41
42 template <class Action>
43 CssmData *walk(Action &operate, CssmData * &data)
44 {
45 operate(data);
46 operate.blob(data->Data, data->Length);
47 return data;
48 }
49
50 template <class Action>
51 void walk(Action &operate, CSSM_DATA &data)
52 { walk(operate, CssmData::overlay(data)); }
53
54 template <class Action>
55 CSSM_DATA *walk(Action &operate, CSSM_DATA * &data)
56 { return walk(operate, CssmData::overlayVar(data)); }
57
58
59
60 //
61 // Walking a C string is almost regular (the size comes from strlen()).
62 // Just make sure you honor the needsSize preference of the operator.
63 //
64 template <class Action>
65 char *walk(Action &operate, char * &s)
66 {
67 operate(s, operate.needsSize ? (strlen(s) + 1) : 0);
68 return s;
69 }
70
71
72 //
73 // We "walk" an integer by simply returning it unchanged.
74 // This is a degenerate special case that makes some templated
75 // uses of walking easier (notably for Context use). Note that
76 // the action is never called, so operations don't need to be able
77 // to cope with integer (non-ref) arguments. This is strictly for
78 // notational convenience.
79 //
80 template <class Action>
81 uint32 walk(Action &, uint32 arg)
82 {
83 return arg;
84 }
85
86
87 //
88 // Flattener functions for common CSSM data types that have internal
89 // structure. (The flat ones are handled by the default above.)
90 //
91 template <class Action>
92 CssmKey *walk(Action &operate, CssmKey * &key)
93 {
94 operate(key);
95 walk(operate, static_cast<CssmData &>(*key));
96 return key;
97 }
98
99 template <class Action>
100 CSSM_KEY *walk(Action &operate, CSSM_KEY * &data)
101 { return walk(operate, CssmKey::overlayVar(data)); }
102
103 template <class Action>
104 CssmCryptoData *walk(Action &operate, CssmCryptoData * &data)
105 {
106 operate(data);
107 walk(operate, data->param());
108 return data;
109 }
110
111 template <class Action>
112 CSSM_CRYPTO_DATA *walk(Action &operate, CSSM_CRYPTO_DATA * &data)
113 { return walk(operate, CssmCryptoData::overlayVar(data)); }
114
115 template <class Action>
116 CSSM_PKCS5_PBKDF2_PARAMS *walk(Action &operate, CSSM_PKCS5_PBKDF2_PARAMS * &data)
117 {
118 operate(data);
119 walk(operate, data->Passphrase);
120 return data;
121 }
122
123
124 } // end namespace DataWalkers
125 } // end namespace Security
126
127 #endif //_H_CSSMWALKERS