CLodHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. #include "stdafx.h"
  2. #include "CLodHandler.h"
  3. #include <sstream>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations
  7. int readNormalNr (int pos, int bytCon, unsigned char * str)
  8. {
  9. int ret=0;
  10. int amp=1;
  11. if (str)
  12. {
  13. for (int i=0; i<bytCon; i++)
  14. {
  15. ret+=str[pos+i]*amp;
  16. amp*=256;
  17. }
  18. }
  19. else return -1;
  20. return ret;
  21. }
  22. void CPCXConv::openPCX(char * PCX, int len)
  23. {
  24. pcxs=len;
  25. pcx=(unsigned char*)PCX;
  26. /*pcx = new unsigned char[len];
  27. for (int i=0;i<len;i++)
  28. pcx[i]=PCX[i];*/
  29. }
  30. void CPCXConv::fromFile(std::string path)
  31. {
  32. std::ifstream * is = new std::ifstream();
  33. is -> open(path.c_str(),std::ios::binary);
  34. is->seekg(0,std::ios::end); // na koniec
  35. pcxs = is->tellg(); // read length
  36. is->seekg(0,std::ios::beg); // wracamy na poczatek
  37. pcx = new unsigned char[pcxs]; // allocate memory
  38. is->read((char*)pcx, pcxs); // read map file to buffer
  39. is->close();
  40. delete is;
  41. }
  42. void CPCXConv::saveBMP(std::string path)
  43. {
  44. std::ofstream os;
  45. os.open(path.c_str(), std::ios::binary);
  46. os.write((char*)bmp,bmps);
  47. os.close();
  48. }
  49. void CPCXConv::convert()
  50. {
  51. BMPHeader bh;
  52. BMPPalette pal[256];
  53. Epcxformat format;
  54. int fSize,i,y;
  55. bool check1, check2;
  56. unsigned char add;
  57. int it=0;
  58. std::stringstream out;
  59. fSize = readNormalNr(it,4,pcx);it+=4;
  60. bh.x = readNormalNr(it,4,pcx);it+=4;
  61. bh.y = readNormalNr(it,4,pcx);it+=4;
  62. if (fSize==bh.x*bh.y*3)
  63. check1=true;
  64. else
  65. check1=false;
  66. if (fSize==bh.x*bh.y)
  67. check2=true;
  68. else
  69. check2=false;
  70. if (check1)
  71. format=Epcxformat::PCX24B;
  72. else if (check2)
  73. format=Epcxformat::PCX8B;
  74. else
  75. return;
  76. add=(int)(4*(((float)1)-(((float)bh.x/(float)4)-((int)((float)bh.x/(float)4)))));
  77. if (add==4)
  78. add=0;
  79. bh._h3=bh.x*bh.y;
  80. if (format==Epcxformat::PCX8B)
  81. {
  82. bh._c1=0x436;
  83. bh._c2=0x28;
  84. bh._c3=1;
  85. bh._c4=8;
  86. //bh.dataSize2=bh.dataSize1=maxx*maxy;
  87. bh.dataSize1=bh.x;
  88. bh.dataSize2=bh.y;
  89. bh.fullSize = bh.dataSize1+436;
  90. }
  91. else
  92. {
  93. bh._c1=0x36;
  94. bh._c2=0x28;
  95. bh._c3=1;
  96. bh._c4=0x18;
  97. //bh.dataSize2=bh.dataSize1=0xB12;
  98. bh.dataSize1=bh.x;
  99. bh.dataSize2=bh.y;
  100. bh.fullSize=(bh.x+add)*bh.y*3+36+18;
  101. bh._h3*=3;
  102. }
  103. if (format==Epcxformat::PCX8B)
  104. {
  105. it = pcxs-256*3;
  106. for (int i=0;i<256;i++)
  107. {
  108. pal[i].R=pcx[it++];
  109. pal[i].G=pcx[it++];
  110. pal[i].B=pcx[it++];
  111. pal[i].F='\0';
  112. }
  113. }
  114. out<<"BM";
  115. bh.print(out);
  116. if (format==Epcxformat::PCX8B)
  117. {
  118. for (int i=0;i<256;i++)
  119. {
  120. out<<pal[i].B;
  121. out<<pal[i].G;
  122. out<<pal[i].R;
  123. out<<pal[i].F;
  124. }
  125. for (y=bh.y;y>0;y--)
  126. {
  127. it=0xC+(y-1)*bh.x;
  128. for (int j=0;j<bh.x;j++)
  129. out<<pcx[it+j];
  130. if (add>0)
  131. {
  132. for (int j=0;j<add;j++)
  133. out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
  134. }
  135. }
  136. }
  137. else
  138. {
  139. for (y=bh.y; y>0; y--)
  140. {
  141. it=0xC+(y-1)*bh.x*3;
  142. for (int j=0;j<bh.x*3;j++)
  143. out<<pcx[it+j];
  144. if (add>0)
  145. {
  146. for (int j=0;j<add*3;j++)
  147. out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
  148. }
  149. }
  150. }
  151. std::string temp = out.str();
  152. bmp = new unsigned char[temp.length()];
  153. bmps=temp.length();
  154. for (int a=0;a<temp.length();a++)
  155. {
  156. bmp[a]=temp[a];
  157. }
  158. }
  159. SDL_Surface * CPCXConv::getSurface()
  160. {
  161. SDL_Surface * ret;
  162. BMPHeader bh;
  163. BMPPalette pal[256];
  164. Epcxformat format;
  165. int fSize,i,y;
  166. bool check1, check2;
  167. unsigned char add;
  168. int it=0;
  169. fSize = readNormalNr(it,4,pcx);it+=4;
  170. bh.x = readNormalNr(it,4,pcx);it+=4;
  171. bh.y = readNormalNr(it,4,pcx);it+=4;
  172. if (fSize==bh.x*bh.y*3)
  173. check1=true;
  174. else
  175. check1=false;
  176. if (fSize==bh.x*bh.y)
  177. check2=true;
  178. else
  179. check2=false;
  180. if (check1)
  181. format=Epcxformat::PCX24B;
  182. else if (check2)
  183. format=Epcxformat::PCX8B;
  184. else
  185. return NULL;
  186. add = 4 - bh.x%4;
  187. if (add==4)
  188. add=0;
  189. if (format==Epcxformat::PCX8B)
  190. {
  191. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, bh.x+add, bh.y, 8, 0, 0, 0, 0);
  192. }
  193. else
  194. {
  195. int bmask = 0x0000ff;
  196. int gmask = 0x00ff00;
  197. int rmask = 0xff0000;
  198. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, bh.x+add, bh.y, 24, rmask, gmask, bmask, 0);
  199. }
  200. if (format==Epcxformat::PCX8B)
  201. {
  202. it = pcxs-256*3;
  203. for (int i=0;i<256;i++)
  204. {
  205. pal[i].R=pcx[it++];
  206. pal[i].G=pcx[it++];
  207. pal[i].B=pcx[it++];
  208. pal[i].F='\0';
  209. }
  210. }
  211. if (format==Epcxformat::PCX8B)
  212. {
  213. for(int i=0; i<256; ++i)
  214. {
  215. SDL_Color tp;
  216. tp.r = pal[i].R;
  217. tp.g = pal[i].G;
  218. tp.b = pal[i].B;
  219. tp.unused = pal[i].F;
  220. *(ret->format->palette->colors+i) = tp;
  221. }
  222. for (y=bh.y;y>0;y--)
  223. {
  224. it=0xC+(y-1)*bh.x;
  225. for (int j=0;j<bh.x;j++)
  226. {
  227. *((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * j) = pcx[it+j];
  228. }
  229. if (add>0)
  230. {
  231. for (int j=0;j<add;j++)
  232. {
  233. *((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * (j+bh.x)) = 0;
  234. }
  235. }
  236. }
  237. }
  238. else
  239. {
  240. for (y=bh.y; y>0; y--)
  241. {
  242. it=0xC+(y-1)*bh.x*3;
  243. for (int j=0;j<bh.x*3;j++)
  244. {
  245. *((char*)ret->pixels + ret->pitch * (y-1) + j) = pcx[it+j];
  246. }
  247. if (add>0)
  248. {
  249. for (int j=0;j<add*3;j++)
  250. {
  251. *((char*)ret->pixels + ret->pitch * (y-1) + (j+bh.x*3)) = 0;
  252. }
  253. }
  254. }
  255. }
  256. return ret;
  257. }
  258. SDL_Surface * CLodHandler::loadBitmap(std::string fname)
  259. {
  260. unsigned char * pcx;
  261. std::transform(fname.begin(),fname.end(),fname.begin(),toupper);
  262. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  263. int index=-1;
  264. for (int i=0;i<entries.size();i++)
  265. {
  266. std::string buf1 = std::string((char*)entries[i].name);
  267. if(buf1==fname)
  268. {
  269. index=i;
  270. break;
  271. }
  272. }
  273. if(index==-1)
  274. {
  275. std::cout<<"File "<<fname<<" did't found"<<std::endl;
  276. }
  277. FLOD.seekg(entries[index].offset,std::ios_base::beg);
  278. if (entries[index].size==0) //file is not compressed
  279. {
  280. pcx = new unsigned char[entries[index].realSize];
  281. FLOD.read((char*)pcx,entries[index].realSize);
  282. }
  283. else
  284. {
  285. unsigned char * pcd = new unsigned char[entries[index].size];
  286. FLOD.read((char*)pcd,entries[index].size);
  287. int res=infs2(pcd,entries[index].size,entries[index].realSize,pcx);
  288. if(res!=0)
  289. {
  290. std::cout<<"an error "<<res<<" ocured during extracting file "<<fname<<std::endl;
  291. }
  292. }
  293. CPCXConv cp;
  294. cp.openPCX((char*)pcx,entries[index].realSize);
  295. return cp.getSurface();
  296. }
  297. int CLodHandler::decompress (unsigned char * source, int size, int realSize, std::string & dest)
  298. {
  299. std::ofstream lb;
  300. lb.open("lodbuf\\buf.gz", std::ios::out|std::ios::binary);
  301. for(int i=0; i<size; ++i)
  302. {
  303. lb<<source[i];
  304. }
  305. lb.close();
  306. FILE * inputf = fopen("lodbuf\\buf.gz", "rb+");
  307. FILE * outputf = fopen(dest.c_str(), "wb+");
  308. int ret = infm(inputf, outputf);
  309. fclose(inputf);
  310. fclose(outputf);
  311. return ret;
  312. }
  313. int CLodHandler::infm(FILE *source, FILE *dest, int wBits)
  314. {
  315. int ret;
  316. unsigned have;
  317. z_stream strm;
  318. unsigned char in[NLoadHandlerHelp::fCHUNK];
  319. unsigned char out[NLoadHandlerHelp::fCHUNK];
  320. /* allocate inflate state */
  321. strm.zalloc = Z_NULL;
  322. strm.zfree = Z_NULL;
  323. strm.opaque = Z_NULL;
  324. strm.avail_in = 0;
  325. strm.next_in = Z_NULL;
  326. ret = inflateInit2(&strm, wBits);
  327. if (ret != Z_OK)
  328. return ret;
  329. /* decompress until deflate stream ends or end of file */
  330. do
  331. {
  332. strm.avail_in = fread(in, 1, NLoadHandlerHelp::fCHUNK, source);
  333. if (ferror(source)) {
  334. (void)inflateEnd(&strm);
  335. return Z_ERRNO;
  336. }
  337. if (strm.avail_in == 0)
  338. break;
  339. strm.next_in = in;
  340. /* run inflate() on input until output buffer not full */
  341. do
  342. {
  343. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  344. strm.next_out = out;
  345. ret = inflate(&strm, Z_NO_FLUSH);
  346. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  347. switch (ret)
  348. {
  349. case Z_NEED_DICT:
  350. ret = Z_DATA_ERROR; /* and fall through */
  351. case Z_DATA_ERROR:
  352. case Z_MEM_ERROR:
  353. (void)inflateEnd(&strm);
  354. return ret;
  355. }
  356. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  357. if (fwrite(out, 1, have, dest) != have || ferror(dest))
  358. {
  359. (void)inflateEnd(&strm);
  360. return Z_ERRNO;
  361. }
  362. } while (strm.avail_out == 0);
  363. /* done when inflate() says it's done */
  364. } while (ret != Z_STREAM_END);
  365. /* clean up and return */
  366. (void)inflateEnd(&strm);
  367. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  368. }
  369. CDefHandler * CLodHandler::giveDef(std::string defName) // TODO: zamienic
  370. {
  371. std::vector<std::string> pom;
  372. pom.push_back(defName);
  373. return extractManyFiles(pom)[0];
  374. }
  375. std::vector<CDefHandler *> CLodHandler::extractManyFiles(std::vector<std::string> defNamesIn)
  376. {
  377. std::vector<CDefHandler *> ret(defNamesIn.size());
  378. for(int hh=0; hh<defNamesIn.size(); ++hh)
  379. {
  380. std::transform(defNamesIn[hh].begin(), defNamesIn[hh].end(), defNamesIn[hh].begin(), (int(*)(int))toupper);
  381. }
  382. int i;
  383. std::vector<char> found(defNamesIn.size(), 0);
  384. for (int i=0;i<totalFiles;i++)
  385. {
  386. //std::cout<<'\r'<<"Reading defs: "<<(100.0*i)/((float)(totalFiles))<<"% ";
  387. std::string buf1 = std::string((char*)entries[i].name);
  388. //std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  389. bool exists = false;
  390. int curDef;
  391. for(int hh=0; hh<defNamesIn.size(); ++hh)
  392. {
  393. if(buf1==defNamesIn[hh])
  394. {
  395. exists = true;
  396. found[hh] = '\1';
  397. curDef = hh;
  398. break;
  399. }
  400. }
  401. if(!exists)
  402. continue;
  403. FLOD.seekg(entries[i].offset,std::ios_base::beg);
  404. unsigned char * outp;
  405. if (entries[i].size==0) //file is not compressed
  406. {
  407. outp = new unsigned char[entries[i].realSize];
  408. FLOD.read((char*)outp, entries[i].realSize);
  409. CDefHandler * nh = new CDefHandler;
  410. nh->openFromMemory(outp, entries[i].realSize, std::string((char*)entries[i].name));
  411. ret[curDef] = nh;
  412. }
  413. else //we will decompressing file
  414. {
  415. outp = new unsigned char[entries[i].size];
  416. FLOD.read((char*)outp, entries[i].size);
  417. FLOD.seekg(0, std::ios_base::beg);
  418. unsigned char * decomp = NULL;
  419. int decRes = infs2(outp, entries[i].size, entries[i].realSize, decomp);
  420. CDefHandler * nh = new CDefHandler;
  421. nh->openFromMemory(decomp, entries[i].realSize, std::string((char*)entries[i].name));
  422. ret[curDef] = nh;
  423. }
  424. delete outp;
  425. }
  426. //std::cout<<'\r'<<"Reading defs: 100% "<<std::endl;
  427. for(int hh=0; hh<found.size(); ++hh)
  428. {
  429. if(!found[hh])
  430. {
  431. for(int ff=0; ff<hh; ++ff)
  432. {
  433. if(defNamesIn[ff]==defNamesIn[hh])
  434. {
  435. ret[hh]=ret[ff];
  436. }
  437. }
  438. }
  439. }
  440. //std::cout<<"*** Archive: "+FName+" closed\n";
  441. return ret;
  442. }
  443. int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
  444. {
  445. int ret;
  446. unsigned have;
  447. z_stream strm;
  448. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  449. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  450. /* allocate inflate state */
  451. strm.zalloc = Z_NULL;
  452. strm.zfree = Z_NULL;
  453. strm.opaque = Z_NULL;
  454. strm.avail_in = 0;
  455. strm.next_in = Z_NULL;
  456. ret = inflateInit2(&strm, wBits);
  457. if (ret != Z_OK)
  458. return ret;
  459. int chunkNumber = 0;
  460. do
  461. {
  462. int readBytes = 0;
  463. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  464. {
  465. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  466. ++readBytes;
  467. }
  468. ++chunkNumber;
  469. strm.avail_in = readBytes;
  470. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  471. /*if (in.bad())
  472. {
  473. (void)inflateEnd(&strm);
  474. return Z_ERRNO;
  475. }*/
  476. if (strm.avail_in == 0)
  477. break;
  478. strm.next_in = inx;
  479. /* run inflate() on input until output buffer not full */
  480. do
  481. {
  482. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  483. strm.next_out = outx;
  484. ret = inflate(&strm, Z_NO_FLUSH);
  485. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  486. switch (ret)
  487. {
  488. case Z_NEED_DICT:
  489. ret = Z_DATA_ERROR; /* and fall through */
  490. case Z_DATA_ERROR:
  491. case Z_MEM_ERROR:
  492. (void)inflateEnd(&strm);
  493. return ret;
  494. }
  495. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  496. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  497. {
  498. (void)inflateEnd(&strm);
  499. return Z_ERRNO;
  500. }*/
  501. out.write((char*)outx, have);
  502. if(out.bad())
  503. {
  504. (void)inflateEnd(&strm);
  505. return Z_ERRNO;
  506. }
  507. } while (strm.avail_out == 0);
  508. /* done when inflate() says it's done */
  509. } while (ret != Z_STREAM_END);
  510. /* clean up and return */
  511. (void)inflateEnd(&strm);
  512. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  513. }
  514. int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
  515. {
  516. int ret;
  517. unsigned have;
  518. z_stream strm;
  519. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  520. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  521. out = new unsigned char [realSize];
  522. int latPosOut = 0;
  523. /* allocate inflate state */
  524. strm.zalloc = Z_NULL;
  525. strm.zfree = Z_NULL;
  526. strm.opaque = Z_NULL;
  527. strm.avail_in = 0;
  528. strm.next_in = Z_NULL;
  529. ret = inflateInit2(&strm, wBits);
  530. if (ret != Z_OK)
  531. return ret;
  532. int chunkNumber = 0;
  533. do
  534. {
  535. int readBytes = 0;
  536. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  537. {
  538. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  539. ++readBytes;
  540. }
  541. ++chunkNumber;
  542. strm.avail_in = readBytes;
  543. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  544. /*if (in.bad())
  545. {
  546. (void)inflateEnd(&strm);
  547. return Z_ERRNO;
  548. }*/
  549. if (strm.avail_in == 0)
  550. break;
  551. strm.next_in = inx;
  552. /* run inflate() on input until output buffer not full */
  553. do
  554. {
  555. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  556. strm.next_out = outx;
  557. ret = inflate(&strm, Z_NO_FLUSH);
  558. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  559. switch (ret)
  560. {
  561. case Z_NEED_DICT:
  562. ret = Z_DATA_ERROR; /* and fall through */
  563. case Z_DATA_ERROR:
  564. case Z_MEM_ERROR:
  565. (void)inflateEnd(&strm);
  566. return ret;
  567. }
  568. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  569. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  570. {
  571. (void)inflateEnd(&strm);
  572. return Z_ERRNO;
  573. }*/
  574. //out.write((char*)outx, have);
  575. for(int oo=0; oo<have; ++oo)
  576. {
  577. out[latPosOut] = outx[oo];
  578. ++latPosOut;
  579. }
  580. /*if(out.bad())
  581. {
  582. (void)inflateEnd(&strm);
  583. return Z_ERRNO;
  584. }*/
  585. } while (strm.avail_out == 0);
  586. /* done when inflate() says it's done */
  587. } while (ret != Z_STREAM_END);
  588. /* clean up and return */
  589. (void)inflateEnd(&strm);
  590. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  591. }
  592. void CLodHandler::extract(std::string FName)
  593. {
  594. std::ofstream FOut;
  595. int i;
  596. //std::cout<<" done\n";
  597. for (int i=0;i<totalFiles;i++)
  598. {
  599. FLOD.seekg(entries[i].offset,std::ios_base::beg);
  600. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  601. unsigned char * outp;
  602. if (entries[i].size==0) //file is not compressed
  603. {
  604. outp = new unsigned char[entries[i].realSize];
  605. FLOD.read((char*)outp, entries[i].realSize);
  606. std::ofstream out;
  607. out.open(bufff.c_str(), std::ios::binary);
  608. if(!out.is_open())
  609. {
  610. std::cout<<"Unable to create "<<bufff;
  611. }
  612. else
  613. {
  614. for(int hh=0; hh<entries[i].realSize; ++hh)
  615. {
  616. out<<*(outp+hh);
  617. }
  618. out.close();
  619. }
  620. }
  621. else
  622. {
  623. outp = new unsigned char[entries[i].size];
  624. FLOD.read((char*)outp, entries[i].size);
  625. FLOD.seekg(0, std::ios_base::beg);
  626. std::ofstream destin;
  627. destin.open(bufff.c_str(), std::ios::binary);
  628. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  629. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  630. destin.close();
  631. if(decRes!=0)
  632. {
  633. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  634. }
  635. }
  636. //for (int j=0; j<entries[i].size; j++)
  637. // FOut << outp[j];
  638. //FOut.flush();
  639. delete outp;
  640. //FOut.close();
  641. //std::cout<<"*** done\n";
  642. }
  643. FLOD.close();
  644. //std::cout<<"*** Archive: "+FName+" closed\n";
  645. }
  646. void CLodHandler::extractFile(std::string FName, std::string name)
  647. {
  648. //std::ifstream FLOD;
  649. std::ofstream FOut;
  650. int i;
  651. //std::string Ts;
  652. ////std::cout<<"*** Loading FAT ... \n";
  653. //FLOD.open(FName.c_str(),std::ios::binary);
  654. ////std::cout<<"*** Archive: "+FName+" loaded\n";
  655. //FLOD.seekg(8,std::ios_base::beg);
  656. //unsigned char temp[4];
  657. //FLOD.read((char*)temp,4);
  658. //totalFiles = readNormalNr(temp,4);
  659. //FLOD.seekg(0x5c,std::ios_base::beg);
  660. //entries.reserve(totalFiles);
  661. ////std::cout<<"*** Loading FAT ...\n";
  662. //for (int i=0; i<totalFiles; i++)
  663. //{
  664. // entries.push_back(Entry());
  665. // //FLOD.read((char*)entries[i].name,12);
  666. // char * bufc = new char;
  667. // bool appending = true;
  668. // for(int kk=0; kk<12; ++kk)
  669. // {
  670. // FLOD.read(bufc, 1);
  671. // if(appending)
  672. // {
  673. // entries[i].name[kk] = *bufc;
  674. // }
  675. // else
  676. // {
  677. // entries[i].name[kk] = 0;
  678. // appending = false;
  679. // }
  680. // }
  681. // delete bufc;
  682. // FLOD.read((char*)entries[i].hlam_1,4);
  683. // FLOD.read((char*)temp,4);
  684. // entries[i].offset=readNormalNr(temp,4);
  685. // FLOD.read((char*)temp,4);
  686. // entries[i].realSize=readNormalNr(temp,4);
  687. // FLOD.read((char*)entries[i].hlam_2,4);
  688. // FLOD.read((char*)temp,4);
  689. // entries[i].size=readNormalNr(temp,4);
  690. //}
  691. //std::cout<<" done\n";
  692. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  693. for (int i=0;i<totalFiles;i++)
  694. {
  695. std::string buf1 = std::string((char*)entries[i].name);
  696. std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  697. if(buf1!=name)
  698. continue;
  699. FLOD.seekg(entries[i].offset,std::ios_base::beg);
  700. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  701. unsigned char * outp;
  702. if (entries[i].size==0) //file is not compressed
  703. {
  704. outp = new unsigned char[entries[i].realSize];
  705. FLOD.read((char*)outp, entries[i].realSize);
  706. std::ofstream out;
  707. out.open(bufff.c_str(), std::ios::binary);
  708. if(!out.is_open())
  709. {
  710. std::cout<<"Unable to create "<<bufff;
  711. }
  712. else
  713. {
  714. for(int hh=0; hh<entries[i].realSize; ++hh)
  715. {
  716. out<<*(outp+hh);
  717. }
  718. out.close();
  719. }
  720. }
  721. else //we will decompressing file
  722. {
  723. outp = new unsigned char[entries[i].size];
  724. FLOD.read((char*)outp, entries[i].size);
  725. FLOD.seekg(0, std::ios_base::beg);
  726. std::ofstream destin;
  727. destin.open(bufff.c_str(), std::ios::binary);
  728. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  729. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  730. destin.close();
  731. if(decRes!=0)
  732. {
  733. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  734. }
  735. }
  736. //for (int j=0; j<entries[i].size; j++)
  737. // FOut << outp[j];
  738. //FOut.flush();
  739. delete outp;
  740. //FOut.close();
  741. //std::cout<<"*** done\n";
  742. }
  743. FLOD.close();
  744. //std::cout<<"*** Archive: "+FName+" closed\n";
  745. }
  746. int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic)
  747. {
  748. int ret=0;
  749. int amp=1;
  750. for (int i=0; i<bytCon; i++)
  751. {
  752. ret+=bufor[i]*amp;
  753. amp*=256;
  754. }
  755. if(cyclic && bytCon<4 && ret>=amp/2)
  756. {
  757. ret = ret-amp;
  758. }
  759. return ret;
  760. }
  761. void CLodHandler::init(std::string lodFile)
  762. {
  763. FLOD;
  764. std::string Ts;
  765. //std::cout<<"*** Loading FAT ... \n";
  766. FLOD.open(lodFile.c_str(),std::ios::binary);
  767. //std::cout<<"*** Archive: "+FName+" loaded\n";
  768. FLOD.seekg(8,std::ios_base::beg);
  769. unsigned char temp[4];
  770. FLOD.read((char*)temp,4);
  771. totalFiles = readNormalNr(temp,4);
  772. FLOD.seekg(0x5c,std::ios_base::beg);
  773. entries.reserve(totalFiles);
  774. //std::cout<<"*** Loading FAT ...\n";
  775. for (int i=0; i<totalFiles; i++)
  776. {
  777. entries.push_back(Entry());
  778. //FLOD.read((char*)entries[i].name,12);
  779. char * bufc = new char;
  780. bool appending = true;
  781. for(int kk=0; kk<12; ++kk)
  782. {
  783. FLOD.read(bufc, 1);
  784. if(appending)
  785. {
  786. entries[i].name[kk] = toupper(*bufc);
  787. }
  788. else
  789. {
  790. entries[i].name[kk] = 0;
  791. appending = false;
  792. }
  793. }
  794. delete bufc;
  795. FLOD.read((char*)entries[i].hlam_1,4);
  796. FLOD.read((char*)temp,4);
  797. entries[i].offset=readNormalNr(temp,4);
  798. FLOD.read((char*)temp,4);
  799. entries[i].realSize=readNormalNr(temp,4);
  800. FLOD.read((char*)entries[i].hlam_2,4);
  801. FLOD.read((char*)temp,4);
  802. entries[i].size=readNormalNr(temp,4);
  803. }
  804. }