Coverage Report

Created: 2025-08-03 06:59

/src/lzo-2.10/src/lzo1f_d.ch
Line
Count
Source (jump to first uncovered line)
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.12k
{
41
1.12k
    lzo_bytep op;
42
1.12k
    const lzo_bytep ip;
43
1.12k
    lzo_uint t;
44
1.12k
    const lzo_bytep m_pos;
45
46
1.12k
    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.12k
    LZO_UNUSED(wrkmem);
52
53
1.12k
    *out_len = 0;
54
55
1.12k
    op = out;
56
1.12k
    ip = in;
57
58
173k
    while (TEST_IP_AND_TEST_OP)
59
173k
    {
60
173k
        t = *ip++;
61
173k
        if (t > 31)
62
121k
            goto match;
63
64
        /* a literal run */
65
52.0k
        if (t == 0)
66
10.6k
        {
67
10.6k
            NEED_IP(1);
68
34.9k
            while (*ip == 0)
69
24.2k
            {
70
24.2k
                t += 255;
71
24.2k
                ip++;
72
24.2k
                TEST_IV(t);
73
24.2k
                NEED_IP(1);
74
24.2k
            }
75
10.6k
            t += 31 + *ip++;
76
10.6k
        }
77
        /* copy literals */
78
52.0k
        assert(t > 0); NEED_OP(t); NEED_IP(t+1);
79
52.0k
#if (LZO_OPT_UNALIGNED32)
80
52.0k
        if (t >= 4)
81
51.5k
        {
82
1.93M
            do {
83
1.93M
                UA_COPY4(op, ip);
84
1.93M
                op += 4; ip += 4; t -= 4;
85
1.93M
            } while (t >= 4);
86
65.1k
            if (t > 0) do *op++ = *ip++; while (--t > 0);
87
51.5k
        }
88
514
        else
89
514
#endif
90
888
        do *op++ = *ip++; while (--t > 0);
91
92
52.0k
        t = *ip++;
93
94
147k
        while (TEST_IP_AND_TEST_OP)
95
147k
        {
96
            /* handle matches */
97
147k
            if (t < 32)
98
4.61k
            {
99
4.61k
                m_pos = op - 1 - 0x800;
100
4.61k
                m_pos -= (t >> 2) & 7;
101
4.61k
                m_pos -= *ip++ << 3;
102
4.61k
                TEST_LB(m_pos); NEED_OP(3);
103
4.61k
                *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
104
4.61k
            }
105
142k
            else
106
142k
            {
107
264k
match:
108
264k
                if (t < M3_MARKER)
109
136k
                {
110
136k
                    m_pos = op - 1;
111
136k
                    m_pos -= (t >> 2) & 7;
112
136k
                    m_pos -= *ip++ << 3;
113
136k
                    t >>= 5;
114
136k
                    TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
115
136k
                    goto copy_match;
116
136k
                }
117
127k
                else
118
127k
                {
119
127k
                    t &= 31;
120
127k
                    if (t == 0)
121
33.6k
                    {
122
33.6k
                        NEED_IP(1);
123
79.0k
                        while (*ip == 0)
124
45.3k
                        {
125
45.3k
                            t += 255;
126
45.3k
                            ip++;
127
45.3k
                            TEST_OV(t);
128
45.3k
                            NEED_IP(1);
129
45.3k
                        }
130
33.6k
                        t += 31 + *ip++;
131
33.6k
                    }
132
127k
                    NEED_IP(2);
133
127k
                    m_pos = op;
134
127k
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
135
127k
                    m_pos -= UA_GET_LE16(ip) >> 2;
136
127k
                    ip += 2;
137
#else
138
                    m_pos -= *ip++ >> 2;
139
                    m_pos -= *ip++ << 6;
140
#endif
141
127k
                    if (m_pos == op)
142
1.12k
                        goto eof_found;
143
127k
                }
144
145
                /* copy match */
146
126k
                TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
147
126k
#if (LZO_OPT_UNALIGNED32)
148
126k
                if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
149
81.8k
                {
150
81.8k
                    UA_COPY4(op, m_pos);
151
81.8k
                    op += 4; m_pos += 4; t -= 4 - (3 - 1);
152
3.01M
                    do {
153
3.01M
                        UA_COPY4(op, m_pos);
154
3.01M
                        op += 4; m_pos += 4; t -= 4;
155
3.01M
                    } while (t >= 4);
156
113k
                    if (t > 0) do *op++ = *m_pos++; while (--t > 0);
157
81.8k
                }
158
44.8k
                else
159
44.8k
#endif
160
44.8k
                {
161
181k
copy_match:
162
181k
                *op++ = *m_pos++; *op++ = *m_pos++;
163
4.03M
                do *op++ = *m_pos++; while (--t > 0);
164
181k
                }
165
126k
            }
166
267k
            t = ip[-2] & 3;
167
267k
            if (t == 0)
168
172k
                break;
169
170
            /* copy literals */
171
95.4k
            assert(t > 0); NEED_OP(t); NEED_IP(t+1);
172
151k
            do *op++ = *ip++; while (--t > 0);
173
95.4k
            t = *ip++;
174
95.4k
        }
175
52.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.12k
eof_found:
184
1.12k
    assert(t == 1);
185
1.12k
    *out_len = pd(op, out);
186
1.12k
    return (ip == ip_end ? LZO_E_OK :
187
1.12k
           (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.12k
}
208
209
210
/* vim:set ts=4 sw=4 et: */