2015-04-12 06:22:03 +00:00
|
|
|
/* lzo1a.c -- implementation of the LZO1A 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/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "lzo_conf.h"
|
|
|
|
#include <lzo/lzo1a.h>
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
// The next two defines can be changed to customize LZO1A.
|
|
|
|
// The default version is LZO1A-5/1.
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/* run bits (3 - 5) - the compressor and the decompressor
|
|
|
|
* must use the same value. */
|
|
|
|
#if !defined(RBITS)
|
|
|
|
# define RBITS 5
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* compression level (1 - 9) - this only affects the compressor.
|
|
|
|
* 1 is fastest, 9 is best compression ratio
|
|
|
|
*/
|
|
|
|
#if !defined(CLEVEL)
|
|
|
|
# define CLEVEL 1 /* fastest by default */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Collect statistics */
|
|
|
|
#if 0 && !defined(LZO_COLLECT_STATS)
|
|
|
|
# define LZO_COLLECT_STATS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
// You should not have to change anything below this line.
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/* check configuration */
|
|
|
|
#if (RBITS < 3 || RBITS > 5)
|
|
|
|
# error "invalid RBITS"
|
|
|
|
#endif
|
|
|
|
#if (CLEVEL < 1 || CLEVEL > 9)
|
|
|
|
# error "invalid CLEVEL"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
// internal configuration
|
|
|
|
// all of these affect compression only
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/* choose the hashing strategy */
|
|
|
|
#ifndef LZO_HASH
|
|
|
|
#define LZO_HASH LZO_HASH_LZO_INCREMENTAL_A
|
|
|
|
#endif
|
|
|
|
#define D_INDEX1(d,p) d = DM(DMUL(0x21,DX2(p,5,5)) >> 5)
|
|
|
|
#define D_INDEX2(d,p) d = d ^ D_MASK
|
|
|
|
|
|
|
|
#include "lzo1a_de.h"
|
|
|
|
#include "stats1a.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* check other constants */
|
|
|
|
#if (LBITS < 5 || LBITS > 8)
|
|
|
|
# error "invalid LBITS"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if (LZO_COLLECT_STATS)
|
2015-05-19 03:24:56 +00:00
|
|
|
static lzo1a_stats_t lzo_statistics;
|
|
|
|
lzo1a_stats_t* lzo1a_stats = &lzo_statistics;
|
2015-04-12 06:22:03 +00:00
|
|
|
# define lzo_stats lzo1a_stats
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
// get algorithm info, return memory required for compression
|
|
|
|
************************************************************************/
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
LZO_EXTERN(lzo_uint) lzo1a_info(int* rbits, int* clevel);
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
LZO_PUBLIC(lzo_uint)
|
2015-05-19 03:24:56 +00:00
|
|
|
lzo1a_info(int* rbits, int* clevel)
|
2015-04-12 06:22:03 +00:00
|
|
|
{
|
|
|
|
if (rbits)
|
|
|
|
*rbits = RBITS;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (clevel)
|
|
|
|
*clevel = CLEVEL;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
return D_SIZE * lzo_sizeof(lzo_bytep);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
// LZO1A decompress a block of data.
|
|
|
|
//
|
|
|
|
// Could be easily translated into assembly code.
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
LZO_PUBLIC(int)
|
2015-05-19 03:24:56 +00:00
|
|
|
lzo1a_decompress(const lzo_bytep in , lzo_uint in_len,
|
|
|
|
lzo_bytep out, lzo_uintp out_len,
|
|
|
|
lzo_voidp wrkmem)
|
2015-04-12 06:22:03 +00:00
|
|
|
{
|
|
|
|
lzo_bytep op;
|
|
|
|
const lzo_bytep ip;
|
|
|
|
lzo_uint t;
|
|
|
|
const lzo_bytep m_pos;
|
|
|
|
const lzo_bytep const ip_end = in + in_len;
|
|
|
|
|
|
|
|
LZO_UNUSED(wrkmem);
|
|
|
|
|
|
|
|
op = out;
|
|
|
|
ip = in;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
while (ip < ip_end)
|
|
|
|
{
|
|
|
|
t = *ip++; /* get marker */
|
|
|
|
LZO_STATS(lzo_stats->marker[t]++);
|
|
|
|
|
|
|
|
if (t == 0) /* a R0 literal run */
|
|
|
|
{
|
|
|
|
t = *ip++;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (t >= R0FAST - R0MIN) /* a long R0 run */
|
|
|
|
{
|
|
|
|
t -= R0FAST - R0MIN;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (t == 0)
|
|
|
|
t = R0FAST;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
t = 256u << ((unsigned) t);
|
|
|
|
#else
|
|
|
|
/* help the optimizer */
|
|
|
|
lzo_uint tt = 256;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
|
|
do tt <<= 1;
|
|
|
|
|
|
|
|
while (--t > 0);
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
t = tt;
|
|
|
|
#endif
|
|
|
|
}
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
|
|
MEMCPY8_DS(op, ip, t);
|
2015-04-12 06:22:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
t += R0MIN;
|
|
|
|
goto literal;
|
|
|
|
}
|
|
|
|
else if (t < R0MIN) /* a short literal run */
|
|
|
|
{
|
|
|
|
literal:
|
2015-05-19 03:24:56 +00:00
|
|
|
MEMCPY_DS(op, ip, t);
|
2015-04-12 06:22:03 +00:00
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* after a literal a match must follow */
|
2015-04-12 06:22:03 +00:00
|
|
|
while (ip < ip_end)
|
|
|
|
{
|
|
|
|
t = *ip++; /* get R1 marker */
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (t >= R0MIN)
|
|
|
|
goto match;
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* R1 match - a context sensitive 3 byte match + 1 byte literal */
|
2015-04-12 06:22:03 +00:00
|
|
|
assert((t & OMASK) == t);
|
|
|
|
m_pos = op - MIN_OFFSET;
|
2015-05-19 03:24:56 +00:00
|
|
|
m_pos -= t | (((lzo_uint) * ip++) << OBITS);
|
|
|
|
assert(m_pos >= out);
|
|
|
|
assert(m_pos < op);
|
2015-04-12 06:22:03 +00:00
|
|
|
*op++ = m_pos[0];
|
|
|
|
*op++ = m_pos[1];
|
|
|
|
*op++ = m_pos[2];
|
|
|
|
*op++ = *ip++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* a match */
|
|
|
|
{
|
|
|
|
match:
|
|
|
|
/* get match offset */
|
|
|
|
m_pos = op - MIN_OFFSET;
|
2015-05-19 03:24:56 +00:00
|
|
|
m_pos -= (t & OMASK) | (((lzo_uint) * ip++) << OBITS);
|
|
|
|
assert(m_pos >= out);
|
|
|
|
assert(m_pos < op);
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
/* get match len */
|
|
|
|
if (t < ((MSIZE - 1) << OBITS)) /* a short match */
|
|
|
|
{
|
|
|
|
t >>= OBITS;
|
|
|
|
*op++ = *m_pos++;
|
|
|
|
*op++ = *m_pos++;
|
2015-05-19 03:24:56 +00:00
|
|
|
MEMCPY_DS(op, m_pos, t);
|
2015-04-12 06:22:03 +00:00
|
|
|
}
|
|
|
|
else /* a long match */
|
|
|
|
{
|
|
|
|
#if (LBITS < 8)
|
|
|
|
t = (MIN_MATCH_LONG - THRESHOLD) + ((lzo_uint)(*ip++) & LMASK);
|
|
|
|
#else
|
|
|
|
t = (MIN_MATCH_LONG - THRESHOLD) + (lzo_uint)(*ip++);
|
|
|
|
#endif
|
|
|
|
*op++ = *m_pos++;
|
|
|
|
*op++ = *m_pos++;
|
2015-05-19 03:24:56 +00:00
|
|
|
MEMCPY_DS(op, m_pos, t);
|
2015-04-12 06:22:03 +00:00
|
|
|
#if (LBITS < 8)
|
|
|
|
/* a very short literal following a long match */
|
|
|
|
t = ip[-1] >> LBITS;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (t) do
|
2015-05-19 03:24:56 +00:00
|
|
|
*op++ = *ip++;
|
|
|
|
|
|
|
|
while (--t);
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = pd(op, out);
|
|
|
|
|
|
|
|
/* the next line is the only check in the decompressor */
|
|
|
|
return (ip == ip_end ? LZO_E_OK :
|
2015-05-19 03:24:56 +00:00
|
|
|
(ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
|
2015-04-12 06:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
// LZO1A compress a block of data.
|
|
|
|
//
|
|
|
|
// I apologize for the spaghetti code, but it really helps the optimizer.
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#include "lzo1a_cr.ch"
|
|
|
|
|
|
|
|
static int
|
2015-05-19 03:24:56 +00:00
|
|
|
do_compress(const lzo_bytep in , lzo_uint in_len,
|
|
|
|
lzo_bytep out, lzo_uintp out_len,
|
|
|
|
lzo_voidp wrkmem)
|
2015-04-12 06:22:03 +00:00
|
|
|
{
|
|
|
|
const lzo_bytep ip;
|
|
|
|
#if defined(__LZO_HASH_INCREMENTAL)
|
|
|
|
lzo_xint dv;
|
|
|
|
#endif
|
|
|
|
const lzo_bytep m_pos;
|
|
|
|
lzo_bytep op;
|
2015-05-19 03:24:56 +00:00
|
|
|
const lzo_bytep const ip_end = in + in_len - DVAL_LEN - MIN_MATCH_LONG;
|
|
|
|
const lzo_bytep const in_end = in + in_len - DVAL_LEN;
|
2015-04-12 06:22:03 +00:00
|
|
|
const lzo_bytep ii;
|
|
|
|
lzo_dict_p const dict = (lzo_dict_p) wrkmem;
|
|
|
|
const lzo_bytep r1 = ip_end; /* pointer for R1 match (none yet) */
|
|
|
|
#if (LBITS < 8)
|
|
|
|
const lzo_bytep im = ip_end; /* pointer to last match start */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
const lzo_bytep m_pos_sav;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
op = out;
|
|
|
|
ip = in;
|
|
|
|
ii = ip; /* point to start of current literal run */
|
|
|
|
|
|
|
|
/* init dictionary */
|
|
|
|
#if (LZO_DETERMINISTIC)
|
2015-05-19 03:24:56 +00:00
|
|
|
BZERO8_PTR(wrkmem, sizeof(lzo_dict_t), D_SIZE);
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
DVAL_FIRST(dv, ip);
|
|
|
|
UPDATE_D(dict, 0, dv, ip, in);
|
|
|
|
ip++;
|
|
|
|
DVAL_NEXT(dv, ip);
|
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
|
|
|
LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0);
|
|
|
|
lzo_uint dindex;
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
DINDEX1(dindex, ip);
|
|
|
|
GINDEX(m_pos, m_off, dict, dindex, in);
|
|
|
|
|
|
|
|
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET))
|
2015-04-12 06:22:03 +00:00
|
|
|
goto literal;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
|
|
|
|
goto match;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
|
|
DINDEX2(dindex, ip);
|
|
|
|
GINDEX(m_pos, m_off, dict, dindex, in);
|
|
|
|
|
|
|
|
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET))
|
2015-04-12 06:22:03 +00:00
|
|
|
goto literal;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
|
|
|
|
goto match;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
goto literal;
|
|
|
|
|
|
|
|
literal:
|
2015-05-19 03:24:56 +00:00
|
|
|
UPDATE_I(dict, 0, dindex, ip, in);
|
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (++ip >= ip_end)
|
|
|
|
break;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
match:
|
2015-05-19 03:24:56 +00:00
|
|
|
UPDATE_I(dict, 0, dindex, ip, in);
|
2015-04-12 06:22:03 +00:00
|
|
|
#if !defined(NDEBUG) && (LZO_DICT_USE_PTR)
|
|
|
|
assert(m_pos == NULL || m_pos >= in);
|
|
|
|
m_pos_sav = m_pos;
|
|
|
|
#endif
|
|
|
|
m_pos += 3;
|
|
|
|
{
|
2015-05-19 03:24:56 +00:00
|
|
|
/* we have found a match (of at least length 3) */
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
#if !defined(NDEBUG) && !(LZO_DICT_USE_PTR)
|
|
|
|
assert((m_pos_sav = ip - m_off) == (m_pos - 3));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(m_pos >= in);
|
|
|
|
assert(ip < ip_end);
|
|
|
|
|
|
|
|
/* 1) store the current literal run */
|
2015-05-19 03:24:56 +00:00
|
|
|
if (pd(ip, ii) > 0)
|
2015-04-12 06:22:03 +00:00
|
|
|
{
|
2015-05-19 03:24:56 +00:00
|
|
|
lzo_uint t = pd(ip, ii);
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
if (ip - r1 == MIN_MATCH + 1)
|
|
|
|
{
|
2015-05-19 03:24:56 +00:00
|
|
|
/* Code a context sensitive R1 match.
|
|
|
|
* This is tricky and somewhat difficult to explain:
|
|
|
|
* multiplex a literal run of length 1 into the previous
|
|
|
|
* short match of length MIN_MATCH.
|
|
|
|
* The key idea is:
|
|
|
|
* - after a short run a match MUST follow
|
|
|
|
* - therefore the value m = 000 in the mmmooooo marker is free
|
|
|
|
* - use 000ooooo to indicate a MIN_MATCH match (this
|
|
|
|
* is already coded) plus a 1 byte literal
|
|
|
|
*/
|
2015-04-12 06:22:03 +00:00
|
|
|
assert(t == 1);
|
|
|
|
/* modify marker byte */
|
|
|
|
assert((op[-2] >> OBITS) == (MIN_MATCH - THRESHOLD));
|
|
|
|
op[-2] &= OMASK;
|
|
|
|
assert((op[-2] >> OBITS) == 0);
|
|
|
|
/* copy 1 literal */
|
|
|
|
*op++ = *ii;
|
|
|
|
LZO_STATS(lzo_stats->r1_matches++);
|
|
|
|
r1 = ip; /* set new R1 pointer */
|
|
|
|
}
|
|
|
|
else if (t < R0MIN)
|
|
|
|
{
|
|
|
|
/* inline the copying of a short run */
|
|
|
|
#if (LBITS < 8)
|
2015-05-19 03:24:56 +00:00
|
|
|
if (t < (1 << (8 - LBITS)) && ii - im >= MIN_MATCH_LONG)
|
2015-04-12 06:22:03 +00:00
|
|
|
{
|
2015-05-19 03:24:56 +00:00
|
|
|
/* Code a very short literal run into the
|
|
|
|
* previous long match length byte.
|
|
|
|
*/
|
2015-04-12 06:22:03 +00:00
|
|
|
LZO_STATS(lzo_stats->lit_runs_after_long_match++);
|
|
|
|
LZO_STATS(lzo_stats->lit_run_after_long_match[t]++);
|
|
|
|
assert(ii - im <= MAX_MATCH_LONG);
|
|
|
|
assert((op[-1] >> LBITS) == 0);
|
|
|
|
op[-1] = LZO_BYTE(op[-1] | (t << LBITS));
|
|
|
|
MEMCPY_DS(op, ii, t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
LZO_STATS(lzo_stats->lit_runs++);
|
|
|
|
LZO_STATS(lzo_stats->lit_run[t]++);
|
|
|
|
*op++ = LZO_BYTE(t);
|
|
|
|
MEMCPY_DS(op, ii, t);
|
|
|
|
r1 = ip; /* set new R1 pointer */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (t < R0FAST)
|
|
|
|
{
|
|
|
|
/* inline the copying of a short R0 run */
|
|
|
|
LZO_STATS(lzo_stats->r0short_runs++);
|
2015-05-19 03:24:56 +00:00
|
|
|
*op++ = 0;
|
|
|
|
*op++ = LZO_BYTE(t - R0MIN);
|
2015-04-12 06:22:03 +00:00
|
|
|
MEMCPY_DS(op, ii, t);
|
|
|
|
r1 = ip; /* set new R1 pointer */
|
|
|
|
}
|
|
|
|
else
|
2015-05-19 03:24:56 +00:00
|
|
|
op = store_run(op, ii, t);
|
2015-04-12 06:22:03 +00:00
|
|
|
}
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#if (LBITS < 8)
|
|
|
|
im = ip;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* 2) compute match len */
|
|
|
|
ii = ip; /* point to start of current match */
|
|
|
|
|
|
|
|
/* we already matched MIN_MATCH bytes,
|
|
|
|
* m_pos also already advanced MIN_MATCH bytes */
|
|
|
|
ip += MIN_MATCH;
|
|
|
|
assert(m_pos < ip);
|
|
|
|
|
|
|
|
/* try to match another MIN_MATCH_LONG - MIN_MATCH bytes
|
|
|
|
* to see if we get a long match */
|
|
|
|
|
|
|
|
#define PS *m_pos++ != *ip++
|
|
|
|
|
|
|
|
#if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (PS || PS)
|
|
|
|
#elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */
|
|
|
|
if (PS || PS || PS || PS || PS || PS)
|
|
|
|
#elif (MIN_MATCH_LONG - MIN_MATCH == 14) /* MBITS == 4 */
|
|
|
|
if (PS || PS || PS || PS || PS || PS || PS ||
|
2015-05-19 03:24:56 +00:00
|
|
|
PS || PS || PS || PS || PS || PS || PS)
|
2015-04-12 06:22:03 +00:00
|
|
|
#elif (MIN_MATCH_LONG - MIN_MATCH == 30) /* MBITS == 5 */
|
|
|
|
if (PS || PS || PS || PS || PS || PS || PS || PS ||
|
2015-05-19 03:24:56 +00:00
|
|
|
PS || PS || PS || PS || PS || PS || PS || PS ||
|
|
|
|
PS || PS || PS || PS || PS || PS || PS || PS ||
|
|
|
|
PS || PS || PS || PS || PS || PS)
|
2015-04-12 06:22:03 +00:00
|
|
|
#else
|
|
|
|
# error "MBITS not yet implemented"
|
|
|
|
#endif
|
|
|
|
{
|
2015-05-19 03:24:56 +00:00
|
|
|
/* we've found a short match */
|
2015-04-12 06:22:03 +00:00
|
|
|
lzo_uint m_len;
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* 2a) compute match parameters */
|
|
|
|
assert(ip - m_pos == (int)m_off);
|
2015-04-12 06:22:03 +00:00
|
|
|
--ip; /* ran one too far, point back to non-match */
|
|
|
|
m_len = pd(ip, ii);
|
2015-05-19 03:24:56 +00:00
|
|
|
assert(m_len >= MIN_MATCH_SHORT);
|
|
|
|
assert(m_len <= MAX_MATCH_SHORT);
|
|
|
|
assert(m_off >= MIN_OFFSET);
|
|
|
|
assert(m_off <= MAX_OFFSET);
|
|
|
|
assert(ii - m_off == m_pos_sav);
|
|
|
|
assert(lzo_memcmp(m_pos_sav, ii, m_len) == 0);
|
2015-04-12 06:22:03 +00:00
|
|
|
m_off -= MIN_OFFSET;
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* 2b) code a short match */
|
2015-04-12 06:22:03 +00:00
|
|
|
/* code short match len + low offset bits */
|
|
|
|
*op++ = LZO_BYTE(((m_len - THRESHOLD) << OBITS) |
|
|
|
|
(m_off & OMASK));
|
|
|
|
/* code high offset bits */
|
|
|
|
*op++ = LZO_BYTE(m_off >> OBITS);
|
|
|
|
|
|
|
|
|
|
|
|
#if (LZO_COLLECT_STATS)
|
|
|
|
lzo_stats->short_matches++;
|
|
|
|
lzo_stats->short_match[m_len]++;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (m_off < OSIZE)
|
|
|
|
lzo_stats->short_match_offset_osize[m_len]++;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (m_off < 256)
|
|
|
|
lzo_stats->short_match_offset_256[m_len]++;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
if (m_off < 1024)
|
|
|
|
lzo_stats->short_match_offset_1024[m_len]++;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* 2c) Insert phrases (beginning with ii+1) into the dictionary. */
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
#define SI /* nothing */
|
|
|
|
#define DI ++ii; DVAL_NEXT(dv,ii); UPDATE_D(dict,0,dv,ii,in);
|
|
|
|
#define XI assert(ii < ip); ii = ip; DVAL_FIRST(dv,(ip));
|
|
|
|
|
|
|
|
#if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3)
|
2015-05-19 03:24:56 +00:00
|
|
|
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
2015-04-12 06:22:03 +00:00
|
|
|
++ii;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
DVAL_NEXT(dv, ii);
|
|
|
|
UPDATE_D(dict, 0, dv, ii, in);
|
|
|
|
}
|
|
|
|
while (++ii < ip);
|
|
|
|
|
|
|
|
DVAL_NEXT(dv, ii);
|
2015-04-12 06:22:03 +00:00
|
|
|
assert(ii == ip);
|
2015-05-19 03:24:56 +00:00
|
|
|
DVAL_ASSERT(dv, ip);
|
2015-04-12 06:22:03 +00:00
|
|
|
#elif (CLEVEL >= 3)
|
|
|
|
SI DI DI XI
|
|
|
|
#elif (CLEVEL >= 2)
|
|
|
|
SI DI XI
|
|
|
|
#else
|
2015-05-19 03:24:56 +00:00
|
|
|
XI
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-19 03:24:56 +00:00
|
|
|
/* we've found a long match - see how far we can still go */
|
2015-04-12 06:22:03 +00:00
|
|
|
const lzo_bytep end;
|
|
|
|
lzo_uint m_len;
|
|
|
|
|
|
|
|
assert(ip <= in_end);
|
|
|
|
assert(ii == ip - MIN_MATCH_LONG);
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
if (pd(in_end, ip) <= (MAX_MATCH_LONG - MIN_MATCH_LONG))
|
2015-04-12 06:22:03 +00:00
|
|
|
end = in_end;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
end = ip + (MAX_MATCH_LONG - MIN_MATCH_LONG);
|
|
|
|
assert(end < in_end);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ip < end && *m_pos == *ip)
|
|
|
|
m_pos++, ip++;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
assert(ip <= in_end);
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* 2a) compute match parameters */
|
2015-04-12 06:22:03 +00:00
|
|
|
m_len = pd(ip, ii);
|
2015-05-19 03:24:56 +00:00
|
|
|
assert(m_len >= MIN_MATCH_LONG);
|
|
|
|
assert(m_len <= MAX_MATCH_LONG);
|
|
|
|
assert(m_off >= MIN_OFFSET);
|
|
|
|
assert(m_off <= MAX_OFFSET);
|
|
|
|
assert(ii - m_off == m_pos_sav);
|
|
|
|
assert(lzo_memcmp(m_pos_sav, ii, m_len) == 0);
|
|
|
|
assert(pd(ip, m_pos) == m_off);
|
2015-04-12 06:22:03 +00:00
|
|
|
m_off -= MIN_OFFSET;
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* 2b) code the long match */
|
2015-04-12 06:22:03 +00:00
|
|
|
/* code long match flag + low offset bits */
|
|
|
|
*op++ = LZO_BYTE(((MSIZE - 1) << OBITS) | (m_off & OMASK));
|
|
|
|
/* code high offset bits */
|
|
|
|
*op++ = LZO_BYTE(m_off >> OBITS);
|
|
|
|
/* code match len */
|
|
|
|
*op++ = LZO_BYTE(m_len - MIN_MATCH_LONG);
|
|
|
|
|
|
|
|
|
|
|
|
#if (LZO_COLLECT_STATS)
|
|
|
|
lzo_stats->long_matches++;
|
|
|
|
lzo_stats->long_match[m_len]++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
/* 2c) Insert phrases (beginning with ii+1) into the dictionary. */
|
2015-04-12 06:22:03 +00:00
|
|
|
#if (CLEVEL == 9)
|
2015-05-19 03:24:56 +00:00
|
|
|
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
|
|
|
/* This is not recommended because it is slow. */
|
2015-04-12 06:22:03 +00:00
|
|
|
++ii;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
DVAL_NEXT(dv, ii);
|
|
|
|
UPDATE_D(dict, 0, dv, ii, in);
|
|
|
|
}
|
|
|
|
while (++ii < ip);
|
|
|
|
|
|
|
|
DVAL_NEXT(dv, ii);
|
2015-04-12 06:22:03 +00:00
|
|
|
assert(ii == ip);
|
2015-05-19 03:24:56 +00:00
|
|
|
DVAL_ASSERT(dv, ip);
|
2015-04-12 06:22:03 +00:00
|
|
|
#elif (CLEVEL >= 8)
|
|
|
|
SI DI DI DI DI DI DI DI DI XI
|
|
|
|
#elif (CLEVEL >= 7)
|
|
|
|
SI DI DI DI DI DI DI DI XI
|
|
|
|
#elif (CLEVEL >= 6)
|
|
|
|
SI DI DI DI DI DI DI XI
|
|
|
|
#elif (CLEVEL >= 5)
|
|
|
|
SI DI DI DI DI XI
|
|
|
|
#elif (CLEVEL >= 4)
|
|
|
|
SI DI DI DI XI
|
|
|
|
#elif (CLEVEL >= 3)
|
|
|
|
SI DI DI XI
|
|
|
|
#elif (CLEVEL >= 2)
|
|
|
|
SI DI XI
|
|
|
|
#else
|
2015-05-19 03:24:56 +00:00
|
|
|
XI
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ii now points to the start of the next literal run */
|
|
|
|
assert(ii == ip);
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
}
|
|
|
|
while (ip < ip_end);
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
assert(ip <= in_end);
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
/* return -1 if op == out to indicate that we
|
|
|
|
* couldn't compress and didn't copy anything.
|
|
|
|
*/
|
|
|
|
if (op == out)
|
|
|
|
{
|
|
|
|
*out_len = 0;
|
|
|
|
return LZO_E_NOT_COMPRESSIBLE;
|
|
|
|
}
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* store the final literal run */
|
2015-05-19 03:24:56 +00:00
|
|
|
if (pd(in_end + DVAL_LEN, ii) > 0)
|
|
|
|
op = store_run(op, ii, pd(in_end + DVAL_LEN, ii));
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
*out_len = pd(op, out);
|
|
|
|
return 0; /* compression went ok */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
// LZO1A compress public entry point.
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
LZO_PUBLIC(int)
|
2015-05-19 03:24:56 +00:00
|
|
|
lzo1a_compress(const lzo_bytep in , lzo_uint in_len,
|
|
|
|
lzo_bytep out, lzo_uintp out_len,
|
|
|
|
lzo_voidp wrkmem)
|
2015-04-12 06:22:03 +00:00
|
|
|
{
|
|
|
|
int r = LZO_E_OK;
|
|
|
|
|
|
|
|
|
|
|
|
#if (LZO_COLLECT_STATS)
|
2015-05-19 03:24:56 +00:00
|
|
|
lzo_memset(lzo_stats, 0, sizeof(*lzo_stats));
|
2015-04-12 06:22:03 +00:00
|
|
|
lzo_stats->rbits = RBITS;
|
|
|
|
lzo_stats->clevel = CLEVEL;
|
|
|
|
lzo_stats->dbits = DBITS;
|
|
|
|
lzo_stats->lbits = LBITS;
|
|
|
|
lzo_stats->min_match_short = MIN_MATCH_SHORT;
|
|
|
|
lzo_stats->max_match_short = MAX_MATCH_SHORT;
|
|
|
|
lzo_stats->min_match_long = MIN_MATCH_LONG;
|
|
|
|
lzo_stats->max_match_long = MAX_MATCH_LONG;
|
|
|
|
lzo_stats->min_offset = MIN_OFFSET;
|
|
|
|
lzo_stats->max_offset = MAX_OFFSET;
|
|
|
|
lzo_stats->r0min = R0MIN;
|
|
|
|
lzo_stats->r0fast = R0FAST;
|
|
|
|
lzo_stats->r0max = R0MAX;
|
|
|
|
lzo_stats->in_len = in_len;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* don't try to compress a block that's too short */
|
|
|
|
if (in_len == 0)
|
|
|
|
*out_len = 0;
|
|
|
|
else if (in_len <= MIN_MATCH_LONG + DVAL_LEN + 1)
|
|
|
|
{
|
|
|
|
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
|
|
|
|
r = LZO_E_NOT_COMPRESSIBLE;
|
|
|
|
#else
|
2015-05-19 03:24:56 +00:00
|
|
|
*out_len = pd(store_run(out, in, in_len), out);
|
2015-04-12 06:22:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
2015-05-19 03:24:56 +00:00
|
|
|
r = do_compress(in, in_len, out, out_len, wrkmem);
|
2015-04-12 06:22:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if (LZO_COLLECT_STATS)
|
|
|
|
lzo_stats->short_matches -= lzo_stats->r1_matches;
|
|
|
|
lzo_stats->short_match[MIN_MATCH] -= lzo_stats->r1_matches;
|
|
|
|
lzo_stats->out_len = *out_len;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* vim:set ts=4 sw=4 et: */
|