122 lines
3.0 KiB
C++
122 lines
3.0 KiB
C++
#include <yproxy/ydecode.hpp>
|
|
#include <common/pathfnd.hpp>
|
|
#include <common/profile.hpp>
|
|
#include <common/console.hpp>
|
|
#include <common/file.hpp>
|
|
#include <common/array.hpp>
|
|
#include <yproxy/yheader.hpp>
|
|
#include <yproxy/pdecode.hpp>
|
|
|
|
bool YDecoder::isYEnc(const String &pathFileName)
|
|
{
|
|
YHeader yHeader;
|
|
String strLine;
|
|
File inFile;
|
|
bool haveHeader=false;
|
|
|
|
if(!inFile.open(pathFileName,"rb"))return false;
|
|
while(true)
|
|
{
|
|
if(!inFile.readLine(strLine))break;
|
|
if(yHeader.fromString(strLine))
|
|
{
|
|
haveHeader=true;
|
|
break;
|
|
}
|
|
}
|
|
if(!haveHeader)return false;
|
|
return true;
|
|
}
|
|
|
|
bool YDecoder::decode(const String &pathFileName,String &strPathImageFile,Method method)
|
|
{
|
|
File inFile;
|
|
File outFile;
|
|
String strLine;
|
|
YHeader yHeader;
|
|
PureDecoder pureDecoder;
|
|
String strPath;
|
|
bool haveHeader=false;
|
|
bool result=false;
|
|
Profile profile;
|
|
|
|
if(!inFile.open(pathFileName,"rb"))return false;
|
|
while(true)
|
|
{
|
|
if(!inFile.readLine(strLine))break;
|
|
if(yHeader.fromString(strLine))
|
|
{
|
|
haveHeader=true;
|
|
break;
|
|
}
|
|
}
|
|
if(!haveHeader)return false;
|
|
::unlink(strPath+yHeader.getName());
|
|
inFile.readLine(strLine);
|
|
strPath=pathFileName;
|
|
if(!Profile::makeDirectoryName(strPath))strPath=String();
|
|
else strPath+=String("\\");
|
|
outFile.open(strPath+yHeader.getName(),"w+b");
|
|
packFile(outFile,yHeader.getSize());
|
|
yHeader.setPart(0);
|
|
try
|
|
{
|
|
result=pureDecoder.yDecode(outFile.getFile(),inFile.getFile(),yHeader.getLine(),yHeader.getSize(),yHeader.getPart());
|
|
}
|
|
catch(...)
|
|
{
|
|
::printf("[YDecoder::decode] caught an exception decoding %s\n",pathFileName);
|
|
}
|
|
if(!result && AbortOnError==method)
|
|
{
|
|
outFile.close();
|
|
::unlink(strPath+yHeader.getName());
|
|
return false;
|
|
}
|
|
strPathImageFile=yHeader.getName();
|
|
outFile.close();
|
|
inFile.close();
|
|
return true;
|
|
}
|
|
|
|
void YDecoder::packFile(File &outFile,int byteCount)
|
|
{
|
|
Array<BYTE> bytes;
|
|
bytes.size(byteCount);
|
|
::memset(&bytes[0],255,byteCount);
|
|
outFile.write(&bytes[0],byteCount);
|
|
outFile.rewind();
|
|
}
|
|
|
|
void main(int argc,char**argv)
|
|
{
|
|
FindData findData;
|
|
String strPathImageFile;
|
|
String strPath;
|
|
String strPattern;
|
|
String strSeparator("\\");
|
|
|
|
if(argc!=3)
|
|
{
|
|
::printf("USAGE: <directory> <filemask>\n");
|
|
::printf("(ie) ydecode ..\\news *.uue\n");
|
|
return;
|
|
}
|
|
strPath=argv[1];
|
|
strPattern=argv[2];
|
|
::printf("%s%s%s\n",strPath.str(),strSeparator.str(),strPattern.str());
|
|
if(findData.findFirst(strPath+strSeparator+strPattern))
|
|
{
|
|
if(!YDecoder::decode(strPath+strSeparator+findData.fileName(),strPathImageFile,YDecoder::ForceDecode))
|
|
::printf("Decode failed for %s\n",String(strPath+strSeparator+findData.fileName()).str());
|
|
else ::printf("Decoded %s->%s\n",String(strPath+strSeparator+findData.fileName()).str(),strPathImageFile.str());
|
|
while(findData.findNext())
|
|
{
|
|
if(!YDecoder::decode(strPath+strSeparator+findData.fileName(),strPathImageFile,YDecoder::ForceDecode))
|
|
::printf("Decode failed for %s\n",String(strPath+strSeparator+findData.fileName()).str());
|
|
else ::printf("Decoded %s->%s\n",String(strPath+strSeparator+findData.fileName()).str(),strPathImageFile.str());
|
|
}
|
|
}
|
|
}
|
|
|