/*

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     :   PathRec.cpp
|  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.
\*------------------------------------------------------------------------*/

#ifdef VX /* Added by Masanori Satake. */
#include "vxWorks.h"
#include "vxdbg.h"
#endif

#include "PathRec.h"
#include "cmapi.h"
#include <string.h>
#include "utils.h"

bool CPathRec::operator==(const CPathRec& other) const
{ 

	char *rec2 = other.get_key();

	if ( CPLI_IsSameDN(rec2,m_lpszKey) )
	{
		return true; 
	}
	return false;
}
/*
bool CPathRec::operator==(const CPathRec *other)
{ 
	char *rec2 = other->get_key();

//	if ( strcmp(rec2,m_lpszKey) == 0 )
	{
		return true; 
	}
	return false;
}
*/
void CPathRec::CopyFrom( CPathRec *rec )
{
	set_entry(rec->get_entry_pointer(), rec->get_flag());
	m_entryNumber = rec->m_entryNumber;

//set_entry(const char *key, const char *data, const unsigned int datalen, const int val);
}

long CPathRec::set_entry(LPPATH_ENTRY_STRUCT lpPath_t, 
						 const unsigned long flag )
{

	int len = 0;

	if( NULL == lpPath_t )
	{
		return CPATHREC_REQUIRED_PARAM_MISSING;
	}
	if( NULL == lpPath_t->subjectIssuerDN || 
		NULL == lpPath_t->subjectSerial.data )
	{
		return CPATHREC_REQUIRED_PARAM_MISSING;
	}

	FreeEntry();

	/* Copy the whole struct (all data members) */

	/* Copy the subject serial number */
	m_entry.subjectSerial.data = new uchar[lpPath_t->subjectSerial.num + 2];
	if( NULL == m_entry.subjectSerial.data )
		return CPATHREC_ALLOC_FAIL;

	memcpy( m_entry.subjectSerial.data, lpPath_t->subjectSerial.data, lpPath_t->subjectSerial.num );
	m_entry.subjectSerial.num = lpPath_t->subjectSerial.num;

	/* Copy the subject issuer dn */
	m_entry.subjectIssuerDN = new char[strlen(lpPath_t->subjectIssuerDN) + 2];
	if( NULL == m_entry.subjectIssuerDN )
		return CPATHREC_ALLOC_FAIL;

	strcpy(m_entry.subjectIssuerDN,lpPath_t->subjectIssuerDN);

	/* Copy the subject issuer serial, if it exists */
	if( NULL != lpPath_t->issuerSerial.data )
	{
		m_entry.issuerSerial.data = new uchar[lpPath_t->issuerSerial.num + 1];
		if( NULL == m_entry.issuerSerial.data )
			return CPATHREC_ALLOC_FAIL;

		memcpy( m_entry.issuerSerial.data, lpPath_t->issuerSerial.data, lpPath_t->issuerSerial.num );
		m_entry.issuerSerial.num = lpPath_t->issuerSerial.num;
	}

	/* Copy the issuers CA DN, if it exists */
	if( NULL != lpPath_t->issuerCADN )
	{
		m_entry.issuerCADN = new char[strlen(lpPath_t->issuerCADN) + 2];
		if( NULL == m_entry.issuerCADN )
			return CPATHREC_ALLOC_FAIL;

		strcpy(m_entry.issuerCADN,lpPath_t->issuerCADN);
	}
	

	m_lpszKey = new char[strlen(m_entry.subjectIssuerDN) + (2 * m_entry.subjectSerial.num) + 1];	// Allocate for key value
	if( NULL == m_lpszKey )
		return CPATHREC_ALLOC_FAIL;

	strcpy(m_lpszKey,m_entry.subjectIssuerDN);
	CPLI_SquashSpacesInDN(m_lpszKey);			

	len = strlen(m_lpszKey);
	CPLI_btoa( (char*)m_entry.subjectSerial.data, &m_lpszKey[len], m_entry.subjectSerial.num );

	m_flag = flag;	/* Copy flag value */

	return 0;
}

