Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / C

Domain authentication based on-the-fly encryption/decryption system for USB storage devices

Rate me:
Please Sign up or sign in to vote.
4.22/5 (7 votes)
27 Feb 2008CPOL8 min read 49.3K   2.5K   50  
Encrypts/Decrypts files on a USB within a domain,on the fly.

#include "stdafx.h"
#include "LogTrace.h"

#define __ESCAPE__CHAR _T("\\\\?\\");
#define _ERROR_CODE _T("Error Code : ")

// default constructor
CLogTrace::CLogTrace()
{
	this->fName = _T("Log.txt");
	this->Log   = _T("");
	this->logFP = NULL;
	return;
}

//default destructor
CLogTrace::~CLogTrace()
{
	if(this->Log != NULL)
	{
		free(this->Log);
		this->Log = NULL;
	}

}

//Set File Name
BOOL CLogTrace::SetFileName(LPCTSTR fName)
{
	this->fName = _T("");
	wcscpy(this->fName,fName);
	return TRUE;
}

BOOL CLogTrace::Init()
{
	WCHAR lpFileName[MAX_PATH];
	WCHAR wszFilePath[MAX_PATH];
	WCHAR	wszDrive[_MAX_DRIVE];
	WCHAR	wszDir[_MAX_DIR];



	// Making of full path of control process
	if (!::GetModuleFileNameW(NULL, lpFileName, MAX_PATH)) 
	{
		DWORD dwErr = ::GetLastError();
		return dwErr;
	}

	//Log file path getting generated in the directory of exe
	_wsplitpath_s( lpFileName, wszDrive, _MAX_DRIVE, wszDir, _MAX_DIR, NULL, 0, NULL, 0);
	_wmakepath_s( wszFilePath, _MAX_PATH, wszDrive, wszDir, this->fName, NULL );

	//Open the file in read-write mode
	FILE * logFP =_wfopen(wszFilePath,L"a");
	if(logFP == NULL)
	{
		return FALSE;
	}

}

//Log Simple Text
BOOL CLogTrace::LOG_DBG(LPCTSTR log)
{
	//Start

	if(log != NULL)
	{
		if(!WriteLog())
		{
			return FALSE;
		}
		return TRUE;
	}

	return FALSE;
	//End
}

//Log Text with specific string
BOOL CLogTrace::LOG_DBG(LPCTSTR log, LPCTSTR str)
{
	//Start

	if((log != NULL) && (str != NULL))
	{
		if(!CLogTrace::Format(log,str))
		{
			return FALSE;
		}

		if(!WriteLog())
		{
			return FALSE;
		}
		return TRUE;
	}
	if(this->Log != NULL)
	{
		free(this->Log);
		this->Log = NULL;
	}

	return FALSE;
	//End
}

//Log Text with error code
BOOL CLogTrace::LOG_ERR(LPCTSTR log, DWORD errCode)
{
	//Start

	if(log != NULL)
	{
		if(!CLogTrace::Format(log,errCode))
		{
			return FALSE;
		}

		if(!WriteLog())
		{
			return FALSE;
		}
	}

	if(this->Log != NULL)
	{
		free(this->Log);
		this->Log = NULL;
	}

	return TRUE;

	//End
}

//Format Text strings
BOOL CLogTrace::Format(LPCTSTR log, LPCTSTR str)
{
	//Start
	LPTSTR lpDateTime;
	DWORD totalBufSize = 0;
	
	if(!this->GetDateTime(lpDateTime))
	{
		return FALSE;
	}

	
	if((log != NULL) && (str != NULL))
	{
		totalBufSize = wcslen(log)+wcslen(str)+wcslen(lpDateTime)+10;
		this->Log = (wchar_t*)malloc((totalBufSize+1)*sizeof(wchar_t));
		if(this->Log != NULL)
		{
			_swprintf(this->Log,L"%S\t%S\t%S\r\n",lpDateTime,log,str);
			if(lpDateTime != NULL)
			{
				free(lpDateTime);
				lpDateTime = NULL;
			}

		}
		else{
			return FALSE;
		}
	}
	else{
		return FALSE;
	}

	if(lpDateTime != NULL)
	{
		free(lpDateTime);
		lpDateTime = NULL;
	}

	return TRUE;
	//End
}


