LOAD.BAK 19 KB

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