LZWSTRAW.CPP 9.8 KB

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