]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cdsa_utilities/lib/cssmdb.h
Security-59306.140.5.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utilities / lib / cssmdb.h
1 /*
2 * Copyright (c) 2000-2006,2011-2012,2014 Apple 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/cssmwalkers.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 if (ix >= count()) {
204 secemergency("CssmDlDbList: attempt to index beyond bounds");
205 abort();
206 }
207 return CssmDlDbHandle::overlay(DLDBHandle[ix]);
208 }
209
210 void setDlDbList(uint32 n, CSSM_DL_DB_HANDLE *list)
211 { count() = n; handles() = CssmDlDbHandle::overlay(list); }
212 };
213
214
215 //
216 // CssmDLPolyData
217 //
218 class CssmDLPolyData
219 {
220 public:
221 CssmDLPolyData(const CSSM_DATA &data, CSSM_DB_ATTRIBUTE_FORMAT format)
222 : mData(CssmData::overlay(data))
223 #ifndef NDEBUG
224 , mFormat(format)
225 #endif
226 {}
227
228 // @@@ Don't use assert, but throw an exception.
229 // @@@ Do a size check on mData as well.
230
231 // @@@ This method is dangerous since the returned string is not guaranteed to be zero terminated.
232 operator const char *() const
233 {
234 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_STRING
235 || mFormat == CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE);
236 return reinterpret_cast<const char *>(mData.Data);
237 }
238 operator bool() const
239 {
240 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_UINT32 || mFormat == CSSM_DB_ATTRIBUTE_FORMAT_SINT32);
241 return *reinterpret_cast<uint32 *>(mData.Data);
242 }
243 operator uint32() const
244 {
245 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_UINT32);
246 return *reinterpret_cast<uint32 *>(mData.Data);
247 }
248 operator const uint32 *() const
249 {
250 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_MULTI_UINT32);
251 return reinterpret_cast<const uint32 *>(mData.Data);
252 }
253 operator sint32() const
254 {
255 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_SINT32);
256 return *reinterpret_cast<sint32 *>(mData.Data);
257 }
258 operator double() const
259 {
260 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_REAL);
261 return *reinterpret_cast<double *>(mData.Data);
262 }
263 operator CSSM_DATE () const;
264 operator Guid () const;
265 operator const CssmData &() const
266 {
267 return mData;
268 }
269
270 private:
271 const CssmData &mData;
272 #ifndef NDEBUG
273 CSSM_DB_ATTRIBUTE_FORMAT mFormat;
274 #endif
275 };
276
277
278 //
279 // CssmDbAttributeInfo pod wrapper for CSSM_DB_ATTRIBUTE_INFO
280 //
281 class CssmDbAttributeInfo : public PodWrapper<CssmDbAttributeInfo, CSSM_DB_ATTRIBUTE_INFO>
282 {
283 public:
284 CssmDbAttributeInfo(const CSSM_DB_ATTRIBUTE_INFO &attr)
285 { assignPod(attr); }
286
287 CssmDbAttributeInfo(const char *name,
288 CSSM_DB_ATTRIBUTE_FORMAT vFormat = CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX);
289 CssmDbAttributeInfo(const CSSM_OID &oid,
290 CSSM_DB_ATTRIBUTE_FORMAT vFormat = CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX);
291 CssmDbAttributeInfo(uint32 id,
292 CSSM_DB_ATTRIBUTE_FORMAT vFormat = CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX);
293
294 CSSM_DB_ATTRIBUTE_NAME_FORMAT nameFormat() const { return AttributeNameFormat; }
295 void nameFormat(CSSM_DB_ATTRIBUTE_NAME_FORMAT nameFormat) { AttributeNameFormat = nameFormat; }
296
297 CSSM_DB_ATTRIBUTE_FORMAT format() const { return AttributeFormat; }
298 void format(CSSM_DB_ATTRIBUTE_FORMAT format) { AttributeFormat = format; }
299
300 const char *stringName() const
301 {
302 assert(nameFormat() == CSSM_DB_ATTRIBUTE_NAME_AS_STRING);
303 return Label.AttributeName;
304 }
305 const CssmOid &oidName() const
306 {
307 assert(nameFormat() == CSSM_DB_ATTRIBUTE_NAME_AS_OID);
308 return CssmOid::overlay(Label.AttributeOID);
309 }
310 uint32 intName() const
311 {
312 assert(nameFormat() == CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER);
313 return Label.AttributeID;
314 }
315
316 operator const char *() const { return stringName(); }
317 operator const CssmOid &() const { return oidName(); }
318 operator uint32() const { return intName(); }
319
320 bool operator <(const CssmDbAttributeInfo& other) const;
321 bool operator ==(const CssmDbAttributeInfo& other) const;
322 bool operator !=(const CssmDbAttributeInfo& other) const
323 { return !(*this == other); }
324 };
325
326 //
327 // CssmDbRecordAttributeInfo pod wrapper for CSSM_DB_RECORD_ATTRIBUTE_INFO
328 //
329 class CssmDbRecordAttributeInfo : public PodWrapper<CssmDbRecordAttributeInfo, CSSM_DB_RECORD_ATTRIBUTE_INFO>
330 {
331 public:
332 CssmDbRecordAttributeInfo()
333 { DataRecordType = CSSM_DL_DB_RECORD_ANY; }
334
335 CssmDbRecordAttributeInfo(CSSM_DB_RECORDTYPE recordType, uint32 numberOfAttributes,
336 CSSM_DB_ATTRIBUTE_INFO_PTR attributeInfo)
337 {
338 DataRecordType = recordType;
339 NumberOfAttributes = numberOfAttributes;
340 AttributeInfo = attributeInfo;
341 }
342
343 CSSM_DB_RECORDTYPE recordType() const { return DataRecordType; }
344 void recordType(CSSM_DB_RECORDTYPE recordType) { DataRecordType = recordType; }
345
346 uint32 size() const { return NumberOfAttributes; }
347
348 // attribute access
349 CssmDbAttributeInfo *&attributes()
350 { return CssmDbAttributeInfo::overlayVar(AttributeInfo); }
351 CssmDbAttributeInfo *attributes() const
352 { return CssmDbAttributeInfo::overlay(AttributeInfo); }
353 CssmDbAttributeInfo &at(uint32 ix) const {
354 if (ix >= size()) {
355 secemergency("CssmDbRecordAttributeInfo: attempt to index beyond bounds");
356 abort();
357 }
358 return attributes()[ix];
359 }
360
361 CssmDbAttributeInfo &operator [] (uint32 ix) const { return at(ix); }
362 };
363
364 //
365 // CssmAutoDbRecordAttributeInfo pod wrapper for CSSM_DB_RECORD_ATTRIBUTE_INFO
366 //
367 class CssmAutoDbRecordAttributeInfo: public CssmDbRecordAttributeInfo, public ArrayBuilder<CssmDbAttributeInfo>
368 {
369 public:
370 CssmAutoDbRecordAttributeInfo(uint32 capacity = 0, Allocator &allocator = Allocator::standard()) :
371 CssmDbRecordAttributeInfo(),
372 ArrayBuilder<CssmDbAttributeInfo>(CssmDbAttributeInfo::overlayVar(AttributeInfo),
373 NumberOfAttributes, capacity, allocator) {}
374 };
375
376
377 //
378 // CssmDbAttributeData pod wrapper for CSSM_DB_ATTRIBUTE_DATA
379 //
380 class CssmDbAttributeData : public PodWrapper<CssmDbAttributeData, CSSM_DB_ATTRIBUTE_DATA>
381 {
382 public:
383 CssmDbAttributeData() { NumberOfValues = 0; Value = NULL; }
384 CssmDbAttributeData(const CSSM_DB_ATTRIBUTE_DATA &attr)
385 { assignPod(attr); }
386 CssmDbAttributeData(const CSSM_DB_ATTRIBUTE_INFO &info)
387 { Info = info; NumberOfValues = 0; Value = NULL; }
388
389 CssmDbAttributeInfo &info() { return CssmDbAttributeInfo::overlay(Info); }
390 const CssmDbAttributeInfo &info() const { return CssmDbAttributeInfo::overlay(Info); }
391 void info (const CSSM_DB_ATTRIBUTE_INFO &inInfo) { Info = inInfo; }
392
393 CSSM_DB_ATTRIBUTE_FORMAT format() const { return info().format(); }
394 void format(CSSM_DB_ATTRIBUTE_FORMAT f) { info().format(f); }
395
396 uint32 size() const { return NumberOfValues; }
397 CssmData *&values() { return CssmData::overlayVar(Value); }
398 CssmData *values() const { return CssmData::overlay(Value); }
399
400 CssmData &at(unsigned int ix) const
401 {
402 if (ix >= size()) CssmError::throwMe(CSSMERR_DL_MISSING_VALUE);
403 return values()[ix];
404 }
405
406 CssmData &operator [] (unsigned int ix) const { return at(ix); }
407
408 template <class T>
409 T at(unsigned int ix) const { return CssmDLPolyData(Value[ix], format()); }
410
411 // this is intentionally unspecified since it could lead to bugs; the
412 // data is not guaranteed to be NULL-terminated
413 // operator const char *() const;
414
415 operator string() const;
416 operator const Guid &() const;
417 operator bool() const;
418 operator uint32() const;
419 operator const uint32 *() const;
420 operator sint32() const;
421 operator double() const;
422 operator const CssmData &() const;
423
424 // set values without allocation (caller owns the data contents)
425 void set(CssmData &data) { set(1, &data); }
426 void set(uint32 count, CssmData *datas) { NumberOfValues = count; Value = datas; }
427
428 // Set the value of this Attr (assuming it was not set before).
429 void set(const CSSM_DB_ATTRIBUTE_INFO &inInfo, const CssmPolyData &inValue,
430 Allocator &inAllocator);
431
432 // copy (just) the return-value part from another AttributeData to this one
433 void copyValues(const CssmDbAttributeData &source, Allocator &alloc);
434
435 // Set the value of this Attr (which must be unset so far)
436 void set(const CSSM_DB_ATTRIBUTE_DATA &source, Allocator &alloc)
437 {
438 info(source.Info);
439 copyValues(source, alloc);
440 }
441
442 // Add a value to this attribute.
443 void add(const CssmPolyData &inValue, Allocator &inAllocator);
444
445 void add(const char *value, Allocator &alloc)
446 { format(CSSM_DB_ATTRIBUTE_FORMAT_STRING); add(CssmPolyData(value), alloc); }
447
448 void add(const std::string &value, Allocator &alloc)
449 { format(CSSM_DB_ATTRIBUTE_FORMAT_STRING); add(CssmPolyData(value), alloc); }
450
451 void add(uint32 value, Allocator &alloc)
452 { format(CSSM_DB_ATTRIBUTE_FORMAT_UINT32); add(CssmPolyData(value), alloc); }
453
454 void add(sint32 value, Allocator &alloc)
455 { format(CSSM_DB_ATTRIBUTE_FORMAT_SINT32); add(CssmPolyData(value), alloc); }
456
457 void add(const CssmData &value, Allocator &alloc)
458 { format(CSSM_DB_ATTRIBUTE_FORMAT_BLOB); add(CssmPolyData(value), alloc); }
459
460 void add(const CssmDbAttributeData &src, Allocator &inAllocator);
461
462 // delete specific values if they are present in this attribute data
463 bool deleteValue(const CssmData &src, Allocator &inAllocator);
464 void deleteValues(const CssmDbAttributeData &src, Allocator &inAllocator);
465
466 void deleteValues(Allocator &inAllocator);
467
468 bool operator <(const CssmDbAttributeData& other) const;
469 };
470
471
472 //
473 // CssmDbRecordAttributeData pod wrapper for CSSM_DB_RECORD_ATTRIBUTE_DATA
474 //
475 class CssmDbRecordAttributeData : public PodWrapper<CssmDbRecordAttributeData, CSSM_DB_RECORD_ATTRIBUTE_DATA>
476 {
477 public:
478 CssmDbRecordAttributeData()
479 { clearPod(); DataRecordType = CSSM_DL_DB_RECORD_ANY; }
480
481 CSSM_DB_RECORDTYPE recordType() const { return DataRecordType; }
482 void recordType(CSSM_DB_RECORDTYPE recordType) { DataRecordType = recordType; }
483
484 uint32 semanticInformation() const { return SemanticInformation; }
485 void semanticInformation(uint32 semanticInformation) { SemanticInformation = semanticInformation; }
486
487 uint32 size() const { return NumberOfAttributes; }
488 CssmDbAttributeData *&attributes()
489 { return CssmDbAttributeData::overlayVar(AttributeData); }
490 CssmDbAttributeData *attributes() const
491 { return CssmDbAttributeData::overlay(AttributeData); }
492
493 // Attributes by position
494 CssmDbAttributeData &at(unsigned int ix) const {
495 if (ix >= size()) {
496 secemergency("CssmDbRecordAttributeData: attempt to index beyond bounds");
497 abort();
498 }
499 return attributes()[ix];
500 }
501
502 CssmDbAttributeData &operator [] (unsigned int ix) const { return at(ix); }
503
504 void deleteValues(Allocator &allocator)
505 { for (uint32 ix = 0; ix < size(); ++ix) at(ix).deleteValues(allocator); }
506
507 CssmDbAttributeData *find(const CSSM_DB_ATTRIBUTE_INFO &inInfo);
508
509 bool operator <(const CssmDbRecordAttributeData& other) const;
510 };
511
512
513 //
514 // CssmAutoDbRecordAttributeData
515 //
516 class CssmAutoDbRecordAttributeData : public CssmDbRecordAttributeData, public ArrayBuilder<CssmDbAttributeData>
517 {
518 public:
519 CssmAutoDbRecordAttributeData(uint32 capacity = 0,
520 Allocator &valueAllocator = Allocator::standard(),
521 Allocator &dataAllocator = Allocator::standard()) :
522 CssmDbRecordAttributeData(),
523 ArrayBuilder<CssmDbAttributeData>(CssmDbAttributeData::overlayVar(AttributeData),
524 NumberOfAttributes, capacity, dataAllocator),
525 mValueAllocator(valueAllocator) {}
526 ~CssmAutoDbRecordAttributeData();
527
528 void clear();
529 void deleteValues() { CssmDbRecordAttributeData::deleteValues(mValueAllocator); }
530 void invalidate();
531
532 CssmDbAttributeData &add() { return ArrayBuilder<CssmDbAttributeData>::add(); } // XXX using doesn't work here.
533 CssmDbAttributeData &add(const CSSM_DB_ATTRIBUTE_INFO &info);
534 CssmDbAttributeData &add(const CSSM_DB_ATTRIBUTE_INFO &info, const CssmPolyData &value);
535
536 // Take the attributes from the object, and overlay them onto this one
537 void updateWith(const CssmAutoDbRecordAttributeData* newValues);
538
539 // So clients can pass this as the allocator argument to add()
540 operator Allocator &() const { return mValueAllocator; }
541
542 CssmDbAttributeData* findAttribute (const CSSM_DB_ATTRIBUTE_INFO &info);
543 private:
544 Allocator &mValueAllocator;
545
546 CssmDbAttributeData& getAttributeReference (const CSSM_DB_ATTRIBUTE_INFO &info);
547 };
548
549
550 //
551 // CssmSelectionPredicate a PodWrapper for CSSM_SELECTION_PREDICATE
552 //
553 class CssmSelectionPredicate : public PodWrapper<CssmSelectionPredicate, CSSM_SELECTION_PREDICATE> {
554 public:
555 CssmSelectionPredicate() { clearPod(); }
556
557 CSSM_DB_OPERATOR dbOperator() const { return DbOperator; }
558 void dbOperator(CSSM_DB_OPERATOR dbOperator) { DbOperator = dbOperator; }
559
560 CssmSelectionPredicate(CSSM_DB_OPERATOR inDbOperator)
561 { dbOperator(inDbOperator); Attribute.NumberOfValues = 0; Attribute.Value = NULL; }
562
563 CssmDbAttributeData &attribute() { return CssmDbAttributeData::overlay(Attribute); }
564 const CssmDbAttributeData &attribute() const { return CssmDbAttributeData::overlay(Attribute); }
565
566 // Set the value of this CssmSelectionPredicate (assuming it was not set before).
567 void set(const CSSM_DB_ATTRIBUTE_INFO &inInfo,
568 const CssmPolyData &inValue, Allocator &inAllocator)
569 { attribute().set(inInfo, inValue, inAllocator); }
570
571 // Set the value of this CssmSelectionPredicate using another CssmSelectionPredicate's value.
572 void set(const CSSM_SELECTION_PREDICATE &other, Allocator &inAllocator)
573 { DbOperator = other.DbOperator; attribute().set(other.Attribute, inAllocator); }
574
575 // Add a value to the list of values for this CssmSelectionPredicate.
576 void add(const CssmPolyData &inValue, Allocator &inAllocator)
577 { attribute().add(inValue, inAllocator); }
578
579 void deleteValues(Allocator &inAllocator) { attribute().deleteValues(inAllocator); }
580 };
581
582 class CssmQuery : public PodWrapper<CssmQuery, CSSM_QUERY> {
583 public:
584 CssmQuery(CSSM_DB_RECORDTYPE type = CSSM_DL_DB_RECORD_ANY)
585 { clearPod(); RecordType = type; }
586
587 // copy or assign flat from CSSM_QUERY
588 CssmQuery(const CSSM_QUERY &q) { assignPod(q); }
589 CssmQuery &operator = (const CSSM_QUERY &q) { assignPod(q); return *this; }
590
591 // flat copy and change record type
592 CssmQuery(const CssmQuery &q, CSSM_DB_RECORDTYPE type)
593 { *this = q; RecordType = type; }
594
595 CSSM_DB_RECORDTYPE recordType() const { return RecordType; }
596 void recordType(CSSM_DB_RECORDTYPE recordType) { RecordType = recordType; }
597
598 CSSM_DB_CONJUNCTIVE conjunctive() const { return Conjunctive; }
599 void conjunctive(CSSM_DB_CONJUNCTIVE conjunctive) { Conjunctive = conjunctive; }
600
601 CSSM_QUERY_LIMITS queryLimits() const { return QueryLimits; }
602 void queryLimits(CSSM_QUERY_LIMITS queryLimits) { QueryLimits = queryLimits; }
603
604 CSSM_QUERY_FLAGS queryFlags() const { return QueryFlags; }
605 void queryFlags(CSSM_QUERY_FLAGS queryFlags) { QueryFlags = queryFlags; }
606
607 uint32 size() const { return NumSelectionPredicates; }
608
609 CssmSelectionPredicate *&predicates()
610 { return CssmSelectionPredicate::overlayVar(SelectionPredicate); }
611 CssmSelectionPredicate *predicates() const
612 { return CssmSelectionPredicate::overlay(SelectionPredicate); }
613
614 CssmSelectionPredicate &at(uint32 ix) const {
615 if (ix >= size()) {
616 secemergency("CssmDbRecordAttributeData: attempt to index beyond bounds");
617 abort();
618 }
619 return predicates()[ix];
620 }
621
622 CssmSelectionPredicate &operator[] (uint32 ix) const { return at(ix); }
623
624 void set(uint32 count, CSSM_SELECTION_PREDICATE *preds)
625 { NumSelectionPredicates = count; SelectionPredicate = preds; }
626
627 void deleteValues(Allocator &allocator)
628 { for (uint32 ix = 0; ix < size(); ++ix) at(ix).deleteValues(allocator); }
629 };
630
631
632 class CssmAutoQuery : public CssmQuery, public ArrayBuilder<CssmSelectionPredicate> {
633 public:
634 CssmAutoQuery(const CSSM_QUERY &query, Allocator &allocator = Allocator::standard());
635 CssmAutoQuery(uint32 capacity = 0, Allocator &allocator = Allocator::standard()) :
636 ArrayBuilder<CssmSelectionPredicate>(CssmSelectionPredicate::overlayVar(SelectionPredicate),
637 NumSelectionPredicates,
638 capacity, allocator) {}
639 ~CssmAutoQuery();
640 void clear();
641 void deleteValues() { CssmQuery::deleteValues(allocator()); }
642
643 CssmSelectionPredicate &add() { return ArrayBuilder<CssmSelectionPredicate>::add(); }
644 CssmSelectionPredicate &add(CSSM_DB_OPERATOR dbOperator, const CSSM_DB_ATTRIBUTE_INFO &info, const CssmPolyData &value);
645
646 // So clients can pass this as the allocator argument to add()
647 operator Allocator &() const { return allocator(); }
648 };
649
650
651 //
652 // DLDbIdentifier
653 //
654 class DLDbIdentifier
655 {
656 protected:
657 class Impl : public RefCount
658 {
659 NOCOPY(Impl)
660 public:
661 Impl(const CSSM_SUBSERVICE_UID &ssuid,const char *DbName,const CSSM_NET_ADDRESS *DbLocation) :
662 mCssmSubserviceUid(ssuid),mDbName(DbName,DbLocation) {}
663
664 ~Impl() {} // Must be public since RefPointer uses it.
665
666 // Accessors
667 const CssmSubserviceUid &ssuid() const { return mCssmSubserviceUid; }
668 const char *dbName() const { return mDbName.dbName(); }
669 const CssmNetAddress *dbLocation() const { return mDbName.dbLocation(); }
670
671 // comparison (simple lexicographic)
672 bool operator < (const Impl &other) const;
673 bool operator == (const Impl &other) const;
674 private:
675 // Private member variables
676 CssmSubserviceUid mCssmSubserviceUid;
677 DbName mDbName;
678 };
679
680 public:
681 // Constructors
682 DLDbIdentifier() {}
683 DLDbIdentifier(const CSSM_SUBSERVICE_UID &ssuid, const char *DbName, const CSSM_NET_ADDRESS *DbLocation)
684 : mImpl(new Impl(ssuid, DbName, DbLocation)) {}
685 DLDbIdentifier(const char *name, const Guid &guid, uint32 ssid, uint32 sstype,
686 const CSSM_NET_ADDRESS *location = NULL)
687 : mImpl(new Impl(CssmSubserviceUid(guid, NULL, ssid, sstype), name, location)) { }
688
689 // Conversion Operators
690 bool operator !() const { return !mImpl; }
691 operator bool() const { return mImpl; }
692
693 // Operators
694 bool operator <(const DLDbIdentifier &other) const
695 { return mImpl && other.mImpl ? *mImpl < *other.mImpl : mImpl.get() < other.mImpl.get(); }
696 bool operator ==(const DLDbIdentifier &other) const
697 { return mImpl && other.mImpl ? *mImpl == *other.mImpl : mImpl.get() == other.mImpl.get(); }
698 DLDbIdentifier &operator =(const DLDbIdentifier &other)
699 { mImpl = other.mImpl; return *this; }
700
701 // Accessors
702 const CssmSubserviceUid &ssuid() const { return mImpl->ssuid(); }
703 const char *dbName() const { return mImpl->dbName(); }
704 const CssmNetAddress *dbLocation() const { return mImpl->dbLocation(); }
705 bool IsImplEmpty() const {return mImpl == NULL;}
706
707 RefPointer<Impl> mImpl;
708 };
709
710 // Wrappers for index-related CSSM objects.
711
712 class CssmDbIndexInfo : public PodWrapper<CssmDbIndexInfo, CSSM_DB_INDEX_INFO>
713 {
714 public:
715 CssmDbIndexInfo(const CSSM_DB_INDEX_INFO &attr)
716 { (CSSM_DB_INDEX_INFO &)*this = attr; }
717
718 CSSM_DB_INDEX_TYPE indexType() const { return IndexType; }
719 void indexType(CSSM_DB_INDEX_TYPE indexType) { IndexType = indexType; }
720
721 CSSM_DB_INDEXED_DATA_LOCATION dataLocation() const { return IndexedDataLocation; }
722 void dataLocation(CSSM_DB_INDEXED_DATA_LOCATION dataLocation)
723 {
724 IndexedDataLocation = dataLocation;
725 }
726
727 const CssmDbAttributeInfo &attributeInfo() const
728 {
729 return CssmDbAttributeInfo::overlay(Info);
730 }
731 };
732
733
734 namespace DataWalkers {
735
736
737 //
738 // DLDbIdentifiers don't walk directly because they have Impl structure and use strings.
739 // Happily, they are easily transcribed into a walkable form.
740 //
741 struct DLDbFlatIdentifier {
742 CssmSubserviceUid *uid; // module reference
743 char *name; // string name
744 CssmNetAddress *address; // optional network address
745
746 DLDbFlatIdentifier(const DLDbIdentifier &ident) :
747 uid(const_cast<CssmSubserviceUid *>(&ident.ssuid())),
748 name(const_cast<char *>(ident.dbName())),
749 address(const_cast<CssmNetAddress *>(ident.dbLocation()))
750 { }
751
752 operator DLDbIdentifier () { return DLDbIdentifier(*uid, name, address); }
753 };
754
755 template<class Action>
756 DLDbFlatIdentifier *walk(Action &operate, DLDbFlatIdentifier * &ident)
757 {
758 operate(ident);
759 if (ident->uid)
760 walk(operate, ident->uid);
761 walk(operate, ident->name);
762 if (ident->address)
763 walk(operate, ident->address);
764 return ident;
765 }
766
767
768 //
769 // Walkers for the byzantine data structures of the DL universe.
770 // Geez, what WERE they smoking when they invented this?
771 //
772
773 // DbAttributeInfos
774 template<class Action>
775 void enumerate(Action &operate, CssmDbAttributeInfo &info)
776 {
777 switch (info.nameFormat()) {
778 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING:
779 walk(operate, info.Label.AttributeName);
780 break;
781 case CSSM_DB_ATTRIBUTE_NAME_AS_OID:
782 walk(operate, info.Label.AttributeOID);
783 break;
784 default:
785 break;
786 }
787 }
788
789 template <class Action>
790 void walk(Action &operate, CssmDbAttributeInfo &info)
791 {
792 operate(info);
793 enumerate(operate, info);
794 }
795
796 template <class Action>
797 CssmDbAttributeInfo *walk(Action &operate, CssmDbAttributeInfo * &info)
798 {
799 operate(info);
800 enumerate(operate, *info);
801 return info;
802 }
803
804 // DbRecordAttributeInfo
805 template <class Action>
806 void walk(Action &operate, CssmDbRecordAttributeInfo &info)
807 {
808 operate(info);
809 enumerateArray(operate, info, &CssmDbRecordAttributeInfo::attributes);
810 }
811
812 template <class Action>
813 CssmDbRecordAttributeInfo *walk(Action &operate, CssmDbRecordAttributeInfo * &info)
814 {
815 operate(info);
816 enumerateArray(operate, *info, &CssmDbRecordAttributeInfo::attributes);
817 return info;
818 }
819
820 // DbAttributeData (Info + value vector)
821 template <class Action>
822 void walk(Action &operate, CssmDbAttributeData &data)
823 {
824 operate(data);
825 walk(operate, data.info());
826 enumerateArray(operate, data, &CssmDbAttributeData::values);
827 }
828
829 template <class Action>
830 CssmDbAttributeData *walk(Action &operate, CssmDbAttributeData * &data)
831 {
832 operate(data);
833 walk(operate, data->info());
834 enumerateArray(operate, *data, &CssmDbAttributeData::values);
835 return data;
836 }
837
838 // DbRecordAttributeData (array of ...datas)
839 template <class Action>
840 void walk(Action &operate, CssmDbRecordAttributeData &data)
841 {
842 operate(data);
843 enumerateArray(operate, data, &CssmDbRecordAttributeData::attributes);
844 }
845
846 template <class Action>
847 CssmDbRecordAttributeData *walk(Action &operate, CssmDbRecordAttributeData * &data)
848 {
849 operate(data);
850 enumerateArray(operate, *data, &CssmDbRecordAttributeData::attributes);
851 return data;
852 }
853
854 // SelectionPredicates
855 template <class Action>
856 CssmSelectionPredicate *walk(Action &operate, CssmSelectionPredicate * &predicate)
857 {
858 operate(predicate);
859 walk(operate, predicate->attribute());
860 return predicate;
861 }
862
863 template<class Action>
864 void walk(Action &operate, CssmSelectionPredicate &predicate)
865 {
866 operate(predicate);
867 walk(operate, predicate.attribute());
868 }
869
870 // Queries
871 template <class Action>
872 void walk(Action &operate, CssmQuery &query)
873 {
874 operate(query);
875 enumerateArray(operate, query, &CssmQuery::predicates);
876 }
877
878 template <class Action>
879 CssmQuery *walk(Action &operate, CssmQuery * &query)
880 {
881 operate(query);
882 enumerateArray(operate, *query, &CssmQuery::predicates);
883 return query;
884 }
885
886 template <class Action>
887 CSSM_QUERY *walk(Action &operate, CSSM_QUERY * &query)
888 {
889 return walk(operate, CssmQuery::overlayVar(query));
890 }
891
892
893 } // end namespace DataWalkers
894 } // end namespace Security
895
896
897 #endif // _H_CDSA_UTILITIES_CSSMDB