Coverage Report

Created: 2026-02-14 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lzo-2.10/src/lzo1c_9x.c
Line
Count
Source
1
/* lzo1c_9x.c -- implementation of the LZO1C-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
#include "config1c.h"
30
31
32
/***********************************************************************
33
//
34
************************************************************************/
35
36
641
#define SWD_N           16383           /* size of ring buffer */
37
9.19M
#define SWD_THRESHOLD       2           /* lower limit for match length */
38
641
#define SWD_F            2048           /* upper limit for match length */
39
40
41
#define LZO1C 1
42
1.28k
#define LZO_COMPRESS_T  lzo1c_999_t
43
641
#define lzo_swd_t       lzo1c_999_swd_t
44
#include "lzo_mchw.ch"
45
46
47
48
/***********************************************************************
49
//
50
************************************************************************/
51
52
static lzo_bytep
53
code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
54
99.2k
{
55
99.2k
    if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
56
69.5k
    {
57
69.5k
        assert(m_len >= M2_MIN_LEN);
58
69.5k
        assert(m_off >= M2_MIN_OFFSET);
59
60
69.5k
        m_off -= M2_MIN_OFFSET;
61
        /* code match len + low offset bits */
62
69.5k
        *op++ = LZO_BYTE(((m_len - (M2_MIN_LEN - 2)) << M2O_BITS) |
63
69.5k
                          (m_off & M2O_MASK));
64
        /* code high offset bits */
65
69.5k
        *op++ = LZO_BYTE(m_off >> M2O_BITS);
66
69.5k
        c->m2_m++;
67
69.5k
    }
68
29.6k
    else
69
29.6k
    {
70
29.6k
        assert(m_len >= M3_MIN_LEN);
71
29.6k
        assert(m_off <= M3_MAX_OFFSET);
72
73
29.6k
        m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
74
        /* code match len */
75
29.6k
        if (m_len <= M3_MAX_LEN)
76
19.5k
            *op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
77
10.1k
        else
78
10.1k
        {
79
10.1k
            assert(m_len >= M4_MIN_LEN);
80
            /* code M4 match len flag */
81
10.1k
            *op++ = M4_MARKER;
82
            /* code match len */
83
10.1k
            m_len -= M4_MIN_LEN - 1;
84
23.4k
            while (m_len > 255)
85
13.3k
            {
86
13.3k
                m_len -= 255;
87
13.3k
                *op++ = 0;
88
13.3k
            }
89
10.1k
            assert(m_len > 0);
90
10.1k
            *op++ = LZO_BYTE(m_len);
91
10.1k
        }
92
        /* code low offset bits */
93
29.6k
        *op++ = LZO_BYTE(m_off & M3O_MASK);
94
        /* code high offset bits */
95
29.6k
        *op++ = LZO_BYTE(m_off >> M3O_BITS);
96
97
29.6k
        c->r1_m_len = 0;
98
29.6k
        c->m3 = op;
99
29.6k
        c->m3_m++;
100
29.6k
    }
101
99.2k
    return op;
102
99.2k
}
103
104
105
/***********************************************************************
106
// this is a public function, but there is no prototype in a header file
107
************************************************************************/
108
109
LZO_EXTERN(int)
110
lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
111
                                    lzo_bytep out, lzo_uintp out_len,
112
                                    lzo_voidp wrkmem,
113
                                    lzo_callback_p cb,
114
                                    lzo_uint max_chain );
115
116
LZO_PUBLIC(int)
117
lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
118
                                    lzo_bytep out, lzo_uintp out_len,
119
                                    lzo_voidp wrkmem,
120
                                    lzo_callback_p cb,
121
                                    lzo_uint max_chain )
