Coverage Report

Created: 2026-12-01 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lzo-2.10/src/lzo1f_9x.c
Line
Count
Source
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
923
#define SWD_N           16383           /* size of ring buffer */
37
16.1M
#define SWD_THRESHOLD       2           /* lower limit for match length */
38
923
#define SWD_F            2048           /* upper limit for match length */
39
40
41
#define LZO1F 1
42
1.84k
#define LZO_COMPRESS_T  lzo1f_999_t
43
923
#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
116k
{
55
116k
    if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
56
56.9k
    {
57
56.9k
        m_off -= 1;
58
56.9k
        *op++ = LZO_BYTE(((m_len - 2) << 5) | ((m_off & 7) << 2));
59
56.9k
        *op++ = LZO_BYTE(m_off >> 3);
60
56.9k
        c->m2_m++;
61
56.9k
    }
62
59.4k
    else if (m_len == M2_MIN_LEN && m_off <= 2 * M2_MAX_OFFSET &&
63
5.50k
             c->r1_lit > 0)
64
4.79k
    {
65
4.79k
        assert(m_off > M2_MAX_OFFSET);
66
4.79k
        m_off -= 1 + M2_MAX_OFFSET;
67
4.79k
        *op++ = LZO_BYTE(((m_off & 7) << 2));
68
4.79k
        *op++ = LZO_BYTE(m_off >> 3);
69
4.79k
        c->r1_r++;
70
4.79k
    }
71
54.6k
    else
72
54.6k
    {
73
54.6k
        if (m_len <= M3_MAX_LEN)
74
40.5k
            *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
75
14.0k
        else
76
14.0k
        {
77
14.0k
            m_len -= M3_MAX_LEN;
78
14.0k
            *op++ = M3_MARKER | 0;
79
34.9k
            while (m_len > 255)
80
20.8k
            {
81
20.8k
                m_len -= 255;
82
20.8k
                *op++ = 0;
83
20.8k
            }
84
14.0k
            assert(m_len > 0);
85
14.0k
            *op++ = LZO_BYTE(m_len);
86
14.0k
        }
87
54.6k
        *op++ = LZO_BYTE((m_off & 63) << 2);
88
54.6k
        *op++ = LZO_BYTE(m_off >> 6);
89
54.6k
        c->m3_m++;
90
54.6k
    }
91
92
116k
    return op;
93
116k
}
94
95
96
static lzo_bytep
97
STORE_RUN ( lzo_bytep op, const lzo_bytep ii, lzo_uint t, lzo_bytep out )
98
84.1k
{
99
84.1k
    if (t < 4 && op > out)
100
50.5k
        op[-2] = LZO_BYTE(op[-2] | t);
101
33.5k
    else if (t <= 31)
102
23.1k
        *op++ = LZO_BYTE(t);
103
10.4k
    else
104
10.4k
    {
105
10.4k
        lzo_uint tt = t - 31;
106
107
10.4k
        *op++ = 0;
108
66.9k
        while (tt > 255)
109
56.5k
        {
110
56.5k
            tt -= 255;
111
56.5k
            *op++ = 0;
112
56.5k
        }
113
10.4k
        assert(tt > 0);
114
10.4k
        *op++ = LZO_BYTE(tt);
115
10.4k
    }
116
15.9M
    do *op++ = *ii++; while (--t > 0);
117
118
84.1k
    return op;
119
84.1k
}
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
923
{
140
923
    lzo_bytep op;
141
923
    const lzo_bytep ii;
142
923
    lzo_uint lit;
143
923
    lzo_uint m_len, m_off;
144
923
    LZO_COMPRESS_T cc;
145
923
    LZO_COMPRESS_T * const c = &cc;
146
923
    lzo_swd_p const swd = (lzo_swd_p) wrkmem;
147
923
    int r;
148
149
    /* sanity check */
150
923
    LZO_COMPILE_TIME_ASSERT(LZO1F_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
151
152
923
    c->init = 0;
153
923
    c->ip = c->in = in;
154
923
    c->in_end = in + in_len;
155
923
    c->cb = cb;
156
923
    c->r1_r = c->m2_m = c->m3_m = 0;
157
158
923
    op = out;
159
923
    ii = c->ip;             /* point to start of literal run */
160
923
    lit = 0;
161
923
    c->r1_lit = c->r1_m_len = 0;
162
163
923
    r = init_match(c,swd,NULL,0,0);
164
923
    if (r != 0)
165
0
        return r;
166
923
    if (max_chain > 0)
167
0
        swd->max_chain = max_chain;
168
169
923
    r = find_match(c,swd,0,0);
170
923
    if (r != 0)
171
0
        return r;
172
16.0M
    while (c->look > 0)
173
16.0M
    {
174
16.0M
        int lazy_match_min_gain = -1;
175
16.0M
        lzo_uint ahead = 0;
176
177
16.0M
        m_len = c->m_len;
178
16.0M
        m_off = c->m_off;
179
180
16.0M
        assert(c->ip - c->look >= in);
181
16.0M
        if (lit == 0)
182
116k
            ii = c->ip - c->look;
183
16.0M
        assert(ii + lit == c->ip - c->look);
184
16.0M
        assert(swd->b_char == *(c->ip - c->look));
185
186
16.0M
        if ((m_len < M2_MIN_LEN) ||
187
139k
            (m_len < M3_MIN_LEN && m_off > M2_MAX_OFFSET))
188
15.8M
        {
189
15.8M
            m_len = 0;
190
15.8M
        }
191
139k
        else
192
139k
        {
193
139k
            assert(c->ip - c->look - m_off >= in);
194
139k
            assert(c->ip - c->look - m_off + m_len < c->ip);
195
139k
            assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
196
139k
                              m_len) == 0);
197
198
139k
            if (lit < 3)
199
95.2k
                lazy_match_min_gain = 1;
200
44.4k
            else if (lit == 3)
201
7.33k
                lazy_match_min_gain = 3;
202
37.0k
            else if (lit == 31)
203
367
                lazy_match_min_gain = 3;
204
36.7k
            else
205
36.7k
                lazy_match_min_gain = 1;
206
139k
        }
207
208
        /* try a lazy match */
209
16.0M
        if (m_len > 0 && lazy_match_min_gain >= 0 && c->look > m_len)
210
139k
        {
211
139k
            r = find_match(c,swd,1,0);
212
139k
            assert(r == 0); LZO_UNUSED(r);
213
139k
            assert(c->look > 0);
214
215
139k
            if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
216
65.5k
                c->m_off > M2_MAX_OFFSET)
217
6.03k
            {
218
6.03k
                lazy_match_min_gain += 1;
219
6.03k
            }
220
133k
            else if (c->m_len <= M2_MAX_LEN &&
221
93.5k
                     c->m_off <= M2_MAX_OFFSET &&
222
81.4k
                     m_off > M2_MAX_OFFSET)
223
21.6k
            {
224
21.6k
                if (lazy_match_min_gain > 0)
225
21.6k
                    lazy_match_min_gain -= 1;
226
21.6k
            }
227
111k
            else if (m_len == M2_MIN_LEN && c->m_len == M2_MIN_LEN &&
228
7.79k
                     c->m_off <= 2 * M2_MAX_OFFSET &&
229
5.46k
                     m_off > M2_MAX_OFFSET)
230
1.50k
            {
231
1.50k
                if (lazy_match_min_gain > 0)
232
1.50k
                    lazy_match_min_gain -= 1;
233
1.50k
            }
234
235
139k
            if (c->m_len >= m_len + lazy_match_min_gain)
236
23.2k
            {
237
23.2k
                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
23.2k
                lit++;
245
23.2k
                assert(ii + lit == c->ip - c->look);
246
23.2k
                continue;
247
23.2k
            }
248
116k
            else
249
116k
            {
250
116k
                ahead = 1;
251
116k
                assert(ii + lit + 1 == c->ip - c->look);
252
116k
            }
253
139k
            assert(m_len > 0);
254
116k
        }
