Coverage Report

Created: 2025-11-16 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lzo-2.10/src/lzo2a_9x.c
Line
Count
Source
1
/* lzo2a_9x.c -- implementation of the LZO2A-999 compression algorithm
2
3
   This file is part of the LZO real-time data compression library.
4
5
   Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
6
   All Rights Reserved.
7
8
   The LZO library is free software; you can redistribute it and/or
9
   modify it under the terms of the GNU General Public License as
10
   published by the Free Software Foundation; either version 2 of
11
   the License, or (at your option) any later version.
12
13
   The LZO library is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with the LZO library; see the file COPYING.
20
   If not, write to the Free Software Foundation, Inc.,
21
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23
   Markus F.X.J. Oberhumer
24
   <markus@oberhumer.com>
25
   http://www.oberhumer.com/opensource/lzo/
26
 */
27
28
29
30
#include "config2a.h"
31
32
33
/***********************************************************************
34
//
35
************************************************************************/
36
37
3.47M
#define SWD_THRESHOLD       1           /* lower limit for match length */
38
646
#define SWD_F            2048           /* upper limit for match length */
39
40
41
#define LZO2A 1
42
1.29k
#define LZO_COMPRESS_T  lzo2a_999_t
43
646
#define lzo_swd_t       lzo2a_999_swd_t
44
#include "lzo_mchw.ch"
45
46
47
#if (LZO_CC_BORLANDC && LZO_MM_FLAT)
48
#  if ((__BORLANDC__) >= 0x0450 && (__BORLANDC__) < 0x0460)
49
     /* avoid internal compiler error */
50
#    pragma option -Od
51
#  endif
52
#endif
53
54
55
/***********************************************************************
56
//
57
************************************************************************/
58
59
3.42M
#define putbyte(x)      *op++ = LZO_BYTE(x)
60
61
#define putbits(j,x) \
62
3.56M
    if (k == 0) bitp = op++; \
63
3.56M
    SETBITS(j,x); \
64
3.56M
    if (k >= 8) { *bitp = LZO_BYTE(MASKBITS(8)); DUMPBITS(8); \
65
456k
                    if (k > 0) bitp = op++; }
66
67
3.47M
#define putbit(x)       putbits(1,x)
68
69
70
/***********************************************************************
71
// this is a public function, but there is no prototype in a header file
72
************************************************************************/
73
74
LZO_EXTERN(int)
75
lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
76
                                    lzo_bytep out, lzo_uintp out_len,
77
                                    lzo_voidp wrkmem,
78
                                    lzo_callback_p cb,
79
                                    lzo_uint max_chain );
80
81
LZO_PUBLIC(int)
82
lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
83
                                    lzo_bytep out, lzo_uintp out_len,
84
                                    lzo_voidp wrkmem,
85
                                    lzo_callback_p cb,
86
                                    lzo_uint max_chain )
