grfio.c 27 KB

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