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) 2011 Markus Franz Xaver Johannes Oberhumer
6   Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
7   Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
8   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
9   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
10   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
11   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
12   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
13   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
14   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
15   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
16   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
17   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
18   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
19   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
20   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
21   All Rights Reserved.
22
23   The LZO library is free software; you can redistribute it and/or
24   modify it under the terms of the GNU General Public License as
25   published by the Free Software Foundation; either version 2 of
26   the License, or (at your option) any later version.
27
28   The LZO library is distributed in the hope that it will be useful,
29   but WITHOUT ANY WARRANTY; without even the implied warranty of
30   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31   GNU General Public License for more details.
32
33   You should have received a copy of the GNU General Public License
34   along with the LZO library; see the file COPYING.
35   If not, write to the Free Software Foundation, Inc.,
36   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37
38   Markus F.X.J. Oberhumer
39   <markus@oberhumer.com>
40   http://www.oberhumer.com/opensource/lzo/
41 */
42
43
44#include "lzo1_d.ch"
45
46
47/***********************************************************************
48// decompress a block of data.
49************************************************************************/
50
51#define _NEEDBYTE   NEED_IP(1)
52#define _NEXTBYTE   (*ip++)
53
54LZO_PUBLIC(int)
55DO_DECOMPRESS    ( const lzo_bytep in , lzo_uint  in_len,
56                         lzo_bytep out, lzo_uintp out_len,
57                         lzo_voidp wrkmem )
58{
59    register lzo_bytep op;
60    register const lzo_bytep ip;
61    register const lzo_bytep m_pos;
62
63    lzo_uint t;
64    const lzo_bytep const ip_end = in + in_len;
65#if defined(HAVE_ANY_OP)
66    lzo_bytep const op_end = out + *out_len;
67#endif
68
69    lzo_uint32 b = 0;       /* bit buffer */
70    unsigned k = 0;         /* bits in bit buffer */
71
72    LZO_UNUSED(wrkmem);
73
74    op = out;
75    ip = in;
76
77    while (TEST_IP && TEST_OP)
78    {
79        NEEDBITS(1);
80        if (MASKBITS(1) == 0)
81        {
82            DUMPBITS(1);
83            /* a literal */
84            NEED_IP(1); NEED_OP(1);
85            *op++ = *ip++;
86            continue;
87        }
88        DUMPBITS(1);
89
90        NEEDBITS(1);
91        if (MASKBITS(1) == 0)
92        {
93            DUMPBITS(1);
94            /* a M1 match */
95            NEEDBITS(2);
96            t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
97            DUMPBITS(2);
98            NEED_IP(1); NEED_OP(t);
99            m_pos = op - 1 - *ip++;
100            assert(m_pos >= out); assert(m_pos < op);
101            TEST_LB(m_pos);
102            MEMCPY_DS(op,m_pos,t);
103            continue;
104        }
105        DUMPBITS(1);
106
107        NEED_IP(2);
108        t = *ip++;
109        m_pos = op;
110        m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
111        t >>= 5;
112        if (t == 0)
113        {
114#if (SWD_N >= 8192)
115            NEEDBITS(1);
116            t = MASKBITS(1);
117            DUMPBITS(1);
118            if (t == 0)
119                t = 10 - 1;
120            else
121            {
122                /* a M3 match */
123                m_pos -= 8192;      /* t << 13 */
124                t = M3_MIN_LEN - 1;
125            }
126#else
127            t = 10 - 1;
128#endif
129            NEED_IP(1);
130            while (*ip == 0)
131            {
132                t += 255;
133                ip++;
134                NEED_IP(1);
135            }
136            t += *ip++;
137        }
138        else
139        {
140#if defined(LZO_EOF_CODE)
141            if (m_pos == op)
142                goto eof_found;
143#endif
144            t += 2;
145        }
146        assert(m_pos >= out); assert(m_pos < op);
147        TEST_LB(m_pos);
148        NEED_OP(t);
149        MEMCPY_DS(op,m_pos,t);
150    }
151
152
153#if defined(LZO_EOF_CODE)
154#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
155    /* no EOF code was found */
156    *out_len = pd(op, out);
157    return LZO_E_EOF_NOT_FOUND;
158#endif
159
160eof_found:
161    assert(t == 1);
162#endif
163    *out_len = pd(op, out);
164    return (ip == ip_end ? LZO_E_OK :
165           (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
166
167
168#if defined(HAVE_NEED_IP)
169input_overrun:
170    *out_len = pd(op, out);
171    return LZO_E_INPUT_OVERRUN;
172#endif
173
174#if defined(HAVE_NEED_OP)
175output_overrun:
176    *out_len = pd(op, out);
177    return LZO_E_OUTPUT_OVERRUN;
178#endif
179
180#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
181lookbehind_overrun:
182    *out_len = pd(op, out);
183    return LZO_E_LOOKBEHIND_OVERRUN;
184#endif
185}
186
187
188/*
189vi:ts=4:et
190*/
191
192