extract.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. //#include <direct.h>
  6. FILE *infile;
  7. FILE *index_file;
  8. int cd;
  9. int file_position, i;
  10. //unsigned long cd1_size = 718738272; // english version
  11. unsigned long cd1_size = 718199664; // japanese version
  12. unsigned long cd2_size = 688700880; // english version
  13. //unsigned long cd2_size = 729929088; // japanese version
  14. char strindex[44] = {82,73,70,70,0, 0, 0, 0, 67,68,88,65,0, 0, 0, 0,
  15. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  17. void parse_file(unsigned long start_sector, unsigned long file_size, int id, char *file_name)
  18. {
  19. unsigned char arfile[2352];
  20. unsigned char type[4];
  21. const char* extention;
  22. unsigned int file_type;
  23. char number[10];
  24. int size_to_skip, size_to_read;
  25. int i;
  26. int ultimo;
  27. FILE *file;
  28. // find file start sector
  29. file_position = fseek(infile, 2352L * start_sector, 0);
  30. // skip sector header
  31. file_position = fseek(infile, 24L, 1);
  32. // read type
  33. file_position = fread(type, 1, 4, infile);
  34. // move back to start of sector
  35. file_position = fseek(infile, -28L, 1);
  36. // reverse byte order
  37. file_type = type[3] | (type[2] << 8) | (type[1] << 16) | (type[0] << 24);
  38. printf("file_type = 0x%x\n", file_type);
  39. // by default we skip sector header
  40. size_to_skip = 24;
  41. size_to_read = 2048;
  42. extention = "";
  43. // TIM
  44. if (file_type == 0x10000000)
  45. {
  46. extention = ".tim";
  47. }
  48. // SND
  49. if (file_type == 0x77647320)
  50. {
  51. extention = ".snd";
  52. }
  53. // SMD
  54. if (file_type == 0x736d6473)
  55. {
  56. extention = ".smd";
  57. }
  58. // SED
  59. if (file_type == 0x73656473)
  60. {
  61. extention = ".sed";
  62. }
  63. // DUMMY (Stringhe generate dal programma che ha creato il CD originale.)
  64. if (file_type == 0x49742773)
  65. {
  66. extention = ".dummy";
  67. }
  68. // UNK1
  69. if (file_type == 0x01000000)
  70. {
  71. extention = ".unk1";
  72. }
  73. // UNK2
  74. if (file_type == 0x02000000)
  75. {
  76. extention = ".unk2";
  77. }
  78. // UNK3
  79. if (file_type == 0x03000000)
  80. {
  81. extention = ".unk3";
  82. }
  83. // UNK4
  84. if (file_type == 0x04000000)
  85. {
  86. extention = ".unk4";
  87. }
  88. // UNK5
  89. if (file_type == 0x05000000)
  90. {
  91. extention = ".unk5";
  92. }
  93. // UNK6
  94. if (file_type == 0x06000000)
  95. {
  96. extention = ".unk6";
  97. }
  98. // UNK7
  99. if (file_type == 0x07000000)
  100. {
  101. extention = ".unk7";
  102. }
  103. // UNK8
  104. if (file_type == 0x08000000)
  105. {
  106. extention = ".unk8";
  107. }
  108. // STR
  109. if (file_type == 0x60010180)
  110. {
  111. // if we have str format we read with file sector
  112. size_to_skip = 0;
  113. size_to_read = 2352;
  114. extention = ".str";
  115. }
  116. // RAW
  117. if (file_type == 0x00120000)
  118. {
  119. extention = ".raw1";
  120. }
  121. // RAW
  122. if (file_type == 0x01120000)
  123. {
  124. extention = ".raw2";
  125. }
  126. // add zeros to file name 0000.xxx
  127. if (id < 1000)
  128. {
  129. strcat(file_name, "0");
  130. }
  131. if (id < 100)
  132. {
  133. strcat(file_name, "0");
  134. }
  135. if (id < 10)
  136. {
  137. strcat(file_name, "0");
  138. }
  139. // add number to filepath/filename
  140. sprintf(number, "%d", id);
  141. strcat(file_name, number);
  142. strcat(file_name, extention);
  143. FILE* desc_file = fopen("description.txt", "a+");
  144. char desc[ 255 ];
  145. sprintf(desc, "%s - 0x%04x, 0x%04x\n", file_name, static_cast<unsigned int>(start_sector), static_cast<unsigned int>(file_size));
  146. fputs (desc, desc_file);
  147. fclose(desc_file);
  148. printf("File name: \\%s\n\n", file_name);
  149. file = fopen(file_name, "wb");
  150. // if this is str then write str index
  151. if (size_to_read == 2352)
  152. {
  153. file_position = fwrite(strindex, 1, 44, file);
  154. }
  155. while ((file_size / (long)size_to_read) >= 1)
  156. {
  157. file_position = fseek(infile, (long)size_to_skip, 1);
  158. file_position = fread(arfile, 1, size_to_read, infile);
  159. file_position = fwrite(arfile, 1, size_to_read, file);
  160. file_position = fseek(infile, 2352L - (((long)size_to_read) + ((long)size_to_skip)), 1);
  161. file_size = file_size - (long)size_to_read;
  162. }
  163. if (size_to_read == 2048)
  164. {
  165. file_position = fseek(infile, (long)size_to_skip, 1);
  166. file_position = fread(arfile, 1, file_size, infile);
  167. file_position = fwrite(arfile, 1, file_size, file);
  168. }
  169. for(i = 0; ; i++)
  170. {
  171. if (file_name[i] == 0)
  172. {
  173. break;
  174. }
  175. if (file_name[i] == '\\')
  176. {
  177. ultimo = i;
  178. }
  179. }
  180. file_name[ultimo + 1] = 0;
  181. fclose(file);
  182. }
  183. // create file with cd file index
  184. void create_index_file()
  185. {
  186. // array for one sector
  187. unsigned char arfile[2048];
  188. printf("Creating index_file file list\n");
  189. if (cd == 1)
  190. {
  191. index_file = fopen("index_cd1.bin", "wb");
  192. }
  193. else if (cd == 2)
  194. {
  195. index_file = fopen("index_cd2.bin", "wb");
  196. }
  197. // find start of game files (the first file in the 24 sector)
  198. file_position = fseek(infile, 24L * 2352L, 0);
  199. // the size of index - 16 sectors
  200. for (i = 0; i < 16; i++)
  201. {
  202. printf("Sector %d\n", 24 + i);
  203. // pass sector header
  204. file_position = fseek(infile, 24L, 1);
  205. // read and write sector data
  206. file_position = fread(arfile, 1, 2048, infile);
  207. file_position = fwrite(arfile, 1, 2048, index_file);
  208. // go to next sector
  209. file_position = fseek(infile, 280L, 1);
  210. }
  211. fclose(index_file);
  212. }
  213. void extract(char *filename)
  214. {
  215. signed long start_sector, file_size;
  216. unsigned char start[3], size[4];
  217. char numero[10];
  218. // first directory number 0
  219. int directory_number = 0;
  220. // create file with cd file index
  221. create_index_file();
  222. if (cd == 1)
  223. {
  224. index_file = fopen("index_cd1.bin", "rb");
  225. }
  226. else if (cd == 2)
  227. {
  228. index_file = fopen("index_cd2.bin", "rb");
  229. }
  230. for (i = 0; ; i++)
  231. {
  232. file_position = fread(start, 1, 3, index_file);
  233. file_position = fread(size, 1, 4, index_file);
  234. // reverse bytes order (low endian)
  235. start_sector = start[0] | (start[1] << 8) | (start[2] << 16);
  236. file_size = size[0] | (size[1] << 8) | (size[2] << 16) | (size[3] << 24);
  237. // end of file
  238. if (file_size == 0x00ffffff)
  239. {
  240. break;
  241. }
  242. if (start_sector == 0 && file_size > 0)
  243. {
  244. filename[9] = 0;
  245. }
  246. // directory
  247. if (file_size > 0xff000000)
  248. {
  249. // create directory name (name - number of directory)
  250. filename[9] = 0;
  251. sprintf(numero, "%d", directory_number++);
  252. strcat(filename, numero);
  253. strcat(filename, "\\");
  254. // TODO FIX ME
  255. //mkdir(filename);
  256. abort();
  257. printf("number of file %d, in dir %d", static_cast<int>(-file_size), directory_number);
  258. }
  259. // file
  260. if (start_sector != 0 && file_size < 0xff000000 && file_size > 0)
  261. {
  262. printf("file number %d, size: 0x%x byte - ", i, static_cast<unsigned int>(file_size));
  263. parse_file(start_sector, file_size, i, filename);
  264. }
  265. }
  266. }
  267. int verify_iso()
  268. {
  269. unsigned long position, size;
  270. position = ftell(infile);
  271. // set cursor to end of file
  272. fseek(infile, 0L, SEEK_END);
  273. size = ftell(infile);
  274. // set cursor to beginning of file
  275. fseek(infile, position, SEEK_SET);
  276. if (size == cd1_size)
  277. {
  278. printf("Found a valid Xenogears CD1 image (size: %ld bytes)\n", size);
  279. return 1;
  280. }
  281. if (size == cd2_size)
  282. {
  283. printf("Found a valid Xenogears CD2 image (size: %ld bytes)\n", size);
  284. return 2;
  285. }
  286. else
  287. {
  288. printf("Found image (size: %ld bytes)\n", size);
  289. printf("ERROR: Selected file is not a valid Xenogears image.\n Check if you are using the correct file or if the CD/ISO is damaged.\n");
  290. return 0;
  291. }
  292. }
  293. int
  294. main(int argc, char *argv[])
  295. {
  296. char filename[20] = "STRIPCD";
  297. printf("Xenogears Main Disk Dumper v0.9.67\n Created by Graph and _Ombra_\n\n");
  298. if (argc != 2)
  299. {
  300. printf("USAGE:'extract.exe isofile' extract files from xeno cd image.\n");
  301. return EXIT_FAILURE;
  302. }
  303. if ((infile = fopen(argv[1], "rb")) == NULL)
  304. {
  305. printf("The file %s does not exist, Please check the file name.\n", argv[1]);
  306. return EXIT_FAILURE;
  307. }
  308. cd = verify_iso();
  309. if (cd == 0)
  310. {
  311. return EXIT_FAILURE;
  312. }
  313. else if (cd == 1)
  314. {
  315. strcat(filename, "1\\");
  316. }
  317. else if (cd == 2)
  318. {
  319. strcat(filename, "2\\");
  320. }
  321. // TODO FIX ME
  322. //mkdir(filename);
  323. abort();
  324. extract(filename);
  325. fclose(infile);
  326. return EXIT_SUCCESS;
  327. }