87
646
{
88
646
    lzo_bytep op;
89
646
    lzo_bytep bitp = 0;
90
646
    lzo_uint m_len, m_off;
91
646
    LZO_COMPRESS_T cc;
92
646
    LZO_COMPRESS_T * const c = &cc;
93
646
    lzo_swd_p const swd = (lzo_swd_p) wrkmem;
94
646
    int r;
95
96
646
    lzo_uint32_t b = 0;     /* bit buffer */
97
646
    unsigned k = 0;         /* bits in bit buffer */
98
99
    /* sanity check */
100
646
    LZO_COMPILE_TIME_ASSERT(LZO2A_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
101
102
646
    c->init = 0;
103
646
    c->ip = c->in = in;
104
646
    c->in_end = in + in_len;
105
646
    c->cb = cb;
106
646
    c->m1 = c->m2 = c->m3 = c->m4 = 0;
107
108
646
    op = out;
109
110
646
    r = init_match(c,swd,NULL,0,0);
111
646
    if (r != 0)
112
0
        return r;
113
646
    if (max_chain > 0)
114
0
        swd->max_chain = max_chain;
115
116
646
    r = find_match(c,swd,0,0);
117
646
    if (r != 0)
118
0
        return r;
119
3.32M
    while (c->look > 0)
120
3.32M
    {
121
3.32M
        lzo_uint lazy_match_min_gain = 0;
122
#if (SWD_N >= 8192)
123
        lzo_uint extra1 = 0;
124
#endif
125
3.32M
        lzo_uint extra2 = 0;
126
3.32M
        lzo_uint ahead = 0;
127
128
3.32M
        m_len = c->m_len;
129
3.32M
        m_off = c->m_off;
130
131
#if (SWD_N >= 8192)
132
        if (m_off >= 8192)
133
        {
134
            if (m_len < M3_MIN_LEN)
135
                m_len = 0;
136
            else
137
                lazy_match_min_gain = 1;
138
        }
139
        else
140
#endif
141
3.32M
        if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
142
95.4k
        {
143
95.4k
            lazy_match_min_gain = 2;
144
#if (SWD_N >= 8192)
145
            extra1 = 3;
146
#endif
147
95.4k
            extra2 = 2;
148
95.4k
        }
149
3.22M
        else if (m_len >= 10)
150
27.9k
            lazy_match_min_gain = 1;
151
3.20M
        else if (m_len >= 3)
152
44.1k
        {
153
44.1k
            lazy_match_min_gain = 1;
154
#if (SWD_N >= 8192)
155
            extra1 = 1;
156
#endif
157
44.1k
        }
158
3.15M
        else
159
3.15M
            m_len = 0;
160
161
162
        /* try a lazy match */
163
3.32M
        if (lazy_match_min_gain > 0 && c->look > m_len)
164
167k
        {
165
167k
            unsigned char lit = LZO_BYTE(swd->b_char);
166
167
167k
            r = find_match(c,swd,1,0);
168
167k
            assert(r == 0); LZO_UNUSED(r);
169
167k
            assert(c->look > 0);
170
171
#if (SWD_N >= 8192)
172
            if (m_off < 8192 && c->m_off >= 8192)
173
                lazy_match_min_gain += extra1;
174
            else
175
#endif
176
167k
            if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
177
95.2k
            {
178
95.2k
                if (!(c->m_len >= M1_MIN_LEN &&
179
40.0k
                      c->m_len <= M1_MAX_LEN && c->m_off <= 256))
180
67.7k
                    lazy_match_min_gain += extra2;
181
95.2k
            }
182
167k
            if (c->m_len >= M1_MIN_LEN &&
183
111k
                c->m_len <= M1_MAX_LEN && c->m_off <= 256)
184
36.5k
            {
185
36.5k
                    lazy_match_min_gain -= 1;
186
36.5k
            }
187
188
167k
            if ((lzo_int) lazy_match_min_gain < 1)
189
9.01k
                lazy_match_min_gain = 1;
190
191
167k
            if (c->m_len >= m_len + lazy_match_min_gain)
192
17.6k
            {
193
17.6k
                c->lazy++;
194
#if !defined(NDEBUG)
195
                m_len = c->m_len;
196
                m_off = c->m_off;
197
                assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
198
                                  m_len) == 0);
199
                assert(m_len >= 3 || (m_len >= 2 && m_off <= 256));
200
#endif
201
                /* code literal */
202
17.6k
                putbit(0);
203
17.6k
                putbyte(lit);
204
17.6k
                c->lit_bytes++;
205
17.6k
                continue;
206
17.6k
            }
207
149k
            else
208
149k
                ahead = 1;
209
167k
            assert(m_len > 0);
210
149k
        }
211
212
213
3.30M
        if (m_len == 0)
214
3.15M
        {
215
            /* a literal */
216
3.15M
            putbit(0);
217
3.15M
            putbyte(swd->b_char);
218
3.15M
            c->lit_bytes++;
219
3.15M
            r = find_match(c,swd,1,0);
220
3.15M
            assert(r == 0); LZO_UNUSED(r);
221
3.15M
        }
222
149k
        else
223
149k
        {
224
149k
            assert(m_len >= M1_MIN_LEN);
225
149k
            assert(m_off > 0);
226
149k
            assert(m_off <= SWD_N);
227
228
            /* 2 - code match */
229
149k
            if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
230
90.2k
            {
231
90.2k
                putbit(1);
232
90.2k
                putbit(0);
233
90.2k
                putbits(2,m_len - M1_MIN_LEN);
234
90.2k
                putbyte(m_off - 1);
235
90.2k
                c->m1++;
236
90.2k
            }
237
#if (SWD_N >= 8192)
238
            else if (m_off >= 8192)
239
            {
240
                unsigned len = m_len;
241
                assert(m_len >= M3_MIN_LEN);
242
                putbit(1);
243
                putbit(1);
244
                putbyte(m_off & 31);
245
                putbyte(m_off >> 5);
246
                putbit(1);
247
                len -= M3_MIN_LEN - 1;
248
                while (len > 255)
249
                {
250
                    len -= 255;
251
                    putbyte(0);
252
                }
253
                putbyte(len);
254
                c->m4++;
255
            }
256
#endif
257
59.6k
            else
258
59.6k
            {
259
59.6k
                assert(m_len >= 3);
260
261
59.6k
                putbit(1);
262
59.6k
                putbit(1);
263
59.6k
                if (m_len <= 9)
264
35.2k
                {
265
35.2k
                    putbyte(((m_len - 2) << 5) | (m_off & 31));
266
35.2k
                    putbyte(m_off >> 5);
267
35.2k
                    c->m2++;
268
35.2k
                }
269
24.4k
                else
270
24.4k
                {
271
24.4k
                    lzo_uint len = m_len;
272
24.4k
                    putbyte(m_off & 31);
273
24.4k
                    putbyte(m_off >> 5);
274
#if (SWD_N >= 8192)
275
                    putbit(0);
276
#endif
277
24.4k
                    len -= 10 - 1;
278
38.0k
                    while (len > 255)
279
13.6k
                    {
280
13.6k
                        len -= 255;
281
13.6k
                        putbyte(0);
282
13.6k
                    }
283
24.4k
                    putbyte(len);
284
24.4k
                    c->m3++;
285
24.4k
                }
286
59.6k
            }
287
149k
            r = find_match(c,swd,m_len,1+ahead);
288
149k
            assert(r == 0); LZO_UNUSED(r);
289
149k
        }
290
291
3.30M
        c->codesize = pd(op, out);
292
3.30M
    }
