lib/lzo: separate lzo-rle from lzo
[muen/linux.git] / lib / lzo / lzo1x_compress.c
1 /*
2  *  LZO1X Compressor from LZO
3  *
4  *  Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <markus@oberhumer.com>
5  *
6  *  The full LZO package can be found at:
7  *  http://www.oberhumer.com/opensource/lzo/
8  *
9  *  Changed for Linux kernel use by:
10  *  Nitin Gupta <nitingupta910@gmail.com>
11  *  Richard Purdie <rpurdie@openedhand.com>
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <asm/unaligned.h>
17 #include <linux/lzo.h>
18 #include "lzodefs.h"
19
20 static noinline size_t
21 lzo1x_1_do_compress(const unsigned char *in, size_t in_len,
22                     unsigned char *out, size_t *out_len,
23                     size_t ti, void *wrkmem, signed char *state_offset,
24                     const unsigned char bitstream_version)
25 {
26         const unsigned char *ip;
27         unsigned char *op;
28         const unsigned char * const in_end = in + in_len;
29         const unsigned char * const ip_end = in + in_len - 20;
30         const unsigned char *ii;
31         lzo_dict_t * const dict = (lzo_dict_t *) wrkmem;
32
33         op = out;
34         ip = in;
35         ii = ip;
36         ip += ti < 4 ? 4 - ti : 0;
37
38         for (;;) {
39                 const unsigned char *m_pos = NULL;
40                 size_t t, m_len, m_off;
41                 u32 dv;
42                 u32 run_length = 0;
43 literal:
44                 ip += 1 + ((ip - ii) >> 5);
45 next:
46                 if (unlikely(ip >= ip_end))
47                         break;
48                 dv = get_unaligned_le32(ip);
49
50                 if (dv == 0 && bitstream_version) {
51                         const unsigned char *ir = ip + 4;
52                         const unsigned char *limit = ip_end
53                                 < (ip + MAX_ZERO_RUN_LENGTH + 1)
54                                 ? ip_end : ip + MAX_ZERO_RUN_LENGTH + 1;
55 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \
56         defined(LZO_FAST_64BIT_MEMORY_ACCESS)
57                         u64 dv64;
58
59                         for (; (ir + 32) <= limit; ir += 32) {
60                                 dv64 = get_unaligned((u64 *)ir);
61                                 dv64 |= get_unaligned((u64 *)ir + 1);
62                                 dv64 |= get_unaligned((u64 *)ir + 2);
63                                 dv64 |= get_unaligned((u64 *)ir + 3);
64                                 if (dv64)
65                                         break;
66                         }
67                         for (; (ir + 8) <= limit; ir += 8) {
68                                 dv64 = get_unaligned((u64 *)ir);
69                                 if (dv64) {
70 #  if defined(__LITTLE_ENDIAN)
71                                         ir += __builtin_ctzll(dv64) >> 3;
72 #  elif defined(__BIG_ENDIAN)
73                                         ir += __builtin_clzll(dv64) >> 3;
74 #  else
75 #    error "missing endian definition"
76 #  endif
77                                         break;
78                                 }
79                         }
80 #else
81                         while ((ir < (const unsigned char *)
82                                         ALIGN((uintptr_t)ir, 4)) &&
83                                         (ir < limit) && (*ir == 0))
84                                 ir++;
85                         for (; (ir + 4) <= limit; ir += 4) {
86                                 dv = *((u32 *)ir);
87                                 if (dv) {
88 #  if defined(__LITTLE_ENDIAN)
89                                         ir += __builtin_ctz(dv) >> 3;
90 #  elif defined(__BIG_ENDIAN)
91                                         ir += __builtin_clz(dv) >> 3;
92 #  else
93 #    error "missing endian definition"
94 #  endif
95                                         break;
96                                 }
97                         }
98 #endif
99                         while (likely(ir < limit) && unlikely(*ir == 0))
100                                 ir++;
101                         run_length = ir - ip;
102                         if (run_length > MAX_ZERO_RUN_LENGTH)
103                                 run_length = MAX_ZERO_RUN_LENGTH;
104                 } else {
105                         t = ((dv * 0x1824429d) >> (32 - D_BITS)) & D_MASK;
106                         m_pos = in + dict[t];
107                         dict[t] = (lzo_dict_t) (ip - in);
108                         if (unlikely(dv != get_unaligned_le32(m_pos)))
109                                 goto literal;
110                 }
111
112                 ii -= ti;
113                 ti = 0;
114                 t = ip - ii;
115                 if (t != 0) {
116                         if (t <= 3) {
117                                 op[*state_offset] |= t;
118                                 COPY4(op, ii);
119                                 op += t;
120                         } else if (t <= 16) {
121                                 *op++ = (t - 3);
122                                 COPY8(op, ii);
123                                 COPY8(op + 8, ii + 8);
124                                 op += t;
125                         } else {
126                                 if (t <= 18) {
127                                         *op++ = (t - 3);
128                                 } else {
129                                         size_t tt = t - 18;
130                                         *op++ = 0;
131                                         while (unlikely(tt > 255)) {
132                                                 tt -= 255;
133                                                 *op++ = 0;
134                                         }
135                                         *op++ = tt;
136                                 }
137                                 do {
138                                         COPY8(op, ii);
139                                         COPY8(op + 8, ii + 8);
140                                         op += 16;
141                                         ii += 16;
142                                         t -= 16;
143                                 } while (t >= 16);
144                                 if (t > 0) do {
145                                         *op++ = *ii++;
146                                 } while (--t > 0);
147                         }
148                 }
149
150                 if (unlikely(run_length)) {
151                         ip += run_length;
152                         run_length -= MIN_ZERO_RUN_LENGTH;
153                         put_unaligned_le32((run_length << 21) | 0xfffc18
154                                            | (run_length & 0x7), op);
155                         op += 4;
156                         run_length = 0;
157                         *state_offset = -3;
158                         goto finished_writing_instruction;
159                 }
160
161                 m_len = 4;
162                 {
163 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ64)
164                 u64 v;
165                 v = get_unaligned((const u64 *) (ip + m_len)) ^
166                     get_unaligned((const u64 *) (m_pos + m_len));
167                 if (unlikely(v == 0)) {
168                         do {
169                                 m_len += 8;
170                                 v = get_unaligned((const u64 *) (ip + m_len)) ^
171                                     get_unaligned((const u64 *) (m_pos + m_len));
172                                 if (unlikely(ip + m_len >= ip_end))
173                                         goto m_len_done;
174                         } while (v == 0);
175                 }
176 #  if defined(__LITTLE_ENDIAN)
177                 m_len += (unsigned) __builtin_ctzll(v) / 8;
178 #  elif defined(__BIG_ENDIAN)
179                 m_len += (unsigned) __builtin_clzll(v) / 8;
180 #  else
181 #    error "missing endian definition"
182 #  endif
183 #elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ32)
184                 u32 v;
185                 v = get_unaligned((const u32 *) (ip + m_len)) ^
186                     get_unaligned((const u32 *) (m_pos + m_len));
187                 if (unlikely(v == 0)) {
188                         do {
189                                 m_len += 4;
190                                 v = get_unaligned((const u32 *) (ip + m_len)) ^
191                                     get_unaligned((const u32 *) (m_pos + m_len));
192                                 if (v != 0)
193                                         break;
194                                 m_len += 4;
195                                 v = get_unaligned((const u32 *) (ip + m_len)) ^
196                                     get_unaligned((const u32 *) (m_pos + m_len));
197                                 if (unlikely(ip + m_len >= ip_end))
198                                         goto m_len_done;
199                         } while (v == 0);
200                 }
201 #  if defined(__LITTLE_ENDIAN)
202                 m_len += (unsigned) __builtin_ctz(v) / 8;
203 #  elif defined(__BIG_ENDIAN)
204                 m_len += (unsigned) __builtin_clz(v) / 8;
205 #  else
206 #    error "missing endian definition"
207 #  endif
208 #else
209                 if (unlikely(ip[m_len] == m_pos[m_len])) {
210                         do {
211                                 m_len += 1;
212                                 if (ip[m_len] != m_pos[m_len])
213                                         break;
214                                 m_len += 1;
215                                 if (ip[m_len] != m_pos[m_len])
216                                         break;
217                                 m_len += 1;
218                                 if (ip[m_len] != m_pos[m_len])
219                                         break;
220                                 m_len += 1;
221                                 if (ip[m_len] != m_pos[m_len])
222                                         break;
223                                 m_len += 1;
224                                 if (ip[m_len] != m_pos[m_len])
225                                         break;
226                                 m_len += 1;
227                                 if (ip[m_len] != m_pos[m_len])
228                                         break;
229                                 m_len += 1;
230                                 if (ip[m_len] != m_pos[m_len])
231                                         break;
232                                 m_len += 1;
233                                 if (unlikely(ip + m_len >= ip_end))
234                                         goto m_len_done;
235                         } while (ip[m_len] == m_pos[m_len]);
236                 }
237 #endif
238                 }
239 m_len_done:
240
241                 m_off = ip - m_pos;
242                 ip += m_len;
243                 if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) {
244                         m_off -= 1;
245                         *op++ = (((m_len - 1) << 5) | ((m_off & 7) << 2));
246                         *op++ = (m_off >> 3);
247                 } else if (m_off <= M3_MAX_OFFSET) {
248                         m_off -= 1;
249                         if (m_len <= M3_MAX_LEN)
250                                 *op++ = (M3_MARKER | (m_len - 2));
251                         else {
252                                 m_len -= M3_MAX_LEN;
253                                 *op++ = M3_MARKER | 0;
254                                 while (unlikely(m_len > 255)) {
255                                         m_len -= 255;
256                                         *op++ = 0;
257                                 }
258                                 *op++ = (m_len);
259                         }
260                         *op++ = (m_off << 2);
261                         *op++ = (m_off >> 6);
262                 } else {
263                         m_off -= 0x4000;
264                         if (m_len <= M4_MAX_LEN)
265                                 *op++ = (M4_MARKER | ((m_off >> 11) & 8)
266                                                 | (m_len - 2));
267                         else {
268                                 m_len -= M4_MAX_LEN;
269                                 *op++ = (M4_MARKER | ((m_off >> 11) & 8));
270                                 while (unlikely(m_len > 255)) {
271                                         m_len -= 255;
272                                         *op++ = 0;
273                                 }
274                                 *op++ = (m_len);
275                         }
276                         *op++ = (m_off << 2);
277                         *op++ = (m_off >> 6);
278                 }
279                 *state_offset = -2;
280 finished_writing_instruction:
281                 ii = ip;
282                 goto next;
283         }
284         *out_len = op - out;
285         return in_end - (ii - ti);
286 }
287
288 int lzogeneric1x_1_compress(const unsigned char *in, size_t in_len,
289                      unsigned char *out, size_t *out_len,
290                      void *wrkmem, const unsigned char bitstream_version)
291 {
292         const unsigned char *ip = in;
293         unsigned char *op = out;
294         size_t l = in_len;
295         size_t t = 0;
296         signed char state_offset = -2;
297         unsigned int m4_max_offset;
298
299         // LZO v0 will never write 17 as first byte,
300         // so this is used to version the bitstream
301         if (bitstream_version > 0) {
302                 *op++ = 17;
303                 *op++ = bitstream_version;
304                 m4_max_offset = M4_MAX_OFFSET_V1;
305         } else {
306                 m4_max_offset = M4_MAX_OFFSET_V0;
307         }
308
309         while (l > 20) {
310                 size_t ll = l <= (m4_max_offset + 1) ? l : (m4_max_offset + 1);
311                 uintptr_t ll_end = (uintptr_t) ip + ll;
312                 if ((ll_end + ((t + ll) >> 5)) <= ll_end)
313                         break;
314                 BUILD_BUG_ON(D_SIZE * sizeof(lzo_dict_t) > LZO1X_1_MEM_COMPRESS);
315                 memset(wrkmem, 0, D_SIZE * sizeof(lzo_dict_t));
316                 t = lzo1x_1_do_compress(ip, ll, op, out_len, t, wrkmem,
317                                         &state_offset, bitstream_version);
318                 ip += ll;
319                 op += *out_len;
320                 l  -= ll;
321         }
322         t += l;
323
324         if (t > 0) {
325                 const unsigned char *ii = in + in_len - t;
326
327                 if (op == out && t <= 238) {
328                         *op++ = (17 + t);
329                 } else if (t <= 3) {
330                         op[state_offset] |= t;
331                 } else if (t <= 18) {
332                         *op++ = (t - 3);
333                 } else {
334                         size_t tt = t - 18;
335                         *op++ = 0;
336                         while (tt > 255) {
337                                 tt -= 255;
338                                 *op++ = 0;
339                         }
340                         *op++ = tt;
341                 }
342                 if (t >= 16) do {
343                         COPY8(op, ii);
344                         COPY8(op + 8, ii + 8);
345                         op += 16;
346                         ii += 16;
347                         t -= 16;
348                 } while (t >= 16);
349                 if (t > 0) do {
350                         *op++ = *ii++;
351                 } while (--t > 0);
352         }
353
354         *op++ = M4_MARKER | 1;
355         *op++ = 0;
356         *op++ = 0;
357
358         *out_len = op - out;
359         return LZO_E_OK;
360 }
361
362 int lzo1x_1_compress(const unsigned char *in, size_t in_len,
363                      unsigned char *out, size_t *out_len,
364                      void *wrkmem)
365 {
366         return lzogeneric1x_1_compress(in, in_len, out, out_len, wrkmem, 0);
367 }
368
369 int lzorle1x_1_compress(const unsigned char *in, size_t in_len,
370                      unsigned char *out, size_t *out_len,
371                      void *wrkmem)
372 {
373         return lzogeneric1x_1_compress(in, in_len, out, out_len,
374                                        wrkmem, LZO_VERSION);
375 }
376
377 EXPORT_SYMBOL_GPL(lzo1x_1_compress);
378 EXPORT_SYMBOL_GPL(lzorle1x_1_compress);
379
380 MODULE_LICENSE("GPL");
381 MODULE_DESCRIPTION("LZO1X-1 Compressor");