Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
lzo1x_c.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/Code/wwlib/lzo1x_c.cpp $*
26 * *
27 * $Author:: Jani_p $*
28 * *
29 * $Modtime:: 6/28/00 10:13a $*
30 * *
31 * $Revision:: 2 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37/* $Header: /Commando/Code/wwlib/lzo1x_c.cpp 2 7/05/00 6:26p Jani_p $ */
38/* lzo1x_c.c -- standalone LZO1X-1 compressor
39
40 This file is part of the LZO real-time data compression library.
41
42 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
43
44 The LZO library is free software; you can redistribute it and/or
45 modify it under the terms of the GNU Library General Public
46 License as published by the Free Software Foundation; either
47 version 2 of the License, or (at your option) any later version.
48
49 The LZO library is distributed in the hope that it will be useful,
50 but WITHOUT ANY WARRANTY; without even the implied warranty of
51 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
52 Library General Public License for more details.
53
54 You should have received a copy of the GNU Library General Public
55 License along with the LZO library; see the file COPYING.LIB.
56 If not, write to the Free Software Foundation, Inc.,
57 675 Mass Ave, Cambridge, MA 02139, USA.
58
59 Markus F.X.J. Oberhumer
60 markus.oberhumer@jk.uni-linz.ac.at
61 */
62
63#include "always.h"
64#include "lzo1x.h"
65#include "lzo_conf.h"
66#include "wwdebug.h"
67#include <assert.h>
68
69#if !defined(LZO1X) && !defined(LZO1Y)
70# define LZO1X
71#endif
72
73
74/***********************************************************************
75//
76************************************************************************/
77
78#define M1_MAX_OFFSET 0x0400
79#if defined(LZO1X)
80#define M2_MAX_OFFSET 0x0800
81#elif defined(LZO1Y)
82#define M2_MAX_OFFSET 0x0400
83#endif
84#define M3_MAX_OFFSET 0x4000
85#define M4_MAX_OFFSET 0xbfff
86
87#define MX_MAX_OFFSET (M1_MAX_OFFSET + M2_MAX_OFFSET)
88
89#define M1_MARKER 0
90#define M2_MARKER 64
91#define M3_MARKER 32
92#define M4_MARKER 16
93
94
95#define _DV2(p,shift1,shift2) \
96 (((( (lzo_uint)(p[2]) << shift1) ^ p[1]) << shift2) ^ p[0])
97#define DVAL_NEXT(dv,p) \
98 dv ^= p[-1]; dv = (((dv) >> 5) ^ ((lzo_uint)(p[2]) << (2*5)))
99#define _DV(p,shift) _DV2(p,shift,shift)
100#define DVAL_FIRST(dv,p) dv = _DV((p),5)
101#define _DINDEX(dv,p) ((40799u * (dv)) >> 5)
102#define DINDEX(dv,p) (((_DINDEX(dv,p)) & 0x3fff) << 0)
103#define UPDATE_D(dict,cycle,dv,p) dict[ DINDEX(dv,p) ] = (p)
104#define UPDATE_I(dict,cycle,index,p) dict[index] = (p)
105
106
107/***********************************************************************
108// compress a block of data.
109************************************************************************/
110static int do_compress(const lzo_byte * in, lzo_uint in_len,
111 lzo_byte *out, lzo_uint *out_len,
112 lzo_voidp wrkmem )
113{
114
115 register const lzo_byte *ip;
116 lzo_uint dv;
117 lzo_byte *op;
118 const lzo_byte * const in_end = in + in_len;
119 const lzo_byte * const ip_end = in + in_len - 9 - 4;
120 const lzo_byte *ii;
121 const lzo_bytepp const dict = (const lzo_bytepp) wrkmem;
122
123 op = out;
124 ip = in;
125 ii = ip;
126
127 DVAL_FIRST(dv,ip); UPDATE_D(dict,cycle,dv,ip); ip++;
128 DVAL_NEXT(dv,ip); UPDATE_D(dict,cycle,dv,ip); ip++;
129 DVAL_NEXT(dv,ip); UPDATE_D(dict,cycle,dv,ip); ip++;
130 DVAL_NEXT(dv,ip); UPDATE_D(dict,cycle,dv,ip); ip++;
131
132 for (;;) {
133 register const lzo_byte *m_pos;
134 lzo_uint m_len;
135 lzo_ptrdiff_t m_off;
136 lzo_uint lit;
137
138 lzo_uint dindex = DINDEX(dv,ip);
139 m_pos = dict[dindex];
140 UPDATE_I(dict,cycle,dindex,ip);
141
142
143 if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET)) {
144 }
145#if defined(LZO_UNALIGNED_OK_2)
146 else
147 if (* (unsigned short *) m_pos != * (unsigned short *) ip)
148#else
149 else
150 if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
151#endif
152 {
153 } else {
154 if (m_pos[2] == ip[2]) {
155 lit = ip - ii;
156 m_pos += 3;
157 if (m_off <= M2_MAX_OFFSET)
158 goto match;
159
160 /* better compression, but slower */
161 if (lit == 3) {
162 assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
163 *op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
164 goto code_match;
165 }
166
167 if (*m_pos == ip[3]) {
168 goto match;
169 }
170 } else {
171 /* still need a better way for finding M1 matches */
172 }
173 }
174
175
176 /* a literal */
177 ++ip;
178 if (ip >= ip_end) {
179 break;
180 }
181 DVAL_NEXT(dv,ip);
182 continue;
183
184
185 /* a match */
186
187match:
188
189 /* store current literal run */
190 if (lit > 0) {
191 register lzo_uint t = lit;
192
193 if (t <= 3) {
194 assert(op - 2 > out);
195 op[-2] |= LZO_BYTE(t);
196 } else {
197 if (t <= 18) {
198 *op++ = LZO_BYTE(t - 3);
199 } else {
200 register lzo_uint tt = t - 18;
201
202 *op++ = 0;
203 while (tt > 255) {
204 tt -= 255;
205 *op++ = 0;
206 }
207 assert(tt > 0);
208 *op++ = LZO_BYTE(tt);
209 }
210 }
211
212 do {
213 *op++ = *ii++;
214 } while (--t > 0);
215 }
216
217
218 /* code the match */
219code_match:
220 assert(ii == ip);
221 ip += 3;
222 if (*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++ ||
223 *m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++)
224 {
225 --ip;
226 m_len = ip - ii;
227 assert(m_len >= 3); assert(m_len <= 8);
228
229 if (m_off <= M2_MAX_OFFSET) {
230 m_off -= 1;
231 *op++ = LZO_BYTE(((m_len - 1) << 5) | ((m_off & 7) << 2));
232 *op++ = LZO_BYTE(m_off >> 3);
233 } else {
234 if (m_off <= M3_MAX_OFFSET) {
235 m_off -= 1;
236 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
237 goto m3_m4_offset;
238 } else {
239 m_off -= 0x4000;
240 assert(m_off > 0); assert(m_off <= 0x7fff);
241 *op++ = LZO_BYTE(M4_MARKER |
242 ((m_off & 0x4000) >> 11) | (m_len - 2));
243 goto m3_m4_offset;
244 }
245 }
246 } else {
247 const lzo_byte *end;
248 end = in_end;
249 while (ip < end && *m_pos == *ip) {
250 m_pos++;
251 ip++;
252 }
253 m_len = (ip - ii);
254 assert(m_len >= 3);
255
256 if (m_off <= M3_MAX_OFFSET) {
257 m_off -= 1;
258 if (m_len <= 33) {
259 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
260 } else {
261 m_len -= 33;
262 *op++ = M3_MARKER | 0;
263 goto m3_m4_len;
264 }
265 } else {
266 m_off -= 0x4000;
267 assert(m_off > 0); assert(m_off <= 0x7fff);
268 if (m_len <= 9) {
269 *op++ = LZO_BYTE(M4_MARKER |
270 ((m_off & 0x4000) >> 11) | (m_len - 2));
271 } else {
272 m_len -= 9;
273 *op++ = LZO_BYTE(M4_MARKER | ((m_off & 0x4000) >> 11));
274m3_m4_len:
275 while (m_len > 255) {
276 m_len -= 255;
277 *op++ = 0;
278 }
279 assert(m_len > 0);
280 *op++ = LZO_BYTE(m_len);
281 }
282 }
283
284m3_m4_offset:
285 *op++ = LZO_BYTE((m_off & 63) << 2);
286 *op++ = LZO_BYTE(m_off >> 6);
287 }
288
289 ii = ip;
290 if (ip >= ip_end) {
291 break;
292 }
293 DVAL_FIRST(dv,ip);
294 }
295
296 /* store final literal run */
297 if (in_end - ii > 0) {
298 register lzo_uint t = in_end - ii;
299
300 if (op == out && t <= 238) {
301 *op++ = LZO_BYTE(17 + t);
302 } else {
303 if (t <= 3) {
304 op[-2] |= LZO_BYTE(t);
305 } else {
306 if (t <= 18) {
307 *op++ = LZO_BYTE(t - 3);
308 } else {
309 register lzo_uint tt = t - 18;
310
311 *op++ = 0;
312 while (tt > 255) {
313 tt -= 255;
314 *op++ = 0;
315 }
316 assert(tt > 0);
317 *op++ = LZO_BYTE(tt);
318 }
319 }
320 }
321 do {
322 *op++ = *ii++;
323 } while (--t > 0);
324 }
325
326 *out_len = op - out;
327 return LZO_E_OK;
328}
329
330
331/***********************************************************************
332// public entry point
333************************************************************************/
334
335int lzo1x_1_compress ( const lzo_byte * in, lzo_uint in_len,
336 lzo_byte * out, lzo_uint *out_len,
337 lzo_voidp wrkmem )
338{
339 lzo_byte *op = out;
340 int r = LZO_E_OK;
341
342 if (in_len <= 0) {
343 *out_len = 0;
344 } else {
345 if (in_len <= 9 + 4) {
346 *op++ = LZO_BYTE(17 + in_len);
347 do *op++ = *in++; while (--in_len > 0);
348 *out_len = op - out;
349 } else {
350 r = do_compress(in,in_len,out,out_len,wrkmem);
351 }
352 }
353
354 if (r == LZO_E_OK) {
355 op = out + *out_len;
356 *op++ = M4_MARKER | 1;
357 *op++ = 0;
358 *op = 0;
359 *out_len += 3;
360 }
361
362 return r;
363}
364
365
366/*
367vi:ts=4
368*/
#define DINDEX(dv, p)
Definition lzo1x_c.cpp:102
#define M2_MAX_OFFSET
Definition lzo1x_c.cpp:80
#define UPDATE_I(dict, cycle, index, p)
Definition lzo1x_c.cpp:104
#define DVAL_FIRST(dv, p)
Definition lzo1x_c.cpp:100
#define M3_MARKER
Definition lzo1x_c.cpp:91
#define DVAL_NEXT(dv, p)
Definition lzo1x_c.cpp:97
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
#define M4_MARKER
Definition lzo1x_c.cpp:92
#define UPDATE_D(dict, cycle, dv, p)
Definition lzo1x_c.cpp:103
#define M4_MAX_OFFSET
Definition lzo1x_c.cpp:85
#define M3_MAX_OFFSET
Definition lzo1x_c.cpp:84
#define LZO_BYTE(x)
Definition lzo_conf.h:218
#define LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, max_offset)
Definition lzo_conf.h:293
ptrdiff_t lzo_ptrdiff_t
Definition lzo_conf.h:115
#define lzo_voidp
Definition lzoconf.h:128
#define lzo_bytepp
Definition lzoconf.h:133
#define LZO_E_OK
Definition lzoconf.h:191
#define lzo_byte
Definition lzoconf.h:127
unsigned int lzo_uint
Definition lzoconf.h:99