CLodHandler.cpp 21 KB

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