|
kusano |
7d535a |
/* lzo1f_1.c -- implementation of the LZO1F-1 compression algorithm
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
This file is part of the LZO real-time data compression library.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
|
|
kusano |
7d535a |
All Rights Reserved.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The LZO library is free software; you can redistribute it and/or
|
|
kusano |
7d535a |
modify it under the terms of the GNU General Public License as
|
|
kusano |
7d535a |
published by the Free Software Foundation; either version 2 of
|
|
kusano |
7d535a |
the License, or (at your option) any later version.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The LZO library is distributed in the hope that it will be useful,
|
|
kusano |
7d535a |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
kusano |
7d535a |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
kusano |
7d535a |
GNU General Public License for more details.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
You should have received a copy of the GNU General Public License
|
|
kusano |
7d535a |
along with the LZO library; see the file COPYING.
|
|
kusano |
7d535a |
If not, write to the Free Software Foundation, Inc.,
|
|
kusano |
7d535a |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Markus F.X.J. Oberhumer
|
|
kusano |
7d535a |
<markus@oberhumer.com></markus@oberhumer.com>
|
|
kusano |
7d535a |
http://www.oberhumer.com/opensource/lzo/
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include "lzo_conf.h"
|
|
kusano |
7d535a |
#include "lzo/lzo1f.h"
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/***********************************************************************
|
|
kusano |
7d535a |
//
|
|
kusano |
7d535a |
************************************************************************/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define M2_MAX_OFFSET 0x0800
|
|
kusano |
7d535a |
#define M3_MAX_OFFSET 0x3fff
|
|
kusano |
7d535a |
#define M3_MARKER 224
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifndef LZO_HASH
|
|
kusano |
7d535a |
#define LZO_HASH LZO_HASH_LZO_INCREMENTAL_A
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#define D_BITS 14
|
|
kusano |
7d535a |
#define D_INDEX1(d,p) d = DM(DMUL(0x21,DX3(p,5,5,6)) >> 5)
|
|
kusano |
7d535a |
#define D_INDEX2(d,p) d = (d & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f)
|
|
kusano |
7d535a |
#include "lzo_dict.h"
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/***********************************************************************
|
|
kusano |
7d535a |
// compress a block of data.
|
|
kusano |
7d535a |
************************************************************************/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static __lzo_noinline
|
|
kusano |
7d535a |
int do_compress ( const lzo_bytep in , lzo_uint in_len,
|
|
kusano |
7d535a |
lzo_bytep out, lzo_uintp out_len,
|
|
kusano |
7d535a |
lzo_voidp wrkmem )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register const lzo_bytep ip;
|
|
kusano |
7d535a |
lzo_bytep op;
|
|
kusano |
7d535a |
const lzo_bytep const in_end = in + in_len;
|
|
kusano |
7d535a |
const lzo_bytep const ip_end = in + in_len - 9;
|
|
kusano |
7d535a |
const lzo_bytep ii;
|
|
kusano |
7d535a |
lzo_dict_p const dict = (lzo_dict_p) wrkmem;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
op = out;
|
|
kusano |
7d535a |
ip = in;
|
|
kusano |
7d535a |
ii = ip;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
ip++;
|
|
kusano |
7d535a |
for (;;)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register const lzo_bytep m_pos;
|
|
kusano |
7d535a |
lzo_uint m_off;
|
|
kusano |
7d535a |
lzo_uint m_len;
|
|
kusano |
7d535a |
lzo_uint dindex;
|
|
kusano |
7d535a |
lzo_uint lit;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
DINDEX1(dindex,ip);
|
|
kusano |
7d535a |
GINDEX(m_pos,m_off,dict,dindex,in);
|
|
kusano |
7d535a |
if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M3_MAX_OFFSET))
|
|
kusano |
7d535a |
goto literal;
|
|
kusano |
7d535a |
#if 1
|
|
kusano |
7d535a |
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
|
kusano |
7d535a |
goto try_match;
|
|
kusano |
7d535a |
DINDEX2(dindex,ip);
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
GINDEX(m_pos,m_off,dict,dindex,in);
|
|
kusano |
7d535a |
if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M3_MAX_OFFSET))
|
|
kusano |
7d535a |
goto literal;
|
|
kusano |
7d535a |
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
|
kusano |
7d535a |
goto try_match;
|
|
kusano |
7d535a |
goto literal;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
try_match:
|
|
kusano |
7d535a |
#if 0 && defined(LZO_UNALIGNED_OK_2)
|
|
kusano |
7d535a |
if (* (const lzo_ushortp) m_pos != * (const lzo_ushortp) ip)
|
|
kusano |
7d535a |
#else
|
|
kusano |
7d535a |
if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
if (m_pos[2] == ip[2])
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
m_pos += 3;
|
|
kusano |
7d535a |
#if 0
|
|
kusano |
7d535a |
if (m_off <= M2_MAX_OFFSET)
|
|
kusano |
7d535a |
goto match;
|
|
kusano |
7d535a |
if (lit <= 3)
|
|
kusano |
7d535a |
goto match;
|
|
kusano |
7d535a |
if (lit == 3) /* better compression, but slower */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
|
|
kusano |
7d535a |
*op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
|
|
kusano |
7d535a |
goto code_match;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (*m_pos == ip[3])
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
goto match;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* a literal */
|
|
kusano |
7d535a |
literal:
|
|
kusano |
7d535a |
UPDATE_I(dict,0,dindex,ip,in);
|
|
kusano |
7d535a |
if (++ip >= ip_end)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
continue;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* a match */
|
|
kusano |
7d535a |
match:
|
|
kusano |
7d535a |
UPDATE_I(dict,0,dindex,ip,in);
|
|
kusano |
7d535a |
/* store current literal run */
|
|
kusano |
7d535a |
lit = pd(ip,ii);
|
|
kusano |
7d535a |
if (lit > 0)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register lzo_uint t = lit;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (t < 4 && op > out)
|
|
kusano |
7d535a |
op[-2] |= LZO_BYTE(t);
|
|
kusano |
7d535a |
else if (t <= 31)
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(t);
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register lzo_uint tt = t - 31;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
*op++ = 0;
|
|
kusano |
7d535a |
while (tt > 255)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
tt -= 255;
|
|
kusano |
7d535a |
*op++ = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
assert(tt > 0);
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(tt);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
do *op++ = *ii++; while (--t > 0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
assert(ii == ip);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* code the match */
|
|
kusano |
7d535a |
ip += 3;
|
|
kusano |
7d535a |
if (*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++ ||
|
|
kusano |
7d535a |
*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
--ip;
|
|
kusano |
7d535a |
m_len = pd(ip, ii);
|
|
kusano |
7d535a |
assert(m_len >= 3); assert(m_len <= 8);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (m_off <= M2_MAX_OFFSET)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
m_off -= 1;
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(((m_len - 2) << 5) | ((m_off & 7) << 2));
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(m_off >> 3);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else if (m_len == 3 && m_off <= 2*M2_MAX_OFFSET && lit > 0)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
m_off -= 1;
|
|
kusano |
7d535a |
/* m_off -= M2_MAX_OFFSET; */
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(((m_off & 7) << 2));
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(m_off >> 3);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
|
|
kusano |
7d535a |
*op++ = LZO_BYTE((m_off & 63) << 2);
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(m_off >> 6);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
const lzo_bytep end;
|
|
kusano |
7d535a |
end = in_end;
|
|
kusano |
7d535a |
while (ip < end && *m_pos == *ip)
|
|
kusano |
7d535a |
m_pos++, ip++;
|
|
kusano |
7d535a |
m_len = pd(ip, ii);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
assert(m_len >= 3);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (m_len <= 33)
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
m_len -= 33;
|
|
kusano |
7d535a |
*op++ = M3_MARKER | 0;
|
|
kusano |
7d535a |
while (m_len > 255)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
m_len -= 255;
|
|
kusano |
7d535a |
*op++ = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
assert(m_len > 0);
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(m_len);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
*op++ = LZO_BYTE((m_off & 63) << 2);
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(m_off >> 6);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
ii = ip;
|
|
kusano |
7d535a |
if (ip >= ip_end)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* store final literal run */
|
|
kusano |
7d535a |
if (pd(in_end,ii) > 0)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register lzo_uint t = pd(in_end,ii);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (t < 4 && op > out)
|
|
kusano |
7d535a |
op[-2] |= LZO_BYTE(t);
|
|
kusano |
7d535a |
else if (t <= 31)
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(t);
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register lzo_uint tt = t - 31;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
*op++ = 0;
|
|
kusano |
7d535a |
while (tt > 255)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
tt -= 255;
|
|
kusano |
7d535a |
*op++ = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
assert(tt > 0);
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(tt);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
do *op++ = *ii++; while (--t > 0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
*out_len = pd(op, out);
|
|
kusano |
7d535a |
return LZO_E_OK;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/***********************************************************************
|
|
kusano |
7d535a |
// public entry point
|
|
kusano |
7d535a |
************************************************************************/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LZO_PUBLIC(int)
|
|
kusano |
7d535a |
lzo1f_1_compress ( const lzo_bytep in , lzo_uint in_len,
|
|
kusano |
7d535a |
lzo_bytep out, lzo_uintp out_len,
|
|
kusano |
7d535a |
lzo_voidp wrkmem )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
lzo_bytep op = out;
|
|
kusano |
7d535a |
int r = LZO_E_OK;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (in_len <= 0)
|
|
kusano |
7d535a |
*out_len = 0;
|
|
kusano |
7d535a |
else if (in_len <= 10)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
*op++ = LZO_BYTE(in_len);
|
|
kusano |
7d535a |
do *op++ = *in++; while (--in_len > 0);
|
|
kusano |
7d535a |
*out_len = pd(op, out);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
r = do_compress(in,in_len,out,out_len,wrkmem);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (r == LZO_E_OK)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
op = out + *out_len;
|
|
kusano |
7d535a |
*op++ = M3_MARKER | 1;
|
|
kusano |
7d535a |
*op++ = 0;
|
|
kusano |
7d535a |
*op++ = 0;
|
|
kusano |
7d535a |
*out_len += 3;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
vi:ts=4:et
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|