/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 | 411 | { |
41 | 411 | lzo_bytep op; |
42 | 411 | const lzo_bytep ip; |
43 | 411 | lzo_uint t; |
44 | 411 | const lzo_bytep m_pos; |
45 | | |
46 | 411 | const lzo_bytep const ip_end = in + in_len; |
47 | 411 | #if defined(HAVE_ANY_OP) |
48 | 411 | lzo_bytep const op_end = out + *out_len; |
49 | 411 | #endif |
50 | | |
51 | 411 | LZO_UNUSED(wrkmem); |
52 | | |
53 | 411 | *out_len = 0; |
54 | | |
55 | 411 | op = out; |
56 | 411 | ip = in; |
57 | | |
58 | 411 | while (TEST_IP_AND_TEST_OP) |
59 | 19.0k | { |
60 | 19.0k | t = *ip++; |
61 | 19.0k | if (t > 31) |
62 | 14.9k | goto match; |
63 | | |
64 | | /* a literal run */ |
65 | 4.08k | if (t == 0) |
66 | 851 | { |
67 | 851 | NEED_IP(1); |
68 | 1.22M | while (*ip == 0) |
69 | 1.22M | { |
70 | 1.22M | t += 255; |
71 | 1.22M | ip++; |
72 | 1.22M | TEST_IV(t); |
73 | 1.22M | NEED_IP(1); |
74 | 1.22M | } |
75 | 827 | t += 31 + *ip++; |
76 | 827 | } |
77 | | /* copy literals */ |
78 | 4.06k | assert(t > 0); NEED_OP(t); NEED_IP(t+1); |
79 | 3.96k | #if (LZO_OPT_UNALIGNED32) |
80 | 3.96k | if (t >= 4) |
81 | 2.86k | { |
82 | 237k | do { |
83 | 237k | UA_COPY4(op, ip); |
84 | 237k | op += 4; ip += 4; t -= 4; |
85 | 237k | } while (t >= 4); |
86 | 3.33k | if (t > 0) do *op++ = *ip++; while (--t > 0); |
87 | 2.86k | } |
88 | 1.09k | else |
89 | 1.09k | #endif |
90 | 2.19k | do *op++ = *ip++; while (--t > 0); |
91 | | |
92 | 3.96k | t = *ip++; |
93 | | |
94 | 3.96k | while (TEST_IP_AND_TEST_OP) |
95 | 27.8k | { |
96 | | /* handle matches */ |
97 | 27.8k | if (t < 32) |
98 | 7.09k | { |
99 | 7.09k | m_pos = op - 1 - 0x800; |
100 | 7.09k | m_pos -= (t >> 2) & 7; |
101 | 7.09k | m_pos -= *ip++ << 3; |
102 | 7.09k | TEST_LB(m_pos); NEED_OP(3); |
103 | 7.03k | *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++; |
104 | 7.03k | } |
105 | 20.7k | else |
106 | 20.7k | { |
107 | 35.7k | match: |
108 | 35.7k | if (t < M3_MARKER) |
109 | 31.2k | { |
110 | 31.2k | m_pos = op - 1; |
111 | 31.2k | m_pos -= (t >> 2) & 7; |
112 | 31.2k | m_pos -= *ip++ << 3; |
113 | 31.2k | t >>= 5; |
114 | 31.2k | TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1); |
115 | 31.2k | goto copy_match; |
116 | 31.2k | } |
117 | 4.44k | else |
118 | 4.44k | { |
119 | 4.44k | t &= 31; |
120 | 4.44k | if (t == 0) |
121 | 904 | { |
122 | 904 | NEED_IP(1); |
123 | 1.91M | while (*ip == 0) |
124 | 1.91M | { |
125 | 1.91M | t += 255; |
126 | 1.91M | ip++; |
127 | 1.91M | TEST_OV(t); |
128 | 1.91M | NEED_IP(1); |
129 | 1.91M | } |
130 | 877 | t += 31 + *ip++; |
131 | 877 | } |
132 | 4.41k | NEED_IP(2); |
133 | 4.38k | m_pos = op; |
134 | 4.38k | #if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN) |
135 | 4.38k | m_pos -= UA_GET_LE16(ip) >> 2; |
136 | 4.38k | ip += 2; |
137 | | #else |
138 | | m_pos -= *ip++ >> 2; |
139 | | m_pos -= *ip++ << 6; |
140 | | #endif |
141 | 4.38k | if (m_pos == op) |
142 | 12 | goto eof_found; |
143 | 4.38k | } |
144 | | |
145 | | /* copy match */ |
146 | 4.37k | TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1); |
147 | 4.33k | #if (LZO_OPT_UNALIGNED32) |
148 | 4.33k | if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) |
149 | 2.97k | { |
150 | 2.97k | UA_COPY4(op, m_pos); |
151 | 2.97k | op += 4; m_pos += 4; t -= 4 - (3 - 1); |
152 | 615k | do { |
153 | 615k | UA_COPY4(op, m_pos); |
154 | 615k | op += 4; m_pos += 4; t -= 4; |
155 | 615k | } while (t >= 4); |
156 | 3.90k | if (t > 0) do *op++ = *m_pos++; while (--t > 0); |
157 | 2.97k | } |
158 | 1.36k | else |
159 | 1.36k | #endif |
160 | 1.36k | { |
161 | 32.6k | copy_match: |
162 | 32.6k | *op++ = *m_pos++; *op++ = *m_pos++; |
163 | 4.90M | do *op++ = *m_pos++; while (--t > 0); |
164 | 32.6k | } |
165 | 4.33k | } |
166 | 42.6k | t = ip[-2] & 3; |
167 | 42.6k | if (t == 0) |
168 | 18.6k | break; |
169 | | |
170 | | /* copy literals */ |
171 | 23.9k | assert(t > 0); NEED_OP(t); NEED_IP(t+1); |
172 | 41.5k | do *op++ = *ip++; while (--t > 0); |
173 | 23.9k | t = *ip++; |
174 | 23.9k | } |
175 | 3.96k | } |
176 | | |
177 | 44 | #if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP) |
178 | | /* no EOF code was found */ |
179 | 44 | *out_len = pd(op, out); |
180 | 44 | return LZO_E_EOF_NOT_FOUND; |
181 | 0 | #endif |
182 | | |
183 | 12 | eof_found: |
184 | 12 | assert(t == 1); |
185 | 12 | *out_len = pd(op, out); |
186 | 12 | return (ip == ip_end ? LZO_E_OK : |
187 | 12 | (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN)); |
188 | | |
189 | | |
190 | 0 | #if defined(HAVE_NEED_IP) |
191 | 217 | input_overrun: |
192 | 217 | *out_len = pd(op, out); |
193 | 217 | return LZO_E_INPUT_OVERRUN; |
194 | 0 | #endif |
195 | | |
196 | 0 | #if defined(HAVE_NEED_OP) |
197 | 28 | output_overrun: |
198 | 28 | *out_len = pd(op, out); |
199 | 28 | return LZO_E_OUTPUT_OVERRUN; |
200 | 0 | #endif |
201 | | |
202 | 0 | #if defined(LZO_TEST_OVERRUN_LOOKBEHIND) |
203 | 110 | lookbehind_overrun: |
204 | 110 | *out_len = pd(op, out); |
205 | 110 | return LZO_E_LOOKBEHIND_OVERRUN; |
206 | 411 | #endif |
207 | 411 | } |
208 | | |
209 | | |
210 | | /* vim:set ts=4 sw=4 et: */ |