Coverage Report

Created: 2026-09-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lzo-2.10/src/lzo1f_d.ch
Line
Count
Source
1
/* lzo1f_d.ch -- implementation of the LZO1F 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
LZO_PUBLIC(int)
37
DO_DECOMPRESS  ( const lzo_bytep in , lzo_uint  in_len,
38
                       lzo_bytep out, lzo_uintp out_len,
39
                       lzo_voidp wrkmem )
40
1.62k
{
41
1.62k
    lzo_bytep op;
42
1.62k
    const lzo_bytep ip;
43
1.62k
    lzo_uint t;
44
1.62k
    const lzo_bytep m_pos;
45
46
1.62k
    const lzo_bytep const ip_end = in + in_len;
47
#if defined(HAVE_ANY_OP)
48
437
    lzo_bytep const op_end = out + *out_len;
49
#endif
50
51
1.62k
    LZO_UNUSED(wrkmem);
52
53
1.62k
    *out_len = 0;
54
55
1.62k
    op = out;
56
1.62k
    ip = in;
57
58
322k
    while (TEST_IP_AND_TEST_OP)
59
389k
    {
60
389k
        t = *ip++;
61
389k
        if (t > 31)
62
297k
            goto match;
63
64
        /* a literal run */
65
92.5k
        if (t == 0)
66
13.3k
        {
67
13.3k
            NEED_IP(1);
68
1.75M
            while (*ip == 0)
69
1.74M
            {
70
1.74M
                t += 255;
71
1.74M
                ip++;
72
1.74M
                TEST_IV(t);
73
1.74M
                NEED_IP(1);
74
1.67M
            }
75
785
            t += 31 + *ip++;
76
785
        }
77
        /* copy literals */
78
92.5k
        assert(t > 0); NEED_OP(t); NEED_IP(t+1);
79
8.30k
#if (LZO_OPT_UNALIGNED32)
80
92.4k
        if (t >= 4)
81
90.3k
        {
82
4.91M
            do {
83
4.91M
                UA_COPY4(op, ip);
84
4.91M
                op += 4; ip += 4; t -= 4;
85
4.91M
            } while (t >= 4);
86
111k
            if (t > 0) do *op++ = *ip++; while (--t > 0);
87
90.3k
        }
88
2.00k
        else
89
2.00k
#endif
90
3.44k
        do *op++ = *ip++; while (--t > 0);
91
92
8.30k
        t = *ip++;
93
94
269k
        while (TEST_IP_AND_TEST_OP)
95
395k
        {
96
            /* handle matches */
97
395k
            if (t < 32)
98
52.7k
            {
99
52.7k
                m_pos = op - 1 - 0x800;
100
52.7k
                m_pos -= (t >> 2) & 7;
101
52.7k
                m_pos -= *ip++ << 3;
102
52.7k
                TEST_LB(m_pos); NEED_OP(3);
103
43.0k
                *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
104
43.0k
            }
105
342k
            else
106
342k
            {
107
639k
match:
108
639k
                if (t < M3_MARKER)
109
431k
                {
110
431k
                    m_pos = op - 1;
111
431k
                    m_pos -= (t >> 2) & 7;
112
431k
                    m_pos -= *ip++ << 3;
113
431k
                    t >>= 5;
114
431k
                    TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
115
122k
                    goto copy_match;
116
122k
                }
117
208k
                else
118
208k
                {
119
208k
                    t &= 31;
120
208k
                    if (t == 0)
121
17.4k
                    {
122
17.4k
                        NEED_IP(1);
123
1.14M
                        while (*ip == 0)
124
1.12M
                        {
125
1.12M
                            t += 255;
126
1.12M
                            ip++;
127
1.12M
                            TEST_OV(t);
128
1.12M
                            NEED_IP(1);
129
1.05M
                        }
130
3.16k
                        t += 31 + *ip++;
131
3.16k
                    }
132
208k
                    NEED_IP(2);
133
18.0k
                    m_pos = op;
134
18.0k
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
135
208k
                    m_pos -= UA_GET_LE16(ip) >> 2;
136
18.0k
                    ip += 2;
137
#else
138
                    m_pos -= *ip++ >> 2;
139
                    m_pos -= *ip++ << 6;
140
#endif
141
208k
                    if (m_pos == op)
142
1.21k
                        goto eof_found;
143
18.0k
                }
144
145
                /* copy match */
146
207k
                TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
147
18.0k
#if (LZO_OPT_UNALIGNED32)
148
207k
                if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
149
79.5k
                {
150
79.5k
                    UA_COPY4(op, m_pos);
151
79.5k
                    op += 4; m_pos += 4; t -= 4 - (3 - 1);
152
2.72M
                    do {
153
2.72M
                        UA_COPY4(op, m_pos);
154
2.72M
                        op += 4; m_pos += 4; t -= 4;
155
2.72M
                    } while (t >= 4);
156
109k
                    if (t > 0) do *op++ = *m_pos++; while (--t > 0);
157
79.5k
                }
158
127k
                else
159
127k
#endif
160
127k
                {
161
558k
copy_match:
162
558k
                *op++ = *m_pos++; *op++ = *m_pos++;
163
18.4M
                do *op++ = *m_pos++; while (--t > 0);
164
558k
                }
165
18.0k
            }
166
690k
            t = ip[-2] & 3;
167
690k
            if (t == 0)
168
388k
                break;
169
170
            /* copy literals */
171
690k
            assert(t > 0); NEED_OP(t); NEED_IP(t+1);
172
518k
            do *op++ = *ip++; while (--t > 0);
173
116k
            t = *ip++;
174
116k
        }
