//
// Construct an Attachment object.
// This constructor does almost all the work: it initializes the Attachment
// object, calls the plugin's attach function, and initializes everything.
// The only job left for the subclass's constructor is to take the spiFunctionTable
// field and extract from it the plugin's dispatch table in suitable form.
//
Attachment::Attachment(Module *parent,
                       const CSSM_VERSION &version,
                       uint32 ssId,
                       CSSM_SERVICE_TYPE ssType,
                       const CSSM_API_MEMORY_FUNCS &memoryOps,
                       CSSM_ATTACH_FLAGS attachFlags,
                       CSSM_KEY_HIERARCHY keyHierarchy)
	: CssmMemoryFunctionsAllocator(memoryOps), module(*parent)
{
    // record our origins
    mVersion = version;
    mSubserviceId = ssId;
    mSubserviceType = ssType;
    mAttachFlags = attachFlags;
    mKeyHierarchy = keyHierarchy;

    // we are not (yet) attached to our plugin
    mIsActive = false;
    
    // build the upcalls table
    // (we could do this once in a static, but then we'd have to lock on it)
    upcalls.malloc_func = upcallMalloc;
    upcalls.free_func = upcallFree;
    upcalls.realloc_func = upcallRealloc;
    upcalls.calloc_func = upcallCalloc;
    upcalls.CcToHandle_func = upcallCcToHandle;
    upcalls.GetModuleInfo_func = upcallGetModuleInfo;

    // tell the module to create an attachment
    spiFunctionTable = NULL;	// preset invalid
    if (CSSM_RETURN err = module.plugin->attach(&module.myGuid(),
            &mVersion,
            mSubserviceId,
            mSubserviceType,
            mAttachFlags,
            handle(),
            mKeyHierarchy,
            &gGuidCssm,			// CSSM's Guid
            &gGuidCssm,			// module manager Guid
            &module.cssm.callerGuid(), // caller Guid
            &upcalls,
            &spiFunctionTable)) {
        // attach rejected by module
		secdebug("cssm", "attach of module %p(%s) failed",
			&module, module.name().c_str());
        CssmError::throwMe(err);
    }
    try {
        if (spiFunctionTable == NULL || spiFunctionTable->ServiceType != subserviceType())
            CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_FUNCTION_TABLE);
        mIsActive = true;	// now officially attached to plugin
		secdebug("cssm", "%p attached module %p(%s) (ssid %ld type %ld)",
			this, parent, parent->name().c_str(), (long)ssId, (long)ssType);
        // subclass is responsible for taking spiFunctionTable and build
        // whatever dispatch is needed
    } catch (...) {
        module.plugin->detach(handle());	// with extreme prejudice
        throw;
    }
}
bool CssmSubserviceUid::operator < (const CSSM_SUBSERVICE_UID &otherUid) const
{
    if (&otherUid == 0x0)
    {
        return false;
    }
    
	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();
}
bool CssmSubserviceUid::operator == (const CSSM_SUBSERVICE_UID &otherUid) const
{
    // make sure we don't crash if we get bad data
    if (&otherUid == 0x0)
    {
        return false;
    }
    
	const CssmSubserviceUid &other = CssmSubserviceUid::overlay(otherUid);
	return subserviceId() == other.subserviceId()
		&& subserviceType() == other.subserviceType()
		&& guid() == other.guid();
}