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

📄 base64.cpp

📁 Base64 decoder for Visual C++, 所谓的Base64 , 是密码学中的一门算法 , 在计算机界来说 , 常常运用在EMAIL传送之上
💻 CPP
字号:
// Base64.cpp : implementation file
//

#include "stdafx.h"
#include "Base64.h"

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

#include <stdio.h>
#include <string.h>
#include <ctype.h>
/////////////////////////////////////////////////////////////////////////////
// BASE64CODE
const char szBASE64CODE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define ENCODE_BYTE(b) szBASE64CODE[((char)b) & 077];
/////////////////////////////////////////////////////////////////////////////
// CBase64 message handlers

int DecodeValue(const int c)
{
	// Note that this works only on ASCII machines.
	if ('A' <= c && c <= 'Z')
		return c - 'A';
	if ('a' <= c && c <= 'z')
		return c - 'a' + 26;
	if ('0' <= c && c <= '9')
		return c - '0' + 52;
	if (c == '+')
		return 62;
	if (c == '/')
		return 63;
	if (c == '=')
		return -1;
	return -2;
}

/////////////////////////////////////////////////////////////////////////////
//	Class: CBase64Enc
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
//	Function: CBase64Enc::OpenFile
//	Description:
//		Opens the source file from which the data will be drawn.
/////////////////////////////////////////////////////////////////////////////
int CBase64Enc::OpenFile(char * pszFileName)
{
	// Open the file.  Handle any errors.
	m_hfile = _lopen(pszFileName, OF_READ);
	if (m_hfile == HFILE_ERROR) 
	{
		m_hfile = NULL;
		return RESULT_ERROR;
	}
	else
		return RESULT_OK;
}

/////////////////////////////////////////////////////////////////////////////
//	Function: CBase64Enc::EncodeLine
//	Description:
//		Reads a line's-worth of data from the file, encodes it,
//		and returns it to the caller.
/////////////////////////////////////////////////////////////////////////////
int CBase64Enc::EncodeLine(char * pszLine, int nLen)
{
	// Our buffer size must be a multiple of 3.
	ASSERT(sizeof(m_uchBuffer) % 3 == 0);

	// Make sure the line buffer passed to us will be able to hold
	// all the encoded data.  If not, error.
	if (nLen < ((sizeof(m_uchBuffer) / 3) * 4 + 1))
		return RESULT_SMALLBUFFER;

	// Zero out the byte buffer so that when we reach the final
	// line and it's not full that it'll be padded with zero.
	for (int i = 0; i < sizeof(m_uchBuffer); i++)
    	m_uchBuffer[i] = 0;

	// Read a line's-worth of bytes from the file.
	int nBytes = _lread(m_hfile, m_uchBuffer, sizeof(m_uchBuffer));
	int iByteIndex = 0;
	int iCharIndex = 0;
	while (nBytes > 0 && iByteIndex < nBytes) 
	{
		pszLine[iCharIndex] = ENCODE_BYTE(m_uchBuffer[iByteIndex] >> 2);
		pszLine[iCharIndex + 1] = ENCODE_BYTE((m_uchBuffer[iByteIndex] << 4) | (m_uchBuffer[iByteIndex + 1] >> 4));
		if (iByteIndex + 1 < nBytes) 
		{
			pszLine[iCharIndex + 2] = ENCODE_BYTE((m_uchBuffer[iByteIndex + 1] << 2) | (m_uchBuffer[iByteIndex + 2] >> 6));
		}
		else 
		{
			pszLine[iCharIndex + 2] = '=';
		}
		if (iByteIndex + 2 < nBytes) 
		{
			pszLine[iCharIndex + 3] = ENCODE_BYTE(m_uchBuffer[iByteIndex + 2]);
		}
		else 
		{
			pszLine[iCharIndex + 3] = '=';
		}
		iByteIndex += 3;
		iCharIndex += 4;
    }

	// Add the line terminator.
	pszLine[iCharIndex] = '\0';
	// See if we hit the end of the file.
	if (nBytes == 0) 
	{
		// Yup, so close it up, handling any error as appropriate.
		if (_lclose(m_hfile) == HFILE_ERROR) 
		{
			m_hfile = NULL;
			return RESULT_ERROR;
		}
		else 
		{
			m_hfile = NULL;
			return RESULT_DONE;
		}
	}
	else 
	{
		return RESULT_OK;
	}
}

