CLodHandler.cpp 20 KB

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