IFF.CPP 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 A S S O C I A T E S **
  20. ***************************************************************************
  21. * *
  22. * Project Name : Westwood Library *
  23. * *
  24. * File Name : IFF.C *
  25. * *
  26. * Programmer : Joe L. Bostic *
  27. * *
  28. * Start Date : May 16, 1991 *
  29. * *
  30. * Last Update : April 19, 1994 [SKB] *
  31. * *
  32. * *
  33. * IFF reader code designed for loading pictures (ILBM or PBM). *
  34. * *
  35. *-------------------------------------------------------------------------*
  36. * Functions: *
  37. * Close_Iff_File -- Closes an IFF file handle. *
  38. * Get_Iff_Chunk_Size -- Get the size of the given IFF chunk. *
  39. * Open_Iff_File -- Opens an IFF file for reading. *
  40. * Read_Iff_Chunk -- Reads a chunk from an IFF file. *
  41. * Write_Iff_Chunk -- Writes an IFF chuck out. *
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. #include "iff.h"
  44. #include "file.h"
  45. #define ID_FORM MAKE_ID('F','O','R','M')
  46. #ifdef MIN
  47. #undef MIN
  48. #endif
  49. /***************************************************************************
  50. * OPEN_IFF_FILE -- Opens an IFF file for reading. *
  51. * *
  52. * This function will open an IFF file for reading. It will perform *
  53. * a the simple validity test of checking the first four bytes to make *
  54. * sure they are "FORM". The value returned is the filehandle of the *
  55. * opened file. *
  56. * *
  57. * INPUT: filename - ASCII name of the IFF file to be opened. *
  58. * *
  59. * OUTPUT: Returns the filehandle. If there is an error or the file *
  60. * is not an IFF FORM then -1 will be returned. *
  61. * *
  62. * WARNINGS: You are responsible for error handling if this function *
  63. * returns -1 (not an IFF file). *
  64. * *
  65. * HISTORY: *
  66. * 05/16/1991 JLB : Created. *
  67. * 04/19/1994 SKB : Update to 32 bit library. *
  68. *=========================================================================*/
  69. int __cdecl Open_Iff_File(char const *filename)
  70. {
  71. int fh; // File handle.
  72. long type; // IFF file type.
  73. /* We want to be able to open the file for READ | WRITE, but we do not
  74. want the Open_File to create it. So check to see if it exists before
  75. the Open_File */
  76. // fh = Open_File(filename, READ); // Open the source file for READ
  77. // Close_File(fh);
  78. //fh = Open_File(filename, READ | WRITE); // Open the source file again
  79. fh = Open_File(filename, READ); // Open the source file again
  80. // Validate that it is a FORM type.
  81. Read_File(fh, &type, 4L);
  82. if (type == ID_FORM) {
  83. // The file is valid (so far). Position the read so that the actual
  84. // IFF file type code can be read.
  85. Seek_File(fh, 4L, SEEK_CUR); // Skip the filesize bytes.
  86. } else {
  87. // This is NOT an IFF file. Close the source file and return with
  88. // the error code.
  89. Close_File(fh);
  90. fh = WW_ERROR;
  91. }
  92. return fh;
  93. }
  94. /***************************************************************************
  95. * CLOSE_IFF_FILE -- Closes an IFF file handle. *
  96. * *
  97. * The routine will close the file that was opened with the *
  98. * Open_Iff_File() function. *
  99. * *
  100. * INPUT: fh - File handle that was returned from Open_Iff_File(). *
  101. * *
  102. * OUTPUT: none *
  103. * *
  104. * WARNINGS: none *
  105. * *
  106. * HISTORY: *
  107. * 05/16/1991 JLB : Created. *
  108. * 04/19/1994 SKB : Update to 32 bit library. *
  109. *=========================================================================*/
  110. void __cdecl Close_Iff_File(int fh)
  111. {
  112. if (fh != WW_ERROR) Close_File(fh);
  113. }
  114. /***************************************************************************
  115. * GET_IFF_CHUNK_SIZE -- Get the size of the given IFF chunk. *
  116. * *
  117. * INPUT: int file handle to open IFF file, long id to get size of *
  118. * *
  119. * OUTPUT: long size of the chunk or 0L if it was not found *
  120. * *
  121. * WARNINGS: none *
  122. * *
  123. * HISTORY: *
  124. * 06/03/1991 CY : Created. *
  125. * 04/19/1994 SKB : Update to 32 bit library. *
  126. *=========================================================================*/
  127. unsigned long __cdecl Get_Iff_Chunk_Size(int fh, long id)
  128. {
  129. long form; // Chunk iff form name.
  130. long chunksize; // Size of the chunk.
  131. char first_iteration; // Check once the current chunk name
  132. first_iteration = TRUE;
  133. for (;;) {
  134. if (Read_File(fh, &form, 4L) != 4L && !first_iteration) break;
  135. if (Read_File(fh, (char *) &chunksize, 4L) != 4L && !first_iteration) break;
  136. #if(IBM)
  137. chunksize = Reverse_Long(chunksize);
  138. #endif
  139. if (id == form) {
  140. Seek_File(fh, -8L, SEEK_CUR); // Seek back to the start of
  141. return(chunksize); // the chunk & return size
  142. } else {
  143. if (first_iteration) {
  144. Seek_File(fh, 12L, SEEK_SET); // Start at beginning of file.
  145. first_iteration = FALSE; // Don't do this again
  146. } else {
  147. /* Otherwise, go to the next chunk in the file */
  148. chunksize = (chunksize + 1) & 0xFFFFFFFEL;
  149. Seek_File(fh, chunksize, SEEK_CUR);
  150. }
  151. }
  152. }
  153. return(0L);
  154. }
  155. /***************************************************************************
  156. * READ_IFF_CHUNK -- Reads a chunk from an IFF file. *
  157. * *
  158. * Once an IFF file is opened, various chunks must be read from it. *
  159. * This routine will search through the IFF file and load in the *
  160. * specified chunk. It will scan through the entire file when *
  161. * searching for the chunk. It will load the FIRST chunk of the given *
  162. * type. *
  163. * *
  164. * INPUT: fh - File handle of IFF file. *
  165. * *
  166. * id - Chunk ID code. *
  167. * *
  168. * buffer - Pointer to buffer to load the chunk. *
  169. * *
  170. * maxsize - Maximum data bytes to read. *
  171. * *
  172. * OUTPUT: Returns with the number of bytes read from the chunk. *
  173. * If 0 is returned, this indicates that the chunk wasn't *
  174. * found. *
  175. * *
  176. * WARNINGS: none *
  177. * *
  178. * HISTORY: *
  179. * 05/16/1991 JLB : Created. *
  180. * 04/19/1994 SKB : Update to 32 bit library. *
  181. *=========================================================================*/
  182. unsigned long __cdecl Read_Iff_Chunk(int fh, long id, void *buffer, unsigned long maxsize)
  183. {
  184. long form; // Chunk iff form name.
  185. unsigned long chunksize; // Size of the chunk.
  186. char first_iteration; // Check once the current chunk name
  187. first_iteration = TRUE;
  188. for (;;) {
  189. if (Read_File(fh, &form, 4L) != 4L && !first_iteration) break;
  190. if (Read_File(fh, (char *) &chunksize, 4L) != 4L && !first_iteration) break;
  191. #if(IBM)
  192. chunksize = Reverse_Long(chunksize);
  193. #endif
  194. if (id == form) {
  195. maxsize = MIN(maxsize, chunksize);
  196. Read_File(fh, buffer, maxsize); // Read the buffer.
  197. chunksize = (chunksize + 1) & 0xFFFFFFFEL;
  198. if (maxsize < chunksize) {
  199. Seek_File(fh, chunksize - maxsize, SEEK_CUR);
  200. }
  201. return(maxsize);
  202. } else {
  203. if (first_iteration) {
  204. Seek_File(fh, 12L, SEEK_SET); // Start at beginning of file.
  205. first_iteration = FALSE; // Don't do this again
  206. } else {
  207. /* Otherwise, go to the next chunk in the file */
  208. chunksize = (chunksize + 1) & 0xFFFFFFFEL;
  209. Seek_File(fh, chunksize, SEEK_CUR);
  210. }
  211. }
  212. }
  213. return(0L);
  214. }
  215. /***************************************************************************
  216. * WRITE_IFF_CHUNK -- Writes an IFF chuck out. *
  217. * *
  218. * INPUT: *
  219. * *
  220. * OUTPUT: *
  221. * *
  222. * WARNINGS: *
  223. * *
  224. * HISTORY: *
  225. * 04/19/1994 SKB : Created. *
  226. *=========================================================================*/
  227. void __cdecl Write_Iff_Chunk(int file, long id, void *buffer, long length)
  228. {
  229. long pos; // Current position in the IFF file.
  230. long oldpos; // Record of start of chunk offset.
  231. long endpos; // end of file offset before we write our data
  232. long value;
  233. BOOL odd; // Is length odd?
  234. char pad = 0; // Optional padding byte for even sized chunks.
  235. /*
  236. ** Get the current end of file (before we write more data to the file)
  237. */
  238. pos = Seek_File (file, 0L, SEEK_CUR);
  239. endpos = Seek_File (file, 0L, SEEK_END);
  240. Seek_File (file, pos, SEEK_SET);
  241. if (length) {
  242. value = id;
  243. odd = (short)length & 0x01;
  244. Write_File(file, &value, 4L);
  245. oldpos = Seek_File(file, 0L, SEEK_CUR);
  246. Write_File(file, &value, 4L);
  247. Write_File(file, buffer, length);
  248. pos = Seek_File(file, 0L, SEEK_CUR);
  249. if (odd) {
  250. Write_File(file, &pad, 1L);
  251. }
  252. /*
  253. ** Update the chunk size long.
  254. */
  255. Seek_File(file, oldpos, SEEK_SET);
  256. value = IFFize_LONG((pos - oldpos)-4);
  257. Write_File(file, &value, 4L);
  258. /*
  259. ** Update the file size LONG. if we are not just overwriting existing data
  260. */
  261. // (MCC)
  262. if ( endpos < pos ) {
  263. Seek_File(file, 4L, SEEK_SET);
  264. value = IFFize_LONG((pos+odd) - 8);
  265. Write_File(file, &value, 4L);
  266. }
  267. /*
  268. ** Return to end of file.
  269. */
  270. Seek_File(file, 0L, SEEK_END);
  271. }
  272. }