CLodHandler.cpp 21 KB

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