Coverage Report

Created: 2025-11-11 07:04

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
588
#define SWD_N           16383           /* size of ring buffer */
37
5.70M
#define SWD_THRESHOLD       2           /* lower limit for match length */
38
588
#define SWD_F            2048           /* upper limit for match length */
39
40
41
#define LZO1C 1
42
1.17k
#define LZO_COMPRESS_T  lzo1c_999_t
43
588
#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
152k
{
55
152k
    if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
56
95.4k
    {
57
95.4k
        assert(m_len >= M2_MIN_LEN);
58
95.4k
        assert(m_off >= M2_MIN_OFFSET);
59
60
95.4k
        m_off -= M2_MIN_OFFSET;
61
        /* code match len + low offset bits */
62
95.4k
        *op++ = LZO_BYTE(((m_len - (M2_MIN_LEN - 2)) << M2O_BITS) |
63
95.4k
                          (m_off & M2O_MASK));
64
        /* code high offset bits */
65
95.4k
        *op++ = LZO_BYTE(m_off >> M2O_BITS);
66
95.4k
        c->m2_m++;
67
95.4k
    }
68
56.6k
    else
69
56.6k
    {
70
56.6k
        assert(m_len >= M3_MIN_LEN);
71
56.6k
        assert(m_off <= M3_MAX_OFFSET);
72
73
56.6k
        m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
74
        /* code match len */
75
56.6k
        if (m_len <= M3_MAX_LEN)
76
42.4k
            *op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
77
14.2k
        else
78
14.2k
        {
79
14.2k
            assert(m_len >= M4_MIN_LEN);
80
            /* code M4 match len flag */
81
14.2k
            *op++ = M4_MARKER;
82
            /* code match len */
83
14.2k
            m_len -= M4_MIN_LEN - 1;
84
24.4k
            while (m_len > 255)
85
10.2k
            {
86
10.2k
                m_len -= 255;
87
10.2k
                *op++ = 0;
88
10.2k
            }
89
14.2k
            assert(m_len > 0);
90
14.2k
            *op++ = LZO_BYTE(m_len);
91
14.2k
        }
92
        /* code low offset bits */
93
56.6k
        *op++ = LZO_BYTE(m_off & M3O_MASK);
94
        /* code high offset bits */
95
56.6k
        *op++ = LZO_BYTE(m_off >> M3O_BITS);
96
97
56.6k
        c->r1_m_len = 0;
98
56.6k
        c->m3 = op;
99
56.6k
        c->m3_m++;
100
56.6k
    }
101
152k
    return op;
102
152k
}
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
588
{
123
588
    lzo_bytep op;
124
588
    const lzo_bytep ii;
125
588
    lzo_uint lit;
126
588
    lzo_uint m_len, m_off;
127
588
    LZO_COMPRESS_T cc;
128
588
    LZO_COMPRESS_T * const c = &cc;
129
588
    lzo_swd_p const swd = (lzo_swd_p) wrkmem;
130
588
    int r;
131
132
    /* sanity check */
133
588
    LZO_COMPILE_TIME_ASSERT(LZO1C_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
134
135
588
    c->init = 0;
136
588
    c->ip = c->in = in;
137
588
    c->in_end = in + in_len;
138
588
    c->cb = cb;
139
588
    c->r1_r = c->m3_r = c->m2_m = c->m3_m = 0;
140
141
588
    op = out;
142
588
    ii = c->ip;             /* point to start of literal run */
143
588
    lit = 0;
144
588
    c->r1_m_len = 0;
145
588
    c->m3 = out + 1;        /* pointer after last m3/m4 match */
146
147
588
    r = init_match(c,swd,NULL,0,0);
148
588
    if (r != 0)
149
0
        return r;
150
588
    if (max_chain > 0)
151
0
        swd->max_chain = max_chain;
152
153
588
    r = find_match(c,swd,0,0);
154
588
    if (r != 0)
155
0
        return r;
156
5.60M
    while (c->look > 0)
157
5.60M
    {
158
5.60M
        int lazy_match_min_gain = -1;
159
5.60M
        lzo_uint ahead = 0;
160
161
5.60M
        m_len = c->m_len;
162
5.60M
        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
5.60M
        assert(c->ip - c->look >= in);
170
5.60M
        if (lit == 0)
171
152k
            ii = c->ip - c->look;
172
5.60M
        assert(ii + lit == c->ip - c->look);
173
5.60M
        assert(swd->b_char == *(c->ip - c->look));
174
175
5.60M
        if ((m_len < M2_MIN_LEN) ||
176
180k
            (m_len < M3_MIN_LEN && m_off > M2_MAX_OFFSET))
177
5.42M
        {
178
5.42M
            m_len = 0;
179
5.42M
        }
180
175k
        else
181
175k
        {
182
175k
            assert(c->ip - c->look - m_off >= in);
183
175k
            assert(c->ip - c->look - m_off + m_len < c->ip);
184
175k
            assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
185
175k
                              m_len) == 0);
186
187
175k
            if (lit > 0)
188
97.0k
            {
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
97.0k
                if (lit == 1 && c->r1_m_len == M2_MIN_LEN)
192
6.44k
                    lazy_match_min_gain = -1;
193
90.5k
                else if (lit == 3 && op == c->m3)
194
3.62k
                    lazy_match_min_gain = -1;
195
86.9k
                else if (lit < 3 && op == c->m3)
196
27.0k
                    lazy_match_min_gain = 0;
197
59.9k
                else
198
59.9k
                    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
97.0k
            }
217
78.8k
            else
218
78.8k
            {
219
                /* no current literal run: only try a lazy match,
220
                   if the literal could be coded into a r1 or m3 match */
221
78.8k
                if (c->r1_m_len == M2_MIN_LEN || op == c->m3)
222
40.3k
                    lazy_match_min_gain = 0;
223
38.5k
                else
224
38.5k
                    lazy_match_min_gain = -1;
225
78.8k
            }
226
175k
        }
227
228
229
        /* try a lazy match */
230
5.60M
        if (m_len == 0)
231
5.42M
            lazy_match_min_gain = -1;
232
5.60M
        if (lazy_match_min_gain >= 0 && c->look > m_len)
233
127k
        {
234
127k
            assert(m_len > 0);
235
236
127k
            r = find_match(c,swd,1,0);
237
127k
            assert(r == 0); LZO_UNUSED(r);
238
127k
            assert(c->look > 0);
239
240
127k
            if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
241
76.5k
                c->m_off > M2_MAX_OFFSET)
242
2.18k
                lazy_match_min_gain += 1;
243
244
127k
            if (c->m_len >= m_len + lazy_match_min_gain)
245
23.7k
            {
246
23.7k
                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
23.7k
                lit++;
254
23.7k
                assert(ii + lit == c->ip - c->look);
255
23.7k
                continue;
256
23.7k
            }
257
103k
            else
258
103k
            {
259
103k
                ahead = 1;
260
103k
                assert(ii + lit + 1 == c->ip - c->look);
261
103k
            }
262
127k
            assert(m_len > 0);
263
103k
        }
