securityfilter.cpp

来自「c++系统开发实例精粹内附的80例源代码 环境:windows2000,c++」· C++ 代码 · 共 400 行

CPP
400 行
字号
//////////////////////////////////////////////////////////////////////
// FileFury
// Copyright (c) 2000 Tenebril Incorporated
// All rights reserved.
//
// This source code is governed by the Tenebril open source
// license (http://www.tenebril.com/developers/opensource/license.html)
//
// For more information on this and other open source applications,
// visit the Tenebril OpenSource page:
//       http://www.tenebril.com/developers/opensource
//
//////////////////////////////////////////////////////////////////////

// SecurityFilter.cpp: implementation of the CSecurityFilter class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Oscar.h"
#include "SecurityFilter.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSecurityFilter::CSecurityFilter()
{
	m_nReferences = 1;
}

CSecurityFilter::~CSecurityFilter()
{

}

BOOL CSecurityFilter::AddDirectory(LPCTSTR czDir)
{
	m_cMutex.Lock();

	CString cszDir = czDir;

	// Format the directory name.
	if(cszDir.GetAt(cszDir.GetLength() - 1) != _T('\\'))
		cszDir += CString(_T('\\'));

	// Make sure it's not already shared.
	if(IsShared(cszDir))
	{
		m_cMutex.Unlock();
		return FALSE;
	}

	// Make sure it's legally shareable.
	if(!CanShare(cszDir))
	{
		m_cMutex.Unlock();
		return FALSE;
	}

	// We're ready to add; delete all shared sub-directories.
	int nSize = m_csDirectories.GetSize();

	for(int i = 0; i < nSize; i++)
	{
		CString cszTemp = m_csDirectories.GetAt(i);

		if(IsSubDir(cszDir, cszTemp))
		{
			// Delete this entry.
			m_csDirectories.RemoveAt(i);

			// Update indices.
			i -= 1;
			nSize -= 1;
		}
	}

	// Now add the new directory.
	int nIndex = m_csDirectories.Add(cszDir);

	m_cMutex.Unlock();

	return (nIndex >= 0);
}

BOOL CSecurityFilter::RemoveDirectory(LPCTSTR czDir)
{
	m_cMutex.Lock();

	CString cszDir = czDir;

	// Format the directory name.
	if(cszDir.GetAt(cszDir.GetLength() - 1) != _T('\\'))
		cszDir += CString(_T('\\'));

	int nIndex = FindDirectory(cszDir);
	if(nIndex < 0) { m_cMutex.Unlock(); return FALSE; }

	m_csDirectories.RemoveAt(nIndex);
	m_cMutex.Unlock();
	return TRUE;
}

BOOL CSecurityFilter::IsShared(LPCTSTR czDir)
{
	m_cMutex.Lock();
	int nSize = m_csDirectories.GetSize();

	CString cszDir = czDir;

	if(cszDir.GetLength() == 0)
	{
		m_cMutex.Unlock();
		return FALSE;
	}

	// Format the name.
	if(cszDir.GetAt(cszDir.GetLength() - 1) != _T('\\'))
		cszDir += CString(_T('\\'));

	for(int i = 0; i < nSize; i++)
	{
		CString cszTemp = m_csDirectories.GetAt(i);

		if(IsSubDir(cszTemp, cszDir))
		{
			m_cMutex.Unlock();
			return TRUE;
		}
	}

	m_cMutex.Unlock();
	return FALSE;
}

BOOL CSecurityFilter::IsSubDir(LPCTSTR czMainDir, LPCTSTR czSubDir)
{
	m_cMutex.Lock();

	CString cszMainDir = czMainDir;
	CString cszSubDir = czSubDir;

	cszSubDir = cszSubDir.Left(cszMainDir.GetLength());

	m_cMutex.Unlock();
	return (cszMainDir.CompareNoCase(cszSubDir) == 0);
}

int CSecurityFilter::FindDirectory(LPCTSTR czDir)
{
	m_cMutex.Lock();

	int nSize = m_csDirectories.GetSize();

	for(int i = 0; i < nSize; i++)
	{
		CString cszTemp = m_csDirectories.GetAt(i);

		if(cszTemp.CompareNoCase(czDir) == 0)
		{
			m_cMutex.Unlock();
			return i;
		}
	}

	m_cMutex.Unlock();
	return -1;
}

UINT CSecurityFilter::GetSharedDirectories(LPCTSTR czDrive,
										   CStringArray &csArray)
{
	m_cMutex.Lock();

	int nSize = m_csDirectories.GetSize();
	CString cszDrive = czDrive;
	UINT nReturned = 0;

	// Clear out the return array.
	csArray.RemoveAll();

	for(int i = 0; i < nSize; i++)
	{
		CString cszTemp = m_csDirectories.GetAt(i);

		if(IsSubDir(czDrive, cszTemp))
		{
			csArray.Add(cszTemp);
			nReturned += 1;
		}
	}

	m_cMutex.Unlock();
	return nReturned;
}

