CLodHandler.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "stdafx.h"
  2. #include "CLodHandler.h"
  3. #include <sstream>
  4. int readNormalNr (int pos, int bytCon, unsigned char * str)
  5. {
  6. int ret=0;
  7. int amp=1;
  8. if (str)
  9. {
  10. for (int i=0; i<bytCon; i++)
  11. {
  12. ret+=str[pos+i]*amp;
  13. amp*=256;
  14. }
  15. }
  16. else return -1;
  17. return ret;
  18. }
  19. void CPCXConv::openPCX(char * PCX, int len)
  20. {
  21. pcx = new unsigned char[len];
  22. for (int i=0;i<len;i++)
  23. pcx[i]=PCX[i];
  24. }
  25. void CPCXConv::fromFile(std::string path)
  26. {
  27. std::ifstream * is = new std::ifstream();
  28. is -> open(path.c_str(),std::ios::binary);
  29. is->seekg(0,std::ios::end); // na koniec
  30. pcxs = is->tellg(); // read length
  31. is->seekg(0,std::ios::beg); // wracamy na poczatek
  32. pcx = new unsigned char[pcxs]; // allocate memory
  33. is->read((char*)pcx, pcxs); // read map file to buffer
  34. is->close();
  35. delete is;
  36. }
  37. void CPCXConv::saveBMP(std::string path)
  38. {
  39. std::ofstream os;
  40. os.open(path.c_str(), std::ios::binary);
  41. os.write((char*)bmp,bmps);
  42. os.close();
  43. }
  44. void CPCXConv::convert()
  45. {
  46. BMPHeader bh;
  47. BMPPalette pal[256];
  48. Epcxformat format;
  49. int fSize, maxx, maxy,i,y;
  50. bool check1, check2;
  51. unsigned char add;
  52. int it=0;
  53. std::stringstream out;
  54. fSize = readNormalNr(it,4,pcx);it+=4;
  55. maxx = readNormalNr(it,4,pcx);it+=4;
  56. maxy = readNormalNr(it,4,pcx);it+=4;
  57. if (fSize==maxx*maxy*3)
  58. check1=true;
  59. else
  60. check1=false;
  61. if (fSize==maxx*maxy)
  62. check2=true;
  63. else
  64. check2=false;
  65. if (check1)
  66. format=Epcxformat::PCX24B;
  67. else if (check2)
  68. format=Epcxformat::PCX8B;
  69. else
  70. return;
  71. bh.x=maxx;
  72. bh.y=maxy;
  73. add=(int)(4*(((float)1)-(((float)maxx/(float)4)-((int)((float)maxx/(float)4)))));
  74. if (add==4)
  75. add=0;
  76. if (format==Epcxformat::PCX8B)
  77. {
  78. bh._c1=0x436;
  79. bh._c2=0x28;
  80. bh._c3=1;
  81. bh._c4=8;
  82. bh.dataSize2=bh.dataSize1=maxx*maxy;
  83. bh.fullSize = bh.dataSize1+436;
  84. }
  85. else
  86. {
  87. bh._c1=0x36;
  88. bh._c2=0x28;
  89. bh._c3=1;
  90. bh._c4=0x18;
  91. bh.dataSize2=bh.dataSize1=0xB12;
  92. bh.fullSize=(maxx+add)*maxy*3+36;
  93. }
  94. if (format==Epcxformat::PCX8B)
  95. {
  96. it = pcxs-256*3;
  97. for (int i=0;i<256;i++)
  98. {
  99. pal[i].R=pcx[it++];
  100. pal[i].G=pcx[it++];
  101. pal[i].B=pcx[it++];
  102. pal[i].F='\0';
  103. }
  104. }
  105. out<<"BM";
  106. bh.print(out);
  107. if (format==Epcxformat::PCX8B)
  108. {
  109. for (int i=0;i<256;i++)
  110. {
  111. out<<pal[i].B;
  112. out<<pal[i].G;
  113. out<<pal[i].R;
  114. out<<pal[i].F;
  115. }
  116. for (y=maxy;y>0;y--)
  117. {
  118. it=0xC+(y-1)*maxx;
  119. for (int j=0;j<maxx;j++)
  120. out<<pcx[it+j];
  121. if (add>0)
  122. {
  123. for (int j=0;j<add;j++)
  124. out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
  125. }
  126. }
  127. }
  128. else
  129. {
  130. for (y=maxy; y>0; y--)
  131. {
  132. it=0xC+(y-1)*maxx*3;
  133. for (int j=0;j<maxx*3;j++)
  134. out<<pcx[it+j];
  135. if (add>0)
  136. {
  137. for (int j=0;j<add*3;j++)
  138. out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
  139. }
  140. }
  141. }
  142. std::string temp = out.str();
  143. bmp = new unsigned char[temp.length()];
  144. bmps=temp.length();
  145. for (int a=0;a<temp.length();a++)
  146. {
  147. bmp[a]=temp[a];
  148. }
  149. }
  150. // if PFileType=pcx24bit then
  151. // for y:=MaxY downto 1 do
  152. // begin
  153. // FPcx.Seek($0C+(y-1)*(MaxX*3),spBegin);
  154. // Stream2Stream(FTemp,FPcx,MaxX*3);
  155. // if Add>0 then FTemp.Write(Buffer,Add*3);
  156. // end;
  157. // FTemp.Seek(0,spBegin);
  158. // BufferBitmap.LoadFromStream(FTemp);
  159. // FTemp.Free;
  160. // Result:=True;
  161. //end;
  162. int CLodHandler::decompress (unsigned char * source, int size, int realSize, std::ofstream & dest)
  163. {
  164. std::ofstream lb;
  165. lb.open("lodbuf\\buf.gz", std::ios::out|std::ios::binary);
  166. for(int i=0; i<size; ++i)
  167. {
  168. lb<<source[i];
  169. }
  170. lb.close();
  171. FILE * inputf = fopen("lodbuf\\buf.gz", "rb+");
  172. FILE * outputf = fopen("lodbuf\\buf.un", "wb+");
  173. int ret = infm(inputf, outputf);
  174. fclose(inputf);
  175. fclose(outputf);
  176. return ret;
  177. }
  178. int CLodHandler::decompress (unsigned char * source, int size, int realSize, std::string & dest)
  179. {
  180. std::ofstream lb;
  181. lb.open("lodbuf\\buf.gz", std::ios::out|std::ios::binary);
  182. for(int i=0; i<size; ++i)
  183. {
  184. lb<<source[i];
  185. }
  186. lb.close();
  187. FILE * inputf = fopen("lodbuf\\buf.gz", "rb+");
  188. FILE * outputf = fopen(dest.c_str(), "wb+");
  189. int ret = infm(inputf, outputf);
  190. fclose(inputf);
  191. fclose(outputf);
  192. return ret;
  193. }
  194. int CLodHandler::infm(FILE *source, FILE *dest, int wBits)
  195. {
  196. int ret;
  197. unsigned have;
  198. z_stream strm;
  199. unsigned char in[NLoadHandlerHelp::fCHUNK];
  200. unsigned char out[NLoadHandlerHelp::fCHUNK];
  201. /* allocate inflate state */
  202. strm.zalloc = Z_NULL;
  203. strm.zfree = Z_NULL;
  204. strm.opaque = Z_NULL;
  205. strm.avail_in = 0;
  206. strm.next_in = Z_NULL;
  207. ret = inflateInit2(&strm, wBits);
  208. if (ret != Z_OK)
  209. return ret;
  210. /* decompress until deflate stream ends or end of file */
  211. do
  212. {
  213. strm.avail_in = fread(in, 1, NLoadHandlerHelp::fCHUNK, source);
  214. if (ferror(source)) {
  215. (void)inflateEnd(&strm);
  216. return Z_ERRNO;
  217. }
  218. if (strm.avail_in == 0)
  219. break;
  220. strm.next_in = in;
  221. /* run inflate() on input until output buffer not full */
  222. do
  223. {
  224. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  225. strm.next_out = out;
  226. ret = inflate(&strm, Z_NO_FLUSH);
  227. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  228. switch (ret)
  229. {
  230. case Z_NEED_DICT:
  231. ret = Z_DATA_ERROR; /* and fall through */
  232. case Z_DATA_ERROR:
  233. case Z_MEM_ERROR:
  234. (void)inflateEnd(&strm);
  235. return ret;
  236. }
  237. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  238. if (fwrite(out, 1, have, dest) != have || ferror(dest))
  239. {
  240. (void)inflateEnd(&strm);
  241. return Z_ERRNO;
  242. }
  243. } while (strm.avail_out == 0);
  244. /* done when inflate() says it's done */
  245. } while (ret != Z_STREAM_END);
  246. /* clean up and return */
  247. (void)inflateEnd(&strm);
  248. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  249. }
  250. void CLodHandler::extract(std::string FName)
  251. {
  252. std::ifstream FLOD;
  253. std::ofstream FOut;
  254. int i;
  255. std::string Ts;
  256. //std::cout<<"*** Loading FAT ... \n";
  257. FLOD.open(FName.c_str(),std::ios::binary);
  258. //std::cout<<"*** Archive: "+FName+" loaded\n";
  259. FLOD.seekg(8,std::ios_base::beg);
  260. unsigned char temp[4];
  261. FLOD.read((char*)temp,4);
  262. totalFiles = readNormalNr(temp,4);
  263. FLOD.seekg(0x5c,std::ios_base::beg);
  264. entries.reserve(totalFiles);
  265. //std::cout<<"*** Loading FAT ...\n";
  266. for (int i=0; i<totalFiles; i++)
  267. {
  268. entries.push_back(Entry());
  269. //FLOD.read((char*)entries[i].name,12);
  270. char * bufc = new char;
  271. bool appending = true;
  272. for(int kk=0; kk<12; ++kk)
  273. {
  274. FLOD.read(bufc, 1);
  275. if(appending)
  276. {
  277. entries[i].name[kk] = *bufc;
  278. }
  279. else
  280. {
  281. entries[i].name[kk] = 0;
  282. appending = false;
  283. }
  284. }
  285. delete bufc;
  286. FLOD.read((char*)entries[i].hlam_1,4);
  287. FLOD.read((char*)temp,4);
  288. entries[i].offset=readNormalNr(temp,4);
  289. FLOD.read((char*)temp,4);
  290. entries[i].realSize=readNormalNr(temp,4);
  291. FLOD.read((char*)entries[i].hlam_2,4);
  292. FLOD.read((char*)temp,4);
  293. entries[i].size=readNormalNr(temp,4);
  294. }
  295. //std::cout<<" done\n";
  296. for (int i=0;i<totalFiles;i++)
  297. {
  298. FLOD.seekg(entries[i].offset,std::ios_base::beg);
  299. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  300. unsigned char * outp;
  301. if (entries[i].size==0) //file is not compressed
  302. {
  303. outp = new unsigned char[entries[i].realSize];
  304. FLOD.read((char*)outp, entries[i].realSize);
  305. std::ofstream out;
  306. out.open(bufff.c_str(), std::ios::binary);
  307. if(!out.is_open())
  308. {
  309. std::cout<<"Unable to create "<<bufff;
  310. }
  311. else
  312. {
  313. for(int hh=0; hh<entries[i].realSize; ++hh)
  314. {
  315. out<<*(outp+hh);
  316. }
  317. out.close();
  318. }
  319. }
  320. else
  321. {
  322. outp = new unsigned char[entries[i].size];
  323. FLOD.read((char*)outp, entries[i].size);
  324. FLOD.seekg(0, std::ios_base::beg);
  325. int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  326. if(decRes!=0)
  327. {
  328. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  329. }
  330. }
  331. //for (int j=0; j<entries[i].size; j++)
  332. // FOut << outp[j];
  333. //FOut.flush();
  334. delete outp;
  335. //FOut.close();
  336. //std::cout<<"*** done\n";
  337. }
  338. FLOD.close();
  339. //std::cout<<"*** Archive: "+FName+" closed\n";
  340. }
  341. int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic)
  342. {
  343. int ret=0;
  344. int amp=1;
  345. for (int i=0; i<bytCon; i++)
  346. {
  347. ret+=bufor[i]*amp;
  348. amp*=256;
  349. }
  350. if(cyclic && bytCon<4 && ret>=amp/2)
  351. {
  352. ret = ret-amp;
  353. }
  354. return ret;
  355. }
  356. void CLodHandler::init(std::string lodFile)
  357. {
  358. extract(lodFile);
  359. }