]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmwalkers.h
Security-28.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 #ifdef _CPP_CSSMWALKERS
28 # pragma export on
29 #endif
30
31
32 namespace Security
33 {
34
35 namespace DataWalkers
36 {
37
38 //
39 // Walk an INLINE CSSM_DATA by dealing with the data it points to.
40 // Note that this is not the walker for an OUT OF LINE CSSM_DATA,
41 // which is quite regular and handled below.
42 //
43 template <class Action>
44 void walk(Action &operate, CSSM_DATA &data)
45 {
46 void *p = data.Data;
47 operate(p, data.Length);
48 data.Data = reinterpret_cast<unsigned char *>(p);
49 }
50
51
52 //
53 // Walking a C string is almost regular (the size comes from strlen()).
54 //
55 template <class Action>
56 char *walk(Action &operate, char * &s)
57 {
58 // A string's length is obtained by reading the string value.
59 // We must honor the operator's preference for not calculating length
60 // (e.g. because s won't be valid until some magic thing was done to it).
61 operate(s, operate.needsSize ? (strlen(s) + 1) : 0);
62 return s;
63 }
64
65
66 //
67 // We "walk" an integer by simply returning it unchanged.
68 // This is a degenerate special case that makes some templated
69 // uses of walking easier (notably for Context use). Note that
70 // the action is never called, so operations don't need to be able
71 // to cope with integer (non-ref) arguments. This is strictly for
72 // notational convenience.
73 //
74 template <class Action>
75 uint32 walk(Action &, uint32 arg)
76 {
77 return arg;
78 }
79
80
81 //
82 // Flattener functions for common CSSM data types that have internal
83 // structure. (The flat ones are handled by the default above.)
84 //
85 template <class Action>
86 CssmData *walk(Action &operate, CssmData * &data)
87 {
88 operate(data);
89 walk(operate, *data);
90 return data;
91 }
92
93 template <class Action>
94 CSSM_DATA *walk(Action &operate, CSSM_DATA * &data)
95 { return walk(operate, CssmData::overlayVar(data)); }
96
97 template <class Action>
98 CssmKey *walk(Action &operate, CssmKey * &key)
99 {
100 operate(key);
101 walk(operate, static_cast<CssmData &>(*key));
102 return key;
103 }
104
105 template <class Action>
106 CSSM_KEY *walk(Action &operate, CSSM_KEY * &data)
107 { return walk(operate, CssmKey::overlayVar(data)); }
108
109 template <class Action>
110 CssmCryptoData *walk(Action &operate, CssmCryptoData * &data)
111 {
112 operate(data);
113 walk(operate, data->param());
114 return data;
115 }
116
117 template <class Action>
118 CSSM_CRYPTO_DATA *walk(Action &operate, CSSM_CRYPTO_DATA * &data)
119 { return walk(operate, CssmCryptoData::overlayVar(data)); }
120
121
122 } // end namespace DataWalkers
123
124 } // end namespace Security
125
126 #ifdef _CPP_CSSMWALKERS
127 # pragma export off
128 #endif
129
130 #endif //_H_CSSMWALKERS