#include "dictionary.h"
#include <ctype.h>
namespace Security {
CssmData NameValuePair::CloneData (const CssmData &value)
{
void* clonedData = (void*) new unsigned char [value.length ()];
memcpy (clonedData, value.data (), value.length ());
return CssmData (clonedData, value.length ());
}
NameValuePair::NameValuePair (uint32 name, const CssmData &value) : mName (name), mValue (CloneData (value))
{
}
NameValuePair::NameValuePair (const CssmData &data)
{
unsigned char* finger = (unsigned char*) data.data ();
mName = 0;
unsigned int i;
for (i = 0; i < sizeof (uint32); ++i)
{
mName = (mName << 8) | *finger++;
}
uint32 length = 0;
for (i = 0; i < sizeof (uint32); ++i)
{
length = (length << 8) | *finger++;
}
mValue = CloneData (CssmData (finger, length));
}
NameValuePair::~NameValuePair ()
{
delete (unsigned char*) mValue.data ();
}
void NameValuePair::Export (CssmData &data) const
{
uint32 outSize = 2 * sizeof (uint32) + mValue.length ();
unsigned char* d = new unsigned char [outSize];
unsigned char* finger = d;
uint32 intBuffer = mName;
int i;
for (i = sizeof (uint32) - 1; i >= 0; --i)
{
finger[i] = intBuffer & 0xFF;
intBuffer >>= 8;
}
finger += sizeof (uint32);
intBuffer = mValue.length ();
for (i = sizeof (uint32) - 1; i >= 0; --i)
{
finger[i] = intBuffer & 0xFF;
intBuffer >>= 8;
}
finger += sizeof (uint32);
memcpy (finger, mValue.data (), mValue.length ());
data = CssmData (d, outSize);
}
NameValueDictionary::NameValueDictionary ()
{
}
NameValueDictionary::~NameValueDictionary ()
{
int i = mVector.size ();
while (i > 0)
{
delete mVector[--i];
mVector.erase (mVector.begin () + i);
}
}
NameValueDictionary::NameValueDictionary (const CssmData &data)
{
unsigned char* finger = (unsigned char*) data.data ();
unsigned char* target = finger + data.length ();
do
{
unsigned int i;
uint32 length = 0;
for (i = sizeof (uint32); i < 2 * sizeof (uint32); ++i)
{
length = (length << 8) | finger[i];
}
length += 2 * sizeof (uint32);
Insert (new NameValuePair (CssmData (finger, length)));
finger += length;
} while (finger < target);
}
void NameValueDictionary::Insert (NameValuePair* pair)
{
mVector.push_back (pair);
}
void NameValueDictionary::RemoveByName (uint32 name)
{
int which = FindPositionByName (name);
if (which != -1)
{
NameValuePair* nvp = mVector[which];
mVector.erase (mVector.begin () + which);
delete nvp;
}
}
int NameValueDictionary::FindPositionByName (uint32 name) const
{
int target = CountElements ();
int i;
for (i = 0; i < target; ++i)
{
if (mVector[i]->Name () == name)
{
return i;
}
}
return -1;
}
const NameValuePair* NameValueDictionary::FindByName (uint32 name) const
{
int which = FindPositionByName (name);
return which == -1 ? NULL : mVector[which];
}
int NameValueDictionary::CountElements () const
{
return mVector.size ();
}
const NameValuePair* NameValueDictionary::GetElement (int which)
{
return mVector[which];
}
void NameValueDictionary::Export (CssmData &outData)
{
int i;
uint32 length = 0;
unsigned char* data = 0;
for (i = 0; i < CountElements (); ++i)
{
CssmData exportedData;
const NameValuePair *nvp = GetElement (i);
nvp->Export (exportedData);
uint32 oldLength = length;
length += exportedData.length ();
data = (unsigned char*) realloc (data, length);
memcpy (data + oldLength, exportedData.data (), exportedData.length ());
delete (unsigned char*) exportedData.data ();
}
outData = CssmData (data, length);
}
void NameValueDictionary::MakeNameValueDictionaryFromDLDbIdentifier (const DLDbIdentifier &identifier, NameValueDictionary &nvd)
{
const CssmSubserviceUid &ssuid = identifier.ssuid ();
const CSSM_SUBSERVICE_UID* baseID = &ssuid;
nvd.Insert (new NameValuePair (SSUID_KEY, CssmData ((void*) (baseID), sizeof (CSSM_SUBSERVICE_UID))));
const char* dbName = identifier.dbName ();
nvd.Insert (new NameValuePair (DB_NAME, CssmData ((void*) (dbName), strlen (dbName) + 1)));
const CSSM_NET_ADDRESS* add = identifier.dbLocation ();
if (add != NULL)
{
nvd.Insert (new NameValuePair (DB_LOCATION, CssmData ((void*) add, sizeof (CSSM_NET_ADDRESS))));
}
}
DLDbIdentifier NameValueDictionary::MakeDLDbIdentifierFromNameValueDictionary (const NameValueDictionary &nvd)
{
CSSM_SUBSERVICE_UID* uid = (CSSM_SUBSERVICE_UID*) nvd.FindByName (SSUID_KEY)->Value ().data ();
char* name = (char*) nvd.FindByName (DB_NAME)->Value ().data ();
const NameValuePair* nvp = nvd.FindByName (DB_LOCATION);
CSSM_NET_ADDRESS* address = nvp ? (CSSM_NET_ADDRESS*) nvp->Value ().data () : NULL;
return DLDbIdentifier (*uid, name, address);
}
};