CLodHandler.cpp 20 KB

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