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

📄 base64ex.cpp

📁 Base64编码算法, 可以分别对字符串和文件进行编码/解码,带进度条, 内含VC++6.0源码
💻 CPP
字号:

/*
	Copyright 2006 - 2008
	ZhangLuduo <zhangluduo@msn.com>
	All Rights Reserved.
							
	Base64 编码及解码

	作者	- 张鲁夺(zhangluduo)
	MSN		- zhangluduo@msn.com
	QQ群	- 34064264

	为所有爱我的人和我爱的人努力!
*/

#include "stdafx.h"
#include "Base64Ex.h"

void Base64Ex::OnEncodProcessing(int nProgress, MemberFxn addr)
{
	void*			pThis	= addr.GetThis();
	unsigned long	Addr	= addr.GetAddr();

	/** the Calling Convention must be __stdcall or 
		thiscall (thiscall not a keyword),
		thiscall is the default calling convention used by C++ member functions 
	*/
	__asm
	{
		push	nProgress	;
	 	mov		ecx, pThis	;
		call	Addr		;
	}
}

void Base64Ex::OnDecodProcessing(int nProgress, MemberFxn addr)
{
	void*			pThis	= addr.GetThis();
	unsigned long	Addr	= addr.GetAddr();

	/** the Calling Convention must be __stdcall or 
		thiscall (thiscall not a keyword),
		thiscall is the default calling convention used by C++ member functions 
	*/
	__asm
	{
		push	nProgress	;
	 	mov		ecx, pThis	;
		call	Addr		;
	}
}

unsigned long Base64Ex::GetFileSize(const char* FileName)
{
	HANDLE hFile = CreateFile(FileName, NULL, FILE_SHARE_READ, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
	if(hFile == NULL)
	{
		CloseHandle(hFile);
		return -1;
	}
	unsigned long nFileSize = ::GetFileSize(hFile, NULL);
	CloseHandle(hFile);
	return nFileSize;
}

bool Base64Ex::FileEncode(const char* inFileName, const char* outFileName, MemberFxn addr)
{
	FILE *inFile			= 0;
	FILE *outFile			= 0;
	int len					= 0;
	int ReadCount			= 0;
	m_bEncodeTerminate		= false;
	string strTmp			= "";

	unsigned long nFileSize	= this->GetFileSize(inFileName);
	if(nFileSize == -1)
	{
		m_strEncodeErr = "Get file size failed!";
		return false;
	}
	unsigned char *buffer	= new unsigned char[ENCODE_BUFFER_SIZE + 1];
	memset(buffer, 0, ENCODE_BUFFER_SIZE + 1);

	try
	{
		if ((inFile = fopen (inFileName, "rb")) == NULL)
		{
			m_strEncodeErr = "Open in put file failed!";
			goto ERR;
		}

		if ((outFile = fopen (outFileName, "wb")) == NULL)
		{
			m_strEncodeErr = "Open out put file failed!";
			goto ERR;
		}

		if(!addr.IsNull())
			OnEncodProcessing(0, addr);

		while (len = fread (buffer, 1, ENCODE_BUFFER_SIZE, inFile))
		{
			ReadCount++;

			if(m_bEncodeTerminate)
			{
				m_strEncodeErr = "User terminate calculate!";
				goto ERR;
			}

			strTmp = Encode(buffer, len);
			fwrite(strTmp.c_str(), 1, strTmp.length(), outFile);

			if(!addr.IsNull())
				OnEncodProcessing((int)(ENCODE_BUFFER_SIZE * ReadCount / (nFileSize / 100)), addr);

			memset(buffer, 0, ENCODE_BUFFER_SIZE + 1);
		}

		if(!addr.IsNull())
			OnEncodProcessing(100, addr);

		if(inFile != 0)
			fclose (inFile);

		if(outFile != 0)
			fclose (outFile);

		delete[] buffer;
	}
	catch(...)
	{
		goto ERR;
	}

	return true;

ERR:

	if(inFile != 0)
		fclose (inFile);

	if(outFile != 0)
		fclose (outFile);
	delete[] buffer;

	return false;
}

bool Base64Ex::FileDecode(const char* inFileName, const char* outFileName, MemberFxn addr)
{
	FILE *inFile			= 0;
	FILE *outFile			= 0;
	int len					= 0;
	int ReadCount			= 0;
	m_bDecodeTerminate		= false;
	string strTmp			= "";
	int	nByte				= 0;

	unsigned long nFileSize	= this->GetFileSize(inFileName);
	if(nFileSize == -1)
	{
		m_strDecodeErr = "Get file size failed!";
		return false;
	}
	unsigned char *buffer	= new unsigned char[DECODE_BUFFER_SIZE + 1];
	memset(buffer, 0, DECODE_BUFFER_SIZE + 1);

	try
	{
		if ((inFile = fopen (inFileName, "rb")) == NULL)
		{
			m_strDecodeErr = "Open in put file failed!";
			goto ERR;
		}

		if ((outFile = fopen (outFileName, "wb")) == NULL)
		{
			m_strDecodeErr = "Open out put file failed!";
			goto ERR;
		}

		if(!addr.IsNull())
			OnDecodProcessing(0, addr);

		while (len = fread (buffer, 1, DECODE_BUFFER_SIZE, inFile))
		{
			ReadCount++;

			if(m_bDecodeTerminate)
			{
				m_strDecodeErr = "User terminate calculate!";
				goto ERR;
			}

			strTmp = Decode(buffer, len, &nByte);
			fwrite(strTmp.c_str(), 1, nByte, outFile);

			if(!addr.IsNull())
				OnDecodProcessing((int)(DECODE_BUFFER_SIZE * ReadCount / (nFileSize / 100)), addr);

			memset(buffer, 0, DECODE_BUFFER_SIZE + 1);
		}

		if(!addr.IsNull())
			OnDecodProcessing(100, addr);

		if(inFile != 0)
			fclose (inFile);

		if(outFile != 0)
			fclose (outFile);

		delete[] buffer;
	}
	catch(...)
	{
		goto ERR;
	}

	return true;

ERR:

	if(inFile != 0)
		fclose (inFile);

	if(outFile != 0)
		fclose (outFile);
	delete[] buffer;

	return false;
}

void Base64Ex::EncodeTerminate()
{
	m_bEncodeTerminate = true;
}

void Base64Ex::DecodeTerminate()
{
	m_bDecodeTerminate = true;
}
string Base64Ex::GetDecodeError()
{
	return m_strDecodeErr;
}

string Base64Ex::GetEncodeError()
{
	return m_strEncodeErr;
}

⌨️ 快捷键说明

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