122
641
{
123
641
    lzo_bytep op;
124
641
    const lzo_bytep ii;
125
641
    lzo_uint lit;
126
641
    lzo_uint m_len, m_off;
127
641
    LZO_COMPRESS_T cc;
128
641
    LZO_COMPRESS_T * const c = &cc;
129
641
    lzo_swd_p const swd = (lzo_swd_p) wrkmem;
130
641
    int r;
131
132
    /* sanity check */
133
641
    LZO_COMPILE_TIME_ASSERT(LZO1C_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
134
135
641
    c->init = 0;
136
641
    c->ip = c->in = in;
137
641
    c->in_end = in + in_len;
138
641
    c->cb = cb;
139
641
    c->r1_r = c->m3_r = c->m2_m = c->m3_m = 0;
140
141
641
    op = out;
142
641
    ii = c->ip;             /* point to start of literal run */
143
641
    lit = 0;
144
641
    c->r1_m_len = 0;
145
641
    c->m3 = out + 1;        /* pointer after last m3/m4 match */
146
147
641
    r = init_match(c,swd,NULL,0,0);
148
641
    if (r != 0)
149
0
        return r;
150
641
    if (max_chain > 0)
151
0
        swd->max_chain = max_chain;
152
153
641
    r = find_match(c,swd,0,0);
154
641
    if (r != 0)
155
0
        return r;
156
9.12M
    while (c->look > 0)
157
9.12M
    {
158
9.12M
        int lazy_match_min_gain = -1;
159
9.12M
        lzo_uint ahead = 0;
160
161
9.12M
        m_len = c->m_len;
162
9.12M
        m_off = c->m_off;
163
164
#if 0
165
        printf("%5ld: %5d len:%3d off:%5d\n", (c->ip-c->look)-in, c->look,
166
                m_len, m_off);
167
#endif
168
169
9.12M
        assert(c->ip - c->look >= in);
170
9.12M
        if (lit == 0)
171
99.6k
            ii = c->ip - c->look;
172
9.12M
        assert(ii + lit == c->ip - c->look);
173
9.12M
        assert(swd->b_char == *(c->ip - c->look));
174
175
9.12M
        if ((m_len < M2_MIN_LEN) ||
176
118k
            (m_len < M3_MIN_LEN && m_off > M2_MAX_OFFSET))
177
9.01M
        {
178
9.01M
            m_len = 0;
179
9.01M
        }
180
114k
        else
181
114k
        {
182
114k
            assert(c->ip - c->look - m_off >= in);
183
114k
            assert(c->ip - c->look - m_off + m_len < c->ip);
184
114k
            assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
185
114k
                              m_len) == 0);
186
187
114k
            if (lit > 0)
188
73.6k
            {
189
                /* we have a current literal run: do not try a lazy match,
190
                   if the literal could be coded into a r1 or m3 match */
191
73.6k
                if (lit == 1 && c->r1_m_len == M2_MIN_LEN)
192
9.01k
                    lazy_match_min_gain = -1;
193
64.6k
                else if (lit == 3 && op == c->m3)
194
2.97k
                    lazy_match_min_gain = -1;
195
61.6k
                else if (lit < 3 && op == c->m3)
196
15.8k
                    lazy_match_min_gain = 0;
197
45.8k
                else
198
45.8k
                    lazy_match_min_gain = 1;
199
200
#if (M2_MIN_LEN == 2)
201
                if (m_len == 2)
202
                {
203
                    /* don't code a match of len 2 if we have to
204
                       code a literal run. Code a literal instead. */
205
                    m_len = 0;
206
                }
207
#endif
208
#if (M2_MIN_LEN == M3_MIN_LEN)
209
                if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET)
210
                {
211
                    /* don't code a M3 match of len 3 if we have to
212
                       code a literal run. Code a literal instead. */
213
                    m_len = 0;
214
                }
215
#endif
216
73.6k
            }
217
41.2k
            else
218
41.2k
            {
219
                /* no current literal run: only try a lazy match,
220
                   if the literal could be coded into a r1 or m3 match */
221
41.2k
                if (c->r1_m_len == M2_MIN_LEN || op == c->m3)
222
21.9k
                    lazy_match_min_gain = 0;
223
19.3k
                else
224
19.3k
                    lazy_match_min_gain = -1;
225
41.2k
            }
226
114k
        }
227
228
229
        /* try a lazy match */
230
9.12M
        if (m_len == 0)
231
9.01M
            lazy_match_min_gain = -1;
232
9.12M
        if (lazy_match_min_gain >= 0 && c->look > m_len)
233
83.3k
        {
234
83.3k
            assert(m_len > 0);
235
236
83.3k
            r = find_match(c,swd,1,0);
237
83.3k
            assert(r == 0); LZO_UNUSED(r);
238
83.3k
            assert(c->look > 0);
239
240
83.3k
            if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
241
53.8k
                c->m_off > M2_MAX_OFFSET)
242
1.06k
                lazy_match_min_gain += 1;
243
244
83.3k
            if (c->m_len >= m_len + lazy_match_min_gain)
245
15.6k
            {
246
15.6k
                c->lazy++;
247
#if !defined(NDEBUG)
248
                m_len = c->m_len;
249
                m_off = c->m_off;
250
                assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
251
                                  m_len) == 0);
