写了一个UUE编码的类.
生活随笔
收集整理的這篇文章主要介紹了
写了一个UUE编码的类.
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前段時間翻了翻以前收集的一些文檔,看見了這個東西,于是想寫個類封裝一下.?
這個東西在電子郵件中用的比較多,詳細的情況自己搜索一下吧.
頭文件部分:
?
/**//*--------------------------------------------------------------------------------*///定義一些額外的信息,保存這些信息到文件中可以用winrar打開
#define?BEGIN_FLAG???????????"begin?"
#define?END_FLAG?????????????"' end"
#define?DEFAULT_ID???????????"777"
#define?DEFAULT_FILE_NAME????"UueEncodeFile.txt"
enum?UueOutMode?...{out_str,out_file};
class?CUuencode
...{
public:
????CUuencode(/**//*UueOutMode?Mode*/);
????void?SetFileModeInf(unsigned?char*?filename=(unsigned?char*)DEFAULT_FILE_NAME,unsigned?char*?ID=(unsigned?char*)DEFAULT_ID);
public:
????~CUuencode(void);
public:
????void?unUueChar?(unsigned?char?chuue[4],unsigned?char?chasc[3]);
????void?UueChar???(unsigned?char?chasc[3],unsigned?char?chuue[4]);
????DWORD?UueStr?(const?unsigned?char?ascstr[],size_t?asclen,unsigned?char?uuestr[],size_t?&uuelen,UueOutMode?mode=out_str);
????size_t?GetUueStrDataLen(const?unsigned?char?uuestr[]);
????DWORD?UnUueStr(const?unsigned?char?uuestr[],size_t?uuelen,unsigned?char?ascstr[],size_t?&asclen,UueOutMode?&mode);
private:
????UueOutMode?FMode;
????unsigned?char?*??FID;
????unsigned?char?*??FileName;
????size_t?????IDlen;
????size_t?????namelen;
????unsigned?char?*?beginFlag;
????unsigned?char?*?endFlag;
};
?
解碼的那部分未貼出來,因為當輸入的字符為非uue編碼時有可能會溢出,打算先考慮一下如何判斷輸入的為非UUE編碼,或者錯誤的編碼.
實現:
?
/**//*------------------------------------------------*?*??????????????????????????????????????????????????*
?*?????????????????Uue字符編碼????????????????????*
?*?????????????????2007-3-16??????????????????????*
?*??????????????????????????????????????????????????*
*------------------------------------------------*/
CUuencode::CUuencode(/**//*UueOutMode?Mode=out_str*/)
...{
????beginFlag=(unsigned?char*)BEGIN_FLAG;
????endFlag=??(unsigned?char*)END_FLAG;
}
void?CUuencode::SetFileModeInf(unsigned?char*?filename,unsigned?char*?ID)
...{
????
????FID=ID;
????FileName=filename;
????IDlen=_mbslen(FID);
????namelen=_mbslen(filename);
????if(IDlen==0)
????...{
????????_mbscpy(FID,(unsigned?char*)DEFAULT_ID);
????????IDlen=_mbslen(FID);
????}
????if(namelen==0)
????...{
????????_mbscpy(FileName,(unsigned?char*)DEFAULT_ID);
????????namelen=_mbslen((unsigned?char*)DEFAULT_FILE_NAME);
????}
}
CUuencode::~CUuencode()
...{
????//
}
/**//*Uuencode編碼*/
/**//*
chasc:未編碼的二進制代碼
chuue:編碼過的Uue代碼
*/
void?CUuencode::UueChar(unsigned?char?chasc[3],unsigned?char?chuue[4])
...{
????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解碼*/
/**//*
chuue:未解碼的Uue代碼
chasc:解碼過的二進制代碼
*/
void?CUuencode::unUueChar(unsigned?char?chuue[4],unsigned?char?chasc[3])
...{
????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;
????}
}?
//編碼一個ASC串為Uue串,先傳入NULL到uuestr,返回uuelen,未包含' DWORD??CUuencode::UueStr(const?unsigned?char?ascstr[],size_t?asclen,unsigned?char?uuestr[],size_t?&uuelen,UueOutMode?mode)
...{
????size_t?asclen_3=asclen+((asclen%3)?(3-(asclen%3)):0);?
????size_t?require=asclen_3;
????if(uuestr==NULL)
????...{
????????
????????if(mode==out_str)
????????????/**//*????|----DATA---|---------Line?Header?+' '-------|*/
????????...{????
????????????uuelen=?require*4/3+(asclen/45)*2+((asclen%45)?1:0)*2;
????????????return?0;
????????}
????????else
????????...{
????????????/**//*????|----DATA---|---------Line?Header?+' '-------|---"BEGIN?"----|------|-'?'-|-------|-'/n'-|---"' END"---*/
????????????uuelen=?require*4/3+(asclen/45)*2+((asclen%45)?1:0)*2+_mbslen(beginFlag)+IDlen+??1??+namelen+?1????+_mbslen(endFlag);
????????????return?0;
????????}
????????
????}
????unsigned?char?*temp=new?unsigned?char[asclen_3];
????//memory?alloc?fail
????if(temp==NULL)?return?GetLastError();
????unsigned?char?*pdel=temp;
????unsigned?char?*puue=uuestr;
????memset(temp,0,asclen_3);
????memcpy(temp,ascstr,asclen);
????if(mode==out_file)
????...{
????????_mbscpy(uuestr,beginFlag);
????????uuestr+=_mbslen(beginFlag);
????????_mbscpy(uuestr,FID);
????????uuestr+=IDlen;
????????*uuestr++='?';
????????_mbscpy(uuestr,FileName);
????????uuestr+=namelen;
????????
????????*uuestr++=' ';
????}
????for(size_t?i=0;i<asclen_3;)
????...{
????????if(0==i%45)
????????????*uuestr++=0x20+((asclen_3-i>=45)?45:(asclen_3-i));
????????UueChar(temp,uuestr);
????????temp+=3;
????????uuestr+=4;
????????if(!((i+3)%45))
????????...{
????????????*uuestr++=' ';
????????}
????????i+=3;
????}
????*uuestr++=' ';
????if(mode==out_file)
????...{
????????_mbscpy(uuestr,endFlag);
????}
????//*uuestr='
總結
以上是生活随笔為你收集整理的写了一个UUE编码的类.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《AutoCAD 2016中文版室内装潢
- 下一篇: NPOI获取Excel单元格自定义度分秒