LZWPIPE.CPP 14 KB

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