175
8.30k
    }
176
177
#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
178
    /* no EOF code was found */
179
49
    *out_len = pd(op, out);
180
49
    return LZO_E_EOF_NOT_FOUND;
181
0
#endif
182
183
1.21k
eof_found:
184
1.21k
    assert(t == 1);
185
1.21k
    *out_len = pd(op, out);
186
1.21k
    return (ip == ip_end ? LZO_E_OK :
187
1.21k
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
188
189
190
#if defined(HAVE_NEED_IP)
191
221
input_overrun:
192
221
    *out_len = pd(op, out);
193
221
    return LZO_E_INPUT_OVERRUN;
194
0
#endif
195
196
#if defined(HAVE_NEED_OP)
197
38
output_overrun:
198
38
    *out_len = pd(op, out);
199
38
    return LZO_E_OUTPUT_OVERRUN;
200
0
#endif
201
202
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
203
109
lookbehind_overrun:
204
109
    *out_len = pd(op, out);
205
109
    return LZO_E_LOOKBEHIND_OVERRUN;
206
#endif
207
1.62k
}
lzo1f_decompress
Line
Count
Source
40
1.19k
{
41
1.19k
    lzo_bytep op;
42
1.19k
    const lzo_bytep ip;
43
1.19k
    lzo_uint t;
44
1.19k
    const lzo_bytep m_pos;
45
46
1.19k
    const lzo_bytep const ip_end = in + in_len;
47
#if defined(HAVE_ANY_OP)
48
    lzo_bytep const op_end = out + *out_len;
49
#endif
50
51
1.19k
    LZO_UNUSED(wrkmem);
52
53
1.19k
    *out_len = 0;
54
55
1.19k
    op = out;
56
1.19k
    ip = in;
57
58
322k
    while (TEST_IP_AND_TEST_OP)
59
322k
    {
60
322k
        t = *ip++;
61
322k
        if (t > 31)
62
238k
            goto match;
63
64
        /* a literal run */
65
84.0k
        if (t == 0)
66
12.5k
        {
67
12.5k
            NEED_IP(1);
68
76.3k
            while (*ip == 0)
69
63.8k
            {
70
63.8k
                t += 255;
71
63.8k
                ip++;
72
63.8k
                TEST_IV(t);
73
63.8k
                NEED_IP(1);
74
63.8k
            }
75
12.5k
            t += 31 + *ip++;
76
12.5k
        }
77
        /* copy literals */
78
84.0k
        assert(t > 0); NEED_OP(t); NEED_IP(t+1);
79
84.0k
#if (LZO_OPT_UNALIGNED32)
80
84.0k
        if (t >= 4)
81
83.5k
        {
82
4.55M
            do {
83
4.55M
                UA_COPY4(op, ip);
84
4.55M
                op += 4; ip += 4; t -= 4;
85
4.55M
            } while (t >= 4);
86
102k
            if (t > 0) do *op++ = *ip++; while (--t > 0);
87
83.5k
        }
88
523
        else
89
523
#endif
90
863
        do *op++ = *ip++; while (--t > 0);
91
92
84.0k
        t = *ip++;
93
94
269k
        while (TEST_IP_AND_TEST_OP)
95
269k
        {
96
            /* handle matches */
97
269k
            if (t < 32)
98
9.68k
            {
99
9.68k
                m_pos = op - 1 - 0x800;
100
9.68k
                m_pos -= (t >> 2) & 7;
101
9.68k
                m_pos -= *ip++ << 3;
102
9.68k
                TEST_LB(m_pos); NEED_OP(3);
103
9.68k
                *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
104
9.68k
            }
105
260k
            else
106
260k
            {
107
498k
match:
108
498k
                if (t < M3_MARKER)
109
308k
                {
110
308k
                    m_pos = op - 1;
111
308k
                    m_pos -= (t >> 2) & 7;
112
308k
                    m_pos -= *ip++ << 3;
113
308k
                    t >>= 5;
114
308k
                    TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
115
308k
                    goto copy_match;
116
308k
                }
117
190k
                else
118
190k
                {
119
190k
                    t &= 31;
120
190k
                    if (t == 0)
121
14.2k
                    {
122
14.2k
                        NEED_IP(1);
123
81.9k
                        while (*ip == 0)
124
67.6k
                        {
125
67.6k
                            t += 255;
126
67.6k
                            ip++;
127
67.6k
                            TEST_OV(t);
128
67.6k
                            NEED_IP(1);
129
67.6k
                        }
130
14.2k
                        t += 31 + *ip++;
131
14.2k
                    }
132
190k
                    NEED_IP(2);
133
190k
                    m_pos = op;
134
190k
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
135
190k
                    m_pos -= UA_GET_LE16(ip) >> 2;
136
190k
                    ip += 2;
137
#else
138
                    m_pos -= *ip++ >> 2;
139
                    m_pos -= *ip++ << 6;
140
#endif
141
190k
                    if (m_pos == op)
142
1.19k
                        goto eof_found;
143
190k
                }
144
145
                /* copy match */
146
189k
                TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
147
189k
#if (LZO_OPT_UNALIGNED32)
148
189k
                if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
149
66.5k
                {
150
66.5k
                    UA_COPY4(op, m_pos);
151
66.5k
                    op += 4; m_pos += 4; t -= 4 - (3 - 1);
152
2.00M
                    do {
153
2.00M
                        UA_COPY4(op, m_pos);
154
2.00M
                        op += 4; m_pos += 4; t -= 4;
155
2.00M
                    } while (t >= 4);
156
91.5k
                    if (t > 0) do *op++ = *m_pos++; while (--t > 0);
157
66.5k
                }
158
122k
                else
159
122k
#endif
160
122k
                {
161
431k
copy_match:
162
431k
                *op++ = *m_pos++; *op++ = *m_pos++;
163
12.2M
                do *op++ = *m_pos++; while (--t > 0);
164
431k
                }
165
189k
            }
166
507k
            t = ip[-2] & 3;
167
507k
            if (t == 0)
168
321k
                break;
169
170
            /* copy literals */
171
507k
            assert(t > 0); NEED_OP(t); NEED_IP(t+1);
172
292k
            do *op++ = *ip++; while (--t > 0);
173
185k
            t = *ip++;
174
185k
        }
175
84.0k
    }
176
177
#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
178
    /* no EOF code was found */
179
    *out_len = pd(op, out);
180
    return LZO_E_EOF_NOT_FOUND;
181
#endif
182
183
1.19k
eof_found:
184
1.19k
    assert(t == 1);
185
1.19k
    *out_len = pd(op, out);
186
1.19k
    return (ip == ip_end ? LZO_E_OK :
187
1.19k
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
188
189
190
#if defined(HAVE_NEED_IP)
191
input_overrun:
192
    *out_len = pd(op, out);
193
    return LZO_E_INPUT_OVERRUN;
194
#endif
195
196
#if defined(HAVE_NEED_OP)
197
output_overrun:
198
    *out_len = pd(op, out);
199
    return LZO_E_OUTPUT_OVERRUN;
200
#endif
201
202
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
203
lookbehind_overrun:
204
    *out_len = pd(op, out);
205
    return LZO_E_LOOKBEHIND_OVERRUN;
206
#endif
207
1.19k
}
lzo1f_decompress_safe
Line
Count
Source
40
437
{
41
437
    lzo_bytep op;
42
437
    const lzo_bytep ip;
43
437
    lzo_uint t;
44
437
    const lzo_bytep m_pos;
45
46
437
    const lzo_bytep const ip_end = in + in_len;
47
437
#if defined(HAVE_ANY_OP)
48
437
    lzo_bytep const op_end = out + *out_len;
49
437
#endif
50
51
437
    LZO_UNUSED(wrkmem);
52
53
437
    *out_len = 0;
54
55
437
    op = out;
56
437
    ip = in;
57
58
437
    while (TEST_IP_AND_TEST_OP)
59
66.8k
    {
60
66.8k
        t = *ip++;
61
66.8k
        if (t > 31)
62
58.4k
            goto match;
63
64
        /* a literal run */
65
8.45k
        if (t == 0)
66
816
        {
67
816
            NEED_IP(1);
68
1.68M
            while (*ip == 0)
69
1.67M
            {
70
1.67M
                t += 255;
71
1.67M
                ip++;
72
1.67M
                TEST_IV(t);
73
1.67M
                NEED_IP(1);
74
1.67M
            }
75
785
            t += 31 + *ip++;
76
785
        }
77
        /* copy literals */
78
8.45k
        assert(t > 0); NEED_OP(t); NEED_IP(t+1);
79
8.30k
#if (LZO_OPT_UNALIGNED32)
80
8.30k
        if (t >= 4)
81
6.82k
        {
82
355k
            do {
83
355k
                UA_COPY4(op, ip);
84
355k
                op += 4; ip += 4; t -= 4;
85
355k
            } while (t >= 4);
86
8.17k
            if (t > 0) do *op++ = *ip++; while (--t > 0);
87
6.82k
        }
88
1.48k
        else
89
1.48k
#endif
90
2.57k
        do *op++ = *ip++; while (--t > 0);
91
92
8.30k
        t = *ip++;
93
94
8.30k
        while (TEST_IP_AND_TEST_OP)
95
125k
        {
96
            /* handle matches */
97
125k
            if (t < 32)
98
43.0k
            {
99
43.0k
                m_pos = op - 1 - 0x800;
100
43.0k
                m_pos -= (t >> 2) & 7;
101
43.0k
                m_pos -= *ip++ << 3;
102
43.0k
                TEST_LB(m_pos); NEED_OP(3);
103
43.0k
                *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
104
43.0k
            }
105
82.0k
            else
106
82.0k
            {
107
140k
match:
108
140k
                if (t < M3_MARKER)
109
122k
                {
110
122k
                    m_pos = op - 1;
111
122k
                    m_pos -= (t >> 2) & 7;
112
122k
                    m_pos -= *ip++ << 3;
113
122k
                    t >>= 5;
114
122k
                    TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
115
122k
                    goto copy_match;
116
122k
                }
117
18.1k
                else
118
18.1k
                {
119
18.1k
                    t &= 31;
120
18.1k
                    if (t == 0)
121
3.18k
                    {
122
3.18k
                        NEED_IP(1);
123
1.06M
                        while (*ip == 0)
124
1.05M
                        {
125
1.05M
                            t += 255;
126
1.05M
                            ip++;
127
1.05M
                            TEST_OV(t);
128
1.05M
                            NEED_IP(1);
129
1.05M
                        }
130
3.16k
                        t += 31 + *ip++;
131
3.16k
                    }
132
18.1k
                    NEED_IP(2);
133
18.0k
                    m_pos = op;
134
18.0k
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
135
18.0k
                    m_pos -= UA_GET_LE16(ip) >> 2;
136
18.0k
                    ip += 2;
137
#else
138
                    m_pos -= *ip++ >> 2;
139
                    m_pos -= *ip++ << 6;
140
#endif
141
18.0k
                    if (m_pos == op)
142
20
                        goto eof_found;
143
18.0k
                }
144
145
                /* copy match */
146
18.0k
                TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
147
18.0k
#if (LZO_OPT_UNALIGNED32)
148
18.0k
                if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
149
13.0k
                {
150
13.0k
                    UA_COPY4(op, m_pos);
151
13.0k
                    op += 4; m_pos += 4; t -= 4 - (3 - 1);
152
718k
                    do {
153
718k
                        UA_COPY4(op, m_pos);
154
718k
                        op += 4; m_pos += 4; t -= 4;
155
718k
                    } while (t >= 4);
156
18.3k
                    if (t > 0) do *op++ = *m_pos++; while (--t > 0);
157
13.0k
                }
158
4.98k
                else
159
4.98k
#endif
160
4.98k
                {
161
127k
copy_match:
162
127k
                *op++ = *m_pos++; *op++ = *m_pos++;
163
6.11M
                do *op++ = *m_pos++; while (--t > 0);
164
127k
                }
165
18.0k
            }
166
183k
            t = ip[-2] & 3;
167
183k
            if (t == 0)
168
66.4k
                break;
169
170
            /* copy literals */
171
183k
            assert(t > 0); NEED_OP(t); NEED_IP(t+1);
172
225k
            do *op++ = *ip++; while (--t > 0);
173
116k
            t = *ip++;
174
116k
        }
175
8.30k
    }
176
177
49
#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
178
    /* no EOF code was found */
179
49
    *out_len = pd(op, out);
180
49
    return LZO_E_EOF_NOT_FOUND;
181
0
#endif
182
183
20
eof_found:
184
20
    assert(t == 1);
185
20
    *out_len = pd(op, out);
186
20
    return (ip == ip_end ? LZO_E_OK :
187
20
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
188
189
190
0
#if defined(HAVE_NEED_IP)
191
221
input_overrun:
192
221
    *out_len = pd(op, out);
193
221
    return LZO_E_INPUT_OVERRUN;
194
0
#endif
195
196
0
#if defined(HAVE_NEED_OP)
197
38
output_overrun:
198
38
    *out_len = pd(op, out);
199
38
    return LZO_E_OUTPUT_OVERRUN;
200
0
#endif
201
202
0
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
203
109
lookbehind_overrun:
204
109
    *out_len = pd(op, out);
205
109
    return LZO_E_LOOKBEHIND_OVERRUN;
206
437
#endif
207
437
}
208
209
210
/* vim:set ts=4 sw=4 et: */