CLodHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. delete [] pcd;
  288. }
  289. CPCXConv cp;
  290. cp.openPCX((char*)pcx,entries[index].realSize);
  291. return cp.getSurface();
  292. }
  293. int CLodHandler::decompress (unsigned char * source, int size, int realSize, std::string & dest)
  294. {
  295. std::ofstream lb;
  296. lb.open("lodbuf\\buf.gz", std::ios::out|std::ios::binary);
  297. for(int i=0; i<size; ++i)
  298. {
  299. lb<<source[i];
  300. }
  301. lb.close();
  302. FILE * inputf = fopen("lodbuf\\buf.gz", "rb+");
  303. FILE * outputf = fopen(dest.c_str(), "wb+");
  304. int ret = infm(inputf, outputf);
  305. fclose(inputf);
  306. fclose(outputf);
  307. return ret;
  308. }
  309. int CLodHandler::infm(FILE *source, FILE *dest, int wBits)
  310. {
  311. int ret;
  312. unsigned have;
  313. z_stream strm;
  314. unsigned char in[NLoadHandlerHelp::fCHUNK];
  315. unsigned char out[NLoadHandlerHelp::fCHUNK];
  316. /* allocate inflate state */
  317. strm.zalloc = Z_NULL;
  318. strm.zfree = Z_NULL;
  319. strm.opaque = Z_NULL;
  320. strm.avail_in = 0;
  321. strm.next_in = Z_NULL;
  322. ret = inflateInit2(&strm, wBits);
  323. if (ret != Z_OK)
  324. return ret;
  325. /* decompress until deflate stream ends or end of file */
  326. do
  327. {
  328. strm.avail_in = fread(in, 1, NLoadHandlerHelp::fCHUNK, source);
  329. if (ferror(source)) {
  330. (void)inflateEnd(&strm);
  331. return Z_ERRNO;
  332. }
  333. if (strm.avail_in == 0)
  334. break;
  335. strm.next_in = in;
  336. /* run inflate() on input until output buffer not full */
  337. do
  338. {
  339. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  340. strm.next_out = out;
  341. ret = inflate(&strm, Z_NO_FLUSH);
  342. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  343. switch (ret)
  344. {
  345. case Z_NEED_DICT:
  346. ret = Z_DATA_ERROR; /* and fall through */
  347. case Z_DATA_ERROR:
  348. case Z_MEM_ERROR:
  349. (void)inflateEnd(&strm);
  350. return ret;
  351. }
  352. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  353. if (fwrite(out, 1, have, dest) != have || ferror(dest))
  354. {
  355. (void)inflateEnd(&strm);
  356. return Z_ERRNO;
  357. }
  358. } while (strm.avail_out == 0);
  359. /* done when inflate() says it's done */
  360. } while (ret != Z_STREAM_END);
  361. /* clean up and return */
  362. (void)inflateEnd(&strm);
  363. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  364. }
  365. CDefHandler * CLodHandler::giveDef(std::string defName)
  366. {
  367. std::transform(defName.begin(), defName.end(), defName.begin(), (int(*)(int))toupper);
  368. Entry * ourEntry = entries.znajdz(Entry(defName));
  369. CDefHandler * ret;
  370. fseek(FLOD, ourEntry->offset, 0);
  371. unsigned char * outp;
  372. if (ourEntry->size==0) //file is not compressed
  373. {
  374. outp = new unsigned char[ourEntry->realSize];
  375. fread((char*)outp, 1, ourEntry->realSize, FLOD);
  376. CDefHandler * nh = new CDefHandler();
  377. nh->openFromMemory(outp, ourEntry->realSize, std::string((char*)ourEntry->name));
  378. nh->alphaTransformed = false;
  379. ret = nh;
  380. }
  381. else //we will decompress file
  382. {
  383. outp = new unsigned char[ourEntry->size];
  384. fread((char*)outp, 1, ourEntry->size, FLOD);
  385. fseek(FLOD, 0, 0);
  386. unsigned char * decomp = NULL;
  387. int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp);
  388. CDefHandler * nh = new CDefHandler();
  389. nh->openFromMemory(decomp, ourEntry->realSize, std::string((char*)ourEntry->name));
  390. nh->alphaTransformed = false;
  391. ret = nh;
  392. delete[] decomp;
  393. }
  394. delete[] outp;
  395. return ret;
  396. }
  397. CDefEssential * CLodHandler::giveDefEss(std::string defName)
  398. {
  399. CDefEssential * ret;
  400. CDefHandler * temp = giveDef(defName);
  401. ret = temp->essentialize();
  402. delete temp;
  403. return ret;
  404. }
  405. std::vector<CDefHandler *> CLodHandler::extractManyFiles(std::vector<std::string> defNamesIn)
  406. {
  407. std::vector<CDefHandler *> ret(defNamesIn.size());
  408. for(int hh=0; hh<defNamesIn.size(); ++hh)
  409. {
  410. std::transform(defNamesIn[hh].begin(), defNamesIn[hh].end(), defNamesIn[hh].begin(), (int(*)(int))toupper);
  411. }
  412. int i;
  413. std::vector<char> found(defNamesIn.size(), 0);
  414. for (int i=0;i<totalFiles;i++)
  415. {
  416. //std::cout<<'\r'<<"Reading defs: "<<(100.0*i)/((float)(totalFiles))<<"% ";
  417. std::string buf1 = std::string((char*)entries[i].name);
  418. //std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  419. bool exists = false;
  420. int curDef;
  421. for(int hh=0; hh<defNamesIn.size(); ++hh)
  422. {
  423. if(buf1==defNamesIn[hh])
  424. {
  425. exists = true;
  426. found[hh] = '\1';
  427. curDef = hh;
  428. break;
  429. }
  430. }
  431. if(!exists)
  432. continue;
  433. fseek(FLOD, entries[i].offset, 0);
  434. unsigned char * outp;
  435. if (entries[i].size==0) //file is not compressed
  436. {
  437. outp = new unsigned char[entries[i].realSize];
  438. fread((char*)outp, 1, entries[i].realSize, FLOD);
  439. CDefHandler * nh = new CDefHandler;
  440. nh->openFromMemory(outp, entries[i].realSize, std::string((char*)entries[i].name));
  441. nh->alphaTransformed = false;
  442. ret[curDef] = nh;
  443. }
  444. else //we will decompressing file
  445. {
  446. outp = new unsigned char[entries[i].size];
  447. fread((char*)outp, 1, entries[i].size, FLOD);
  448. fseek(FLOD, 0, 0);
  449. unsigned char * decomp = NULL;
  450. int decRes = infs2(outp, entries[i].size, entries[i].realSize, decomp);
  451. CDefHandler * nh = new CDefHandler;
  452. nh->openFromMemory(decomp, entries[i].realSize, std::string((char*)entries[i].name));
  453. nh->alphaTransformed = false;
  454. delete [] decomp;
  455. ret[curDef] = nh;
  456. }
  457. delete[] outp;
  458. }
  459. //std::cout<<'\r'<<"Reading defs: 100% "<<std::endl;
  460. for(int hh=0; hh<found.size(); ++hh)
  461. {
  462. if(!found[hh])
  463. {
  464. for(int ff=0; ff<hh; ++ff)
  465. {
  466. if(defNamesIn[ff]==defNamesIn[hh])
  467. {
  468. ret[hh]=ret[ff];
  469. }
  470. }
  471. }
  472. }
  473. //std::cout<<"*** Archive: "+FName+" closed\n";
  474. return ret;
  475. }
  476. int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
  477. {
  478. int ret;
  479. unsigned have;
  480. z_stream strm;
  481. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  482. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  483. /* allocate inflate state */
  484. strm.zalloc = Z_NULL;
  485. strm.zfree = Z_NULL;
  486. strm.opaque = Z_NULL;
  487. strm.avail_in = 0;
  488. strm.next_in = Z_NULL;
  489. ret = inflateInit2(&strm, wBits);
  490. if (ret != Z_OK)
  491. return ret;
  492. int chunkNumber = 0;
  493. do
  494. {
  495. int readBytes = 0;
  496. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  497. {
  498. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  499. ++readBytes;
  500. }
  501. ++chunkNumber;
  502. strm.avail_in = readBytes;
  503. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  504. /*if (in.bad())
  505. {
  506. (void)inflateEnd(&strm);
  507. return Z_ERRNO;
  508. }*/
  509. if (strm.avail_in == 0)
  510. break;
  511. strm.next_in = inx;
  512. /* run inflate() on input until output buffer not full */
  513. do
  514. {
  515. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  516. strm.next_out = outx;
  517. ret = inflate(&strm, Z_NO_FLUSH);
  518. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  519. switch (ret)
  520. {
  521. case Z_NEED_DICT:
  522. ret = Z_DATA_ERROR; /* and fall through */
  523. case Z_DATA_ERROR:
  524. case Z_MEM_ERROR:
  525. (void)inflateEnd(&strm);
  526. return ret;
  527. }
  528. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  529. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  530. {
  531. (void)inflateEnd(&strm);
  532. return Z_ERRNO;
  533. }*/
  534. out.write((char*)outx, have);
  535. if(out.bad())
  536. {
  537. (void)inflateEnd(&strm);
  538. return Z_ERRNO;
  539. }
  540. } while (strm.avail_out == 0);
  541. /* done when inflate() says it's done */
  542. } while (ret != Z_STREAM_END);
  543. /* clean up and return */
  544. (void)inflateEnd(&strm);
  545. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  546. }
  547. int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
  548. {
  549. int ret;
  550. unsigned have;
  551. z_stream strm;
  552. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  553. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  554. out = new unsigned char [realSize];
  555. int latPosOut = 0;
  556. /* allocate inflate state */
  557. strm.zalloc = Z_NULL;
  558. strm.zfree = Z_NULL;
  559. strm.opaque = Z_NULL;
  560. strm.avail_in = 0;
  561. strm.next_in = Z_NULL;
  562. ret = inflateInit2(&strm, wBits);
  563. if (ret != Z_OK)
  564. return ret;
  565. int chunkNumber = 0;
  566. do
  567. {
  568. int readBytes = 0;
  569. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  570. {
  571. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  572. ++readBytes;
  573. }
  574. ++chunkNumber;
  575. strm.avail_in = readBytes;
  576. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  577. /*if (in.bad())
  578. {
  579. (void)inflateEnd(&strm);
  580. return Z_ERRNO;
  581. }*/
  582. if (strm.avail_in == 0)
  583. break;
  584. strm.next_in = inx;
  585. /* run inflate() on input until output buffer not full */
  586. do
  587. {
  588. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  589. strm.next_out = outx;
  590. ret = inflate(&strm, Z_NO_FLUSH);
  591. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  592. switch (ret)
  593. {
  594. case Z_NEED_DICT:
  595. ret = Z_DATA_ERROR; /* and fall through */
  596. case Z_DATA_ERROR:
  597. case Z_MEM_ERROR:
  598. (void)inflateEnd(&strm);
  599. return ret;
  600. }
  601. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  602. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  603. {
  604. (void)inflateEnd(&strm);
  605. return Z_ERRNO;
  606. }*/
  607. //out.write((char*)outx, have);
  608. for(int oo=0; oo<have; ++oo)
  609. {
  610. out[latPosOut] = outx[oo];
  611. ++latPosOut;
  612. }
  613. /*if(out.bad())
  614. {
  615. (void)inflateEnd(&strm);
  616. return Z_ERRNO;
  617. }*/
  618. } while (strm.avail_out == 0);
  619. /* done when inflate() says it's done */
  620. } while (ret != Z_STREAM_END);
  621. /* clean up and return */
  622. (void)inflateEnd(&strm);
  623. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  624. }
  625. void CLodHandler::extract(std::string FName)
  626. {
  627. std::ofstream FOut;
  628. int i;
  629. //std::cout<<" done\n";
  630. for (int i=0;i<totalFiles;i++)
  631. {
  632. fseek(FLOD, entries[i].offset, 0);
  633. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  634. unsigned char * outp;
  635. if (entries[i].size==0) //file is not compressed
  636. {
  637. outp = new unsigned char[entries[i].realSize];
  638. fread((char*)outp, 1, entries[i].realSize, FLOD);
  639. std::ofstream out;
  640. out.open(bufff.c_str(), std::ios::binary);
  641. if(!out.is_open())
  642. {
  643. std::cout<<"Unable to create "<<bufff;
  644. }
  645. else
  646. {
  647. for(int hh=0; hh<entries[i].realSize; ++hh)
  648. {
  649. out<<*(outp+hh);
  650. }
  651. out.close();
  652. }
  653. }
  654. else
  655. {
  656. outp = new unsigned char[entries[i].size];
  657. fread((char*)outp, 1, entries[i].size, FLOD);
  658. fseek(FLOD, 0, 0);
  659. std::ofstream destin;
  660. destin.open(bufff.c_str(), std::ios::binary);
  661. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  662. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  663. destin.close();
  664. if(decRes!=0)
  665. {
  666. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  667. }
  668. }
  669. delete[] outp;
  670. }
  671. fclose(FLOD);
  672. }
  673. void CLodHandler::extractFile(std::string FName, std::string name)
  674. {
  675. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  676. for (int i=0;i<totalFiles;i++)
  677. {
  678. std::string buf1 = std::string((char*)entries[i].name);
  679. std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  680. if(buf1!=name)
  681. continue;
  682. fseek(FLOD, entries[i].offset, 0);
  683. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  684. unsigned char * outp;
  685. if (entries[i].size==0) //file is not compressed
  686. {
  687. outp = new unsigned char[entries[i].realSize];
  688. fread((char*)outp, 1, entries[i].realSize, FLOD);
  689. std::ofstream out;
  690. out.open(bufff.c_str(), std::ios::binary);
  691. if(!out.is_open())
  692. {
  693. std::cout<<"Unable to create "<<bufff;
  694. }
  695. else
  696. {
  697. for(int hh=0; hh<entries[i].realSize; ++hh)
  698. {
  699. out<<*(outp+hh);
  700. }
  701. out.close();
  702. }
  703. }
  704. else //we will decompressing file
  705. {
  706. outp = new unsigned char[entries[i].size];
  707. fread((char*)outp, 1, entries[i].size, FLOD);
  708. fseek(FLOD, 0, 0);
  709. std::ofstream destin;
  710. destin.open(bufff.c_str(), std::ios::binary);
  711. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  712. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  713. destin.close();
  714. if(decRes!=0)
  715. {
  716. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  717. }
  718. }
  719. delete[] outp;
  720. }
  721. fclose(FLOD);
  722. }
  723. int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic)
  724. {
  725. int ret=0;
  726. int amp=1;
  727. for (int i=0; i<bytCon; i++)
  728. {
  729. ret+=bufor[i]*amp;
  730. amp*=256;
  731. }
  732. if(cyclic && bytCon<4 && ret>=amp/2)
  733. {
  734. ret = ret-amp;
  735. }
  736. return ret;
  737. }
  738. void CLodHandler::init(std::string lodFile)
  739. {
  740. std::string Ts;
  741. FLOD = fopen(lodFile.c_str(), "rb");
  742. fseek(FLOD, 8, 0);
  743. unsigned char temp[4];
  744. fread((char*)temp, 1, 4, FLOD);
  745. totalFiles = readNormalNr(temp,4);
  746. fseek(FLOD, 0x5c, 0);
  747. for (int i=0; i<totalFiles; i++)
  748. {
  749. Entry entry;
  750. char * bufc = new char;
  751. bool appending = true;
  752. for(int kk=0; kk<12; ++kk)
  753. {
  754. //FLOD.read(bufc, 1);
  755. fread(bufc, 1, 1, FLOD);
  756. if(appending)
  757. {
  758. entry.name[kk] = toupper(*bufc);
  759. }
  760. else
  761. {
  762. entry.name[kk] = 0;
  763. appending = false;
  764. }
  765. }
  766. delete bufc;
  767. fread((char*)entry.hlam_1, 1, 4, FLOD);
  768. fread((char*)temp, 1, 4, FLOD);
  769. entry.offset=readNormalNr(temp,4);
  770. fread((char*)temp, 1, 4, FLOD);
  771. entry.realSize=readNormalNr(temp,4);
  772. fread((char*)entry.hlam_2, 1, 4, FLOD);
  773. fread((char*)temp, 1, 4, FLOD);
  774. entry.size=readNormalNr(temp,4);
  775. for (int z=0;z<12;z++)
  776. {
  777. if (entry.name[z])
  778. entry.nameStr+=entry.name[z];
  779. else break;
  780. }
  781. entries.push_back(entry);
  782. }
  783. }
  784. std::string CLodHandler::getTextFile(std::string name)
  785. {
  786. std::string ret0;
  787. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  788. int i;
  789. for (int i=0;i<totalFiles;i++)
  790. {
  791. std::string buf1 = std::string((char*)entries[i].name);
  792. bool exists = false;
  793. int curDef;
  794. if(buf1!=name)
  795. continue;
  796. fseek(FLOD, entries[i].offset, 0);
  797. unsigned char * outp;
  798. if (entries[i].size==0) //file is not compressed
  799. {
  800. outp = new unsigned char[entries[i].realSize];
  801. fread((char*)outp, 1, entries[i].realSize, FLOD);
  802. std::string ret = std::string((char*)outp);
  803. delete[] outp;
  804. return ret;
  805. }
  806. else //we will decompressing file
  807. {
  808. outp = new unsigned char[entries[i].size];
  809. fread((char*)outp, 1, entries[i].size, FLOD);
  810. fseek(FLOD, 0, 0);
  811. unsigned char * decomp = NULL;
  812. int decRes = infs2(outp, entries[i].size, entries[i].realSize, decomp);
  813. std::string ret;
  814. for (int itr=0;itr<entries[i].realSize;itr++)
  815. ret+= *((char*)decomp+itr);
  816. delete[] outp;
  817. delete[] decomp;
  818. return ret;
  819. }
  820. }
  821. return ret0;
  822. }