293
294
646
#if defined(LZO_EOF_CODE)
295
    /* code EOF code */
296
646
    putbit(1);
297
646
    putbit(1);
298
646
    putbyte(1 << 5);
299
646
    putbyte(0);
300
646
#endif
301
302
    /* flush remaining bits */
303
646
    assert(k < CHAR_BIT);
304
646
    if (k > 0)
305
569
    {
306
569
        assert(b == MASKBITS(k));
307
569
        assert(op - bitp > 1);
308
569
        *bitp = LZO_BYTE(MASKBITS(k));
309
569
        DUMPBITS(k);
310
569
        assert(b == 0);
311
569
        assert(k == 0);
312
569
    }
313
314
646
    assert(c->textsize == in_len);
315
646
    c->codesize = pd(op, out);
316
317
646
    *out_len = pd(op, out);
318
319
646
    if (c->cb && c->cb->nprogress)
320
0
        (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);
321
322
#if 0
323
    printf("%ld -> %ld: %ld %ld %ld %ld %ld %ld\n",
324
        (long) c->textsize, (long) c->codesize,
325
        c->lit_bytes, c->m1, c->m2, c->m3, c->m4, c->lazy);
326
#endif
327
646
    return LZO_E_OK;
328
646
}
329
330
331
332
/***********************************************************************
333
//
334
************************************************************************/
335
336
LZO_PUBLIC(int)
337
lzo2a_999_compress  ( const lzo_bytep in , lzo_uint  in_len,
338
                            lzo_bytep out, lzo_uintp out_len,
339
                            lzo_voidp wrkmem )
340
646
{
341
646
    return lzo2a_999_compress_callback(in,in_len,out,out_len,wrkmem,
342
646
                                       (lzo_callback_p) 0, 0);
343
646
}
344
345
346
/* vim:set ts=4 sw=4 et: */