LZWOTRAW.CPP 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/LZWOTRAW.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 : LZWSTRAW.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 07/02/96 *
  26. * *
  27. * Last Update : July 4, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * LZWStraw::Get -- Fetch data through the LZW processor. *
  32. * LZWStraw::LZWStraw -- Constructor for LZW straw object. *
  33. * LZWStraw::~LZWStraw -- Destructor for the LZW straw. *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "lzostraw.h"
  36. #include "lzwstraw.h"
  37. #include "lzo.h"
  38. #include <string.h>
  39. #include <assert.h>
  40. /***********************************************************************************************
  41. * LZWStraw::LZWStraw -- Constructor for LZW straw object. *
  42. * *
  43. * This will initialize the LZW straw object. Whether the object is to compress or *
  44. * decompress and the block size to use is specified. The data is compressed in blocks *
  45. * that are sized to be quick to compress and yet still yield good compression ratios. *
  46. * *
  47. * INPUT: decrypt -- Should the data be decompressed? *
  48. * *
  49. * blocksize-- The size of the blocks to process. *
  50. * *
  51. * OUTPUT: none *
  52. * *
  53. * WARNINGS: It takes two buffers of the blocksize specified if compression is to be *
  54. * performed. *
  55. * *
  56. * HISTORY: *
  57. * 07/04/1996 JLB : Created. *
  58. *=============================================================================================*/
  59. LZWStraw::LZWStraw(CompControl control, int blocksize) :
  60. Control(control),
  61. Counter(0),
  62. Buffer(NULL),
  63. Buffer2(NULL),
  64. BlockSize(blocksize)
  65. {
  66. SafetyMargin = BlockSize;
  67. // SafetyMargin = BlockSize/128+1;
  68. Buffer = new char[BlockSize+SafetyMargin];
  69. if (control == COMPRESS) {
  70. Buffer2 = new char[BlockSize+SafetyMargin];
  71. }
  72. }
  73. /***********************************************************************************************
  74. * LZWStraw::~LZWStraw -- Destructor for the LZW straw. *
  75. * *
  76. * The destructor will free up the allocated buffers that it allocated in the constructor. *
  77. * *
  78. * INPUT: none *
  79. * *
  80. * OUTPUT: none *
  81. * *
  82. * WARNINGS: none *
  83. * *
  84. * HISTORY: *
  85. * 07/04/1996 JLB : Created. *
  86. *=============================================================================================*/
  87. LZWStraw::~LZWStraw(void)
  88. {
  89. delete [] Buffer;
  90. Buffer = NULL;
  91. delete [] Buffer2;
  92. Buffer2 = NULL;
  93. }
  94. /***********************************************************************************************
  95. * LZWStraw::Get -- Fetch data through the LZW processor. *
  96. * *
  97. * This routine will fetch the data bytes specified. It does this by first accumulating *
  98. * a full block of data and then compressing or decompressing it as indicated. Subsequent *
  99. * requests for data will draw from this buffer of processed data until it is exhausted *
  100. * and another block must be fetched. *
  101. * *
  102. * INPUT: destbuf -- Pointer to the buffer to hold the data requested. *
  103. * *
  104. * length -- The number of data bytes requested. *
  105. * *
  106. * OUTPUT: Returns with the actual number of bytes stored into the buffer. If this number *
  107. * is less than that requested, then this indicates that the data source has been *
  108. * exhausted. *
  109. * *
  110. * WARNINGS: none *
  111. * *
  112. * HISTORY: *
  113. * 07/04/1996 JLB : Created. *
  114. *=============================================================================================*/
  115. int LZWStraw::Get(void * destbuf, int slen)
  116. {
  117. assert(Buffer != NULL);
  118. int total = 0;
  119. /*
  120. ** Verify parameters for legality.
  121. */
  122. if (destbuf == NULL || slen < 1) {
  123. return(0);
  124. }
  125. while (slen > 0) {
  126. /*
  127. ** Copy as much data is requested and available into the desired
  128. ** destination buffer.
  129. */
  130. if (Counter) {
  131. int len = (slen < Counter) ? slen : Counter;
  132. if (Control == DECOMPRESS) {
  133. memmove(destbuf, &Buffer[BlockHeader.UncompCount-Counter], len);
  134. } else {
  135. memmove(destbuf, &Buffer2[(BlockHeader.CompCount+sizeof(BlockHeader))-Counter], len);
  136. }
  137. destbuf = ((char *)destbuf) + len;
  138. slen -= len;
  139. Counter -= len;
  140. total += len;
  141. }
  142. if (slen == 0) break;
  143. if (Control == DECOMPRESS) {
  144. int incount = Straw::Get(&BlockHeader, sizeof(BlockHeader));
  145. if (incount != sizeof(BlockHeader)) break;
  146. char *stageing_buffer = new char [BlockHeader.CompCount];
  147. incount = Straw::Get(staging_buffer, BlockHeader.CompCount);
  148. if (incount != BlockHeader.CompCount) break;
  149. lz01x_decompress (ptr, BlockHeader.CompCount, Buffer, sizeof(Buffer), NULL);
  150. Counter = BlockHeader.UncompCount;
  151. } else {
  152. BlockHeader.UncompCount = (unsigned short)Straw::Get(Buffer, BlockSize);
  153. if (BlockHeader.UncompCount == 0) break;
  154. int len = sizeof (Buffer2) - sizeof (BlockHeader);
  155. char *dictionary = new char [64*1024];
  156. lzo1x_1_compress (Buffer, BlockHeader.UncompCount, &Buffer2[sizeof(BlockHeader)], &BlockHeader.CompCount, dictionary);
  157. delete [] dictionary;
  158. memmove(Buffer2, &BlockHeader, sizeof(BlockHeader));
  159. Counter = BlockHeader.CompCount+sizeof(BlockHeader);
  160. }
  161. }
  162. return(total);
  163. }