int EncodeQuoted(const unsigned char* pSrc, char* pDst, int nSrcLen, int nMaxLineLen)
{
int nDstLen; // 输出的字符计数
int nLineLen; // 输出的行长度计数
nDstLen = 0;
nLineLen = 0;
for (int i = 0; i < nSrcLen; i++, pSrc++)
{
// ASCII 33-60, 62-126原样输出,其余的需编码
if ((*pSrc >= '!') && (*pSrc <= '~') && (*pSrc != '='))
{
*pDst++ = (char)*pSrc;
nDstLen++;
nLineLen++;
}
else
{
sprintf(pDst, "=%02X", *pSrc);
pDst += 3;
nDstLen += 3;
nLineLen += 3;
}
// 输出换行?
if (nLineLen >= nMaxLineLen - 3)
{
sprintf(pDst, "=\r\n");
pDst += 3;
nDstLen += 3;
nLineLen = 0;
}
}
// 输出加个结束符
*pDst = '\0';
return nDstLen;
}
Quoted-Printable解码很简单,将编码过程反过来就行了。
int DecodeQuoted(const char* pSrc, unsigned char* pDst, int nSrcLen)
{
int nDstLen; // 输出的字符计数
int i;
i = 0;
nDstLen = 0;
while (i < nSrcLen)
{
if (strncmp(pSrc, "=\r\n", 3) == 0) // 软回车,跳过
{
pSrc += 3;
i += 3;
}
else
{
if (*pSrc == '=') // 是编码字节
{
sscanf(pSrc, "=%02X", pDst);
pDst++;
pSrc += 3;
i += 3;
}
else // 非编码字节
{
*pDst++ = (unsigned char)*pSrc++;
i++;
}
nDstLen++;
}
}
// 输出加个结束符
*pDst = '\0';
return nDstLen;
}
//---------------------------------------------------------------------------
//用HZ进行编码
void __fastcall EncodeHZ(void)
{
unsigned int x=0,i=0;
unsigned char hz;
while((hz=*FInputText++)!='\0')
{
if(hz>=161)
{
if(!x)
{
*(FOutputText+i)='~';
i++;
*(FOutputText+i)='{';
i++;
x=1;
}
else x=1;
*(FOutputText+i)=(char)(hz-128);
i++;
}
else
{
if(x)
{
*(FOutputText+i)='~';
i++;
*(FOutputText+i)='}';
i++;
x=0;
}
else x=0;
*(FOutputText+i)=hz;
i++;
}
}
if(x)
{
*(FOutputText+i)='~';
i++;
*(FOutputText+i)='}';
i++;
x=0;
}
*(FOutputText+i)='\0';
FOutput=(AnsiString)FOutputText;
}
//---------------------------------------------------------------------------
//对HZ进行解码
void __fastcall DecodeHZ(void)
{
unsigned i=0;
unsigned char hz;
while((hz=*FInputText++)!='\0')
{
loop2:
if(hz==126)
if((hz=*FInputText++)!='\0')
if(hz==123)
while((hz=*FInputText++)!='\0')
{
loop3:
if(hz==126)
{
if((hz=*FInputText++)!='\0')
if(hz==125) goto loop;
else {*(FOutputText+i)=(char)254;i++;goto loop3;}
}
else {*(FOutputText+i)=(char)(hz+128);i++;}
}
else {*(FOutputText+i)='~';i++;goto loop2;}
else {*(FOutputText+i)='~';i++;goto end;}
else {*(FOutputText+i)=hz;i++;}
loop:;
}
end:;
*(FOutputText+i)='\0';
FOutput=(AnsiString)FOutputText;
}
//---------------------------------------------------------------------------
//用QP进行编码
void __fastcall EncodeQP(void)
{
unsigned char first,second,sour;
unsigned int i=0;
while((sour=*FInputText++)!='\0')
{
if(sour==61)
{
*(FOutputText+i)='=';
i++;
*(FOutputText+i)='3';
i++;
*(FOutputText+i)='D';
i++;
}
else
{
if(sour>127)
{
first=sour>>4;
second=sour&15;
if(first>9) first+=55;
else first+=48;
if(second>9) second+=55;
else second+=48;
*(FOutputText+i)='=';
i++;
*(FOutputText+i)=first;
i++;
*(FOutputText+i)=second;
i++;
}
else {*(FOutputText+i)=sour;i++;}
}
}
*(FOutputText+i)='\0';
FOutput=(AnsiString)FOutputText;
}
//---------------------------------------------------------------------------
//对QP进行解码
void __fastcall DecodeQP(void)
{
unsigned char first,second,sour;
unsigned int i=0;
while((sour=*FInputText++)!='\0')
{
if(sour==61)
{
if((first=*FInputText++)=='\0') {*(FOutputText+i)=sour;i++;break;}
else if((first<48)||((first>57)&&(first<65))||(first>70))
{
*(FOutputText+i)=sour;
i++;
*(FOutputText+i)=first;
i++;
continue;
}
else if((second=*FInputText++)=='\0')
{
*(FOutputText+i)=sour;
i++;
*(FOutputText+i)=first;
i++;
break;
}
else if((second<48)||((second>57)&&(second<65))||(second>70))
{
*(FOutputText+i)=sour;
i++;
*(FOutputText+i)=first;
i++;
*(FOutputText+i)=second;
i++;
continue;
}
if(first>=65) first-=55;
else first-=48;
if(second>=65) second-=55;
else second-=48;
sour=NULL;
sour=first<<4;
sour|=second;
}
*(FOutputText+i)=sour;
i++;
}
*(FOutputText+i)='\0';
FOutput=(AnsiString)FOutputText;
}
//---------------------------------------------------------------------------
//用Base64进行编码
void __fastcall EncodeBase64(void)
{
unsigned char *sour,*obje,*inputtext;
unsigned int i,j=0,m=57,n=57;
inputtext=FInputText;
sour=new char[57];
obje=new char[76];
while(m==57)
{
m=strlen(inputtext);
if(m<57)
{
if(m==0) break;
n=m;
strncpy(sour,inputtext,m);
if(n%3) n=n-n%3+3;
for(i=m;i<n;i++)
*(sour+i)=NULL;
}
else
{
m=57;
strncpy(sour,inputtext,m);
}
inputtext+=m;
for(i=0;i<n/3;i++)
Base64(sour+i*3,obje+i*4);
strncpy(FOutputText+j,obje,n/3+m); //n/3*4-(n-m)
j+=n/3+m;
for(i=0;i<(n-m);i++,j++)
*(FOutputText+j)='=';
*(FOutputText+j++)='\n';
}
*(FOutputText+j)='\0';
FOutput=(AnsiString)FOutputText;
delete []sour;
delete []obje;
}
//---------------------------------------------------------------------------
void __fastcall Base64(unsigned char chasc[3],unsigned char chuue[4])
/*
chasc:未编码的二进制代码
chuue:编码过的Base64代码
*/
{
unsigned int i,k=2;
unsigned char t=NULL;
for(i=0;i<3;i++)
{
*(chuue+i)=*(chasc+i)>>k;
*(chuue+i)|=t;
t=*(chasc+i)<<(8-k);
t>>=2;
k+=2;
}
*(chuue+3)=*(chasc+2)&63;
for(i=0;i<4;i++)
if((*(chuue+i)>=0)&&(*(chuue+i)<=25)) *(chuue+i)+=65;
else if((*(chuue+i)>=26)&&(*(chuue+i)<=51)) *(chuue+i)+=71;
else if((*(chuue+i)>=52)&&(*(chuue+i)<=61)) *(chuue+i)-=4;
else if(*(chuue+i)==62) *(chuue+i)=43;
else if(*(chuue+i)==63) *(chuue+i)=47;
}
//---------------------------------------------------------------------------
//对Base64解码
void __fastcall DecodeBase64(void)
{
unsigned char *sour,*obje,*inputtext;
unsigned int i,j=0,m=57,n=76;
sour=new char[76];
obje=new char[57];
inputtext=FInputText;
while(m==57)
{
while(*inputtext=='\r'||*inputtext=='\n')
inputtext++;
m=strlen(inputtext);
if(m<76)
{
if(m==0) break;
n=m;
}
m=n/4*3;
strncpy(sour,inputtext,n);
inputtext+=n;
for(i=0;i<n/4;i++)
unBase64(sour+i*4,obje+i*3);
strncpy(FOutputText+j,obje,m);
j+=m;
}
*(FOutputText+j)='\0';
FOutput=(AnsiString)FOutputText;
delete []sour;
delete []obje;
}
//---------------------------------------------------------------------------
/*Base64解码*/
void __fastcall unBase64(unsigned char chuue[4],unsigned char chasc[3])
/*
chuue:未解码的Base64代码
chasc:解码过的二进制代码
*/
{
int i,k=2;
unsigned char t=NULL;
for(i=0;i<4;i++)
if((*(chuue+i)>=65)&&(*(chuue+i)<=90)) *(chuue+i)-=65;
else if((*(chuue+i)>=97)&&(*(chuue+i)<=122)) *(chuue+i)-=71;
else if((*(chuue+i)>=48)&&(*(chuue+i)<=57)) *(chuue+i)+=4;
else if(*(chuue+i)==43) *(chuue+i)=62;
else if(*(chuue+i)==47) *(chuue+i)=63;
else if(*(chuue+i)==61) *(chuue+i)=0;
for(i=0;i<3;i++)
{
*(chasc+i)=*(chuue+i)<<k;
k+=2;
t=*(chuue+i+1)>>8-k;
*(chasc+i)|=t;
}
}
//---------------------------------------------------------------------------
//用UUenCode进行编码
void __fastcall EncodeUue(void)
{
unsigned char *sour,*obje,*inputtext;
unsigned int i,j=0,m=45,n=45,num;
inputtext=FInputText;
sour=new char[45];
obje=new char[60];
while(m==45)
{
m=strlen(inputtext);
if(m<45)
{
if(m==0) break;
n=m;
strncpy(sour,inputtext,m);
if(n%3) n=n-n%3+3;
for(i=m;i<n;i++)
*(sour+i)=NULL;
}
else
{
m=45;
strncpy(sour,inputtext,m);
}
inputtext+=m;
for(i=0;i<n/3;i++)
Uue(sour+i*3,obje+i*4);
*(FOutputText+j++)=m+32;
strncpy(FOutputText+j,obje,n/3*4);
j+=n/3*4;
*(FOutputText+j++)='\n';
}
*(FOutputText+j++)='`';
*(FOutputText+j++)='\n';
*(FOutputText+j)='\0';
FOutput=(AnsiString)FOutputText;
delete []sour;
delete []obje;
}
//---------------------------------------------------------------------------
/*Uuencode编码*/
void __fastcall Uue(unsigned char chasc[3],unsigned char chuue[4])
/*
chasc:未编码的二进制代码
chuue:编码过的Uue代码
*/
{
int i,k=2;
unsigned char t=NULL;
for(i=0;i<3;i++)
{
*(chuue+i)=*(chasc+i)>>k;
*(chuue+i)|=t;
if(*(chuue+i)==NULL) *(chuue+i)+=96;
else *(chuue+i)+=32;
t=*(chasc+i)<<(8-k);
t>>=2;
k+=2;
}
*(chuue+3)=*(chasc+2)&63;
if(*(chuue+3)==NULL) *(chuue+3)+=96;
else *(chuue+3)+=32;
}
//---------------------------------------------------------------------------
//对UUenCode解码
void __fastcall DecodeUue(void)
{
unsigned char *sour,*obje,*inputtext;
unsigned int i,j=0,m=45,n;
sour=new char[60];
obje=new char[45];
inputtext=FInputText;
while(m==45)
{
while(*inputtext=='\r'||*inputtext=='\n')
inputtext++;
if((m=*inputtext)!=96||m=='\0')
{
n=m-32;
m=n;
inputtext++;
}
else break;
if(n<45)
{
if(n%3) n=(n-n%3+3)/3*4;
else n=n/3*4;
}
else
n=n/3*4;
strncpy(sour,inputtext,n);
inputtext+=n;
for(i=0;i<n/4;i++)
unUue(sour+i*4,obje+i*3);
strncpy(FOutputText+j,obje,m);
j+=m;
}
*(FOutputText+j)='\0';
FOutput=(AnsiString)FOutputText;
delete []sour;
delete []obje;
}
//---------------------------------------------------------------------------
/*Uuencode解码*/
void __fastcall unUue(unsigned char chuue[4],unsigned char chasc[3])
/*
chuue:未解码的Uue代码
chasc:解码过的二进制代码
*/
{
int i,k=2;
unsigned char t=NULL;
if(*chuue==96) *chuue=NULL;
else *chuue-=32;
for(i=0;i<3;i++)
{
*(chasc+i)=*(chuue+i)<<k;
k+=2;
if(*(chuue+i+1)==96) *(chuue+i+1)=NULL;
else *(chuue+i+1)-=32;
t=*(chuue+i+1)>>8-k;
*(chasc+i)|=t;
}
}
//---------------------------------------------------------------------------
//用XXenCode进行编码
void __fastcall EncodeXxe(void)
{
unsigned char *sour,*obje,*inputtext;
unsigned int i,j=0,m=45,n=45;
char set[]="+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
inputtext=FInputText;
sour=new char[45];
obje=new char[60];
while(m==45)
{
m=strlen(inputtext);
if(m<45)
{
if(m==0) break;
n=m;
strncpy(sour,inputtext,m);
if(n%3) n=n-n%3+3;
for(i=m;i<n;i++)
*(sour+i)=NULL;
}
else
{
m=45;
strncpy(sour,inputtext,m);
}
inputtext+=m;
for(i=0;i<n/3;i++)
Xxe(sour+i*3,obje+i*4);
*(FOutputText+j++)=set[m&63];
strncpy(FOutputText+j,obje,n/3*4);
j+=n/3*4;
*(FOutputText+j++)='\n';
}
*(FOutputText+j++)='+';
*(FOutputText+j++)='\n';
*(FOutputText+j)='\0';
FOutput=(AnsiString)FOutputText;
delete []sour;
delete []obje;
}
//---------------------------------------------------------------------------
/*Xxencode编码*/
void __fastcall Xxe(unsigned char chasc[3],unsigned char chxxe[4])
/*
chasc:未编码的二进制代码
chxxe:编码过的Xxe代码
*/
{
int i;
char set[]="+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
chxxe[0]=chasc[0]>>2;
chxxe[1]=(chasc[0]<<4)&48|(chasc[1]>>4)&15;
chxxe[2]=(chasc[1]<<2)&60|(chasc[2]>>6)&3;
chxxe[3]=chasc[2]&63;
for(i=0;i<4;i++) chxxe[i]=set[chxxe[i]]; /*查表*/
}
/*需注意的是,Xxencode文件正文部分中每一行的第一个字母是:从源文件中实际 读取的字符数的ASCII值取后六位后用set[]查表得到的。*/
//---------------------------------------------------------------------------
//对XXenCode解码
void __fastcall DecodeXxe(void)
{
unsigned char *sour,*obje,*inputtext;
unsigned int i,j=0,m=45,n;
sour=new char[60];
obje=new char[45];
inputtext=FInputText;
while(m==45)
{
while(*inputtext=='\r'||*inputtext=='\n')
inputtext++;
if((m=*inputtext)!='+'||m=='\0')
{
n=set(m);
m=n;
inputtext++;
}
else break;
if(n<45)
{
if(n%3) n=(n-n%3+3)/3*4;
else n=n/3*4;
}
else n=n/3*4;
strncpy(sour,inputtext,n);
inputtext+=n;
for(i=0;i<n/4;i++)
unXxe(sour+i*4,obje+i*3);
strncpy(FOutputText+j,obje,m);
j+=m;
}
*(FOutputText+j)='\0';
FOutput=(AnsiString)FOutputText;
delete []sour;
delete []obje;
}
//---------------------------------------------------------------------------
/*Xxencode解码*/
unsigned char __fastcall set(unsigned char ch) /*查表函数*/
{
if(ch==43) ch=0;
else if(ch==45) ch=1;
else if(ch>=48&&ch<=57) ch-=46;
else if(ch>=65&&ch<=90) ch-=53;
else if(ch>=97&&ch<=122) ch-=59;
return ch;
}
//---------------------------------------------------------------------------
void __fastcall unXxe(unsigned char chxxe[4],unsigned char chasc[3])
/*
chxxe:未解码的Xxe代码
chasc:解码过的二进制代码
*/
{
int k=2 ,i;
unsigned char t;
t=NULL;
*chxxe=set(*chxxe);
for(i=0;i<3;i++)
{
*(chxxe+i+1)=set(*(chxxe+i+1));
*(chasc+i)=*(chxxe+i)<<k;
k+=2;
t=*(chxxe+i+1)>>8-k;
*(chasc+i)|=t;
}
}
本文地址:http://com.8s8s.com/it/it26898.htm