/*

The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.

The Original Code is a portion of the Path Development API developed
by CygnaCom Solutions, Inc.

Portions created by CygnaCom Solutions, Inc. are Copyright (C) CygnaCom 
Solutions, Inc. All Rights Reserved.

Contributor(s): ______________________________________.

http://www.cygnacom.com

*/
/*------------------------------------------------------------------------*\
|  Project Name  :   Path Development API
|  File Name     :   PathCache.h
|  Creation Date :   May 17, 1999
|  Author        :   Matt Cooper (mcooper@cygnacom.com)
|  Last Mode Date:   May 17, 1999
|  Modification History:
|--------------------------------------------------------------------------
|
|     Known Bugs:   None
|                   
|--------------------------------------------------------------------------
| Cryptographic devices and technical data regarding them are subject to
| Federal Government export controls as specified in Title 22, Code of
| Federal Regulations, Parts 121 through 128.  Cryptographic devices
| implementing the Data Encryption Standard (DES) and technical data
| regarding them must comply with these Federal regulations
|--------------------------------------------------------------------------
| Copyright (c) 1999, CygnaCom Solutions, Inc.
| All Rights Reserved.
\*------------------------------------------------------------------------*/

// PathCache.h: interface for the CPathCache class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PATHCACHE_H__1A847CF3_E134_11D2_B1A5_00207814A98B__INCLUDED_)
#define AFX_PATHCACHE_H__1A847CF3_E134_11D2_B1A5_00207814A98B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Cache.h"
#include "PathRec.h"
#include "utils.h"

/* Pick a prime for the table size (PATHCACHE_TABLE_SIZE)
      29      31      37      41      43      47      53      59      61 
      67      71      73      79      83      89      97     101     103 
     107     109     113     127     131     137     139     149     151 
     157     163     167     173     179     181     191     193     197 
     199     211     223     227     229     233     239     241     251 
     257     263     269     271     277     281     283     293     307 
     311     313     317     331     337     347     349     353     359 
     367     373     379     383     389     397     401     409     419 
     421     431     433     439     443     449     457     461     463 
     467     479     487     491     499     503     509     521     523 
     541     547     557     563     569     571     577     587     593 
     599     601     607     613     617     619     631     641     643 
     647     653     659     661     673     677     683     691     701 
     709     719     727     733     739     743     751     757     761 
     769     773     787     797     809     811     821     823     827 
     829     839     853     857     859     863     877     881     883 
     887     907     911     919     929     937     941     947     953 
     967     971     977     983     991     997    1009    1013    1019 
    1021    1031    1033    1039    1049    1051    1061    1063    1069 
    1087    1091    1093    1097    1103    1109    1117    1123    1129 
    1151    1153    1163    1171    1181    1187    1193    1201    1213 
    1217    1223    1229    1231    1237    1249    1259    1277    1279 
    1283    1289    1291    1297    1301    1303    1307    1319    1321 

The larger you expect the cache to (possibly) grow, the larger the number should be..
(The size of the table does not limit the cache size, 
rather a larger table improves lookup speed with larger data sets)
*/

#ifdef _DEBUG
//#define PATHCACHE_TABLE_SIZE		5
//#define PATHCACHE_MAX_ENTRY_COUNT	10
//#define PATHCACHE_MAX_ENTRY_FLUX	1
#define PATHCACHE_TABLE_SIZE		5237
#define PATHCACHE_MAX_ENTRY_COUNT	12231
#define PATHCACHE_MAX_ENTRY_FLUX	1000
#else
#define PATHCACHE_TABLE_SIZE		479
#define PATHCACHE_MAX_ENTRY_COUNT	1231
#define PATHCACHE_MAX_ENTRY_FLUX	100
#endif

#define PATHCACHE_FILE_NAME		"_cpd_db.bin"

#define PATHCACHE_ERRBASE				12000
#define PATHCACHE_NOT_INITIALIZED		PATHCACHE_ERRBASE + 1
#define PATHCACHE_INVALID_CERT_DATA		PATHCACHE_ERRBASE + 2
#define PATHCACHE_ALLOC_FAIL			PATHCACHE_ERRBASE + 3
#define PATHCACHE_DELETE_ERROR			PATHCACHE_ERRBASE + 4
#define PATHCACHE_INSERT_ERROR			PATHCACHE_ERRBASE + 5
#define PATHCACHE_OUTPUTFILE_FAIL		PATHCACHE_ERRBASE + 6
#define PATHCACHE_NO_SUCH_ENTRY			PATHCACHE_ERRBASE + 7
#define PATHCACHE_DEL_ERROR				PATHCACHE_ERRBASE + 8
#define PATHCACHE_REQUIRED_PARAM		PATHCACHE_ERRBASE + 9
#define PATHCACHE_PARAM_INVALID			PATHCACHE_ERRBASE + 10
#define PATHCACHE_BUFF_TOO_SHORT		PATHCACHE_ERRBASE + 11
#define PATHCACHE_INPUTFILE_FAIL		PATHCACHE_ERRBASE + 12
#define PATHCACHE_CACHEFILE_WRONG_OS	PATHCACHE_ERRBASE + 13
#define PATHCACHE_FILENOTFOUND			PATHCACHE_ERRBASE + 14
#define PATHCACHE_INCOMPATABLE_VER		PATHCACHE_ERRBASE + 15
#define PATHCACHE_FILE_CORRUPT			PATHCACHE_ERRBASE + 16
#define PATHCACHE_EXCEPTION				PATHCACHE_ERRBASE + 17
#define PATH_CACHE_NO_DISK_PATH			PATHCACHE_ERRBASE + 18



#define PATHCACHE_VERSION	0x1
#define PATHCACHE_HEADER_STR	"Certification Path Cache File Ver 0.1 -< DO NOT MODIFY >- "
#define PATHCACHE_HEADER_LENGTH		160
#define PATHCACHE_MAX_ENTRY_SIZE	1024
#define PATHCACHE_MAX_SERIAL		40

class CPathCache  
{
public:
	long DeleteEntry(Cert_struct *subjectCert);
	long DeleteEntry(const char *subjectIssuer,Bytes_struct *subjectSerial);
	long DeleteEntry( CPathRec &pathRec );
	long AddEntry(const char *subjectIssuer, 
						  Bytes_struct *subjectSerial, 
						  const char *issuerCA, 
						  Bytes_struct *issuerSerial, 
						  const unsigned long flag = 0, 
						  CPathRec** newRecord = NULL );
	long Serialize();
	char * m_diskPath;
	long Initialize( const char *diskPath );
	long AddEntry( Cert_struct *subject, Cert_struct *issuer, CPathRec** newRecord = NULL );
	CPathRec* FindEntry( CPathRec& pathRec );
	CPathRec* FindEntry( Cert_struct* subjectCert );
	CPathRec* FindEntry( const char* subjectIssuer, Bytes_struct* subjectSerial );

	long IsValid();
	CPathCache();
	virtual ~CPathCache();
protected:
	long MakeHeaderString( char *pStr, unsigned int strLength, unsigned int *headerLength );
	long ReadCacheFile();
	long MakeSerialString( CPathRec* pathRec, long vectorPos, char* pStr, unsigned int strLength, unsigned int *dataLen );
	long ReindexEntryNums();
	long m_lastEntryNum;
	long RemoveOldItems();
	CMemCache<CPathRec>* m_lpPathTable;
	long m_entryCount;
	CPathRec tmpPathRec;

};

#endif // !defined(AFX_PATHCACHE_H__1A847CF3_E134_11D2_B1A5_00207814A98B__INCLUDED_)
