Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
lzopipe.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(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/***********************************************************************************************
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 * $Archive:: /Commando/Library/LZOPIPE.CPP $*
26 * *
27 * $Author:: Greg_h $*
28 * *
29 * $Modtime:: 7/22/97 11:37a $*
30 * *
31 * $Revision:: 1 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * LZWPipe::Flush -- Flushes any partially accumulated block. *
36 * LZWPipe::LZWPipe -- Constructor for the LZO processor pipe. *
37 * LZWPipe::Put -- Send some data through the LZO processor pipe. *
38 * LZWPipe::~LZWPipe -- Deconstructor for the LZO pipe object. *
39 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
40
41
42#include "always.h"
43#include "buff.h"
44#include "lzo.h"
45#include "lzopipe.h"
46#include <assert.h>
47#include <string.h>
48
49
50/***********************************************************************************************
51 * LZOPipe::LZOPipe -- Constructor for the LZO processor pipe. *
52 * *
53 * This will initialize the LZOPipe object so that it is prepared for compression or *
54 * decompression as indicated. *
55 * *
56 * INPUT: decrypt -- Should decompression be performed? *
57 * *
58 * blocksize-- The size of the data blocks to process. *
59 * *
60 * OUTPUT: none *
61 * *
62 * WARNINGS: none *
63 * *
64 * HISTORY: *
65 * 07/04/1996 JLB : Created. *
66 *=============================================================================================*/
67LZOPipe::LZOPipe(CompControl control, int blocksize) :
68 Control(control),
69 Counter(0),
70 Buffer(NULL),
71 Buffer2(NULL),
72 BlockSize(blocksize)
73{
74 SafetyMargin = BlockSize;
75 Buffer = new char[BlockSize+SafetyMargin];
76 Buffer2 = new char[BlockSize+SafetyMargin];
77 BlockHeader.CompCount = 0xFFFF;
78}
79
80
81/***********************************************************************************************
82 * LZOPipe::~LZOPipe -- Deconstructor for the LZO pipe object. *
83 * *
84 * This will free any buffers it may have allocated. *
85 * *
86 * INPUT: none *
87 * *
88 * OUTPUT: none *
89 * *
90 * WARNINGS: none *
91 * *
92 * HISTORY: *
93 * 07/04/1996 JLB : Created. *
94 *=============================================================================================*/
96{
97 delete [] Buffer;
98 Buffer = NULL;
99
100 delete [] Buffer2;
101 Buffer2 = NULL;
102}
103
104
105/***********************************************************************************************
106 * LZOPipe::Put -- Send some data through the LZO processor pipe. *
107 * *
108 * This routine will take the data requested and process it (decompression or compression). *
109 * It does this by accumulating the necessary bytes to make a whole block. Then the block *
110 * is processed and the entire contents are flushed to the next pipe segment in the chain. *
111 * *
112 * INPUT: source -- Pointer to the data to be fed to this LZO processor. *
113 * *
114 * length -- The number of bytes received. *
115 * *
116 * OUTPUT: Returns with the actual number of bytes output at the far distant final link in *
117 * the pipe chain. *
118 * *
119 * WARNINGS: The compression process may be slow as well as consuming two buffers. *
120 * *
121 * HISTORY: *
122 * 07/04/1996 JLB : Created. *
123 *=============================================================================================*/
124int LZOPipe::Put(void const * source, int slen)
125{
126 if (source == NULL || slen < 1) {
127 return(Pipe::Put(source, slen));
128 }
129
130 assert(Buffer != NULL);
131
132 int total = 0;
133
134 /*
135 ** Copy as much as can fit into the buffer from the source data supplied.
136 */
137 if (Control == DECOMPRESS) {
138
139 while (slen > 0) {
140
141 /*
142 ** First check to see if we are in the block header accumulation phase.
143 ** When a whole block header has been accumulated, only then will the regular
144 ** data processing begin for the block.
145 */
146 if (BlockHeader.CompCount == 0xFFFF) {
147 int len = ((unsigned)slen < (sizeof(BlockHeader)-Counter)) ? slen : (sizeof(BlockHeader)-Counter);
148 memmove(&Buffer[Counter], source, len);
149 source = ((char *)source) + len;
150 slen -= len;
151 Counter += len;
152
153 /*
154 ** A whole block header has been accumulated. Store it for safekeeping.
155 */
156 if (Counter == sizeof(BlockHeader)) {
157 memmove(&BlockHeader, Buffer, sizeof(BlockHeader));
158 Counter = 0;
159 }
160 }
161
162 /*
163 ** Fill the buffer with compressed data until there is enough to make a whole
164 ** data block.
165 */
166 if (slen > 0) {
167 int len = (slen < (BlockHeader.CompCount-Counter)) ? slen : (BlockHeader.CompCount-Counter);
168
169 memmove(&Buffer[Counter], source, len);
170 slen -= len;
171 source = ((char *)source) + len;
172 Counter += len;
173
174 /*
175 ** If an entire block has been accumulated, then uncompress it and feed it
176 ** through the pipe.
177 */
178 if (Counter == BlockHeader.CompCount) {
179 unsigned int length = sizeof (Buffer2);
180 lzo1x_decompress ((unsigned char*)Buffer, BlockHeader.CompCount, (unsigned char*)Buffer2, &length, NULL);
181 total += Pipe::Put(Buffer2, BlockHeader.UncompCount);
182 Counter = 0;
183 BlockHeader.CompCount = 0xFFFF;
184 }
185 }
186 }
187
188 } else {
189
190 /*
191 ** If the buffer already contains some data, then any new data must be stored
192 ** into the staging buffer until a full set has been accumulated.
193 */
194 if (Counter > 0) {
195 int tocopy = (slen < (BlockSize-Counter)) ? slen : (BlockSize-Counter);
196 memmove(&Buffer[Counter], source, tocopy);
197 source = ((char *)source) + tocopy;
198 slen -= tocopy;
199 Counter += tocopy;
200
201 if (Counter == BlockSize) {
202 unsigned int len = sizeof (Buffer2);
203 char *dictionary = new char [64*1024];
204 lzo1x_1_compress ((unsigned char*)Buffer, BlockSize, (unsigned char*)Buffer2, &len, dictionary);
205 delete [] dictionary;
206 BlockHeader.CompCount = (unsigned short)len;
207 BlockHeader.UncompCount = (unsigned short)BlockSize;
208 total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
209 total += Pipe::Put(Buffer2, len);
210 Counter = 0;
211 }
212 }
213
214 /*
215 ** Process the source data in whole block chunks until there is insufficient
216 ** source data left for a whole data block.
217 */
218 while (slen >= BlockSize) {
219 unsigned int len = sizeof (Buffer2);
220 char *dictionary = new char [64*1024];
221 lzo1x_1_compress ((unsigned char*)source, BlockSize, (unsigned char*)Buffer2, &len, dictionary);
222 delete [] dictionary;
223 source = ((char *)source) + BlockSize;
224 slen -= BlockSize;
225
226 BlockHeader.CompCount = (unsigned short)len;
227 BlockHeader.UncompCount = (unsigned short)BlockSize;
228 total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
229 total += Pipe::Put(Buffer2, len);
230 }
231
232 /*
233 ** If there is any remaining data, then it is stored into the buffer
234 ** until a full data block has been accumulated.
235 */
236 if (slen > 0) {
237 memmove(Buffer, source, slen);
238 Counter = slen;
239 }
240 }
241
242 return(total);
243}
244
245
246/***********************************************************************************************
247 * LZOPipe::Flush -- Flushes any partially accumulated block. *
248 * *
249 * This routine is called when any buffered data must be flushed out the pipe. For the *
250 * compression process, this will generate the sub-sized compressed block. For *
251 * decompression, this routine should not have any data in the buffer. In such a case, it *
252 * means that the data source was prematurely truncated. In such a case, just dump the *
253 * accumulated data through the pipe. *
254 * *
255 * INPUT: none *
256 * *
257 * OUTPUT: Returns with the actual number of data bytes output to the distant final link in *
258 * the pipe chain. *
259 * *
260 * WARNINGS: none *
261 * *
262 * HISTORY: *
263 * 07/04/1996 JLB : Created. *
264 *=============================================================================================*/
266{
267 assert(Buffer != NULL);
268
269 int total = 0;
270
271 /*
272 ** If there is accumulated data, then it must processed.
273 */
274 if (Counter > 0) {
275 if (Control == DECOMPRESS) {
276
277 /*
278 ** If the accumulated data is insufficient to make a block header, then
279 ** this means the data has been truncated. Just dump the data through
280 ** as if were already decompressed.
281 */
282 if (BlockHeader.CompCount == 0xFFFF) {
283 total += Pipe::Put(Buffer, Counter);
284 Counter = 0;
285 }
286
287 /*
288 ** There appears to be a partial block accumulated in the buffer. It would
289 ** be disastrous to try to decompress the data since there wouldn't be
290 ** the special end of data code that LZO decompression needs. In this
291 ** case, dump the data out as if it were already decompressed.
292 */
293 if (Counter > 0) {
294 total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
295 total += Pipe::Put(Buffer, Counter);
296 Counter = 0;
297 BlockHeader.CompCount = 0xFFFF;
298 }
299
300 } else {
301
302 /*
303 ** A partial block in the compression process is a normal occurrence. Just
304 ** compress the partial block and output normally.
305 */
306 unsigned int len = sizeof (Buffer2);
307 char *dictionary = new char [64*1024];
308 lzo1x_1_compress ((unsigned char*)Buffer, Counter, (unsigned char *)Buffer2, &len, dictionary);
309 delete [] dictionary;
310 BlockHeader.CompCount = (unsigned short)len;
311 BlockHeader.UncompCount = (unsigned short)Counter;
312 total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
313 total += Pipe::Put(Buffer2, len);
314 Counter = 0;
315 }
316 }
317
318 total += Pipe::Flush();
319 return(total);
320}
321
#define NULL
Definition BaseType.h:92
virtual int Flush(void)
Definition lzopipe.cpp:265
virtual int Put(void const *source, int slen)
Definition lzopipe.cpp:124
virtual ~LZOPipe(void)
Definition lzopipe.cpp:95
CompControl
Definition lzopipe.h:51
@ DECOMPRESS
Definition lzopipe.h:53
LZOPipe(CompControl, int blocksize=1024 *8)
Definition lzopipe.cpp:67
virtual int Flush(void)
Definition pipe.cpp:158
virtual int Put(void const *source, int slen)
Definition pipe.cpp:131
int lzo1x_1_compress(const lzo_byte *in, lzo_uint in_len, lzo_byte *out, lzo_uint *out_len, lzo_voidp wrkmem)
Definition lzo1x_c.cpp:335
int lzo1x_decompress(const lzo_byte *in, lzo_uint in_len, lzo_byte *out, lzo_uint *out_len, lzo_voidp)
Definition lzo1x_d.cpp:82