LZWPIPE.CPP 14 KB

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