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

📄 base64operadlg.cpp

📁 进行base64格式转换的工具
💻 CPP
字号:
// Base64OperaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Base64Opera.h"
#include "Base64OperaDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBase64OperaDlg dialog

CBase64OperaDlg::CBase64OperaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBase64OperaDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBase64OperaDlg)
	m_ascii = _T("123456");
	m_base = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBase64OperaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBase64OperaDlg)
	DDX_Control(pDX, IDC_BASECODE, m_basectrl);
	DDX_Control(pDX, IDC_ASCIICODE, m_asciictrl);
	DDX_Text(pDX, IDC_ASCIICODE, m_ascii);
	DDX_Text(pDX, IDC_BASECODE, m_base);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBase64OperaDlg, CDialog)
	//{{AFX_MSG_MAP(CBase64OperaDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDEncode, OnEncode)
	ON_BN_CLICKED(IDDecode, OnDecode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBase64OperaDlg message handlers

BOOL CBase64OperaDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBase64OperaDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBase64OperaDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBase64OperaDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


/////////////////////////////////////////////////////////////////
////////////////////////Base64编码///////////////////////////////
/////////////////////////////////////////////////////////////////
BYTE	pEncodeTable[] = {
	'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
	'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
	'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
	'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};

void	base64_encode( LPVOID pParameter, LPVOID pOutput )
{
	static int	i=0;

	i++;
	DWORD	*pParam = (DWORD*)pParameter;
	BYTE	*pOut = (BYTE*)pOutput;

	pOut[0] = pEncodeTable[((*pParam>>18)&0x3f)];
	pOut[1] = pEncodeTable[((*pParam>>12)&0x3f)];
	pOut[2] = pEncodeTable[((*pParam>>6)&0x3f)];
	pOut[3] = pEncodeTable[(*pParam&0x3f)];
}

/*
BOOL mfBase64Encode(BYTE *pbSource, DWORD dwSourceLen, BYTE *pbDestination, DWORD *pdwDestinationLen)
{
	if( !pbSource || 
		dwSourceLen <=0 )			// checks source data
	{
		SetLastError( ERROR_INVALID_PARAMETER );
		return -1;
	}


	DWORD	dwLen = ( (dwSourceLen/3) + ( (dwSourceLen%3) ? 1 : 0 ) )*4 ;
	DWORD	dwIndex=(dwLen/4)*3;
	dwLen += (dwLen/64)*2;
	dwLen += 5;
	if( *pdwDestinationLen < dwLen )		// checks length of buffer
	{
		*pdwDestinationLen = dwLen;
		SetLastError( ERROR_MORE_DATA );
		return -1;
	}

	*pdwDestinationLen = dwLen;

	if( !pbDestination )
		return 0;

	memset(pbDestination,0,*pdwDestinationLen);

	BYTE	*pbDes = pbDestination;
	BYTE	*pSource = (BYTE*)LocalAlloc(0x40,dwIndex);
	if( !pbSource )
	{
		SetLastError( 8 );
		return -1;
	}

	memmove(pSource,pbSource,dwSourceLen);
	ENCODE_STRUCT	*pencodeBLOB = (ENCODE_STRUCT*)LocalAlloc(0x40,sizeof(ENCODE_STRUCT));
	if( !pencodeBLOB )
	{
		LocalFree( pSource );
		SetLastError( 8 );
		return -1;
	}

	BYTE	*pbSuc = pSource;
	int		i=0;
	do
	{
		pencodeBLOB->bStream1 = pbSuc[2];
		pencodeBLOB->bStream2 = pbSuc[1];
		pencodeBLOB->bStream3 = pbSuc[0];
		pbSuc += 3;
		dwIndex -= 3;
		base64_encode( pencodeBLOB,pbDes );
		pbDes += 4;
		i += 3;
//
//		// 在每一行(48个字符)后加入回车\换行符
//		if( i == 48 )
//		{
//			pbDes[0] = 0x0d;
//			pbDes[1] = 0x0a;
//			pbDes +=2;
//			i = 0;
//		}

	}while(dwIndex);

	// 结束符为 =
	if( (dwSourceLen%3) )
	{
		pbDes -= 2;
		if( pbDes[0] == 'A' )
		{
			pbDes[0] = '=';
			pbDes[1] = '=';
		}
		else
		{
			if( pbDes[1] == 'A' )
				pbDes[1] = '=';
		}
	}

	LocalFree(pSource);
	return 0;
}
*/

//////////////////////////Base64解码///////////////////////////
BYTE	mfDecode( BYTE	suc )
{
	if( suc >= 'A' && suc <= 'Z' )
		return( suc-'A');
	if( suc >= 'a' && suc <= 'z' )
		return( suc-'a'+26 );
	if( suc >= '0' && suc <= '9' )
		return( suc-'0'+52 );
	if( suc == '+' ) return( 62 );
	return( 63 );
}

BOOL mfBase64Decode(BYTE *pbSource,DWORD dwSourceLen,BYTE *pbDestination,DWORD *pdwDestinationLen)
{
	if( !pbSource ||
		dwSourceLen <=0 )
	{
		SetLastError( ERROR_INVALID_PARAMETER );
		return -1;
	}

	DWORD	dwLen;
	BOOL	bFlag = 0;

	for(int i=0;i<=64;i++)
	{
		if( pbSource[i] == 0x0d )
		{
			dwLen = (dwSourceLen/66)*48 + ((dwSourceLen-(dwSourceLen/66)*66)/4)*3;
			bFlag = 1;
		}
		else
			dwLen = (dwSourceLen/64)*48 + ((dwSourceLen-(dwSourceLen/64)*64)/4)*3;
	}

	//dwLen = (dwSourceLen/64)*48 + ((dwSourceLen-(dwSourceLen/64)*64)/4)*3;
	dwLen += 2;
	if( !pbDestination )
	{
		*pdwDestinationLen = dwLen;
		return 0;
	}

	if( *pdwDestinationLen < dwLen )
		return -1;

	*pdwDestinationLen = dwLen;

	BYTE	*pSuc = pbSource;
	BYTE	*pDes = pbDestination;
	dwLen = dwSourceLen;
	if( pSuc[dwLen-1] == '=' )
		pSuc[dwLen-1] = 'A';
	if( pSuc[dwLen-2] == '=' )
		pSuc[dwLen-2] = 'A';

	do
	{
		for( DWORD i = 4 ; i<=(dwLen<64?dwLen:64) ; i+= 4 )
		{
			pDes[2] = mfDecode(pSuc[3]) | ((mfDecode(pSuc[2])<<6)&0xc0);
			pDes[1] = ((mfDecode(pSuc[2])>>2)&0x0f) | ((mfDecode(pSuc[1])<<4)&0xf0);
			pDes[0] = ((mfDecode(pSuc[1])>>4)&0x03) | ((mfDecode(pSuc[0])<<2)&0xfc);
			pDes += 3;
			pSuc += 4;
		}

		dwLen -= (dwLen<64?dwLen:64);
		if( dwLen && bFlag )
		{
			pSuc += 2;
			dwLen -= (dwLen<2?dwLen:2);
		}
	}while(dwLen);

	return 0;
}
/////////////////////////////////////////////////////////////////
////////////////////////Base64编码///////////////////////////////
/////////////////////////////////////////////////////////////////

int strtoint24b(CString str, int len, int *outbuf)
{
	char *temp;
	int i=0;
	temp=(char*)malloc(len*2);
	memset(temp, 0, len*2);
	memcpy(temp, str, len);
	
	//for(; i<(((len%3)==0)?(len/3):(len/3+1)); i++)
	for(; i<(len/3); i++)
	{
		outbuf[i]=((temp[3*i]&0xff)<<16) + ((temp[3*i+1]&0xff)<<8) + ((temp[3*i+2]&0xff));
	}
	//if((len%3)==1)		outbuf[i] = (temp[3*i]&0xff)<<16;
	//else if((len%3)==2)	outbuf[i] = ((temp[3*i]&0xff)<<16) + ((temp[3*i+1]&0xff)<<8);
	if((len%3)==1)		outbuf[i] = (temp[3*i]&0xff);
	else if((len%3)==2)	outbuf[i] = ((temp[3*i]&0xff)<<8) + ((temp[3*i+1]&0xff));
	
	free(temp);
	return 0;
}

void CBase64OperaDlg::OnEncode() 
{
	UpdateData(true);

	int *out, j;
	char *tmp;
	out = (int* )malloc(m_ascii.GetLength()*2);
	tmp = (char*)malloc(m_ascii.GetLength()*2);
	memset(out, 0, m_ascii.GetLength()*2);
	memset(tmp, 0, m_ascii.GetLength()*2);
	
	strtoint24b(m_ascii, m_ascii.GetLength(), out);
	for(j=0; j<(m_ascii.GetLength()/3);j++)
	{
		base64_encode(out+j, tmp+4*j);
	}
	
	if((m_ascii.GetLength()%3)==1){
		tmp[4*j]   = pEncodeTable[(out[j]>>2)&0x3f];
		tmp[4*j+1] = pEncodeTable[(out[j]<<4)&0x3F];
	}
	if((m_ascii.GetLength()%3)==2){
		tmp[4*j]   = pEncodeTable[(out[j]>>10)&0x3f];
		tmp[4*j+1] = pEncodeTable[(out[j]>>4)&0x3f];
		tmp[4*j+2] = pEncodeTable[(out[j]<<2)&0x3f];
	}
	
	/*if((m_ascii.GetLength()%3)!=0 )
		base64_encode(out+j, tmp+4*j);*/
	
	m_basectrl.SetWindowText(tmp);
	free(out);
}

void CBase64OperaDlg::OnDecode() 
{
	UpdateData(true);
	
	DWORD m, n;
	unsigned char *tmp, *out;
	out = (unsigned char* )malloc(m_ascii.GetLength()*2);
	tmp = (unsigned char*)malloc(m_ascii.GetLength()*2);
	memset(out, 0, m_ascii.GetLength()*2);
	memset(tmp, 0, m_ascii.GetLength()*2);
	memcpy(tmp, m_ascii.GetBuffer(0), m_ascii.GetLength()+1);
	
	m=m_ascii.GetLength();

	mfBase64Decode(tmp, m, out, &n);
	
	m_basectrl.SetWindowText((char*)out);
	free(out);
}

⌨️ 快捷键说明

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