252
#endif
253
15.6k
                lit++;
254
15.6k
                assert(ii + lit == c->ip - c->look);
255
15.6k
                continue;
256
15.6k
            }
257
67.7k
            else
258
67.7k
            {
259
67.7k
                ahead = 1;
260
67.7k
                assert(ii + lit + 1 == c->ip - c->look);
261
67.7k
            }
262
83.3k
            assert(m_len > 0);
263
67.7k
        }
264
9.12M
        assert(ii + lit + ahead == c->ip - c->look);
265
266
267
9.11M
        if (m_len == 0)
268
9.01M
        {
269
            /* a literal */
270
9.01M
            lit++;
271
9.01M
            r = find_match(c,swd,1,0);
272
9.01M
            assert(r == 0); LZO_UNUSED(r);
273
9.01M
        }
274
99.2k
        else
275
99.2k
        {
276
            /* 1 - store run */
277
99.2k
            if (lit > 0)
278
64.9k
            {
279
                /* code current literal run */
280
64.9k
                if (lit == 1 && c->r1_m_len == M2_MIN_LEN)
281
9.01k
                {
282
                    /* Code a context sensitive R1 match. */
283
9.01k
                    assert((op[-2] >> M2O_BITS) == (M2_MARKER >> M2O_BITS));
284
9.01k
                    op[-2] &= M2O_MASK;
285
9.01k
                    assert((op[-2] >> M2O_BITS) == 0);
286
                    /* copy 1 literal */
287
9.01k
                    *op++ = *ii++;
288
9.01k
                    assert(ii + ahead == c->ip - c->look);
289
9.01k
                    c->r1_r++;
290
9.01k
                }
291
55.9k
                else if (lit < 4 && op == c->m3)
292
14.1k
                {
293
14.1k
                    assert((c->m3[-2] >> M3O_BITS) == 0);
294
14.1k
                    c->m3[-2] = LZO_BYTE(c->m3[-2] | (lit << M3O_BITS));
295
14.1k
                    MEMCPY_DS(op, ii, lit);
296
14.1k
                    assert(ii + ahead == c->ip - c->look);
297
14.1k
                    c->m3_r++;
298
14.1k
                }
299
41.7k
                else
300
41.7k
                {
301
41.7k
                    op = STORE_RUN(op,ii,lit);
302
41.7k
                }
303
64.9k
                if (lit < R0FAST)
304
62.7k
                    c->r1_m_len = m_len;
305
2.22k
                else
306
2.22k
                    c->r1_m_len = 0;
307
64.9k
                lit = 0;
308
64.9k
            }
309
34.3k
            else
310
34.3k
                c->r1_m_len = 0;
311
312
            /* 2 - code match */
313
99.2k
            op = code_match(c,op,m_len,m_off);
314
99.2k
            r = find_match(c,swd,m_len,1+ahead);
315
99.2k
            assert(r == 0); LZO_UNUSED(r);
316
99.2k
        }
317
318
9.11M
        c->codesize = pd(op, out);
319
9.11M
    }
320
321
322
    /* store final run */
323
641
    if (lit > 0)
324
372
        op = STORE_RUN(op,ii,lit);
325
326
641
#if defined(LZO_EOF_CODE)
327
641
    *op++ = M3_MARKER | 1;
328
641
    *op++ = 0;
329
641
    *op++ = 0;
330
641
#endif
331
332
641
    c->codesize = pd(op, out);
333
641
    assert(c->textsize == in_len);
334
335
641
    *out_len = pd(op, out);
336
337
641
    if (c->cb && c->cb->nprogress)
338
0
        (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);
339
340
#if 0
341
    printf("%ld %ld -> %ld: %ld %ld %ld %ld %ld\n",
342
        (long) c->textsize, (long)in_len, (long) c->codesize,
343
        c->r1_r, c->m3_r, c->m2_m, c->m3_m, c->lazy);
344
#endif
345
641
    return LZO_E_OK;
346
641
}
347
348
349
350
/***********************************************************************
351
//
352
************************************************************************/
353
354
LZO_PUBLIC(int)
355
lzo1c_999_compress  ( const lzo_bytep in , lzo_uint  in_len,
356
                            lzo_bytep out, lzo_uintp out_len,
357
                            lzo_voidp wrkmem )
358
641
{
359
641
    return lzo1c_999_compress_callback(in,in_len,out,out_len,wrkmem,
360
641
                                       (lzo_callback_p) 0, 0);
361
641
}
362
363
364
/* vim:set ts=4 sw=4 et: */