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