summaryrefslogtreecommitdiff
path: root/src/lzo1f_d.ch
blob: b22916e5924d6082f4d5db67c1b03d7ba9dee14f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* lzo1f_d.ch -- implementation of the LZO1F decompression algorithm

   This file is part of the LZO real-time data compression library.

   Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer
   All Rights Reserved.

   The LZO library is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   The LZO library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the LZO library; see the file COPYING.
   If not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

   Markus F.X.J. Oberhumer
   <markus@oberhumer.com>
   http://www.oberhumer.com/opensource/lzo/
 */


#include "lzo1_d.ch"


/***********************************************************************
// decompress a block of data.
************************************************************************/

LZO_PUBLIC(int)
DO_DECOMPRESS  ( const lzo_bytep in , lzo_uint  in_len,
                       lzo_bytep out, lzo_uintp out_len,
                       lzo_voidp wrkmem )
{
    lzo_bytep op;
    const lzo_bytep ip;
    lzo_uint t;
    const lzo_bytep m_pos;

    const lzo_bytep const ip_end = in + in_len;
#if defined(HAVE_ANY_OP)
    lzo_bytep const op_end = out + *out_len;
#endif

    LZO_UNUSED(wrkmem);

    *out_len = 0;

    op = out;
    ip = in;

    while (TEST_IP_AND_TEST_OP)
    {
        t = *ip++;
        if (t > 31)
            goto match;

        /* a literal run */
        if (t == 0)
        {
            NEED_IP(1);
            while (*ip == 0)
            {
                t += 255;
                ip++;
                TEST_IV(t);
                NEED_IP(1);
            }
            t += 31 + *ip++;
        }
        /* copy literals */
        assert(t > 0); NEED_OP(t); NEED_IP(t+1);
#if (LZO_OPT_UNALIGNED32)
        if (t >= 4)
        {
            do {
                UA_COPY4(op, ip);
                op += 4; ip += 4; t -= 4;
            } while (t >= 4);
            if (t > 0) do *op++ = *ip++; while (--t > 0);
        }
        else
#endif
        do *op++ = *ip++; while (--t > 0);

        t = *ip++;

        while (TEST_IP_AND_TEST_OP)
        {
            /* handle matches */
            if (t < 32)
            {
                m_pos = op - 1 - 0x800;
                m_pos -= (t >> 2) & 7;
                m_pos -= *ip++ << 3;
                TEST_LB(m_pos); NEED_OP(3);
                *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
            }
            else
            {
match:
                if (t < M3_MARKER)
                {
                    m_pos = op - 1;
                    m_pos -= (t >> 2) & 7;
                    m_pos -= *ip++ << 3;
                    t >>= 5;
                    TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
                    goto copy_match;
                }
                else
                {
                    t &= 31;
                    if (t == 0)
                    {
                        NEED_IP(1);
                        while (*ip == 0)
                        {
                            t += 255;
                            ip++;
                            TEST_OV(t);
                            NEED_IP(1);
                        }
                        t += 31 + *ip++;
                    }
                    NEED_IP(2);
                    m_pos = op;
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
                    m_pos -= UA_GET_LE16(ip) >> 2;
                    ip += 2;
#else
                    m_pos -= *ip++ >> 2;
                    m_pos -= *ip++ << 6;
#endif
                    if (m_pos == op)
                        goto eof_found;
                }

                /* copy match */
                TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
#if (LZO_OPT_UNALIGNED32)
                if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
                {
                    UA_COPY4(op, m_pos);
                    op += 4; m_pos += 4; t -= 4 - (3 - 1);
                    do {
                        UA_COPY4(op, m_pos);
                        op += 4; m_pos += 4; t -= 4;
                    } while (t >= 4);
                    if (t > 0) do *op++ = *m_pos++; while (--t > 0);
                }
                else
#endif
                {
copy_match:
                *op++ = *m_pos++; *op++ = *m_pos++;
                do *op++ = *m_pos++; while (--t > 0);
                }
            }
            t = ip[-2] & 3;
            if (t == 0)
                break;

            /* copy literals */
            assert(t > 0); NEED_OP(t); NEED_IP(t+1);
            do *op++ = *ip++; while (--t > 0);
            t = *ip++;
        }
    }

#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
    /* no EOF code was found */
    *out_len = pd(op, out);
    return LZO_E_EOF_NOT_FOUND;
#endif

eof_found:
    assert(t == 1);
    *out_len = pd(op, out);
    return (ip == ip_end ? LZO_E_OK :
           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));


#if defined(HAVE_NEED_IP)
input_overrun:
    *out_len = pd(op, out);
    return LZO_E_INPUT_OVERRUN;
#endif

#if defined(HAVE_NEED_OP)
output_overrun:
    *out_len = pd(op, out);
    return LZO_E_OUTPUT_OVERRUN;
#endif

#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
lookbehind_overrun:
    *out_len = pd(op, out);
    return LZO_E_LOOKBEHIND_OVERRUN;
#endif
}


/*
vi:ts=4:et
*/