MIXFILE.CPP 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. ** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Data file creator. *
  23. * *
  24. * File Name : MIXFILE.CPP *
  25. * *
  26. * Programmer : Joe L. Bostic *
  27. * *
  28. * Start Date : August 6, 1994 *
  29. * *
  30. * Last Update : 10/27/94 [JLB] *
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Calc_CRC -- Calculate the CRC value of a block of data. *
  35. * compfunc -- Comparison function used by qsort(). *
  36. * DataClass::DataClass -- Constructor for a data file object node. *
  37. * DataClass::Process_Input -- Process the input file list and builds linked records. *
  38. * DataClass::Process_Output -- Creates the output data file from the component source files.*
  39. * DataClass::~DataClass -- Destructor for the data file object. *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. #pragma inline
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <ctype.h>
  45. #include <dir.h>
  46. #include <string.h>
  47. #define FALSE 0
  48. #define TRUE 1
  49. long Calc_CRC(void const *data, long size);
  50. /********************************************************************
  51. ** This is the data block controlling class. It is used for every data
  52. ** file as well as processing the entire data file list.
  53. */
  54. class DataClass {
  55. public:
  56. long CRC;
  57. static short Count;
  58. static long TotalSize;
  59. static char *ExtFrom[10];
  60. static char *ExtTo[10];
  61. static int ExtCount;
  62. static char *AltPath[10];
  63. static int AltPathCount;
  64. DataClass(char const *filename);
  65. ~DataClass(void);
  66. static void Process_Input(char const *infile, int quiet, int paths);
  67. static void Process_Output(char const *outfile);
  68. char const * Output_Filename(void);
  69. char const * Input_Filename(void);
  70. private:
  71. DataClass *Next; // Pointer to next file in chain.
  72. char *Filename; // Raw original filename.
  73. long Size; // Size of data element.
  74. long Offset; // Offset within mixfile for data start.
  75. int Index; // Write order number.
  76. static DataClass *First;
  77. static int Quiet;
  78. static int Paths;
  79. };
  80. char *DataClass::AltPath[10];
  81. char *DataClass::ExtFrom[10];
  82. char *DataClass::ExtTo[10];
  83. int DataClass::ExtCount = 0;
  84. int DataClass::AltPathCount = 0;
  85. short DataClass::Count = 0;
  86. DataClass * DataClass::First = 0;
  87. int DataClass::Quiet = FALSE;
  88. int DataClass::Paths = FALSE;
  89. long DataClass::TotalSize = 0;
  90. int main(int argc, char ** argv)
  91. {
  92. class UsageError{}; // Parameter error or usage display desired.
  93. char *infile = 0;
  94. char *outfile = 0;
  95. int quiet = FALSE;
  96. int paths = FALSE;
  97. /*
  98. ** Banner message.
  99. */
  100. printf("MIXFILE V1.4 (c)\n");
  101. /*
  102. ** Process the parameter list and dispatch the packing function.
  103. */
  104. try {
  105. /*
  106. ** If not enough parameters were specified, then immediately
  107. ** display the usage instructions.
  108. */
  109. if (argc < 2) throw UsageError();
  110. try {
  111. for (int index = 1; index < argc; index++) {
  112. char *arg = argv[index];
  113. switch (*arg) {
  114. /*
  115. ** Process any command line switches.
  116. */
  117. case '/':
  118. case '-':
  119. switch (toupper(arg[1])) {
  120. case 'Q':
  121. quiet = TRUE;
  122. break;
  123. case 'S':
  124. paths = TRUE;
  125. break;
  126. case 'E':
  127. if (DataClass::ExtCount >= sizeof(DataClass::ExtFrom)/sizeof(DataClass::ExtFrom[0])) {
  128. throw "Too many extensions specified";
  129. } else {
  130. char * ptr = strupr(strtok(&arg[2], "="));
  131. if (*ptr == '.') ptr++;
  132. DataClass::ExtFrom[DataClass::ExtCount] = ptr;
  133. ptr = strupr(strtok(NULL, "=\r\n"));
  134. if (*ptr == '.') ptr++;
  135. DataClass::ExtTo[DataClass::ExtCount] = ptr;
  136. DataClass::ExtCount++;
  137. }
  138. break;
  139. case 'I':
  140. if (DataClass::AltPathCount >= sizeof(DataClass::AltPath)/sizeof(DataClass::AltPath[0])) {
  141. throw "Too many paths specified";
  142. } else {
  143. char dir[MAXDIR];
  144. strcpy(dir, &arg[2]);
  145. if (dir[strlen(dir)-1] != '\\') {
  146. strcat(dir, "\\");
  147. }
  148. DataClass::AltPath[DataClass::AltPathCount++] = strupr(strdup(dir));
  149. }
  150. break;
  151. default:
  152. throw "Unrecognized option flag";
  153. }
  154. break;
  155. /*
  156. ** Process command line filenames for either input or output.
  157. */
  158. default:
  159. if (outfile) throw "Unrecognized parameter";
  160. if (!infile) {
  161. FILE *file = fopen(arg, "r");
  162. if (!file) throw "Unable to open input file";
  163. fclose(file);
  164. infile = arg;
  165. continue;
  166. }
  167. if (!outfile) {
  168. outfile = arg;
  169. }
  170. break;
  171. }
  172. }
  173. /*
  174. ** Perform a last minute check to make sure both an input and
  175. ** output filename was specified. If not, then throw an
  176. ** error.
  177. */
  178. if (!outfile) throw "No output file specified";
  179. if (!infile) throw "No input file specified";
  180. /*
  181. ** Process the data.
  182. */
  183. try {
  184. DataClass::Process_Input(infile, quiet, paths);
  185. DataClass::Process_Output(outfile);
  186. printf("Created mix file '%s'\nEmbedded objects = %d\nTotal file size = %ld\n", outfile, DataClass::Count, DataClass::TotalSize);
  187. }
  188. catch (char *message) {
  189. printf("\nERROR: %s.\n", message);
  190. exit(EXIT_FAILURE);
  191. }
  192. }
  193. /*
  194. ** This exception is called for any of the various fatal errors that
  195. ** can occur while parsing the parameters.
  196. */
  197. catch(char *message) {
  198. printf("\nERROR: %s.\n", message);
  199. throw UsageError();
  200. }
  201. }
  202. /*
  203. ** This exception is thrown when the utility can't proceed and the
  204. ** utility usage instructions are desired to be displayed.
  205. */
  206. catch(UsageError) {
  207. printf("\nUSAGE: DATFILE <controlfile> <datafile>\n"
  208. "\n"
  209. " <controlfile> : list of filenames to pack\n"
  210. " <datafile> : output data filename\n"
  211. " Options\n"
  212. " -q : quiet operation\n"
  213. " -i<path> : alternate to find files\n"
  214. " -e<ext1>=<ext2> : changes file extensions from <ext1> to <ext2>\n"
  215. " -s : record paths in ID\n");
  216. exit(EXIT_FAILURE);
  217. }
  218. return EXIT_SUCCESS;
  219. }
  220. /***********************************************************************************************
  221. * DataClass::Process_Input -- Process the input file list and builds linked records. *
  222. * *
  223. * This routine will process the list of files in the input file. It builds a linked *
  224. * list of DataClass objects. This routine is the initial process of creating a packed *
  225. * data file. * *
  226. * *
  227. * INPUT: infile -- Pointer to input filename. *
  228. * *
  229. * quiet -- Process quietly? *
  230. * *
  231. * paths -- Should the path of the filename be considered as part of its signature?*
  232. * *
  233. * OUTPUT: none *
  234. * *
  235. * WARNINGS: none *
  236. * *
  237. * HISTORY: *
  238. * 08/06/1994 JLB : Created. *
  239. *=============================================================================================*/
  240. void DataClass::Process_Input(char const *infile, int quiet, int paths)
  241. {
  242. FILE *file; // Input file.
  243. static char buffer[MAXFILE];
  244. Quiet = quiet;
  245. Paths = paths;
  246. if (!infile) throw "No input file specified";
  247. file = fopen(infile, "ra");
  248. if (!file) throw "Could not open input file";
  249. if (!Quiet) {
  250. printf("Processing '%s'.\n", infile);
  251. }
  252. /*
  253. ** Process the source file list.
  254. */
  255. try {
  256. for (;;) {
  257. int result = fscanf(file, "%s", buffer);
  258. if (result == EOF) break;
  259. new DataClass(buffer);
  260. }
  261. }
  262. /*
  263. ** This error handler is used if there are any errors that occur while
  264. ** parsing and verifying the data block filenames. Errors can include
  265. ** source filename errors as well as missing source files themselves.
  266. */
  267. catch (char *message) {
  268. fclose(file);
  269. throw message;
  270. }
  271. fclose(file);
  272. }
  273. /***********************************************************************************************
  274. * DataClass::DataClass -- Constructor for a data file object node. *
  275. * *
  276. * This constructs a data file object node and links it into the list of object nodes. *
  277. * It performs a preliminary check on the existance of the the source file. *
  278. * *
  279. * INPUT: filename -- The filename of the object to add. *
  280. * *
  281. * OUTPUT: none *
  282. * *
  283. * WARNINGS: none *
  284. * *
  285. * HISTORY: *
  286. * 08/06/1994 JLB : Created. *
  287. * 10/27/94 JLB : Handles multiple data paths. *
  288. *=============================================================================================*/
  289. DataClass::DataClass(char const *filename)
  290. {
  291. static char buffer[100];
  292. if (!filename) throw "NULL filename";
  293. Filename = strdup(filename);
  294. strupr(Filename);
  295. /*
  296. ** Try to find the data file.
  297. */
  298. FILE *datafile = fopen(Filename, "rb");
  299. if (!datafile) {
  300. /*
  301. ** If the file couldn't be found in the current directory, check
  302. ** the alternate paths. If the alternate path is successful, then
  303. ** alter the pathname to match and continue.
  304. */
  305. for (int index = 0; index < AltPathCount; index++) {
  306. strcpy(buffer, AltPath[index]);
  307. strcat(buffer, Filename);
  308. datafile = fopen(buffer, "rb");
  309. if (datafile) {
  310. free(Filename);
  311. Filename = strdup(buffer);
  312. break;
  313. }
  314. }
  315. if (!datafile) {
  316. sprintf(buffer, "Could not find source file '%s'", Filename);
  317. throw buffer;
  318. }
  319. }
  320. /*
  321. ** Determine the CRC identifier for the filename, This can be either
  322. ** the base filename or the complete path (as indicated by the command
  323. ** line parameter).
  324. */
  325. char const * name = Output_Filename();
  326. CRC = Calc_CRC(name, strlen(name));
  327. /*
  328. ** Find out the size of the source data file.
  329. */
  330. fseek(datafile, 0, SEEK_END);
  331. long size = ftell(datafile);
  332. fclose(datafile);
  333. if (size == -1) throw "Seek failure";
  334. Size = size;
  335. Index = Count;
  336. Count++;
  337. Next = First;
  338. First = this;
  339. }
  340. /***********************************************************************************************
  341. * DataClass::~DataClass -- Destructor for the data file object. *
  342. * *
  343. * This is the destructor for the data file object. It deallocates any memory allocated *
  344. * and de-links it from the list of file objects. *
  345. * *
  346. * INPUT: none *
  347. * *
  348. * OUTPUT: none *
  349. * *
  350. * WARNINGS: none *
  351. * *
  352. * HISTORY: *
  353. * 08/06/1994 JLB : Created. *
  354. *=============================================================================================*/
  355. DataClass::~DataClass(void)
  356. {
  357. if (Filename) {
  358. free(Filename);
  359. Filename = 0;
  360. }
  361. Count--;
  362. DataClass *ptr = First;
  363. DataClass *prev = 0;
  364. while (ptr) {
  365. if (ptr == this) {
  366. if (prev) {
  367. prev->Next = Next;
  368. }
  369. return;
  370. }
  371. prev = ptr;
  372. ptr = ptr->Next;
  373. }
  374. }
  375. /***********************************************************************************************
  376. * compfunc -- Comparison function used by qsort(). *
  377. * *
  378. * This is a support function that compares two file data objects against their CRC *
  379. * values. *
  380. * *
  381. * INPUT: ptr1 -- Pointer to object number 1. *
  382. * *
  383. * ptr2 -- Pointer to object number 2. *
  384. * *
  385. * OUTPUT: Returns with a logical comparison of object 1 to object 2. *
  386. * *
  387. * WARNINGS: none *
  388. * *
  389. * HISTORY: *
  390. * 08/06/1994 JLB : Created. *
  391. *=============================================================================================*/
  392. int compfunc(const void *ptr1, const void *ptr2)
  393. {
  394. DataClass const *obj1 = *(DataClass const **)ptr1;
  395. DataClass const *obj2 = *(DataClass const **)ptr2;
  396. if (obj1->CRC < obj2->CRC) {
  397. return (-1);
  398. }
  399. if (obj1->CRC > obj2->CRC) {
  400. return (1);
  401. }
  402. return(0);
  403. }
  404. /***********************************************************************************************
  405. * DataClass::Process_Output -- Creates the output data file from the component source files. *
  406. * *
  407. * This is the final step in creation of the data output file. It writes the header *
  408. * block and then appends the appropriate files to the output file. *
  409. * *
  410. * INPUT: outname -- Pointer to the filename to use for output. *
  411. * *
  412. * OUTPUT: none *
  413. * *
  414. * WARNINGS: none *
  415. * *
  416. * HISTORY: *
  417. * 08/06/1994 JLB : Created. *
  418. *=============================================================================================*/
  419. void DataClass::Process_Output(char const *outname)
  420. {
  421. FILE *outfile;
  422. if (Count) {
  423. DataClass **array = new DataClass *[Count];
  424. /*
  425. ** Open the output file for creation.
  426. */
  427. if (!outname) throw "Missing output filename";
  428. outfile = fopen(outname, "wb");
  429. if (!outfile) {
  430. throw "Unable to open output file";
  431. }
  432. /*
  433. ** Build a working array to the file objects.
  434. */
  435. DataClass * ptr = First;
  436. for (int index = 0; index < Count; index++) {
  437. array[index] = ptr;
  438. ptr->Index = index;
  439. ptr = ptr->Next;
  440. }
  441. /*
  442. ** Precalculate the offset value for each data file will be.
  443. ** This value will be inserted into the header block. This is
  444. ** performed BEFORE the files are sorted because the data files
  445. ** are written to disk in the order that they were specified.
  446. */
  447. TotalSize = 0;
  448. for (index = 0; index < Count; index++) {
  449. array[index]->Offset = TotalSize;
  450. TotalSize += array[index]->Size;
  451. }
  452. /*
  453. ** Next, sort the data objects so that a binary search on CRC values
  454. ** can be used for file retrieval.
  455. */
  456. qsort(array, Count, sizeof(array[0]), compfunc);
  457. /*
  458. ** Output the header section of the data file. This contains
  459. ** the count of objects contained, the CRC, and offset for each.
  460. */
  461. fwrite(&Count, sizeof(Count), 1, outfile);
  462. fwrite(&TotalSize, sizeof(TotalSize), 1, outfile);
  463. for (index = 0; index < Count; index++) {
  464. fwrite(&array[index]->CRC, sizeof(array[index]->CRC), 1, outfile);
  465. fwrite(&array[index]->Offset, sizeof(array[index]->Offset), 1, outfile);
  466. fwrite(&array[index]->Size, sizeof(array[index]->Size), 1, outfile);
  467. }
  468. TotalSize += sizeof(Count) + sizeof(TotalSize) + (Count*12);
  469. if (!Quiet) {
  470. printf("size CRC Filename\n");
  471. printf("------ -------- -------------------------\n");
  472. }
  473. /*
  474. ** Now write the actual data -- one file at a time in the order that they were
  475. ** originally specified.
  476. */
  477. for (int order = 0; order < Count; order++) {
  478. for (index = 0; index < Count; index++) {
  479. DataClass * entry = array[index];
  480. if (entry->Index == order) {
  481. FILE *infile;
  482. long size;
  483. static char buffer[1024*30];
  484. if (!Quiet) {
  485. printf("%-8ld [%08lX] %s\n", entry->Size, entry->CRC, entry->Output_Filename());
  486. }
  487. size = entry->Size;
  488. infile = fopen(entry->Input_Filename(), "rb");
  489. while (size > 0) {
  490. int count;
  491. count = (size < sizeof(buffer)) ? (int)size : sizeof(buffer);
  492. count = fread(buffer, sizeof(buffer[0]), count, infile);
  493. fwrite(buffer, sizeof(buffer[0]), count, outfile);
  494. size -= count;
  495. if (!count) break; // Hmm..
  496. }
  497. fclose(infile);
  498. break;
  499. }
  500. }
  501. }
  502. fclose(outfile);
  503. }
  504. }
  505. /***********************************************************************************************
  506. * Calc_CRC -- Calculate the CRC value of a block of data. *
  507. * *
  508. * This routine is used to create a CRC value from a string of data bytes. *
  509. * *
  510. * INPUT: data -- Pointer to the data bytes to calculate the CRC value for. *
  511. * *
  512. * size -- The number of bytes in the data block. *
  513. * *
  514. * OUTPUT: Returns with the CRC value of the buffer specified. *
  515. * *
  516. * WARNINGS: none *
  517. * *
  518. * HISTORY: *
  519. * 08/06/1994 JLB : Created. *
  520. *=============================================================================================*/
  521. long Calc_CRC(void const *data, long size)
  522. {
  523. long crc = 0; // Accumulating CRC value.
  524. long const *ptr = static_cast<long const *>(data);
  525. /*
  526. ** Process the bulk of the data (4 bytes at a time).
  527. */
  528. for (; size > sizeof(long); size -= sizeof(long)) {
  529. long temp = *ptr++;
  530. asm {
  531. mov eax,crc
  532. rol eax,1
  533. add eax,temp
  534. mov crc,eax
  535. }
  536. }
  537. /*
  538. ** If there are any remainder bytes, then process them
  539. ** as a group of four, but the trailing left over bytes
  540. ** are forced to be NULL.
  541. */
  542. if (size) {
  543. long temp = 0;
  544. memcpy(&temp, ptr, (size_t)size);
  545. asm {
  546. mov eax,crc
  547. rol eax,1
  548. add eax,temp
  549. mov crc,eax
  550. }
  551. }
  552. return (crc);
  553. }
  554. char const * DataClass::Output_Filename(void)
  555. {
  556. char file[MAXFILE];
  557. char ext[MAXEXT];
  558. char path[MAXPATH];
  559. char drive[MAXDRIVE];
  560. static char filename[255];
  561. /*
  562. ** Break the original filename into its component parts.
  563. */
  564. fnsplit(Filename, drive, path, file, ext);
  565. /*
  566. ** Substitute the extension if a substitution is called for.
  567. */
  568. for (int index = 0; index < ExtCount; index++) {
  569. if (stricmp(ExtFrom[index], &ext[1]) == 0) {
  570. strcpy(&ext[1], ExtTo[index]);
  571. break;
  572. }
  573. }
  574. /*
  575. ** Strip the path from the filename if path stripping is enabled.
  576. */
  577. if (!Paths) {
  578. fnmerge(filename, NULL, NULL, file, ext);
  579. } else {
  580. fnmerge(filename, drive, path, file, ext);
  581. }
  582. return(&filename[0]);
  583. }
  584. char const * DataClass::Input_Filename(void)
  585. {
  586. return(Filename);
  587. }