rtc: Au1000 On-Chip Counter0-as-RTC driver.
[linux-2.6/linux-mips.git] / lib / lzo / lzo1x_compress.c
bloba6040990a62ee36054794b53cc5f91379c2a3be7
1 /*
2 * LZO1X Compressor from MiniLZO
4 * Copyright (C) 1996-2005 Markus F.X.J. Oberhumer <markus@oberhumer.com>
6 * The full LZO package can be found at:
7 * http://www.oberhumer.com/opensource/lzo/
9 * Changed for kernel use by:
10 * Nitin Gupta <nitingupta910@gmail.com>
11 * Richard Purdie <rpurdie@openedhand.com>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/lzo.h>
17 #include <asm/unaligned.h>
18 #include "lzodefs.h"
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, void *wrkmem)
24 const unsigned char * const in_end = in + in_len;
25 const unsigned char * const ip_end = in + in_len - M2_MAX_LEN - 5;
26 const unsigned char ** const dict = wrkmem;
27 const unsigned char *ip = in, *ii = ip;
28 const unsigned char *end, *m, *m_pos;
29 size_t m_off, m_len, dindex;
30 unsigned char *op = out;
32 ip += 4;
34 for (;;) {
35 dindex = ((size_t)(0x21 * DX3(ip, 5, 5, 6)) >> 5) & D_MASK;
36 m_pos = dict[dindex];
38 if (m_pos < in)
39 goto literal;
41 if (ip == m_pos || ((size_t)(ip - m_pos) > M4_MAX_OFFSET))
42 goto literal;
44 m_off = ip - m_pos;
45 if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
46 goto try_match;
48 dindex = (dindex & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f);
49 m_pos = dict[dindex];
51 if (m_pos < in)
52 goto literal;
54 if (ip == m_pos || ((size_t)(ip - m_pos) > M4_MAX_OFFSET))
55 goto literal;
57 m_off = ip - m_pos;
58 if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
59 goto try_match;
61 goto literal;
63 try_match:
64 if (get_unaligned((const unsigned short *)m_pos)
65 == get_unaligned((const unsigned short *)ip)) {
66 if (likely(m_pos[2] == ip[2]))
67 goto match;
70 literal:
71 dict[dindex] = ip;
72 ++ip;
73 if (unlikely(ip >= ip_end))
74 break;
75 continue;
77 match:
78 dict[dindex] = ip;
79 if (ip != ii) {
80 size_t t = ip - ii;
82 if (t <= 3) {
83 op[-2] |= t;
84 } else if (t <= 18) {
85 *op++ = (t - 3);
86 } else {
87 size_t tt = t - 18;
89 *op++ = 0;
90 while (tt > 255) {
91 tt -= 255;
92 *op++ = 0;
94 *op++ = tt;
96 do {
97 *op++ = *ii++;
98 } while (--t > 0);
101 ip += 3;
102 if (m_pos[3] != *ip++ || m_pos[4] != *ip++
103 || m_pos[5] != *ip++ || m_pos[6] != *ip++
104 || m_pos[7] != *ip++ || m_pos[8] != *ip++) {
105 --ip;
106 m_len = ip - ii;
108 if (m_off <= M2_MAX_OFFSET) {
109 m_off -= 1;
110 *op++ = (((m_len - 1) << 5)
111 | ((m_off & 7) << 2));
112 *op++ = (m_off >> 3);
113 } else if (m_off <= M3_MAX_OFFSET) {
114 m_off -= 1;
115 *op++ = (M3_MARKER | (m_len - 2));
116 goto m3_m4_offset;
117 } else {
118 m_off -= 0x4000;
120 *op++ = (M4_MARKER | ((m_off & 0x4000) >> 11)
121 | (m_len - 2));
122 goto m3_m4_offset;
124 } else {
125 end = in_end;
126 m = m_pos + M2_MAX_LEN + 1;
128 while (ip < end && *m == *ip) {
129 m++;
130 ip++;
132 m_len = ip - ii;
134 if (m_off <= M3_MAX_OFFSET) {
135 m_off -= 1;
136 if (m_len <= 33) {
137 *op++ = (M3_MARKER | (m_len - 2));
138 } else {
139 m_len -= 33;
140 *op++ = M3_MARKER | 0;
141 goto m3_m4_len;
143 } else {
144 m_off -= 0x4000;
145 if (m_len <= M4_MAX_LEN) {
146 *op++ = (M4_MARKER
147 | ((m_off & 0x4000) >> 11)
148 | (m_len - 2));
149 } else {
150 m_len -= M4_MAX_LEN;
151 *op++ = (M4_MARKER
152 | ((m_off & 0x4000) >> 11));
153 m3_m4_len:
154 while (m_len > 255) {
155 m_len -= 255;
156 *op++ = 0;
159 *op++ = (m_len);
162 m3_m4_offset:
163 *op++ = ((m_off & 63) << 2);
164 *op++ = (m_off >> 6);
167 ii = ip;
168 if (unlikely(ip >= ip_end))
169 break;
172 *out_len = op - out;
173 return in_end - ii;
176 int lzo1x_1_compress(const unsigned char *in, size_t in_len, unsigned char *out,
177 size_t *out_len, void *wrkmem)
179 const unsigned char *ii;
180 unsigned char *op = out;
181 size_t t;
183 if (unlikely(in_len <= M2_MAX_LEN + 5)) {
184 t = in_len;
185 } else {
186 t = _lzo1x_1_do_compress(in, in_len, op, out_len, wrkmem);
187 op += *out_len;
190 if (t > 0) {
191 ii = in + in_len - t;
193 if (op == out && t <= 238) {
194 *op++ = (17 + t);
195 } else if (t <= 3) {
196 op[-2] |= t;
197 } else if (t <= 18) {
198 *op++ = (t - 3);
199 } else {
200 size_t tt = t - 18;
202 *op++ = 0;
203 while (tt > 255) {
204 tt -= 255;
205 *op++ = 0;
208 *op++ = tt;
210 do {
211 *op++ = *ii++;
212 } while (--t > 0);
215 *op++ = M4_MARKER | 1;
216 *op++ = 0;
217 *op++ = 0;
219 *out_len = op - out;
220 return LZO_E_OK;
222 EXPORT_SYMBOL_GPL(lzo1x_1_compress);
224 MODULE_LICENSE("GPL");
225 MODULE_DESCRIPTION("LZO1X-1 Compressor");