Coverage Report

Created: 2026-04-01 06:18

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
646k
#define _NEEDBYTE   NEED_IP(1)
37
646k
#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
958
{
44
958
    lzo_bytep op;
45
958
    const lzo_bytep ip;
46
958
    const lzo_bytep m_pos;
47
48
958
    lzo_uint t;
49
958
    const lzo_bytep const ip_end = in + in_len;
50
#if defined(HAVE_ANY_OP)
51
199
    lzo_bytep const op_end = out + *out_len;
52
#endif
53
54
958
    lzo_uint32_t b = 0;     /* bit buffer */
55
958
    unsigned k = 0;         /* bits in bit buffer */
56
57
958
    LZO_UNUSED(wrkmem);
58
59
958
    op = out;
60
958
    ip = in;
61
62
3.20M
    while (TEST_IP_AND_TEST_OP)
63
3.22M
    {
64
3.22M
        NEEDBITS(1);
65
3.22M
        if (MASKBITS(1) == 0)
66
2.53M
        {
67
2.53M
            DUMPBITS(1);
68
            /* a literal */
69
2.53M
            NEED_IP(1); NEED_OP(1);
70
13.1k
            *op++ = *ip++;
71
13.1k
            continue;
72
13.1k
        }
73
690k
        DUMPBITS(1);
74
75
690k
        NEEDBITS(1);
76
690k
        if (MASKBITS(1) == 0)
77
626k
        {
78
626k
            DUMPBITS(1);
79
            /* a M1 match */
80
626k
            NEEDBITS(2);
81
626k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
626k
            DUMPBITS(2);
83
626k
            NEED_IP(1); NEED_OP(t);
84
2.45k
            m_pos = op - 1 - *ip++;
85
2.45k
            assert(m_pos >= out); assert(m_pos < op);
86
626k
            TEST_LB(m_pos);
87
626k
            MEMCPY_DS(op,m_pos,t);
88
2.44k
            continue;
89
2.45k
        }
90
64.1k
        DUMPBITS(1);
91
92
64.1k
        NEED_IP(2);
93
1.00k
        t = *ip++;
94
1.00k
        m_pos = op;
95
1.00k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
1.00k
        t >>= 5;
97
64.1k
        if (t == 0)
98
23.8k
        {
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
23.8k
            t = 10 - 1;
113
23.8k
#endif
114
23.8k
            NEED_IP(1);
115
1.64M
            while (*ip == 0)
116
1.62M
            {
117
1.62M
                t += 255;
118
1.62M
                ip++;
119
1.62M
                TEST_OV(t);
120
1.62M
                NEED_IP(1);
121
1.60M
            }
122
444
            t += *ip++;
123
444
        }
124
40.2k
        else
125
40.2k
        {
126
40.2k
#if defined(LZO_EOF_CODE)
127
40.2k
            if (m_pos == op)
128
763
                goto eof_found;
129
39.5k
#endif
130
39.5k
            t += 2;
131
39.5k
        }
132
63.3k
        assert(m_pos >= out); assert(m_pos < op);
133
63.3k
        TEST_LB(m_pos);
134
63.2k
        NEED_OP(t);
135
63.2k
        MEMCPY_DS(op,m_pos,t);
136
910
    }
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
763
eof_found:
147
763
    assert(t == 1);
148
763
#endif
149
763
    *out_len = pd(op, out);
150
763
    return (ip == ip_end ? LZO_E_OK :
151
763
           (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
76
lookbehind_overrun:
168
76
    *out_len = pd(op, out);
169
76
    return LZO_E_LOOKBEHIND_OVERRUN;
170
#endif
171
958
}
lzo2a_decompress
Line
Count
Source
43
759
{
44
759
    lzo_bytep op;
45
759
    const lzo_bytep ip;
46
759
    const lzo_bytep m_pos;
47
48
759
    lzo_uint t;
49
759
    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
759
    lzo_uint32_t b = 0;     /* bit buffer */
55
759
    unsigned k = 0;         /* bits in bit buffer */
56
57
759
    LZO_UNUSED(wrkmem);
58
59
759
    op = out;
60
759
    ip = in;
61
62
3.20M
    while (TEST_IP_AND_TEST_OP)
63
3.20M
    {
64
3.20M
        NEEDBITS(1);
65
3.20M
        if (MASKBITS(1) == 0)
66
2.51M
        {
67
2.51M
            DUMPBITS(1);
68
            /* a literal */
69
2.51M
            NEED_IP(1); NEED_OP(1);
70
2.51M
            *op++ = *ip++;
71
2.51M
            continue;
72
2.51M
        }
73
686k
        DUMPBITS(1);
74
75
686k
        NEEDBITS(1);
76
686k
        if (MASKBITS(1) == 0)
77
623k
        {
78
623k
            DUMPBITS(1);
79
            /* a M1 match */
80
623k
            NEEDBITS(2);
81
623k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
623k
            DUMPBITS(2);
83
623k
            NEED_IP(1); NEED_OP(t);
84
623k
            m_pos = op - 1 - *ip++;
85
623k
            assert(m_pos >= out); assert(m_pos < op);
86
623k
            TEST_LB(m_pos);
87
623k
            MEMCPY_DS(op,m_pos,t);
88
623k
            continue;
89
623k
        }
90
63.1k
        DUMPBITS(1);
91
92
63.1k
        NEED_IP(2);
93
63.1k
        t = *ip++;
94
63.1k
        m_pos = op;
95
63.1k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
63.1k
        t >>= 5;
97
63.1k
        if (t == 0)
98
23.3k
        {
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
23.3k
            t = 10 - 1;
113
23.3k
#endif
114
23.3k
            NEED_IP(1);
115
39.9k
            while (*ip == 0)
116
16.5k
            {
117
16.5k
                t += 255;
118
16.5k
                ip++;
119
16.5k
                TEST_OV(t);
120
16.5k
                NEED_IP(1);
121
16.5k
            }
122
23.3k
            t += *ip++;
123
23.3k
        }
124
39.7k
        else
125
39.7k
        {
126
39.7k
#if defined(LZO_EOF_CODE)
127
39.7k
            if (m_pos == op)
128
759
                goto eof_found;
129
38.9k
#endif
130
38.9k
            t += 2;
131
38.9k
        }
132
63.1k
        assert(m_pos >= out); assert(m_pos < op);
133
62.3k
        TEST_LB(m_pos);
134
62.3k
        NEED_OP(t);
135
62.3k
        MEMCPY_DS(op,m_pos,t);
136
62.3k
    }
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
759
eof_found:
147
759
    assert(t == 1);
148
759
#endif
149
759
    *out_len = pd(op, out);
150
759
    return (ip == ip_end ? LZO_E_OK :
151
759
           (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
759
}
lzo2a_decompress_safe
Line
Count
Source
43
199
{
44
199
    lzo_bytep op;
45
199
    const lzo_bytep ip;
46
199
    const lzo_bytep m_pos;
47
48
199
    lzo_uint t;
49
199
    const lzo_bytep const ip_end = in + in_len;
50
199
#if defined(HAVE_ANY_OP)
51
199
    lzo_bytep const op_end = out + *out_len;
52
199
#endif
53
54
199
    lzo_uint32_t b = 0;     /* bit buffer */
55
199
    unsigned k = 0;         /* bits in bit buffer */
56
57
199
    LZO_UNUSED(wrkmem);
58
59
199
    op = out;
60
199
    ip = in;
61
62
199
    while (TEST_IP_AND_TEST_OP)
63
16.6k
    {
64
16.6k
        NEEDBITS(1);
65
16.6k
        if (MASKBITS(1) == 0)
66
13.1k
        {
67
13.1k
            DUMPBITS(1);
68
            /* a literal */
69
13.1k
            NEED_IP(1); NEED_OP(1);
70
13.1k
            *op++ = *ip++;
71
13.1k
            continue;
72
13.1k
        }
73
3.48k
        DUMPBITS(1);
74
75
3.48k
        NEEDBITS(1);
76
3.48k
        if (MASKBITS(1) == 0)
77
2.46k
        {
78
2.46k
            DUMPBITS(1);
79
            /* a M1 match */
80
2.46k
            NEEDBITS(2);
81
2.46k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
2.46k
            DUMPBITS(2);
83
2.46k
            NEED_IP(1); NEED_OP(t);
84
2.45k
            m_pos = op - 1 - *ip++;
85
2.45k
            assert(m_pos >= out); assert(m_pos < op);
86
2.45k
            TEST_LB(m_pos);
87
2.44k
            MEMCPY_DS(op,m_pos,t);
88
2.44k
            continue;
89
2.45k
        }
90
1.01k
        DUMPBITS(1);
91
92
1.01k
        NEED_IP(2);
93
1.00k
        t = *ip++;
94
1.00k
        m_pos = op;
95
1.00k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
1.00k
        t >>= 5;
97
1.00k
        if (t == 0)
98
460
        {
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
460
            t = 10 - 1;
113
460
#endif
114
460
            NEED_IP(1);
115
1.60M
            while (*ip == 0)
116
1.60M
            {
117
1.60M
                t += 255;
118
1.60M
                ip++;
119
1.60M
                TEST_OV(t);
120
1.60M
                NEED_IP(1);
121
1.60M
            }
122
444
            t += *ip++;
123
444
        }
124
540
        else
125
540
        {
126
540
#if defined(LZO_EOF_CODE)
127
540
            if (m_pos == op)
128
4
                goto eof_found;
129
536
#endif
130
536
            t += 2;
131
536
        }
132
1.00k
        assert(m_pos >= out); assert(m_pos < op);
133
980
        TEST_LB(m_pos);
134
917
        NEED_OP(t);
135
910
        MEMCPY_DS(op,m_pos,t);
136
910
    }
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
4
eof_found:
147
4
    assert(t == 1);
148
4
#endif
149
4
    *out_len = pd(op, out);
150
4
    return (ip == ip_end ? LZO_E_OK :
151
4
           (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
76
lookbehind_overrun:
168
76
    *out_len = pd(op, out);
169
76
    return LZO_E_LOOKBEHIND_OVERRUN;
170
199
#endif
171
199
}
172
173
174
/* vim:set ts=4 sw=4 et: */