123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- //
- // JoinFile.cpp
- // JOINFILE
- //
- // Created by mac on 14-11-21.
- // Copyright (c) 2014年 mac. All rights reserved.
- //
- #include "JoinFile.h"
- #include <assert.h>
- #include "json.h"
- using namespace std;
- JoinFile* JoinFile::s_sharedJoinFile = NULL;
- JoinFile::JoinFile() {
-
- }
- JoinFile::~JoinFile()
- {
- }
- JoinFile* JoinFile::getInstance()
- {
- if (s_sharedJoinFile == NULL)
- {
- s_sharedJoinFile = new JoinFile();
- }
- return s_sharedJoinFile;
- }
- unsigned char* JoinFile::getFileData(const char* pszFilePath, const char* pszMode, unsigned long * pSize)
- {
- unsigned char * pBuffer = NULL;
- assert(pszFilePath != NULL && pSize != NULL && pszMode != NULL);
- *pSize = 0;
-
- // read the file from hardware
- FILE *fp = fopen(pszFilePath, pszMode);
- assert(fp);
-
- fseek(fp,0,SEEK_END);
- *pSize = ftell(fp);
- fseek(fp,0,SEEK_SET);
- pBuffer = new unsigned char[*pSize];
- *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
- fclose(fp);
-
- if (! pBuffer)
- {
- std::string msg = "Get data from file(";
- msg.append(pszFilePath).append(") failed!");
- printf("%s", msg.c_str());
- }
- return pBuffer;
- }
- void JoinFile::doJoinFile(const char* pszFromFilePath,const char* pszToFilePath,const char* type,const char* tmpName,const char* projectPath)
- {
- string fromFilePath=string(pszFromFilePath);
- string toFilePath=string(pszToFilePath);
- int backslashIndex;
- backslashIndex = (int)fromFilePath.find_last_of('/');
- string fromfilename = fromFilePath.substr(backslashIndex+1,-1);
- backslashIndex = (int)toFilePath.find_last_of('/');
- string tofilename = toFilePath.substr(backslashIndex+1,-1);
-
-
-
- const char* readMode=NULL;
- const char* appendMode=NULL;
- if (strcmp(type,"b") == 0) {
- readMode="rb";
- appendMode="ab+";
-
- }else{
- readMode="r";
- appendMode="a+";
- }
- //读取pszToFileName大小
- unsigned long tarFileSize = 0;
- getFileData(pszToFilePath, appendMode, &tarFileSize);
-
-
-
- unsigned long nSize = 0;
- unsigned char *pBuffer =getFileData(pszFromFilePath,readMode, &nSize);
- FILE* tarFile = fopen(pszToFilePath, appendMode);
- assert(tarFile);
- //mp3文件不加密
- backslashIndex = (int)fromfilename.find_last_of('.');
- string ext = fromfilename.substr(backslashIndex+1,-1);
- if (strcmp(ext.c_str(), "mp3"))
- {
- char key[256];
- sprintf(key,"%s%s",_packname,fromfilename.c_str());
- rc4_crypt(key,pBuffer,nSize);
- }
- fwrite(pBuffer, sizeof(unsigned char), nSize, tarFile);
- fclose(tarFile);
-
-
- //修改配置文件
- unsigned long len=0;
- char confFilePath[200];
- sprintf(confFilePath, "%s/%s",projectPath,"assets_tmp/filesconf.json");
- auto data =getFileData(confFilePath, "a+", &len);
-
- char key[256];
- sprintf(key,"%s%s",_packname,"filesconf.json");
-
- rc4_crypt(key,data,len);
-
-
- Json::Value root;
- // 解析json用Json::Reader
- Json::Reader reader;
- // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array...
- if (!reader.parse((char *)data, (char *)data+len, root))
- {
- }
- std::string out1= root.toStyledString();
- printf("111%s",out1.c_str());
- Json::Value arrayObj; // 构建对象
- if (!root[fromfilename.c_str()].isNull()) {
- arrayObj=root[fromfilename.c_str()];
- }
- Json::Value new_item, new_item1;
- new_item["tarfile"]=tofilename.c_str();
- new_item["name"] = fromfilename.c_str();
- new_item["start"] = (unsigned int)tarFileSize;
- new_item["len"] = (unsigned int)nSize;
- new_item["type"] = type;
- new_item["tmpname"] = tmpName;
- arrayObj.append(new_item);
- root[fromfilename.c_str()] = arrayObj; // 插入原json中
- std::string out = root.toStyledString();
- printf("%s",out.c_str());
- unsigned long conflen=strlen(out.c_str());
- FILE* _fileconf = fopen(confFilePath, "w+");
- unsigned char *buffer=(unsigned char *)out.c_str();
- rc4_crypt(key,buffer,conflen);
- fwrite(buffer, sizeof(unsigned char), conflen, _fileconf);
- fclose(_fileconf);
-
- }
- void JoinFile::setPackName(const char* packname){
- _packname=packname;
- }
- void JoinFile::rc4_crypt(char *key, unsigned char *Data, unsigned long Len) { //加解密
- // return;
- int x,y,j=0;
- int box[256];
- for (int i = 0; i < 256; ++i)
- {
- box[i]=i;
- }
- for (int i = 0; i < 256; ++i)
- {
- j=(key[i % strlen(key)] + box[i] + j)%256;
- x=box[i];
- box[i]=box[j];
- box[j]=x;
- }
- for (int i = 0; i < Len; ++i)
- {
- y = i%256;
- j = (box[y] + j) %256;
- x = box[y];
- box[y] = box[j];
- box[j] = x;
- Data[i]=Data[i] ^ box[(box[y] + box[j]) %256];
- }
- }
- void JoinFile::createFileFromConf(const char * projectPath)
- {
- unsigned long len=0;
- char confFilePath[200];
- sprintf(confFilePath, "%s/%s",projectPath,"assets_tmp/filesconf.json");
- auto data =getFileData(confFilePath, "r+", &len);
- char key[256];
- sprintf(key,"%s%s",_packname,"filesconf.json");
-
- rc4_crypt(key,data,len);
-
-
-
- Json::Value root;
-
- // 解析json用Json::Reader
- Json::Reader reader;
- // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array...
- if (reader.parse((char *)data, (char *)data+len, root))
- {
- }
- Json::Value::Members member = root.getMemberNames();
- for(Json::Value::Members::iterator iter = member.begin(); iter != member.end(); ++iter) {
- int _size = root[(*iter)].size(); // 得到"files"的数组个数
- for (int i=0; i<_size; i++) {
- int filestart=root[(*iter)][i]["start"].asInt() ;
- int filelen=root[(*iter)][i]["len"].asInt() ;
- string tarfile=root[(*iter)][i]["tarfile"].asString();
- string type=root[(*iter)][i]["type"].asString();
- string name=root[(*iter)][i]["name"].asString();
- const char * readmode=NULL;
- const char * writemode=NULL;
- if (type=="b") {
- readmode="rb";
- writemode="wb+";
- }else{
- readmode="r";
- writemode="w+";
- }
- char tarPath[100]={'\0'};
- sprintf(tarPath, "%s/%s/%s",projectPath,"assets_tmp",tarfile.c_str());
- FILE* tarFile = fopen(tarPath, readmode);
- char caifenPath1[100]={'\0'};
- sprintf(caifenPath1, "%s/%s/%s",projectPath,"caifen",name.c_str());
- FILE* caifen1 = fopen(caifenPath1, writemode);
- unsigned char *buffer_chaifen1=NULL;
- fseek(tarFile,filestart,SEEK_SET);
- buffer_chaifen1 = new unsigned char[filelen];
- fread(buffer_chaifen1,sizeof(unsigned char), filelen,tarFile);
- int backslashIndex = (int)name.find_last_of('.');
- string ext = name.substr(backslashIndex+1,-1);
- if (strcmp(ext.c_str(), "mp3"))
- {
- char key[256];
- sprintf(key,"%s%s",_packname,name.c_str());
-
- rc4_crypt(key,buffer_chaifen1,filelen);
- }
- fwrite(buffer_chaifen1, sizeof(unsigned char), filelen, caifen1);
- fclose(tarFile);
- fclose(caifen1);
- }
- }
- }
- void JoinFile::test(){
- char * pszFromFilePath = "/Users/zhangyuntao/Desktop/rc4Test/1235678.txt";
- unsigned long nSize = 0;
- unsigned char *pBuffer =getFileData(pszFromFilePath,"rb", &nSize);
-
- rc4_crypt("abc", pBuffer, nSize);
- rc4_crypt("abc", pBuffer, nSize);
- printf("%s",pBuffer);
- }
- void JoinFile::encryptSingleFile(std::string srcPath, std::string dstPath, std::string mode){
- unsigned long nSize = 0;
- unsigned char *pBuffer =getFileData(srcPath.c_str(),("r"+mode).c_str(), &nSize);
-
- rc4_crypt((char*)_packname, pBuffer, nSize);
- FILE* pf = fopen(dstPath.c_str(), ("w"+mode).c_str());
- fwrite(pBuffer, sizeof(unsigned char), nSize, pf);
- fclose(pf);
- }
|