MIXFILE.CPP 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. *
  20. * 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
  21. *
  22. *----------------------------------------------------------------------------
  23. *
  24. * FILE
  25. * mixfile.c
  26. *
  27. * DESCRIPTION
  28. * A mix file is basically a group of files concatinated together
  29. * proceeded by a header describing where in the file each individual
  30. * entry is located. These routines are provided to simplify the access
  31. * to these file entries.
  32. *
  33. * PROGRAMMER
  34. * Denzil E. Long, Jr.
  35. *
  36. * DATE
  37. * January 26, 1995
  38. *
  39. *----------------------------------------------------------------------------
  40. *
  41. * MixFile format:
  42. *
  43. * HEADER
  44. * (2 bytes) Count - The number of entries in this file.
  45. * (4 bytes) Size - Size of the mix file in bytes.
  46. *
  47. * SUBBLOCKS (There are "Count" number of these.)
  48. * (4 bytes) CRC - Entry descriptor (CRC of filename).
  49. * (4 bytes) Offset - Offset in bytes from beginning of the DATA chunk.
  50. * (4 bytes) Size - Size of entry.
  51. *
  52. * DATA
  53. * Entry data.
  54. *
  55. *----------------------------------------------------------------------------
  56. *
  57. * PUBLIC
  58. * OpenMix - Open mix file for access.
  59. * CloseMix - Close a mix file.
  60. * OpenMixEntry - Open a mix file entry.
  61. *
  62. ****************************************************************************/
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <fcntl.h>
  66. #include <io.h>
  67. #include <mem.h>
  68. #include <string.h>
  69. #include "mixfile.h"
  70. #include "crc.h"
  71. /*---------------------------------------------------------------------------
  72. * PRIVATE DECLARATIONS
  73. *-------------------------------------------------------------------------*/
  74. int compfunc(void const *ptr1, void const *ptr2);
  75. /****************************************************************************
  76. *
  77. * NAME
  78. * OpenMix - Open mix file for access.
  79. *
  80. * SYNOPSIS
  81. * MixHandle = OpenMix(Name)
  82. *
  83. * MixHandle *OpenMix(char *);
  84. *
  85. * FUNCTION
  86. * Open a mix file for access.
  87. *
  88. * INPUTS
  89. * Name - Pointer to name of mix file to open.
  90. *
  91. * RESULT
  92. * MixHandle - Pointer to handle for mix file.
  93. *
  94. ****************************************************************************/
  95. MIXHandle *OpenMix(char *name)
  96. {
  97. MIXHeader mfhdr;
  98. MIXHandle *mix = NULL;
  99. long fh;
  100. long sbsize;
  101. long size;
  102. /* Open mix file and read in header. */
  103. if ((fh = open(name, (O_RDONLY|O_BINARY))) != -1) {
  104. if (read(fh, &mfhdr, sizeof(MIXHeader)) == sizeof(MIXHeader)) {
  105. /* Allocate handle based on the number of SubBlocks. */
  106. sbsize = (mfhdr.Count * sizeof(MIXSubBlock));
  107. size = sbsize + sizeof(MIXHandle);
  108. if ((mix = (MIXHandle *)malloc(size)) != NULL) {
  109. memset(mix, 0, size);
  110. mix->Name = name;
  111. mix->Size = mfhdr.Size;
  112. mix->Count = mfhdr.Count;
  113. /* Read in the SubBlock entries. */
  114. if (read(fh, &mix->Entries[0], sbsize) != sbsize) {
  115. free(mix);
  116. mix = NULL;
  117. }
  118. }
  119. }
  120. close(fh);
  121. }
  122. return (mix);
  123. }
  124. /****************************************************************************
  125. *
  126. * NAME
  127. * CloseMix - Close a mix file.
  128. *
  129. * SYNOPSIS
  130. * CloseMix(MixHandle)
  131. *
  132. * void CloseMix(MixHandle *);
  133. *
  134. * FUNCTION
  135. * Close a mix file by freeing its handle.
  136. *
  137. * INPUTS
  138. * MixHandle - Pointer to MixHandle returned by OpenMix().
  139. *
  140. * RESULT
  141. * NONE
  142. *
  143. ****************************************************************************/
  144. void CloseMix(MIXHandle *mix)
  145. {
  146. free(mix);
  147. }
  148. /****************************************************************************
  149. *
  150. * NAME
  151. * OpenMixEntry - Open a mix file entry.
  152. *
  153. * SYNOPSIS
  154. * FH = OpenMixEntry(MixHandle, EntryName)
  155. *
  156. * short OpenMixEntry(MIXHandle *, char *);
  157. *
  158. * FUNCTION
  159. * Opens an entry from the specified mix file handle. Use close() to close
  160. * the file when done.
  161. *
  162. * INPUTS
  163. * MixHandle - Pointer to MIXHandle containing entry to open.
  164. * EntryName - Pointer to name of mix file entry to open.
  165. *
  166. * RESULT
  167. * FH - DOS filehandle, -1 if unable to open.
  168. *
  169. ****************************************************************************/
  170. long OpenMixEntry(MIXHandle *mix, char *name)
  171. {
  172. MIXSubBlock key;
  173. MIXSubBlock *block;
  174. long fh;
  175. /* Search for the specified file in the mix file. */
  176. key.CRC = Calculate_CRC(name, strlen(name));
  177. block = (MIXSubBlock *)bsearch(&key, &mix->Entries[0], mix->Count,
  178. sizeof(MIXSubBlock), compfunc);
  179. /* If the block exists for the requested filename. */
  180. if (block != NULL) {
  181. /* Initialize the key for file access. */
  182. key.Offset = block->Offset;
  183. key.Offset += (mix->Count * sizeof(MIXSubBlock)) + sizeof(MIXHeader);
  184. /* Open the mix file. */
  185. if ((fh = open(mix->Name, (O_RDONLY|O_BINARY))) != -1) {
  186. /* Seek to the start of the file. */
  187. if (lseek(fh, key.Offset, SEEK_SET) == -1) {
  188. close(fh);
  189. fh = -1;
  190. }
  191. }
  192. }
  193. return (fh);
  194. }
  195. /****************************************************************************
  196. *
  197. * NAME
  198. * compfunc - Compare function for bsearch().
  199. *
  200. * SYNOPSIS
  201. * Result = compfunc(Entry1, Entry2);
  202. *
  203. * int compfunc(void const *, void const *);
  204. *
  205. * FUNCTION
  206. *
  207. * INPUTS
  208. * Entry1 - Pointer to first entry to compare.
  209. * Entry2 - Pointer to second entry to compare.
  210. *
  211. * RESULT
  212. * Result -
  213. *
  214. ****************************************************************************/
  215. int compfunc(void const *ptr1, void const *ptr2)
  216. {
  217. if (((MIXSubBlock *)ptr1)->CRC < ((MIXSubBlock *)ptr2)->CRC) return -1;
  218. if (((MIXSubBlock *)ptr1)->CRC > ((MIXSubBlock *)ptr2)->CRC) return 1;
  219. return(0);
  220. }