grfio.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. /*********************************************************************
  4. *
  5. * Ragnarok Online Emulator : grfio.c -- grf file I/O Module
  6. *--------------------------------------------------------------------
  7. * special need library : zlib
  8. *********************************************************************
  9. * $Id: grfio.c,v 1.2 2004/09/29 17:31:49 kalaspuff Exp $
  10. *
  11. * 2002/12/18... the original edition
  12. * 2003/01/23 ... Code correction
  13. * 2003/02/01 ... An addition and decryption processing are improved for LocalFile and two or more GRF(s) check processing.
  14. * 2003/02/02 ... Even if there is no grf it does not stop -- as -- correction
  15. * 2003/02/02... grf reading specification can be added later -- as -- correction (grfio_add function addition)
  16. * 2003/02 / 03... at the time of grfio_resourcecheck processing the entry addition processing method -- correction
  17. * 2003/02/05... change of the processing in grfio_init
  18. * 2003/02/23... a local file check -- GRFIO_LOCAL -- switch (Defoe -- Function Off)
  19. * 2003/10/21 ... The data of alpha client was read.
  20. * 2003/11/10 ... Ready new grf format.
  21. * 2003/11/11 ... version check fix & bug fix
  22. * 2006/04/16 ... fixed crash grfio_find_file when file is not found.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <sys/stat.h>
  29. #include "grfio.h"
  30. #include "../common/mmo.h"
  31. #include "../common/showmsg.h"
  32. #include "../common/malloc.h"
  33. #include "../zlib/unzip.h"
  34. #define CHUNK 16384
  35. #ifdef __WIN32
  36. #include "../zlib/zlib.h"
  37. #include "../zlib/iowin32.h"
  38. #else
  39. #ifndef __FREEBSD__
  40. #include <zlib.h>
  41. #endif
  42. #endif
  43. typedef unsigned char BYTE;
  44. typedef unsigned short WORD;
  45. typedef unsigned long DWORD;
  46. //static char data_file[1024] = ""; // "data.grf";
  47. //static char sdata_file[1024] = ""; // "sdata.grf";
  48. //static char adata_file[1024] = ""; // "adata.grf";
  49. static char data_dir[1024] = ""; // "../";
  50. //----------------------------
  51. // file entry table struct
  52. //----------------------------
  53. typedef struct {
  54. int srclen; // compressed size
  55. int srclen_aligned; //
  56. int declen; // original size
  57. int srcpos;
  58. short next;
  59. int cycle;
  60. char type;
  61. char fn[128-4*5]; // file name
  62. char *fnd;
  63. char gentry; // read grf file select
  64. } FILELIST;
  65. //gentry ... 0 : It acquires from a local file.
  66. // It acquires from the resource file of 1>=:gentry_table[gentry-1].
  67. // 1<=: Check a local file.
  68. // If it is, after re-setting to 0, it acquires from a local file.
  69. // If there is nothing, mark reversal will be carried out, and it will re-set, and will acquire from a resource file as well as 1>=.
  70. //Since char defines *FILELIST.gentry, the maximum which can be added by grfio_add becomes by 127 pieces.
  71. #define GENTRY_LIMIT 512
  72. #define FILELIST_LIMIT 1048576 // temporary maximum, and a theory top maximum are 2G.
  73. static FILELIST *filelist = NULL;
  74. static int filelist_entrys = 0;
  75. static int filelist_maxentry = 0;
  76. static char **gentry_table = NULL;
  77. static int gentry_entrys = 0;
  78. static int gentry_maxentry = 0;
  79. //----------------------------
  80. // file list hash table
  81. //----------------------------
  82. static int filelist_hash[256];
  83. //----------------------------
  84. // grf decode data table
  85. //----------------------------
  86. static unsigned char BitMaskTable[8] = {
  87. 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  88. };
  89. static char BitSwapTable1[64] = {
  90. 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
  91. 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
  92. 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
  93. 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
  94. };
  95. static char BitSwapTable2[64] = {
  96. 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
  97. 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
  98. 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
  99. 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
  100. };
  101. static char BitSwapTable3[32] = {
  102. 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
  103. 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
  104. };
  105. static unsigned char NibbleData[4][64]={
  106. {
  107. 0xef, 0x03, 0x41, 0xfd, 0xd8, 0x74, 0x1e, 0x47, 0x26, 0xef, 0xfb, 0x22, 0xb3, 0xd8, 0x84, 0x1e,
  108. 0x39, 0xac, 0xa7, 0x60, 0x62, 0xc1, 0xcd, 0xba, 0x5c, 0x96, 0x90, 0x59, 0x05, 0x3b, 0x7a, 0x85,
  109. 0x40, 0xfd, 0x1e, 0xc8, 0xe7, 0x8a, 0x8b, 0x21, 0xda, 0x43, 0x64, 0x9f, 0x2d, 0x14, 0xb1, 0x72,
  110. 0xf5, 0x5b, 0xc8, 0xb6, 0x9c, 0x37, 0x76, 0xec, 0x39, 0xa0, 0xa3, 0x05, 0x52, 0x6e, 0x0f, 0xd9,
  111. }, {
  112. 0xa7, 0xdd, 0x0d, 0x78, 0x9e, 0x0b, 0xe3, 0x95, 0x60, 0x36, 0x36, 0x4f, 0xf9, 0x60, 0x5a, 0xa3,
  113. 0x11, 0x24, 0xd2, 0x87, 0xc8, 0x52, 0x75, 0xec, 0xbb, 0xc1, 0x4c, 0xba, 0x24, 0xfe, 0x8f, 0x19,
  114. 0xda, 0x13, 0x66, 0xaf, 0x49, 0xd0, 0x90, 0x06, 0x8c, 0x6a, 0xfb, 0x91, 0x37, 0x8d, 0x0d, 0x78,
  115. 0xbf, 0x49, 0x11, 0xf4, 0x23, 0xe5, 0xce, 0x3b, 0x55, 0xbc, 0xa2, 0x57, 0xe8, 0x22, 0x74, 0xce,
  116. }, {
  117. 0x2c, 0xea, 0xc1, 0xbf, 0x4a, 0x24, 0x1f, 0xc2, 0x79, 0x47, 0xa2, 0x7c, 0xb6, 0xd9, 0x68, 0x15,
  118. 0x80, 0x56, 0x5d, 0x01, 0x33, 0xfd, 0xf4, 0xae, 0xde, 0x30, 0x07, 0x9b, 0xe5, 0x83, 0x9b, 0x68,
  119. 0x49, 0xb4, 0x2e, 0x83, 0x1f, 0xc2, 0xb5, 0x7c, 0xa2, 0x19, 0xd8, 0xe5, 0x7c, 0x2f, 0x83, 0xda,
  120. 0xf7, 0x6b, 0x90, 0xfe, 0xc4, 0x01, 0x5a, 0x97, 0x61, 0xa6, 0x3d, 0x40, 0x0b, 0x58, 0xe6, 0x3d,
  121. }, {
  122. 0x4d, 0xd1, 0xb2, 0x0f, 0x28, 0xbd, 0xe4, 0x78, 0xf6, 0x4a, 0x0f, 0x93, 0x8b, 0x17, 0xd1, 0xa4,
  123. 0x3a, 0xec, 0xc9, 0x35, 0x93, 0x56, 0x7e, 0xcb, 0x55, 0x20, 0xa0, 0xfe, 0x6c, 0x89, 0x17, 0x62,
  124. 0x17, 0x62, 0x4b, 0xb1, 0xb4, 0xde, 0xd1, 0x87, 0xc9, 0x14, 0x3c, 0x4a, 0x7e, 0xa8, 0xe2, 0x7d,
  125. 0xa0, 0x9f, 0xf6, 0x5c, 0x6a, 0x09, 0x8d, 0xf0, 0x0f, 0xe3, 0x53, 0x25, 0x95, 0x36, 0x28, 0xcb,
  126. }
  127. };
  128. /*-----------------
  129. * long data get
  130. */
  131. static unsigned int getlong(unsigned char *p)
  132. {
  133. // return *p+p[1]*256+(p[2]+p[3]*256)*65536;
  134. return p[0]
  135. | p[1] << 0x08
  136. | p[2] << 0x10
  137. | p[3] << 0x18; // Shinomori
  138. }
  139. /*==========================================
  140. * Grf data decode : Subs
  141. *------------------------------------------
  142. */
  143. static void NibbleSwap(BYTE *Src, int len)
  144. {
  145. for(;0<len;len--,Src++) {
  146. *Src = (*Src>>4) | (*Src<<4);
  147. }
  148. }
  149. static void BitConvert(BYTE *Src,char *BitSwapTable)
  150. {
  151. int lop,prm;
  152. BYTE tmp[8];
  153. // *(DWORD*)tmp=*(DWORD*)(tmp+4)=0;
  154. memset(tmp,0,8);
  155. for(lop=0;lop!=64;lop++) {
  156. prm = BitSwapTable[lop]-1;
  157. if (Src[(prm >> 3) & 7] & BitMaskTable[prm & 7]) {
  158. tmp[(lop >> 3) & 7] |= BitMaskTable[lop & 7];
  159. }
  160. }
  161. // *(DWORD*)Src = *(DWORD*)tmp;
  162. // *(DWORD*)(Src+4) = *(DWORD*)(tmp+4);
  163. memcpy(Src,tmp,8);
  164. }
  165. static void BitConvert4(BYTE *Src)
  166. {
  167. int lop,prm;
  168. BYTE tmp[8];
  169. tmp[0] = ((Src[7]<<5) | (Src[4]>>3)) & 0x3f; // ..0 vutsr
  170. tmp[1] = ((Src[4]<<1) | (Src[5]>>7)) & 0x3f; // ..srqpo n
  171. tmp[2] = ((Src[4]<<5) | (Src[5]>>3)) & 0x3f; // ..o nmlkj
  172. tmp[3] = ((Src[5]<<1) | (Src[6]>>7)) & 0x3f; // ..kjihg f
  173. tmp[4] = ((Src[5]<<5) | (Src[6]>>3)) & 0x3f; // ..g fedcb
  174. tmp[5] = ((Src[6]<<1) | (Src[7]>>7)) & 0x3f; // ..cba98 7
  175. tmp[6] = ((Src[6]<<5) | (Src[7]>>3)) & 0x3f; // ..8 76543
  176. tmp[7] = ((Src[7]<<1) | (Src[4]>>7)) & 0x3f; // ..43210 v
  177. for(lop=0;lop!=4;lop++) {
  178. tmp[lop] = (NibbleData[lop][tmp[lop*2]] & 0xf0)
  179. | (NibbleData[lop][tmp[lop*2+1]] & 0x0f);
  180. }
  181. *(DWORD*)(tmp+4)=0;
  182. for(lop=0;lop!=32;lop++) {
  183. prm = BitSwapTable3[lop]-1;
  184. if (tmp[prm >> 3] & BitMaskTable[prm & 7]) {
  185. tmp[(lop >> 3) + 4] |= BitMaskTable[lop & 7];
  186. }
  187. }
  188. // *(DWORD*)Src ^= *(DWORD*)(tmp+4);
  189. Src[0] ^= tmp[4];
  190. Src[1] ^= tmp[5];
  191. Src[2] ^= tmp[6];
  192. Src[3] ^= tmp[7];
  193. }
  194. static void decode_des_etc(BYTE *buf,int len,int type,int cycle)
  195. {
  196. int lop,cnt=0;
  197. if(cycle<3) cycle=3;
  198. else if(cycle<5) cycle++;
  199. else if(cycle<7) cycle+=9;
  200. else cycle+=15;
  201. for(lop=0;lop*8<len;lop++,buf+=8) {
  202. if(lop<20 || (type==0 && lop%cycle==0)){ // des
  203. BitConvert(buf,BitSwapTable1);
  204. BitConvert4(buf);
  205. BitConvert(buf,BitSwapTable2);
  206. } else {
  207. if(cnt==7 && type==0){
  208. int a;
  209. BYTE tmp[8];
  210. *(DWORD*)tmp = *(DWORD*)buf;
  211. *(DWORD*)(tmp+4) = *(DWORD*)(buf+4);
  212. cnt=0;
  213. buf[0]=tmp[3];
  214. buf[1]=tmp[4];
  215. buf[2]=tmp[6];
  216. buf[3]=tmp[0];
  217. buf[4]=tmp[1];
  218. buf[5]=tmp[2];
  219. buf[6]=tmp[5];
  220. a=tmp[7];
  221. if(a==0x00) a=0x2b;
  222. else if(a==0x2b) a=0x00;
  223. else if(a==0x01) a=0x68;
  224. else if(a==0x68) a=0x01;
  225. else if(a==0x48) a=0x77;
  226. else if(a==0x77) a=0x48;
  227. else if(a==0x60) a=0xff;
  228. else if(a==0xff) a=0x60;
  229. else if(a==0x6c) a=0x80;
  230. else if(a==0x80) a=0x6c;
  231. else if(a==0xb9) a=0xc0;
  232. else if(a==0xc0) a=0xb9;
  233. else if(a==0xeb) a=0xfe;
  234. else if(a==0xfe) a=0xeb;
  235. buf[7]=a;
  236. }
  237. cnt++;
  238. }
  239. }
  240. }
  241. /*==========================================
  242. * Grf data decode sub : zip
  243. *------------------------------------------
  244. */
  245. int decode_zip(unsigned char *dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen)
  246. {
  247. z_stream stream;
  248. int err;
  249. stream.next_in = (Bytef*)source;
  250. stream.avail_in = (uInt)sourceLen;
  251. /* Check for source > 64K on 16-bit machine: */
  252. if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  253. stream.next_out = (Bytef*) dest;
  254. stream.avail_out = (uInt)*destLen;
  255. if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  256. stream.zalloc = (alloc_func)0;
  257. stream.zfree = (free_func)0;
  258. err = inflateInit(&stream);
  259. if (err != Z_OK) return err;
  260. err = inflate(&stream, Z_FINISH);
  261. if (err != Z_STREAM_END) {
  262. inflateEnd(&stream);
  263. return err == Z_OK ? Z_BUF_ERROR : err;
  264. }
  265. *destLen = stream.total_out;
  266. err = inflateEnd(&stream);
  267. return err;
  268. }
  269. int encode_zip(unsigned char *dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen) {
  270. z_stream stream;
  271. int err;
  272. memset(&stream, 0, sizeof(stream));
  273. stream.next_in = (Bytef*)source;
  274. stream.avail_in = (uInt)sourceLen;
  275. /* Check for source > 64K on 16-bit machine: */
  276. if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  277. stream.next_out = (Bytef*) dest;
  278. stream.avail_out = (uInt)*destLen;
  279. if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  280. stream.zalloc = (alloc_func)0;
  281. stream.zfree = (free_func)0;
  282. err = deflateInit(&stream,Z_DEFAULT_COMPRESSION);
  283. if (err != Z_OK) return err;
  284. err = deflate(&stream, Z_FINISH);
  285. if (err != Z_STREAM_END) {
  286. inflateEnd(&stream);
  287. return err == Z_OK ? Z_BUF_ERROR : err;
  288. }
  289. *destLen = stream.total_out;
  290. err = deflateEnd(&stream);
  291. return err;
  292. }
  293. /* ===================================
  294. * Unzips a file. 1: success, 0: error
  295. * Adapted from miniunz.c [Celest]
  296. * Version 1.01b, May 30th, 2004
  297. * Copyright (C) 1998-2004 Gilles Vollant
  298. * -------------------------------------
  299. */
  300. int deflate_file (const char *source, const char *filename)
  301. {
  302. #ifdef _WIN32
  303. zlib_filefunc_def ffunc;
  304. #endif
  305. unzFile uf = NULL;
  306. int err = UNZ_OK;
  307. uInt size_buf = 8192;
  308. FILE *fout = NULL;
  309. void *buf;
  310. #ifdef _WIN32
  311. fill_win32_filefunc(&ffunc);
  312. uf = unzOpen2(source, &ffunc);
  313. #else
  314. uf = unzOpen(source);
  315. #endif
  316. if (uf == NULL) {
  317. //printf("Cannot open %s\n", source);
  318. return 0;
  319. }
  320. //printf("%s opened\n", source);
  321. if (unzLocateFile(uf, filename, 0) != UNZ_OK) {
  322. //printf("file %s not found in the zipfile\n", filename);
  323. return 0;
  324. }
  325. err = unzOpenCurrentFilePassword(uf, NULL);
  326. //if (err != UNZ_OK)
  327. // printf("error %d with zipfile in unzOpenCurrentFilePassword\n", err);
  328. fout = fopen(filename,"wb");
  329. if (fout == NULL) {
  330. //printf("error opening %s\n", filename);
  331. return 0;
  332. }
  333. buf = (void *)aMalloc(size_buf);
  334. do {
  335. err = unzReadCurrentFile(uf, buf, size_buf);
  336. if (err < 0) {
  337. //printf("error %d with zipfile in unzReadCurrentFile\n", err);
  338. break;
  339. }
  340. if (err > 0 &&
  341. fwrite(buf, err, 1, fout)!=1)
  342. {
  343. //printf("error in writing extracted file\n");
  344. err = UNZ_ERRNO;
  345. break;
  346. }
  347. } while (err > 0);
  348. if (fout) fclose(fout);
  349. if (err == UNZ_OK) {
  350. err = unzCloseCurrentFile (uf);
  351. //if (err != UNZ_OK)
  352. // printf("error %d with zipfile in unzCloseCurrentFile\n", err);
  353. aFree(buf);
  354. return (err == UNZ_OK);
  355. }
  356. unzCloseCurrentFile(uf); /* don't lose the error */
  357. return 0;
  358. }
  359. unsigned long grfio_crc32 (const char *buf, unsigned int len)
  360. {
  361. return crc32(crc32(0L, Z_NULL, 0), buf, len);
  362. }
  363. /***********************************************************
  364. *** File List Subroutines ***
  365. ***********************************************************/
  366. /*==========================================
  367. * File List : Hash make
  368. *------------------------------------------
  369. */
  370. static int filehash(unsigned char *fname)
  371. {
  372. unsigned int hash=0;
  373. while(*fname) {
  374. hash = ((hash<<1)+(hash>>7)*9+tolower(*fname));
  375. fname++;
  376. }
  377. return hash & 255;
  378. }
  379. /*==========================================
  380. * File List : Hash initalize
  381. *------------------------------------------
  382. */
  383. static void hashinit(void)
  384. {
  385. int lop;
  386. for (lop = 0; lop < 256; lop++)
  387. filelist_hash[lop] = -1;
  388. }
  389. /*==========================================
  390. * File List : File find
  391. *------------------------------------------
  392. */
  393. static FILELIST *filelist_find(char *fname)
  394. {
  395. int hash;
  396. if (!filelist)
  397. return NULL;
  398. for (hash = filelist_hash[filehash((unsigned char *) fname)]; hash >= 0; hash = filelist[hash].next) {
  399. if(strcmpi(filelist[hash].fn, fname) == 0)
  400. break;
  401. }
  402. return (hash >= 0) ? &filelist[hash] : NULL;
  403. }
  404. char *grfio_find_file(char *fname){
  405. FILELIST *filelist = filelist_find(fname);
  406. if (!filelist) return NULL;
  407. return (!filelist->fnd?filelist->fn:filelist->fnd);
  408. }
  409. /*==========================================
  410. * File List : Filelist add
  411. *------------------------------------------
  412. */
  413. #define FILELIST_ADDS 1024 // number increment of file lists `
  414. static FILELIST* filelist_add(FILELIST *entry)
  415. {
  416. int hash;
  417. if (filelist_entrys >= FILELIST_LIMIT) {
  418. ShowFatalError("GRF filelist limit reached (filelist_add)!\n");
  419. exit(1);
  420. }
  421. if (filelist_entrys >= filelist_maxentry) {
  422. filelist = (FILELIST *)aRealloc(filelist, (filelist_maxentry + FILELIST_ADDS) * sizeof(FILELIST));
  423. memset(filelist + filelist_maxentry, '\0', FILELIST_ADDS * sizeof(FILELIST));
  424. filelist_maxentry += FILELIST_ADDS;
  425. }
  426. memcpy (&filelist[filelist_entrys], entry, sizeof(FILELIST));
  427. hash = filehash((unsigned char *) entry->fn);
  428. filelist[filelist_entrys].next = filelist_hash[hash];
  429. filelist_hash[hash] = filelist_entrys;
  430. filelist_entrys++;
  431. return &filelist[filelist_entrys - 1];
  432. }
  433. static FILELIST* filelist_modify(FILELIST *entry)
  434. {
  435. FILELIST *fentry;
  436. if ((fentry = filelist_find(entry->fn)) != NULL) {
  437. int tmp = fentry->next;
  438. memcpy(fentry, entry, sizeof(FILELIST));
  439. fentry->next = tmp;
  440. } else {
  441. fentry = filelist_add(entry);
  442. }
  443. return fentry;
  444. }
  445. /*==========================================
  446. * File List : filelist size adjust
  447. *------------------------------------------
  448. */
  449. static void filelist_adjust(void)
  450. {
  451. if (filelist != NULL) {
  452. if (filelist_maxentry > filelist_entrys) {
  453. filelist = (FILELIST *)aRealloc(
  454. filelist, filelist_entrys * sizeof(FILELIST));
  455. filelist_maxentry = filelist_entrys;
  456. }
  457. }
  458. }
  459. /***********************************************************
  460. *** Grfio Sobroutines ***
  461. ***********************************************************/
  462. /*==========================================
  463. * Grfio : Resource file size get
  464. *------------------------------------------
  465. */
  466. int grfio_size(char *fname)
  467. {
  468. FILELIST *entry;
  469. entry = filelist_find(fname);
  470. if (entry == NULL || entry->gentry < 0) { // LocalFileCheck
  471. char lfname[256], *p;
  472. FILELIST lentry;
  473. struct stat st;
  474. sprintf(lfname, "%s%s", data_dir, fname);
  475. for (p = &lfname[0]; *p != 0; p++)
  476. if (*p=='\\') *p = '/'; // * At the time of Unix
  477. if (stat(lfname, &st) == 0) {
  478. strncpy(lentry.fn, fname, sizeof(lentry.fn) - 1);
  479. lentry.fnd = NULL;
  480. lentry.declen = st.st_size;
  481. lentry.gentry = 0; // 0:LocalFile
  482. entry = filelist_modify(&lentry);
  483. } else if (entry == NULL) {
  484. ShowError("%s not found (grfio_size)\n", fname);
  485. //exit(1);
  486. return -1;
  487. }
  488. }
  489. return entry->declen;
  490. }
  491. /*==========================================
  492. * Grfio : Resource file read & size get
  493. *------------------------------------------
  494. */
  495. void* grfio_reads(char *fname, int *size)
  496. {
  497. FILE *in;
  498. FILELIST *entry;
  499. unsigned char *buf2 = NULL;
  500. entry = filelist_find(fname);
  501. if (entry == NULL || entry->gentry <= 0) { // LocalFileCheck
  502. char lfname[256], *p;
  503. FILELIST lentry;
  504. sprintf(lfname, "%s%s", data_dir, fname);
  505. for (p = &lfname[0]; *p != 0; p++)
  506. if (*p == '\\') *p = '/'; // * At the time of Unix
  507. in = fopen(lfname, "rb");
  508. if (in != NULL) {
  509. if (entry != NULL && entry->gentry == 0) {
  510. lentry.declen = entry->declen;
  511. } else {
  512. fseek(in,0,2); // SEEK_END
  513. lentry.declen = ftell(in);
  514. }
  515. fseek(in,0,0); // SEEK_SET
  516. buf2 = (unsigned char *)aCallocA(lentry.declen + 1024, 1);
  517. fread(buf2, 1, lentry.declen, in);
  518. fclose(in);
  519. strncpy(lentry.fn, fname, sizeof(lentry.fn) - 1);
  520. lentry.fnd = NULL;
  521. lentry.gentry = 0; // 0:LocalFile
  522. entry = filelist_modify(&lentry);
  523. } else {
  524. if (entry != NULL && entry->gentry < 0) {
  525. entry->gentry = -entry->gentry; // local file checked
  526. } else {
  527. ShowError("%s not found (grfio_reads - local file %s)\n", fname, lfname);
  528. return NULL;
  529. }
  530. }
  531. }
  532. if (entry != NULL && entry->gentry > 0) { // Archive[GRF] File Read
  533. char *gfname = gentry_table[entry->gentry - 1];
  534. in = fopen(gfname, "rb");
  535. if(in != NULL) {
  536. unsigned char *buf = (unsigned char *)aCallocA(entry->srclen_aligned + 1024, 1);
  537. fseek(in, entry->srcpos, 0);
  538. fread(buf, 1, entry->srclen_aligned, in);
  539. fclose(in);
  540. buf2 = (unsigned char *)aCallocA(entry->declen + 1024, 1);
  541. if (entry->type == 1 || entry->type == 3 || entry->type == 5) {
  542. uLongf len;
  543. if (entry->cycle >= 0)
  544. decode_des_etc(buf, entry->srclen_aligned, entry->cycle == 0, entry->cycle);
  545. len = entry->declen;
  546. decode_zip(buf2, &len, buf, entry->srclen);
  547. if (len != entry->declen) {
  548. ShowError("decode_zip size miss match err: %d != %d\n", (int)len, entry->declen);
  549. aFree(buf);
  550. aFree(buf2);
  551. return NULL;
  552. }
  553. } else {
  554. memcpy(buf2, buf, entry->declen);
  555. }
  556. aFree(buf);
  557. } else {
  558. ShowError("%s not found (grfio_reads - grf file %s)\n", fname, gfname);
  559. return NULL;
  560. }
  561. }
  562. if (size != NULL && entry != NULL)
  563. *size = entry->declen;
  564. return buf2;
  565. }
  566. /*==========================================
  567. * Resource filename decode
  568. *------------------------------------------
  569. */
  570. static char * decode_filename(unsigned char *buf,int len)
  571. {
  572. int lop;
  573. for(lop=0;lop<len;lop+=8) {
  574. NibbleSwap(&buf[lop],8);
  575. BitConvert(&buf[lop],BitSwapTable1);
  576. BitConvert4(&buf[lop]);
  577. BitConvert(&buf[lop],BitSwapTable2);
  578. }
  579. return (char*)buf;
  580. }
  581. /*==========================================
  582. * Grfio : Entry table read
  583. *------------------------------------------
  584. */
  585. static int grfio_entryread(char *gfname,int gentry)
  586. {
  587. FILE *fp;
  588. long grf_size,list_size;
  589. unsigned char grf_header[0x2e];
  590. int lop,entry,entrys,ofs,grf_version;
  591. char *fname;
  592. unsigned char *grf_filelist;
  593. fp = fopen(gfname, "rb");
  594. if (fp == NULL) {
  595. ShowWarning("GRF Data File not found: '"CL_WHITE"%s"CL_RESET"'.\n",gfname);
  596. return 1; // 1:not found error
  597. }
  598. fseek(fp,0,2); // SEEK_END
  599. grf_size = ftell(fp);
  600. fseek(fp,0,0); // SEEK_SET
  601. fread(grf_header,1,0x2e,fp);
  602. if (strcmp((const char *) grf_header,"Master of Magic") ||
  603. fseek(fp,getlong(grf_header+0x1e),1)) // SEEK_CUR
  604. {
  605. fclose(fp);
  606. ShowError("GRF %s read error\n",gfname);
  607. return 2; // 2:file format error
  608. }
  609. grf_version = getlong(grf_header+0x2a) >> 8;
  610. if (grf_version == 0x01) { //****** Grf version 01xx ******
  611. list_size = grf_size - ftell(fp);
  612. grf_filelist = (unsigned char *) aCallocA(list_size, 1);
  613. /*if (grf_filelist == NULL){
  614. fclose(fp);
  615. ShowError("out of memory : grf_filelist\n");
  616. return 3; // 3:memory alloc error
  617. }*/
  618. fread(grf_filelist,1,list_size,fp);
  619. fclose(fp);
  620. entrys = getlong(grf_header+0x26) - getlong(grf_header+0x22) - 7;
  621. // Get an entry
  622. for (entry = 0,ofs = 0; entry < entrys; entry++) {
  623. int ofs2, srclen, srccount, type;
  624. char *period_ptr;
  625. FILELIST aentry;
  626. ofs2 = ofs+getlong(grf_filelist+ofs)+4;
  627. type = grf_filelist[ofs2+12];
  628. if (type != 0) { // Directory Index ... skip
  629. fname = decode_filename(grf_filelist+ofs+6, grf_filelist[ofs]-6);
  630. if (strlen(fname) > sizeof(aentry.fn) - 1) {
  631. ShowFatalError("GRF file name %s is too long\n", fname);
  632. aFree(grf_filelist);
  633. exit(1);
  634. }
  635. srclen = 0;
  636. if ((period_ptr = strrchr(fname, '.')) != NULL) {
  637. for(lop = 0; lop < 4; lop++) {
  638. if (strcmpi(period_ptr, ".gnd\0.gat\0.act\0.str"+lop*5) == 0)
  639. break;
  640. }
  641. srclen = getlong(grf_filelist+ofs2) - getlong(grf_filelist+ofs2+8) - 715;
  642. if(lop == 4) {
  643. for(lop = 10, srccount = 1; srclen >= lop; lop = lop * 10, srccount++);
  644. } else {
  645. srccount = 0;
  646. }
  647. } else {
  648. srccount = 0;
  649. }
  650. aentry.srclen = srclen;
  651. aentry.srclen_aligned = getlong(grf_filelist+ofs2+4)-37579;
  652. aentry.declen = getlong(grf_filelist+ofs2+8);
  653. aentry.srcpos = getlong(grf_filelist+ofs2+13)+0x2e;
  654. aentry.cycle = srccount;
  655. aentry.type = type;
  656. strncpy(aentry.fn, fname,sizeof(aentry.fn)-1);
  657. aentry.fnd = NULL;
  658. #ifdef GRFIO_LOCAL
  659. aentry.gentry = -(gentry+1); // As Flag for making it a negative number carrying out the first time LocalFileCheck
  660. #else
  661. aentry.gentry = gentry+1; // With no first time LocalFileCheck
  662. #endif
  663. filelist_modify(&aentry);
  664. }
  665. ofs = ofs2 + 17;
  666. }
  667. aFree(grf_filelist);
  668. } else if (grf_version == 0x02) { //****** Grf version 02xx ******
  669. unsigned char eheader[8];
  670. unsigned char *rBuf;
  671. uLongf rSize, eSize;
  672. fread(eheader,1,8,fp);
  673. rSize = getlong(eheader); // Read Size
  674. eSize = getlong(eheader+4); // Extend Size
  675. if ((long)rSize > grf_size-ftell(fp)) { // Warning fix [Lance]
  676. fclose(fp);
  677. ShowError("Illegal data format : grf compress entry size\n");
  678. return 4;
  679. }
  680. rBuf = (unsigned char *)aCallocA(rSize , 1); // Get a Read Size
  681. /*if (rBuf==NULL) {
  682. fclose(fp);
  683. ShowError("out of memory : grf compress entry table buffer\n");
  684. return 3;
  685. }*/
  686. grf_filelist = (unsigned char *)aCallocA(eSize , 1); // Get a Extend Size
  687. /*if (grf_filelist==NULL) {
  688. aFree(rBuf);
  689. fclose(fp);
  690. ShowError("out of memory : grf extract entry table buffer\n");
  691. return 3;
  692. }*/
  693. fread(rBuf,1,rSize,fp);
  694. fclose(fp);
  695. decode_zip(grf_filelist, &eSize, rBuf, rSize); // Decode function
  696. list_size = eSize;
  697. aFree(rBuf);
  698. entrys = getlong(grf_header+0x26) - 7;
  699. // Get an entry
  700. for(entry = 0, ofs = 0; entry < entrys; entry++){
  701. int ofs2, srclen, srccount, type;
  702. FILELIST aentry;
  703. fname = (char*)(grf_filelist+ofs);
  704. if (strlen(fname) > sizeof(aentry.fn)-1) {
  705. ShowFatalError("GRF file name %s is too long\n", fname);
  706. aFree(grf_filelist);
  707. exit(1);
  708. }
  709. //ofs2 = ofs+strlen((char*)(grf_filelist+ofs))+1;
  710. ofs2 = ofs + strlen(fname)+1;
  711. type = grf_filelist[ofs2+12];
  712. if (type == 1 || type == 3 || type == 5) {
  713. srclen = getlong(grf_filelist+ofs2);
  714. if (grf_filelist[ofs2+12] == 3) {
  715. for (lop = 10, srccount = 1; srclen >= lop; lop = lop * 10, srccount++);
  716. } else if (grf_filelist[ofs2+12] == 5) {
  717. srccount = 0;
  718. } else { // if (grf_filelist[ofs2+12]==1) {
  719. srccount = -1;
  720. }
  721. aentry.srclen = srclen;
  722. aentry.srclen_aligned = getlong(grf_filelist+ofs2+4);
  723. aentry.declen = getlong(grf_filelist+ofs2+8);
  724. aentry.srcpos = getlong(grf_filelist+ofs2+13)+0x2e;
  725. aentry.cycle = srccount;
  726. aentry.type = type;
  727. strncpy(aentry.fn,fname,sizeof(aentry.fn)-1);
  728. aentry.fnd = NULL;
  729. #ifdef GRFIO_LOCAL
  730. aentry.gentry = -(gentry+1); // As Flag for making it a negative number carrying out the first time LocalFileCheck
  731. #else
  732. aentry.gentry = gentry+1; // With no first time LocalFileCheck
  733. #endif
  734. filelist_modify(&aentry);
  735. }
  736. ofs = ofs2 + 17;
  737. }
  738. aFree(grf_filelist);
  739. } else { //****** Grf Other version ******
  740. fclose(fp);
  741. ShowError("GRF version %04x not supported\n",getlong(grf_header+0x2a));
  742. return 4;
  743. }
  744. filelist_adjust(); // Unnecessary area release of filelist
  745. return 0; // 0:no error
  746. }
  747. /*==========================================
  748. * Grfio : Resource file check
  749. *------------------------------------------
  750. */
  751. static void grfio_resourcecheck(void)
  752. {
  753. char w1[256], w2[256], src[256], dst[256], restable[256], line[256];
  754. char *ptr, *buf;
  755. FILELIST *entry;
  756. int size, i = 0;
  757. FILE *fp;
  758. // read resnametable from data directory and return if successful
  759. sprintf(restable, "%sresnametable.txt", data_dir);
  760. for (ptr = &restable[0]; *ptr != 0; ptr++)
  761. if (*ptr == '\\') *ptr = '/';
  762. fp = fopen(restable,"rb");
  763. if (fp) {
  764. while (fgets(line, sizeof(line) - 1, fp)) {
  765. if (sscanf(line, "%[^#]#%[^#]#", w1, w2) == 2 &&
  766. // we only need the map names and text files
  767. (strstr(w2, ".gat") || strstr(w2, ".txt")))
  768. {
  769. sprintf(src, "%s", w1);
  770. sprintf(dst, "%s", w2);
  771. entry = filelist_find(dst);
  772. // create new entries reusing the original's info
  773. if (entry != NULL) {
  774. FILELIST fentry;
  775. memcpy(&fentry, entry, sizeof(FILELIST));
  776. strncpy(fentry.fn, src, sizeof(fentry.fn) - 1);
  777. fentry.fnd = grfio_alloc_ptr(w2);
  778. filelist_modify(&fentry);
  779. i++;
  780. }
  781. }
  782. }
  783. fclose(fp);
  784. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", i, "resnametable.txt");
  785. return; // we're done here!
  786. }
  787. // read resnametable from loaded GRF's, only if it cannot be
  788. // loaded from the data directory
  789. buf = (char *)grfio_reads("data\\resnametable.txt", &size);
  790. if (buf) {
  791. buf[size] = 0;
  792. ptr = buf;
  793. while (ptr - buf < size) {
  794. if (sscanf(ptr, "%[^#]#%[^#]#", w1, w2) == 2 &&
  795. (strstr(w2, ".gat") || strstr(w2, ".txt")))
  796. {
  797. sprintf(src, "data\\%s", w1);
  798. sprintf(dst, "data\\%s", w2);
  799. entry = filelist_find(dst);
  800. if (entry != NULL) {
  801. FILELIST fentry;
  802. memcpy(&fentry, entry, sizeof(FILELIST));
  803. strncpy(fentry.fn, src, sizeof(fentry.fn) - 1);
  804. fentry.fnd = grfio_alloc_ptr(w2);
  805. filelist_modify(&fentry);
  806. i++;
  807. }
  808. }
  809. ptr = strchr(ptr,'\n'); // Next line
  810. if (!ptr) break;
  811. ptr++;
  812. }
  813. aFree(buf);
  814. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", i, "data\\resnametable.txt");
  815. return;
  816. }
  817. //ShowWarning("GRF: No resnametable found! Panic?\n");
  818. }
  819. /*==========================================
  820. * Grfio : Resource add
  821. *------------------------------------------
  822. */
  823. #define GENTRY_ADDS 4 // The number increment of gentry_table entries
  824. static int grfio_add(char *fname)
  825. {
  826. grfio_alloc_ptr(fname);
  827. return grfio_entryread(fname, gentry_entrys - 1);
  828. }
  829. char *grfio_alloc_ptr(char *fname)
  830. {
  831. int len;
  832. char *buf;
  833. if (gentry_entrys >= GENTRY_LIMIT) {
  834. ShowFatalError("gentrys limit : grfio_add\n");
  835. exit(1);
  836. }
  837. if (gentry_entrys >= gentry_maxentry) {
  838. gentry_maxentry += GENTRY_ADDS;
  839. gentry_table = (char**)aRealloc(gentry_table, gentry_maxentry * sizeof(char*));
  840. memset(gentry_table + (gentry_maxentry - GENTRY_ADDS), 0, sizeof(char*) * GENTRY_ADDS);
  841. }
  842. len = strlen( fname );
  843. buf = (char*)aCallocA(len + 1, 1);
  844. strcpy(buf, fname);
  845. gentry_table[gentry_entrys++] = buf;
  846. return buf;
  847. }
  848. /*==========================================
  849. * Grfio : Finalize
  850. *------------------------------------------
  851. */
  852. void grfio_final(void)
  853. {
  854. if (filelist != NULL)
  855. aFree(filelist);
  856. filelist_entrys = filelist_maxentry = 0;
  857. if (gentry_table != NULL) {
  858. int lop;
  859. for (lop = 0; lop < gentry_entrys; lop++) {
  860. if (gentry_table[lop] != NULL)
  861. aFree(gentry_table[lop]);
  862. }
  863. aFree(gentry_table);
  864. }
  865. gentry_table = NULL;
  866. gentry_entrys = gentry_maxentry = 0;
  867. }
  868. /*==========================================
  869. * Grfio : Initialize
  870. *------------------------------------------
  871. */
  872. void grfio_init(char *fname)
  873. {
  874. FILE *data_conf;
  875. char line[1024], w1[1024], w2[1024];
  876. int result = 0;
  877. hashinit(); // hash table initialization
  878. data_conf = fopen(fname, "r");
  879. // It will read, if there is grf-files.txt.
  880. if (data_conf) {
  881. while(fgets(line, sizeof(line) - 1, data_conf)) {
  882. if (line[0] == '/' && line[1] == '/')
  883. continue;
  884. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  885. continue;
  886. // Entry table reading
  887. if(strcmp(w1, "grf") == 0 ||
  888. strcmp(w1, "data") == 0 || // Primary data file
  889. strcmp(w1, "sdata") == 0 || // Sakray data file
  890. strcmp(w1, "adata") == 0) // Alpha version data file
  891. // increment if successfully loaded
  892. result += (grfio_add(w2) == 0);
  893. else if(strcmp(w1,"data_dir") == 0) // Data directory
  894. strcpy(data_dir, w2);
  895. }
  896. fclose(data_conf);
  897. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n", fname);
  898. } // end of reading grf-files.txt
  899. if (result == 0) {
  900. ShowInfo("No grf's loaded.. using default data directory\n");
  901. //exit(1); // It ends, if a resource cannot read one.
  902. }
  903. // Unnecessary area release of filelist
  904. filelist_adjust();
  905. // Resource check
  906. grfio_resourcecheck();
  907. return;
  908. }