Coverage Report

Created: 2026-09-03 06:55

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
68.5k
#define _NEEDBYTE   NEED_IP(1)
37
68.5k
#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
193
{
44
193
    lzo_bytep op;
45
193
    const lzo_bytep ip;
46
193
    const lzo_bytep m_pos;
47
48
193
    lzo_uint t;
49
193
    const lzo_bytep const ip_end = in + in_len;
50
193
#if defined(HAVE_ANY_OP)
51
193
    lzo_bytep const op_end = out + *out_len;
52
193
#endif
53
54
193
    lzo_uint32_t b = 0;     /* bit buffer */
55
193
    unsigned k = 0;         /* bits in bit buffer */
56
57
193
    LZO_UNUSED(wrkmem);
58
59
193
    op = out;
60
193
    ip = in;
61
62
193
    while (TEST_IP_AND_TEST_OP)
63
536k
    {
64
536k
        NEEDBITS(1);
65
536k
        if (MASKBITS(1) == 0)
66
532k
        {
67
532k
            DUMPBITS(1);
68
            /* a literal */
69
532k
            NEED_IP(1); NEED_OP(1);
70
532k
            *op++ = *ip++;
71
532k
            continue;
72
532k
        }
73
4.56k
        DUMPBITS(1);
74
75
4.56k
        NEEDBITS(1);
76
4.56k
        if (MASKBITS(1) == 0)
77
3.00k
        {
78
3.00k
            DUMPBITS(1);
79
            /* a M1 match */
80
3.00k
            NEEDBITS(2);
81
3.00k
            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
82
3.00k
            DUMPBITS(2);
83
3.00k
            NEED_IP(1); NEED_OP(t);
84
2.99k
            m_pos = op - 1 - *ip++;
85
2.99k
            assert(m_pos >= out); assert(m_pos < op);
86
2.99k
            TEST_LB(m_pos);
87
2.98k
            MEMCPY_DS(op,m_pos,t);
88
2.98k
            continue;
89
2.99k
        }
90
1.56k
        DUMPBITS(1);
91
92
1.56k
        NEED_IP(2);
93
1.54k
        t = *ip++;
94
1.54k
        m_pos = op;
95
1.54k
        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
96
1.54k
        t >>= 5;
97
1.54k
        if (t == 0)
98
679
        {
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
679
            t = 10 - 1;
113
679
#endif
114
679
            NEED_IP(1);
115
1.72M
            while (*ip == 0)
116
1.72M
            {
117
1.72M
                t += 255;
118
1.72M
                ip++;
119
1.72M
                TEST_OV(t);
120
1.72M
                NEED_IP(1);
121
1.72M
            }
122
661
            t += *ip++;
123
661
        }
124
870
        else
125
870
        {
126
870
#if defined(LZO_EOF_CODE)
127
870
            if (m_pos == op)
128
5
                goto eof_found;
129
865
#endif
130
865
            t += 2;
131
865
        }
132
1.54k
        assert(m_pos >= out); assert(m_pos < op);
133
1.52k
        TEST_LB(m_pos);
134
1.46k
        NEED_OP(t);
135
1.45k
        MEMCPY_DS(op,m_pos,t);
136
1.45k
    }
137
138
139
56
#if defined(LZO_EOF_CODE)
140
56
#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
141
    /* no EOF code was found */
142
56
    *out_len = pd(op, out);
143
56
    return LZO_E_EOF_NOT_FOUND;
144
0
#endif
145
146
5
eof_found:
147
5
    assert(t == 1);
148
5
#endif
149
5
    *out_len = pd(op, out);
150
5
    return (ip == ip_end ? LZO_E_OK :
151
5
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
152
153
154
0
#if defined(HAVE_NEED_IP)
155
46
input_overrun:
156
46
    *out_len = pd(op, out);
157
46
    return LZO_E_INPUT_OVERRUN;
158
0
#endif
159
160
0
#if defined(HAVE_NEED_OP)
161
14
output_overrun:
162
14
    *out_len = pd(op, out);
163
14
    return LZO_E_OUTPUT_OVERRUN;
164
0
#endif
165
166
0
#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
167
72
lookbehind_overrun:
168
72
    *out_len = pd(op, out);
169
72
    return LZO_E_LOOKBEHIND_OVERRUN;
170
193
#endif
171
193
}
172
173
174
/* vim:set ts=4 sw=4 et: */