Coverage Report

Created: 2026-01-17 06:09

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