Coverage Report

Created: 2026-06-08 06:19

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.31k
{
41
1.31k
    lzo_bytep op;
42
1.31k
    const lzo_bytep ip;
43
1.31k
    lzo_uint t;
44
1.31k
    const lzo_bytep m_pos;
45
46
1.31k
    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.31k
    LZO_UNUSED(wrkmem);
52
53
1.31k
    *out_len = 0;
54
55
1.31k
    op = out;
56
1.31k
    ip = in;
57
58
127k
    while (TEST_IP_AND_TEST_OP)
59
127k
    {
60
127k
        t = *ip++;
61
127k
        if (t > 31)
62
85.1k
            goto match;
63
64
        /* a literal run */
65
42.3k
        if (t == 0)
66
12.4k
        {
67
12.4k
            NEED_IP(1);
68
70.9k
            while (*ip == 0)
69
58.5k
            {
70
58.5k
                t += 255;
71
58.5k
                ip++;
72
58.5k
                TEST_IV(t);
73
58.5k
                NEED_IP(1);
74
58.5k
            }
75
12.4k
            t += 31 + *ip++;
76
12.4k
        }
77
        /* copy literals */
78
42.3k
        assert(t > 0); NEED_OP(t); NEED_IP(t+1);
79
42.3k
#if (LZO_OPT_UNALIGNED32)
80
42.3k
        if (t >= 4)
81
41.8k
        {
82
4.13M
            do {
83
4.13M
                UA_COPY4(op, ip);
84
4.13M
                op += 4; ip += 4; t -= 4;
85
4.13M
            } while (t >= 4);
86
54.9k
            if (t > 0) do *op++ = *ip++; while (--t > 0);
87
41.8k
        }
88
555
        else
89
555
#endif
90
905
        do *op++ = *ip++; while (--t > 0);
91
92
42.3k
        t = *ip++;
93
94
117k
        while (TEST_IP_AND_TEST_OP)
95
117k
        {
96
            /* handle matches */
97
117k
            if (t < 32)
98
5.51k
            {
99
5.51k
                m_pos = op - 1 - 0x800;
100
5.51k
                m_pos -= (t >> 2) & 7;
101
5.51k
                m_pos -= *ip++ << 3;
102
5.51k
                TEST_LB(m_pos); NEED_OP(3);
103
5.51k
                *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
104
5.51k
            }
105
111k
            else
106
111k
            {
107
196k
match:
108
196k
                if (t < M3_MARKER)
109
106k
                {
110
106k
                    m_pos = op - 1;
111
106k
                    m_pos -= (t >> 2) & 7;
112
106k
                    m_pos -= *ip++ << 3;
113
106k
                    t >>= 5;
114
106k
                    TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
115
106k
                    goto copy_match;
116
106k
                }
117
89.9k
                else
118
89.9k
                {
119
89.9k
                    t &= 31;
120
89.9k
                    if (t == 0)
121
28.4k
                    {
122
28.4k
                        NEED_IP(1);
123
103k
                        while (*ip == 0)
124
74.5k
                        {
125
74.5k
                            t += 255;
126
74.5k
                            ip++;
127
74.5k
                            TEST_OV(t);
128
74.5k
                            NEED_IP(1);
129
74.5k
                        }
130
28.4k
                        t += 31 + *ip++;
131
28.4k
                    }
132
89.9k
                    NEED_IP(2);
133
89.9k
                    m_pos = op;
134
89.9k
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
135
89.9k
                    m_pos -= UA_GET_LE16(ip) >> 2;
136
89.9k
                    ip += 2;
137
#else
138
                    m_pos -= *ip++ >> 2;
139
                    m_pos -= *ip++ << 6;
140
#endif
141
89.9k
                    if (m_pos == op)
142
1.31k
                        goto eof_found;
143
89.9k
                }
144
145
                /* copy match */
146
88.6k
                TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
147
88.6k
#if (LZO_OPT_UNALIGNED32)
148
88.6k
                if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
149
53.5k
                {
150
53.5k
                    UA_COPY4(op, m_pos);
151
53.5k
                    op += 4; m_pos += 4; t -= 4 - (3 - 1);
152
2.56M
                    do {
153
2.56M
                        UA_COPY4(op, m_pos);
154
2.56M
                        op += 4; m_pos += 4; t -= 4;
155
2.56M
                    } while (t >= 4);
156
62.9k
                    if (t > 0) do *op++ = *m_pos++; while (--t > 0);
157
53.5k
                }
158
35.1k
                else
159
35.1k
#endif
160
35.1k
                {
161
142k
copy_match:
162
142k
                *op++ = *m_pos++; *op++ = *m_pos++;
163
11.4M
                do *op++ = *m_pos++; while (--t > 0);
164
142k
                }
165
88.6k
            }
166
201k
            t = ip[-2] & 3;
167
201k
            if (t == 0)
168
126k
                break;
169
170
            /* copy literals */
171
201k
            assert(t > 0); NEED_OP(t); NEED_IP(t+1);
172
111k
            do *op++ = *ip++; while (--t > 0);
173
74.9k
            t = *ip++;
174
74.9k
        }
175
42.3k
    }
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.31k
eof_found:
184
1.31k
    assert(t == 1);
185
1.31k
    *out_len = pd(op, out);
186
1.31k
    return (ip == ip_end ? LZO_E_OK :
187
1.31k
           (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.31k
}
208
209
210
/* vim:set ts=4 sw=4 et: */