/////////////////////////////////////////////////////////////////////////////
//	Class: CBase64Dec
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
//	Function: CBase64Dec::OpenFile
//	Description:
//		Open a file that we can write the characters to.
/////////////////////////////////////////////////////////////////////////////
int CBase64Dec::OpenFile(char * pszFileName)
{
	// Open the file.  Handle any errors.
	//m_hfile = _lopen(pszFileName, OF_WRITE);
	m_hfile=CreateFile(pszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	//if (m_hfile == HFILE_ERROR) 
	if (m_hfile == NULL)
	{
		m_hfile = NULL;
		return RESULT_ERROR;
	}
	else
		return RESULT_OK;
}

/////////////////////////////////////////////////////////////////////////////
//	Function: CBase64Dec::DecodeLine
//	Description:
//		Decode a line of data and write the bytes to the open file.
/////////////////////////////////////////////////////////////////////////////
int CBase64Dec::DecodeLine(char * pszLine, int nLineLen)
{
	DWORD Written;
	uchar * uchBuffer = new uchar[nLineLen];
	int nDecodedLen = DecodeLine(pszLine, nLineLen, uchBuffer, nLineLen);
	if (nDecodedLen >= 0) 
	{
		//if (_lwrite(m_hfile, (LPCSTR)uchBuffer, nDecodedLen) ==	(UINT)HFILE_ERROR)
		if (WriteFile(m_hfile, (LPCSTR)uchBuffer, nDecodedLen, &Written, NULL) == NULL)
			nDecodedLen = RESULT_ERROR;
	}
	delete[] uchBuffer;
    return nDecodedLen;
}

/////////////////////////////////////////////////////////////////////////////
//	Function: CBase64Dec::DecodeLine
//	Description:
//		Simply decode a line of bytes.  Note that a file doesn't
//		have to be opened to use this member function. Returns
//		the count of the decoded bytes or -1 on error.
/////////////////////////////////////////////////////////////////////////////
int CBase64Dec::DecodeLine(char * pszLine, int nLineLen,
	uchar * uchBuffer, int nBufferLen)
{
	int iLineIndex = 0;
	int iBufferIndex = 0;

	while (iLineIndex < nLineLen) 
	{
		// Group together four characters for decode.
		while (iLineIndex < nLineLen && m_iChars < 4) 
		{
			int c = pszLine[iLineIndex++];
			// Ignore characters that aren't BASE64 characters
            // (e.g., spaces, CRLF, etc.).
			if (DecodeValue(c) != -2)
				m_uchStoredChars[m_iChars++] = (uchar) c;
		}

		if (m_iChars == 4) 
		{
			// We've got four characters, so decode them.
			m_iChars = 0;

			// Decode first byte.
			if (iBufferIndex == nBufferLen) return RESULT_ERROR;
			uchBuffer[iBufferIndex++] = (uchar)	(((uchar)DecodeValue(m_uchStoredChars[0]) << 2) | ((uchar)DecodeValue(m_uchStoredChars[1]) >> 4));

			// Decode second byte.
			if (iBufferIndex == nBufferLen) 
				return RESULT_ERROR;
			if (m_uchStoredChars[2] == '=') 
				return iBufferIndex;
			uchBuffer[iBufferIndex++] = (uchar)	(((uchar)DecodeValue(m_uchStoredChars[1]) << 4) | ((uchar)DecodeValue(m_uchStoredChars[2]) >> 2));

			// Decode third byte.
			if (iBufferIndex == nBufferLen) 
				return RESULT_ERROR;
			if (m_uchStoredChars[3] == '=') 
				return iBufferIndex;
			uchBuffer[iBufferIndex++] = (uchar)	(((uchar)DecodeValue(m_uchStoredChars[2]) << 6) | ((uchar)DecodeValue(m_uchStoredChars[3])));
		}
	}

    // Return the count of decoded bytes.
    return iBufferIndex;
}
/////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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