255
16.0M
        assert(ii + lit + ahead == c->ip - c->look);
256
257
258
16.0M
        if (m_len == 0)
259
15.8M
        {
260
            /* a literal */
261
15.8M
            lit++;
262
15.8M
            r = find_match(c,swd,1,0);
263
15.8M
            assert(r == 0); LZO_UNUSED(r);
264
15.8M
        }
265
116k
        else
266
116k
        {
267
            /* 1 - store run */
268
116k
            if (lit > 0)
269
83.6k
            {
270
83.6k
                op = STORE_RUN(op,ii,lit,out);
271
83.6k
                c->r1_m_len = m_len;
272
83.6k
                c->r1_lit = lit;
273
83.6k
                lit = 0;
274
83.6k
            }
275
32.7k
            else
276
32.7k
                c->r1_lit = c->r1_m_len = 0;
277
278
            /* 2 - code match */
279
116k
            op = code_match(c,op,m_len,m_off);
280
116k
            r = find_match(c,swd,m_len,1+ahead);
281
116k
            assert(r == 0); LZO_UNUSED(r);
282
116k
        }
283
284
16.0M
        c->codesize = pd(op, out);
285
16.0M
    }
286
287
288
    /* store final run */
289
923
    if (lit > 0)
290
527
        op = STORE_RUN(op,ii,lit,out);
291
292
923
#if defined(LZO_EOF_CODE)
293
923
    *op++ = M3_MARKER | 1;
294
923
    *op++ = 0;
295
923
    *op++ = 0;
296
923
#endif
297
298
923
    c->codesize = pd(op, out);
299
923
    assert(c->textsize == in_len);
300
301
923
    *out_len = pd(op, out);
302
303
923
    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
923
    return LZO_E_OK;
312
923
}
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
923
{
325
923
    return lzo1f_999_compress_callback(in,in_len,out,out_len,wrkmem,
326
923
                                       (lzo_callback_p) 0, 0);
327
923
}
328
329
330
/* vim:set ts=4 sw=4 et: */