long CPathRec::set_entry( const char* subjectIssuerDN, 
						 Bytes_struct* subjectSerial, 
						 const char* issuerCADN, 
						 Bytes_struct* issuerSerial, 
						 const unsigned long flag  )
{

	int len = 0;

	if( NULL == subjectIssuerDN || NULL == subjectSerial )
	{
		return CPATHREC_REQUIRED_PARAM_MISSING;
	}

	if( NULL == subjectSerial->data || 0 == subjectSerial->num )
	{
		return CPATHREC_SERIAL_NUMBER_MISSING;
	}

	FreeEntry();

	/* Copy the subject serial number */
	m_entry.subjectSerial.data = new uchar[subjectSerial->num + 2];
	if( NULL == m_entry.subjectSerial.data )
		return CPATHREC_ALLOC_FAIL;

	memcpy( m_entry.subjectSerial.data, subjectSerial->data, subjectSerial->num );
	m_entry.subjectSerial.num = subjectSerial->num;
	

	/* Copy the subject issuer dn */
	m_entry.subjectIssuerDN = new char[strlen(subjectIssuerDN) + 2];
	if( NULL == m_entry.subjectIssuerDN )
		return CPATHREC_ALLOC_FAIL;

	strcpy(m_entry.subjectIssuerDN,subjectIssuerDN);

	if( NULL != issuerSerial )
	{
		/* Copy the subject issuer serial, if it exists */
		if( NULL != issuerSerial->data )
		{
			m_entry.issuerSerial.data = new uchar[issuerSerial->num + 2];
			if( NULL == m_entry.issuerSerial.data )
				return CPATHREC_ALLOC_FAIL;
			memcpy( m_entry.issuerSerial.data, issuerSerial->data, issuerSerial->num );
			m_entry.issuerSerial.num = issuerSerial->num;
		}

		/* Copy the issuers CA DN, if it exists */
		if( NULL != issuerCADN )
		{
			m_entry.issuerCADN = new char[strlen(issuerCADN) + 2];
			if( NULL == m_entry.issuerCADN )
				return CPATHREC_ALLOC_FAIL;

			strcpy(m_entry.issuerCADN,issuerCADN);
		}
	}

	m_lpszKey = new char[strlen(m_entry.subjectIssuerDN) + (2 * m_entry.subjectSerial.num) + 1];	// Allocate for key value
	if( NULL == m_lpszKey )
		return CPATHREC_ALLOC_FAIL;

	strcpy(m_lpszKey,m_entry.subjectIssuerDN);
	CPLI_SquashSpacesInDN(m_lpszKey);			

	len = strlen(m_lpszKey);
	CPLI_btoa( (char*)m_entry.subjectSerial.data, &m_lpszKey[len], m_entry.subjectSerial.num );

	m_flag = flag;	/* Copy flag value */

	return 0;
}


CPathRec::CPathRec()
{
	m_lpszKey = NULL;
	NextRec = NULL;
	m_flag = 0;
	memset(&m_entry,0,sizeof(PATH_ENTRY_STRUCT));
	m_entryNumber = 0;

}

CPathRec::CPathRec( LPPATH_ENTRY_STRUCT lpPath_t, const unsigned long flag )
{

	m_lpszKey = NULL;
	NextRec = NULL;
	m_flag = 0;

	memset(&m_entry,0,sizeof(PATH_ENTRY_STRUCT));

	set_entry(lpPath_t,flag);

	m_flag = flag;
	NextRec = NULL;

	m_entryNumber = 0;

}

CPathRec::~CPathRec()
{

	FreeEntry();

	if ( NextRec != NULL )
	{
		delete NextRec;
		NextRec = NULL;
	}

}

void CPathRec::FreeEntry()
{
	if( NULL != m_entry.issuerCADN )
	{
		delete []m_entry.issuerCADN;
	}
	if( NULL != m_entry.subjectIssuerDN )
	{
		delete []m_entry.subjectIssuerDN;
	}
	if( NULL != m_entry.issuerSerial.data )
	{
		delete []m_entry.issuerSerial.data;
	}	
	if( NULL != m_entry.subjectSerial.data )
	{
		delete []m_entry.subjectSerial.data;
	}

	memset( &m_entry,0,sizeof(PATH_ENTRY_STRUCT) );

	if ( m_lpszKey != NULL )
	{	
		delete []m_lpszKey;
		m_lpszKey = NULL;
	}

}

#ifdef _DEBUG_VERBOSE
#undef _DEBUG_VERBOSE
#endif
