lzostraw.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. ** Command & Conquer Renegade(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. /***********************************************************************************************
  19. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Library/LZOSTRAW.CPP $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 7/22/97 11:37a $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * LZOStraw::Get -- Fetch data through the LZO processor. *
  35. * LZOStraw::LZOStraw -- Constructor for LZO straw object. *
  36. * LZOStraw::~LZOStraw -- Destructor for the LZO straw. *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "always.h"
  39. #include "lzo.h"
  40. #include "lzostraw.h"
  41. #include <assert.h>
  42. #include <string.h>
  43. /***********************************************************************************************
  44. * LZOStraw::LZOStraw -- Constructor for LZO straw object. *
  45. * *
  46. * This will initialize the LZO 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. LZOStraw::LZOStraw(CompControl control, int blocksize) :
  63. Control(control),
  64. Counter(0),
  65. Buffer(NULL),
  66. Buffer2(NULL),
  67. BlockSize(blocksize)
  68. {
  69. SafetyMargin = BlockSize;
  70. Buffer = new char[BlockSize+SafetyMargin];
  71. if (control == COMPRESS) {
  72. Buffer2 = new char[BlockSize+SafetyMargin];
  73. }
  74. }
  75. /***********************************************************************************************
  76. * LZOStraw::~LZOStraw -- Destructor for the LZO straw. *
  77. * *
  78. * The destructor will free up the allocated buffers that it allocated in the constructor. *
  79. * *
  80. * INPUT: none *
  81. * *
  82. * OUTPUT: none *
  83. * *
  84. * WARNINGS: none *
  85. * *
  86. * HISTORY: *
  87. * 07/04/1996 JLB : Created. *
  88. *=============================================================================================*/
  89. LZOStraw::~LZOStraw(void)
  90. {
  91. delete [] Buffer;
  92. Buffer = NULL;
  93. delete [] Buffer2;
  94. Buffer2 = NULL;
  95. }
  96. /***********************************************************************************************
  97. * LZOStraw::Get -- Fetch data through the LZO processor. *
  98. * *
  99. * This routine will fetch the data bytes specified. It does this by first accumulating *
  100. * a full block of data and then compressing or decompressing it as indicated. Subsequent *
  101. * requests for data will draw from this buffer of processed data until it is exhausted *
  102. * and another block must be fetched. *
  103. * *
  104. * INPUT: destbuf -- Pointer to the buffer to hold the data requested. *
  105. * *
  106. * length -- The number of data bytes requested. *
  107. * *
  108. * OUTPUT: Returns with the actual number of bytes stored into the buffer. If this number *
  109. * is less than that requested, then this indicates that the data source has been *
  110. * exhausted. *
  111. * *
  112. * WARNINGS: none *
  113. * *
  114. * HISTORY: *
  115. * 07/04/1996 JLB : Created. *
  116. *=============================================================================================*/
  117. int LZOStraw::Get(void * destbuf, int slen)
  118. {
  119. assert(Buffer != NULL);
  120. int total = 0;
  121. /*
  122. ** Verify parameters for legality.
  123. */
  124. if (destbuf == NULL || slen < 1) {
  125. return(0);
  126. }
  127. while (slen > 0) {
  128. /*
  129. ** Copy as much data is requested and available into the desired
  130. ** destination buffer.
  131. */
  132. if (Counter) {
  133. int len = (slen < Counter) ? slen : Counter;
  134. if (Control == DECOMPRESS) {
  135. memmove(destbuf, &Buffer[BlockHeader.UncompCount-Counter], len);
  136. } else {
  137. memmove(destbuf, &Buffer2[(BlockHeader.CompCount+sizeof(BlockHeader))-Counter], len);
  138. }
  139. destbuf = ((char *)destbuf) + len;
  140. slen -= len;
  141. Counter -= len;
  142. total += len;
  143. }
  144. if (slen == 0) break;
  145. if (Control == DECOMPRESS) {
  146. int incount = Straw::Get(&BlockHeader, sizeof(BlockHeader));
  147. if (incount != sizeof(BlockHeader)) break;
  148. char *staging_buffer = new char [BlockHeader.CompCount];
  149. incount = Straw::Get(staging_buffer, BlockHeader.CompCount);
  150. if (incount != BlockHeader.CompCount) break;
  151. unsigned int length = sizeof(Buffer);
  152. lzo1x_decompress ((unsigned char*)staging_buffer, BlockHeader.CompCount, (unsigned char*)Buffer, &length, NULL);
  153. delete [] staging_buffer;
  154. Counter = BlockHeader.UncompCount;
  155. } else {
  156. BlockHeader.UncompCount = (unsigned short)Straw::Get(Buffer, BlockSize);
  157. if (BlockHeader.UncompCount == 0) break;
  158. char *dictionary = new char [64*1024];
  159. unsigned int length = sizeof (Buffer2) - sizeof (BlockHeader);
  160. lzo1x_1_compress ((unsigned char*)Buffer, BlockHeader.UncompCount, (unsigned char*)(&Buffer2[sizeof(BlockHeader)]), &length, dictionary);
  161. BlockHeader.CompCount = (unsigned short)length;
  162. delete [] dictionary;
  163. memmove(Buffer2, &BlockHeader, sizeof(BlockHeader));
  164. Counter = BlockHeader.CompCount+sizeof(BlockHeader);
  165. }
  166. }
  167. return(total);
  168. }