#include <security_cdsa_utilities/cssmpods.h>
#include <security_cdsa_utilities/cssmbridge.h>
#include <security_utilities/endian.h>
char *Guid::toString(char buffer[stringRepLength+1]) const
{
sprintf(buffer, "{%8.8x-%4.4hx-%4.4hx-",
int(n2h(Data1)), n2h(Data2), n2h(Data3));
for (int n = 0; n < 2; n++)
sprintf(buffer + 20 + 2*n, "%2.2hhx", Data4[n]);
buffer[24] = '-';
for (int n = 2; n < 8; n++)
sprintf(buffer + 21 + 2*n, "%2.2hhx", Data4[n]);
buffer[37] = '}';
buffer[38] = '\0';
return buffer;
}
string Guid::toString() const
{
char buffer[stringRepLength+1];
return toString(buffer);
}
Guid::Guid(const char *s)
{
parseGuid(s);
}
Guid::Guid(const string &s)
{
parseGuid(s.c_str());
}
void Guid::parseGuid(const char *string)
{
if (string == NULL)
{
CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
}
if (strlen(string) < 37) {
CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
}
int d1;
uint16 d2, d3;
if (sscanf(string, "{%8x-%4hx-%4hx-", &d1, &d2, &d3) != 3)
CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
Data1 = h2n(uint32(d1));
Data2 = h2n(d2);
Data3 = h2n(d3);
bool newForm = string[24] == '-';
for (int n = 0; n < 8; n++) {
unsigned char dn;
if (sscanf(string + 20 + 2*n + (newForm && n >= 2), "%2hhx", &dn) != 1)
CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
Data4[n] = dn;
}
if (string[37 - !newForm] != '}')
CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
}
CssmGuidData::CssmGuidData(const CSSM_GUID &guid) : CssmData(buffer, sizeof(buffer))
{
Guid::overlay(guid).toString(buffer);
}
CssmSubserviceUid::CssmSubserviceUid(const CSSM_GUID &guid,
const CSSM_VERSION *version, uint32 subserviceId, CSSM_SERVICE_TYPE subserviceType)
{
Guid = guid;
SubserviceId = subserviceId;
SubserviceType = subserviceType;
if (version)
Version = *version;
else
Version.Major = Version.Minor = 0;
}
bool CssmSubserviceUid::operator == (const CSSM_SUBSERVICE_UID &otherUid) const
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-undefined-compare"
if (&otherUid == 0x0) { return false; }
#pragma clang diagnostic pop
const CssmSubserviceUid &other = CssmSubserviceUid::overlay(otherUid);
return subserviceId() == other.subserviceId()
&& subserviceType() == other.subserviceType()
&& guid() == other.guid();
}
bool CssmSubserviceUid::operator < (const CSSM_SUBSERVICE_UID &otherUid) const
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-undefined-compare"
if (&otherUid == 0x0) { return false; }
#pragma clang diagnostic pop
const CssmSubserviceUid &other = CssmSubserviceUid::overlay(otherUid);
if (subserviceId() < other.subserviceId())
return true;
if (subserviceId() > other.subserviceId())
return false;
if (subserviceType() < other.subserviceType())
return true;
if (subserviceType() > other.subserviceType())
return false;
return guid() < other.guid();
}
CryptoDataClass::~CryptoDataClass()
{ }
CSSM_RETURN CryptoDataClass::callbackShim(CSSM_DATA *output, void *ctx)
{
BEGIN_API
*output = reinterpret_cast<CryptoDataClass *>(ctx)->yield();
END_API(CSSM)
}