char *CSecurityFilter::ToBuffer(UINT &nLen)
{
	m_cMutex.Lock();

	int nSize, nPos;
	nSize = 1024, nPos = 0;
	char *cBuf = new char[nSize];

	// First, push over the number of strings.
	int nEntries = m_csDirectories.GetSize();
	memcpy((void *)(&(cBuf[nPos])), (void *)&nEntries, sizeof(int));
	nPos += sizeof(int);

	// Now, push over each string.
	for(int i = 0; i < nEntries; i++)
	{
		CString cszName = m_csDirectories.GetAt(i);
		int nLenString = (cszName.GetLength() + 1) * sizeof(TCHAR);
		int nNeededSpace = sizeof(int) + nLenString;

		// Grow the memory buffer to handle the next string.
		while(nNeededSpace + nPos >= nSize)
		{
			nSize += 1024;
			char *cNewBuf = new char[nSize];
			memcpy((void *)cNewBuf, (void *)cBuf, nSize - 1024);
			delete cBuf;
			cBuf = cNewBuf;
		}

		// Push the string length in.
		memcpy((void *)(&(cBuf[nPos])), (void *)&nLenString, sizeof(int));
		nPos += sizeof(int);

		// Push the string itself in.
		memcpy((void *)(&(cBuf[nPos])), (void *)cszName.GetBuffer(0),
			nLenString * sizeof(TCHAR));
		nPos += nLenString * sizeof(TCHAR);
	}

	// Set the total length.
	nLen = nPos;
	// and return the array.
	m_cMutex.Unlock();
	return cBuf;
}

int CSecurityFilter::FromBuffer(char *cBuf)
{
	ASSERT(cBuf != NULL);

	if(!cBuf) return -1;

	m_cMutex.Lock();

	int nStrings, nPos = 0;

	// Clear the old array.
	m_csDirectories.RemoveAll();

	// First, get the number of strings.
	memcpy((void *)&nStrings, (void *)(&(cBuf[nPos])), sizeof(int));
	nPos += sizeof(int);

	// Now, push in each string.
	for(int i = 0; i < nStrings; i++)
	{
		// Get the string length.
		int nLen;
		memcpy((void *)&nLen, (void *)(&(cBuf[nPos])), sizeof(int));
		nPos += sizeof(int);

		// Pull over the string.  We assume an end-of-string character.
		CString cszString = (LPCTSTR)(&(cBuf[nPos]));

		// Add the string to the array.
		m_csDirectories.Add(cszString);

		// Advance the array.
		nPos += nLen;
	}

	m_cMutex.Unlock();
	return nStrings;
}

// A little Python trick
void CSecurityFilter::Reference()
{
	m_nReferences += 1;
}

void CSecurityFilter::Dereference()
{
	m_nReferences -= 1;

	if(m_nReferences == 0)
		delete this;
}

BOOL CSecurityFilter::CanShare(LPCTSTR czPath)
{
	m_cMutex.Lock();

	TCHAR PathBuffer[MAX_PATH * 2 + 1];
	UINT nAns;

	// Don't allow the windows directories to be shared.
	nAns = GetWindowsDirectory( PathBuffer, MAX_PATH * 2);
	ASSERT(nAns < MAX_PATH * 2 && nAns > 0);
	if(IsSubDir(PathBuffer, czPath))
	{
		m_cMutex.Unlock();
		return FALSE;
	}

	// Don't allow roots to be shared.
	CString cszMPath = czPath;
	if(cszMPath.GetLength() == 0) return FALSE;
	if(cszMPath.GetAt(cszMPath.GetLength() - 1) == _T('\\'))
		cszMPath = cszMPath.Left(cszMPath.GetLength() - 1);
	if(cszMPath.GetLength() == 0) return FALSE;
	if(cszMPath.GetAt(cszMPath.GetLength() - 1) == _T(':'))
		return FALSE;

	// TODO: Add any other protected directories here.

	// Okay.
	m_cMutex.Unlock();
	return TRUE;
}

BOOL CSecurityFilter::IsDirectlyShared(LPCTSTR czDir)
{
	m_cMutex.Lock();
	int nSize = m_csDirectories.GetSize();

	CString cszDir = czDir;

	if(cszDir.GetLength() == 0)
	{
		m_cMutex.Unlock();
		return FALSE;
	}

	// Format the name.
	if(cszDir.GetAt(cszDir.GetLength() - 1) != _T('\\'))
		cszDir += CString(_T('\\'));

	for(int i = 0; i < nSize; i++)
	{
		CString cszTemp = m_csDirectories.GetAt(i);

		if(cszTemp.CompareNoCase(cszDir) == 0)
		{
			m_cMutex.Unlock();
			return TRUE;
		}
	}

	m_cMutex.Unlock();
	return FALSE;
}

BOOL CSecurityFilter::GetDirectory(int nID, CString &cszDir)
{
	int nSize = m_csDirectories.GetSize();

	// Make sure we don't over- or under-run the buffer.
	if(nID >= nSize || nID < 0)
		return FALSE;

	// Copy the directory into the string.
	cszDir = m_csDirectories.GetAt(nID);
	return TRUE;
}

BOOL CSecurityFilter::IsDriveShared(LPCTSTR lpDrive)
{
	CString cszDir;
	CString cszDrive = lpDrive;

	ASSERT(cszDrive.GetLength() >= 1);
	if(cszDrive.GetLength() < 1)
		return FALSE;

	for(int i = 0; GetDirectory(i, cszDir); i++)
	{
		cszDir = cszDir.Left(cszDrive.GetLength());

		if(cszDir.CompareNoCase(cszDrive) == 0)
			return TRUE;
	}

	return FALSE;
}

⌨️ 快捷键说明

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