/src/lzo-2.10/src/lzo1b_9x.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* lzo1b_9x.c -- implementation of the LZO1B-999 compression 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 "config1b.h" |
30 | | |
31 | | |
32 | | /*********************************************************************** |
33 | | // |
34 | | ************************************************************************/ |
35 | | |
36 | 705 | #define SWD_N 0xffffL /* size of ring buffer */ |
37 | 4.13M | #define SWD_THRESHOLD 2 /* lower limit for match length */ |
38 | 705 | #define SWD_F 2048 /* upper limit for match length */ |
39 | | |
40 | | |
41 | | #define LZO1B 1 |
42 | 1.41k | #define LZO_COMPRESS_T lzo1b_999_t |
43 | 705 | #define lzo_swd_t lzo1b_999_swd_t |
44 | | #include "lzo_mchw.ch" |
45 | | |
46 | | |
47 | | |
48 | | /*********************************************************************** |
49 | | // |
50 | | ************************************************************************/ |
51 | | |
52 | | static lzo_bytep |
53 | | code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off ) |
54 | 148k | { |
55 | 148k | if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) |
56 | 94.0k | { |
57 | 94.0k | assert(m_len >= M2_MIN_LEN); |
58 | 94.0k | assert(m_off >= M2_MIN_OFFSET); |
59 | | |
60 | 94.0k | m_off -= M2_MIN_OFFSET; |
61 | | /* code match len + low offset bits */ |
62 | 94.0k | *op++ = LZO_BYTE(((m_len - (M2_MIN_LEN - 2)) << M2O_BITS) | |
63 | 94.0k | (m_off & M2O_MASK)); |
64 | | /* code high offset bits */ |
65 | 94.0k | *op++ = LZO_BYTE(m_off >> M2O_BITS); |
66 | 94.0k | c->m2_m++; |
67 | 94.0k | } |
68 | 54.3k | else |
69 | 54.3k | { |
70 | 54.3k | assert(m_len >= M3_MIN_LEN); |
71 | 54.3k | assert(m_off <= M3_MAX_OFFSET); |
72 | | |
73 | 54.3k | m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET; |
74 | | /* code match len */ |
75 | 54.3k | if (m_len <= M3_MAX_LEN) |
76 | 35.7k | *op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1))); |
77 | 18.6k | else |
78 | 18.6k | { |
79 | 18.6k | assert(m_len >= M4_MIN_LEN); |
80 | | /* code M4 match len flag */ |
81 | 18.6k | *op++ = M4_MARKER; |
82 | | /* code match len */ |
83 | 18.6k | m_len -= M4_MIN_LEN - 1; |
84 | 39.4k | while (m_len > 255) |
85 | 20.8k | { |
86 | 20.8k | m_len -= 255; |
87 | 20.8k | *op++ = 0; |
88 | 20.8k | } |
89 | 18.6k | assert(m_len > 0); |
90 | 18.6k | *op++ = LZO_BYTE(m_len); |
91 | 18.6k | } |
92 | | /* code low offset bits */ |
93 | 54.3k | *op++ = LZO_BYTE(m_off & M3O_MASK); |
94 | | /* code high offset bits */ |
95 | 54.3k | *op++ = LZO_BYTE(m_off >> M3O_BITS); |
96 | | |
97 | 54.3k | c->r1_m_len = 0; |
98 | 54.3k | c->m3_m++; |
99 | 54.3k | } |
100 | 148k | return op; |
101 | 148k | } |
102 | | |
103 | | |
104 | | /*********************************************************************** |
105 | | // this is a public function, but there is no prototype in a header file |
106 | | ************************************************************************/ |
107 | | |
108 | | LZO_EXTERN(int) |
109 | | lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len, |
110 | | lzo_bytep out, lzo_uintp out_len, |
111 | | lzo_voidp wrkmem, |
112 | | lzo_callback_p cb, |
113 | | lzo_uint max_chain ); |
114 | | |
115 | | LZO_PUBLIC(int) |
116 | | lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len, |
117 | | lzo_bytep out, lzo_uintp out_len, |
118 | | lzo_voidp wrkmem, |
119 | | lzo_callback_p cb, |
120 | | lzo_uint max_chain ) |
121 | 705 | { |
122 | 705 | lzo_bytep op; |
123 | 705 | const lzo_bytep ii; |
124 | 705 | lzo_uint lit; |
125 | 705 | lzo_uint m_len, m_off; |
126 | 705 | LZO_COMPRESS_T cc; |
127 | 705 | LZO_COMPRESS_T * const c = &cc; |
128 | 705 | lzo_swd_p const swd = (lzo_swd_p) wrkmem; |
129 | 705 | int r; |
130 | | |
131 | | /* sanity check */ |
132 | 705 | LZO_COMPILE_TIME_ASSERT(LZO1B_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T) |
133 | | |
134 | 705 | c->init = 0; |
135 | 705 | c->ip = c->in = in; |
136 | 705 | c->in_end = in + in_len; |
137 | 705 | c->cb = cb; |
138 | 705 | c->r1_r = c->m3_r = c->m2_m = c->m3_m = 0; |
139 | | |
140 | 705 | op = out; |
141 | 705 | ii = c->ip; /* point to start of literal run */ |
142 | 705 | lit = 0; |
143 | 705 | c->r1_m_len = 0; |
144 | | |
145 | 705 | r = init_match(c,swd,NULL,0,0); |
146 | 705 | if (r != 0) |
147 | 0 | return r; |
148 | 705 | if (max_chain > 0) |
149 | 0 | swd->max_chain = max_chain; |
150 | | |
151 | 705 | r = find_match(c,swd,0,0); |
152 | 705 | if (r != 0) |
153 | 0 | return r; |
154 | 4.05M | while (c->look > 0) |
155 | 4.05M | { |
156 | 4.05M | int lazy_match_min_gain = -1; |
157 | 4.05M | lzo_uint ahead = 0; |
158 | | |
159 | 4.05M | m_len = c->m_len; |
160 | 4.05M | m_off = c->m_off; |
161 | | |
162 | | #if 0 |
163 | | printf("%5ld: %5d len:%3d off:%5d\n", (c->ip-c->look)-in, c->look, |
164 | | m_len, m_off); |
165 | | #endif |
166 | | |
167 | 4.05M | assert(c->ip - c->look >= in); |
168 | 4.05M | if (lit == 0) |
169 | 148k | ii = c->ip - c->look; |
170 | 4.05M | assert(ii + lit == c->ip - c->look); |
171 | 4.05M | assert(swd->b_char == *(c->ip - c->look)); |
172 | | |
173 | 4.05M | if ((m_len < M2_MIN_LEN) || |
174 | 4.05M | (m_len < M3_MIN_LEN && m_off > M2_MAX_OFFSET)) |
175 | 3.89M | { |
176 | 3.89M | m_len = 0; |
177 | 3.89M | } |
178 | 156k | else |
179 | 156k | { |
180 | 156k | assert(c->ip - c->look - m_off >= in); |
181 | 156k | assert(c->ip - c->look - m_off + m_len < c->ip); |
182 | 156k | assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off, |
183 | 156k | m_len) == 0); |
184 | | |
185 | 156k | if (lit > 0) |
186 | 90.1k | { |
187 | | /* we have a current literal run: do not try a lazy match, |
188 | | if the literal could be coded into a r1 match */ |
189 | 90.1k | if (lit == 1 && c->r1_m_len == M2_MIN_LEN) |
190 | 6.02k | lazy_match_min_gain = -1; |
191 | 84.1k | else |
192 | 84.1k | lazy_match_min_gain = 1; |
193 | | |
194 | | #if (M2_MIN_LEN == 2) |
195 | | if (m_len == 2) |
196 | | { |
197 | | /* don't code a match of len 2 if we have to |
198 | | code a literal run. Code a literal instead. */ |
199 | | m_len = 0; |
200 | | } |
201 | | #endif |
202 | | #if (M2_MIN_LEN == M3_MIN_LEN) |
203 | | if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET) |
204 | | { |
205 | | /* don't code a M3 match of len 3 if we have to |
206 | | code a literal run. Code a literal instead. */ |
207 | | m_len = 0; |
208 | | } |
209 | | #endif |
210 | 90.1k | } |
211 | 65.9k | else |
212 | 65.9k | { |
213 | | /* no current literal run: only try a lazy match, |
214 | | if the literal could be coded into a r1 match */ |
215 | 65.9k | if (c->r1_m_len == M2_MIN_LEN) |
216 | 4.84k | lazy_match_min_gain = 0; |
217 | 61.0k | else |
218 | 61.0k | lazy_match_min_gain = -1; |
219 | 65.9k | } |
220 | 156k | } |
221 | | |
222 | | |
223 | | /* try a lazy match */ |
224 | 4.05M | if (m_len == 0) |
225 | 3.89M | lazy_match_min_gain = -1; |
226 | 4.05M | if (lazy_match_min_gain >= 0 && c->look > m_len) |
227 | 88.7k | { |
228 | 88.7k | assert(m_len > 0); |
229 | | |
230 | 88.7k | r = find_match(c,swd,1,0); |
231 | 88.7k | assert(r == 0); LZO_UNUSED(r); |
232 | 88.7k | assert(c->look > 0); |
233 | | |
234 | 88.7k | if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET && |
235 | 88.7k | c->m_off > M2_MAX_OFFSET) |
236 | 2.97k | lazy_match_min_gain += 1; |
237 | | |
238 | 88.7k | if (c->m_len >= m_len + lazy_match_min_gain) |
239 | 7.60k | { |
240 | 7.60k | c->lazy++; |
241 | | #if !defined(NDEBUG) |
242 | | m_len = c->m_len; |
243 | | m_off = c->m_off; |
244 | | assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off, |
245 | | m_len) == 0); |
246 | | #endif |
247 | 7.60k | lit++; |
248 | 7.60k | assert(ii + lit == c->ip - c->look); |
249 | 7.60k | continue; |
250 | 7.60k | } |
251 | 81.1k | else |
252 | 81.1k | { |
253 | 81.1k | ahead = 1; |
254 | 81.1k | assert(ii + lit + 1 == c->ip - c->look); |
255 | 81.1k | } |
256 | 81.1k | assert(m_len > 0); |
257 | 81.1k | } |
258 | 4.04M | assert(ii + lit + ahead == c->ip - c->look); |
259 | | |
260 | | |
261 | 4.04M | if (m_len == 0) |
262 | 3.89M | { |
263 | | /* a literal */ |
264 | 3.89M | lit++; |
265 | 3.89M | r = find_match(c,swd,1,0); |
266 | 3.89M | assert(r == 0); LZO_UNUSED(r); |
267 | 3.89M | } |
268 | 148k | else |
269 | 148k | { |
270 | | /* 1 - store run */ |
271 | 148k | if (lit > 0) |
272 | 83.6k | { |
273 | | /* code current literal run */ |
274 | 83.6k | if (lit == 1 && c->r1_m_len == M2_MIN_LEN) |
275 | 6.02k | { |
276 | | /* Code a context sensitive R1 match. */ |
277 | 6.02k | assert((op[-2] >> M2O_BITS) == (M2_MARKER >> M2O_BITS)); |
278 | 6.02k | op[-2] &= M2O_MASK; |
279 | 6.02k | assert((op[-2] >> M2O_BITS) == 0); |
280 | | /* copy 1 literal */ |
281 | 6.02k | *op++ = *ii++; |
282 | 6.02k | assert(ii + ahead == c->ip - c->look); |
283 | 6.02k | c->r1_r++; |
284 | 6.02k | } |
285 | 77.6k | else |
286 | 77.6k | { |
287 | 77.6k | op = STORE_RUN(op,ii,lit); |
288 | 77.6k | } |
289 | 83.6k | if (lit < R0FAST) |
290 | 81.4k | c->r1_m_len = m_len; |
291 | 2.18k | else |
292 | 2.18k | c->r1_m_len = 0; |
293 | 83.6k | lit = 0; |
294 | 83.6k | } |
295 | 64.7k | else |
296 | 64.7k | c->r1_m_len = 0; |
297 | | |
298 | | /* 2 - code match */ |
299 | 148k | op = code_match(c,op,m_len,m_off); |
300 | 148k | r = find_match(c,swd,m_len,1+ahead); |
301 | 148k | assert(r == 0); LZO_UNUSED(r); |
302 | 148k | } |
303 | | |
304 | 4.04M | c->codesize = pd(op, out); |
305 | 4.04M | } |
306 | | |
307 | | |
308 | | /* store final run */ |
309 | 705 | if (lit > 0) |
310 | 410 | op = STORE_RUN(op,ii,lit); |
311 | | |
312 | 705 | #if defined(LZO_EOF_CODE) |
313 | 705 | *op++ = M3_MARKER | 1; |
314 | 705 | *op++ = 0; |
315 | 705 | *op++ = 0; |
316 | 705 | #endif |
317 | | |
318 | 705 | c->codesize = pd(op, out); |
319 | 705 | assert(c->textsize == in_len); |
320 | | |
321 | 705 | *out_len = pd(op, out); |
322 | | |
323 | 705 | if (c->cb && c->cb->nprogress) |
324 | 0 | (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0); |
325 | | |
326 | | #if 0 |
327 | | printf("%ld %ld -> %ld: %ld %ld %ld %ld %ld\n", |
328 | | (long) c->textsize, (long)in_len, (long) c->codesize, |
329 | | c->r1_r, c->m3_r, c->m2_m, c->m3_m, c->lazy); |
330 | | #endif |
331 | 705 | return LZO_E_OK; |
332 | 705 | } |
333 | | |
334 | | |
335 | | |
336 | | /*********************************************************************** |
337 | | // |
338 | | ************************************************************************/ |
339 | | |
340 | | LZO_PUBLIC(int) |
341 | | lzo1b_999_compress ( const lzo_bytep in , lzo_uint in_len, |
342 | | lzo_bytep out, lzo_uintp out_len, |
343 | | lzo_voidp wrkmem ) |
344 | 705 | { |
345 | 705 | return lzo1b_999_compress_callback(in,in_len,out,out_len,wrkmem, |
346 | 705 | (lzo_callback_p) 0, 0); |
347 | 705 | } |
348 | | |
349 | | |
350 | | /* vim:set ts=4 sw=4 et: */ |