#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<io.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int x,y,dx,index;
int seedMap[8][7] = {
{0x4a, 0xd6, 0xca, 0x90, 0x67, 0xf7, 0x52},
{0x5e, 0x95, 0x23, 0x9f, 0x13, 0x11, 0x7e},
{0x47, 0x74, 0x3d, 0x90, 0xaa, 0x3f, 0x51},
{0xc6, 0x09, 0xd5, 0x9f, 0xfa, 0x66, 0xf9},
{0xf3, 0xd6, 0xa1, 0x90, 0xa0, 0xf7, 0xf0},
{0x1d, 0x95, 0xde, 0x9f, 0x84, 0x11, 0xf4},
{0x0e, 0x74, 0xbb, 0x90, 0xbc, 0x3f, 0x92},
{0x00, 0x09, 0x5b, 0x9f, 0x62, 0x66, 0xa1}
};
void init()
{
x=-1;
y=8;
dx=1;
index=-1;
}
int next_mask()
{
index+=1;
int ret = 0;
if(x<0)
{
dx=1;
y= ((8 - y) % 8);
ret = 0xc3;
}
else if(x>6)
{
dx=-1;
y=7-y;
ret = 0xd8;
}
else
{
ret = seedMap[y][x];
}
x+=dx;
if(index == 0x8000 || (index > 0x8000 && (index + 1) % 0x8000 == 0))
{
return next_mask();
}
return ret;
}
void conversion(string path,string outpath)
{
init();
FILE *fp = fopen(path.c_str(), "rb");
FILE *fout=fopen(outpath.c_str(),"wb");
fseek (fp, 0, SEEK_END);
int buf_len=ftell(fp);
fseek (fp, 0, 0);
int t=0;
for(int i=0;i<buf_len;i++)
{
t=fgetc(fp);
t = next_mask() ^ t;
fputc(t,fout);
}
fclose(fp);
fclose(fout);
printf("Output %s success!\n",outpath.c_str());
}
int main(int argc, char *argv[] )
{
if(argc==1)
{
string arg = argv[0];
string path = arg.substr(0,arg.find_last_of("\\"));
string dir=path+"\\flac";
if (access(dir.c_str(), 0) == -1)
{
int flag=mkdir(dir.c_str());
}
string inPath = path+ "\\*.qmcflac";
long handle;
struct _finddata_t fileinfo;
handle = _findfirst(inPath.c_str(),&fileinfo);
if(handle == -1)
{
printf("No qmcflac file found!\n");
system("pause");
return -1;
}
do
{
string name = fileinfo.name;
name = name.substr(0,name.find_last_of('.'));
string outname = path+"\\flac\\"+name+".flac";
string inname=path+"\\"""+fileinfo.name;
conversion(inname,outname);
} while (!_findnext(handle,&fileinfo));
_findclose(handle);
system("pause");
return 0;
}
string filename = argv[1];
int index = filename.find_last_of('.');
string filetype = filename.substr(index,filename.length()-index);
transform(filetype.begin(), filetype.end(), filetype.begin(), ::tolower);
if(filetype.compare(".qmcflac"))
{
printf("Please input qmcflac file!\n");
system("pause");
return 0;
}
string outfile = filename.substr(0,index)+".flac";
init();
conversion(filename,outfile);
system("pause");
return 0;
}