CK_RV xGetSlotList()

in source/core_pkcs11.c [70:139]


CK_RV xGetSlotList( CK_SLOT_ID ** ppxSlotId,
                    CK_ULONG * pxSlotCount )
{
    CK_RV xResult = CKR_OK;
    CK_FUNCTION_LIST_PTR pxFunctionList = NULL;
    CK_SLOT_ID * pxSlotId = NULL;

    if( ( ppxSlotId == NULL ) || ( pxSlotCount == NULL ) )
    {
        xResult = CKR_ARGUMENTS_BAD;
    }
    else
    {
        xResult = C_GetFunctionList( &pxFunctionList );

        if( pxFunctionList == NULL )
        {
            xResult = CKR_FUNCTION_FAILED;
        }
        else if( pxFunctionList->C_GetSlotList == NULL )
        {
            xResult = CKR_FUNCTION_FAILED;
        }
        else
        {
            /* MISRA */
        }
    }

    if( xResult == CKR_OK )
    {
        xResult = pxFunctionList->C_GetSlotList( CK_TRUE, /* Token Present. */
                                                 NULL,    /* We just want to know how many slots there are. */
                                                 pxSlotCount );
    }

    if( xResult == CKR_OK )
    {
        if( *pxSlotCount == ( ( sizeof( CK_SLOT_ID ) * ( *pxSlotCount ) ) / ( sizeof( CK_SLOT_ID ) ) ) )
        {
            pxSlotId = pkcs11configPKCS11_MALLOC( sizeof( CK_SLOT_ID ) * ( *pxSlotCount ) );

            if( pxSlotId == NULL )
            {
                xResult = CKR_HOST_MEMORY;
            }
            else
            {
                *ppxSlotId = pxSlotId;
            }
        }
        else
        {
            xResult = CKR_HOST_MEMORY;
        }
    }

    if( xResult == CKR_OK )
    {
        xResult = pxFunctionList->C_GetSlotList( CK_TRUE, pxSlotId, pxSlotCount );
    }

    if( ( xResult != CKR_OK ) && ( pxSlotId != NULL ) )
    {
        pkcs11configPKCS11_FREE( pxSlotId );
        *ppxSlotId = NULL;
    }

    return xResult;
}