CLodHandler.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "zlib.h"
  4. #include "CLodHandler.h"
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <cctype>
  8. #include <cstring>
  9. DLL_EXPORT int readNormalNr (int pos, int bytCon, unsigned char * str)
  10. {
  11. int ret=0;
  12. int amp=1;
  13. if (str)
  14. {
  15. for (int i=0; i<bytCon; i++)
  16. {
  17. ret+=str[pos+i]*amp;
  18. amp<<=8;
  19. }
  20. }
  21. else return -1;
  22. return ret;
  23. }
  24. unsigned char * CLodHandler::giveFile(std::string defName, int * length)
  25. {
  26. std::transform(defName.begin(), defName.end(), defName.begin(), (int(*)(int))toupper);
  27. Entry * ourEntry = entries.znajdz(Entry(defName));
  28. if(!ourEntry) //nothing's been found
  29. {
  30. std::cerr<<"Cannot find file: "<<defName;
  31. return NULL;
  32. }
  33. if(length) *length = ourEntry->realSize;
  34. fseek(FLOD, ourEntry->offset, 0);
  35. unsigned char * outp;
  36. if (ourEntry->offset<0) //file is in the sprites/ folder; no compression
  37. {
  38. unsigned char * outp = new unsigned char[ourEntry->realSize];
  39. char name[30];memset(name,0,30);
  40. strcat(name,"Sprites/");
  41. strcat(name,(char*)ourEntry->name);
  42. FILE * f = fopen(name,"rb");
  43. int result = fread(outp,1,ourEntry->realSize,f);
  44. if(result<0) {std::cout<<"Error in file reading: "<<name<<std::endl;delete[] outp; return NULL;}
  45. else
  46. return outp;
  47. }
  48. else if (ourEntry->size==0) //file is not compressed
  49. {
  50. outp = new unsigned char[ourEntry->realSize];
  51. fread((char*)outp, 1, ourEntry->realSize, FLOD);
  52. return outp;
  53. }
  54. else //we will decompress file
  55. {
  56. outp = new unsigned char[ourEntry->size];
  57. fread((char*)outp, 1, ourEntry->size, FLOD);
  58. fseek(FLOD, 0, 0);
  59. unsigned char * decomp = NULL;
  60. int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp);
  61. delete[] outp;
  62. return decomp;
  63. }
  64. return NULL;
  65. }
  66. int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
  67. {
  68. int ret;
  69. unsigned have;
  70. z_stream strm;
  71. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  72. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  73. /* allocate inflate state */
  74. strm.zalloc = Z_NULL;
  75. strm.zfree = Z_NULL;
  76. strm.opaque = Z_NULL;
  77. strm.avail_in = 0;
  78. strm.next_in = Z_NULL;
  79. ret = inflateInit2(&strm, wBits);
  80. if (ret != Z_OK)
  81. return ret;
  82. int chunkNumber = 0;
  83. do
  84. {
  85. int readBytes = 0;
  86. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  87. {
  88. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  89. ++readBytes;
  90. }
  91. ++chunkNumber;
  92. strm.avail_in = readBytes;
  93. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  94. /*if (in.bad())
  95. {
  96. (void)inflateEnd(&strm);
  97. return Z_ERRNO;
  98. }*/
  99. if (strm.avail_in == 0)
  100. break;
  101. strm.next_in = inx;
  102. /* run inflate() on input until output buffer not full */
  103. do
  104. {
  105. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  106. strm.next_out = outx;
  107. ret = inflate(&strm, Z_NO_FLUSH);
  108. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  109. switch (ret)
  110. {
  111. case Z_NEED_DICT:
  112. ret = Z_DATA_ERROR; /* and fall through */
  113. case Z_DATA_ERROR:
  114. case Z_MEM_ERROR:
  115. (void)inflateEnd(&strm);
  116. return ret;
  117. }
  118. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  119. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  120. {
  121. (void)inflateEnd(&strm);
  122. return Z_ERRNO;
  123. }*/
  124. out.write((char*)outx, have);
  125. if(out.bad())
  126. {
  127. (void)inflateEnd(&strm);
  128. return Z_ERRNO;
  129. }
  130. } while (strm.avail_out == 0);
  131. /* done when inflate() says it's done */
  132. } while (ret != Z_STREAM_END);
  133. /* clean up and return */
  134. (void)inflateEnd(&strm);
  135. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  136. }
  137. DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
  138. {
  139. int ret;
  140. unsigned have;
  141. z_stream strm;
  142. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  143. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  144. out = new unsigned char [realSize];
  145. int latPosOut = 0;
  146. /* allocate inflate state */
  147. strm.zalloc = Z_NULL;
  148. strm.zfree = Z_NULL;
  149. strm.opaque = Z_NULL;
  150. strm.avail_in = 0;
  151. strm.next_in = Z_NULL;
  152. ret = inflateInit2(&strm, wBits);
  153. if (ret != Z_OK)
  154. return ret;
  155. int chunkNumber = 0;
  156. do
  157. {
  158. int readBytes = 0;
  159. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  160. {
  161. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  162. ++readBytes;
  163. }
  164. ++chunkNumber;
  165. strm.avail_in = readBytes;
  166. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  167. /*if (in.bad())
  168. {
  169. (void)inflateEnd(&strm);
  170. return Z_ERRNO;
  171. }*/
  172. if (strm.avail_in == 0)
  173. break;
  174. strm.next_in = inx;
  175. /* run inflate() on input until output buffer not full */
  176. do
  177. {
  178. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  179. strm.next_out = outx;
  180. ret = inflate(&strm, Z_NO_FLUSH);
  181. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  182. switch (ret)
  183. {
  184. case Z_NEED_DICT:
  185. ret = Z_DATA_ERROR; /* and fall through */
  186. case Z_DATA_ERROR:
  187. case Z_MEM_ERROR:
  188. (void)inflateEnd(&strm);
  189. return ret;
  190. }
  191. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  192. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  193. {
  194. (void)inflateEnd(&strm);
  195. return Z_ERRNO;
  196. }*/
  197. //out.write((char*)outx, have);
  198. for(int oo=0; oo<have; ++oo)
  199. {
  200. out[latPosOut] = outx[oo];
  201. ++latPosOut;
  202. }
  203. /*if(out.bad())
  204. {
  205. (void)inflateEnd(&strm);
  206. return Z_ERRNO;
  207. }*/
  208. } while (strm.avail_out == 0);
  209. /* done when inflate() says it's done */
  210. } while (ret != Z_STREAM_END);
  211. /* clean up and return */
  212. (void)inflateEnd(&strm);
  213. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  214. }
  215. void CLodHandler::extract(std::string FName)
  216. {
  217. std::ofstream FOut;
  218. for (int i=0;i<totalFiles;i++)
  219. {
  220. fseek(FLOD, entries[i].offset, 0);
  221. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  222. unsigned char * outp;
  223. if (entries[i].size==0) //file is not compressed
  224. {
  225. outp = new unsigned char[entries[i].realSize];
  226. fread((char*)outp, 1, entries[i].realSize, FLOD);
  227. std::ofstream out;
  228. out.open(bufff.c_str(), std::ios::binary);
  229. if(!out.is_open())
  230. {
  231. std::cout<<"Unable to create "<<bufff;
  232. }
  233. else
  234. {
  235. for(int hh=0; hh<entries[i].realSize; ++hh)
  236. {
  237. out<<*(outp+hh);
  238. }
  239. out.close();
  240. }
  241. }
  242. else
  243. {
  244. outp = new unsigned char[entries[i].size];
  245. fread((char*)outp, 1, entries[i].size, FLOD);
  246. fseek(FLOD, 0, 0);
  247. std::ofstream destin;
  248. destin.open(bufff.c_str(), std::ios::binary);
  249. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  250. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  251. destin.close();
  252. if(decRes!=0)
  253. {
  254. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  255. }
  256. }
  257. delete[] outp;
  258. }
  259. fclose(FLOD);
  260. }
  261. void CLodHandler::extractFile(std::string FName, std::string name)
  262. {
  263. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  264. for (int i=0;i<totalFiles;i++)
  265. {
  266. std::string buf1 = std::string((char*)entries[i].name);
  267. std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  268. if(buf1!=name)
  269. continue;
  270. fseek(FLOD, entries[i].offset, 0);
  271. std::string bufff = (FName);
  272. unsigned char * outp;
  273. if (entries[i].size==0) //file is not compressed
  274. {
  275. outp = new unsigned char[entries[i].realSize];
  276. fread((char*)outp, 1, entries[i].realSize, FLOD);
  277. std::ofstream out;
  278. out.open(bufff.c_str(), std::ios::binary);
  279. if(!out.is_open())
  280. {
  281. std::cout<<"Unable to create "<<bufff;
  282. }
  283. else
  284. {
  285. for(int hh=0; hh<entries[i].realSize; ++hh)
  286. {
  287. out<<*(outp+hh);
  288. }
  289. out.close();
  290. }
  291. }
  292. else //we will decompressing file
  293. {
  294. outp = new unsigned char[entries[i].size];
  295. fread((char*)outp, 1, entries[i].size, FLOD);
  296. fseek(FLOD, 0, 0);
  297. std::ofstream destin;
  298. destin.open(bufff.c_str(), std::ios::binary);
  299. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  300. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  301. destin.close();
  302. if(decRes!=0)
  303. {
  304. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  305. }
  306. }
  307. delete[] outp;
  308. }
  309. }
  310. int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic)
  311. {
  312. int ret=0;
  313. int amp=1;
  314. for (int i=0; i<bytCon; i++)
  315. {
  316. ret+=bufor[i]*amp;
  317. amp*=256;
  318. }
  319. if(cyclic && bytCon<4 && ret>=amp/2)
  320. {
  321. ret = ret-amp;
  322. }
  323. return ret;
  324. }
  325. void CLodHandler::init(std::string lodFile)
  326. {
  327. std::string Ts;
  328. FLOD = fopen(lodFile.c_str(), "rb");
  329. fseek(FLOD, 8, 0);
  330. unsigned char temp[4];
  331. fread((char*)temp, 1, 4, FLOD);
  332. totalFiles = readNormalNr(temp,4);
  333. fseek(FLOD, 0x5c, 0);
  334. for (int i=0; i<totalFiles; i++)
  335. {
  336. Entry entry;
  337. char * bufc = new char;
  338. bool appending = true;
  339. for(int kk=0; kk<12; ++kk)
  340. {
  341. //FLOD.read(bufc, 1);
  342. fread(bufc, 1, 1, FLOD);
  343. if(appending)
  344. {
  345. entry.name[kk] = toupper(*bufc);
  346. }
  347. else
  348. {
  349. entry.name[kk] = 0;
  350. appending = false;
  351. }
  352. }
  353. delete bufc;
  354. fread((char*)entry.hlam_1, 1, 4, FLOD);
  355. fread((char*)temp, 1, 4, FLOD);
  356. entry.offset=readNormalNr(temp,4);
  357. fread((char*)temp, 1, 4, FLOD);
  358. entry.realSize=readNormalNr(temp,4);
  359. fread((char*)entry.hlam_2, 1, 4, FLOD);
  360. fread((char*)temp, 1, 4, FLOD);
  361. entry.size=readNormalNr(temp,4);
  362. for (int z=0;z<12;z++)
  363. {
  364. if (entry.name[z])
  365. entry.nameStr+=entry.name[z];
  366. else break;
  367. }
  368. entries.push_back(entry);
  369. }
  370. }
  371. std::string CLodHandler::getTextFile(std::string name)
  372. {
  373. int length=-1;
  374. unsigned char* data = giveFile(name,&length);
  375. std::string ret;
  376. ret.reserve(length);
  377. for(int i=0;i<length;i++)
  378. ret+=data[i];
  379. delete [] data;
  380. return ret;
  381. }