- 论坛徽章:
- 59
|
本帖最后由 folklore 于 2018-01-17 15:11 编辑
- #pragma once
- #include <string>
- #include <vector>
- class Base64
- {
- public:
- Base64();
- ~Base64();
- void SetLastTocken(const char Ch2ndLast, const char ChLast);
- std::string Encode(const char Source[], const size_t nSize);
- std::vector<char> Decode(const char *const lpSource);
- private:
- int indexof(char ch);
- char * Base64Decode4Char(const char src[4], char buf[3]);
- static char DefaultEndCodeTable[];
- char EndCodeTable[65]; // 64+1
- };
- #include "stdafx.h"
- #include "Base64.h"
- #define CH2ndLast (sizeof(Base64::EndCodeTable) - 3)
- #define CHLast (sizeof(Base64::EndCodeTable) - 2)
- ///<summary>Base 64 translate table.</summary>
- char Base64::DefaultEndCodeTable[] =
- "ABCDEFGH"
- "IJKLMNOP"
- "QRSTUVWX"
- "YZ"
- "abcdefgh"
- "ijklmnop"
- "qrstuvwx"
- "yz"
- "0123456789+/";
- Base64::Base64()
- {
- strcpy_s(this->EndCodeTable, sizeof(this->EndCodeTable), DefaultEndCodeTable);
- }
- Base64::~Base64()
- {
- }
- void Base64::SetLastTocken(const char Ch2ndLast, const char ChLast){
- this->EndCodeTable[CH2ndLast] = Ch2ndLast; // array[62]
- this->EndCodeTable[CHLast] = ChLast; // array[63]
- }
- static int* Base64Encode3Byte(const char src[3], int buf[4]){
- int Work = (src[0] >>2) &0x3f; // Get 6 bits of 1st byte
- buf[0] = Work;
-
- Work = (src[0] & 0x3) <<4; // Left 2 bits shift to top 6 bit
- Work |= ((src[1] >> 4) & 0x0f); // Get top 4 bits of 2nd bytes
- buf[1] = Work;
-
- Work = ((src[1] << 2) & 0x3C); // Get left 4 bits of 2nd bytes
- Work |= ((src[2] >> 6) & 0x3); // Get top 2 bits of 3rd bytes
- buf[2] = Work;
- Work = (src[2] & 0x3f);
- buf[3] = Work;
- return buf;
- }
- std::string Base64::Encode(const char Source[], const size_t nSize){
- std::string rs;
- if (nSize > 0){
- size_t nResult = (nSize + 2) - (nSize + 2) % 3;
- rs.reserve(nResult + 1);
- for (size_t iEncode = 0; iEncode < nResult - 3; iEncode += 3){
- int buf[4];
- Base64Encode3Byte(&Source[iEncode], buf);
- for (char EncodedByte : buf){
- rs.push_back(this->EndCodeTable[EncodedByte]);
- }
- }
- // -- Encode left bytes
- char LastSource[3] = { 0, }; // Clear to 0
- for (size_t iEncode = nResult - 3, index = 0; iEncode < nSize; iEncode++, index++){
- LastSource[index] = Source[iEncode];
- }
- int buf[4];
- Base64Encode3Byte(LastSource, buf);
- int nRealLeft = ((3 - (nResult - nSize)) * 8 + 5) / 6;
- int index = 0;
- for (index = 0; index < nRealLeft; index++){
- rs.push_back(this->EndCodeTable[buf[index]]);
- }
- for (; index < 4; index++){
- rs.push_back('=');
- }
- }
- return std::move(rs);
- }
- int Base64::indexof(char ch){
- int rs = 0;
- if (ch >= 'A' &&ch <= 'Z'){
- rs = ch - 'A';
- }else if (ch >= 'a' &&ch <= 'z'){
- rs = ch - 'a' + 26;
- }else if (ch >= '0' &&ch <= '9'){
- rs = ch - '0' + 52;
- }else if (ch == this->EndCodeTable[CH2ndLast]){
- rs = CH2ndLast;
- }else if (ch == this->EndCodeTable[CHLast]){
- rs = CHLast;
- }
- return rs;
- }
- char * Base64::Base64Decode4Char(const char src[4], char buf[3]){
- int Indexies[4];
- for (int iInput = 0; iInput < _countof(Indexies); iInput++){
- Indexies[iInput] = this->indexof(src[iInput]);
- }
- int Work = Indexies[0] << 2; // 1st 6 bits go top
- Work |= ((Indexies[1] >> 4) & 0x03); // Append 2 bits of 2nd index
- buf[0] = Work & 0xff;
- Work = (Indexies[1] << 4) & 0xf0; // left 4 bits of 2nd index go top
- Work |= ((Indexies[2] >> 2) & 0x0f); // Append 4 bits of 3nd index to bottom
- buf[1] = Work & 0xff;
- Work = ((Indexies[2] << 6) & 0xc0);
- Work |= (Indexies[3] & 0x3f); // Append last 6 bits
- buf[2] = Work&0xff;
- return buf;
- }
- std::vector<char> Base64::Decode(const char *const lpSource){
- std::vector<char> rs;
- std::string Input(lpSource);
- if (Input.length() > 0){
- while (*Input.rbegin() == '='){
- Input.pop_back();
- }
- size_t nSize = Input.length();
- size_t nSizeHead = nSize - nSize % 4;
- rs.reserve((nSize >> 2) + (nSize >> 1));
- for (int iOutput = 0; iOutput < nSizeHead; iOutput += 4){
- char buf[3];
- this->Base64Decode4Char(Input.c_str() + iOutput, buf);
- for (char ch : buf){
- rs.push_back(ch);
- }
- }
- // -- Cal left byte
- if (nSize != nSizeHead){
- int nCharLeft = (nSize - nSizeHead) * 6 / 8;
- char InputBuf[4] = { 0, };
- strcpy_s(InputBuf, 4, Input.c_str() + nSizeHead);
- char OuptBuf[3] = { 0, };
- this->Base64Decode4Char(InputBuf, OuptBuf);
- _ASSERT(nCharLeft <3);
- for (int iLeft = 0; iLeft < nCharLeft; iLeft++){
- rs.push_back(OuptBuf[iLeft]);
- }
- }
- }
- return std::move(rs);
- }
复制代码
|
|