1 /* implementation of the LZO1[XY]-1 compression algorithm
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 1996..2008 Markus Franz Xaver Johannes Oberhumer
8 Markus F.X.J. Oberhumer <markus@oberhumer.com>
9 http://www.oberhumer.com/opensource/lzo/
11 The LZO library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 The LZO library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with the LZO library; see the file COPYING.
23 If not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 /***********************************************************************
28 // compress a block of data.
29 ************************************************************************/
30 static NOINLINE
unsigned
31 do_compress(const uint8_t* in
, unsigned in_len
,
32 uint8_t* out
, unsigned* out_len
,
35 register const uint8_t* ip
;
37 const uint8_t* const in_end
= in
+ in_len
;
38 const uint8_t* const ip_end
= in
+ in_len
- M2_MAX_LEN
- 5;
40 const void* *const dict
= (const void**) wrkmem
;
48 register const uint8_t* m_pos
;
54 GINDEX(m_pos
,m_off
,dict
,dindex
,in
);
55 if (LZO_CHECK_MPOS_NON_DET(m_pos
,m_off
,in
,ip
,M4_MAX_OFFSET
))
58 if (m_off
<= M2_MAX_OFFSET
|| m_pos
[3] == ip
[3])
62 GINDEX(m_pos
,m_off
,dict
,dindex
,in
);
63 if (LZO_CHECK_MPOS_NON_DET(m_pos
,m_off
,in
,ip
,M4_MAX_OFFSET
))
65 if (m_off
<= M2_MAX_OFFSET
|| m_pos
[3] == ip
[3])
70 #if 1 && defined(LZO_UNALIGNED_OK_2)
71 if (* (const lzo_ushortp
) m_pos
!= * (const lzo_ushortp
) ip
)
73 if (m_pos
[0] != ip
[0] || m_pos
[1] != ip
[1])
77 if (m_pos
[2] == ip
[2]) {
79 if (m_off
<= M2_MAX_OFFSET
)
83 if (lit
== 3) { /* better compression, but slower */
84 assert(op
- 2 > out
); op
[-2] |= (uint8_t)(3);
85 *op
++ = *ii
++; *op
++ = *ii
++; *op
++ = *ii
++;
88 if (m_pos
[3] == ip
[3])
93 /* still need a better way for finding M1 matches */
97 if (m_off
<= M1_MAX_OFFSET
&& lit
> 0 && lit
<= 3)
99 if (m_off
<= M1_MAX_OFFSET
&& lit
== 3)
105 assert(op
- 2 > out
); op
[-2] |= (uint8_t)(t
);
106 do *op
++ = *ii
++; while (--t
> 0);
109 *op
++ = (uint8_t)(M1_MARKER
| ((m_off
& 3) << 2));
110 *op
++ = (uint8_t)(m_off
>> 2);
120 UPDATE_I(dict
, 0, dindex
, ip
, in
);
128 UPDATE_I(dict
, 0, dindex
, ip
, in
);
129 /* store current literal run */
130 if (pd(ip
, ii
) > 0) {
131 register unsigned t
= pd(ip
, ii
);
134 assert(op
- 2 > out
);
135 op
[-2] |= (uint8_t)(t
);
138 *op
++ = (uint8_t)(t
- 3);
140 register unsigned tt
= t
- 18;
148 *op
++ = (uint8_t)(tt
);
150 do *op
++ = *ii
++; while (--t
> 0);
156 if (m_pos
[3] != *ip
++ || m_pos
[4] != *ip
++ || m_pos
[5] != *ip
++
157 || m_pos
[6] != *ip
++ || m_pos
[7] != *ip
++ || m_pos
[8] != *ip
++
159 || m_pos
[ 9] != *ip
++ || m_pos
[10] != *ip
++ || m_pos
[11] != *ip
++
160 || m_pos
[12] != *ip
++ || m_pos
[13] != *ip
++ || m_pos
[14] != *ip
++
166 assert(m_len
<= M2_MAX_LEN
);
168 if (m_off
<= M2_MAX_OFFSET
) {
171 *op
++ = (uint8_t)(((m_len
- 1) << 5) | ((m_off
& 7) << 2));
172 *op
++ = (uint8_t)(m_off
>> 3);
174 *op
++ = (uint8_t)(((m_len
+ 1) << 4) | ((m_off
& 3) << 2));
175 *op
++ = (uint8_t)(m_off
>> 2);
178 else if (m_off
<= M3_MAX_OFFSET
) {
180 *op
++ = (uint8_t)(M3_MARKER
| (m_len
- 2));
186 assert(m_off
<= 0x7fff);
187 *op
++ = (uint8_t)(M4_MARKER
| ((m_off
& 0x4000) >> 11) | (m_len
- 2));
196 const uint8_t* end
= in_end
;
197 const uint8_t* m
= m_pos
+ M2_MAX_LEN
+ 1;
198 while (ip
< end
&& *m
== *ip
)
202 assert(m_len
> M2_MAX_LEN
);
204 if (m_off
<= M3_MAX_OFFSET
) {
207 *op
++ = (uint8_t)(M3_MARKER
| (m_len
- 2));
210 *op
++ = M3_MARKER
| 0;
219 assert(m_off
<= 0x7fff);
220 if (m_len
<= M4_MAX_LEN
)
221 *op
++ = (uint8_t)(M4_MARKER
| ((m_off
& 0x4000) >> 11) | (m_len
- 2));
224 *op
++ = (uint8_t)(M4_MARKER
| ((m_off
& 0x4000) >> 11));
226 while (m_len
> 255) {
231 *op
++ = (uint8_t)(m_len
);
235 *op
++ = (uint8_t)((m_off
& 63) << 2);
236 *op
++ = (uint8_t)(m_off
>> 6);
246 *out_len
= pd(op
, out
);
247 return pd(in_end
, ii
);
250 /***********************************************************************
251 // public entry point
252 ************************************************************************/
253 int DO_COMPRESS(const uint8_t* in
, unsigned in_len
,
254 uint8_t* out
, unsigned* out_len
,
260 if (in_len
<= M2_MAX_LEN
+ 5)
263 t
= do_compress(in
,in_len
,op
,out_len
,wrkmem
);
268 const uint8_t* ii
= in
+ in_len
- t
;
270 if (op
== out
&& t
<= 238)
271 *op
++ = (uint8_t)(17 + t
);
273 op
[-2] |= (uint8_t)(t
);
275 *op
++ = (uint8_t)(t
- 3);
277 unsigned tt
= t
- 18;
285 *op
++ = (uint8_t)(tt
);
287 do *op
++ = *ii
++; while (--t
> 0);
290 *op
++ = M4_MARKER
| 1;
294 *out_len
= pd(op
, out
);
295 return 0; /*LZO_E_OK*/