/src/lzo-2.10/src/lzo1f_9x.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* lzo1f_9x.c -- implementation of the LZO1F-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 "config1f.h" |
30 | | |
31 | | |
32 | | /*********************************************************************** |
33 | | // |
34 | | ************************************************************************/ |
35 | | |
36 | 896 | #define SWD_N 16383 /* size of ring buffer */ |
37 | 5.82M | #define SWD_THRESHOLD 2 /* lower limit for match length */ |
38 | 896 | #define SWD_F 2048 /* upper limit for match length */ |
39 | | |
40 | | |
41 | | #define LZO1F 1 |
42 | 1.79k | #define LZO_COMPRESS_T lzo1f_999_t |
43 | 896 | #define lzo_swd_t lzo1f_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 | 90.8k | { |
55 | 90.8k | if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) |
56 | 47.1k | { |
57 | 47.1k | m_off -= 1; |
58 | 47.1k | *op++ = LZO_BYTE(((m_len - 2) << 5) | ((m_off & 7) << 2)); |
59 | 47.1k | *op++ = LZO_BYTE(m_off >> 3); |
60 | 47.1k | c->m2_m++; |
61 | 47.1k | } |
62 | 43.7k | else if (m_len == M2_MIN_LEN && m_off <= 2 * M2_MAX_OFFSET && |
63 | 43.7k | c->r1_lit > 0) |
64 | 2.78k | { |
65 | 2.78k | assert(m_off > M2_MAX_OFFSET); |
66 | 2.78k | m_off -= 1 + M2_MAX_OFFSET; |
67 | 2.78k | *op++ = LZO_BYTE(((m_off & 7) << 2)); |
68 | 2.78k | *op++ = LZO_BYTE(m_off >> 3); |
69 | 2.78k | c->r1_r++; |
70 | 2.78k | } |
71 | 40.9k | else |
72 | 40.9k | { |
73 | 40.9k | if (m_len <= M3_MAX_LEN) |
74 | 28.9k | *op++ = LZO_BYTE(M3_MARKER | (m_len - 2)); |
75 | 11.9k | else |
76 | 11.9k | { |
77 | 11.9k | m_len -= M3_MAX_LEN; |
78 | 11.9k | *op++ = M3_MARKER | 0; |
79 | 29.5k | while (m_len > 255) |
80 | 17.5k | { |
81 | 17.5k | m_len -= 255; |
82 | 17.5k | *op++ = 0; |
83 | 17.5k | } |
84 | 11.9k | assert(m_len > 0); |
85 | 11.9k | *op++ = LZO_BYTE(m_len); |
86 | 11.9k | } |
87 | 40.9k | *op++ = LZO_BYTE((m_off & 63) << 2); |
88 | 40.9k | *op++ = LZO_BYTE(m_off >> 6); |
89 | 40.9k | c->m3_m++; |
90 | 40.9k | } |
91 | | |
92 | 90.8k | return op; |
93 | 90.8k | } |
94 | | |
95 | | |
96 | | static lzo_bytep |
97 | | STORE_RUN ( lzo_bytep op, const lzo_bytep ii, lzo_uint t, lzo_bytep out ) |
98 | 61.1k | { |
99 | 61.1k | if (t < 4 && op > out) |
100 | 36.2k | op[-2] = LZO_BYTE(op[-2] | t); |
101 | 24.9k | else if (t <= 31) |
102 | 18.0k | *op++ = LZO_BYTE(t); |
103 | 6.90k | else |
104 | 6.90k | { |
105 | 6.90k | lzo_uint tt = t - 31; |
106 | | |
107 | 6.90k | *op++ = 0; |
108 | 25.2k | while (tt > 255) |
109 | 18.3k | { |
110 | 18.3k | tt -= 255; |
111 | 18.3k | *op++ = 0; |
112 | 18.3k | } |
113 | 6.90k | assert(tt > 0); |
114 | 6.90k | *op++ = LZO_BYTE(tt); |
115 | 6.90k | } |
116 | 5.64M | do *op++ = *ii++; while (--t > 0); |
117 | | |
118 | 61.1k | return op; |
119 | 61.1k | } |
120 | | |
121 | | |
122 | | /*********************************************************************** |
123 | | // this is a public function, but there is no prototype in a header file |
124 | | ************************************************************************/ |
125 | | |
126 | | LZO_EXTERN(int) |
127 | | lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len, |
128 | | lzo_bytep out, lzo_uintp out_len, |
129 | | lzo_voidp wrkmem, |
130 | | lzo_callback_p cb, |
131 | | lzo_uint max_chain ); |
132 | | |
133 | | LZO_PUBLIC(int) |
134 | | lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len, |
135 | | lzo_bytep out, lzo_uintp out_len, |
136 | | lzo_voidp wrkmem, |
137 | | lzo_callback_p cb, |
138 | | lzo_uint max_chain ) |
139 | 896 | { |
140 | 896 | lzo_bytep op; |
141 | 896 | const lzo_bytep ii; |
142 | 896 | lzo_uint lit; |
143 | 896 | lzo_uint m_len, m_off; |
144 | 896 | LZO_COMPRESS_T cc; |
145 | 896 | LZO_COMPRESS_T * const c = &cc; |
146 | 896 | lzo_swd_p const swd = (lzo_swd_p) wrkmem; |
147 | 896 | int r; |
148 | | |
149 | | /* sanity check */ |
150 | 896 | LZO_COMPILE_TIME_ASSERT(LZO1F_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T) |
151 | | |
152 | 896 | c->init = 0; |
153 | 896 | c->ip = c->in = in; |
154 | 896 | c->in_end = in + in_len; |
155 | 896 | c->cb = cb; |
156 | 896 | c->r1_r = c->m2_m = c->m3_m = 0; |
157 | | |
158 | 896 | op = out; |
159 | 896 | ii = c->ip; /* point to start of literal run */ |
160 | 896 | lit = 0; |
161 | 896 | c->r1_lit = c->r1_m_len = 0; |
162 | | |
163 | 896 | r = init_match(c,swd,NULL,0,0); |
164 | 896 | if (r != 0) |
165 | 0 | return r; |
166 | 896 | if (max_chain > 0) |
167 | 0 | swd->max_chain = max_chain; |
168 | | |
169 | 896 | r = find_match(c,swd,0,0); |
170 | 896 | if (r != 0) |
171 | 0 | return r; |
172 | 5.73M | while (c->look > 0) |
173 | 5.73M | { |
174 | 5.73M | int lazy_match_min_gain = -1; |
175 | 5.73M | lzo_uint ahead = 0; |
176 | | |
177 | 5.73M | m_len = c->m_len; |
178 | 5.73M | m_off = c->m_off; |
179 | | |
180 | 5.73M | assert(c->ip - c->look >= in); |
181 | 5.73M | if (lit == 0) |
182 | 91.4k | ii = c->ip - c->look; |
183 | 5.73M | assert(ii + lit == c->ip - c->look); |
184 | 5.73M | assert(swd->b_char == *(c->ip - c->look)); |
185 | | |
186 | 5.73M | if ((m_len < M2_MIN_LEN) || |
187 | 5.73M | (m_len < M3_MIN_LEN && m_off > M2_MAX_OFFSET)) |
188 | 5.63M | { |
189 | 5.63M | m_len = 0; |
190 | 5.63M | } |
191 | 103k | else |
192 | 103k | { |
193 | 103k | assert(c->ip - c->look - m_off >= in); |
194 | 103k | assert(c->ip - c->look - m_off + m_len < c->ip); |
195 | 103k | assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off, |
196 | 103k | m_len) == 0); |
197 | | |
198 | 103k | if (lit < 3) |
199 | 70.7k | lazy_match_min_gain = 1; |
200 | 32.8k | else if (lit == 3) |
201 | 6.52k | lazy_match_min_gain = 3; |
202 | 26.3k | else if (lit == 31) |
203 | 349 | lazy_match_min_gain = 3; |
204 | 25.9k | else |
205 | 25.9k | lazy_match_min_gain = 1; |
206 | 103k | } |
207 | | |
208 | | /* try a lazy match */ |
209 | 5.73M | if (m_len > 0 && lazy_match_min_gain >= 0 && c->look > m_len) |
210 | 103k | { |
211 | 103k | r = find_match(c,swd,1,0); |
212 | 103k | assert(r == 0); LZO_UNUSED(r); |
213 | 103k | assert(c->look > 0); |
214 | | |
215 | 103k | if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET && |
216 | 103k | c->m_off > M2_MAX_OFFSET) |
217 | 2.30k | { |
218 | 2.30k | lazy_match_min_gain += 1; |
219 | 2.30k | } |
220 | 100k | else if (c->m_len <= M2_MAX_LEN && |
221 | 100k | c->m_off <= M2_MAX_OFFSET && |
222 | 100k | m_off > M2_MAX_OFFSET) |
223 | 10.7k | { |
224 | 10.7k | if (lazy_match_min_gain > 0) |
225 | 10.7k | lazy_match_min_gain -= 1; |
226 | 10.7k | } |
227 | 90.1k | else if (m_len == M2_MIN_LEN && c->m_len == M2_MIN_LEN && |
228 | 90.1k | c->m_off <= 2 * M2_MAX_OFFSET && |
229 | 90.1k | m_off > M2_MAX_OFFSET) |
230 | 490 | { |
231 | 490 | if (lazy_match_min_gain > 0) |
232 | 490 | lazy_match_min_gain -= 1; |
233 | 490 | } |
234 | | |
235 | 103k | if (c->m_len >= m_len + lazy_match_min_gain) |
236 | 12.6k | { |
237 | 12.6k | c->lazy++; |
238 | | #if !defined(NDEBUG) |
239 | | m_len = c->m_len; |
240 | | m_off = c->m_off; |
241 | | assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off, |
242 | | m_len) == 0); |
243 | | #endif |
244 | 12.6k | lit++; |
245 | 12.6k | assert(ii + lit == c->ip - c->look); |
246 | 12.6k | continue; |
247 | 12.6k | } |
248 | 90.5k | else |
249 | 90.5k | { |
250 | 90.5k | ahead = 1; |
251 | 90.5k | assert(ii + lit + 1 == c->ip - c->look); |
252 | 90.5k | } |
253 | 90.5k | assert(m_len > 0); |
254 | 90.5k | } |
255 | 5.72M | assert(ii + lit + ahead == c->ip - c->look); |
256 | | |
257 | | |
258 | 5.72M | if (m_len == 0) |
259 | 5.63M | { |
260 | | /* a literal */ |
261 | 5.63M | lit++; |
262 | 5.63M | r = find_match(c,swd,1,0); |
263 | 5.63M | assert(r == 0); LZO_UNUSED(r); |
264 | 5.63M | } |
265 | 90.8k | else |
266 | 90.8k | { |
267 | | /* 1 - store run */ |
268 | 90.8k | if (lit > 0) |
269 | 60.6k | { |
270 | 60.6k | op = STORE_RUN(op,ii,lit,out); |
271 | 60.6k | c->r1_m_len = m_len; |
272 | 60.6k | c->r1_lit = lit; |
273 | 60.6k | lit = 0; |
274 | 60.6k | } |
275 | 30.2k | else |
276 | 30.2k | c->r1_lit = c->r1_m_len = 0; |
277 | | |
278 | | /* 2 - code match */ |
279 | 90.8k | op = code_match(c,op,m_len,m_off); |
280 | 90.8k | r = find_match(c,swd,m_len,1+ahead); |
281 | 90.8k | assert(r == 0); LZO_UNUSED(r); |
282 | 90.8k | } |
283 | | |
284 | 5.72M | c->codesize = pd(op, out); |
285 | 5.72M | } |
286 | | |
287 | | |
288 | | /* store final run */ |
289 | 896 | if (lit > 0) |
290 | 529 | op = STORE_RUN(op,ii,lit,out); |
291 | | |
292 | 896 | #if defined(LZO_EOF_CODE) |
293 | 896 | *op++ = M3_MARKER | 1; |
294 | 896 | *op++ = 0; |
295 | 896 | *op++ = 0; |
296 | 896 | #endif |
297 | | |
298 | 896 | c->codesize = pd(op, out); |
299 | 896 | assert(c->textsize == in_len); |
300 | | |
301 | 896 | *out_len = pd(op, out); |
302 | | |
303 | 896 | if (c->cb && c->cb->nprogress) |
304 | 0 | (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0); |
305 | | |
306 | | #if 0 |
307 | | printf("%ld %ld -> %ld: %ld %ld %ld %ld\n", |
308 | | (long) c->textsize, (long)in_len, (long) c->codesize, |
309 | | c->r1_r, c->m2_m, c->m3_m, c->lazy); |
310 | | #endif |
311 | 896 | return LZO_E_OK; |
312 | 896 | } |
313 | | |
314 | | |
315 | | |
316 | | /*********************************************************************** |
317 | | // |
318 | | ************************************************************************/ |
319 | | |
320 | | LZO_PUBLIC(int) |
321 | | lzo1f_999_compress ( const lzo_bytep in , lzo_uint in_len, |
322 | | lzo_bytep out, lzo_uintp out_len, |
323 | | lzo_voidp wrkmem ) |
324 | 896 | { |
325 | 896 | return lzo1f_999_compress_callback(in,in_len,out,out_len,wrkmem, |
326 | 896 | (lzo_callback_p) 0, 0); |
327 | 896 | } |
328 | | |
329 | | |
330 | | /* vim:set ts=4 sw=4 et: */ |