Files
Work/uuencode/uuencode.cpp
2024-08-07 09:16:27 -04:00

75 lines
2.2 KiB
C++

#include <uuencode/uuencode.hpp>
#include <common/StringBuffer.hpp>
#include <common/file.hpp>
bool UUEncode::encode(Array<BYTE> &bytes,const String &strName,Block<String> &encodedLines)
{
File inFile;
int readCount;
char readBuff[45];
int byteIndex;
int byteCount;
char *ptrBuff;
encodedLines.remove();
encodedLines.insert(&String(String("begin 644 ")+strName));
byteIndex=0;
while(readCount=read(bytes,byteIndex,readBuff,sizeof(readBuff)))
{
StringBuffer stringBuffer;
stringBuffer.append(chEncode(readCount));
for(ptrBuff=readBuff,byteCount=readCount;byteCount>0;byteCount-=3,ptrBuff+=3)
{
stringBuffer.append(chEncode(*ptrBuff>>2));
stringBuffer.append(chEncode((*ptrBuff<<4)&0x30|(ptrBuff[1]>>4)&0x0F));
stringBuffer.append(chEncode((ptrBuff[1]<<2)&0x3C|(ptrBuff[2]>>6)&0x03));
stringBuffer.append(chEncode(ptrBuff[2]&0x3F));
}
encodedLines.insert(&stringBuffer.toString());
if(readCount!=sizeof(readBuff))break;
}
encodedLines.insert(&String("'"));
encodedLines.insert(&String("end"));
return encodedLines.size()?true:false;
}
String UUEncode::encode(Array<BYTE> &bytes,const String &strName)
{
StringBuffer stringBuffer;
File inFile;
int readCount;
char readBuff[45];
int byteIndex;
int byteCount;
char *ptrBuff;
stringBuffer.append(String("begin 644 ")+strName+String("\n"));
byteIndex=0;
while(readCount=read(bytes,byteIndex,readBuff,sizeof(readBuff)))
{
stringBuffer.append(chEncode(readCount));
for(ptrBuff=readBuff,byteCount=readCount;byteCount>0;byteCount-=3,ptrBuff+=3)
{
stringBuffer.append(chEncode(*ptrBuff>>2));
stringBuffer.append(chEncode((*ptrBuff<<4)&0x30|(ptrBuff[1]>>4)&0x0F));
stringBuffer.append(chEncode((ptrBuff[1]<<2)&0x3C|(ptrBuff[2]>>6)&0x03));
stringBuffer.append(chEncode(ptrBuff[2]&0x3F));
}
stringBuffer.append("\n");
if(readCount!=sizeof(readBuff))break;
}
stringBuffer.append("`\n");
stringBuffer.append("end\n");
return stringBuffer.toString();
}
DWORD UUEncode::read(Array<BYTE> &bytes,int &byteIndex,char *readBuff,int sizeBuff)
{
if(byteIndex+sizeBuff>bytes.size())sizeBuff=bytes.size()-byteIndex;
::memcpy(readBuff,&bytes[byteIndex],sizeBuff);
byteIndex+=sizeBuff;
return sizeBuff;
}