Coverage Report

Created: 2025-07-23 07:05

/src/lzo-2.10/src/lzo1f_9x.c
Line
Count
Source (jump to first uncovered line)
1
/* lzo1f_9x.c -- implementation of the LZO1F-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 "config1f.h"
30
31
32
/***********************************************************************
33
//
34
************************************************************************/
35
36
690
#define SWD_N           16383           /* size of ring buffer */
37
4.77M
#define SWD_THRESHOLD       2           /* lower limit for match length */
38
690
#define SWD_F            2048           /* upper limit for match length */
39
40
41
#define LZO1F 1
42
1.38k
#define LZO_COMPRESS_T  lzo1f_999_t
43
690
#define lzo_swd_t       lzo1f_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
144k
{
55
144k
    if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
56
75.7k
    {
57
75.7k
        m_off -= 1;
58
75.7k
        *op++ = LZO_BYTE(((m_len - 2) << 5) | ((m_off & 7) << 2));
59
75.7k
        *op++ = LZO_BYTE(m_off >> 3);
60
75.7k
        c->m2_m++;
61
75.7k
    }
62
68.7k
    else if (m_len == M2_MIN_LEN && m_off <= 2 * M2_MAX_OFFSET &&
63
68.7k
             c->r1_lit > 0)
64
3.94k
    {
65
3.94k
        assert(m_off > M2_MAX_OFFSET);
66
3.94k
        m_off -= 1 + M2_MAX_OFFSET;
67
3.94k
        *op++ = LZO_BYTE(((m_off & 7) << 2));
68
3.94k
        *op++ = LZO_BYTE(m_off >> 3);
69
3.94k
        c->r1_r++;
70
3.94k
    }
71
64.8k
    else
72
64.8k
    {
73
64.8k
        if (m_len <= M3_MAX_LEN)
74
51.2k
            *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
75
13.5k
        else
76
13.5k
        {
77
13.5k
            m_len -= M3_MAX_LEN;
78
13.5k
            *op++ = M3_MARKER | 0;
79
30.9k
            while (m_len > 255)
80
17.3k
            {
81
17.3k
                m_len -= 255;
82
17.3k
                *op++ = 0;
83
17.3k
            }
84
13.5k
            assert(m_len > 0);
85
13.5k
            *op++ = LZO_BYTE(m_len);
86
13.5k
        }
87
64.8k
        *op++ = LZO_BYTE((m_off & 63) << 2);
88
64.8k
        *op++ = LZO_BYTE(m_off >> 6);
89
64.8k
        c->m3_m++;
90
64.8k
    }
91
92
144k
    return op;
93
144k
}
94
95
96
static lzo_bytep
97
STORE_RUN ( lzo_bytep op, const lzo_bytep ii, lzo_uint t, lzo_bytep out )
98
92.5k
{
99
92.5k
    if (t < 4 && op > out)
100
61.3k
        op[-2] = LZO_BYTE(op[-2] | t);
101
31.1k
    else if (t <= 31)
102
23.2k
        *op++ = LZO_BYTE(t);
103
7.92k
    else
104
7.92k
    {
105
7.92k
        lzo_uint tt = t - 31;
106
107
7.92k
        *op++ = 0;
108
20.5k
        while (tt > 255)
109
12.6k
        {
110
12.6k
            tt -= 255;
111
12.6k
            *op++ = 0;
112
12.6k
        }
113
7.92k
        assert(tt > 0);
114
7.92k
        *op++ = LZO_BYTE(tt);
115
7.92k
    }
116
4.48M
    do *op++ = *ii++; while (--t > 0);
117
118
92.5k
    return op;
119
92.5k
}
120
121
122
/***********************************************************************
123
// this is a public function, but there is no prototype in a header file
124
************************************************************************/
125
126
LZO_EXTERN(int)
127
lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
128
                                    lzo_bytep out, lzo_uintp out_len,
129
                                    lzo_voidp wrkmem,
