grfio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <ctype.h> // tolower()
  8. #include "grfio.h"
  9. #include <zlib.h>
  10. #ifndef __WIN32
  11. #define strcmpi strcasecmp
  12. #endif
  13. //----------------------------
  14. // file entry table struct
  15. //----------------------------
  16. typedef struct _FILELIST {
  17. int srclen; // compressed size
  18. int srclen_aligned;
  19. int declen; // original size
  20. int srcpos; // position of entry in grf
  21. int next; // index of next filelist entry with same hash (-1: end of entry chain)
  22. int cycle;
  23. char type;
  24. char fn[128-4*5]; // file name
  25. char* fnd; // if the file was cloned, contains name of original file
  26. char gentry; // read grf file select
  27. } FILELIST;
  28. //gentry ... > 0 : data read from a grf file (gentry_table[gentry-1])
  29. //gentry ... 0 : data read from a local file (data directory)
  30. //gentry ... < 0 : entry "-(gentry)" is marked for a local file check
  31. // - if local file exists, gentry will be set to 0 (thus using the local file)
  32. // - if local file doesn't exist, sign is inverted (thus using the original file inside a grf)
  33. // (NOTE: this case is only used once (during startup) and only if GRFIO_LOCAL is enabled)
  34. // (NOTE: probably meant to be used to override grf contents by files in the data directory)
  35. //#define GRFIO_LOCAL
  36. // stores info about every loaded file
  37. FILELIST* filelist = NULL;
  38. int filelist_entrys = 0;
  39. int filelist_maxentry = 0;
  40. // stores grf file names
  41. char** gentry_table = NULL;
  42. int gentry_entrys = 0;
  43. int gentry_maxentry = 0;
  44. // the path to the data directory
  45. char data_dir[1024] = "";
  46. //----------------------------
  47. // file list hash table
  48. //----------------------------
  49. int filelist_hash[256];
  50. //----------------------------
  51. // grf decode data table
  52. //----------------------------
  53. static unsigned char BitMaskTable[8] = {
  54. 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  55. };
  56. static char BitSwapTable1[64] = {
  57. 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
  58. 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
  59. 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
  60. 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
  61. };
  62. static char BitSwapTable2[64] = {
  63. 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
  64. 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
  65. 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
  66. 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
  67. };
  68. static char BitSwapTable3[32] = {
  69. 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
  70. 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
  71. };
  72. static unsigned char NibbleData[4][64]={
  73. {
  74. 0xef, 0x03, 0x41, 0xfd, 0xd8, 0x74, 0x1e, 0x47, 0x26, 0xef, 0xfb, 0x22, 0xb3, 0xd8, 0x84, 0x1e,
  75. 0x39, 0xac, 0xa7, 0x60, 0x62, 0xc1, 0xcd, 0xba, 0x5c, 0x96, 0x90, 0x59, 0x05, 0x3b, 0x7a, 0x85,
  76. 0x40, 0xfd, 0x1e, 0xc8, 0xe7, 0x8a, 0x8b, 0x21, 0xda, 0x43, 0x64, 0x9f, 0x2d, 0x14, 0xb1, 0x72,
  77. 0xf5, 0x5b, 0xc8, 0xb6, 0x9c, 0x37, 0x76, 0xec, 0x39, 0xa0, 0xa3, 0x05, 0x52, 0x6e, 0x0f, 0xd9,
  78. }, {
  79. 0xa7, 0xdd, 0x0d, 0x78, 0x9e, 0x0b, 0xe3, 0x95, 0x60, 0x36, 0x36, 0x4f, 0xf9, 0x60, 0x5a, 0xa3,
  80. 0x11, 0x24, 0xd2, 0x87, 0xc8, 0x52, 0x75, 0xec, 0xbb, 0xc1, 0x4c, 0xba, 0x24, 0xfe, 0x8f, 0x19,
  81. 0xda, 0x13, 0x66, 0xaf, 0x49, 0xd0, 0x90, 0x06, 0x8c, 0x6a, 0xfb, 0x91, 0x37, 0x8d, 0x0d, 0x78,
  82. 0xbf, 0x49, 0x11, 0xf4, 0x23, 0xe5, 0xce, 0x3b, 0x55, 0xbc, 0xa2, 0x57, 0xe8, 0x22, 0x74, 0xce,
  83. }, {
  84. 0x2c, 0xea, 0xc1, 0xbf, 0x4a, 0x24, 0x1f, 0xc2, 0x79, 0x47, 0xa2, 0x7c, 0xb6, 0xd9, 0x68, 0x15,
  85. 0x80, 0x56, 0x5d, 0x01, 0x33, 0xfd, 0xf4, 0xae, 0xde, 0x30, 0x07, 0x9b, 0xe5, 0x83, 0x9b, 0x68,
  86. 0x49, 0xb4, 0x2e, 0x83, 0x1f, 0xc2, 0xb5, 0x7c, 0xa2, 0x19, 0xd8, 0xe5, 0x7c, 0x2f, 0x83, 0xda,
  87. 0xf7, 0x6b, 0x90, 0xfe, 0xc4, 0x01, 0x5a, 0x97, 0x61, 0xa6, 0x3d, 0x40, 0x0b, 0x58, 0xe6, 0x3d,
  88. }, {
  89. 0x4d, 0xd1, 0xb2, 0x0f, 0x28, 0xbd, 0xe4, 0x78, 0xf6, 0x4a, 0x0f, 0x93, 0x8b, 0x17, 0xd1, 0xa4,
  90. 0x3a, 0xec, 0xc9, 0x35, 0x93, 0x56, 0x7e, 0xcb, 0x55, 0x20, 0xa0, 0xfe, 0x6c, 0x89, 0x17, 0x62,
  91. 0x17, 0x62, 0x4b, 0xb1, 0xb4, 0xde, 0xd1, 0x87, 0xc9, 0x14, 0x3c, 0x4a, 0x7e, 0xa8, 0xe2, 0x7d,
  92. 0xa0, 0x9f, 0xf6, 0x5c, 0x6a, 0x09, 0x8d, 0xf0, 0x0f, 0xe3, 0x53, 0x25, 0x95, 0x36, 0x28, 0xcb,
  93. }
  94. };
  95. // little endian char array to uint conversion
  96. static unsigned int getlong(unsigned char* p)
  97. {
  98. return (p[0] | p[1] << 0x08 | p[2] << 0x10 | p[3] << 0x18);
  99. }
  100. /*==========================================
  101. * Grf data decode : Subs
  102. *------------------------------------------*/
  103. static void NibbleSwap(unsigned char* Src, int len)
  104. {
  105. for(;0<len;len--,Src++) {
  106. *Src = (*Src>>4) | (*Src<<4);
  107. }
  108. }
  109. static void BitConvert(unsigned char* Src, char* BitSwapTable)
  110. {
  111. int lop,prm;
  112. unsigned char tmp[8];
  113. memset(tmp,0,8);
  114. for(lop=0;lop!=64;lop++) {
  115. prm = BitSwapTable[lop]-1;
  116. if (Src[(prm >> 3) & 7] & BitMaskTable[prm & 7]) {
  117. tmp[(lop >> 3) & 7] |= BitMaskTable[lop & 7];
  118. }
  119. }
  120. memcpy(Src,tmp,8);
  121. }
  122. static void BitConvert4(unsigned char* Src)
  123. {
  124. int lop,prm;
  125. unsigned char tmp[8];
  126. tmp[0] = ((Src[7]<<5) | (Src[4]>>3)) & 0x3f; // ..0 vutsr
  127. tmp[1] = ((Src[4]<<1) | (Src[5]>>7)) & 0x3f; // ..srqpo n
  128. tmp[2] = ((Src[4]<<5) | (Src[5]>>3)) & 0x3f; // ..o nmlkj
  129. tmp[3] = ((Src[5]<<1) | (Src[6]>>7)) & 0x3f; // ..kjihg f
  130. tmp[4] = ((Src[5]<<5) | (Src[6]>>3)) & 0x3f; // ..g fedcb
  131. tmp[5] = ((Src[6]<<1) | (Src[7]>>7)) & 0x3f; // ..cba98 7
  132. tmp[6] = ((Src[6]<<5) | (Src[7]>>3)) & 0x3f; // ..8 76543
  133. tmp[7] = ((Src[7]<<1) | (Src[4]>>7)) & 0x3f; // ..43210 v
  134. for(lop=0;lop!=4;lop++) {
  135. tmp[lop] = (NibbleData[lop][tmp[lop*2]] & 0xf0)
  136. | (NibbleData[lop][tmp[lop*2+1]] & 0x0f);
  137. }
  138. memset(tmp+4,0,4);
  139. for(lop=0;lop!=32;lop++) {
  140. prm = BitSwapTable3[lop]-1;
  141. if (tmp[prm >> 3] & BitMaskTable[prm & 7]) {
  142. tmp[(lop >> 3) + 4] |= BitMaskTable[lop & 7];
  143. }
  144. }
  145. Src[0] ^= tmp[4];
  146. Src[1] ^= tmp[5];
  147. Src[2] ^= tmp[6];
  148. Src[3] ^= tmp[7];
  149. }
  150. static void decode_des_etc(unsigned char* buf, size_t len, int type, int cycle)
  151. {
  152. size_t lop,cnt=0;
  153. if(cycle<3) cycle=3;
  154. else if(cycle<5) cycle++;
  155. else if(cycle<7) cycle+=9;
  156. else cycle+=15;
  157. for(lop=0; lop*8<len; lop++, buf+=8)
  158. {
  159. if(lop<20 || (type==0 && lop%cycle==0)) { // des
  160. BitConvert(buf,BitSwapTable1);
  161. BitConvert4(buf);
  162. BitConvert(buf,BitSwapTable2);
  163. } else {
  164. if(cnt==7 && type==0) {
  165. unsigned char a;
  166. unsigned char tmp[8];
  167. memcpy(tmp,buf,8);
  168. cnt=0;
  169. buf[0]=tmp[3];
  170. buf[1]=tmp[4];
  171. buf[2]=tmp[6];
  172. buf[3]=tmp[0];
  173. buf[4]=tmp[1];
  174. buf[5]=tmp[2];
  175. buf[6]=tmp[5];
  176. a=tmp[7];
  177. if(a==0x00) a=0x2b;
  178. else if(a==0x2b) a=0x00;
  179. else if(a==0x01) a=0x68;
  180. else if(a==0x68) a=0x01;
  181. else if(a==0x48) a=0x77;
  182. else if(a==0x77) a=0x48;
  183. else if(a==0x60) a=0xff;
  184. else if(a==0xff) a=0x60;
  185. else if(a==0x6c) a=0x80;
  186. else if(a==0x80) a=0x6c;
  187. else if(a==0xb9) a=0xc0;
  188. else if(a==0xc0) a=0xb9;
  189. else if(a==0xeb) a=0xfe;
  190. else if(a==0xfe) a=0xeb;
  191. buf[7]=a;
  192. }
  193. cnt++;
  194. }
  195. }
  196. }
  197. /*==========================================
  198. * Grf data decode sub : zip
  199. *------------------------------------------*/
  200. int decode_zip(unsigned char* dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen)
  201. {
  202. z_stream stream;
  203. int err;
  204. stream.next_in = (Bytef*)source;
  205. stream.avail_in = (uInt)sourceLen;
  206. /* Check for source > 64K on 16-bit machine: */
  207. if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  208. stream.next_out = (Bytef*) dest;
  209. stream.avail_out = (uInt)*destLen;
  210. if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  211. stream.zalloc = (alloc_func)0;
  212. stream.zfree = (free_func)0;
  213. err = inflateInit(&stream);
  214. if (err != Z_OK) return err;
  215. err = inflate(&stream, Z_FINISH);
  216. if (err != Z_STREAM_END) {
  217. inflateEnd(&stream);
  218. return err == Z_OK ? Z_BUF_ERROR : err;
  219. }
  220. *destLen = stream.total_out;
  221. err = inflateEnd(&stream);
  222. return err;
  223. }
  224. int encode_zip(unsigned char* dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen)
  225. {
  226. z_stream stream;
  227. int err;
  228. memset(&stream, 0, sizeof(stream));
  229. stream.next_in = (Bytef*)source;
  230. stream.avail_in = (uInt)sourceLen;
  231. /* Check for source > 64K on 16-bit machine: */
  232. if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  233. stream.next_out = (Bytef*) dest;
  234. stream.avail_out = (uInt)*destLen;
  235. if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  236. stream.zalloc = (alloc_func)0;
  237. stream.zfree = (free_func)0;
  238. err = deflateInit(&stream,Z_DEFAULT_COMPRESSION);
  239. if (err != Z_OK) return err;
  240. err = deflate(&stream, Z_FINISH);
  241. if (err != Z_STREAM_END) {
  242. inflateEnd(&stream);
  243. return err == Z_OK ? Z_BUF_ERROR : err;
  244. }
  245. *destLen = stream.total_out;
  246. err = deflateEnd(&stream);
  247. return err;
  248. }
  249. unsigned long grfio_crc32 (const unsigned char* buf, unsigned int len)
  250. {
  251. return crc32(crc32(0L, Z_NULL, 0), buf, len);
  252. }
  253. /***********************************************************
  254. *** File List Subroutines ***
  255. ***********************************************************/
  256. // initializes the table that holds the first elements of all hash chains
  257. static void hashinit(void)
  258. {
  259. int i;
  260. for (i = 0; i < 256; i++)
  261. filelist_hash[i] = -1;
  262. }
  263. // hashes a filename string into a number from {0..255}
  264. static int filehash(char* fname)
  265. {
  266. unsigned int hash = 0;
  267. while(*fname) {
  268. hash = ((hash<<1) + (hash>>7)*9 + (unsigned int)tolower((unsigned char)(*fname)));
  269. fname++;
  270. }
  271. return hash & 255;
  272. }
  273. // finds a FILELIST entry with the specified file name
  274. static FILELIST* filelist_find(char* fname)
  275. {
  276. int hash, index;
  277. if (!filelist)
  278. return NULL;
  279. hash = filelist_hash[filehash(fname)];
  280. for (index = hash; index != -1; index = filelist[index].next)
  281. if(!strcmpi(filelist[index].fn, fname))
  282. break;
  283. return (index >= 0) ? &filelist[index] : NULL;
  284. }
  285. // returns the original file name
  286. /*
  287. char* grfio_find_file(char* fname)
  288. {
  289. FILELIST *filelist = filelist_find(fname);
  290. if (!filelist) return NULL;
  291. return (!filelist->fnd ? filelist->fn : filelist->fnd);
  292. }
  293. */
  294. // adds a FILELIST entry into the list of loaded files
  295. static FILELIST* filelist_add(FILELIST* entry)
  296. {
  297. int hash;
  298. #define FILELIST_ADDS 1024 // number increment of file lists `
  299. if (filelist_entrys >= filelist_maxentry) {
  300. filelist = (FILELIST *)realloc(filelist, (filelist_maxentry + FILELIST_ADDS) * sizeof(FILELIST));
  301. memset(filelist + filelist_maxentry, '\0', FILELIST_ADDS * sizeof(FILELIST));
  302. filelist_maxentry += FILELIST_ADDS;
  303. }
  304. memcpy (&filelist[filelist_entrys], entry, sizeof(FILELIST));
  305. hash = filehash(entry->fn);
  306. filelist[filelist_entrys].next = filelist_hash[hash];
  307. filelist_hash[hash] = filelist_entrys;
  308. filelist_entrys++;
  309. return &filelist[filelist_entrys - 1];
  310. }
  311. // adds a new FILELIST entry or overwrites an existing one
  312. static FILELIST* filelist_modify(FILELIST* entry)
  313. {
  314. FILELIST* fentry = filelist_find(entry->fn);
  315. if (fentry != NULL) {
  316. int tmp = fentry->next;
  317. memcpy(fentry, entry, sizeof(FILELIST));
  318. fentry->next = tmp;
  319. } else {
  320. fentry = filelist_add(entry);
  321. }
  322. return fentry;
  323. }
  324. // shrinks the file list array if too long
  325. static void filelist_adjust(void)
  326. {
  327. if (filelist == NULL)
  328. return;
  329. if (filelist_entrys < filelist_maxentry) {
  330. filelist = (FILELIST *)realloc(filelist, filelist_entrys * sizeof(FILELIST));
  331. filelist_maxentry = filelist_entrys;
  332. }
  333. }
  334. /***********************************************************
  335. *** Grfio Sobroutines ***
  336. ***********************************************************/
  337. // returns a file's size
  338. /*
  339. int grfio_size(char* fname)
  340. {
  341. FILELIST* entry;
  342. entry = filelist_find(fname);
  343. if (entry == NULL || entry->gentry < 0) { // LocalFileCheck
  344. char lfname[256], *p;
  345. FILELIST lentry;
  346. struct stat st;
  347. sprintf(lfname, "%s%s", data_dir, fname);
  348. for (p = &lfname[0]; *p != 0; p++)
  349. if (*p=='\\') *p = '/';
  350. if (stat(lfname, &st) == 0) {
  351. strncpy(lentry.fn, fname, sizeof(lentry.fn) - 1);
  352. lentry.fnd = NULL;
  353. lentry.declen = st.st_size;
  354. lentry.gentry = 0; // 0:LocalFile
  355. entry = filelist_modify(&lentry);
  356. } else if (entry == NULL) {
  357. printf("%s not found (grfio_size)\n", fname);
  358. return -1;
  359. }
  360. }
  361. return entry->declen;
  362. }
  363. */
  364. // reads a file into a newly allocated buffer (from grf or data directory)
  365. void* grfio_reads(char* fname, int* size)
  366. {
  367. FILE* in;
  368. FILELIST* entry;
  369. unsigned char* buf2 = NULL;
  370. entry = filelist_find(fname);
  371. if (entry == NULL || entry->gentry <= 0) { // LocalFileCheck
  372. char lfname[256], *p;
  373. FILELIST lentry;
  374. sprintf(lfname, "%s%s", data_dir, fname);
  375. for (p = &lfname[0]; *p != 0; p++)
  376. if (*p == '\\') *p = '/';
  377. in = fopen(lfname, "rb");
  378. if (in != NULL) {
  379. if (entry != NULL && entry->gentry == 0) {
  380. lentry.declen = entry->declen;
  381. } else {
  382. fseek(in,0,SEEK_END);
  383. lentry.declen = ftell(in);
  384. }
  385. fseek(in,0,SEEK_SET);
  386. buf2 = (unsigned char *)malloc(lentry.declen + 1024);
  387. fread(buf2, 1, lentry.declen, in);
  388. fclose(in);
  389. strncpy(lentry.fn, fname, sizeof(lentry.fn) - 1);
  390. lentry.fnd = NULL;
  391. lentry.gentry = 0; // 0:LocalFile
  392. entry = filelist_modify(&lentry);
  393. } else {
  394. if (entry != NULL && entry->gentry < 0) {
  395. entry->gentry = -entry->gentry; // local file checked
  396. } else {
  397. printf("%s not found (grfio_reads - local file %s)\n", fname, lfname);
  398. return NULL;
  399. }
  400. }
  401. }
  402. if (entry != NULL && entry->gentry > 0) { // Archive[GRF] File Read
  403. char* grfname = gentry_table[entry->gentry - 1];
  404. in = fopen(grfname, "rb");
  405. if(in != NULL) {
  406. unsigned char *buf = (unsigned char *)malloc(entry->srclen_aligned + 1024);
  407. fseek(in, entry->srcpos, 0);
  408. fread(buf, 1, entry->srclen_aligned, in);
  409. fclose(in);
  410. buf2 = (unsigned char *)malloc(entry->declen + 1024);
  411. if (entry->type == 1 || entry->type == 3 || entry->type == 5) {
  412. uLongf len;
  413. if (entry->cycle >= 0)
  414. decode_des_etc(buf, entry->srclen_aligned, entry->cycle == 0, entry->cycle);
  415. len = entry->declen;
  416. decode_zip(buf2, &len, buf, entry->srclen);
  417. if (len != (uLong)entry->declen) {
  418. printf("decode_zip size mismatch err: %d != %d\n", (int)len, entry->declen);
  419. free(buf);
  420. free(buf2);
  421. return NULL;
  422. }
  423. } else {
  424. memcpy(buf2, buf, entry->declen);
  425. }
  426. free(buf);
  427. } else {
  428. printf("%s not found (grfio_reads - GRF file %s)\n", fname, grfname);
  429. return NULL;
  430. }
  431. }
  432. if (size != NULL && entry != NULL)
  433. *size = entry->declen;
  434. return buf2;
  435. }
  436. /*==========================================
  437. * Resource filename decode
  438. *------------------------------------------*/
  439. static char* decode_filename(unsigned char* buf, int len)
  440. {
  441. int lop;
  442. for(lop=0;lop<len;lop+=8) {
  443. NibbleSwap(&buf[lop],8);
  444. BitConvert(&buf[lop],BitSwapTable1);
  445. BitConvert4(&buf[lop]);
  446. BitConvert(&buf[lop],BitSwapTable2);
  447. }
  448. return (char*)buf;
  449. }
  450. // loads all entries in the specified grf file into the filelist
  451. // gentry - index of the grf file name in the gentry_table
  452. static int grfio_entryread(char* grfname, int gentry)
  453. {
  454. FILE* fp;
  455. long grf_size,list_size;
  456. unsigned char grf_header[0x2e];
  457. int lop,entry,entrys,ofs,grf_version;
  458. char *fname;
  459. unsigned char *grf_filelist;
  460. fp = fopen(grfname, "rb");
  461. if (fp == NULL) {
  462. printf("GRF data file not found: '%s'\n",grfname);
  463. return 1; // 1:not found error
  464. } else
  465. printf("GRF data file found: '%s'\n",grfname);
  466. fseek(fp,0,SEEK_END);
  467. grf_size = ftell(fp);
  468. fseek(fp,0,SEEK_SET);
  469. fread(grf_header,1,0x2e,fp);
  470. if (strcmp((const char *) grf_header,"Master of Magic") ||
  471. fseek(fp,getlong(grf_header+0x1e),SEEK_CUR))
  472. {
  473. fclose(fp);
  474. printf("GRF %s read error\n", grfname);
  475. return 2; // 2:file format error
  476. }
  477. grf_version = getlong(grf_header+0x2a) >> 8;
  478. if (grf_version == 0x01) { //****** Grf version 01xx ******
  479. list_size = grf_size - ftell(fp);
  480. grf_filelist = (unsigned char *) malloc(list_size);
  481. fread(grf_filelist,1,list_size,fp);
  482. fclose(fp);
  483. entrys = getlong(grf_header+0x26) - getlong(grf_header+0x22) - 7;
  484. // Get an entry
  485. for (entry = 0,ofs = 0; entry < entrys; entry++) {
  486. int ofs2, srclen, srccount;
  487. unsigned char type;
  488. char* period_ptr;
  489. FILELIST aentry;
  490. ofs2 = ofs+getlong(grf_filelist+ofs)+4;
  491. type = grf_filelist[ofs2+12];
  492. if (type != 0) { // Directory Index ... skip
  493. fname = decode_filename(grf_filelist+ofs+6, grf_filelist[ofs]-6);
  494. if (strlen(fname) > sizeof(aentry.fn) - 1) {
  495. printf("GRF file name %s is too long\n", fname);
  496. free(grf_filelist);
  497. exit(EXIT_FAILURE);
  498. }
  499. srclen = 0;
  500. if ((period_ptr = strrchr(fname, '.')) != NULL) {
  501. for(lop = 0; lop < 4; lop++) {
  502. if (strcmpi(period_ptr, ".gnd\0.gat\0.act\0.str"+lop*5) == 0)
  503. break;
  504. }
  505. srclen = getlong(grf_filelist+ofs2) - getlong(grf_filelist+ofs2+8) - 715;
  506. if(lop == 4) {
  507. for(lop = 10, srccount = 1; srclen >= lop; lop = lop * 10, srccount++);
  508. } else {
  509. srccount = 0;
  510. }
  511. } else {
  512. srccount = 0;
  513. }
  514. aentry.srclen = srclen;
  515. aentry.srclen_aligned = getlong(grf_filelist+ofs2+4)-37579;
  516. aentry.declen = getlong(grf_filelist+ofs2+8);
  517. aentry.srcpos = getlong(grf_filelist+ofs2+13)+0x2e;
  518. aentry.cycle = srccount;
  519. aentry.type = type;
  520. strncpy(aentry.fn, fname,sizeof(aentry.fn)-1);
  521. aentry.fnd = NULL;
  522. #ifdef GRFIO_LOCAL
  523. aentry.gentry = -(gentry+1); // As Flag for making it a negative number carrying out the first time LocalFileCheck
  524. #else
  525. aentry.gentry = gentry+1; // With no first time LocalFileCheck
  526. #endif
  527. filelist_modify(&aentry);
  528. }
  529. ofs = ofs2 + 17;
  530. }
  531. free(grf_filelist);
  532. } else if (grf_version == 0x02) { //****** Grf version 02xx ******
  533. unsigned char eheader[8];
  534. unsigned char *rBuf;
  535. uLongf rSize, eSize;
  536. fread(eheader,1,8,fp);
  537. rSize = getlong(eheader); // Read Size
  538. eSize = getlong(eheader+4); // Extend Size
  539. if ((long)rSize > grf_size-ftell(fp)) {
  540. fclose(fp);
  541. printf("Illegal data format: GRF compress entry size\n");
  542. return 4;
  543. }
  544. rBuf = (unsigned char *)malloc(rSize); // Get a Read Size
  545. grf_filelist = (unsigned char *)malloc(eSize); // Get a Extend Size
  546. fread(rBuf,1,rSize,fp);
  547. fclose(fp);
  548. decode_zip(grf_filelist, &eSize, rBuf, rSize); // Decode function
  549. list_size = eSize;
  550. free(rBuf);
  551. entrys = getlong(grf_header+0x26) - 7;
  552. // Get an entry
  553. for(entry = 0, ofs = 0; entry < entrys; entry++) {
  554. int ofs2, srclen, srccount, type;
  555. FILELIST aentry;
  556. fname = (char*)(grf_filelist+ofs);
  557. if (strlen(fname) > sizeof(aentry.fn)-1) {
  558. printf("GRF file name %s is too long\n", fname);
  559. free(grf_filelist);
  560. exit(EXIT_FAILURE);
  561. }
  562. ofs2 = ofs + (int)strlen(fname)+1;
  563. type = grf_filelist[ofs2+12];
  564. if (type == 1 || type == 3 || type == 5) {
  565. srclen = getlong(grf_filelist+ofs2);
  566. if (grf_filelist[ofs2+12] == 3) {
  567. for (lop = 10, srccount = 1; srclen >= lop; lop = lop * 10, srccount++);
  568. } else if (grf_filelist[ofs2+12] == 5) {
  569. srccount = 0;
  570. } else { // if (grf_filelist[ofs2+12]==1) {
  571. srccount = -1;
  572. }
  573. aentry.srclen = srclen;
  574. aentry.srclen_aligned = getlong(grf_filelist+ofs2+4);
  575. aentry.declen = getlong(grf_filelist+ofs2+8);
  576. aentry.srcpos = getlong(grf_filelist+ofs2+13)+0x2e;
  577. aentry.cycle = srccount;
  578. aentry.type = type;
  579. strncpy(aentry.fn,fname,sizeof(aentry.fn)-1);
  580. aentry.fnd = NULL;
  581. #ifdef GRFIO_LOCAL
  582. aentry.gentry = -(gentry+1); // As Flag for making it a negative number carrying out the first time LocalFileCheck
  583. #else
  584. aentry.gentry = gentry+1; // With no first time LocalFileCheck
  585. #endif
  586. filelist_modify(&aentry);
  587. }
  588. ofs = ofs2 + 17;
  589. }
  590. free(grf_filelist);
  591. } else { //****** Grf Other version ******
  592. fclose(fp);
  593. printf("GRF version %04x not supported\n",getlong(grf_header+0x2a));
  594. return 4;
  595. }
  596. filelist_adjust(); // Unnecessary area release of filelist
  597. return 0; // 0:no error
  598. }
  599. /*==========================================
  600. * Grfio : Resource file check
  601. *------------------------------------------*/
  602. static void grfio_resourcecheck(void)
  603. {
  604. char w1[256], w2[256], src[256], dst[256], restable[256], line[256];
  605. char *ptr, *buf;
  606. FILELIST* entry;
  607. int size;
  608. FILE* fp;
  609. // read resnametable from data directory and return if successful
  610. sprintf(restable, "%sdata\\resnametable.txt", data_dir);
  611. for (ptr = &restable[0]; *ptr != 0; ptr++)
  612. if (*ptr == '\\') *ptr = '/';
  613. fp = fopen(restable, "rb");
  614. if (fp) {
  615. while(fgets(line, sizeof(line), fp))
  616. {
  617. if (sscanf(line, "%[^#]#%[^#]#", w1, w2) == 2 &&
  618. // we only need the maps' GAT and RSW files
  619. (strstr(w2, ".gat") || strstr(w2, ".rsw")))
  620. {
  621. sprintf(src, "data\\%s", w1);
  622. sprintf(dst, "data\\%s", w2);
  623. entry = filelist_find(dst);
  624. // create new entries reusing the original's info
  625. if (entry != NULL) {
  626. FILELIST fentry;
  627. memcpy(&fentry, entry, sizeof(FILELIST));
  628. strncpy(fentry.fn, src, sizeof(fentry.fn) - 1);
  629. fentry.fnd = strdup(dst);
  630. filelist_modify(&fentry);
  631. }
  632. }
  633. }
  634. fclose(fp);
  635. return; // we're done here!
  636. }
  637. // read resnametable from loaded GRF's, only if it cannot be loaded from the data directory
  638. buf = (char *)grfio_reads("data\\resnametable.txt", &size);
  639. if (buf) {
  640. buf[size] = 0;
  641. ptr = buf;
  642. while (ptr - buf < size) {
  643. if (sscanf(ptr, "%[^#]#%[^#]#", w1, w2) == 2 &&
  644. (strstr(w2, ".gat") || strstr(w2, ".rsw")))
  645. {
  646. sprintf(src, "data\\%s", w1);
  647. sprintf(dst, "data\\%s", w2);
  648. entry = filelist_find(dst);
  649. if (entry != NULL) {
  650. FILELIST fentry;
  651. memcpy(&fentry, entry, sizeof(FILELIST));
  652. strncpy(fentry.fn, src, sizeof(fentry.fn) - 1);
  653. fentry.fnd = strdup(dst);
  654. filelist_modify(&fentry);
  655. }
  656. }
  657. ptr = strchr(ptr, '\n'); // Next line
  658. if (!ptr) break;
  659. ptr++;
  660. }
  661. free(buf);
  662. return;
  663. }
  664. }
  665. // reads a grf file and adds it to the list
  666. static int grfio_add(char* fname)
  667. {
  668. #define GENTRY_ADDS 4 // The number increment of gentry_table entries
  669. if (gentry_entrys >= gentry_maxentry) {
  670. gentry_maxentry += GENTRY_ADDS;
  671. gentry_table = (char**)realloc(gentry_table, gentry_maxentry * sizeof(char*));
  672. memset(gentry_table + (gentry_maxentry - GENTRY_ADDS), 0, sizeof(char*) * GENTRY_ADDS);
  673. }
  674. gentry_table[gentry_entrys++] = strdup(fname);
  675. return grfio_entryread(fname, gentry_entrys - 1);
  676. }
  677. // removes all entries
  678. void grfio_final(void)
  679. {
  680. if (filelist != NULL) {
  681. int i;
  682. for (i = 0; i < filelist_entrys; i++)
  683. if (filelist[i].fnd != NULL)
  684. free(filelist[i].fnd);
  685. free(filelist);
  686. filelist = NULL;
  687. }
  688. filelist_entrys = filelist_maxentry = 0;
  689. if (gentry_table != NULL) {
  690. int i;
  691. for (i = 0; i < gentry_entrys; i++)
  692. if (gentry_table[i] != NULL)
  693. free(gentry_table[i]);
  694. free(gentry_table);
  695. gentry_table = NULL;
  696. }
  697. gentry_entrys = gentry_maxentry = 0;
  698. }
  699. /*==========================================
  700. * Grfio : Initialize
  701. *------------------------------------------*/
  702. void grfio_init(char* fname)
  703. {
  704. FILE* data_conf;
  705. char line[1024], w1[1024], w2[1024];
  706. int grf_num = 0;
  707. hashinit(); // hash table initialization
  708. data_conf = fopen(fname, "r");
  709. // It will read, if there is grf-files.txt.
  710. if (data_conf) {
  711. while(fgets(line, sizeof(line), data_conf))
  712. {
  713. if (line[0] == '/' && line[1] == '/')
  714. continue;
  715. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  716. continue;
  717. // Entry table reading
  718. if(strcmp(w1, "grf") == 0) // GRF file
  719. grf_num += (grfio_add(w2) == 0);
  720. else if(strcmp(w1,"data_dir") == 0) { // Data directory
  721. strcpy(data_dir, w2);
  722. printf("Use data directory %s\n", w2);
  723. }
  724. }
  725. fclose(data_conf);
  726. } // end of reading grf-files.txt
  727. if (grf_num == 0) {
  728. printf("No GRF loaded, using default data directory\n");
  729. }
  730. // Unneccessary area release of filelist
  731. filelist_adjust();
  732. // Resource check
  733. grfio_resourcecheck();
  734. return;
  735. }