#ifndef _H_DBITEM
#define _H_DBITEM
#include <Security/Keychains.h>
#include <Security/PrimaryKey.h>
#include <Security/securestorage.h>
namespace Security
{
using namespace CssmClient;
namespace KeychainCore
{
class Item;
class Keychain;
class ItemImpl : public ReferencedObject
{
friend class Item;
protected:
ItemImpl(SecItemClass itemClass, OSType itemCreator, UInt32 length, const void* data);
ItemImpl(SecItemClass itemClass, SecKeychainAttributeList *attrList, UInt32 length, const void* data);
ItemImpl(const Keychain &keychain, const PrimaryKey &primaryKey, const CssmClient::DbUniqueRecord &uniqueId);
ItemImpl(const Keychain &keychain, const PrimaryKey &primaryKey);
ItemImpl(ItemImpl &item);
void getAttributeFrom(CssmDbAttributeData *data, SecKeychainAttribute &attr, UInt32 *actualLength);
void getClass(SecKeychainAttribute &attr, UInt32 *actualLength);
protected:
friend class KeychainImpl;
PrimaryKey add(const Keychain &keychain);
static const CSSM_DATA &defaultAttributeValue(const CSSM_DB_ATTRIBUTE_INFO &info);
public:
~ItemImpl();
bool isPersistant() const;
bool isModified() const;
void update();
Item copyTo(const Keychain &keychain);
CSSM_DB_RECORDTYPE recordType() const;
CssmClient::DbUniqueRecord dbUniqueRecord();
const CssmClient::DbAttributes *modifiedAttributes() const;
const CssmData *modifiedData() const;
void didModify();
Keychain keychain() const;
PrimaryKey primaryKey() const;
bool operator <(const ItemImpl &other) const;
void getAttribute(SecKeychainAttribute& attr, UInt32 *actualLength);
void getData(CssmDataContainer& outData);
void modifyContent(const SecKeychainAttributeList *attrList, UInt32 dataLength, const void *inData);
void getContent(SecItemClass *itemClass, SecKeychainAttributeList *attrList, UInt32 *length, void **outData);
static void freeContent(SecKeychainAttributeList *attrList, void *data);
static void freeAttributesAndData(SecKeychainAttributeList *attrList, void *data);
void getAttributesAndData(SecKeychainAttributeInfo *info, SecItemClass *itemClass, SecKeychainAttributeList **attrList, UInt32 *length, void **outData);
void modifyAttributesAndData(const SecKeychainAttributeList *attrList, UInt32 dataLength, const void *inData);
void setAttribute(SecKeychainAttribute& attr);
void setAttribute(const CssmDbAttributeInfo &info, const CssmPolyData &data);
void setData(UInt32 length,const void *data);
SSGroup group();
protected:
void getContent(DbAttributes *dbAttributes, CssmDataContainer *itemData);
auto_ptr<CssmDataContainer> mData;
auto_ptr<CssmClient::DbAttributes> mDbAttributes;
CssmClient::DbUniqueRecord mUniqueId;
Keychain mKeychain;
PrimaryKey mPrimaryKey;
};
class Item : public RefPointer<ItemImpl>
{
public:
Item() {}
Item(ItemImpl *impl) : RefPointer<ItemImpl>(impl) {}
Item(SecItemClass itemClass, OSType itemCreator, UInt32 length, const void* data)
: RefPointer<ItemImpl>(new ItemImpl(itemClass, itemCreator, length, data)) {}
Item(SecItemClass itemClass, SecKeychainAttributeList *attrList, UInt32 length, const void* data)
: RefPointer<ItemImpl>(new ItemImpl(itemClass, attrList, length, data)) {}
Item(const Keychain &keychain, const PrimaryKey &primaryKey, const CssmClient::DbUniqueRecord &uniqueId)
: RefPointer<ItemImpl>(new ItemImpl(keychain, primaryKey, uniqueId)) {}
Item(const Keychain &keychain, const PrimaryKey &primaryKey)
: RefPointer<ItemImpl>(new ItemImpl(keychain, primaryKey)) {}
Item(ItemImpl &item)
: RefPointer<ItemImpl>(new ItemImpl(item)) {}
bool operator <(const Item &other) const { return **this < *other; }
bool operator !=(const Item &other) const { return **this < *other || *other < **this; }
bool operator ==(const Item &other) const { return !(*this != other); }
typedef ItemImpl Impl;
};
typedef Ref<Item, ItemImpl, SecKeychainItemRef, errSecInvalidItemRef> ItemRef;
};
}
#endif // _H_DBITEM