JoinFile.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // JoinFile.cpp
  3. // JOINFILE
  4. //
  5. // Created by mac on 14-11-21.
  6. // Copyright (c) 2014年 mac. All rights reserved.
  7. //
  8. #include "JoinFile.h"
  9. #include <assert.h>
  10. #include "json.h"
  11. using namespace std;
  12. JoinFile* JoinFile::s_sharedJoinFile = NULL;
  13. JoinFile::JoinFile() {
  14. }
  15. JoinFile::~JoinFile()
  16. {
  17. }
  18. JoinFile* JoinFile::getInstance()
  19. {
  20. if (s_sharedJoinFile == NULL)
  21. {
  22. s_sharedJoinFile = new JoinFile();
  23. }
  24. return s_sharedJoinFile;
  25. }
  26. unsigned char* JoinFile::getFileData(const char* pszFilePath, const char* pszMode, unsigned long * pSize)
  27. {
  28. unsigned char * pBuffer = NULL;
  29. assert(pszFilePath != NULL && pSize != NULL && pszMode != NULL);
  30. *pSize = 0;
  31. // read the file from hardware
  32. FILE *fp = fopen(pszFilePath, pszMode);
  33. assert(fp);
  34. fseek(fp,0,SEEK_END);
  35. *pSize = ftell(fp);
  36. fseek(fp,0,SEEK_SET);
  37. pBuffer = new unsigned char[*pSize];
  38. *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
  39. fclose(fp);
  40. if (! pBuffer)
  41. {
  42. std::string msg = "Get data from file(";
  43. msg.append(pszFilePath).append(") failed!");
  44. printf("%s", msg.c_str());
  45. }
  46. return pBuffer;
  47. }
  48. void JoinFile::doJoinFile(const char* pszFromFilePath,const char* pszToFilePath,const char* type,const char* tmpName,const char* projectPath)
  49. {
  50. string fromFilePath=string(pszFromFilePath);
  51. string toFilePath=string(pszToFilePath);
  52. int backslashIndex;
  53. backslashIndex = (int)fromFilePath.find_last_of('/');
  54. string fromfilename = fromFilePath.substr(backslashIndex+1,-1);
  55. backslashIndex = (int)toFilePath.find_last_of('/');
  56. string tofilename = toFilePath.substr(backslashIndex+1,-1);
  57. const char* readMode=NULL;
  58. const char* appendMode=NULL;
  59. if (strcmp(type,"b") == 0) {
  60. readMode="rb";
  61. appendMode="ab+";
  62. }else{
  63. readMode="r";
  64. appendMode="a+";
  65. }
  66. //读取pszToFileName大小
  67. unsigned long tarFileSize = 0;
  68. getFileData(pszToFilePath, appendMode, &tarFileSize);
  69. unsigned long nSize = 0;
  70. unsigned char *pBuffer =getFileData(pszFromFilePath,readMode, &nSize);
  71. FILE* tarFile = fopen(pszToFilePath, appendMode);
  72. assert(tarFile);
  73. //mp3文件不加密
  74. backslashIndex = (int)fromfilename.find_last_of('.');
  75. string ext = fromfilename.substr(backslashIndex+1,-1);
  76. if (strcmp(ext.c_str(), "mp3"))
  77. {
  78. char key[256];
  79. sprintf(key,"%s%s",_packname,fromfilename.c_str());
  80. rc4_crypt(key,pBuffer,nSize);
  81. }
  82. fwrite(pBuffer, sizeof(unsigned char), nSize, tarFile);
  83. fclose(tarFile);
  84. //修改配置文件
  85. unsigned long len=0;
  86. char confFilePath[200];
  87. sprintf(confFilePath, "%s/%s",projectPath,"assets_tmp/filesconf.json");
  88. auto data =getFileData(confFilePath, "a+", &len);
  89. char key[256];
  90. sprintf(key,"%s%s",_packname,"filesconf.json");
  91. rc4_crypt(key,data,len);
  92. Json::Value root;
  93. // 解析json用Json::Reader
  94. Json::Reader reader;
  95. // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array...
  96. if (!reader.parse((char *)data, (char *)data+len, root))
  97. {
  98. }
  99. std::string out1= root.toStyledString();
  100. printf("111%s",out1.c_str());
  101. Json::Value arrayObj; // 构建对象
  102. if (!root[fromfilename.c_str()].isNull()) {
  103. arrayObj=root[fromfilename.c_str()];
  104. }
  105. Json::Value new_item, new_item1;
  106. new_item["tarfile"]=tofilename.c_str();
  107. new_item["name"] = fromfilename.c_str();
  108. new_item["start"] = (unsigned int)tarFileSize;
  109. new_item["len"] = (unsigned int)nSize;
  110. new_item["type"] = type;
  111. new_item["tmpname"] = tmpName;
  112. arrayObj.append(new_item);
  113. root[fromfilename.c_str()] = arrayObj; // 插入原json中
  114. std::string out = root.toStyledString();
  115. printf("%s",out.c_str());
  116. unsigned long conflen=strlen(out.c_str());
  117. FILE* _fileconf = fopen(confFilePath, "w+");
  118. unsigned char *buffer=(unsigned char *)out.c_str();
  119. rc4_crypt(key,buffer,conflen);
  120. fwrite(buffer, sizeof(unsigned char), conflen, _fileconf);
  121. fclose(_fileconf);
  122. }
  123. void JoinFile::setPackName(const char* packname){
  124. _packname=packname;
  125. }
  126. void JoinFile::rc4_crypt(char *key, unsigned char *Data, unsigned long Len) { //加解密
  127. // return;
  128. int x,y,j=0;
  129. int box[256];
  130. for (int i = 0; i < 256; ++i)
  131. {
  132. box[i]=i;
  133. }
  134. for (int i = 0; i < 256; ++i)
  135. {
  136. j=(key[i % strlen(key)] + box[i] + j)%256;
  137. x=box[i];
  138. box[i]=box[j];
  139. box[j]=x;
  140. }
  141. for (int i = 0; i < Len; ++i)
  142. {
  143. y = i%256;
  144. j = (box[y] + j) %256;
  145. x = box[y];
  146. box[y] = box[j];
  147. box[j] = x;
  148. Data[i]=Data[i] ^ box[(box[y] + box[j]) %256];
  149. }
  150. }
  151. void JoinFile::createFileFromConf(const char * projectPath)
  152. {
  153. unsigned long len=0;
  154. char confFilePath[200];
  155. sprintf(confFilePath, "%s/%s",projectPath,"assets_tmp/filesconf.json");
  156. auto data =getFileData(confFilePath, "r+", &len);
  157. char key[256];
  158. sprintf(key,"%s%s",_packname,"filesconf.json");
  159. rc4_crypt(key,data,len);
  160. Json::Value root;
  161. // 解析json用Json::Reader
  162. Json::Reader reader;
  163. // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array...
  164. if (reader.parse((char *)data, (char *)data+len, root))
  165. {
  166. }
  167. Json::Value::Members member = root.getMemberNames();
  168. for(Json::Value::Members::iterator iter = member.begin(); iter != member.end(); ++iter) {
  169. int _size = root[(*iter)].size(); // 得到"files"的数组个数
  170. for (int i=0; i<_size; i++) {
  171. int filestart=root[(*iter)][i]["start"].asInt() ;
  172. int filelen=root[(*iter)][i]["len"].asInt() ;
  173. string tarfile=root[(*iter)][i]["tarfile"].asString();
  174. string type=root[(*iter)][i]["type"].asString();
  175. string name=root[(*iter)][i]["name"].asString();
  176. const char * readmode=NULL;
  177. const char * writemode=NULL;
  178. if (type=="b") {
  179. readmode="rb";
  180. writemode="wb+";
  181. }else{
  182. readmode="r";
  183. writemode="w+";
  184. }
  185. char tarPath[100]={'\0'};
  186. sprintf(tarPath, "%s/%s/%s",projectPath,"assets_tmp",tarfile.c_str());
  187. FILE* tarFile = fopen(tarPath, readmode);
  188. char caifenPath1[100]={'\0'};
  189. sprintf(caifenPath1, "%s/%s/%s",projectPath,"caifen",name.c_str());
  190. FILE* caifen1 = fopen(caifenPath1, writemode);
  191. unsigned char *buffer_chaifen1=NULL;
  192. fseek(tarFile,filestart,SEEK_SET);
  193. buffer_chaifen1 = new unsigned char[filelen];
  194. fread(buffer_chaifen1,sizeof(unsigned char), filelen,tarFile);
  195. int backslashIndex = (int)name.find_last_of('.');
  196. string ext = name.substr(backslashIndex+1,-1);
  197. if (strcmp(ext.c_str(), "mp3"))
  198. {
  199. char key[256];
  200. sprintf(key,"%s%s",_packname,name.c_str());
  201. rc4_crypt(key,buffer_chaifen1,filelen);
  202. }
  203. fwrite(buffer_chaifen1, sizeof(unsigned char), filelen, caifen1);
  204. fclose(tarFile);
  205. fclose(caifen1);
  206. }
  207. }
  208. }
  209. void JoinFile::test(){
  210. char * pszFromFilePath = "/Users/zhangyuntao/Desktop/rc4Test/1235678.txt";
  211. unsigned long nSize = 0;
  212. unsigned char *pBuffer =getFileData(pszFromFilePath,"rb", &nSize);
  213. rc4_crypt("abc", pBuffer, nSize);
  214. rc4_crypt("abc", pBuffer, nSize);
  215. printf("%s",pBuffer);
  216. }
  217. void JoinFile::encryptSingleFile(std::string srcPath, std::string dstPath, std::string mode){
  218. unsigned long nSize = 0;
  219. unsigned char *pBuffer =getFileData(srcPath.c_str(),("r"+mode).c_str(), &nSize);
  220. rc4_crypt((char*)_packname, pBuffer, nSize);
  221. FILE* pf = fopen(dstPath.c_str(), ("w"+mode).c_str());
  222. fwrite(pBuffer, sizeof(unsigned char), nSize, pf);
  223. fclose(pf);
  224. }