130
                                    lzo_callback_p cb,
131
                                    lzo_uint max_chain );
132
133
LZO_PUBLIC(int)
134
lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
135
                                    lzo_bytep out, lzo_uintp out_len,
136
                                    lzo_voidp wrkmem,
137
                                    lzo_callback_p cb,
138
                                    lzo_uint max_chain )
139
690
{
140
690
    lzo_bytep op;
141
690
    const lzo_bytep ii;
142
690
    lzo_uint lit;
143
690
    lzo_uint m_len, m_off;
144
690
    LZO_COMPRESS_T cc;
145
690
    LZO_COMPRESS_T * const c = &cc;
146
690
    lzo_swd_p const swd = (lzo_swd_p) wrkmem;
147
690
    int r;
148
149
    /* sanity check */
150
690
    LZO_COMPILE_TIME_ASSERT(LZO1F_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
151
152
690
    c->init = 0;
153
690
    c->ip = c->in = in;
154
690
    c->in_end = in + in_len;
155
690
    c->cb = cb;
156
690
    c->r1_r = c->m2_m = c->m3_m = 0;
157
158
690
    op = out;
159
690
    ii = c->ip;             /* point to start of literal run */
160
690
    lit = 0;
161
690
    c->r1_lit = c->r1_m_len = 0;
162
163
690
    r = init_match(c,swd,NULL,0,0);
164
690
    if (r != 0)
165
0
        return r;
166
690
    if (max_chain > 0)
167
0
        swd->max_chain = max_chain;
168
169
690
    r = find_match(c,swd,0,0);
170
690
    if (r != 0)
171
0
        return r;
172
4.63M
    while (c->look > 0)
173
4.63M
    {
174
4.63M
        int lazy_match_min_gain = -1;
175
4.63M
        lzo_uint ahead = 0;
176
177
4.63M
        m_len = c->m_len;
178
4.63M
        m_off = c->m_off;
179
180
4.63M
        assert(c->ip - c->look >= in);
181
4.63M
        if (lit == 0)
182
144k
            ii = c->ip - c->look;
183
4.63M
        assert(ii + lit == c->ip - c->look);
184
4.63M
        assert(swd->b_char == *(c->ip - c->look));
185
186
4.63M
        if ((m_len < M2_MIN_LEN) ||
187
4.63M
            (m_len < M3_MIN_LEN && m_off > M2_MAX_OFFSET))
188
4.45M
        {
189
4.45M
            m_len = 0;
190
4.45M
        }
191
170k
        else
192
170k
        {
193
170k
            assert(c->ip - c->look - m_off >= in);
194
170k
            assert(c->ip - c->look - m_off + m_len < c->ip);
195
170k
            assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
196
170k
                              m_len) == 0);
197
198
170k
            if (lit < 3)
199
126k
                lazy_match_min_gain = 1;
200
44.5k
            else if (lit == 3)
201
10.5k
                lazy_match_min_gain = 3;
202
34.0k
            else if (lit == 31)
203
121
                lazy_match_min_gain = 3;
204
33.9k
            else
205
33.9k
                lazy_match_min_gain = 1;
206
170k
        }
207
208
        /* try a lazy match */
209
4.63M
        if (m_len > 0 && lazy_match_min_gain >= 0 && c->look > m_len)
210
170k
        {
211
170k
            r = find_match(c,swd,1,0);
212
170k
            assert(r == 0); LZO_UNUSED(r);
213
170k
            assert(c->look > 0);
214
215
170k
            if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
216
170k
                c->m_off > M2_MAX_OFFSET)
217
6.22k
            {
218
6.22k
                lazy_match_min_gain += 1;
219
6.22k
            }
220
164k
            else if (c->m_len <= M2_MAX_LEN &&
221
164k
                     c->m_off <= M2_MAX_OFFSET &&
222
164k
                     m_off > M2_MAX_OFFSET)
223
20.5k
            {
224
20.5k
                if (lazy_match_min_gain > 0)
225
20.5k
                    lazy_match_min_gain -= 1;
226
20.5k
            }
227
143k
            else if (m_len == M2_MIN_LEN && c->m_len == M2_MIN_LEN &&
228
143k
                     c->m_off <= 2 * M2_MAX_OFFSET &&
229
143k
                     m_off > M2_MAX_OFFSET)
230
680
            {
231
680
                if (lazy_match_min_gain > 0)
232
680
                    lazy_match_min_gain -= 1;
233
680
            }
234
235
170k
            if (c->m_len >= m_len + lazy_match_min_gain)
236
26.1k
            {
237
26.1k
                c->lazy++;
238
#if !defined(NDEBUG)
239
                m_len = c->m_len;
240
                m_off = c->m_off;
241
                assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
242
                                  m_len) == 0);
