Coverage Report

Created: 2025-10-13 06:56

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
438
{
41
438
    lzo_bytep op;
42
438
    const lzo_bytep ip;
43
438
    lzo_uint t;
44
438
    const lzo_bytep m_pos;
45
46
438
    const lzo_bytep const ip_end = in + in_len;
47
438
#if defined(HAVE_ANY_OP)
48
438
    lzo_bytep const op_end = out + *out_len;
49
438
#endif
50
51
438
    LZO_UNUSED(wrkmem);
52
53
438
    *out_len = 0;
54
55
438
    op = out;
56
438
    ip = in;
57
58
438
    while (TEST_IP_AND_TEST_OP)
59
19.2k
    {
60
19.2k
        t = *ip++;
61
19.2k
        if (t > 31)
62
9.90k
            goto match;
63
64
        /* a literal run */
65
9.37k
        if (t == 0)
66
642
        {
67
642
            NEED_IP(1);
68
960k
            while (*ip == 0)
69
959k
            {
70
959k
                t += 255;
71
959k
                ip++;
72
959k
                TEST_IV(t);
73
959k
                NEED_IP(1);
74
959k
            }
75
618
            t += 31 + *ip++;
76
618
        }
77
        /* copy literals */
78
9.37k
        assert(t > 0); NEED_OP(t); NEED_IP(t+1);
79
9.24k
#if (LZO_OPT_UNALIGNED32)
80
9.24k
        if (t >= 4)
81
7.61k
        {
82
166k
            do {
83
166k
                UA_COPY4(op, ip);
84
166k
                op += 4; ip += 4; t -= 4;
85
166k
            } while (t >= 4);
86
7.61k
            if (t > 0) do *op++ = *ip++; while (--t > 0);
87
7.61k
        }
88
1.62k
        else
89
1.62k
#endif
90
2.61k
        do *op++ = *ip++; while (--t > 0);
91
92
9.24k
        t = *ip++;
93
94
9.24k
        while (TEST_IP_AND_TEST_OP)
95
33.8k
        {
96
            /* handle matches */
97
33.8k
            if (t < 32)
98
7.72k
            {
99
7.72k
                m_pos = op - 1 - 0x800;
100
7.72k
                m_pos -= (t >> 2) & 7;
101
7.72k
                m_pos -= *ip++ << 3;
102
7.72k
                TEST_LB(m_pos); NEED_OP(3);
103
7.67k
                *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
104
7.67k
            }
105
26.0k
            else
106
26.0k
            {
107
35.9k
match:
108
35.9k
                if (t < M3_MARKER)
109
22.3k
                {
110
22.3k
                    m_pos = op - 1;
111
22.3k
                    m_pos -= (t >> 2) & 7;
112
22.3k
                    m_pos -= *ip++ << 3;
113
22.3k
                    t >>= 5;
114
22.3k
                    TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
115
22.3k
                    goto copy_match;
116
22.3k
                }
117
13.5k
                else
118
13.5k
                {
119
13.5k
                    t &= 31;
120
13.5k
                    if (t == 0)
121
7.77k
                    {
122
7.77k
                        NEED_IP(1);
123
1.84M
                        while (*ip == 0)
124
1.83M
                        {
125
1.83M
                            t += 255;
126
1.83M
                            ip++;
127
1.83M
                            TEST_OV(t);
128
1.83M
                            NEED_IP(1);
129
1.83M
                        }
130
7.75k
                        t += 31 + *ip++;
131
7.75k
                    }
132
13.5k
                    NEED_IP(2);
133
13.5k
                    m_pos = op;
134
13.5k
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
135
13.5k
                    m_pos -= UA_GET_LE16(ip) >> 2;
136
13.5k
                    ip += 2;
137
#else
138
                    m_pos -= *ip++ >> 2;
139
                    m_pos -= *ip++ << 6;
140
#endif
141
13.5k
                    if (m_pos == op)
142
20
                        goto eof_found;
143
13.5k
                }
144
145
                /* copy match */
146
13.5k
                TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
147
13.4k
#if (LZO_OPT_UNALIGNED32)
148
13.4k
                if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
149
8.68k
                {
150
8.68k
                    UA_COPY4(op, m_pos);
151
8.68k
                    op += 4; m_pos += 4; t -= 4 - (3 - 1);
152
631k
                    do {
153
631k
                        UA_COPY4(op, m_pos);
154
631k
                        op += 4; m_pos += 4; t -= 4;
155
631k
                    } while (t >= 4);
156
11.2k
                    if (t > 0) do *op++ = *m_pos++; while (--t > 0);
157
8.68k
                }
158
4.77k
                else
159
4.77k
#endif
160
4.77k
                {
161
27.1k
copy_match:
162
27.1k
                *op++ = *m_pos++; *op++ = *m_pos++;
163
7.81M
                do *op++ = *m_pos++; while (--t > 0);
164
27.1k
                }
165
13.4k
            }
166
43.4k
            t = ip[-2] & 3;
167
43.4k
            if (t == 0)
168
18.8k
                break;
169
170
            /* copy literals */
171
43.4k
            assert(t > 0); NEED_OP(t); NEED_IP(t+1);
172
44.3k
            do *op++ = *ip++; while (--t > 0);
173
24.5k
            t = *ip++;
174
24.5k
        }
175
9.24k
    }
176
177
34
#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
178
    /* no EOF code was found */
179
34
    *out_len = pd(op, out);
180
34
    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
230
input_overrun:
192
230
    *out_len = pd(op, out);
193
230
    return LZO_E_INPUT_OVERRUN;
194
0
#endif
195
196
0
#if defined(HAVE_NEED_OP)
197
33
output_overrun:
198
33
    *out_len = pd(op, out);
199
33
    return LZO_E_OUTPUT_OVERRUN;
200
0
#endif
201
202
0
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
203
121
lookbehind_overrun:
204
121
    *out_len = pd(op, out);
205
121
    return LZO_E_LOOKBEHIND_OVERRUN;
206
438
#endif
207
438
}
208
209
210
/* vim:set ts=4 sw=4 et: */