#include <security_cdsa_utilities/cssmdbname.h>
#include <security_cdsa_utilities/cssmbridge.h>
#include <security_utilities/utilities.h>
CssmNetAddress::CssmNetAddress(CSSM_DB_RECORDTYPE inAddressType, const CssmData &inAddress)
{
AddressType = inAddressType;
Address.Length = inAddress.Length;
if (Address.Length > 0)
{
Address.Data = new uint8[Address.Length];
memcpy (Address.Data, inAddress.Data, Address.Length);
}
else
Address.Data = NULL;
}
CssmNetAddress::CssmNetAddress(const CSSM_NET_ADDRESS &other)
{
AddressType = other.AddressType;
Address.Length = other.Address.Length;
if (Address.Length > 0)
{
Address.Data = new uint8[Address.Length];
memcpy (Address.Data, other.Address.Data, Address.Length);
}
else
Address.Data = NULL;
}
CssmNetAddress::~CssmNetAddress()
{
if (Address.Length > 0)
delete Address.Data;
}
void DbName::CanonicalizeName()
{
if (mDbNameValid)
{
char* s = cached_realpath(mDbName.c_str(), NULL);
if (s != NULL)
{
mCanonicalName = s;
free(s);
}
else
{
ptrdiff_t n = mDbName.length() - 1;
while (n > 0)
{
while (n > 0 && mDbName[n] != '/') {
n -= 1;
}
if (n > 0)
{
string tmpPath = mDbName.substr(0, n);
s = cached_realpath(tmpPath.c_str(), NULL);
if (s != NULL)
{
mCanonicalName = s;
free(s);
mCanonicalName += mDbName.substr(n, mDbName.length() - n);
return;
}
}
n -= 1;
}
mCanonicalName = mDbName;
}
}
}
DbName::DbName(const char *inDbName, const CSSM_NET_ADDRESS *inDbLocation)
: mDbName(inDbName ? inDbName : ""), mDbNameValid(inDbName), mDbLocation(NULL)
{
if (inDbLocation)
{
mDbLocation = new CssmNetAddress(*inDbLocation);
}
CanonicalizeName();
}
DbName::DbName(const DbName &other)
: mDbName(other.mDbName), mDbNameValid(other.mDbNameValid), mDbLocation(NULL)
{
if (other.mDbLocation)
{
mDbLocation = new CssmNetAddress(*other.mDbLocation);
}
CanonicalizeName();
}
DbName &
DbName::operator =(const DbName &other)
{
mDbName = other.mDbName;
mDbNameValid = other.mDbNameValid;
if (other.mDbLocation)
{
mDbLocation = new CssmNetAddress(*other.mDbLocation);
}
return *this;
}
DbName::~DbName()
{
delete mDbLocation;
}