2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
23 #include "MetaRecord.h"
25 MetaRecord::MetaRecord(CSSM_DB_RECORDTYPE inRecordType
) :
26 mRecordType(inRecordType
)
30 MetaRecord::MetaRecord(const CSSM_DB_RECORD_ATTRIBUTE_INFO
&inInfo
)
31 : mRecordType(inInfo
.DataRecordType
)
35 setRecordAttributeInfo(inInfo
);
39 for_each_delete(mAttributeVector
.begin(), mAttributeVector
.end());
43 MetaRecord::MetaRecord(CSSM_DB_RECORDTYPE inRelationID
,
44 uint32 inNumberOfAttributes
,
45 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*inAttributeInfo
) :
46 mRecordType(inRelationID
)
48 // XXX Is there any particular reason not to allow this?
50 if (inNumberOfAttributes
== 0 || inAttributeInfo
== NULL
)
51 CssmError::throwMe(CSSMERR_DL_UNSUPPORTED_NUM_ATTRIBUTES
);
55 for (uint32 anIndex
= 0; anIndex
< inNumberOfAttributes
; anIndex
++)
58 if (inAttributeInfo
[anIndex
].AttributeName
)
59 aName
= string(inAttributeInfo
[anIndex
].AttributeName
);
61 const CssmData
*aNameID
= NULL
;
62 if (inAttributeInfo
[anIndex
].AttributeNameID
.Length
> 0)
63 aNameID
= &CssmData::overlay(inAttributeInfo
[anIndex
].AttributeNameID
);
65 uint32 aNumber
= inAttributeInfo
[anIndex
].AttributeId
;
67 inAttributeInfo
[anIndex
].AttributeName
? &aName
: NULL
,
69 inAttributeInfo
[anIndex
].DataType
);
74 for_each_delete(mAttributeVector
.begin(), mAttributeVector
.end());
78 MetaRecord::~MetaRecord()
80 for_each_delete(mAttributeVector
.begin(), mAttributeVector
.end());
84 MetaRecord::setRecordAttributeInfo(const CSSM_DB_RECORD_ATTRIBUTE_INFO
&inInfo
)
86 // XXX Is there any particular reason not to allow this?
88 if (inInfo
.NumberOfAttributes
== 0 || inInfo
.AttributeInfo
== NULL
)
89 CssmError::throwMe(CSSMERR_DL_UNSUPPORTED_NUM_ATTRIBUTES
);
92 for (uint32 anIndex
= 0; anIndex
< inInfo
.NumberOfAttributes
; anIndex
++)
94 switch (inInfo
.AttributeInfo
[anIndex
].AttributeNameFormat
)
96 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING
:
98 string
aName(inInfo
.AttributeInfo
[anIndex
].Label
.AttributeName
);
99 createAttribute(&aName
, nil
, anIndex
,
100 inInfo
.AttributeInfo
[anIndex
].AttributeFormat
);
103 case CSSM_DB_ATTRIBUTE_NAME_AS_OID
:
105 const CssmData
&aNameID
= CssmOid::overlay(inInfo
.AttributeInfo
[anIndex
].Label
.AttributeOID
);
106 createAttribute(nil
, &aNameID
, anIndex
,
107 inInfo
.AttributeInfo
[anIndex
].AttributeFormat
);
110 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
:
112 uint32 aNumber
= inInfo
.AttributeInfo
[anIndex
].Label
.AttributeID
;
113 createAttribute(nil
, nil
, aNumber
,
114 inInfo
.AttributeInfo
[anIndex
].AttributeFormat
);
118 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
125 MetaRecord::createAttribute(const string
*inAttributeName
,
126 const CssmOid
*inAttributeOID
,
127 uint32 inAttributeID
,
128 CSSM_DB_ATTRIBUTE_FORMAT inAttributeFormat
)
130 // Index of new element is current size of vector
131 uint32 anAttributeIndex
= mAttributeVector
.size();
132 bool aInsertedAttributeName
= false;
133 bool aInsertedAttributeOID
= false;
134 bool aInsertedAttributeID
= false;
138 if (!mNameStringMap
.insert(NameStringMap::value_type(*inAttributeName
, anAttributeIndex
)).second
)
139 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE
);
140 aInsertedAttributeName
= true;
146 if (!mNameOIDMap
.insert(NameOIDMap::value_type(*inAttributeOID
, anAttributeIndex
)).second
)
147 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE
);
148 aInsertedAttributeOID
= true;
151 if (!mNameIntMap
.insert(NameIntMap::value_type(inAttributeID
, anAttributeIndex
)).second
)
152 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE
);
153 aInsertedAttributeID
= true;
155 // Note: this no longer throws INVALID_FIELD_NAME since the attribute will always have
156 // an attribute ID by which it is known
158 mAttributeVector
.push_back(MetaAttribute::create(inAttributeFormat
,
159 anAttributeIndex
, inAttributeID
));
163 if (aInsertedAttributeName
)
164 mNameStringMap
.erase(*inAttributeName
);
165 if (aInsertedAttributeOID
)
166 mNameOIDMap
.erase(*inAttributeOID
);
168 mNameIntMap
.erase(inAttributeID
);
175 // Create a packed record from the given inputs.
177 MetaRecord::packRecord(WriteSection
&inWriteSection
,
178 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributes
,
179 const CssmData
*inData
) const
183 aDataSize
= inData
->Length
;
187 inWriteSection
.put(OffsetDataSize
, aDataSize
);
188 uint32 anOffset
= OffsetAttributeOffsets
+ AtomSize
* mAttributeVector
.size();
190 anOffset
= inWriteSection
.put(anOffset
, aDataSize
, inData
->Data
);
192 vector
<uint32
> aNumValues(mAttributeVector
.size(), ~0UL);
193 vector
<CSSM_DATA_PTR
> aValues(mAttributeVector
.size());
196 if (inAttributes
== NULL
)
197 inWriteSection
.put(OffsetSemanticInformation
, 0);
200 inWriteSection
.put(OffsetSemanticInformation
, inAttributes
->SemanticInformation
);
202 // Put the supplied attribute values into the list of attributes
204 anIndex
= inAttributes
->NumberOfAttributes
;
205 // Make sure that AttributeData is a valid array.
207 Required(inAttributes
->AttributeData
);
209 while (anIndex
-- > 0)
211 CSSM_DB_ATTRIBUTE_DATA
&anAttribute
= inAttributes
->AttributeData
[anIndex
];
212 uint32 anAttributeIndex
= attributeIndex(anAttribute
.Info
);
213 // Make sure that the caller specified the attribute values in the correct format.
214 if (anAttribute
.Info
.AttributeFormat
!= mAttributeVector
[anAttributeIndex
]->attributeFormat())
215 CssmError::throwMe(CSSMERR_DL_INCOMPATIBLE_FIELD_FORMAT
);
217 // If this attribute was specified before, throw.
218 if (aNumValues
[anAttributeIndex
] != ~0UL)
219 CssmError::throwMe(CSSMERR_DL_FIELD_SPECIFIED_MULTIPLE
);
221 aNumValues
[anAttributeIndex
] = anAttribute
.NumberOfValues
;
222 aValues
[anAttributeIndex
] = anAttribute
.Value
;
226 for (anIndex
= 0; anIndex
< mAttributeVector
.size(); ++anIndex
)
228 const MetaAttribute
&aMetaAttribute
= *mAttributeVector
[anIndex
];
229 uint32 aNumberOfValues
= aNumValues
[anIndex
];
230 // Now call the parsingmodule for each attribute that
231 // wasn't explicitly specified and that has a parsingmodule.
232 if (aNumberOfValues
== ~0UL)
233 aNumberOfValues
= aDataSize
== 0 ? 0 : aMetaAttribute
.parse(*inData
, aValues
[anIndex
]);
235 // XXX When do we throw CSSMERR_DL_MISSING_VALUE? Maybe if an
236 // attribute is part of a unique index.
238 // Now we have a valuelist for this attribute. Let's encode it.
239 aMetaAttribute
.packAttribute(inWriteSection
, anOffset
, aNumberOfValues
, aValues
[anIndex
]);
242 inWriteSection
.put(OffsetRecordSize
, anOffset
);
243 inWriteSection
.size(anOffset
);
247 MetaRecord::unpackAttribute(const ReadSection
&inReadSection
,
248 CssmAllocator
&inAllocator
,
249 CSSM_DB_ATTRIBUTE_DATA
&inoutAttribute
) const
251 const MetaAttribute
&aMetaAttribute
= metaAttribute(inoutAttribute
.Info
);
252 // XXX: See ISSUES on whether AttributeFormat should be an outputvalue or not.
253 inoutAttribute
.Info
.AttributeFormat
= aMetaAttribute
.attributeFormat();
254 aMetaAttribute
.unpackAttribute(inReadSection
, inAllocator
,
255 inoutAttribute
.NumberOfValues
,
256 inoutAttribute
.Value
);
260 MetaRecord::unpackRecord(const ReadSection
&inReadSection
,
261 CssmAllocator
&inAllocator
,
262 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
264 CSSM_QUERY_FLAGS inQueryFlags
) const
266 // XXX Use POD wrapper for inoutAttributes here.
267 TrackingAllocator
anAllocator(inAllocator
);
270 // XXX Treat KEY records specially.
272 // If inQueryFlags & CSSM_QUERY_RETURN_DATA is true return the raw
273 // key bits in the CSSM_KEY structure
274 Range aDataRange
= dataRange(inReadSection
);
275 inoutData
->Length
= aDataRange
.mSize
;
276 inoutData
->Data
= inReadSection
.allocCopyRange(aDataRange
, anAllocator
);
281 inoutAttributes
->DataRecordType
= dataRecordType();
282 inoutAttributes
->SemanticInformation
= semanticInformation(inReadSection
);
283 uint32 anIndex
= inoutAttributes
->NumberOfAttributes
;
285 // Make sure that AttributeData is a valid array.
286 if (anIndex
> 0 && inoutAttributes
->AttributeData
== NULL
)
287 CssmError::throwMe(CSSM_ERRCODE_INVALID_POINTER
);
289 while (anIndex
-- > 0)
291 unpackAttribute(inReadSection
, anAllocator
,
292 inoutAttributes
->AttributeData
[anIndex
]);
296 // Don't free anything the trackingAllocator allocated when it is destructed.
297 anAllocator
.commit();
300 // Return the index (0 though NumAttributes - 1) of the attribute
301 // represented by inAttributeInfo
303 MetaRecord::attributeIndex(const CSSM_DB_ATTRIBUTE_INFO
&inAttributeInfo
) const
306 switch (inAttributeInfo
.AttributeNameFormat
)
308 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING
:
310 string
aName(inAttributeInfo
.Label
.AttributeName
);
311 NameStringMap::const_iterator it
= mNameStringMap
.find(aName
);
312 if (it
== mNameStringMap
.end())
313 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
314 anIndex
= it
->second
;
317 case CSSM_DB_ATTRIBUTE_NAME_AS_OID
:
319 const CssmOid
&aName
= CssmOid::overlay(inAttributeInfo
.Label
.AttributeOID
);
320 NameOIDMap::const_iterator it
= mNameOIDMap
.find(aName
);
321 if (it
== mNameOIDMap
.end())
322 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
323 anIndex
= it
->second
;
326 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
:
328 uint32 aName
= inAttributeInfo
.Label
.AttributeID
;
329 NameIntMap::const_iterator it
= mNameIntMap
.find(aName
);
330 if (it
== mNameIntMap
.end())
331 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
332 anIndex
= it
->second
;
336 CssmError::throwMe(CSSMERR_DL_INVALID_FIELD_NAME
);
343 const MetaAttribute
&
344 MetaRecord::metaAttribute(const CSSM_DB_ATTRIBUTE_INFO
&inAttributeInfo
) const
346 return *mAttributeVector
[attributeIndex(inAttributeInfo
)];
349 // Create a packed record from the given inputs and the old packed record inReadSection.
351 MetaRecord::updateRecord(const ReadSection
&inReadSection
,
352 WriteSection
&inWriteSection
,
353 const CssmDbRecordAttributeData
*inAttributes
,
354 const CssmData
*inData
,
355 CSSM_DB_MODIFY_MODE inModifyMode
) const
357 TrackingAllocator
anAllocator(CssmAllocator::standard());
359 // modify the opaque data associated with the record
362 const uint8
*aDataData
= NULL
;
366 // prepare to write new data
367 aDataSize
= inData
->Length
;
368 aDataData
= inData
->Data
;
372 // prepare to copy old data
373 Range aDataRange
= dataRange(inReadSection
);
374 aDataSize
= aDataRange
.mSize
;
376 aDataData
= inReadSection
.range(aDataRange
);
379 // compute the data offset; this will keep a running total of the record size
380 uint32 anOffset
= OffsetAttributeOffsets
+ AtomSize
* mAttributeVector
.size();
382 // write the appropriate data to the new record
383 inWriteSection
.put(OffsetDataSize
, aDataSize
);
385 anOffset
= inWriteSection
.put(anOffset
, aDataSize
, aDataData
);
387 // unpack the old attributes since some of them may need to be preserved
389 auto_array
<CssmDbAttributeData
> attributeData(mAttributeVector
.size());
391 for (uint32 anAttributeIndex
= mAttributeVector
.size(); anAttributeIndex
-- > 0; )
393 // unpack the old attribute data for this attribute index
394 const MetaAttribute
&attribute
= *mAttributeVector
[anAttributeIndex
];
395 attribute
.unpackAttribute(inReadSection
, anAllocator
,
396 attributeData
[anAttributeIndex
].NumberOfValues
,
397 attributeData
[anAttributeIndex
].Value
);
400 // retrieve the currrent semantic information
402 uint32 oldSemanticInformation
= semanticInformation(inReadSection
);
404 // process each input attribute as necessary, based on the modification mode
406 if (inAttributes
== NULL
)
408 // make sure the modification mode is NONE, otherwise it's an
409 // error accordining to the spec
410 if (inModifyMode
!= CSSM_DB_MODIFY_ATTRIBUTE_NONE
)
411 CssmError::throwMe(CSSMERR_DL_INVALID_MODIFY_MODE
);
416 // modify the semantic information
418 uint32 inSemanticInformation
= inAttributes
? inAttributes
->SemanticInformation
: 0;
420 if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_ADD
)
421 oldSemanticInformation
|= inSemanticInformation
;
423 else if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_DELETE
)
424 oldSemanticInformation
&= ~inSemanticInformation
;
426 else if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_REPLACE
)
427 oldSemanticInformation
= inSemanticInformation
;
429 uint32 anIndex
= inAttributes
->NumberOfAttributes
;
431 Required(inAttributes
->AttributeData
);
433 // modify the attributes
435 while (anIndex
-- > 0) {
437 const CssmDbAttributeData
&anAttribute
= inAttributes
->at(anIndex
);
438 uint32 anAttributeIndex
= attributeIndex(anAttribute
.info());
439 if (anAttribute
.format() != mAttributeVector
[anAttributeIndex
]->attributeFormat())
440 CssmError::throwMe(CSSMERR_DL_INCOMPATIBLE_FIELD_FORMAT
);
442 CssmDbAttributeData
&oldAttribute
= attributeData
[anAttributeIndex
];
444 // if the modify mode is ADD, merge new values with pre-existing values
446 if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_ADD
)
447 oldAttribute
.add(anAttribute
, anAllocator
);
449 // if the modify mode is DELETE, remove the indicated values, or remove
450 // all values if none are specified
452 else if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_DELETE
)
454 if (anAttribute
.size() == 0)
455 oldAttribute
.deleteValues(anAllocator
);
457 oldAttribute
.deleteValues(anAttribute
, anAllocator
);
460 // if the modify mode is REPLACE, then replace the specified values, or
461 // delete all values if no values are specified
463 else if (inModifyMode
== CSSM_DB_MODIFY_ATTRIBUTE_REPLACE
)
465 oldAttribute
.deleteValues(anAllocator
);
466 if (anAttribute
.size() > 0)
467 oldAttribute
.add(anAttribute
, anAllocator
);
469 // The spec says "all values are deleted or the the value is replaced
470 // with the default" but doesn't say which. We could call the parsing
471 // module for the attribute here...if they were implemented! But instead
472 // we choose "all values are deleted" and leave it at that.
478 // write the resulting attributes into the new record
480 inWriteSection
.put(OffsetSemanticInformation
, oldSemanticInformation
);
482 for (uint32 anIndex
= 0; anIndex
< mAttributeVector
.size(); ++anIndex
)
484 const MetaAttribute
&metaAttribute
= *mAttributeVector
[anIndex
];
485 metaAttribute
.packAttribute(inWriteSection
, anOffset
,
486 attributeData
[anIndex
].NumberOfValues
,
487 attributeData
[anIndex
].Value
);
490 inWriteSection
.put(OffsetRecordSize
, anOffset
);
491 inWriteSection
.size(anOffset
);