⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.cpp

📁 PKCS#11的微软CSP实现源码
💻 CPP
字号:
/****************************************************************************
* library : pkcs_csp.dll
* Purpose : It is a cryptographic service provider which is an independent 
* software module that actually performs cryptography algorithms for 
* authentication, encoding, and encryption.
* This DLL can be interfaced on any PKCS#11 module.  
*
* Copyright (C) 2003 Ilex Syst鑝es Informatiques
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact :
* Ilex 
* 51 boulevard Voltaire
* 92600 Asni鑢es-sur-Seine
* pkizy@ilex.fr
*
* Author: Delouvrier Antoine
*
*******************************************************************************/

/*
%----------------------------------------------------------------------------
% PROJECT : CSP_PKCS
%
% MODULE : TableOfHandle
%
% VERSION : 1.00
%
% FILE : table.cpp
%
% TableOfHandle: class allows to manage a table of handle
%----------------------------------------------------------------------------
% Version 1.00
% 
% CPX-31/03/2003-Creation
%----------------------------------------------------------------------------
*/

/*
% Libraries ANSI or system
%------------------------------
*/
#include <windows.h>

/*
% HEADER Files include
%-----------------------
*/
#include "table.h"



/*
%--------------------------------------------------------------------------
% TableOfHandle()
%
% TableOfHandle() constructor
%
%---------------------------------------------------------------------------
*/
TableOfHandle::TableOfHandle()
{
	//Initialize the table
	entryTableUsed = 0;
	InitializeCriticalSection(&critical_object);
	pTable = new void*[INITIAL_SIZE];
	if(!pTable)
	{
		tableSize = 0;
		return;
	}
	memset(pTable,NO_ENTRY,INITIAL_SIZE*sizeof(void*));
	tableSize = INITIAL_SIZE;
}


/*
%--------------------------------------------------------------------------
% ~TableOfHandle()
%
% R鬺e : ~TableOfHandle() destructor 
%
*/
TableOfHandle::~TableOfHandle()
{
	DeleteCriticalSection(&critical_object);
	if(pTable)
		delete[] pTable;
}


/*
%--------------------------------------------------------------------------
% AddEntry
%
% Adds a new handle to the table.
%
% Parameters of entry  :
%						IN const void * const IN pHandle: The handle to add
%  
% return :	bool : true if it was successful, false otherwise.
%---------------------------------------------------------------------------
*/
bool TableOfHandle::AddEntry(void * const IN pEntry)
{
	EnterCriticalSection(&critical_object);

	if(pEntry==(void*)DELETED_ENTRY || pEntry==(void*)NO_ENTRY  )
		return false;

	// We must extend the size of the table
	if(entryTableUsed >= tableSize)
	{
		if(!ExtendSize())
		{
			LeaveCriticalSection(&critical_object);
			return false;
		}
	}
	// is entry already exist
	if(VerifyEntry(pEntry))
	{
		LeaveCriticalSection(&critical_object);
		return true;
	}
	
	int i = GetIndex(pEntry);

	for(; pTable[i] != (void*)NO_ENTRY && pTable[i] != (void*)DELETED_ENTRY;i = (i + 1) % tableSize);
		pTable[i] = pEntry;

	++entryTableUsed;
	LeaveCriticalSection(&critical_object);
	return true;
}

/*
%--------------------------------------------------------------------------
% RemoveEntry
%
% Removes a handle from the table.
%
% Parameters of entry  :
%						IN const void * const IN pHandle: The handle to be removed
%  
% return :	const void * const IN pHandle: The handle to be removed
%---------------------------------------------------------------------------
*/
void TableOfHandle::RemoveEntry(const void * const IN pEntry)
{
	EnterCriticalSection(&critical_object);

	if(pEntry==(void*)DELETED_ENTRY || pEntry==(void*)NO_ENTRY  )
		return;

	int i = GetIndex(pEntry);
	int itmp = i;
	do
	{
		if(pTable[i] == pEntry)
		{
			pTable[i] = (void*)DELETED_ENTRY;
	        --entryTableUsed;
			break;
		}
		i = (i + 1)%tableSize;
	}while(i != itmp && pTable[i] != (void*)NO_ENTRY);

	LeaveCriticalSection(&critical_object);
}


/*
%--------------------------------------------------------------------------
% GetNext
%
% Gets the next item out of the handle table
%
% Parameters of entry  :
%						int& i: Where to start looking for the next item.
%					
%  
% return :	void* : A ptr to the item, if there is one, NULL otherwise
%---------------------------------------------------------------------------
*/
void* TableOfHandle::GetNext(int& i)
{
	EnterCriticalSection(&critical_object);

	if(i < 0)
		return NULL;

	while(i < tableSize)
	{
		void* pEntry = pTable[i++];
		if(pEntry != (void*)DELETED_ENTRY && pEntry != (void*)NO_ENTRY)
		{
			LeaveCriticalSection(&critical_object);
			return pEntry;
		}
	}

	LeaveCriticalSection(&critical_object);
	return NULL;
}


/*
%--------------------------------------------------------------------------
% VerifyEntry()
%
% Verify the entry specified
%
% Parameters of entry  :
%						IN const void * const IN pHandle: The handle to be checked for validity
%  
% return :	bool : true if checked, false otherwise.
%---------------------------------------------------------------------------
*/
bool TableOfHandle::VerifyEntry(const void * const IN pEntry)
{
	if(0 == entryTableUsed)
		return false;

	EnterCriticalSection(&critical_object);

	if(pEntry == (void*)DELETED_ENTRY|| pEntry == (void*)NO_ENTRY)
	{
		LeaveCriticalSection(&critical_object);
		return false;
	}

	int i = GetIndex(pEntry);
	int itmp = i;
	bool result = false;

	do
	{
		if(pTable[i] == pEntry)
			result = true;
		i = (i + 1)%tableSize;
	}while(i != itmp && pTable[i] != (void*)NO_ENTRY);

	LeaveCriticalSection(&critical_object);
	return result;
}


/****************************************************************************/
/*																			*/
/*							PRIVATE											*/
/*																			*/
/****************************************************************************/


/*
%--------------------------------------------------------------------------
% GetIndex()
%
% Get the index of the handle item in the hashtable.
%
% Parameters of entry  :
%						IN void * const IN pHandle: The handle to find the index of.
%  
% return  :	int : the initial index of the item in the hashtable.
%---------------------------------------------------------------------------
*/
int TableOfHandle::GetIndex(const void * const IN pEntry)
{
	return ((int)pEntry >> 2) % tableSize;
}


/*
%--------------------------------------------------------------------------
% ExtendSize
%
%   Extend the size of the table 
%  
% return :	bool : Returns true if the table could be extended.
%---------------------------------------------------------------------------
*/
bool TableOfHandle::ExtendSize()
{
	int oldTableSize = tableSize;
	tableSize = (int)(tableSize*EXTEND_FACTOR);
	void** pTmpHandleTable = new void*[tableSize];
	if(!pTmpHandleTable)
	{
		tableSize = oldTableSize;
		return false;
	}
	entryTableUsed = 0;
	memset(pTmpHandleTable, NO_ENTRY, tableSize*sizeof(void*));
	void** pOldHandleTable = pTable;
	pTable = pTmpHandleTable;
	for(int i = 0; i< oldTableSize; ++i)
		AddEntry(pOldHandleTable[i]);
	delete [] pOldHandleTable;
	return true;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -