📄 base64.cpp
字号:
// Base64.cpp: implementation of the Base64 class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Base64.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Base64::Base64()
{
reslenth = 0;
memset(this->m_resultbuffer,0,sizeof(m_resultbuffer));
}
Base64::~Base64()
{
}
//编码函数
bool Base64::EnCode(char *source,int len)
{
if(source == NULL)
return false;
char base64_alphabet[] = {'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','+','/','='};
reslenth = 0;
int tmp[4];
int index = 0;
for(;index < len;index += 3)
{
if(index+2 < len)
{
tmp[0] = (source[index]&0xfc) >> 2;
tmp[1] = ((source[index]&0x03)<<4) + ((source[index+1]&0xf0)>>4);
tmp[2] = ((source[index+1]&0x0f)<<2) + ((source[index+2]&0xc0)>>6);
tmp[3] = source[index+2]&0x3f;
}
else if(index + 1 < len)
{
tmp[0] = (source[index]&0xfc) >> 2;
tmp[1] = ((source[index]&0x03)<<4) + ((source[index+1]&0xf0)>>4);
tmp[2] = ((source[index+1]&0x0f)<<2);
tmp[3] = 64;
}
else
{
tmp[0] = (source[index]&0xfc) >> 2;
tmp[1] = ((source[index]&0x03)<<4);
tmp[2] = 64;
tmp[3] = 64;
}
for(int i = 0;i < 4;++i)
{
if(reslenth + i >= MAX_BASE64_BUFFER)
return false;
m_resultbuffer[reslenth + i] = base64_alphabet[tmp[i]];
}
reslenth += 4;
}
m_resultbuffer[reslenth] = 0;
return true;
}
int Base64::GetResLengh()
{
return reslenth;
}
bool Base64::GetRes(char *result, int lenth)
{
int actlen = reslenth < lenth?reslenth:lenth;
memcpy(result,m_resultbuffer,sizeof(char)*actlen);
result[actlen] = 0;
return true;
}
bool Base64::DeCode(char *source)
{
if(source == NULL)
return false;
char base64_alphabet[] = {'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','+','/','='};
int index,i,table[256];
int lenth = strlen(source);
char tmp[4];
memset(table,0,sizeof(table));
for(i = 0;i <= 64;++i)
table[base64_alphabet[i]] = i;
reslenth = 0;
for(index = 0;index < lenth;index+=4)
{
if(source[index+2] == '=')
{
tmp[0] = ((table[source[index]]&0x3f)<<2)+((table[source[index+1]]&0x30)>>4);
tmp[1] = 0;
tmp[2] = 0;
}
else if(source[index+3] == '=')
{
tmp[0] = ((table[source[index]]&0x3f)<<2)+((table[source[index+1]]&0x30)>>4);
tmp[1] = ((table[source[index+1]]&0x0f)<<4) + ((table[source[index+2]]&0x3c)>>2);
tmp[2] = 0;
}
else
{
tmp[0] = ((table[source[index]]&0x3f)<<2)+((table[source[index+1]]&0x30)>>4);
tmp[1] = ((table[source[index+1]]&0x0f)<<4) + ((table[source[index+2]]&0x3c)>>2);
tmp[2] = ((table[source[index+2]]&0x03)<<6) + ((table[source[index+3]]&0x3f));
}
for(i = 0;i<3&&tmp[i]!=0;++i)
{
if(reslenth >= MAX_BASE64_BUFFER)
return false;
m_resultbuffer[reslenth++] = tmp[i];
}
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -