]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmdb.cpp
Security-30.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / cssmdb.cpp
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 // cssmdb.cpp
20 //
21 //
22
23 #ifdef __MWERKS__
24 #define _CPP_UTILITIES
25 #endif
26
27 #include <Security/cssmdb.h>
28
29 #if 0
30 // XXX Obsolete
31 CSSM_RETURN AddFooToIntelList( void** theIntelListToAddItTo, unsigned long* theNumberOfThingsAlreadyInTheList, const void* theThingToAdd, size_t theSizeOfTheThingToAdd)
32 { // this is to make adding things to Intel LISTs (also called Arrays by the rest of us) easy! We do it everywhere! Join the fun!
33 CSSM_RETURN result = CSSM_OK;
34 void* theReallocatedBuffer = NULL;
35 if( *theIntelListToAddItTo == NULL )
36 {
37
38 *theIntelListToAddItTo = malloc(theSizeOfTheThingToAdd);
39 if(!*theIntelListToAddItTo)
40 {
41 result = CSSMERR_CSSM_MEMORY_ERROR;
42 }
43 }
44 else
45 {
46 theReallocatedBuffer = realloc((void*)*theIntelListToAddItTo, (*theNumberOfThingsAlreadyInTheList+1) * (theSizeOfTheThingToAdd) );
47 if(!theReallocatedBuffer)
48 {
49 result = CSSMERR_CSSM_MEMORY_ERROR;
50 }
51 else
52 {
53 *theIntelListToAddItTo = theReallocatedBuffer;
54 }
55 }
56
57 if(result == CSSM_OK )
58 {
59 memcpy( (void*)((char*)*theIntelListToAddItTo+(theSizeOfTheThingToAdd * (*theNumberOfThingsAlreadyInTheList))), theThingToAdd, theSizeOfTheThingToAdd);
60 (*theNumberOfThingsAlreadyInTheList)++;
61 }
62
63 return result;
64 }
65 #endif
66
67 //
68 // CssmDbAttributeInfo
69 //
70 bool
71 CssmDbAttributeInfo::operator <(const CssmDbAttributeInfo& other) const
72 {
73 if (nameFormat() < other.nameFormat()) return true;
74 if (other.nameFormat() < nameFormat()) return false;
75 // nameFormat's are equal.
76 switch (nameFormat())
77 {
78 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING:
79 {
80 int res = strcmp(static_cast<const char *>(*this), static_cast<const char *>(other));
81 if (res < 0) return true;
82 if (res > 0) return false;
83 break;
84 }
85 case CSSM_DB_ATTRIBUTE_NAME_AS_OID:
86 if (static_cast<const CssmOid &>(*this) < static_cast<const CssmOid &>(other)) return true;
87 if (static_cast<const CssmOid &>(other) < static_cast<const CssmOid &>(*this)) return false;
88 break;
89 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER:
90 if (static_cast<uint32>(*this) < static_cast<uint32>(other)) return true;
91 if (static_cast<uint32>(other) < static_cast<uint32>(*this)) return false;
92 break;
93 default:
94 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME);
95 }
96
97 return format() < other.format();
98 }
99
100 bool
101 CssmDbAttributeInfo::operator ==(const CssmDbAttributeInfo& other) const
102 {
103 if (nameFormat() != other.nameFormat()) return false;
104 if (format() != other.format()) return false;
105 switch (nameFormat())
106 {
107 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING:
108 return !strcmp(static_cast<const char *>(*this), static_cast<const char *>(other));
109 case CSSM_DB_ATTRIBUTE_NAME_AS_OID:
110 return static_cast<const CssmOid &>(*this) == static_cast<const CssmOid &>(other);
111 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER:
112 return static_cast<uint32>(*this) == static_cast<uint32>(other);
113 default:
114 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME);
115 }
116 }
117
118 //
119 // CssmDbAttributeData
120 //
121 void
122 CssmDbAttributeData::deleteValues(CssmAllocator &inAllocator)
123 {
124 // Loop over all values and delete each one.
125 if (Value)
126 {
127 for (uint32 anIndex = 0; anIndex < NumberOfValues; anIndex++)
128 {
129 if (Value[anIndex].Length)
130 {
131 inAllocator.free(Value[anIndex].Data);
132 Value[anIndex].Length = 0;
133 }
134 }
135
136 inAllocator.free(Value);
137 Value = NULL;
138 }
139
140 NumberOfValues = 0;
141 }
142
143 bool
144 CssmDbAttributeData::operator <(const CssmDbAttributeData &other) const
145 {
146 if (info() < other.info()) return true;
147 if (other.info() < info()) return false;
148
149 uint32 minSize = min(size(), other.size());
150 for (uint32 ix = 0; ix < minSize; ++ix)
151 {
152 if (at<const CssmData &>(ix) < other.at<const CssmData &>(ix))
153 return true;
154 if (other.at<const CssmData &>(ix) < at<const CssmData &>(ix))
155 return false;
156 }
157
158 return size() < other.size();
159 }
160
161 void
162 CssmDbAttributeData::add(const CssmDbAttributeData &src, CssmAllocator &inAllocator)
163 {
164 // Add all the values from another attribute into this attribute.
165
166 Value = reinterpret_cast<CSSM_DATA *>(inAllocator.realloc(Value,
167 sizeof(*Value) * (NumberOfValues + src.NumberOfValues)));
168
169 for (uint32 srcIndex = 0; srcIndex < src.NumberOfValues; srcIndex++) {
170 uint32 destIndex = NumberOfValues + srcIndex;
171
172 Value[destIndex].Length = 0;
173 Value[destIndex].Data = inAllocator.alloc<uint8>(src.Value[srcIndex].Length);
174 Value[destIndex].Length = src.Value[srcIndex].Length;
175 memcpy(Value[destIndex].Data, src.Value[srcIndex].Data, src.Value[srcIndex].Length);
176 }
177
178 NumberOfValues += src.NumberOfValues;
179 }
180
181 bool
182 CssmDbAttributeData::deleteValue(const CssmData &src, CssmAllocator &inAllocator)
183 {
184 // Delete a single value from this attribute, if it is present.
185
186 for (uint32 i = 0; i < NumberOfValues; i++)
187 if (CssmData::overlay(Value[i]) == src)
188 {
189 inAllocator.free(Value[i].Data);
190 Value[i].Length = 0;
191
192 NumberOfValues--;
193 Value[i].Data = Value[NumberOfValues].Data;
194 Value[i].Length = Value[NumberOfValues].Length;
195
196 return true;
197 }
198
199 return false;
200 }
201
202 // Delete those values found in src from this object, if they are present.
203 // Warning: This is O(N^2) worst case; if this becomes a performance bottleneck
204 // then it will need to be changed.
205
206 void
207 CssmDbAttributeData::deleteValues(const CssmDbAttributeData &src, CssmAllocator &inAllocator)
208 {
209 for (uint32 i = 0; i < src.NumberOfValues; i++)
210 deleteValue(CssmData::overlay(src.Value[i]), inAllocator);
211 }
212
213 //
214 // CssmDbRecordAttributeData
215 //
216 CssmDbAttributeData *
217 CssmDbRecordAttributeData::find(const CSSM_DB_ATTRIBUTE_INFO &inInfo)
218 {
219 const CssmDbAttributeInfo &anInfo = CssmDbAttributeInfo::overlay(inInfo);
220 for (uint32 ix = 0; ix < size(); ++ix)
221 {
222 if (at(ix).info() == anInfo)
223 return &at(ix);
224 }
225
226 return NULL;
227 }
228
229 bool
230 CssmDbRecordAttributeData::operator <(const CssmDbRecordAttributeData &other) const
231 {
232 if (recordType() < other.recordType()) return true;
233 if (other.recordType() < recordType()) return false;
234 if (semanticInformation() < other.semanticInformation()) return true;
235 if (other.semanticInformation() < semanticInformation()) return false;
236
237 uint32 minSize = min(size(), other.size());
238 for (uint32 ix = 0; ix < minSize; ++ix)
239 {
240 if (at(ix) < other.at(ix))
241 return true;
242 if (other.at(ix) < at(ix))
243 return false;
244 }
245
246 return size() < other.size();
247 }
248
249
250 //
251 // CssmAutoDbRecordAttributeData
252 //
253 CssmAutoDbRecordAttributeData::~CssmAutoDbRecordAttributeData()
254 {
255 clear();
256 }
257
258 void
259 CssmAutoDbRecordAttributeData::clear()
260 {
261 deleteValues();
262 ArrayBuilder<CssmDbAttributeData>::clear();
263 }
264
265 CssmDbAttributeData &
266 CssmAutoDbRecordAttributeData::add(const CSSM_DB_ATTRIBUTE_INFO &info)
267 {
268 CssmDbAttributeData &anAttr = add();
269 anAttr.info(info);
270 return anAttr;
271 }
272
273 CssmDbAttributeData &
274 CssmAutoDbRecordAttributeData::add(const CSSM_DB_ATTRIBUTE_INFO &info, const CssmPolyData &value)
275 {
276 CssmDbAttributeData &anAttr = add();
277 anAttr.set(info, value, mValueAllocator);
278 return anAttr;
279 }
280
281 //
282 // CssmAutoQuery
283 //
284 CssmAutoQuery::CssmAutoQuery(const CSSM_QUERY &query, CssmAllocator &allocator)
285 : ArrayBuilder<CssmSelectionPredicate>(static_cast<CssmSelectionPredicate *>(SelectionPredicate),
286 NumSelectionPredicates,
287 query.NumSelectionPredicates, allocator)
288 {
289 RecordType = query.RecordType;
290 Conjunctive = query.Conjunctive;
291 QueryLimits = query.QueryLimits;
292 QueryFlags = query.QueryFlags;
293 for (uint32 ix = 0; ix < query.NumSelectionPredicates; ++ix)
294 add().set(query.SelectionPredicate[ix], allocator);
295 }
296
297 CssmAutoQuery::~CssmAutoQuery()
298 {
299 clear();
300 }
301
302 void
303 CssmAutoQuery::clear()
304 {
305 deleteValues();
306 ArrayBuilder<CssmSelectionPredicate>::clear();
307 }
308
309 CssmSelectionPredicate &
310 CssmAutoQuery::add(CSSM_DB_OPERATOR dbOperator, const CSSM_DB_ATTRIBUTE_INFO &info, const CssmPolyData &value)
311 {
312 CssmSelectionPredicate &predicate = add();
313 predicate.dbOperator(dbOperator);
314 predicate.set(info, value, allocator());
315 return predicate;
316 }