IFF.CPP 14 KB

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