LOAD.CPP 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. /* $Header: g:/library/wwlib32/file/rcs/load.cpp 1.4 1994/04/22 12:42:21 scott_bowen Exp $ */
  19. /***************************************************************************
  20. ** 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 **
  21. ***************************************************************************
  22. * *
  23. * Project Name : LIBRARY *
  24. * *
  25. * File Name : LOAD.C *
  26. * *
  27. * Programmer : Christopher Yates *
  28. * *
  29. * Last Update : September 17, 1993 [JLB] *
  30. * *
  31. *-------------------------------------------------------------------------*
  32. * Functions: *
  33. * Load_Uncompress -- Load and uncompress the given file. *
  34. * Uncompress_Data -- Uncompress standard CPS buffer. *
  35. * Load_Data -- Loads a data file from disk. *
  36. * Load_Alloc_Data -- Loads and allocates buffer for a file. *
  37. * Write_Data -- Writes a block of data as a file to disk. *
  38. * Uncompress_Data -- Uncompresses data from one buffer to another. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "iff.h"
  41. #include "file.h"
  42. #include <misc.h>
  43. #include <wwstd.h>
  44. #include <dos.h>
  45. #include <wwmem.h>
  46. /*=========================================================================*/
  47. /* The following PRIVATE functions are in this file: */
  48. /*=========================================================================*/
  49. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  50. /***************************************************************************
  51. * LOAD_DATA -- Loads a data file from disk. *
  52. * *
  53. * This routine will load a data file from disk. It does no translation*
  54. * on the data. *
  55. * *
  56. * INPUT: name -- Pointer to ASCII filename of the data file. *
  57. * *
  58. * ptr -- Buffer to load the data file into. *
  59. * *
  60. * size -- Maximum size of the buffer (in bytes). *
  61. * *
  62. * OUTPUT: Returns with the number of bytes read. *
  63. * *
  64. * WARNINGS: none *
  65. * *
  66. * HISTORY: *
  67. * 06/24/1991 JLB : Created. *
  68. *=========================================================================*/
  69. unsigned long __cdecl Load_Data(char const *name, void *ptr, unsigned long size)
  70. {
  71. int fd;
  72. fd = Open_File(name, READ);
  73. size = Read_File(fd, ptr, size);
  74. Close_File(fd);
  75. return(size);
  76. }
  77. /***************************************************************************
  78. * WRITE_DATA -- Writes a block of data as a file to disk. *
  79. * *
  80. * This routine will write a block of data as a file to the disk. It *
  81. * is the compliment of Load_Data. *
  82. * *
  83. * INPUT: name -- Name of the file to create. *
  84. * *
  85. * ptr -- Pointer to the block of data to write. *
  86. * *
  87. * size -- Size of the data block to be written. *
  88. * *
  89. * OUTPUT: Returns with the number of bytes actually written. *
  90. * *
  91. * WARNINGS: none *
  92. * *
  93. * HISTORY: *
  94. * 07/05/1992 JLB : Created. *
  95. *=========================================================================*/
  96. unsigned long __cdecl Write_Data(char const *name, void *ptr, unsigned long size)
  97. {
  98. int fd;
  99. fd = Open_File(name, WRITE);
  100. size = Write_File(fd, ptr, size);
  101. Close_File(fd);
  102. return(size);
  103. }
  104. /***************************************************************************
  105. * LOAD_ALLOC_DATA -- Loads and allocates buffer for a file. *
  106. * *
  107. * The routine will allocate a buffer and load the specified file into *
  108. * it. The kind of memory used for the buffer is determined by the *
  109. * memory allocation flags passed in. *
  110. * *
  111. * INPUT: name -- Name of the file to load. *
  112. * *
  113. * flags -- Memory allocation flags to use when allocating. *
  114. * *
  115. * OUTPUT: Returns with a pointer to the buffer that contains the file's *
  116. * data. *
  117. * *
  118. * WARNINGS: A memory error could occur if regular memory flags are *
  119. * specified. If XMS memory is specified, then this routine *
  120. * could likely return NULL. *
  121. * *
  122. * HISTORY: *
  123. * 05/28/1992 JLB : Created. *
  124. *=========================================================================*/
  125. void * __cdecl Load_Alloc_Data(char const *name, MemoryFlagType flags)
  126. {
  127. int fd; // Working file handle.
  128. unsigned long size; // Size of the file to load.
  129. void *buffer; // Buffer to hold the file.
  130. fd = Open_File(name, READ);
  131. size = File_Size(fd);
  132. buffer = Alloc(size, flags);
  133. if (buffer) {
  134. Read_File(fd, buffer, size);
  135. }
  136. Close_File(fd);
  137. return(buffer);
  138. }
  139. /***************************************************************************
  140. * LOAD_UNCOMPRESS -- Load and uncompress the given file. *
  141. * *
  142. * INPUT: char * - file name to uncompress *
  143. * GraphicBufferClass& - to load the source data into *
  144. * GraphicBufferClass& - for the picture *
  145. * void * - ptr for header uncompressed data *
  146. * *
  147. * OUTPUT: unsigned long size of uncompressed data *
  148. * *
  149. * WARNINGS: none *
  150. * *
  151. * HISTORY: *
  152. * 05/28/1991 CY : Created. *
  153. * 06/26/1991 JLB : Handles load & uncompress to same buffer. *
  154. *=========================================================================*/
  155. unsigned long __cdecl Load_Uncompress(char const *file, BufferClass& uncomp_buff, BufferClass& dest_buff, void *reserved_data)
  156. {
  157. int fd; // Source file handle.
  158. unsigned int isize=0; // Size of the file.
  159. unsigned int skipsize; // Size of the skip data bytes.
  160. void *uncomp_ptr; // Source buffer pointer.
  161. char *newuncomp_ptr; // Adjusted source pointer.
  162. uncomp_ptr = uncomp_buff.Get_Buffer(); // get a pointer to buffer
  163. /*======================================================================*/
  164. /* Read the file into the uncompression buffer. */
  165. /*======================================================================*/
  166. fd = Open_File(file, READ); // Open up the file to read from
  167. Read_File(fd, (char *) &isize, 2L); // Read the file size
  168. Read_File(fd, uncomp_ptr, 8L); // Read the header bytes in.
  169. isize -= 8; // Remaining data in file.
  170. /*======================================================================*/
  171. /* Check for and read in the skip data block. */
  172. /*======================================================================*/
  173. skipsize = *(((short *)uncomp_ptr) + 3);
  174. if (reserved_data && skipsize) {
  175. Read_File(fd, reserved_data, (unsigned long) skipsize);
  176. } else {
  177. Seek_File(fd, skipsize, SEEK_CUR);
  178. }
  179. *( ((short *)uncomp_ptr+3) ) = 0; // K/O any skip value.
  180. isize -= skipsize;
  181. /*======================================================================*/
  182. /* If the source and dest buffer are the same, we adjust the pointer so */
  183. /* that the compressed data is loaded into the end of the buffer. In */
  184. /* this way the uncompress code can write to the same buffer. */
  185. /*======================================================================*/
  186. newuncomp_ptr = (char *)Add_Long_To_Pointer(uncomp_buff.Get_Buffer(), uncomp_buff.Get_Size() - (isize+8L));
  187. /*======================================================================*/
  188. /* Duplicate the header bytes. */
  189. /*======================================================================*/
  190. Mem_Copy(uncomp_ptr,newuncomp_ptr,8);
  191. /*======================================================================*/
  192. /* Read in the main compressed part of the file. */
  193. /*======================================================================*/
  194. Read_File(fd, newuncomp_ptr + 8, (unsigned long)isize);
  195. Close_File(fd);
  196. /*======================================================================*/
  197. /* Uncompress the file into the destination buffer (which may very well */
  198. /* be the source buffer). */
  199. /*======================================================================*/
  200. return(Uncompress_Data(newuncomp_ptr, dest_buff.Get_Buffer()));
  201. }
  202. #if(0)
  203. /***************************************************************************
  204. * LOAD_UNCOMPRESS -- Load and uncompress the given file. *
  205. * *
  206. * INPUT: char *file name to uncompress, BuffType uncomp_buff to load *
  207. * the source data into, BuffType dest_buff for the picture, *
  208. * void *reserved_data pointer for header uncompressed data *
  209. * *
  210. * OUTPUT: unsigned long size of uncompressed data *
  211. * *
  212. * WARNINGS: none *
  213. * *
  214. * HISTORY: *
  215. * 05/28/1991 CY : Created. *
  216. * 06/26/1991 JLB : Handles load & uncompress to same buffer. *
  217. *=========================================================================*/
  218. unsigned long __cdecl Load_Uncompress(char const *file, BuffType uncomp_buff, BuffType dest_buff, void *reserved_data)
  219. {
  220. int fd; // Source file handle.
  221. unsigned int isize; // Size of the file.
  222. unsigned int skipsize; // Size of the skip data bytes.
  223. void *uncomp_ptr; // Source buffer pointer.
  224. char *newuncomp_ptr; // Adjusted source pointer.
  225. uncomp_ptr = Get_Buff(uncomp_buff); /* Get pointer to uncomp buffer */
  226. /* Read the file into the uncomp_buff */
  227. fd = Open_File(file, READ);
  228. Read_File(fd, (char *) &isize, 2L); /* Read the file size */
  229. #if(AMIGA)
  230. isize = Reverse_Word(isize);
  231. #endif
  232. Read_File(fd, uncomp_ptr, 8L); // Read the header bytes in.
  233. isize -= 8; // Remaining data in file.
  234. /*
  235. ** Check for and read in the skip data block.
  236. */
  237. skipsize = *(((short*)uncomp_ptr) + 3);
  238. #if(AMIGA)
  239. skipsize = Reverse_Word(skipsize);
  240. #endif
  241. if (reserved_data && skipsize) {
  242. Read_File(fd, reserved_data, (unsigned long) skipsize);
  243. } else {
  244. Seek_File(fd, skipsize, SEEK_CUR);
  245. }
  246. *( ((short *)uncomp_ptr+3) ) = 0; // K/O any skip value.
  247. isize -= skipsize;
  248. /*
  249. ** If the source and dest buffer are the same, we
  250. ** adjust the pointer so that the compressed data is
  251. ** loaded into the end of the buffer. In this way the
  252. ** uncompress code can write to the same buffer.
  253. */
  254. #if(IBM)
  255. newuncomp_ptr = (char *)Add_Long_To_Pointer(Get_Buff(uncomp_buff), PageArraySize[uncomp_buff] - (isize+8L));
  256. #else
  257. newuncomp_ptr = Get_Buff(uncomp_buff);
  258. newuncomp_ptr += PageArraySize[uncomp_buff] - ((isize+10) & 0xFFFE);
  259. #endif
  260. /*
  261. ** Duplicate the header bytes.
  262. */
  263. Mem_Copy(uncomp_ptr,newuncomp_ptr,8);
  264. /*
  265. ** Read in the main compressed part of the file.
  266. */
  267. Read_File(fd, newuncomp_ptr + 8, (unsigned long)isize);
  268. Close_File(fd);
  269. return(Uncompress_Data(newuncomp_ptr, Get_Buff(dest_buff)));
  270. }
  271. #endif
  272. /***************************************************************************
  273. * Uncompress_Data -- Uncompresses data from one buffer to another. *
  274. * *
  275. * This routine takes data from a compressed file (sans the first two *
  276. * size bytes) and uncompresses it to a destination buffer. The source *
  277. * data MUST have the CompHeaderType at its start. *
  278. * *
  279. * INPUT: src -- Source compressed data pointer. *
  280. * *
  281. * dst -- Destination (paragraph aligned) pointer. *
  282. * *
  283. * OUTPUT: Returns with the size of the uncompressed data. *
  284. * *
  285. * WARNINGS: If LCW compression is used, the destination buffer must *
  286. * be paragraph aligned. *
  287. * *
  288. * HISTORY: *
  289. * 09/17/1993 JLB : Created. *
  290. *=========================================================================*/
  291. unsigned long __cdecl Uncompress_Data(void const *src, void *dst)
  292. {
  293. unsigned int skip; // Number of leading data to skip.
  294. CompressionType method; // Compression method used.
  295. unsigned long uncomp_size=NULL;
  296. if (!src || !dst) return(NULL);
  297. /*
  298. ** Interpret the data block header structure to determine
  299. ** compression method, size, and skip data amount.
  300. */
  301. uncomp_size = ((CompHeaderType*)src)->Size;
  302. #if(AMIGA)
  303. uncomp_size = Reverse_Long(uncomp_size);
  304. #endif
  305. skip = ((CompHeaderType*)src)->Skip;
  306. #if(AMIGA)
  307. skip = Reverse_Word(skip);
  308. #endif
  309. method = (CompressionType) ((CompHeaderType*)src)->Method;
  310. src = Add_Long_To_Pointer((void *)src, (long)sizeof(CompHeaderType) + (long)skip);
  311. switch (method) {
  312. default:
  313. case NOCOMPRESS:
  314. Mem_Copy((void *) src, dst, uncomp_size);
  315. break;
  316. case HORIZONTAL:
  317. #if LIB_EXTERNS_RESOLVED
  318. RLE_Uncompress((void *) src, dst, uncomp_size);
  319. #endif
  320. break;
  321. case LCW:
  322. LCW_Uncompress((void *) src, (void *) dst, (unsigned long) uncomp_size);
  323. break;
  324. }
  325. return(uncomp_size);
  326. }