264
5.60M
        assert(ii + lit + ahead == c->ip - c->look);
265
266
267
5.58M
        if (m_len == 0)
268
5.42M
        {
269
            /* a literal */
270
5.42M
            lit++;
271
5.42M
            r = find_match(c,swd,1,0);
272
5.42M
            assert(r == 0); LZO_UNUSED(r);
273
5.42M
        }
274
152k
        else
275
152k
        {
276
            /* 1 - store run */
277
152k
            if (lit > 0)
278
83.6k
            {
279
                /* code current literal run */
280
83.6k
                if (lit == 1 && c->r1_m_len == M2_MIN_LEN)
281
6.44k
                {
282
                    /* Code a context sensitive R1 match. */
283
6.44k
                    assert((op[-2] >> M2O_BITS) == (M2_MARKER >> M2O_BITS));
284
6.44k
                    op[-2] &= M2O_MASK;
285
6.44k
                    assert((op[-2] >> M2O_BITS) == 0);
286
                    /* copy 1 literal */
287
6.44k
                    *op++ = *ii++;
288
6.44k
                    assert(ii + ahead == c->ip - c->look);
289
6.44k
                    c->r1_r++;
290
6.44k
                }
291
77.1k
                else if (lit < 4 && op == c->m3)
292
24.9k
                {
293
24.9k
                    assert((c->m3[-2] >> M3O_BITS) == 0);
294
24.9k
                    c->m3[-2] = LZO_BYTE(c->m3[-2] | (lit << M3O_BITS));
295
24.9k
                    MEMCPY_DS(op, ii, lit);
296
24.9k
                    assert(ii + ahead == c->ip - c->look);
297
24.9k
                    c->m3_r++;
298
24.9k
                }
299
52.2k
                else
300
52.2k
                {
301
52.2k
                    op = STORE_RUN(op,ii,lit);
302
52.2k
                }
303
83.6k
                if (lit < R0FAST)
304
81.9k
                    c->r1_m_len = m_len;
305
1.69k
                else
306
1.69k
                    c->r1_m_len = 0;
307
83.6k
                lit = 0;
308
83.6k
            }
309
68.5k
            else
310
68.5k
                c->r1_m_len = 0;
311
312
            /* 2 - code match */
313
152k
            op = code_match(c,op,m_len,m_off);
314
152k
            r = find_match(c,swd,m_len,1+ahead);
315
152k
            assert(r == 0); LZO_UNUSED(r);
316
152k
        }
317
318
5.58M
        c->codesize = pd(op, out);
319
5.58M
    }
320
321
322
    /* store final run */
323
588
    if (lit > 0)
324
376
        op = STORE_RUN(op,ii,lit);
325
326
588
#if defined(LZO_EOF_CODE)
327
588
    *op++ = M3_MARKER | 1;
328
588
    *op++ = 0;
329
588
    *op++ = 0;
330
588
#endif
331
332
588
    c->codesize = pd(op, out);
333
588
    assert(c->textsize == in_len);
334
335
588
    *out_len = pd(op, out);
336
337
588
    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
588
    return LZO_E_OK;
346
588
}
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
588
{
359
588
    return lzo1c_999_compress_callback(in,in_len,out,out_len,wrkmem,
360
588
                                       (lzo_callback_p) 0, 0);
361
588
}
362
363
364
/* vim:set ts=4 sw=4 et: */