//Format String with Error Code
BOOL CLogTrace::Format(LPCTSTR log, DWORD errCode)
{
	//Start

	LPTSTR lpDateTime;
	DWORD totalBufSize = 0;
	
	if(!this->GetDateTime(lpDateTime))
	{
		return FALSE;
	}

	if(log != NULL)
	{
		totalBufSize = wcslen(log)+wcslen(lpDateTime)+wcslen(_ERROR_CODE)+10;
		this->Log = (wchar_t*)malloc((totalBufSize+1)*sizeof(wchar_t));
		if(this->Log != NULL)
		{
			_swprintf(this->Log,L"%S\t%S\t%S%d\r\n",lpDateTime,log,_ERROR_CODE,errCode);
			if(lpDateTime != NULL)
			{
				free(lpDateTime);
				lpDateTime = NULL;
			}
		}
		else{
			return FALSE;
		}

	}

	if(lpDateTime != NULL)
	{
		free(lpDateTime);
		lpDateTime = NULL;
	}

	return TRUE;

	//End
}

//Write Log to the file
BOOL CLogTrace::WriteLog()
{
	//Start
	fputws(this->Log,this->logFP);
	if(this->Log != NULL)
	{
		free(this->Log);
		this->Log = NULL;
	}

	return TRUE;
	//End
}
BOOL CLogTrace::GetDateTime(LPTSTR lpDateTime)
{
	//Start
	LPTSTR lpSysTime = NULL;
	LPTSTR lpSysDate = NULL;
	LPTSTR lpLocalDateTime = NULL;
	UINT bufSize = 0;
	UINT totalBufSize = 0;//Total length of bufsize
	DWORD retVal = 0;

	//Get Formatted Time value
	::GetTimeFormat(NULL,TIME_FORCE24HOURFORMAT,NULL,L"hh':'mm':'ss tt",lpSysTime,bufSize);
	if(bufSize != 0)
	{
		lpSysTime = (wchar_t*)malloc((bufSize+1)*sizeof(wchar_t));
	}
	if(lpSysTime == NULL)
	{
		return FALSE;
	}
	else
	{
		::GetTimeFormat(NULL,TIME_FORCE24HOURFORMAT,NULL,L"hh':'mm':'ss tt",lpSysTime,bufSize);
	}
	totalBufSize += bufSize;

	//Get Formatted Date Value
	bufSize = 0;
	::GetDateFormat(NULL,DATE_LONGDATE|DATE_YEARMONTH,NULL,L"dd':'mm':'yyyy",lpSysDate,bufSize);
	if(bufSize != 0)
	{
		lpSysDate = (wchar_t*)malloc((bufSize+1)*sizeof(wchar_t));
	}
	if(lpSysDate == NULL)
	{
		return FALSE;
	}
	else
	{
		::GetDateFormat(NULL,DATE_LONGDATE|DATE_YEARMONTH,NULL,L"dd':'mm':'yyyy",lpSysDate,bufSize);
	}

	totalBufSize += bufSize;

	//Consolidate the formatted time and date
	lpLocalDateTime = (wchar_t*)malloc((totalBufSize+1)*sizeof(wchar_t));
	if(lpLocalDateTime != NULL)
	{
		wcscpy(lpLocalDateTime,lpSysDate);
		wcscpy(lpLocalDateTime,L" : ");
		wcscpy(lpLocalDateTime,lpSysTime);
	}
	else{
		return FALSE;
	}

	lpDateTime = lpLocalDateTime;
	
	// Free Memory
	if(lpSysTime != NULL)
	{
		free(lpSysTime);
		lpSysTime = NULL;
	}
	if(lpSysDate != NULL)
	{
		free(lpSysDate);
		lpSysDate = NULL;
	}
	return TRUE;

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) NEC HCL System Technologies Ltd, India
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions