Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
lzostraw.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/LZOSTRAW.CPP $*
26 * *
27 * $Author:: Greg_h $*
28 * *
29 * $Modtime:: 7/22/97 11:37a $*
30 * *
31 * $Revision:: 1 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * LZOStraw::Get -- Fetch data through the LZO processor. *
36 * LZOStraw::LZOStraw -- Constructor for LZO straw object. *
37 * LZOStraw::~LZOStraw -- Destructor for the LZO straw. *
38 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
39
40#include "always.h"
41#include "lzo.h"
42#include "lzostraw.h"
43#include <assert.h>
44#include <string.h>
45
46
47/***********************************************************************************************
48 * LZOStraw::LZOStraw -- Constructor for LZO straw object. *
49 * *
50 * This will initialize the LZO straw object. Whether the object is to compress or *
51 * decompress and the block size to use is specified. The data is compressed in blocks *
52 * that are sized to be quick to compress and yet still yield good compression ratios. *
53 * *
54 * INPUT: decrypt -- Should the data be decompressed? *
55 * *
56 * blocksize-- The size of the blocks to process. *
57 * *
58 * OUTPUT: none *
59 * *
60 * WARNINGS: It takes two buffers of the blocksize specified if compression is to be *
61 * performed. *
62 * *
63 * HISTORY: *
64 * 07/04/1996 JLB : Created. *
65 *=============================================================================================*/
66LZOStraw::LZOStraw(CompControl control, int blocksize) :
67 Control(control),
68 Counter(0),
69 Buffer(NULL),
70 Buffer2(NULL),
71 BlockSize(blocksize)
72{
73 SafetyMargin = BlockSize;
74 Buffer = new char[BlockSize+SafetyMargin];
75 if (control == COMPRESS) {
76 Buffer2 = new char[BlockSize+SafetyMargin];
77 }
78}
79
80
81/***********************************************************************************************
82 * LZOStraw::~LZOStraw -- Destructor for the LZO straw. *
83 * *
84 * The destructor will free up the allocated buffers that it allocated in the constructor. *
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 * LZOStraw::Get -- Fetch data through the LZO processor. *
107 * *
108 * This routine will fetch the data bytes specified. It does this by first accumulating *
109 * a full block of data and then compressing or decompressing it as indicated. Subsequent *
110 * requests for data will draw from this buffer of processed data until it is exhausted *
111 * and another block must be fetched. *
112 * *
113 * INPUT: destbuf -- Pointer to the buffer to hold the data requested. *
114 * *
115 * length -- The number of data bytes requested. *
116 * *
117 * OUTPUT: Returns with the actual number of bytes stored into the buffer. If this number *
118 * is less than that requested, then this indicates that the data source has been *
119 * exhausted. *
120 * *
121 * WARNINGS: none *
122 * *
123 * HISTORY: *
124 * 07/04/1996 JLB : Created. *
125 *=============================================================================================*/
126int LZOStraw::Get(void * destbuf, int slen)
127{
128 assert(Buffer != NULL);
129
130 int total = 0;
131
132 /*
133 ** Verify parameters for legality.
134 */
135 if (destbuf == NULL || slen < 1) {
136 return(0);
137 }
138
139 while (slen > 0) {
140
141 /*
142 ** Copy as much data is requested and available into the desired
143 ** destination buffer.
144 */
145 if (Counter) {
146 int len = (slen < Counter) ? slen : Counter;
147 if (Control == DECOMPRESS) {
148 memmove(destbuf, &Buffer[BlockHeader.UncompCount-Counter], len);
149 } else {
150 memmove(destbuf, &Buffer2[(BlockHeader.CompCount+sizeof(BlockHeader))-Counter], len);
151 }
152 destbuf = ((char *)destbuf) + len;
153 slen -= len;
154 Counter -= len;
155 total += len;
156 }
157 if (slen == 0) break;
158
159 if (Control == DECOMPRESS) {
160 int incount = Straw::Get(&BlockHeader, sizeof(BlockHeader));
161 if (incount != sizeof(BlockHeader)) break;
162
163 char *staging_buffer = new char [BlockHeader.CompCount];
164 incount = Straw::Get(staging_buffer, BlockHeader.CompCount);
165 if (incount != BlockHeader.CompCount) break;
166 unsigned int length = sizeof(Buffer);
167 lzo1x_decompress ((unsigned char*)staging_buffer, BlockHeader.CompCount, (unsigned char*)Buffer, &length, NULL);
168 delete [] staging_buffer;
169 Counter = BlockHeader.UncompCount;
170 } else {
171 BlockHeader.UncompCount = (unsigned short)Straw::Get(Buffer, BlockSize);
172 if (BlockHeader.UncompCount == 0) break;
173 char *dictionary = new char [64*1024];
174 unsigned int length = sizeof (Buffer2) - sizeof (BlockHeader);
175 lzo1x_1_compress ((unsigned char*)Buffer, BlockHeader.UncompCount, (unsigned char*)(&Buffer2[sizeof(BlockHeader)]), &length, dictionary);
176 BlockHeader.CompCount = (unsigned short)length;
177 delete [] dictionary;
178 memmove(Buffer2, &BlockHeader, sizeof(BlockHeader));
179 Counter = BlockHeader.CompCount+sizeof(BlockHeader);
180 }
181 }
182
183 return(total);
184}
#define NULL
Definition BaseType.h:92
virtual ~LZOStraw(void)
Definition lzostraw.cpp:95
LZOStraw(CompControl control, int blocksize=1024 *8)
Definition lzostraw.cpp:66
@ COMPRESS
Definition lzostraw.h:53
@ DECOMPRESS
Definition lzostraw.h:54
virtual int Get(void *source, int slen)
Definition lzostraw.cpp:126
virtual int Get(void *buffer, int slen)
Definition straw.cpp:132
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