123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #include "StdAfx.h"
- #include "Base64.h"
- CBase64::CBase64(void)
- {
- }
- CBase64::~CBase64(void)
- {
- }
- const char CBase64::sm_base64digits[65] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- const char CBase64::sm_base64val[128] = {
- BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
- BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
- BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
- BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,BAD, BAD,BAD,BAD,BAD,
- BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
- 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,BAD, BAD,BAD,BAD,BAD
- };
- /************************************************************************/
- /* 函数:[6/2/2016 IT];
- /* 描述:将字节转换为Base64字符;
- /* 参数:;
- /* [IN] pbinary: 要转换成Base64字符的二进制字节数组;
- /* [IN] nbinaryLen: pbinary所指向的缓存大小;
- /* [OUT] pOutBase64: 返回转换后的Base64字符,以'\0'结束;
- /* 返回:返回转换成功后的Base64字符数;
- /* 注意:注意参数1是字节BYTE,返回的pOutBase64是人为加上'\0'结束符;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void CBase64::binToBase64(IN const unsigned char *pbinary, IN size_t nbinaryLen, OUT char *pOutBase64)
- {
- for ( ; nbinaryLen >= 3; nbinaryLen -= 3, pbinary += 3)
- {
- *pOutBase64++ = sm_base64digits[pbinary[0] >> 2];
- *pOutBase64++ = sm_base64digits[((pbinary[0] << 4) & 0x30) | (pbinary[1] >> 4)];
- *pOutBase64++ = sm_base64digits[((pbinary[1] << 2) & 0x3c) | (pbinary[2] >> 6)];
- *pOutBase64++ = sm_base64digits[pbinary[2] & 0x3f];
- }
- if (nbinaryLen > 0)
- {
- unsigned char fragment;
- *pOutBase64++ = sm_base64digits[pbinary[0] >> 2];
- fragment = (pbinary[0] << 4) & 0x30;
- if (nbinaryLen > 1)
- fragment |= pbinary[1] >> 4;
- *pOutBase64++ = sm_base64digits[fragment];
- *pOutBase64++ = (nbinaryLen < 2) ? '=' : sm_base64digits[(pbinary[1] << 2) & 0x3c];
- *pOutBase64++ = '=';
- }
- *pOutBase64 = '\0';
- }
- /************************************************************************/
- /* 函数:[6/2/2016 IT];
- /* 描述:将Base64字符串转为化二进制字节字符串;
- /* 参数:;
- /* [IN] pBase64: 要转化成二进制字节字符串的Base64字符;
- /* [OUT] pbinary: 返回转化成二进制字节的字符串;
- /* [IN] maxLen: pbinary指向的缓存大小;
- /* 返回:返回转化成二进制字节字符数;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int CBase64::base64ToBin(IN const char *pBase64, OUT unsigned char *pbinary, IN size_t maxLen)
- {
- size_t len = 0;
- unsigned char digit1, digit2, digit3, digit4;
- if (pBase64[0] == '+' && pBase64[1] == ' ')
- pBase64 += 2;
- if (pBase64[0] == '\r')
- return 0;
- while (*pBase64 && *pBase64 != '\r' /*&& digit4 != '='*/)
- {
- digit1 = pBase64[0];
- if (decode64(digit1) == BAD)
- return -1;
- digit2 = pBase64[1];
- if (decode64(digit2) == BAD)
- return -1;
- digit3 = pBase64[2];
- if (digit3 != '=' && decode64(digit3) == BAD)
- return -1;
- digit4 = pBase64[3];
- if (digit4 != '=' && decode64(digit4) == BAD)
- return -1;
- pBase64 += 4;
- ++len;
- if (maxLen && len > maxLen)
- return -1;
- *(pbinary++) = (decode64(digit1) << 2) | (decode64(digit2) >> 4);
- if (digit3 != '=')
- {
- ++len;
- if (maxLen && len > maxLen)
- return -1;
- *(pbinary++) = ((decode64(digit2) << 4) & 0xf0) | (decode64(digit3) >> 2);
- if (digit4 != '=')
- {
- ++len;
- if (maxLen && len > maxLen)
- return -1;
- *(pbinary++) = ((decode64(digit3) << 6) & 0xc0) | decode64(digit4);
- } // if
- } // if
- } // while
- return len;
- }
- #ifndef VC60
- /************************************************************************/
- /* 函数:StringToBase64[1/14/2017 IT];
- /* 描述:将指定字符串转成Base64字符串;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- CStringA CBase64::StringToBase64A(IN CStringA strSource)
- {
- CStringA strBase64 = "";
- // 计算出Base64字符长度;
- INT nBase64Len = CalcBase64Len(strSource.GetLength());
- CHAR* pBase64 = new CHAR[nBase64Len+1];
- memset(pBase64, 0, nBase64Len+1);
- binToBase64((const unsigned char*)strSource.GetString(), strSource.GetLength(), pBase64);
- strBase64 = pBase64;
- if (pBase64) delete []pBase64;
- return strBase64;
- }
- CStringW CBase64::StringToBase64W(IN CStringW strSource)
- {
- CStringW strBase64 = L"";
- // 获取多字节字符字节数;
- int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, strSource, -1, NULL, 0, NULL, NULL);
- if ( cbMultiByte == 0 )
- return L"";
- // 转换成多字节字符;
- CHAR *pMultiByteStr = new CHAR[cbMultiByte+1];
- memset(pMultiByteStr, 0, cbMultiByte + 1);
- int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, strSource, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
- if (nWriteNum != cbMultiByte)
- {
- if (pMultiByteStr)
- delete []pMultiByteStr;
- return L"";
- }
- // 计算出Base64字符长度;
- INT nBase64Len = CalcBase64Len(cbMultiByte);
- CHAR *pBase64 = new CHAR[nBase64Len+1];
- memset(pBase64, 0, nBase64Len+1);
- binToBase64((const unsigned char*)pMultiByteStr, cbMultiByte, pBase64);
- strBase64 = pBase64;
- if (pBase64) delete []pBase64;
- if (pMultiByteStr) delete []pMultiByteStr;
- return strBase64;
- }
- /************************************************************************/
- /* 函数:[1/14/2017 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- CStringA CBase64::Base64ToStringA(IN CStringA strBase64)
- {
- CStringA strResult = "";
- // 计算出结果长度;
- INT nStrlen = CalcBinLen(strBase64.GetLength());
- BYTE *pString = new BYTE[nStrlen+1];
- memset(pString, 0, nStrlen+1);
- base64ToBin(strBase64, pString, nStrlen);
- strResult = (char*)pString;
- if ( pString) delete []pString;
- return strResult;
- }
- CStringW CBase64::Base64ToStringW(IN CStringW strBase64)
- {
- CStringW strResult = L"";
- // 获取多字节字符字节数;
- int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, strBase64, -1, NULL, 0, NULL, NULL);
- if ( cbMultiByte == 0 )
- return L"";
- // 转换成多字节字符;
- CHAR *pMultiByteStr = new CHAR[cbMultiByte+1];
- memset(pMultiByteStr, 0, cbMultiByte + 1);
- int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, strBase64, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
- if (nWriteNum != cbMultiByte)
- {
- if (pMultiByteStr)
- delete []pMultiByteStr;
- return L"";
- }
- // 计算出Bin字符长度;
- INT nStrlen = CalcBinLen(cbMultiByte);
- CHAR *pString = new CHAR[nStrlen+1];
- memset(pString, 0, nStrlen+1);
- base64ToBin(pMultiByteStr, (unsigned char*)pString, nStrlen );
- // 获取宽字符字节数;
- int cchWideChar = MultiByteToWideChar(CP_ACP, 0, pString, -1, NULL, 0);
- if ( cchWideChar == 0 )
- return L"";
- // 转换成宽字符串;
- WCHAR *pResult = new WCHAR[cchWideChar];
- memset(pResult, 0 , sizeof(WCHAR)*cchWideChar);
- nWriteNum = MultiByteToWideChar(CP_ACP, 0, pString, -1, pResult, cchWideChar );
- if ( nWriteNum != cchWideChar)
- return L"";
- strResult = pResult;
- if ( pResult ) delete[] pResult;
- if (pString) delete []pString;
- if (pMultiByteStr) delete []pMultiByteStr;
- return strResult;
- }
- #endif
|