2 * Copyright (c) 2003-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 // transwalkers - server side transition data walking support
28 // These are data walker operators for securely marshaling and unmarshaling
29 // data structures across IPC. They are also in charge of fixing byte order
30 // inconsistencies between server and clients.
32 #include <transwalkers.h>
35 using LowLevelMemoryUtilities::increment
;
36 using LowLevelMemoryUtilities::difference
;
41 return Server::process().byteFlipped();
46 // CheckingRelocateWalkers
48 CheckingReconstituteWalker::CheckingReconstituteWalker(void *ptr
, void *base
, size_t size
, bool flip
)
49 : mBase(base
), mFlip(flip
)
52 Flippers::flip(mBase
); // came in reversed; fix for base use
53 mOffset
= difference(ptr
, mBase
);
54 mLimit
= increment(mBase
, size
);
61 void relocate(Context
&context
, void *base
, Context::Attr
*attrs
, uint32 attrSize
)
64 CheckingReconstituteWalker
relocator(attrs
, base
, attrSize
, flipClient());
65 context
.ContextAttributes
= attrs
; // fix context->attr vector link
66 for (uint32 n
= 0; n
< context
.attributesInUse(); n
++)
67 walk(relocator
, context
[n
]);
72 // Outbound flipping support
74 FlipWalker::~FlipWalker()
76 for (set
<Flipper
>::const_iterator it
= mFlips
.begin(); it
!= mFlips
.end(); it
++)
80 void FlipWalker::doFlips(bool active
)
83 secdebug("flipwalkers", "starting outbound flips");
84 for (set
<Flipper
>::const_iterator it
= mFlips
.begin(); it
!= mFlips
.end(); it
++)
86 secdebug("flipwalkers", "outbound flips done");
92 // Choose a Database from a choice of two sources, giving preference
93 // to persistent stores and to earlier sources.
95 Database
*pickDb(Database
*db1
, Database
*db2
)
97 // persistent db1 always wins
98 if (db1
&& !db1
->transient())
101 // persistent db2 is next choice
102 if (db2
&& !db2
->transient())
105 // pick any existing transient database
111 // none at all. use the canonical transient store
112 return Server::optionalDatabase(noDb
);
117 void fixDbAttributes (CssmDbAttributeData
&data
)
120 NOTE TO FUTURE MAINTAINERS OF THIS CODE:
122 This code is called by two different routines; the relocation walker on the input attributes, and flips
123 on the output attributtes. This is bad, because the relocation walker flips the Info data structure,
124 and flips does not. We could fix this in flips, but flips is a template and does different things
125 depending on what its parameters are. As a result, the best place to do this is here.
128 // pull this data out first, so that it is unperverted once the flip occurs
129 unsigned limit
= data
.size ();
130 unsigned format
= data
.format ();
131 CssmData
* values
= data
.values ();
133 // flip if it is safe to do so
134 if (format
> CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX
) // is the format screwed up?
137 limit
= data
.size ();
138 format
= data
.format ();
139 values
= data
.values ();
144 for (i
= 0; i
< limit
; ++i
)
148 case CSSM_DB_ATTRIBUTE_FORMAT_UINT32
:
149 Flippers::flip(*(uint32
*) values
[i
].data ());
152 case CSSM_DB_ATTRIBUTE_FORMAT_MULTI_UINT32
:
154 CssmData
& d
= values
[i
];
155 int numValues
= d
.length() / sizeof (UInt32
);
157 UInt32
* v
= (UInt32
*) d
.data();
158 for (j
= 0; j
< numValues
; ++j
)
160 Flippers::flip (v
[j
]);
170 void fixDbAttributes (CssmQuery
&query
)
173 unsigned numItems
= query
.size ();
174 for (i
= 0; i
< numItems
; ++i
)
176 fixDbAttributes(query
.predicates()[i
].attribute());
182 void fixDbAttributes (CssmDbRecordAttributeData
&data
)
185 unsigned numItems
= data
.size ();
186 for (i
= 0; i
< numItems
; ++i
)
188 fixDbAttributes(data
.attributes()[i
]);