Coverage Report

Created: 2026-05-23 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lzo-2.10/src/lzo2a_d.ch
Line
Count
Source
1
/* lzo2a_d.ch -- implementation of the LZO2A decompression 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 "lzo1_d.ch"
30
31
32
/***********************************************************************
33
// decompress a block of data.
34
************************************************************************/
35
36
1.26M
#define _NEEDBYTE   NEED_IP(1)
37
1.26M
#define _NEXTBYTE   (*ip++)
38
39
LZO_PUBLIC(int)
40
DO_DECOMPRESS    ( const lzo_bytep in , lzo_uint  in_len,
41
                         lzo_bytep out, lzo_uintp out_len,
42
                         lzo_voidp wrkmem )
43
922
{
44
922
    lzo_bytep op;
45
922
    const lzo_bytep ip;
46
922
    const lzo_bytep m_pos;
47
48
922
    lzo_uint t;
49
922
    const lzo_bytep const ip_end = in + in_len;
50
#if defined(HAVE_ANY_OP)
51
196
    lzo_bytep const op_end = out + *out_len;
52
#endif
53
54
922
    lzo_uint32_t b = 0;     /* bit buffer */
55
922
    unsigned k = 0;         /* bits in bit buffer */
56
57
922
    LZO_UNUSED(wrkmem);
58
59
922
    op = out;
60
922
    ip = in;
61
62
7.23M
    while (TEST_IP_AND_TEST_OP)
63
7.60M
    {
64
7.60M
        NEEDBITS(1);
65
7.60M
        if (MASKBITS(1) == 0)
66
6.69M
        {
67
6.69M
            DUMPBITS(1);
68
            /* a literal */
69
6.69M
            NEED_IP(1); NEED_OP(1);
70
366k
            *op++ = *ip++;
71
366k
            continue;
72
366k
        }
73
905k
        DUMPBITS(1);
74
75
905k
        NEEDBITS(1);
76
905k
        if (MASKBITS(1) == 0)
77
813k
        {
78
813k
            DUMPBITS(1);
79
            /* a M1 match */
80
813k
            NEEDBITS(2);
81
813k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
813k
            DUMPBITS(2);
83
813k
            NEED_IP(1); NEED_OP(t);
84
2.32k
            m_pos = op - 1 - *ip++;
85
2.32k
            assert(m_pos >= out); assert(m_pos < op);
86
813k
            TEST_LB(m_pos);
87
813k
            MEMCPY_DS(op,m_pos,t);
88
2.31k
            continue;
89
2.32k
        }
90
92.4k
        DUMPBITS(1);
91
92
92.4k
        NEED_IP(2);
93
1.15k
        t = *ip++;
94
1.15k
        m_pos = op;
95
1.15k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
1.15k
        t >>= 5;
97
92.4k
        if (t == 0)
98
29.9k
        {
99
#if (SWD_N >= 8192)
100
            NEEDBITS(1);
101
            t = MASKBITS(1);
102
            DUMPBITS(1);
103
            if (t == 0)
104
                t = 10 - 1;
105
            else
106
            {
107
                /* a M3 match */
108
                m_pos -= 8192;      /* t << 13 */
109
                t = M3_MIN_LEN - 1;
110
            }
111
#else
112
29.9k
            t = 10 - 1;
113
29.9k
#endif
114
29.9k
            NEED_IP(1);
115
1.61M
            while (*ip == 0)
116
1.58M
            {
117
1.58M
                t += 255;
118
1.58M
                ip++;
119
1.58M
                TEST_OV(t);
120
1.58M
                NEED_IP(1);
121
1.57M
            }
122
443
            t += *ip++;
123
443
        }
124
62.5k
        else
125
62.5k
        {
126
62.5k
#if defined(LZO_EOF_CODE)
127
62.5k
            if (m_pos == op)
128
731
                goto eof_found;
129
61.7k
#endif
130
61.7k
            t += 2;
131
61.7k
        }
132
91.7k
        assert(m_pos >= out); assert(m_pos < op);
133
91.7k
        TEST_LB(m_pos);
134
91.6k
        NEED_OP(t);
135
91.6k
        MEMCPY_DS(op,m_pos,t);
136
1.06k
    }
137
138
139
65
#if defined(LZO_EOF_CODE)
140
#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
141
    /* no EOF code was found */
142
65
    *out_len = pd(op, out);
143
65
    return LZO_E_EOF_NOT_FOUND;
144
0
#endif
145
146
731
eof_found:
147
731
    assert(t == 1);
148
731
#endif
149
731
    *out_len = pd(op, out);
150
731
    return (ip == ip_end ? LZO_E_OK :
151
731
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
152
153
154
#if defined(HAVE_NEED_IP)
155
44
input_overrun:
156
44
    *out_len = pd(op, out);
157
44
    return LZO_E_INPUT_OVERRUN;
158
0
#endif
159
160
#if defined(HAVE_NEED_OP)
161
10
output_overrun:
162
10
    *out_len = pd(op, out);
163
10
    return LZO_E_OUTPUT_OVERRUN;
164
0
#endif
165
166
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
167
72
lookbehind_overrun:
168
72
    *out_len = pd(op, out);
169
72
    return LZO_E_LOOKBEHIND_OVERRUN;
170
#endif
171
922
}
lzo2a_decompress
Line
Count
Source
43
726
{
44
726
    lzo_bytep op;
45
726
    const lzo_bytep ip;
46
726
    const lzo_bytep m_pos;
47
48
726
    lzo_uint t;
49
726
    const lzo_bytep const ip_end = in + in_len;
50
#if defined(HAVE_ANY_OP)
51
    lzo_bytep const op_end = out + *out_len;
52
#endif
53
54
726
    lzo_uint32_t b = 0;     /* bit buffer */
55
726
    unsigned k = 0;         /* bits in bit buffer */
56
57
726
    LZO_UNUSED(wrkmem);
58
59
726
    op = out;
60
726
    ip = in;
61
62
7.23M
    while (TEST_IP_AND_TEST_OP)
63
7.23M
    {
64
7.23M
        NEEDBITS(1);
65
7.23M
        if (MASKBITS(1) == 0)
66
6.33M
        {
67
6.33M
            DUMPBITS(1);
68
            /* a literal */
69
6.33M
            NEED_IP(1); NEED_OP(1);
70
6.33M
            *op++ = *ip++;
71
6.33M
            continue;
72
6.33M
        }
73
902k
        DUMPBITS(1);
74
75
902k
        NEEDBITS(1);
76
902k
        if (MASKBITS(1) == 0)
77
810k
        {
78
810k
            DUMPBITS(1);
79
            /* a M1 match */
80
810k
            NEEDBITS(2);
81
810k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
810k
            DUMPBITS(2);
83
810k
            NEED_IP(1); NEED_OP(t);
84
810k
            m_pos = op - 1 - *ip++;
85
810k
            assert(m_pos >= out); assert(m_pos < op);
86
810k
            TEST_LB(m_pos);
87
810k
            MEMCPY_DS(op,m_pos,t);
88
810k
            continue;
89
810k
        }
90
91.2k
        DUMPBITS(1);
91
92
91.2k
        NEED_IP(2);
93
91.2k
        t = *ip++;
94
91.2k
        m_pos = op;
95
91.2k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
91.2k
        t >>= 5;
97
91.2k
        if (t == 0)
98
29.4k
        {
99
#if (SWD_N >= 8192)
100
            NEEDBITS(1);
101
            t = MASKBITS(1);
102
            DUMPBITS(1);
103
            if (t == 0)
104
                t = 10 - 1;
105
            else
106
            {
107
                /* a M3 match */
108
                m_pos -= 8192;      /* t << 13 */
109
                t = M3_MIN_LEN - 1;
110
            }
111
#else
112
29.4k
            t = 10 - 1;
113
29.4k
#endif
114
29.4k
            NEED_IP(1);
115
44.8k
            while (*ip == 0)
116
15.3k
            {
117
15.3k
                t += 255;
118
15.3k
                ip++;
119
15.3k
                TEST_OV(t);
120
15.3k
                NEED_IP(1);
121
15.3k
            }
122
29.4k
            t += *ip++;
123
29.4k
        }
124
61.8k
        else
125
61.8k
        {
126
61.8k
#if defined(LZO_EOF_CODE)
127
61.8k
            if (m_pos == op)
128
726
                goto eof_found;
129
61.0k
#endif
130
61.0k
            t += 2;
131
61.0k
        }
132
91.2k
        assert(m_pos >= out); assert(m_pos < op);
133
90.5k
        TEST_LB(m_pos);
134
90.5k
        NEED_OP(t);
135
90.5k
        MEMCPY_DS(op,m_pos,t);
136
90.5k
    }
137
138
139
0
#if defined(LZO_EOF_CODE)
140
#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
141
    /* no EOF code was found */
142
    *out_len = pd(op, out);
143
    return LZO_E_EOF_NOT_FOUND;
144
#endif
145
146
726
eof_found:
147
726
    assert(t == 1);
148
726
#endif
149
726
    *out_len = pd(op, out);
150
726
    return (ip == ip_end ? LZO_E_OK :
151
726
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
152
153
154
#if defined(HAVE_NEED_IP)
155
input_overrun:
156
    *out_len = pd(op, out);
157
    return LZO_E_INPUT_OVERRUN;
158
#endif
159
160
#if defined(HAVE_NEED_OP)
161
output_overrun:
162
    *out_len = pd(op, out);
163
    return LZO_E_OUTPUT_OVERRUN;
164
#endif
165
166
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
167
lookbehind_overrun:
168
    *out_len = pd(op, out);
169
    return LZO_E_LOOKBEHIND_OVERRUN;
170
#endif
171
726
}
lzo2a_decompress_safe
Line
Count
Source
43
196
{
44
196
    lzo_bytep op;
45
196
    const lzo_bytep ip;
46
196
    const lzo_bytep m_pos;
47
48
196
    lzo_uint t;
49
196
    const lzo_bytep const ip_end = in + in_len;
50
196
#if defined(HAVE_ANY_OP)
51
196
    lzo_bytep const op_end = out + *out_len;
52
196
#endif
53
54
196
    lzo_uint32_t b = 0;     /* bit buffer */
55
196
    unsigned k = 0;         /* bits in bit buffer */
56
57
196
    LZO_UNUSED(wrkmem);
58
59
196
    op = out;
60
196
    ip = in;
61
62
196
    while (TEST_IP_AND_TEST_OP)
63
369k
    {
64
369k
        NEEDBITS(1);
65
369k
        if (MASKBITS(1) == 0)
66
366k
        {
67
366k
            DUMPBITS(1);
68
            /* a literal */
69
366k
            NEED_IP(1); NEED_OP(1);
70
366k
            *op++ = *ip++;
71
366k
            continue;
72
366k
        }
73
3.50k
        DUMPBITS(1);
74
75
3.50k
        NEEDBITS(1);
76
3.50k
        if (MASKBITS(1) == 0)
77
2.33k
        {
78
2.33k
            DUMPBITS(1);
79
            /* a M1 match */
80
2.33k
            NEEDBITS(2);
81
2.33k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
2.33k
            DUMPBITS(2);
83
2.33k
            NEED_IP(1); NEED_OP(t);
84
2.32k
            m_pos = op - 1 - *ip++;
85
2.32k
            assert(m_pos >= out); assert(m_pos < op);
86
2.32k
            TEST_LB(m_pos);
87
2.31k
            MEMCPY_DS(op,m_pos,t);
88
2.31k
            continue;
89
2.32k
        }
90
1.16k
        DUMPBITS(1);
91
92
1.16k
        NEED_IP(2);
93
1.15k
        t = *ip++;
94
1.15k
        m_pos = op;
95
1.15k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
1.15k
        t >>= 5;
97
1.15k
        if (t == 0)
98
462
        {
99
#if (SWD_N >= 8192)
100
            NEEDBITS(1);
101
            t = MASKBITS(1);
102
            DUMPBITS(1);
103
            if (t == 0)
104
                t = 10 - 1;
105
            else
106
            {
107
                /* a M3 match */
108
                m_pos -= 8192;      /* t << 13 */
109
                t = M3_MIN_LEN - 1;
110
            }
111
#else
112
462
            t = 10 - 1;
113
462
#endif
114
462
            NEED_IP(1);
115
1.57M
            while (*ip == 0)
116
1.57M
            {
117
1.57M
                t += 255;
118
1.57M
                ip++;
119
1.57M
                TEST_OV(t);
120
1.57M
                NEED_IP(1);
121
1.57M
            }
122
443
            t += *ip++;
123
443
        }
124
693
        else
125
693
        {
126
693
#if defined(LZO_EOF_CODE)
127
693
            if (m_pos == op)
128
5
                goto eof_found;
129
688
#endif
130
688
            t += 2;
131
688
        }
132
1.15k
        assert(m_pos >= out); assert(m_pos < op);
133
1.13k
        TEST_LB(m_pos);
134
1.07k
        NEED_OP(t);
135
1.06k
        MEMCPY_DS(op,m_pos,t);
136
1.06k
    }
137
138
139
65
#if defined(LZO_EOF_CODE)
140
65
#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
141
    /* no EOF code was found */
142
65
    *out_len = pd(op, out);
143
65
    return LZO_E_EOF_NOT_FOUND;
144
0
#endif
145
146
5
eof_found:
147
5
    assert(t == 1);
148
5
#endif
149
5
    *out_len = pd(op, out);
150
5
    return (ip == ip_end ? LZO_E_OK :
151
5
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
152
153
154
0
#if defined(HAVE_NEED_IP)
155
44
input_overrun:
156
44
    *out_len = pd(op, out);
157
44
    return LZO_E_INPUT_OVERRUN;
158
0
#endif
159
160
0
#if defined(HAVE_NEED_OP)
161
10
output_overrun:
162
10
    *out_len = pd(op, out);
163
10
    return LZO_E_OUTPUT_OVERRUN;
164
0
#endif
165
166
0
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
167
72
lookbehind_overrun:
168
72
    *out_len = pd(op, out);
169
72
    return LZO_E_LOOKBEHIND_OVERRUN;
170
196
#endif
171
196
}
172
173
174
/* vim:set ts=4 sw=4 et: */