CLodHandler.cpp 20 KB

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