243
#endif
244
26.1k
                lit++;
245
26.1k
                assert(ii + lit == c->ip - c->look);
246
26.1k
                continue;
247
26.1k
            }
248
144k
            else
249
144k
            {
250
144k
                ahead = 1;
251
144k
                assert(ii + lit + 1 == c->ip - c->look);
252
144k
            }
253
144k
            assert(m_len > 0);
254
144k
        }
255
4.60M
        assert(ii + lit + ahead == c->ip - c->look);
256
257
258
4.60M
        if (m_len == 0)
259
4.45M
        {
260
            /* a literal */
261
4.45M
            lit++;
262
4.45M
            r = find_match(c,swd,1,0);
263
4.45M
            assert(r == 0); LZO_UNUSED(r);
264
4.45M
        }
265
144k
        else
266
144k
        {
267
            /* 1 - store run */
268
144k
            if (lit > 0)
269
92.1k
            {
270
92.1k
                op = STORE_RUN(op,ii,lit,out);
271
92.1k
                c->r1_m_len = m_len;
272
92.1k
                c->r1_lit = lit;
273
92.1k
                lit = 0;
274
92.1k
            }
275
52.4k
            else
276
52.4k
                c->r1_lit = c->r1_m_len = 0;
277
278
            /* 2 - code match */
279
144k
            op = code_match(c,op,m_len,m_off);
280
144k
            r = find_match(c,swd,m_len,1+ahead);
281
144k
            assert(r == 0); LZO_UNUSED(r);
282
144k
        }
283
284
4.60M
        c->codesize = pd(op, out);
285
4.60M
    }
286
287
288
    /* store final run */
289
690
    if (lit > 0)
290
410
        op = STORE_RUN(op,ii,lit,out);
291
292
690
#if defined(LZO_EOF_CODE)
293
690
    *op++ = M3_MARKER | 1;
294
690
    *op++ = 0;
295
690
    *op++ = 0;
296
690
#endif
297
298
690
    c->codesize = pd(op, out);
299
690
    assert(c->textsize == in_len);
300
301
690
    *out_len = pd(op, out);
302
303
690
    if (c->cb && c->cb->nprogress)
304
0
        (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);
305
306
#if 0
307
    printf("%ld %ld -> %ld: %ld %ld %ld %ld\n",
308
        (long) c->textsize, (long)in_len, (long) c->codesize,
309
        c->r1_r, c->m2_m, c->m3_m, c->lazy);
310
#endif
311
690
    return LZO_E_OK;
312
690
}
313
314
315
316
/***********************************************************************
317
//
318
************************************************************************/
319
320
LZO_PUBLIC(int)
321
lzo1f_999_compress  ( const lzo_bytep in , lzo_uint  in_len,
322
                            lzo_bytep out, lzo_uintp out_len,
323
                            lzo_voidp wrkmem )
324
690
{
325
690
    return lzo1f_999_compress_callback(in,in_len,out,out_len,wrkmem,
326
690
                                       (lzo_callback_p) 0, 0);
327
690
}
328
329
330
/* vim:set ts=4 sw=4 et: */