LCWPIPE.CPP 14 KB

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