]> git.saurik.com Git - apple/security.git/blob - libsecurity_cdsa_utilities/lib/cssmdb.h
Security-55179.11.tar.gz
[apple/security.git] / libsecurity_cdsa_utilities / lib / cssmdb.h
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All Rights Reserved.
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
25 // cssmdb.h
26 //
27 // classes for the DL related data structures
28 //
29
30 #ifndef _H_CDSA_UTILITIES_CSSMDB
31 #define _H_CDSA_UTILITIES_CSSMDB
32
33 #include <security_cdsa_utilities/cssmdata.h>
34 #include <security_cdsa_utilities/cssmpods.h>
35 #include <security_cdsa_utilities/cssmalloc.h>
36 #include <security_cdsa_utilities/walkers.h>
37 #include <security_cdsa_utilities/cssmdbname.h>
38
39
40 namespace Security {
41
42
43 //
44 // Template class to build and maintain external arrays.
45 // Feel free to add and vector<> member functions and behaviours as needed.
46 //
47 // This class differs from vector mainly because it does not construct or
48 // destruct any of the elements it contains. Rather it zero fills the
49 // storage and returns references to elements.
50 // Also it does not implement insert(), erase() or assign(). It does implement
51 // which is equivalent to calling *insert(end()) on a vector.
52 //
53 template <class _Tp>
54 class ArrayBuilder {
55 public:
56 typedef _Tp value_type;
57 typedef value_type* pointer;
58 typedef const value_type* const_pointer;
59 typedef value_type* iterator;
60 typedef const value_type* const_iterator;
61 typedef value_type& reference;
62 typedef const value_type& const_reference;
63 typedef uint32 size_type;
64 typedef ptrdiff_t difference_type;
65
66 typedef reverse_iterator<const_iterator> const_reverse_iterator;
67 typedef reverse_iterator<iterator> reverse_iterator;
68
69 protected:
70 void insert_aux(iterator __position, const _Tp& __x);
71 void insert_aux(iterator __position);
72
73 public:
74 iterator begin() { return mArray; }
75 const_iterator begin() const { return mArray; }
76 iterator end() { return &mArray[mSize]; }
77 const_iterator end() const { return &mArray[mSize]; }
78
79 reverse_iterator rbegin()
80 { return reverse_iterator(end()); }
81 const_reverse_iterator rbegin() const
82 { return const_reverse_iterator(end()); }
83 reverse_iterator rend()
84 { return reverse_iterator(begin()); }
85 const_reverse_iterator rend() const
86 { return const_reverse_iterator(begin()); }
87
88 // Must be defined in base class.
89 //size_type size() const
90 //{ return mSize; }
91 size_type max_size() const
92 { return size_type(-1) / sizeof(_Tp); }
93 size_type capacity() const
94 { return mCapacity; }
95 bool empty() const
96 { return begin() == end(); }
97
98 ArrayBuilder(pointer &array, size_type &size, size_type capacity = 0, Allocator &allocator = Allocator::standard()) :
99 mArray(array), mSize(size), mCapacity(capacity), mAllocator(allocator)
100 {
101 #if BUG_GCC
102 mArray = reinterpret_cast<pointer>(mAllocator.malloc(sizeof(value_type) * mCapacity));
103 #else
104 mArray = reinterpret_cast<pointer>(mAllocator.malloc(sizeof(value_type) * mCapacity));
105 //mArray = mAllocator.alloc(mCapacity);
106 #endif
107 memset(mArray, 0, sizeof(value_type) * mCapacity);
108 mSize = 0;
109 }
110 ~ArrayBuilder() { mAllocator.free(mArray); }
111
112 reference front() { return *begin(); }
113 const_reference front() const { return *begin(); }
114 reference back() { return *(end() - 1); }
115 const_reference back() const { return *(end() - 1); }
116
117 void reserve(size_type newCapacity)
118 {
119 if (newCapacity > mCapacity)
120 {
121 #if BUG_GCC
122 mArray = reinterpret_cast<pointer>(mAllocator.realloc(mArray, sizeof(value_type) * newCapacity));
123 #else
124 mArray = reinterpret_cast<pointer>(mAllocator.realloc(mArray, sizeof(value_type) * newCapacity));
125 //mArray = mAllocator.realloc<value_type>(mArray, newCapacity));
126 #endif
127 memset(&mArray[mCapacity], 0, sizeof(value_type) * (newCapacity - mCapacity));
128 mCapacity = newCapacity;
129 }
130 }
131
132 // XXX Replace by push_back and insert.
133 reference add()
134 {
135 if (mSize >= mCapacity)
136 reserve(max(mSize + 1, mCapacity ? 2 * mCapacity : 1));
137
138 return mArray[mSize++];
139 }
140
141 const_pointer get() const { return mArray; }
142 pointer release() { const_pointer array = mArray; mArray = NULL; return array; }
143 void clear() { if (mSize) { memset(mArray, 0, sizeof(value_type) * mSize); } mSize = 0; }
144
145 // Must be defined in base class.
146 //reference at(size_type ix) { return mArray[ix]; }
147 //const_reference at(size_type ix) const { return mArray[ix]; }
148 //reference operator[] (size_type ix) { assert(ix < size()); return at(ix); }
149 //const_reference operator[] (size_type ix) const { assert(ix < size()); return at(ix); }
150 protected:
151 Allocator &allocator() const { return mAllocator; }
152
153 private:
154
155 pointer &mArray;
156 size_type &mSize;
157 size_type mCapacity;
158 Allocator &mAllocator;
159 };
160
161
162 //
163 // A CSSM_DL_DB_LIST wrapper.
164 // Note that there is a DLDBList class elsewhere that is quite
165 // unrelated to this structure.
166 //
167 class CssmDlDbHandle : public PodWrapper<CssmDlDbHandle, CSSM_DL_DB_HANDLE> {
168 public:
169 CssmDlDbHandle() { clearPod(); }
170 CssmDlDbHandle(CSSM_DL_HANDLE dl, CSSM_DB_HANDLE db) { DLHandle = dl; DBHandle = db; }
171
172 CSSM_DL_HANDLE dl() const { return DLHandle; }
173 CSSM_DB_HANDLE db() const { return DBHandle; }
174
175 operator bool() const { return DLHandle && DBHandle; }
176 };
177
178 inline bool operator < (const CSSM_DL_DB_HANDLE &h1, const CSSM_DL_DB_HANDLE &h2)
179 {
180 return h1.DLHandle < h2.DLHandle
181 || (h1.DLHandle == h2.DLHandle && h1.DBHandle < h2.DBHandle);
182 }
183
184 inline bool operator == (const CSSM_DL_DB_HANDLE &h1, const CSSM_DL_DB_HANDLE &h2)
185 {
186 return h1.DLHandle == h2.DLHandle && h1.DBHandle == h2.DBHandle;
187 }
188
189 inline bool operator != (const CSSM_DL_DB_HANDLE &h1, const CSSM_DL_DB_HANDLE &h2)
190 {
191 return h1.DLHandle != h2.DLHandle || h1.DBHandle != h2.DBHandle;
192 }
193
194
195 class CssmDlDbList : public PodWrapper<CssmDlDbList, CSSM_DL_DB_LIST> {
196 public:
197 uint32 count() const { return NumHandles; }
198 uint32 &count() { return NumHandles; }
199 CssmDlDbHandle *handles() const { return CssmDlDbHandle::overlay(DLDBHandle); }
200 CssmDlDbHandle * &handles() { return CssmDlDbHandle::overlayVar(DLDBHandle); }
201
202 CssmDlDbHandle &operator [] (uint32 ix) const
203 { assert(ix < count()); return CssmDlDbHandle::overlay(DLDBHandle[ix]); }
204
205 void setDlDbList(uint32 n, CSSM_DL_DB_HANDLE *list)
206 { count() = n; handles() = CssmDlDbHandle::overlay(list); }
207 };
208
209
210 //
211 // CssmDLPolyData
212 //
213 class CssmDLPolyData
214 {
215 public:
216 CssmDLPolyData(const CSSM_DATA &data, CSSM_DB_ATTRIBUTE_FORMAT format)
217 : mData(CssmData::overlay(data)), mFormat(format) {}
218
219 // @@@ Don't use assert, but throw an exception.
220 // @@@ Do a size check on mData as well.
221
222 // @@@ This method is dangerous since the returned string is not guaranteed to be zero terminated.
223 operator const char *() const
224 {
225 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_STRING
226 || mFormat == CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE);
227 return reinterpret_cast<const char *>(mData.Data);
228 }
229 operator bool() const
230 {
231 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_UINT32 || mFormat == CSSM_DB_ATTRIBUTE_FORMAT_SINT32);
232 return *reinterpret_cast<uint32 *>(mData.Data);
233 }
234 operator uint32() const
235 {
236 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_UINT32);
237 return *reinterpret_cast<uint32 *>(mData.Data);
238 }
239 operator const uint32 *() const
240 {
241 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_MULTI_UINT32);
242 return reinterpret_cast<const uint32 *>(mData.Data);
243 }
244 operator sint32() const
245 {
246 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_SINT32);
247 return *reinterpret_cast<sint32 *>(mData.Data);
248 }
249 operator double() const
250 {
251 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_REAL);
252 return *reinterpret_cast<double *>(mData.Data);
253 }
254 operator CSSM_DATE () const;
255 operator Guid () const;
256 operator const CssmData &() const
257 {
258 return mData;
259 }
260
261 private:
262 const CssmData &mData;
263 CSSM_DB_ATTRIBUTE_FORMAT mFormat;
264 };
265
266
267 //
268 // CssmDbAttributeInfo pod wrapper for CSSM_DB_ATTRIBUTE_INFO
269 //
270 class CssmDbAttributeInfo : public PodWrapper<CssmDbAttributeInfo, CSSM_DB_ATTRIBUTE_INFO>
271 {
272 public:
273 CssmDbAttributeInfo(const CSSM_DB_ATTRIBUTE_INFO &attr)
274 { assignPod(attr); }
275
276 CssmDbAttributeInfo(const char *name,
277 CSSM_DB_ATTRIBUTE_FORMAT vFormat = CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX);
278 CssmDbAttributeInfo(const CSSM_OID &oid,
279 CSSM_DB_ATTRIBUTE_FORMAT vFormat = CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX);
280 CssmDbAttributeInfo(uint32 id,
281 CSSM_DB_ATTRIBUTE_FORMAT vFormat = CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX);
282
283 CSSM_DB_ATTRIBUTE_NAME_FORMAT nameFormat() const { return AttributeNameFormat; }
284 void nameFormat(CSSM_DB_ATTRIBUTE_NAME_FORMAT nameFormat) { AttributeNameFormat = nameFormat; }
285
286 CSSM_DB_ATTRIBUTE_FORMAT format() const { return AttributeFormat; }
287 void format(CSSM_DB_ATTRIBUTE_FORMAT format) { AttributeFormat = format; }
288
289 const char *stringName() const
290 {
291 assert(nameFormat() == CSSM_DB_ATTRIBUTE_NAME_AS_STRING);
292 return Label.AttributeName;
293 }
294 const CssmOid &oidName() const
295 {
296 assert(nameFormat() == CSSM_DB_ATTRIBUTE_NAME_AS_OID);
297 return CssmOid::overlay(Label.AttributeOID);
298 }
299 uint32 intName() const
300 {
301 assert(nameFormat() == CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER);
302 return Label.AttributeID;
303 }
304
305 operator const char *() const { return stringName(); }
306 operator const CssmOid &() const { return oidName(); }
307 operator uint32() const { return intName(); }
308
309 bool operator <(const CssmDbAttributeInfo& other) const;
310 bool operator ==(const CssmDbAttributeInfo& other) const;
311 bool operator !=(const CssmDbAttributeInfo& other) const
312 { return !(*this == other); }
313 };
314
315 //
316 // CssmDbRecordAttributeInfo pod wrapper for CSSM_DB_RECORD_ATTRIBUTE_INFO
317 //
318 class CssmDbRecordAttributeInfo : public PodWrapper<CssmDbRecordAttributeInfo, CSSM_DB_RECORD_ATTRIBUTE_INFO>
319 {
320 public:
321 CssmDbRecordAttributeInfo()
322 { DataRecordType = CSSM_DL_DB_RECORD_ANY; }
323
324 CssmDbRecordAttributeInfo(CSSM_DB_RECORDTYPE recordType, uint32 numberOfAttributes,
325 CSSM_DB_ATTRIBUTE_INFO_PTR attributeInfo)
326 {
327 DataRecordType = recordType;
328 NumberOfAttributes = numberOfAttributes;
329 AttributeInfo = attributeInfo;
330 }
331
332 CSSM_DB_RECORDTYPE recordType() const { return DataRecordType; }
333 void recordType(CSSM_DB_RECORDTYPE recordType) { DataRecordType = recordType; }
334
335 uint32 size() const { return NumberOfAttributes; }
336
337 // attribute access
338 CssmDbAttributeInfo *&attributes()
339 { return CssmDbAttributeInfo::overlayVar(AttributeInfo); }
340 CssmDbAttributeInfo *attributes() const
341 { return CssmDbAttributeInfo::overlay(AttributeInfo); }
342 CssmDbAttributeInfo &at(uint32 ix) const
343 { assert(ix < size()); return attributes()[ix]; }
344
345 CssmDbAttributeInfo &operator [] (uint32 ix) const { return at(ix); }
346 };
347
348 //
349 // CssmAutoDbRecordAttributeInfo pod wrapper for CSSM_DB_RECORD_ATTRIBUTE_INFO
350 //
351 class CssmAutoDbRecordAttributeInfo: public CssmDbRecordAttributeInfo, public ArrayBuilder<CssmDbAttributeInfo>
352 {
353 public:
354 CssmAutoDbRecordAttributeInfo(uint32 capacity = 0, Allocator &allocator = Allocator::standard()) :
355 CssmDbRecordAttributeInfo(),
356 ArrayBuilder<CssmDbAttributeInfo>(CssmDbAttributeInfo::overlayVar(AttributeInfo),
357 NumberOfAttributes, capacity, allocator) {}
358 };
359
360
361 //
362 // CssmDbAttributeData pod wrapper for CSSM_DB_ATTRIBUTE_DATA
363 //
364 class CssmDbAttributeData : public PodWrapper<CssmDbAttributeData, CSSM_DB_ATTRIBUTE_DATA>
365 {
366 public:
367 CssmDbAttributeData() { NumberOfValues = 0; Value = NULL; }
368 CssmDbAttributeData(const CSSM_DB_ATTRIBUTE_DATA &attr)
369 { assignPod(attr); }
370 CssmDbAttributeData(const CSSM_DB_ATTRIBUTE_INFO &info)
371 { Info = info; NumberOfValues = 0; Value = NULL; }
372
373 CssmDbAttributeInfo &info() { return CssmDbAttributeInfo::overlay(Info); }
374 const CssmDbAttributeInfo &info() const { return CssmDbAttributeInfo::overlay(Info); }
375 void info (const CSSM_DB_ATTRIBUTE_INFO &inInfo) { Info = inInfo; }
376
377 CSSM_DB_ATTRIBUTE_FORMAT format() const { return info().format(); }
378 void format(CSSM_DB_ATTRIBUTE_FORMAT f) { info().format(f); }
379
380 uint32 size() const { return NumberOfValues; }
381 CssmData *&values() { return CssmData::overlayVar(Value); }
382 CssmData *values() const { return CssmData::overlay(Value); }
383
384 CssmData &at(unsigned int ix) const
385 {
386 if (ix >= size()) CssmError::throwMe(CSSMERR_DL_MISSING_VALUE);
387 return values()[ix];
388 }
389
390 CssmData &operator [] (unsigned int ix) const { return at(ix); }
391
392 template <class T>
393 T at(unsigned int ix) const { return CssmDLPolyData(Value[ix], format()); }
394
395 // this is intentionally unspecified since it could lead to bugs; the
396 // data is not guaranteed to be NULL-terminated
397 // operator const char *() const;
398
399 operator string() const;
400 operator const Guid &() const;
401 operator bool() const;
402 operator uint32() const;
403 operator const uint32 *() const;
404 operator sint32() const;
405 operator double() const;
406 operator const CssmData &() const;
407
408 // set values without allocation (caller owns the data contents)
409 void set(CssmData &data) { set(1, &data); }
410 void set(uint32 count, CssmData *datas) { NumberOfValues = count; Value = datas; }
411
412 // Set the value of this Attr (assuming it was not set before).
413 void set(const CSSM_DB_ATTRIBUTE_INFO &inInfo, const CssmPolyData &inValue,
414 Allocator &inAllocator);
415
416 // copy (just) the return-value part from another AttributeData to this one
417 void copyValues(const CssmDbAttributeData &source, Allocator &alloc);
418
419 // Set the value of this Attr (which must be unset so far)
420 void set(const CSSM_DB_ATTRIBUTE_DATA &source, Allocator &alloc)
421 {
422 info(source.Info);
423 copyValues(source, alloc);
424 }
425
426 // Add a value to this attribute.
427 void add(const CssmPolyData &inValue, Allocator &inAllocator);
428
429 void add(const char *value, Allocator &alloc)
430 { format(CSSM_DB_ATTRIBUTE_FORMAT_STRING); add(CssmPolyData(value), alloc); }
431
432 void add(const std::string &value, Allocator &alloc)
433 { format(CSSM_DB_ATTRIBUTE_FORMAT_STRING); add(CssmPolyData(value), alloc); }
434
435 void add(uint32 value, Allocator &alloc)
436 { format(CSSM_DB_ATTRIBUTE_FORMAT_UINT32); add(CssmPolyData(value), alloc); }
437
438 void add(sint32 value, Allocator &alloc)
439 { format(CSSM_DB_ATTRIBUTE_FORMAT_SINT32); add(CssmPolyData(value), alloc); }
440
441 void add(const CssmData &value, Allocator &alloc)
442 { format(CSSM_DB_ATTRIBUTE_FORMAT_BLOB); add(CssmPolyData(value), alloc); }
443
444 void add(const CssmDbAttributeData &src, Allocator &inAllocator);
445
446 // delete specific values if they are present in this attribute data
447 bool deleteValue(const CssmData &src, Allocator &inAllocator);
448 void deleteValues(const CssmDbAttributeData &src, Allocator &inAllocator);
449
450 void deleteValues(Allocator &inAllocator);
451
452 bool operator <(const CssmDbAttributeData& other) const;
453 };
454
455
456 //
457 // CssmDbRecordAttributeData pod wrapper for CSSM_DB_RECORD_ATTRIBUTE_DATA
458 //
459 class CssmDbRecordAttributeData : public PodWrapper<CssmDbRecordAttributeData, CSSM_DB_RECORD_ATTRIBUTE_DATA>
460 {
461 public:
462 CssmDbRecordAttributeData()
463 { clearPod(); DataRecordType = CSSM_DL_DB_RECORD_ANY; }
464
465 CSSM_DB_RECORDTYPE recordType() const { return DataRecordType; }
466 void recordType(CSSM_DB_RECORDTYPE recordType) { DataRecordType = recordType; }
467
468 uint32 semanticInformation() const { return SemanticInformation; }
469 void semanticInformation(uint32 semanticInformation) { SemanticInformation = semanticInformation; }
470
471 uint32 size() const { return NumberOfAttributes; }
472 CssmDbAttributeData *&attributes()
473 { return CssmDbAttributeData::overlayVar(AttributeData); }
474 CssmDbAttributeData *attributes() const
475 { return CssmDbAttributeData::overlay(AttributeData); }
476
477 // Attributes by position
478 CssmDbAttributeData &at(unsigned int ix) const
479 { assert(ix < size()); return attributes()[ix]; }
480
481 CssmDbAttributeData &operator [] (unsigned int ix) const { return at(ix); }
482
483 void deleteValues(Allocator &allocator)
484 { for (uint32 ix = 0; ix < size(); ++ix) at(ix).deleteValues(allocator); }
485
486 CssmDbAttributeData *find(const CSSM_DB_ATTRIBUTE_INFO &inInfo);
487
488 bool operator <(const CssmDbRecordAttributeData& other) const;
489 };
490
491
492 //
493 // CssmAutoDbRecordAttributeData
494 //
495 class CssmAutoDbRecordAttributeData : public CssmDbRecordAttributeData, public ArrayBuilder<CssmDbAttributeData>
496 {
497 public:
498 CssmAutoDbRecordAttributeData(uint32 capacity = 0,
499 Allocator &valueAllocator = Allocator::standard(),
500 Allocator &dataAllocator = Allocator::standard()) :
501 CssmDbRecordAttributeData(),
502 ArrayBuilder<CssmDbAttributeData>(CssmDbAttributeData::overlayVar(AttributeData),
503 NumberOfAttributes, capacity, dataAllocator),
504 mValueAllocator(valueAllocator) {}
505 ~CssmAutoDbRecordAttributeData();
506
507 void clear();
508 void deleteValues() { CssmDbRecordAttributeData::deleteValues(mValueAllocator); }
509 void invalidate();
510
511 CssmDbAttributeData &add() { return ArrayBuilder<CssmDbAttributeData>::add(); } // XXX using doesn't work here.
512 CssmDbAttributeData &add(const CSSM_DB_ATTRIBUTE_INFO &info);
513 CssmDbAttributeData &add(const CSSM_DB_ATTRIBUTE_INFO &info, const CssmPolyData &value);
514
515 // So clients can pass this as the allocator argument to add()
516 operator Allocator &() const { return mValueAllocator; }
517 private:
518 Allocator &mValueAllocator;
519
520 CssmDbAttributeData* findAttribute (const CSSM_DB_ATTRIBUTE_INFO &info);
521 CssmDbAttributeData& getAttributeReference (const CSSM_DB_ATTRIBUTE_INFO &info);
522 };
523
524
525 //
526 // CssmSelectionPredicate a PodWrapper for CSSM_SELECTION_PREDICATE
527 //
528 class CssmSelectionPredicate : public PodWrapper<CssmSelectionPredicate, CSSM_SELECTION_PREDICATE> {
529 public:
530 CssmSelectionPredicate() { clearPod(); }
531
532 CSSM_DB_OPERATOR dbOperator() const { return DbOperator; }
533 void dbOperator(CSSM_DB_OPERATOR dbOperator) { DbOperator = dbOperator; }
534
535 CssmSelectionPredicate(CSSM_DB_OPERATOR inDbOperator)
536 { dbOperator(inDbOperator); Attribute.NumberOfValues = 0; Attribute.Value = NULL; }
537
538 CssmDbAttributeData &attribute() { return CssmDbAttributeData::overlay(Attribute); }
539 const CssmDbAttributeData &attribute() const { return CssmDbAttributeData::overlay(Attribute); }
540
541 // Set the value of this CssmSelectionPredicate (assuming it was not set before).
542 void set(const CSSM_DB_ATTRIBUTE_INFO &inInfo,
543 const CssmPolyData &inValue, Allocator &inAllocator)
544 { attribute().set(inInfo, inValue, inAllocator); }
545
546 // Set the value of this CssmSelectionPredicate using another CssmSelectionPredicate's value.
547 void set(const CSSM_SELECTION_PREDICATE &other, Allocator &inAllocator)
548 { DbOperator = other.DbOperator; attribute().set(other.Attribute, inAllocator); }
549
550 // Add a value to the list of values for this CssmSelectionPredicate.
551 void add(const CssmPolyData &inValue, Allocator &inAllocator)
552 { attribute().add(inValue, inAllocator); }
553
554 void deleteValues(Allocator &inAllocator) { attribute().deleteValues(inAllocator); }
555 };
556
557 class CssmQuery : public PodWrapper<CssmQuery, CSSM_QUERY> {
558 public:
559 CssmQuery(CSSM_DB_RECORDTYPE type = CSSM_DL_DB_RECORD_ANY)
560 { clearPod(); RecordType = type; }
561
562 // copy or assign flat from CSSM_QUERY
563 CssmQuery(const CSSM_QUERY &q) { assignPod(q); }
564 CssmQuery &operator = (const CSSM_QUERY &q) { assignPod(q); return *this; }
565
566 // flat copy and change record type
567 CssmQuery(const CssmQuery &q, CSSM_DB_RECORDTYPE type)
568 { *this = q; RecordType = type; }
569
570 CSSM_DB_RECORDTYPE recordType() const { return RecordType; }
571 void recordType(CSSM_DB_RECORDTYPE recordType) { RecordType = recordType; }
572
573 CSSM_DB_CONJUNCTIVE conjunctive() const { return Conjunctive; }
574 void conjunctive(CSSM_DB_CONJUNCTIVE conjunctive) { Conjunctive = conjunctive; }
575
576 CSSM_QUERY_LIMITS queryLimits() const { return QueryLimits; }
577 void queryLimits(CSSM_QUERY_LIMITS queryLimits) { QueryLimits = queryLimits; }
578
579 CSSM_QUERY_FLAGS queryFlags() const { return QueryFlags; }
580 void queryFlags(CSSM_QUERY_FLAGS queryFlags) { QueryFlags = queryFlags; }
581
582 uint32 size() const { return NumSelectionPredicates; }
583
584 CssmSelectionPredicate *&predicates()
585 { return CssmSelectionPredicate::overlayVar(SelectionPredicate); }
586 CssmSelectionPredicate *predicates() const
587 { return CssmSelectionPredicate::overlay(SelectionPredicate); }
588
589 CssmSelectionPredicate &at(uint32 ix) const
590 { assert(ix < size()); return predicates()[ix]; }
591
592 CssmSelectionPredicate &operator[] (uint32 ix) const { return at(ix); }
593
594 void set(uint32 count, CSSM_SELECTION_PREDICATE *preds)
595 { NumSelectionPredicates = count; SelectionPredicate = preds; }
596
597 void deleteValues(Allocator &allocator)
598 { for (uint32 ix = 0; ix < size(); ++ix) at(ix).deleteValues(allocator); }
599 };
600
601
602 class CssmAutoQuery : public CssmQuery, public ArrayBuilder<CssmSelectionPredicate> {
603 public:
604 CssmAutoQuery(const CSSM_QUERY &query, Allocator &allocator = Allocator::standard());
605 CssmAutoQuery(uint32 capacity = 0, Allocator &allocator = Allocator::standard()) :
606 ArrayBuilder<CssmSelectionPredicate>(CssmSelectionPredicate::overlayVar(SelectionPredicate),
607 NumSelectionPredicates,
608 capacity, allocator) {}
609 ~CssmAutoQuery();
610 void clear();
611 void deleteValues() { CssmQuery::deleteValues(allocator()); }
612
613 CssmSelectionPredicate &add() { return ArrayBuilder<CssmSelectionPredicate>::add(); }
614 CssmSelectionPredicate &add(CSSM_DB_OPERATOR dbOperator, const CSSM_DB_ATTRIBUTE_INFO &info, const CssmPolyData &value);
615
616 // So clients can pass this as the allocator argument to add()
617 operator Allocator &() const { return allocator(); }
618 };
619
620
621 //
622 // DLDbIdentifier
623 //
624 class DLDbIdentifier
625 {
626 protected:
627 class Impl : public RefCount
628 {
629 NOCOPY(Impl)
630 public:
631 Impl(const CSSM_SUBSERVICE_UID &ssuid,const char *DbName,const CSSM_NET_ADDRESS *DbLocation) :
632 mCssmSubserviceUid(ssuid),mDbName(DbName,DbLocation) {}
633
634 ~Impl() {} // Must be public since RefPointer uses it.
635
636 // Accessors
637 const CssmSubserviceUid &ssuid() const { return mCssmSubserviceUid; }
638 const char *dbName() const { return mDbName.dbName(); }
639 const CssmNetAddress *dbLocation() const { return mDbName.dbLocation(); }
640
641 // comparison (simple lexicographic)
642 bool operator < (const Impl &other) const;
643 bool operator == (const Impl &other) const;
644 private:
645 // Private member variables
646 CssmSubserviceUid mCssmSubserviceUid;
647 DbName mDbName;
648 };
649
650 public:
651 // Constructors
652 DLDbIdentifier() {}
653 DLDbIdentifier(const CSSM_SUBSERVICE_UID &ssuid, const char *DbName, const CSSM_NET_ADDRESS *DbLocation)
654 : mImpl(new Impl(ssuid, DbName, DbLocation)) {}
655 DLDbIdentifier(const char *name, const Guid &guid, uint32 ssid, uint32 sstype,
656 const CSSM_NET_ADDRESS *location = NULL)
657 : mImpl(new Impl(CssmSubserviceUid(guid, NULL, ssid, sstype), name, location)) { }
658
659 // Conversion Operators
660 bool operator !() const { return !mImpl; }
661 operator bool() const { return mImpl; }
662
663 // Operators
664 bool operator <(const DLDbIdentifier &other) const
665 { return mImpl && other.mImpl ? *mImpl < *other.mImpl : mImpl.get() < other.mImpl.get(); }
666 bool operator ==(const DLDbIdentifier &other) const
667 { return mImpl && other.mImpl ? *mImpl == *other.mImpl : mImpl.get() == other.mImpl.get(); }
668 DLDbIdentifier &operator =(const DLDbIdentifier &other)
669 { mImpl = other.mImpl; return *this; }
670
671 // Accessors
672 const CssmSubserviceUid &ssuid() const { return mImpl->ssuid(); }
673 const char *dbName() const { return mImpl->dbName(); }
674 const CssmNetAddress *dbLocation() const { return mImpl->dbLocation(); }
675
676 RefPointer<Impl> mImpl;
677 };
678
679 // Wrappers for index-related CSSM objects.
680
681 class CssmDbIndexInfo : public PodWrapper<CssmDbIndexInfo, CSSM_DB_INDEX_INFO>
682 {
683 public:
684 CssmDbIndexInfo(const CSSM_DB_INDEX_INFO &attr)
685 { (CSSM_DB_INDEX_INFO &)*this = attr; }
686
687 CSSM_DB_INDEX_TYPE indexType() const { return IndexType; }
688 void indexType(CSSM_DB_INDEX_TYPE indexType) { IndexType = indexType; }
689
690 CSSM_DB_INDEXED_DATA_LOCATION dataLocation() const { return IndexedDataLocation; }
691 void dataLocation(CSSM_DB_INDEXED_DATA_LOCATION dataLocation)
692 {
693 IndexedDataLocation = dataLocation;
694 }
695
696 const CssmDbAttributeInfo &attributeInfo() const
697 {
698 return CssmDbAttributeInfo::overlay(Info);
699 }
700 };
701
702
703 namespace DataWalkers {
704
705
706 //
707 // DLDbIdentifiers don't walk directly because they have Impl structure and use strings.
708 // Happily, they are easily transcribed into a walkable form.
709 //
710 struct DLDbFlatIdentifier {
711 CssmSubserviceUid *uid; // module reference
712 char *name; // string name
713 CssmNetAddress *address; // optional network address
714
715 DLDbFlatIdentifier(const DLDbIdentifier &ident) :
716 uid(const_cast<CssmSubserviceUid *>(&ident.ssuid())),
717 name(const_cast<char *>(ident.dbName())),
718 address(const_cast<CssmNetAddress *>(ident.dbLocation()))
719 { }
720
721 operator DLDbIdentifier () { return DLDbIdentifier(*uid, name, address); }
722 };
723
724 template<class Action>
725 DLDbFlatIdentifier *walk(Action &operate, DLDbFlatIdentifier * &ident)
726 {
727 operate(ident);
728 if (ident->uid)
729 walk(operate, ident->uid);
730 walk(operate, ident->name);
731 if (ident->address)
732 walk(operate, ident->address);
733 return ident;
734 }
735
736
737 //
738 // Walkers for the byzantine data structures of the DL universe.
739 // Geez, what WERE they smoking when they invented this?
740 //
741
742 // DbAttributeInfos
743 template<class Action>
744 void enumerate(Action &operate, CssmDbAttributeInfo &info)
745 {
746 switch (info.nameFormat()) {
747 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING:
748 walk(operate, info.Label.AttributeName);
749 break;
750 case CSSM_DB_ATTRIBUTE_NAME_AS_OID:
751 walk(operate, info.Label.AttributeOID);
752 break;
753 default:
754 break;
755 }
756 }
757
758 template <class Action>
759 void walk(Action &operate, CssmDbAttributeInfo &info)
760 {
761 operate(info);
762 enumerate(operate, info);
763 }
764
765 template <class Action>
766 CssmDbAttributeInfo *walk(Action &operate, CssmDbAttributeInfo * &info)
767 {
768 operate(info);
769 enumerate(operate, *info);
770 return info;
771 }
772
773 // DbRecordAttributeInfo
774 template <class Action>
775 void walk(Action &operate, CssmDbRecordAttributeInfo &info)
776 {
777 operate(info);
778 enumerateArray(operate, info, &CssmDbRecordAttributeInfo::attributes);
779 }
780
781 template <class Action>
782 CssmDbRecordAttributeInfo *walk(Action &operate, CssmDbRecordAttributeInfo * &info)
783 {
784 operate(info);
785 enumerateArray(operate, *info, &CssmDbRecordAttributeInfo::attributes);
786 return info;
787 }
788
789 // DbAttributeData (Info + value vector)
790 template <class Action>
791 void walk(Action &operate, CssmDbAttributeData &data)
792 {
793 operate(data);
794 walk(operate, data.info());
795 enumerateArray(operate, data, &CssmDbAttributeData::values);
796 }
797
798 template <class Action>
799 CssmDbAttributeData *walk(Action &operate, CssmDbAttributeData * &data)
800 {
801 operate(data);
802 walk(operate, data->info());
803 enumerateArray(operate, *data, &CssmDbAttributeData::values);
804 return data;
805 }
806
807 // DbRecordAttributeData (array of ...datas)
808 template <class Action>
809 void walk(Action &operate, CssmDbRecordAttributeData &data)
810 {
811 operate(data);
812 enumerateArray(operate, data, &CssmDbRecordAttributeData::attributes);
813 }
814
815 template <class Action>
816 CssmDbRecordAttributeData *walk(Action &operate, CssmDbRecordAttributeData * &data)
817 {
818 operate(data);
819 enumerateArray(operate, *data, &CssmDbRecordAttributeData::attributes);
820 return data;
821 }
822
823 // SelectionPredicates
824 template <class Action>
825 CssmSelectionPredicate *walk(Action &operate, CssmSelectionPredicate * &predicate)
826 {
827 operate(predicate);
828 walk(operate, predicate->attribute());
829 return predicate;
830 }
831
832 template<class Action>
833 void walk(Action &operate, CssmSelectionPredicate &predicate)
834 {
835 operate(predicate);
836 walk(operate, predicate.attribute());
837 }
838
839 // Queries
840 template <class Action>
841 void walk(Action &operate, CssmQuery &query)
842 {
843 operate(query);
844 enumerateArray(operate, query, &CssmQuery::predicates);
845 }
846
847 template <class Action>
848 CssmQuery *walk(Action &operate, CssmQuery * &query)
849 {
850 operate(query);
851 enumerateArray(operate, *query, &CssmQuery::predicates);
852 return query;
853 }
854
855 template <class Action>
856 CSSM_QUERY *walk(Action &operate, CSSM_QUERY * &query)
857 {
858 return walk(operate, CssmQuery::overlayVar(query));
859 }
860
861
862 } // end namespace DataWalkers
863 } // end namespace Security
864
865
866 #endif // _H_CDSA_UTILITIES_CSSMDB