#include "MetaAttribute.h"
#include "MetaRecord.h"
MetaAttribute::~MetaAttribute()
{
}
MetaAttribute *
MetaAttribute::create(Format format, uint32 attributeIndex,
uint32 attributeId)
{
switch (format)
{
case CSSM_DB_ATTRIBUTE_FORMAT_STRING:
return new TypedMetaAttribute<StringValue>(format, attributeIndex, attributeId);
case CSSM_DB_ATTRIBUTE_FORMAT_SINT32:
return new TypedMetaAttribute<SInt32Value>(format, attributeIndex, attributeId);
case CSSM_DB_ATTRIBUTE_FORMAT_UINT32:
return new TypedMetaAttribute<UInt32Value>(format, attributeIndex, attributeId);
case CSSM_DB_ATTRIBUTE_FORMAT_BIG_NUM:
return new TypedMetaAttribute<BigNumValue>(format, attributeIndex, attributeId);
case CSSM_DB_ATTRIBUTE_FORMAT_REAL:
return new TypedMetaAttribute<DoubleValue>(format, attributeIndex, attributeId);
case CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE:
return new TypedMetaAttribute<TimeDateValue>(format, attributeIndex, attributeId);
case CSSM_DB_ATTRIBUTE_FORMAT_BLOB:
return new TypedMetaAttribute<BlobValue>(format, attributeIndex, attributeId);
case CSSM_DB_ATTRIBUTE_FORMAT_MULTI_UINT32:
return new TypedMetaAttribute<MultiUInt32Value>(format, attributeIndex, attributeId);
case CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX:
default:
CssmError::throwMe(CSSMERR_DL_UNSUPPORTED_FIELD_FORMAT);
}
}
void
MetaAttribute::packNumberOfValues(WriteSection &ws, uint32 numValues, uint32 &valueOffset) const
{
uint32 offset = MetaRecord::OffsetAttributeOffsets + mAttributeIndex * AtomSize;
if (numValues == 0) {
ws.put(offset, 0);
}
else if (numValues == 1) {
ws.put(offset, valueOffset | 1);
}
else {
ws.put(offset, valueOffset);
valueOffset = ws.put(valueOffset, numValues);
}
}
void
MetaAttribute::unpackNumberOfValues(const ReadSection &rs, uint32 &numValues,
uint32 &valueOffset) const
{
uint32 offset = MetaRecord::OffsetAttributeOffsets + mAttributeIndex * AtomSize;
valueOffset = rs[offset];
if (valueOffset == 0)
numValues = 0;
else if (valueOffset & 1) {
valueOffset ^= 1;
numValues = 1;
}
else {
numValues = rs[valueOffset];
valueOffset += AtomSize;
}
}
void
MetaAttribute::packAttribute(WriteSection &ws, uint32 &valueOffset, uint32 numValues,
const CSSM_DATA *values) const
{
packNumberOfValues(ws, numValues, valueOffset);
for (uint32 i = 0; i < numValues; i++)
packValue(ws, valueOffset, values[i]);
}
void
MetaAttribute::unpackAttribute(const ReadSection &rs, Allocator &allocator,
uint32 &numValues, CSSM_DATA *&values) const
{
uint32 valueOffset;
unpackNumberOfValues(rs, numValues, valueOffset);
if (numValues > 1024)
CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
values = reinterpret_cast<CSSM_DATA *>(allocator.malloc(numValues * sizeof(CSSM_DATA)));
for (uint32 i = 0; i < numValues; i++)
unpackValue(rs, valueOffset, values[i], allocator);
}
uint32
MetaAttribute::getNumberOfValues(const ReadSection &rs) const
{
uint32 numValues, valueOffset;
unpackNumberOfValues(rs, numValues, valueOffset);
return numValues;
}
void
MetaAttribute::copyValueBytes(uint32 valueIndex, const ReadSection &rs, WriteSection &ws,
uint32 &writeOffset) const
{
uint32 numValues, valueOffset;
unpackNumberOfValues(rs, numValues, valueOffset);
for (uint32 i = 0; i < valueIndex; i++)
skipValue(rs, valueOffset);
copyValue(rs, valueOffset, ws, writeOffset);
}