LZOPIPE.CPP 15 KB

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