Coverage Report

Created: 2026-10-17 06:33

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.00M
#define _NEEDBYTE   NEED_IP(1)
37
1.00M
#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
931
{
44
931
    lzo_bytep op;
45
931
    const lzo_bytep ip;
46
931
    const lzo_bytep m_pos;
47
48
931
    lzo_uint t;
49
931
    const lzo_bytep const ip_end = in + in_len;
50
#if defined(HAVE_ANY_OP)
51
200
    lzo_bytep const op_end = out + *out_len;
52
#endif
53
54
931
    lzo_uint32_t b = 0;     /* bit buffer */
55
931
    unsigned k = 0;         /* bits in bit buffer */
56
57
931
    LZO_UNUSED(wrkmem);
58
59
931
    op = out;
60
931
    ip = in;
61
62
5.60M
    while (TEST_IP_AND_TEST_OP)
63
5.70M
    {
64
5.70M
        NEEDBITS(1);
65
5.70M
        if (MASKBITS(1) == 0)
66
4.89M
        {
67
4.89M
            DUMPBITS(1);
68
            /* a literal */
69
4.89M
            NEED_IP(1); NEED_OP(1);
70
97.0k
            *op++ = *ip++;
71
97.0k
            continue;
72
97.1k
        }
73
808k
        DUMPBITS(1);
74
75
808k
        NEEDBITS(1);
76
808k
        if (MASKBITS(1) == 0)
77
755k
        {
78
755k
            DUMPBITS(1);
79
            /* a M1 match */
80
755k
            NEEDBITS(2);
81
755k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
755k
            DUMPBITS(2);
83
755k
            NEED_IP(1); NEED_OP(t);
84
2.65k
            m_pos = op - 1 - *ip++;
85
2.65k
            assert(m_pos >= out); assert(m_pos < op);
86
755k
            TEST_LB(m_pos);
87
755k
            MEMCPY_DS(op,m_pos,t);
88
2.64k
            continue;
89
2.65k
        }
90
53.7k
        DUMPBITS(1);
91
92
53.7k
        NEED_IP(2);
93
1.52k
        t = *ip++;
94
1.52k
        m_pos = op;
95
1.52k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
1.52k
        t >>= 5;
97
53.7k
        if (t == 0)
98
19.5k
        {
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
19.5k
            t = 10 - 1;
113
19.5k
#endif
114
19.5k
            NEED_IP(1);
115
1.61M
            while (*ip == 0)
116
1.59M
            {
117
1.59M
                t += 255;
118
1.59M
                ip++;
119
1.59M
                TEST_OV(t);
120
1.59M
                NEED_IP(1);
121
1.57M
            }
122
499
            t += *ip++;
123
499
        }
124
34.2k
        else
125
34.2k
        {
126
34.2k
#if defined(LZO_EOF_CODE)
127
34.2k
            if (m_pos == op)
128
736
                goto eof_found;
129
33.4k
#endif
130
33.4k
            t += 2;
131
33.4k
        }
132
53.0k
        assert(m_pos >= out); assert(m_pos < op);
133
53.0k
        TEST_LB(m_pos);
134
52.9k
        NEED_OP(t);
135
52.9k
        MEMCPY_DS(op,m_pos,t);
136
1.42k
    }
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
736
eof_found:
147
736
    assert(t == 1);
148
736
#endif
149
736
    *out_len = pd(op, out);
150
736
    return (ip == ip_end ? LZO_E_OK :
151
736
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
152
153
154
#if defined(HAVE_NEED_IP)
155
42
input_overrun:
156
42
    *out_len = pd(op, out);
157
42
    return LZO_E_INPUT_OVERRUN;
158
0
#endif
159
160
#if defined(HAVE_NEED_OP)
161
13
output_overrun:
162
13
    *out_len = pd(op, out);
163
13
    return LZO_E_OUTPUT_OVERRUN;
164
0
#endif
165
166
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
167
75
lookbehind_overrun:
168
75
    *out_len = pd(op, out);
169
75
    return LZO_E_LOOKBEHIND_OVERRUN;
170
#endif
171
931
}
lzo2a_decompress
Line
Count
Source
43
731
{
44
731
    lzo_bytep op;
45
731
    const lzo_bytep ip;
46
731
    const lzo_bytep m_pos;
47
48
731
    lzo_uint t;
49
731
    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
731
    lzo_uint32_t b = 0;     /* bit buffer */
55
731
    unsigned k = 0;         /* bits in bit buffer */
56
57
731
    LZO_UNUSED(wrkmem);
58
59
731
    op = out;
60
731
    ip = in;
61
62
5.60M
    while (TEST_IP_AND_TEST_OP)
63
5.60M
    {
64
5.60M
        NEEDBITS(1);
65
5.60M
        if (MASKBITS(1) == 0)
66
4.79M
        {
67
4.79M
            DUMPBITS(1);
68
            /* a literal */
69
4.79M
            NEED_IP(1); NEED_OP(1);
70
4.79M
            *op++ = *ip++;
71
4.79M
            continue;
72
4.79M
        }
73
804k
        DUMPBITS(1);
74
75
804k
        NEEDBITS(1);
76
804k
        if (MASKBITS(1) == 0)
77
752k
        {
78
752k
            DUMPBITS(1);
79
            /* a M1 match */
80
752k
            NEEDBITS(2);
81
752k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
752k
            DUMPBITS(2);
83
752k
            NEED_IP(1); NEED_OP(t);
84
752k
            m_pos = op - 1 - *ip++;
85
752k
            assert(m_pos >= out); assert(m_pos < op);
86
752k
            TEST_LB(m_pos);
87
752k
            MEMCPY_DS(op,m_pos,t);
88
752k
            continue;
89
752k
        }
90
52.2k
        DUMPBITS(1);
91
92
52.2k
        NEED_IP(2);
93
52.2k
        t = *ip++;
94
52.2k
        m_pos = op;
95
52.2k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
52.2k
        t >>= 5;
97
52.2k
        if (t == 0)
98
19.0k
        {
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
19.0k
            t = 10 - 1;
113
19.0k
#endif
114
19.0k
            NEED_IP(1);
115
37.2k
            while (*ip == 0)
116
18.2k
            {
117
18.2k
                t += 255;
118
18.2k
                ip++;
119
18.2k
                TEST_OV(t);
120
18.2k
                NEED_IP(1);
121
18.2k
            }
122
19.0k
            t += *ip++;
123
19.0k
        }
124
33.2k
        else
125
33.2k
        {
126
33.2k
#if defined(LZO_EOF_CODE)
127
33.2k
            if (m_pos == op)
128
731
                goto eof_found;
129
32.4k
#endif
130
32.4k
            t += 2;
131
32.4k
        }
132
52.2k
        assert(m_pos >= out); assert(m_pos < op);
133
51.5k
        TEST_LB(m_pos);
134
51.5k
        NEED_OP(t);
135
51.5k
        MEMCPY_DS(op,m_pos,t);
136
51.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
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
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
731
}
lzo2a_decompress_safe
Line
Count
Source
43
200
{
44
200
    lzo_bytep op;
45
200
    const lzo_bytep ip;
46
200
    const lzo_bytep m_pos;
47
48
200
    lzo_uint t;
49
200
    const lzo_bytep const ip_end = in + in_len;
50
200
#if defined(HAVE_ANY_OP)
51
200
    lzo_bytep const op_end = out + *out_len;
52
200
#endif
53
54
200
    lzo_uint32_t b = 0;     /* bit buffer */
55
200
    unsigned k = 0;         /* bits in bit buffer */
56
57
200
    LZO_UNUSED(wrkmem);
58
59
200
    op = out;
60
200
    ip = in;
61
62
200
    while (TEST_IP_AND_TEST_OP)
63
101k
    {
64
101k
        NEEDBITS(1);
65
101k
        if (MASKBITS(1) == 0)
66
97.1k
        {
67
97.1k
            DUMPBITS(1);
68
            /* a literal */
69
97.1k
            NEED_IP(1); NEED_OP(1);
70
97.0k
            *op++ = *ip++;
71
97.0k
            continue;
72
97.1k
        }
73
4.19k
        DUMPBITS(1);
74
75
4.19k
        NEEDBITS(1);
76
4.19k
        if (MASKBITS(1) == 0)
77
2.66k
        {
78
2.66k
            DUMPBITS(1);
79
            /* a M1 match */
80
2.66k
            NEEDBITS(2);
81
2.66k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
2.66k
            DUMPBITS(2);
83
2.66k
            NEED_IP(1); NEED_OP(t);
84
2.65k
            m_pos = op - 1 - *ip++;
85
2.65k
            assert(m_pos >= out); assert(m_pos < op);
86
2.65k
            TEST_LB(m_pos);
87
2.64k
            MEMCPY_DS(op,m_pos,t);
88
2.64k
            continue;
89
2.65k
        }
90
1.53k
        DUMPBITS(1);
91
92
1.53k
        NEED_IP(2);
93
1.52k
        t = *ip++;
94
1.52k
        m_pos = op;
95
1.52k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
1.52k
        t >>= 5;
97
1.52k
        if (t == 0)
98
512
        {
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
512
            t = 10 - 1;
113
512
#endif
114
512
            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
499
            t += *ip++;
123
499
        }
124
1.00k
        else
125
1.00k
        {
126
1.00k
#if defined(LZO_EOF_CODE)
127
1.00k
            if (m_pos == op)
128
5
                goto eof_found;
129
1.00k
#endif
130
1.00k
            t += 2;
131
1.00k
        }
132
1.52k
        assert(m_pos >= out); assert(m_pos < op);
133
1.50k
        TEST_LB(m_pos);
134
1.43k
        NEED_OP(t);
135
1.42k
        MEMCPY_DS(op,m_pos,t);
136
1.42k
    }
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
42
input_overrun:
156
42
    *out_len = pd(op, out);
157
42
    return LZO_E_INPUT_OVERRUN;
158
0
#endif
159
160
0
#if defined(HAVE_NEED_OP)
161
13
output_overrun:
162
13
    *out_len = pd(op, out);
163
13
    return LZO_E_OUTPUT_OVERRUN;
164
0
#endif
165
166
0
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
167
75
lookbehind_overrun:
168
75
    *out_len = pd(op, out);
169
75
    return LZO_E_LOOKBEHIND_OVERRUN;
170
200
#endif
171
200
}
172
173
174
/* vim:set ts=4 sw=4 et: */