LOAD.CPP 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 <wwstd.h>
  41. #include "file.h"
  42. #include "iff.h"
  43. #include <misc.h>
  44. #include <dos.h>
  45. #include <wwmem.h>
  46. #if(LZW_SUPPORTED)
  47. /* These are our local pointer and size variables for the LZW table. They
  48. are set through the Set_Uncomp_Buffer routine. */
  49. PRIVATE int LZW_Table = 0; /* No current paragraph */
  50. PRIVATE unsigned int LZW_Table_Size = 0; /* No current size */
  51. #endif
  52. /*=========================================================================*/
  53. /* The following PRIVATE functions are in this file: */
  54. /*=========================================================================*/
  55. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  56. /***************************************************************************
  57. * LOAD_DATA -- Loads a data file from disk. *
  58. * *
  59. * This routine will load a data file from disk. It does no translation*
  60. * on the data. *
  61. * *
  62. * INPUT: name -- Pointer to ASCII filename of the data file. *
  63. * *
  64. * ptr -- Buffer to load the data file into. *
  65. * *
  66. * size -- Maximum size of the buffer (in bytes). *
  67. * *
  68. * OUTPUT: Returns with the number of bytes read. *
  69. * *
  70. * WARNINGS: none *
  71. * *
  72. * HISTORY: *
  73. * 06/24/1991 JLB : Created. *
  74. *=========================================================================*/
  75. unsigned long cdecl Load_Data(char const *name, VOID *ptr, unsigned long size)
  76. {
  77. int fd;
  78. fd = Open_File(name, READ);
  79. size = Read_File(fd, ptr, size);
  80. Close_File(fd);
  81. return(size);
  82. }
  83. /***************************************************************************
  84. * WRITE_DATA -- Writes a block of data as a file to disk. *
  85. * *
  86. * This routine will write a block of data as a file to the disk. It *
  87. * is the compliment of Load_Data. *
  88. * *
  89. * INPUT: name -- Name of the file to create. *
  90. * *
  91. * ptr -- Pointer to the block of data to write. *
  92. * *
  93. * size -- Size of the data block to be written. *
  94. * *
  95. * OUTPUT: Returns with the number of bytes actually written. *
  96. * *
  97. * WARNINGS: none *
  98. * *
  99. * HISTORY: *
  100. * 07/05/1992 JLB : Created. *
  101. *=========================================================================*/
  102. unsigned long cdecl Write_Data(char const *name, VOID *ptr, unsigned long size)
  103. {
  104. int fd;
  105. fd = Open_File(name, WRITE);
  106. size = Write_File(fd, ptr, size);
  107. Close_File(fd);
  108. return(size);
  109. }
  110. #ifdef NEVER
  111. /***************************************************************************
  112. * LOAD_ALLOC_DATA -- Loads and allocates buffer for a file. *
  113. * *
  114. * The routine will allocate a buffer and load the specified file into *
  115. * it. The kind of memory used for the buffer is determined by the *
  116. * memory allocation flags passed in. *
  117. * *
  118. * INPUT: name -- Name of the file to load. *
  119. * *
  120. * flags -- Memory allocation flags to use when allocating. *
  121. * *
  122. * OUTPUT: Returns with a pointer to the buffer that contains the file's *
  123. * data. *
  124. * *
  125. * WARNINGS: A memory error could occur if regular memory flags are *
  126. * specified. If XMS memory is specified, then this routine *
  127. * could likely return NULL. *
  128. * *
  129. * HISTORY: *
  130. * 05/28/1992 JLB : Created. *
  131. *=========================================================================*/
  132. VOID * cdecl Load_Alloc_Data(char const *name, MemoryFlagType flags)
  133. {
  134. int fd; // Working file handle.
  135. unsigned long size; // Size of the file to load.
  136. VOID *buffer; // Buffer to hold the file.
  137. fd = Open_File(name, READ);
  138. size = File_Size(fd);
  139. buffer = Alloc(size, flags);
  140. if (buffer) {
  141. Read_File(fd, buffer, size);
  142. }
  143. Close_File(fd);
  144. return(buffer);
  145. }
  146. #endif
  147. /***************************************************************************
  148. * LOAD_UNCOMPRESS -- Load and uncompress the given file. *
  149. * *
  150. * INPUT: BYTE * - file name to uncompress *
  151. * GraphicBufferClass& - to load the source data into *
  152. * GraphicBufferClass& - for the picture *
  153. * VOID * - ptr for header uncompressed data *
  154. * *
  155. * OUTPUT: unsigned long size of uncompressed data *
  156. * *
  157. * WARNINGS: none *
  158. * *
  159. * HISTORY: *
  160. * 05/28/1991 CY : Created. *
  161. * 06/26/1991 JLB : Handles load & uncompress to same buffer. *
  162. *=========================================================================*/
  163. unsigned long cdecl Load_Uncompress(char const *file, BufferClass& uncomp_buff, BufferClass& dest_buff, VOID *reserved_data)
  164. {
  165. int fd; // Source file handle.
  166. unsigned int isize; // Size of the file.
  167. unsigned int skipsize; // Size of the skip data bytes.
  168. VOID *uncomp_ptr; // Source buffer pointer.
  169. char *newuncomp_ptr; // Adjusted source pointer.
  170. uncomp_ptr = uncomp_buff.Get_Buffer(); // get a pointer to buffer
  171. /*======================================================================*/
  172. /* Read the file into the uncompression buffer. */
  173. /*======================================================================*/
  174. fd = Open_File(file, READ); // Open up the file to read from
  175. Read_File(fd, (char *) &isize, 2L); // Read the file size
  176. Read_File(fd, uncomp_ptr, 8L); // Read the header bytes in.
  177. isize -= 8; // Remaining data in file.
  178. /*======================================================================*/
  179. /* Check for and read in the skip data block. */
  180. /*======================================================================*/
  181. skipsize = *(((int*)uncomp_ptr) + 3);
  182. if (reserved_data && skipsize) {
  183. Read_File(fd, reserved_data, (unsigned long) skipsize);
  184. } else {
  185. Seek_File(fd, skipsize, SEEK_CUR);
  186. }
  187. *( ((int*)uncomp_ptr+3) ) = 0; // K/O any skip value.
  188. isize -= skipsize;
  189. /*======================================================================*/
  190. /* If the source and dest buffer are the same, we adjust the pointer so */
  191. /* that the compressed data is loaded into the end of the buffer. In */
  192. /* this way the uncompress code can write to the same buffer. */
  193. /*======================================================================*/
  194. newuncomp_ptr = (char *)Add_Long_To_Pointer(uncomp_buff.Get_Buffer(), uncomp_buff.Get_Size() - (isize+8L));
  195. /*======================================================================*/
  196. /* Duplicate the header bytes. */
  197. /*======================================================================*/
  198. Mem_Copy(uncomp_ptr,newuncomp_ptr,8);
  199. /*======================================================================*/
  200. /* Read in the main compressed part of the file. */
  201. /*======================================================================*/
  202. Read_File(fd, newuncomp_ptr + 8, (unsigned long)isize);
  203. Close_File(fd);
  204. /*======================================================================*/
  205. /* Uncompress the file into the destination buffer (which may very well */
  206. /* be the source buffer). */
  207. /*======================================================================*/
  208. return(Uncompress_Data(newuncomp_ptr, dest_buff.Get_Buffer()));
  209. }
  210. #if(0)
  211. /***************************************************************************
  212. * LOAD_UNCOMPRESS -- Load and uncompress the given file. *
  213. * *
  214. * INPUT: BYTE *file name to uncompress, BuffType uncomp_buff to load *
  215. * the source data into, BuffType dest_buff for the picture, *
  216. * VOID *reserved_data pointer for header uncompressed data *
  217. * *
  218. * OUTPUT: unsigned long size of uncompressed data *
  219. * *
  220. * WARNINGS: none *
  221. * *
  222. * HISTORY: *
  223. * 05/28/1991 CY : Created. *
  224. * 06/26/1991 JLB : Handles load & uncompress to same buffer. *
  225. *=========================================================================*/
  226. unsigned long cdecl Load_Uncompress(char const *file, BuffType uncomp_buff, BuffType dest_buff, VOID *reserved_data)
  227. {
  228. int fd; // Source file handle.
  229. unsigned int isize; // Size of the file.
  230. unsigned int skipsize; // Size of the skip data bytes.
  231. VOID *uncomp_ptr; // Source buffer pointer.
  232. char *newuncomp_ptr; // Adjusted source pointer.
  233. uncomp_ptr = Get_Buff(uncomp_buff); /* Get pointer to uncomp buffer */
  234. /* Read the file into the uncomp_buff */
  235. fd = Open_File(file, READ);
  236. Read_File(fd, (char *) &isize, 2L); /* Read the file size */
  237. #if(AMIGA)
  238. isize = Reverse_WORD(isize);
  239. #endif
  240. Read_File(fd, uncomp_ptr, 8L); // Read the header bytes in.
  241. isize -= 8; // Remaining data in file.
  242. /*
  243. ** Check for and read in the skip data block.
  244. */
  245. skipsize = *(((int*)uncomp_ptr) + 3);
  246. #if(AMIGA)
  247. skipsize = Reverse_WORD(skipsize);
  248. #endif
  249. if (reserved_data && skipsize) {
  250. Read_File(fd, reserved_data, (unsigned long) skipsize);
  251. } else {
  252. Seek_File(fd, skipsize, SEEK_CUR);
  253. }
  254. *( ((int*)uncomp_ptr+3) ) = 0; // K/O any skip value.
  255. isize -= skipsize;
  256. /*
  257. ** If the source and dest buffer are the same, we
  258. ** adjust the pointer so that the compressed data is
  259. ** loaded into the end of the buffer. In this way the
  260. ** uncompress code can write to the same buffer.
  261. */
  262. #if(IBM)
  263. newuncomp_ptr = (char *)Add_Long_To_Pointer(Get_Buff(uncomp_buff), PageArraySize[uncomp_buff] - (isize+8L));
  264. //newuncomp_ptr = (char*)MK_FP(PageArray[uncomp_buff],0);
  265. //newuncomp_ptr += (unsigned int)(PageArraySize[uncomp_buff] - (isize+8));
  266. //newuncomp_ptr = Normalize_Pointer(newuncomp_ptr);
  267. //newuncomp_ptr = MK_FP(FP_SEG(newuncomp_ptr),0);
  268. #else
  269. newuncomp_ptr = Get_Buff(uncomp_buff);
  270. newuncomp_ptr += PageArraySize[uncomp_buff] - ((isize+10) & 0xFFFE);
  271. #endif
  272. /*
  273. ** Duplicate the header bytes.
  274. */
  275. Mem_Copy(uncomp_ptr,newuncomp_ptr,8);
  276. /*
  277. ** Read in the main compressed part of the file.
  278. */
  279. Read_File(fd, newuncomp_ptr + 8, (unsigned long)isize);
  280. Close_File(fd);
  281. return(Uncompress_Data(newuncomp_ptr, Get_Buff(dest_buff)));
  282. }
  283. #endif
  284. /***************************************************************************
  285. * Uncompress_Data -- Uncompresses data from one buffer to another. *
  286. * *
  287. * This routine takes data from a compressed file (sans the first two *
  288. * size bytes) and uncompresses it to a destination buffer. The source *
  289. * data MUST have the CompHeaderType at its start. *
  290. * *
  291. * INPUT: src -- Source compressed data pointer. *
  292. * *
  293. * dst -- Destination (paragraph aligned) pointer. *
  294. * *
  295. * OUTPUT: Returns with the size of the uncompressed data. *
  296. * *
  297. * WARNINGS: If LCW compression is used, the destination buffer must *
  298. * be paragraph aligned. *
  299. * *
  300. * HISTORY: *
  301. * 09/17/1993 JLB : Created. *
  302. *=========================================================================*/
  303. unsigned long cdecl Uncompress_Data(VOID const *src, VOID *dst)
  304. {
  305. unsigned int skip; // Number of leading data to skip.
  306. CompressionType method; // Compression method used.
  307. unsigned long uncomp_size=NULL;
  308. #if(LZW_SUPPORTED)
  309. VOID *table_buffer;
  310. #endif
  311. if (!src || !dst) return(NULL);
  312. /*
  313. ** Interpret the data block header structure to determine
  314. ** compression method, size, and skip data amount.
  315. */
  316. uncomp_size = ((CompHeaderType*)src)->Size;
  317. #if(AMIGA)
  318. uncomp_size = Reverse_LONG(uncomp_size);
  319. #endif
  320. skip = ((CompHeaderType*)src)->Skip;
  321. #if(AMIGA)
  322. skip = Reverse_WORD(skip);
  323. #endif
  324. method = (CompressionType) ((CompHeaderType*)src)->Method;
  325. src = Add_Long_To_Pointer((VOID *)src, (long)sizeof(CompHeaderType) + (long)skip);
  326. switch (method) {
  327. default:
  328. case NOCOMPRESS:
  329. Mem_Copy((VOID *) src, dst, uncomp_size);
  330. break;
  331. case HORIZONTAL:
  332. #if LIB_EXTERNS_RESOLVED
  333. RLE_Uncompress((VOID *) src, dst, uncomp_size);
  334. #endif
  335. break;
  336. case LCW:
  337. LCW_Uncompress((VOID *) src, (VOID *) dst, (unsigned long) uncomp_size);
  338. break;
  339. #if(LZW_SUPPORTED)
  340. case LZW12:
  341. /* If the current buffer isn't big enough, try to
  342. allocate one that is */
  343. if (LZW_Table_Size < LZW12BUFFERSIZE) {
  344. table_buffer = Alloc((long) LZW12BUFFERSIZE, MEM_PARA);
  345. LZW12_Uncompress(FP_SEG(src), FP_SEG(dst),
  346. FP_SEG(table_buffer));
  347. Free(table_buffer);
  348. }
  349. else {
  350. LZW12_Uncompress(FP_SEG(src), FP_SEG(dst), LZW_Table);
  351. }
  352. break;
  353. case LZW14:
  354. /* If the current buffer isn't big enough, try to
  355. allocate one that is */
  356. if (LZW_Table_Size < LZW14BUFFERSIZE) {
  357. table_buffer = Alloc((long) LZW14BUFFERSIZE, MEM_PARA);
  358. LZW14_Uncompress(FP_SEG(src), FP_SEG(dst),
  359. FP_SEG(table_buffer));
  360. Free(table_buffer);
  361. }
  362. else {
  363. LZW14_Uncompress(FP_SEG(src), FP_SEG(dst), LZW_Table);
  364. }
  365. break;
  366. #endif
  367. }
  368. return(uncomp_size);
  369. }
  370. #if(LZW_SUPPORTED)
  371. /* ARGSUSED */
  372. #pragma argsused
  373. VOID cdecl Set_Uncomp_Buffer(int buffer_segment, unsigned int size_of_buffer)
  374. {
  375. if ((LZW_Table = buffer_segment) == NULL) {
  376. /* ERROR HERE */
  377. }
  378. if ((LZW_Table_Size = size_of_buffer) == 0U) {
  379. /* ERROR HERE */
  380. }
  381. }
  382. #endif
  383.