LCWPIPE.CPP 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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: /CounterStrike/LCWPIPE.CPP 1 3/03/97 10:25a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : LCWPIPE.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 06/30/96 *
  30. * *
  31. * Last Update : July 4, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * LCWPipe::Flush -- Flushes any partially accumulated block. *
  36. * LCWPipe::LCWPipe -- Constructor for the LCW processor pipe. *
  37. * LCWPipe::Put -- Send some data through the LCW processor pipe. *
  38. * LCWPipe::~LCWPipe -- Deconstructor for the LCW pipe object. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "lcwpipe.h"
  41. #include "lcw.h"
  42. #include <string.h>
  43. #include <assert.h>
  44. /***********************************************************************************************
  45. * LCWPipe::LCWPipe -- Constructor for the LCW processor pipe. *
  46. * *
  47. * This will initialize the LCWPipe object so that it is prepared for compression or *
  48. * decompression as indicated. *
  49. * *
  50. * INPUT: decrypt -- Should decompression be performed? *
  51. * *
  52. * blocksize-- The size of the data blocks to process. *
  53. * *
  54. * OUTPUT: none *
  55. * *
  56. * WARNINGS: none *
  57. * *
  58. * HISTORY: *
  59. * 07/04/1996 JLB : Created. *
  60. *=============================================================================================*/
  61. LCWPipe::LCWPipe(CompControl control, int blocksize) :
  62. Control(control),
  63. Counter(0),
  64. Buffer(NULL),
  65. Buffer2(NULL),
  66. BlockSize(blocksize)
  67. {
  68. SafetyMargin = BlockSize/128+1;
  69. Buffer = new char[BlockSize+SafetyMargin];
  70. Buffer2 = new char[BlockSize+SafetyMargin];
  71. BlockHeader.CompCount = 0xFFFF;
  72. }
  73. /***********************************************************************************************
  74. * LCWPipe::~LCWPipe -- Deconstructor for the LCW pipe object. *
  75. * *
  76. * This will free any buffers it may have allocated. *
  77. * *
  78. * INPUT: none *
  79. * *
  80. * OUTPUT: none *
  81. * *
  82. * WARNINGS: none *
  83. * *
  84. * HISTORY: *
  85. * 07/04/1996 JLB : Created. *
  86. *=============================================================================================*/
  87. LCWPipe::~LCWPipe(void)
  88. {
  89. delete [] Buffer;
  90. Buffer = NULL;
  91. delete [] Buffer2;
  92. Buffer2 = NULL;
  93. }
  94. /***********************************************************************************************
  95. * LCWPipe::Put -- Send some data through the LCW processor pipe. *
  96. * *
  97. * This routine will take the data requested and process it (decompression or compression). *
  98. * It does this by accumulating the necessary bytes to make a whole block. Then the block *
  99. * is processed and the entire contents are flushed to the next pipe segment in the chain. *
  100. * *
  101. * INPUT: source -- Pointer to the data to be fed to this LCW processor. *
  102. * *
  103. * length -- The number of bytes received. *
  104. * *
  105. * OUTPUT: Returns with the actual number of bytes output at the far distant final link in *
  106. * the pipe chain. *
  107. * *
  108. * WARNINGS: The compression process may be slow as well as consuming two buffers. *
  109. * *
  110. * HISTORY: *
  111. * 07/04/1996 JLB : Created. *
  112. *=============================================================================================*/
  113. int LCWPipe::Put(void const * source, int slen)
  114. {
  115. if (source == NULL || slen < 1) {
  116. return(Pipe::Put(source, slen));
  117. }
  118. assert(Buffer != NULL);
  119. int total = 0;
  120. /*
  121. ** Copy as much as can fit into the buffer from the source data supplied.
  122. */
  123. if (Control == DECOMPRESS) {
  124. while (slen > 0) {
  125. /*
  126. ** First check to see if we are in the block header accumulation phase.
  127. ** When a whole block header has been accumulated, only then will the regular
  128. ** data processing begin for the block.
  129. */
  130. if (BlockHeader.CompCount == 0xFFFF) {
  131. int len = (slen < (sizeof(BlockHeader)-Counter)) ? slen : (sizeof(BlockHeader)-Counter);
  132. memmove(&Buffer[Counter], source, len);
  133. source = ((char *)source) + len;
  134. slen -= len;
  135. Counter += len;
  136. /*
  137. ** A whole block header has been accumulated. Store it for safekeeping.
  138. */
  139. if (Counter == sizeof(BlockHeader)) {
  140. memmove(&BlockHeader, Buffer, sizeof(BlockHeader));
  141. Counter = 0;
  142. }
  143. }
  144. /*
  145. ** Fill the buffer with compressed data until there is enough to make a whole
  146. ** data block.
  147. */
  148. if (slen > 0) {
  149. int len = (slen < (BlockHeader.CompCount-Counter)) ? slen : (BlockHeader.CompCount-Counter);
  150. memmove(&Buffer[Counter], source, len);
  151. slen -= len;
  152. source = ((char *)source) + len;
  153. Counter += len;
  154. /*
  155. ** If an entire block has been accumulated, then uncompress it and feed it
  156. ** through the pipe.
  157. */
  158. if (Counter == BlockHeader.CompCount) {
  159. LCW_Uncomp(Buffer, Buffer2);
  160. total += Pipe::Put(Buffer2, BlockHeader.UncompCount);
  161. Counter = 0;
  162. BlockHeader.CompCount = 0xFFFF;
  163. }
  164. }
  165. }
  166. } else {
  167. /*
  168. ** If the buffer already contains some data, then any new data must be stored
  169. ** into the staging buffer until a full set has been accumulated.
  170. */
  171. if (Counter > 0) {
  172. int tocopy = (slen < (BlockSize-Counter)) ? slen : (BlockSize-Counter);
  173. memmove(&Buffer[Counter], source, tocopy);
  174. source = ((char *)source) + tocopy;
  175. slen -= tocopy;
  176. Counter += tocopy;
  177. if (Counter == BlockSize) {
  178. int len = LCW_Comp(Buffer, Buffer2, BlockSize);
  179. BlockHeader.CompCount = (unsigned short)len;
  180. BlockHeader.UncompCount = (unsigned short)BlockSize;
  181. total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
  182. total += Pipe::Put(Buffer2, len);
  183. Counter = 0;
  184. }
  185. }
  186. /*
  187. ** Process the source data in whole block chunks until there is insufficient
  188. ** source data left for a whole data block.
  189. */
  190. while (slen >= BlockSize) {
  191. int len = LCW_Comp(source, Buffer2, BlockSize);
  192. source = ((char *)source) + BlockSize;
  193. slen -= BlockSize;
  194. BlockHeader.CompCount = (unsigned short)len;
  195. BlockHeader.UncompCount = (unsigned short)BlockSize;
  196. total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
  197. total += Pipe::Put(Buffer2, len);
  198. }
  199. /*
  200. ** If there is any remaining data, then it is stored into the buffer
  201. ** until a full data block has been accumulated.
  202. */
  203. if (slen > 0) {
  204. memmove(Buffer, source, slen);
  205. Counter = slen;
  206. }
  207. }
  208. return(total);
  209. }
  210. /***********************************************************************************************
  211. * LCWPipe::Flush -- Flushes any partially accumulated block. *
  212. * *
  213. * This routine is called when any buffered data must be flushed out the pipe. For the *
  214. * compression process, this will generate the sub-sized compressed block. For *
  215. * decompression, this routine should not have any data in the buffer. In such a case, it *
  216. * means that the data source was prematurely truncated. In such a case, just dump the *
  217. * accumulated data through the pipe. *
  218. * *
  219. * INPUT: none *
  220. * *
  221. * OUTPUT: Returns with the actual number of data bytes output to the distant final link in *
  222. * the pipe chain. *
  223. * *
  224. * WARNINGS: none *
  225. * *
  226. * HISTORY: *
  227. * 07/04/1996 JLB : Created. *
  228. *=============================================================================================*/
  229. int LCWPipe::Flush(void)
  230. {
  231. assert(Buffer != NULL);
  232. int total = 0;
  233. /*
  234. ** If there is accumulated data, then it must processed.
  235. */
  236. if (Counter > 0) {
  237. if (Control == DECOMPRESS) {
  238. /*
  239. ** If the accumulated data is insufficient to make a block header, then
  240. ** this means the data has been truncated. Just dump the data through
  241. ** as if were already decompressed.
  242. */
  243. if (BlockHeader.CompCount == 0xFFFF) {
  244. total += Pipe::Put(Buffer, Counter);
  245. Counter = 0;
  246. }
  247. /*
  248. ** There appears to be a partial block accumulated in the buffer. It would
  249. ** be disastrous to try to decompress the data since there wouldn't be
  250. ** the special end of data code that LCW decompression needs. In this
  251. ** case, dump the data out as if it were already decompressed.
  252. */
  253. if (Counter > 0) {
  254. total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
  255. total += Pipe::Put(Buffer, Counter);
  256. Counter = 0;
  257. BlockHeader.CompCount = 0xFFFF;
  258. }
  259. } else {
  260. /*
  261. ** A partial block in the compression process is a normal occurrence. Just
  262. ** compress the partial block and output normally.
  263. */
  264. int len = LCW_Comp(Buffer, Buffer2, Counter);
  265. BlockHeader.CompCount = (unsigned short)len;
  266. BlockHeader.UncompCount = (unsigned short)Counter;
  267. total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
  268. total += Pipe::Put(Buffer2, len);
  269. Counter = 0;
  270. }
  271. }
  272. total += Pipe::Flush();
  273. return(total);
  274. }