#include "StdAfx.h"
#include "Base64.h"
//以下函数检查str为NULL时的情况,cnt表示str中的字节个数
size_t Base64Encode(const unsigned char* str, size_t cnt, char* strout)
//当strout为NULL时,返回所需的字节数,计算公式是((cnt-1)/3+1)*4
//否则编码写入strout中,并返回((cnt-1)/3+1)*4
{
size_t i,j,k;
size_t off,num;
const static char strbase[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
if(0==str || 0==cnt)
return 0;
if(0==strout)
return ((cnt-1)/3+1)<<2;
off=cnt%3;
num=cnt/3;
for(i=0;i<num;++i)
{
j=i<<2;//i*4
k=i*3;
strout[j] =strbase[str[k]>>2];
strout[j+1]=strbase[((str[k]<<4)|(str[k+1]>>4))&0x3F];
strout[j+2]=strbase[((str[k+1]<<2)|(str[k+2]>>6))&0x3F];
strout[j+3]=strbase[str[k+2]&0x3F];
}
if(1==off)
{
j=i<<2;//i*4
k=i*3;
strout[j] =strbase[str[k]>>2];
strout[j+1]=strbase[(str[k]<<4)&0x3F];
strout[j+2]='=';
strout[j+3]='=';
return ((cnt-1)/3+1)<<2;
}
else if(2==off)
{
j=i<<2;//i*4
k=i*3;
strout[j] =strbase[str[k]>>2];
strout[j+1]=strbase[((str[k]<<4)|(str[k+1]>>4))&0x3F];
strout[j+2]=strbase[(str[k+1]<<2)&0x3F];
strout[j+3]='=';
return ((cnt-1)/3+1)<<2;
}
else
return ((cnt-1)/3+1)<<2;
}
size_t Base64Decode(const unsigned char* str, size_t cnt, unsigned char* strout)
//当strout为NULL时,返回所需的字节数,cnt须为4的倍数,若不是,则返回0
//若str不能解码,则也返回0,可以解码的情况下,返回写入strout的字节数
//字节数计算公式是:cnt/4*3-num('='),num('=')表示str结尾处的'='个数(只考虑0-2个)
{
size_t i,j,k;
size_t off(cnt%4),num;
const static unsigned char strbase[]=
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\x3e[[[\x3f\x34\x35"//'1'
"\x36\x37\x38\x39\x3a\x3b\x3c\x3d[[[][[[\x00\x01\x02\x03\x04\x05\x06"//'G'
"\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"//'Z'
"[[[[[[\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a"//'Q'
"\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33[[[[["//前128个字符结束了
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[";//'['表示其余值;']'表示'='
if(0==str || 0==cnt || 0!=off)
return 0;
if(0==strout)
{
if('='==str[cnt-1])
{
if('='==str[cnt-2])
return (cnt>>2)*3-2;
else
return (cnt>>2)*3-1;
}
else
return (cnt>>2)*3;
}
for(i=0,num=cnt/4-1;i<num;++i)
{
j=i*3;
k=i<<2;//i*4
if('['==strbase[str[k]] || ']'==strbase[str[k]]
|| '['==strbase[str[k+1]] || ']'==strbase[str[k+1]]
|| '['==strbase[str[k+2]] || '['==strbase[str[k+3]])
return 0;
strout[j] =(strbase[str[k]]<<2)|(strbase[str[k+1]]>>4);
strout[j+1]=(strbase[str[k+1]]<<4)|(strbase[str[k+2]]>>2);
strout[j+2]=(strbase[str[k+2]]<<6)|strbase[str[k+3]];
}
j=i*3;
k=i<<2;//i*4
if('['==strbase[str[k]] || ']'==strbase[str[k]]
|| '['==strbase[str[k+1]] || ']'==strbase[str[k+1]]
|| '['==strbase[str[k+2]] || '['==strbase[str[k+3]])
return 0;
strout[j] =(strbase[str[k]]<<2)|(strbase[str[k+1]]>>4);
if(']'==strbase[str[k+2]])
{
if(']'!=strbase[str[k+3]])
return 0;
else
return (cnt>>2)*3-2;
}
else
{
strout[j+1]=(strbase[str[k+1]]<<4)|(strbase[str[k+2]]>>2);
if(']'!=strbase[str[k+3]])
{
strout[j+2]=(strbase[str[k+2]]<<6)|strbase[str[k+3]];
return (cnt>>2)*3;
}
else
return (cnt>>2)*3-1;
}
}
////////////////////////////////////
HadPtr::HadPtr(void* p):m_ptr(p)
{
}
HadPtr::~HadPtr()
{
if(m_ptr)
{
free(m_ptr);
m_ptr=0;
}
}
void HadPtr::Attach(void* p)
{
if(m_ptr)
free(m_ptr);
m_ptr=p;
}
void* HadPtr::GetPtr()
{
return m_ptr;
}
const void* HadPtr::GetPtr()const
{
return m_ptr;
}
void* HadPtr::Detach()
{
void* pret=m_ptr;
m_ptr=0;
return pret;
}
- 1
- 2
- 3
- 4
前往页