?? base64.cpp
字號:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Base64.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
static const unsigned char vec[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const unsigned char padding = '=';
//---------------------------------------------------------------------------
void TBase64Buffer::Encode(unsigned char * in,int len,char* out)
{
int d = len / 3;
int m = len % 3;
if(m != 0) d++;
int buflen = d * 3;
unsigned char * buf = new char[buflen];
memset(buf,0,buflen);
memcpy(buf,in,len);
int j = 0;
for(int i = 0,k = 0;i < d;i++,k += 3)
{
unsigned char ch;
ch = (unsigned char)((buf[k] & 0xFC) >> 2);
out[j] = vec[ch];
j++;
ch = (unsigned char)(((buf[k] & 0x03) << 4) | (buf[k + 1] >> 4));
out[j] = vec[ch];
j++;
ch = (unsigned char)(((buf[k + 1] & 0x0F) << 2) | (buf[k + 2] >> 6));
out[j] = vec[ch];
j++;
ch = (unsigned char)(buf[k + 2] & 0x3F);
out[j] = vec[ch];
j++;
}
out[j] = 0;
delete[] buf;
}
//---------------------------------------------------------------------------
void TBase64Buffer::Decode(unsigned char* in,int len,unsigned char* out)
{
int loop = len / 4;
unsigned char index[4];
for(int i = 0,j = 0,k = 0;i < loop;i++,k += 4)
{
index[0] = ConvToNumber(in[k]);
index[1] = ConvToNumber(in[k + 1]);
index[2] = ConvToNumber(in[k + 2]);
index[3] = ConvToNumber(in[k + 3]);
unsigned char ch;
ch = (unsigned char)((index[0] << 2) | (index[1] >> 4));
out[j] = ch;
j++;
ch = (unsigned char)((index[1] << 4) | (index[2] >> 2));
out[j] = ch;
j++;
ch = (unsigned char)((index[2] << 6) | index[3]);
out[j] = ch;
j++;
}
}
//---------------------------------------------------------------------------
int TBase64Buffer::GetEncodeOutLen(int inLen)
{
int d = inLen / 3;
int m = inLen % 3;
if(m != 0) d++;
return d * 4 + 1;
}
//---------------------------------------------------------------------------
int TBase64Buffer::GetDecodeOutLen(int inLen)
{
return (inLen / 4) * 3;
}
//---------------------------------------------------------------------------
int TBase64Buffer::ConvToNumber(unsigned char inByte)
{
if (inByte >= 'A' && inByte <= 'Z')
return (inByte - 'A');
if (inByte >= 'a' && inByte <= 'z')
return (inByte - 'a' + 26);
if (inByte >= '0' && inByte <= '9')
return (inByte - '0' + 52);
if (inByte == '+')
return (62);
if (inByte == '/')
return (63);
return (-1);
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -