73 lines
1.6 KiB
C++
73 lines
1.6 KiB
C++
#include <guitar/uutool.hpp>
|
|
#include <common/string.hpp>
|
|
#include <common/stdio.hpp>
|
|
|
|
String UUTool::encode(String strLine)
|
|
{
|
|
int stringLength=strLine.length();
|
|
char *ptrBuff;
|
|
int byteCount;
|
|
|
|
if(!stringLength)return String();
|
|
String lineString;
|
|
char *ptrLine=lineString.str();
|
|
*(ptrLine++)=chEncode(stringLength);
|
|
for(ptrBuff=strLine.str(),byteCount=stringLength;byteCount>0;byteCount-=3,ptrBuff+=3)
|
|
{
|
|
*(ptrLine++)=chEncode(*ptrBuff>>2);
|
|
*(ptrLine++)=chEncode((*ptrBuff<<4)&0x30|(ptrBuff[1]>>4)&0x0F);
|
|
*(ptrLine++)=chEncode((ptrBuff[1]<<2)&0x3C|(ptrBuff[2]>>6)&0x03);
|
|
*(ptrLine++)=chEncode(ptrBuff[2]&0x3F);
|
|
}
|
|
return lineString;
|
|
}
|
|
|
|
String UUTool::decode(String strLine)
|
|
{
|
|
int stringLength=strLine.length();
|
|
char *ptrBuff;
|
|
char *ptrLine;
|
|
int byteCount;
|
|
int n;
|
|
char ch;
|
|
|
|
if(!stringLength)return String();
|
|
String lineString;
|
|
ptrLine=lineString.str();
|
|
ptrBuff=strLine.str();
|
|
n=chDecode(*(ptrBuff));
|
|
if(n<=0)return String();
|
|
for(++ptrBuff;n>0;ptrBuff+=4,n-=3)
|
|
{
|
|
if(n>=3)
|
|
{
|
|
ch=chDecode(ptrBuff[0])<<2|chDecode(ptrBuff[1])>>4;
|
|
*(ptrLine++)=ch;
|
|
ch=chDecode(ptrBuff[1])<<4|chDecode(ptrBuff[2])>>2;
|
|
*(ptrLine++)=ch;
|
|
ch=chDecode(ptrBuff[2])<<6|chDecode(ptrBuff[3]);
|
|
*(ptrLine++)=ch;
|
|
}
|
|
else
|
|
{
|
|
if(n>=1)
|
|
{
|
|
ch=chDecode(ptrBuff[0])<<2|chDecode(ptrBuff[1])>>4;
|
|
(*ptrLine++)=ch;
|
|
}
|
|
else if(n>=2)
|
|
{
|
|
ch=chDecode(ptrBuff[1])<<4|chDecode(ptrBuff[2])>>2;
|
|
(*ptrLine++)=ch;
|
|
}
|
|
else if(n>=3)
|
|
{
|
|
ch=chDecode(ptrBuff[2])<<6|chDecode(ptrBuff[3]);
|
|
(*ptrLine++)=ch;
|
|
}
|
|
}
|
|
}
|
|
return lineString;
|
|
}
|
|
|