LZWSTRAW.CPP 9.9 KB

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