2015-04-12 06:22:03 +00:00
|
|
|
/* lzo1b_sm.ch -- implementation of the LZO1B compression algorithm
|
|
|
|
|
|
|
|
This file is part of the LZO real-time data compression library.
|
|
|
|
|
|
|
|
Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer
|
|
|
|
All Rights Reserved.
|
|
|
|
|
|
|
|
The LZO library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The LZO library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with the LZO library; see the file COPYING.
|
|
|
|
If not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
Markus F.X.J. Oberhumer
|
|
|
|
<markus@oberhumer.com>
|
|
|
|
http://www.oberhumer.com/opensource/lzo/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* WARNING: this file should *not* be used by applications. It is
|
|
|
|
part of the implementation of the library and is subject
|
|
|
|
to change.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
// search for a match
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#if (DD_BITS == 0)
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* search ip in the dictionary */
|
|
|
|
DINDEX1(dindex, ip);
|
|
|
|
GINDEX(m_pos, m_off, dict, dindex, in);
|
|
|
|
|
|
|
|
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
|
|
|
|
goto literal;
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#if 1
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
|
|
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
|
|
|
goto try_match;
|
|
|
|
|
|
|
|
DINDEX2(dindex, ip);
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
2015-05-19 03:24:56 +00:00
|
|
|
GINDEX(m_pos, m_off, dict, dindex, in);
|
|
|
|
|
|
|
|
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
|
|
|
|
goto literal;
|
|
|
|
|
|
|
|
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
|
|
|
goto try_match;
|
|
|
|
|
|
|
|
goto literal;
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
#else /* (DD_BITS == 0) */
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* search ip in the deepened dictionary */
|
|
|
|
{
|
|
|
|
lzo_dict_p d = &dict [ DINDEX(dv, ip) ];
|
|
|
|
const lzo_bytep ip_sav;
|
|
|
|
unsigned j = DD_SIZE;
|
|
|
|
lzo_uint x_len;
|
|
|
|
LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, x_off, 0);
|
|
|
|
|
|
|
|
DVAL_ASSERT(dv, ip);
|
2015-04-12 06:22:03 +00:00
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
ip_sav = ip;
|
|
|
|
m_len = 0;
|
2015-04-12 06:22:03 +00:00
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
do
|
|
|
|
{
|
2015-04-12 06:22:03 +00:00
|
|
|
#if !defined(NDEBUG)
|
2015-05-19 03:24:56 +00:00
|
|
|
const lzo_bytep z_pos = NULL;
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
#if (LZO_DICT_USE_PTR)
|
2015-05-19 03:24:56 +00:00
|
|
|
m_pos = *d;
|
|
|
|
assert((z_pos = m_pos) == *d);
|
2015-04-12 06:22:03 +00:00
|
|
|
#if (LZO_DETERMINISTIC)
|
2015-05-19 03:24:56 +00:00
|
|
|
assert(m_pos == NULL || m_pos >= in);
|
|
|
|
assert(m_pos == NULL || m_pos < ip);
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2015-05-19 03:24:56 +00:00
|
|
|
x_off = *d;
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
2015-05-19 03:24:56 +00:00
|
|
|
assert(ip == ip_sav);
|
2015-04-12 06:22:03 +00:00
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
if (LZO_CHECK_MPOS(m_pos, x_off, in, ip, MAX_OFFSET))
|
2015-04-12 06:22:03 +00:00
|
|
|
#if (CLEVEL == 9)
|
2015-05-19 03:24:56 +00:00
|
|
|
*d = DENTRY(ip, in);
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#else
|
2015-05-19 03:24:56 +00:00
|
|
|
((void)(0));
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
2015-05-19 03:24:56 +00:00
|
|
|
else if (m_pos[m_len] != ip[m_len])
|
|
|
|
((void)(0));
|
|
|
|
else if (*m_pos++ == *ip++ && *m_pos++ == *ip++ && *m_pos++ == *ip++)
|
|
|
|
{
|
2015-04-12 06:22:03 +00:00
|
|
|
#if !(LZO_DICT_USE_PTR)
|
2015-05-19 03:24:56 +00:00
|
|
|
assert((z_pos = ip - 3 - x_off) == (m_pos - 3));
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
|
|
/* a match */
|
|
|
|
if (MATCH_M2)
|
|
|
|
{
|
|
|
|
x_len = pd((ip - 1), ip_sav);
|
|
|
|
|
|
|
|
if (x_len > m_len)
|
|
|
|
{
|
|
|
|
m_len = x_len;
|
|
|
|
m_off = x_off;
|
|
|
|
assert((m_pos_sav = z_pos) != NULL);
|
|
|
|
}
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#if (CLEVEL == 9)
|
2015-05-19 03:24:56 +00:00
|
|
|
/* try to find a closer match */
|
|
|
|
else if (x_len == m_len && x_off < m_off)
|
|
|
|
{
|
|
|
|
m_off = x_off;
|
|
|
|
assert((m_pos_sav = z_pos) != NULL);
|
|
|
|
}
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
2015-05-19 03:24:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert((ip - ip_sav) == M2_MAX_LEN + 1);
|
2015-04-12 06:22:03 +00:00
|
|
|
#if (CLEVEL == 9)
|
|
|
|
#if defined(MATCH_IP_END)
|
2015-05-19 03:24:56 +00:00
|
|
|
{
|
|
|
|
const lzo_bytep end;
|
|
|
|
end = MATCH_IP_END;
|
|
|
|
|
|
|
|
while (ip < end && *m_pos == *ip)
|
|
|
|
m_pos++, ip++;
|
|
|
|
|
|
|
|
assert(ip <= in_end);
|
|
|
|
x_len = pd(ip, ip_sav);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x_len > m_len)
|
|
|
|
{
|
|
|
|
m_len = x_len;
|
|
|
|
m_off = x_off;
|
|
|
|
assert((m_pos_sav = z_pos) != NULL);
|
|
|
|
|
|
|
|
if (ip >= MATCH_IP_END)
|
|
|
|
{
|
|
|
|
ip = ip_sav;
|
2015-04-12 06:22:03 +00:00
|
|
|
#if 0
|
2015-05-19 03:24:56 +00:00
|
|
|
/* not needed - we are at the end */
|
|
|
|
d -= DD_SIZE - j;
|
|
|
|
assert(d == &dict [ DINDEX(dv, ip) ]);
|
|
|
|
UPDATE_P(d, drun, ip, in);
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
2015-05-19 03:24:56 +00:00
|
|
|
goto match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (x_len == m_len && x_off < m_off)
|
|
|
|
{
|
|
|
|
m_off = x_off;
|
|
|
|
assert((m_pos_sav = z_pos) != NULL);
|
|
|
|
}
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#else
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
|
|
/* try to find a closer match */
|
|
|
|
if (m_len < M2_MAX_LEN + 1 || x_off < m_off)
|
|
|
|
{
|
|
|
|
m_len = M2_MAX_LEN + 1;
|
|
|
|
m_off = x_off;
|
|
|
|
assert((m_pos_sav = z_pos) != NULL);
|
|
|
|
}
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2015-05-19 03:24:56 +00:00
|
|
|
/* don't search for a longer/closer match */
|
|
|
|
m_len = M2_MAX_LEN + 1;
|
|
|
|
m_off = x_off;
|
|
|
|
assert((m_pos_sav = z_pos) != NULL);
|
|
|
|
ip = ip_sav;
|
|
|
|
d -= DD_SIZE - j;
|
|
|
|
assert(d == &dict [ DINDEX(dv, ip) ]);
|
|
|
|
UPDATE_P(d, drun, ip, in);
|
|
|
|
goto match;
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
2015-05-19 03:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ip = ip_sav;
|
2015-04-12 06:22:03 +00:00
|
|
|
}
|
2015-05-19 03:24:56 +00:00
|
|
|
else
|
|
|
|
ip = ip_sav;
|
|
|
|
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
while (--j > 0);
|
|
|
|
|
|
|
|
assert(ip == ip_sav);
|
|
|
|
|
|
|
|
d -= DD_SIZE;
|
|
|
|
assert(d == &dict [ DINDEX(dv, ip) ]);
|
|
|
|
UPDATE_P(d, drun, ip, in);
|
|
|
|
}
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
#endif /* (DD_BITS == 0) */
|
|
|
|
|
|
|
|
|
|
|
|
/* vim:set ts=4 sw=4 et: */
|