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 "MetaAttribute.h"
24 #include "MetaRecord.h"
26 MetaAttribute::~MetaAttribute()
30 // Construct an instance of an appropriate subclass of MetaAttribute
31 // based on the given format.
34 MetaAttribute::create(Format format
, uint32 attributeIndex
,
39 case CSSM_DB_ATTRIBUTE_FORMAT_STRING
:
40 return new TypedMetaAttribute
<StringValue
>(format
, attributeIndex
, attributeId
);
42 case CSSM_DB_ATTRIBUTE_FORMAT_SINT32
:
43 return new TypedMetaAttribute
<SInt32Value
>(format
, attributeIndex
, attributeId
);
45 case CSSM_DB_ATTRIBUTE_FORMAT_UINT32
:
46 return new TypedMetaAttribute
<UInt32Value
>(format
, attributeIndex
, attributeId
);
48 case CSSM_DB_ATTRIBUTE_FORMAT_BIG_NUM
:
49 return new TypedMetaAttribute
<BigNumValue
>(format
, attributeIndex
, attributeId
);
51 case CSSM_DB_ATTRIBUTE_FORMAT_REAL
:
52 return new TypedMetaAttribute
<DoubleValue
>(format
, attributeIndex
, attributeId
);
54 case CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE
:
55 return new TypedMetaAttribute
<TimeDateValue
>(format
, attributeIndex
, attributeId
);
57 case CSSM_DB_ATTRIBUTE_FORMAT_BLOB
:
58 return new TypedMetaAttribute
<BlobValue
>(format
, attributeIndex
, attributeId
);
60 case CSSM_DB_ATTRIBUTE_FORMAT_MULTI_UINT32
:
61 return new TypedMetaAttribute
<MultiUInt32Value
>(format
, attributeIndex
, attributeId
);
63 case CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX
:
65 CssmError::throwMe(CSSMERR_DL_UNSUPPORTED_FIELD_FORMAT
);
70 MetaAttribute::packNumberOfValues(WriteSection
&ws
, uint32 numValues
, uint32
&valueOffset
) const
72 uint32 offset
= MetaRecord::OffsetAttributeOffsets
+ mAttributeIndex
* AtomSize
;
75 // a zero offset means the attribute has no values
78 else if (numValues
== 1) {
79 // setting the low bit of the offset means that there is exactly one value
80 ws
.put(offset
, valueOffset
| 1);
83 // write the offset, then write the number of values at that position
84 ws
.put(offset
, valueOffset
);
85 valueOffset
= ws
.put(valueOffset
, numValues
);
90 MetaAttribute::unpackNumberOfValues(const ReadSection
&rs
, uint32
&numValues
,
91 uint32
&valueOffset
) const
93 uint32 offset
= MetaRecord::OffsetAttributeOffsets
+ mAttributeIndex
* AtomSize
;
94 valueOffset
= rs
[offset
];
97 // a zero offset means no values
99 else if (valueOffset
& 1) {
100 // setting the LSB means exactly one value
105 // otherwise, the number of values is at the offset, and the values follow
106 numValues
= rs
[valueOffset
];
107 valueOffset
+= AtomSize
;
112 MetaAttribute::packAttribute(WriteSection
&ws
, uint32
&valueOffset
, uint32 numValues
,
113 const CSSM_DATA
*values
) const
115 packNumberOfValues(ws
, numValues
, valueOffset
);
116 for (uint32 i
= 0; i
< numValues
; i
++)
117 packValue(ws
, valueOffset
, values
[i
]);
121 MetaAttribute::unpackAttribute(const ReadSection
&rs
, CssmAllocator
&allocator
,
122 uint32
&numValues
, CSSM_DATA
*&values
) const
125 unpackNumberOfValues(rs
, numValues
, valueOffset
);
127 values
= reinterpret_cast<CSSM_DATA
*>(allocator
.malloc(numValues
* sizeof(CSSM_DATA
)));
129 for (uint32 i
= 0; i
< numValues
; i
++)
130 unpackValue(rs
, valueOffset
, values
[i
], allocator
);
134 MetaAttribute::getNumberOfValues(const ReadSection
&rs
) const
136 uint32 numValues
, valueOffset
;
137 unpackNumberOfValues(rs
, numValues
, valueOffset
);
142 MetaAttribute::copyValueBytes(uint32 valueIndex
, const ReadSection
&rs
, WriteSection
&ws
,
143 uint32
&writeOffset
) const
145 uint32 numValues
, valueOffset
;
146 unpackNumberOfValues(rs
, numValues
, valueOffset
);
148 // skip bytes before the desired value
149 for (uint32 i
= 0; i
< valueIndex
; i
++)
150 skipValue(rs
, valueOffset
);
152 // copy the value bytes into the write section
153 copyValue(rs
, valueOffset
, ws
, writeOffset
);