Base64.cpp
上传用户:weimei12
上传日期:2022-08-11
资源大小:185k
文件大小:4k
- #include "stdafx.h"
- #include "MIMECode.h"
- #include "Base64.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CBase64::CBase64()
- {
- char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- for( int i = 0; i < 64; i ++ )
- {
- m_Arr[i] = arr[i];
- m_Map.SetAt(arr[i], i);
- }
- }
- CBase64::~CBase64()
- {
- }
- CString CBase64::Encode(LPCTSTR szEncoding, int nSize)
- {
- int nOutlen = 0;
- nOutlen = (nSize / 3 * 4) + (nSize % 3 ? 4 : 0);
- char * pOutbuf = NULL;
- pOutbuf = new char [nOutlen + 1];
- if (NULL == pOutbuf)
- {
- return "";
- }
- pOutbuf[nOutlen] = ' ';
- char * p = pOutbuf;//used as cursor
- int i = 0;
- for(; i < nSize / 3; i++)
- {
- EncodeEvery3Byte(szEncoding + i * 3, p);
- p = p + 4;
- }
- if(nSize % 3)
- {
- EncodeEvery3Byte(szEncoding + i * 3, p, nSize % 3);
- }
- CString strOut(pOutbuf);
- delete []pOutbuf;
- return strOut;
- }
- CString CBase64:: Decode(LPCTSTR szDecoding)
- {
- int nSrclen = 0;
- nSrclen = (int)strlen(szDecoding);
- int nOutlen = 0;
- nOutlen = (nSrclen / 4 * 3);//this len now may be bigger, becuase '=' is not ture code
- char* szOutput = NULL;
- szOutput = new char [nOutlen + 1];
- if (NULL == szOutput)
- {
- return "";
- }
- memset(szOutput, ' ', nOutlen + 1);
- char * p = szOutput;//use as cursor
- int i = 0;
- for(; i < nSrclen / 4; i++ )
- {
- DecodeEvery4Byte(szDecoding + i * 4, p);
- p = p + 3;
- }
- //process the "=" if there are any
- i = 0;
- for(int k = nSrclen-1; szDecoding[k] == '='; k--)
- {
- i++;
- }
- nOutlen = nOutlen - i;
- CString strOutput(szOutput);
- delete szOutput;
- return strOutput;
- }
- ///<summary>
- /// Encode the buffer 3 byte one time
- ///</summary>
- void CBase64::EncodeEvery3Byte(LPCTSTR szInBuf, LPTSTR szOutBuf, int nRest /*= 0*/)
- {
- ASSERT(nRest >= 0 && nRest <= 2);
- if(!nRest)
- {//the part that is the multiple of three
- int i = 0;
- for(; i < 4; i ++)
- {
- * (szOutBuf ++) = m_Arr[Get6Bits(szInBuf, i)];
- }
- }
- else
- {//the remainder
- memset(szOutBuf, '=', 4);
- char cccc[4];
- memset(cccc, 0x00, 4);
- memcpy(cccc, szInBuf, nRest);
- szOutBuf[0] = m_Arr[Get6Bits(cccc, 0)];
- szOutBuf[1] = m_Arr[Get6Bits(cccc, 1)];
- if(nRest == 2)
- {//some bits remain in this byte
- szOutBuf[2] = m_Arr[Get6Bits(cccc, 2)];
- }
- }
- }
- ///<summary>
- /// Get 6 bits from the origin string
- ///</summary>
- char CBase64::Get6Bits(LPCTSTR szInBase, int nOffset)
- {
- ASSERT(nOffset >= 0 && nOffset <= 3);
- unsigned char c = 0x00;
- unsigned char temp = 0;
- if(0 == nOffset)
- {
- c = szInBase[0] & MakeBitSection(0, 5);
- c = c >> 2;
- }
- else if(1 == nOffset)
- {
- c = (szInBase[0] & MakeBitSection(6, 7)) << 4;
- temp = szInBase[1] & MakeBitSection(0, 3);
- c |= temp >> 4;
- }
- else if(2 == nOffset)
- {
- c = (szInBase[1] & MakeBitSection(4, 7)) << 2;
- temp = (szInBase[2] & MakeBitSection(0, 1));
- c |= temp >> 6;
- }
- else if(3 == nOffset)
- {
- c = szInBase[2] & MakeBitSection(2, 7);
- }
- return c;
- }
- ///<summary>
- /// decode the string 4 byte one time
- ///</summary>
- void CBase64::DecodeEvery4Byte(LPCTSTR szInBuf, LPTSTR szOutBuf)
- {
- char b6 = 0;
- int i = 0;
- for(; i < 4; i ++)
- {
- if(!m_Map.Lookup(szInBuf[i], b6))
- {
- b6 = 0x00;
- }
- Set6Bits(i, b6, szOutBuf);
- }
- }
- ///<summary>
- /// Set 6 bits to the destination string
- ///</summary>
- void CBase64::Set6Bits(int nOffset, char n6Bits, LPTSTR szOutBuf)
- {
- ASSERT(nOffset >= 0 && nOffset <= 3);
- unsigned char temp = 0;
- if(0 == nOffset)
- {
- n6Bits = n6Bits << 2;
- szOutBuf[0] |= n6Bits;
- }
- else if(1 == nOffset)
- {
- n6Bits = n6Bits << 2;
- temp = (n6Bits & MakeBitSection(0, 1));
- szOutBuf[0] |= temp >> 6;
- szOutBuf[1] |= (n6Bits & MakeBitSection(2, 5)) << 2;
- }
- else if(2 == nOffset)
- {
- n6Bits = n6Bits << 2;
- temp = (n6Bits & MakeBitSection(0, 3));
- szOutBuf[1] |= temp >> 4;
- szOutBuf[2] |= (n6Bits & MakeBitSection(4, 5)) << 4;
- }
- else if(3 == nOffset)
- {
- szOutBuf[2] |= n6Bits;
- }
- }