kusano 7d535a
/* lzo1x_9x.c -- implementation of the LZO1X-999 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
#if !defined(LZO1X) && !defined(LZO1Y) && !defined(LZO1Z)
kusano 7d535a
#  define LZO1X
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if defined(LZO1X)
kusano 7d535a
#  include "config1x.h"
kusano 7d535a
#elif defined(LZO1Y)
kusano 7d535a
#  include "config1y.h"
kusano 7d535a
#elif defined(LZO1Z)
kusano 7d535a
#  include "config1z.h"
kusano 7d535a
#else
kusano 7d535a
#  error
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
#define N           M4_MAX_OFFSET   /* size of ring buffer */
kusano 7d535a
#define THRESHOLD       1           /* lower limit for match length */
kusano 7d535a
#define F            2048           /* upper limit for match length */
kusano 7d535a
kusano 7d535a
#define SWD_BEST_OFF    (LZO_MAX3( M2_MAX_LEN, M3_MAX_LEN, M4_MAX_LEN ) + 1)
kusano 7d535a
kusano 7d535a
#if defined(LZO1X)
kusano 7d535a
#  define LZO_COMPRESS_T                lzo1x_999_t
kusano 7d535a
#  define lzo_swd_t                     lzo1x_999_swd_t
kusano 7d535a
#elif defined(LZO1Y)
kusano 7d535a
#  define LZO_COMPRESS_T                lzo1y_999_t
kusano 7d535a
#  define lzo_swd_t                     lzo1y_999_swd_t
kusano 7d535a
#  define lzo1x_999_compress_internal   lzo1y_999_compress_internal
kusano 7d535a
#  define lzo1x_999_compress_dict       lzo1y_999_compress_dict
kusano 7d535a
#  define lzo1x_999_compress_level      lzo1y_999_compress_level
kusano 7d535a
#  define lzo1x_999_compress            lzo1y_999_compress
kusano 7d535a
#elif defined(LZO1Z)
kusano 7d535a
#  define LZO_COMPRESS_T                lzo1z_999_t
kusano 7d535a
#  define lzo_swd_t                     lzo1z_999_swd_t
kusano 7d535a
#  define lzo1x_999_compress_internal   lzo1z_999_compress_internal
kusano 7d535a
#  define lzo1x_999_compress_dict       lzo1z_999_compress_dict
kusano 7d535a
#  define lzo1x_999_compress_level      lzo1z_999_compress_level
kusano 7d535a
#  define lzo1x_999_compress            lzo1z_999_compress
kusano 7d535a
#else
kusano 7d535a
#  error
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
#  define HEAD3(b,p) \
kusano 7d535a
    ((((((lzo_xint)b[p]<<3)^b[p+1])<<3)^b[p+2]) & (SWD_HSIZE-1))
kusano 7d535a
#endif
kusano 7d535a
#if 0 && defined(LZO_UNALIGNED_OK_4) && defined(LZO_ABI_LITTLE_ENDIAN)
kusano 7d535a
#  define HEAD3(b,p) \
kusano 7d535a
    (((* (lzo_uint32p) &b[p]) ^ ((* (lzo_uint32p) &b[p])>>10)) & (SWD_HSIZE-1))
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include "lzo_mchw.ch"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* this is a public functions, but there is no prototype in a header file */
kusano 7d535a
LZO_EXTERN(int)
kusano 7d535a
lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint  in_len,
kusano 7d535a
                                    lzo_bytep out, lzo_uintp out_len,
kusano 7d535a
                                    lzo_voidp wrkmem,
kusano 7d535a
                              const lzo_bytep dict, lzo_uint dict_len,
kusano 7d535a
                                    lzo_callback_p cb,
kusano 7d535a
                                    int try_lazy,
kusano 7d535a
                                    lzo_uint good_length,
kusano 7d535a
                                    lzo_uint max_lazy,
kusano 7d535a
                                    lzo_uint nice_length,
kusano 7d535a
                                    lzo_uint max_chain,
kusano 7d535a
                                    lzo_uint32 flags );
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
static lzo_bytep
kusano 7d535a
code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
kusano 7d535a
{
kusano 7d535a
    lzo_uint x_len = m_len;
kusano 7d535a
    lzo_uint x_off = m_off;
kusano 7d535a
kusano 7d535a
    c->match_bytes += (unsigned long) m_len;
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
/*
kusano 7d535a
    static lzo_uint last_m_len = 0, last_m_off = 0;
kusano 7d535a
    static lzo_uint prev_m_off[4];
kusano 7d535a
    static int prev_m_off_ptr = 0;
kusano 7d535a
    int i;
kusano 7d535a
kusano 7d535a
    //if (m_len >= 3 && m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
kusano 7d535a
    if (m_len >= 3 && m_len <= M2_MAX_LEN)
kusano 7d535a
    {
kusano 7d535a
    //if (m_len == last_m_len && m_off == last_m_off)
kusano 7d535a
        //printf("last_m_len + last_m_off\n");
kusano 7d535a
    //else
kusano 7d535a
    if (m_off == last_m_off)
kusano 7d535a
        printf("last_m_off\n");
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        for (i = 0; i < 4; i++)
kusano 7d535a
            if (m_off == prev_m_off[i])
kusano 7d535a
                printf("prev_m_off %d: %5ld\n",i,(long)m_off);
kusano 7d535a
    }
kusano 7d535a
    }
kusano 7d535a
    last_m_len = m_len;
kusano 7d535a
    last_m_off = prev_m_off[prev_m_off_ptr] = m_off;
kusano 7d535a
    prev_m_off_ptr = (prev_m_off_ptr + 1) & 3;
kusano 7d535a
*/
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    assert(op > c->out);
kusano 7d535a
    if (m_len == 2)
kusano 7d535a
    {
kusano 7d535a
        assert(m_off <= M1_MAX_OFFSET);
kusano 7d535a
        assert(c->r1_lit > 0); assert(c->r1_lit < 4);
kusano 7d535a
        m_off -= 1;
kusano 7d535a
#if defined(LZO1Z)
kusano 7d535a
        *op++ = LZO_BYTE(M1_MARKER | (m_off >> 6));
kusano 7d535a
        *op++ = LZO_BYTE(m_off << 2);
kusano 7d535a
#else
kusano 7d535a
        *op++ = LZO_BYTE(M1_MARKER | ((m_off & 3) << 2));
kusano 7d535a
        *op++ = LZO_BYTE(m_off >> 2);
kusano 7d535a
#endif
kusano 7d535a
        c->m1a_m++;
kusano 7d535a
    }
kusano 7d535a
#if defined(LZO1Z)
kusano 7d535a
    else if (m_len <= M2_MAX_LEN && (m_off <= M2_MAX_OFFSET || m_off == c->last_m_off))
kusano 7d535a
#else
kusano 7d535a
    else if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
kusano 7d535a
#endif
kusano 7d535a
    {
kusano 7d535a
        assert(m_len >= 3);
kusano 7d535a
#if defined(LZO1X)
kusano 7d535a
        m_off -= 1;
kusano 7d535a
        *op++ = LZO_BYTE(((m_len - 1) << 5) | ((m_off & 7) << 2));
kusano 7d535a
        *op++ = LZO_BYTE(m_off >> 3);
kusano 7d535a
        assert(op[-2] >= M2_MARKER);
kusano 7d535a
#elif defined(LZO1Y)
kusano 7d535a
        m_off -= 1;
kusano 7d535a
        *op++ = LZO_BYTE(((m_len + 1) << 4) | ((m_off & 3) << 2));
kusano 7d535a
        *op++ = LZO_BYTE(m_off >> 2);
kusano 7d535a
        assert(op[-2] >= M2_MARKER);
kusano 7d535a
#elif defined(LZO1Z)
kusano 7d535a
        if (m_off == c->last_m_off)
kusano 7d535a
            *op++ = LZO_BYTE(((m_len - 1) << 5) | (0x700 >> 6));
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            m_off -= 1;
kusano 7d535a
            *op++ = LZO_BYTE(((m_len - 1) << 5) | (m_off >> 6));
kusano 7d535a
            *op++ = LZO_BYTE(m_off << 2);
kusano 7d535a
        }
kusano 7d535a
#endif
kusano 7d535a
        c->m2_m++;
kusano 7d535a
    }
kusano 7d535a
    else if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && c->r1_lit >= 4)
kusano 7d535a
    {
kusano 7d535a
        assert(m_len == 3);
kusano 7d535a
        assert(m_off > M2_MAX_OFFSET);
kusano 7d535a
        m_off -= 1 + M2_MAX_OFFSET;
kusano 7d535a
#if defined(LZO1Z)
kusano 7d535a
        *op++ = LZO_BYTE(M1_MARKER | (m_off >> 6));
kusano 7d535a
        *op++ = LZO_BYTE(m_off << 2);
kusano 7d535a
#else
kusano 7d535a
        *op++ = LZO_BYTE(M1_MARKER | ((m_off & 3) << 2));
kusano 7d535a
        *op++ = LZO_BYTE(m_off >> 2);
kusano 7d535a
#endif
kusano 7d535a
        c->m1b_m++;
kusano 7d535a
    }
kusano 7d535a
    else if (m_off <= M3_MAX_OFFSET)
kusano 7d535a
    {
kusano 7d535a
        assert(m_len >= 3);
kusano 7d535a
        m_off -= 1;
kusano 7d535a
        if (m_len <= M3_MAX_LEN)
kusano 7d535a
            *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            m_len -= M3_MAX_LEN;
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
#if defined(LZO1Z)
kusano 7d535a
        *op++ = LZO_BYTE(m_off >> 6);
kusano 7d535a
        *op++ = LZO_BYTE(m_off << 2);
kusano 7d535a
#else
kusano 7d535a
        *op++ = LZO_BYTE(m_off << 2);
kusano 7d535a
        *op++ = LZO_BYTE(m_off >> 6);
kusano 7d535a
#endif
kusano 7d535a
        c->m3_m++;
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        lzo_uint k;
kusano 7d535a
kusano 7d535a
        assert(m_len >= 3);
kusano 7d535a
        assert(m_off > 0x4000); assert(m_off <= 0xbfff);
kusano 7d535a
        m_off -= 0x4000;
kusano 7d535a
        k = (m_off & 0x4000) >> 11;
kusano 7d535a
        if (m_len <= M4_MAX_LEN)
kusano 7d535a
            *op++ = LZO_BYTE(M4_MARKER | k | (m_len - 2));
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            m_len -= M4_MAX_LEN;
kusano 7d535a
            *op++ = LZO_BYTE(M4_MARKER | k | 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
#if defined(LZO1Z)
kusano 7d535a
        *op++ = LZO_BYTE(m_off >> 6);
kusano 7d535a
        *op++ = LZO_BYTE(m_off << 2);
kusano 7d535a
#else
kusano 7d535a
        *op++ = LZO_BYTE(m_off << 2);
kusano 7d535a
        *op++ = LZO_BYTE(m_off >> 6);
kusano 7d535a
#endif
kusano 7d535a
        c->m4_m++;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    c->last_m_len = x_len;
kusano 7d535a
    c->last_m_off = x_off;
kusano 7d535a
    return op;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static lzo_bytep
kusano 7d535a
STORE_RUN ( LZO_COMPRESS_T *c, lzo_bytep op, const lzo_bytep ii, lzo_uint t )
kusano 7d535a
{
kusano 7d535a
    c->lit_bytes += (unsigned long) t;
kusano 7d535a
kusano 7d535a
    if (op == c->out && t <= 238)
kusano 7d535a
    {
kusano 7d535a
        *op++ = LZO_BYTE(17 + t);
kusano 7d535a
    }
kusano 7d535a
    else if (t <= 3)
kusano 7d535a
    {
kusano 7d535a
#if defined(LZO1Z)
kusano 7d535a
        op[-1] |= LZO_BYTE(t);
kusano 7d535a
#else
kusano 7d535a
        op[-2] |= LZO_BYTE(t);
kusano 7d535a
#endif
kusano 7d535a
        c->lit1_r++;
kusano 7d535a
    }
kusano 7d535a
    else if (t <= 18)
kusano 7d535a
    {
kusano 7d535a
        *op++ = LZO_BYTE(t - 3);
kusano 7d535a
        c->lit2_r++;
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        lzo_uint tt = t - 18;
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
        c->lit3_r++;
kusano 7d535a
    }
kusano 7d535a
    do *op++ = *ii++; while (--t > 0);
kusano 7d535a
kusano 7d535a
    return op;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static lzo_bytep
kusano 7d535a
code_run ( LZO_COMPRESS_T *c, lzo_bytep op, const lzo_bytep ii,
kusano 7d535a
           lzo_uint lit, lzo_uint m_len )
kusano 7d535a
{
kusano 7d535a
    if (lit > 0)
kusano 7d535a
    {
kusano 7d535a
        assert(m_len >= 2);
kusano 7d535a
        op = STORE_RUN(c,op,ii,lit);
kusano 7d535a
        c->r1_m_len = m_len;
kusano 7d535a
        c->r1_lit = lit;
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        assert(m_len >= 3);
kusano 7d535a
        c->r1_m_len = 0;
kusano 7d535a
        c->r1_lit = 0;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return op;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
len_of_coded_match ( lzo_uint m_len, lzo_uint m_off, lzo_uint lit )
kusano 7d535a
{
kusano 7d535a
    int n = 4;
kusano 7d535a
kusano 7d535a
    if (m_len < 2)
kusano 7d535a
        return -1;
kusano 7d535a
    if (m_len == 2)
kusano 7d535a
        return (m_off <= M1_MAX_OFFSET && lit > 0 && lit < 4) ? 2 : -1;
kusano 7d535a
    if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
kusano 7d535a
        return 2;
kusano 7d535a
    if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
kusano 7d535a
        return 2;
kusano 7d535a
    if (m_off <= M3_MAX_OFFSET)
kusano 7d535a
    {
kusano 7d535a
        if (m_len <= M3_MAX_LEN)
kusano 7d535a
            return 3;
kusano 7d535a
        m_len -= M3_MAX_LEN;
kusano 7d535a
        while (m_len > 255)
kusano 7d535a
        {
kusano 7d535a
            m_len -= 255;
kusano 7d535a
            n++;
kusano 7d535a
        }
kusano 7d535a
        return n;
kusano 7d535a
    }
kusano 7d535a
    if (m_off <= M4_MAX_OFFSET)
kusano 7d535a
    {
kusano 7d535a
        if (m_len <= M4_MAX_LEN)
kusano 7d535a
            return 3;
kusano 7d535a
        m_len -= M4_MAX_LEN;
kusano 7d535a
        while (m_len > 255)
kusano 7d535a
        {
kusano 7d535a
            m_len -= 255;
kusano 7d535a
            n++;
kusano 7d535a
        }
kusano 7d535a
        return n;
kusano 7d535a
    }
kusano 7d535a
    return -1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static lzo_int
kusano 7d535a
min_gain(lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, int l1, int l2, int l3)
kusano 7d535a
{
kusano 7d535a
    lzo_int lazy_match_min_gain = 0;
kusano 7d535a
kusano 7d535a
    assert (ahead >= 1);
kusano 7d535a
    lazy_match_min_gain += ahead;
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
    if (l3 > 0)
kusano 7d535a
        lit2 -= ahead;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    if (lit1 <= 3)
kusano 7d535a
        lazy_match_min_gain += (lit2 <= 3) ? 0 : 2;
kusano 7d535a
    else if (lit1 <= 18)
kusano 7d535a
        lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
kusano 7d535a
kusano 7d535a
    lazy_match_min_gain += (l2 - l1) * 2;
kusano 7d535a
    if (l3 > 0)
kusano 7d535a
        lazy_match_min_gain -= (ahead - l3) * 2;
kusano 7d535a
kusano 7d535a
    if (lazy_match_min_gain < 0)
kusano 7d535a
        lazy_match_min_gain = 0;
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
    if (l1 == 2)
kusano 7d535a
        if (lazy_match_min_gain == 0)
kusano 7d535a
            lazy_match_min_gain = 1;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    return lazy_match_min_gain;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
#if !defined(NDEBUG)
kusano 7d535a
static
kusano 7d535a
void assert_match( const lzo_swd_p swd, lzo_uint m_len, lzo_uint m_off )
kusano 7d535a
{
kusano 7d535a
    const LZO_COMPRESS_T *c = swd->c;
kusano 7d535a
    lzo_uint d_off;
kusano 7d535a
kusano 7d535a
    assert(m_len >= 2);
kusano 7d535a
    if (m_off <= (lzo_uint) (c->bp - c->in))
kusano 7d535a
    {
kusano 7d535a
        assert(c->bp - m_off + m_len < c->ip);
kusano 7d535a
        assert(lzo_memcmp(c->bp, c->bp - m_off, m_len) == 0);
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        assert(swd->dict != NULL);
kusano 7d535a
        d_off = m_off - (lzo_uint) (c->bp - c->in);
kusano 7d535a
        assert(d_off <= swd->dict_len);
kusano 7d535a
        if (m_len > d_off)
kusano 7d535a
        {
kusano 7d535a
            assert(lzo_memcmp(c->bp, swd->dict_end - d_off, d_off) == 0);
kusano 7d535a
            assert(c->in + m_len - d_off < c->ip);
kusano 7d535a
            assert(lzo_memcmp(c->bp + d_off, c->in, m_len - d_off) == 0);
kusano 7d535a
        }
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            assert(lzo_memcmp(c->bp, swd->dict_end - d_off, m_len) == 0);
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
}
kusano 7d535a
#else
kusano 7d535a
#  define assert_match(a,b,c)   ((void)0)
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if defined(SWD_BEST_OFF)
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
kusano 7d535a
{
kusano 7d535a
#if defined(LZO1Z)
kusano 7d535a
    const LZO_COMPRESS_T *c = swd->c;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    if (*m_len <= M2_MIN_LEN)
kusano 7d535a
        return;
kusano 7d535a
#if defined(LZO1Z)
kusano 7d535a
    if (*m_off == c->last_m_off && *m_len <= M2_MAX_LEN)
kusano 7d535a
        return;
kusano 7d535a
#if 1
kusano 7d535a
    if (*m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
kusano 7d535a
        c->last_m_off && swd->best_off[*m_len-1] == c->last_m_off)
kusano 7d535a
    {
kusano 7d535a
        *m_len = *m_len - 1;
kusano 7d535a
        *m_off = swd->best_off[*m_len];
kusano 7d535a
        return;
kusano 7d535a
    }
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    if (*m_off <= M2_MAX_OFFSET)
kusano 7d535a
        return;
kusano 7d535a
kusano 7d535a
#if 1
kusano 7d535a
    /* M3/M4 -> M2 */
kusano 7d535a
    if (*m_off > M2_MAX_OFFSET &&
kusano 7d535a
        *m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
kusano 7d535a
        swd->best_off[*m_len-1] && swd->best_off[*m_len-1] <= M2_MAX_OFFSET)
kusano 7d535a
    {
kusano 7d535a
        *m_len = *m_len - 1;
kusano 7d535a
        *m_off = swd->best_off[*m_len];
kusano 7d535a
        return;
kusano 7d535a
    }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if 1
kusano 7d535a
    /* M4 -> M2 */
kusano 7d535a
    if (*m_off > M3_MAX_OFFSET &&
kusano 7d535a
        *m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 &&
kusano 7d535a
        swd->best_off[*m_len-2] && swd->best_off[*m_len-2] <= M2_MAX_OFFSET)
kusano 7d535a
    {
kusano 7d535a
        *m_len = *m_len - 2;
kusano 7d535a
        *m_off = swd->best_off[*m_len];
kusano 7d535a
        return;
kusano 7d535a
    }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if 1
kusano 7d535a
    /* M4 -> M3 */
kusano 7d535a
    if (*m_off > M3_MAX_OFFSET &&
kusano 7d535a
        *m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 &&
kusano 7d535a
        swd->best_off[*m_len-1] && swd->best_off[*m_len-1] <= M3_MAX_OFFSET)
kusano 7d535a
    {
kusano 7d535a
        *m_len = *m_len - 1;
kusano 7d535a
        *m_off = swd->best_off[*m_len];
kusano 7d535a
    }
kusano 7d535a
#endif
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
LZO_PUBLIC(int)
kusano 7d535a
lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint  in_len,
kusano 7d535a
                                    lzo_bytep out, lzo_uintp out_len,
kusano 7d535a
                                    lzo_voidp wrkmem,
kusano 7d535a
                              const lzo_bytep dict, lzo_uint dict_len,
kusano 7d535a
                                    lzo_callback_p cb,
kusano 7d535a
                                    int try_lazy,
kusano 7d535a
                                    lzo_uint good_length,
kusano 7d535a
                                    lzo_uint max_lazy,
kusano 7d535a
                                    lzo_uint nice_length,
kusano 7d535a
                                    lzo_uint max_chain,
kusano 7d535a
                                    lzo_uint32 flags )
kusano 7d535a
{
kusano 7d535a
    lzo_bytep op;
kusano 7d535a
    const lzo_bytep ii;
kusano 7d535a
    lzo_uint lit;
kusano 7d535a
    lzo_uint m_len, m_off;
kusano 7d535a
    LZO_COMPRESS_T cc;
kusano 7d535a
    LZO_COMPRESS_T * const c = &cc;
kusano 7d535a
    lzo_swd_p const swd = (lzo_swd_p) wrkmem;
kusano 7d535a
    int r;
kusano 7d535a
kusano 7d535a
    /* sanity check */
kusano 7d535a
#if defined(LZO1X)
kusano 7d535a
    LZO_COMPILE_TIME_ASSERT(LZO1X_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
kusano 7d535a
#elif defined(LZO1Y)
kusano 7d535a
    LZO_COMPILE_TIME_ASSERT(LZO1Y_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
kusano 7d535a
#elif defined(LZO1Z)
kusano 7d535a
    LZO_COMPILE_TIME_ASSERT(LZO1Z_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
kusano 7d535a
#else
kusano 7d535a
#  error
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/* setup parameter defaults */
kusano 7d535a
    /* number of lazy match tries */
kusano 7d535a
    if (try_lazy < 0)
kusano 7d535a
        try_lazy = 1;
kusano 7d535a
    /* reduce lazy match search if we already have a match with this length */
kusano 7d535a
    if (good_length <= 0)
kusano 7d535a
        good_length = 32;
kusano 7d535a
    /* do not try a lazy match if we already have a match with this length */
kusano 7d535a
    if (max_lazy <= 0)
kusano 7d535a
        max_lazy = 32;
kusano 7d535a
    /* stop searching for longer matches than this one */
kusano 7d535a
    if (nice_length <= 0)
kusano 7d535a
        nice_length = 0;
kusano 7d535a
    /* don't search more positions than this */
kusano 7d535a
    if (max_chain <= 0)
kusano 7d535a
        max_chain = SWD_MAX_CHAIN;
kusano 7d535a
kusano 7d535a
    c->init = 0;
kusano 7d535a
    c->ip = c->in = in;
kusano 7d535a
    c->in_end = in + in_len;
kusano 7d535a
    c->out = out;
kusano 7d535a
    c->cb = cb;
kusano 7d535a
    c->m1a_m = c->m1b_m = c->m2_m = c->m3_m = c->m4_m = 0;
kusano 7d535a
    c->lit1_r = c->lit2_r = c->lit3_r = 0;
kusano 7d535a
kusano 7d535a
    op = out;
kusano 7d535a
    ii = c->ip;             /* point to start of literal run */
kusano 7d535a
    lit = 0;
kusano 7d535a
    c->r1_lit = c->r1_m_len = 0;
kusano 7d535a
kusano 7d535a
    r = init_match(c,swd,dict,dict_len,flags);
kusano 7d535a
    if (r != 0)
kusano 7d535a
        return r;
kusano 7d535a
    if (max_chain > 0)
kusano 7d535a
        swd->max_chain = max_chain;
kusano 7d535a
    if (nice_length > 0)
kusano 7d535a
        swd->nice_length = nice_length;
kusano 7d535a
kusano 7d535a
    r = find_match(c,swd,0,0);
kusano 7d535a
    if (r != 0)
kusano 7d535a
        return r;
kusano 7d535a
    while (c->look > 0)
kusano 7d535a
    {
kusano 7d535a
        lzo_uint ahead;
kusano 7d535a
        lzo_uint max_ahead;
kusano 7d535a
        int l1, l2, l3;
kusano 7d535a
kusano 7d535a
        c->codesize = pd(op, out);
kusano 7d535a
kusano 7d535a
        m_len = c->m_len;
kusano 7d535a
        m_off = c->m_off;
kusano 7d535a
kusano 7d535a
        assert(c->bp == c->ip - c->look);
kusano 7d535a
        assert(c->bp >= in);
kusano 7d535a
        if (lit == 0)
kusano 7d535a
            ii = c->bp;
kusano 7d535a
        assert(ii + lit == c->bp);
kusano 7d535a
        assert(swd->b_char == *(c->bp));
kusano 7d535a
kusano 7d535a
        if ( m_len < 2 ||
kusano 7d535a
            (m_len == 2 && (m_off > M1_MAX_OFFSET || lit == 0 || lit >= 4)) ||
kusano 7d535a
#if 1
kusano 7d535a
            /* Do not accept this match for compressed-data compatibility
kusano 7d535a
             * with LZO v1.01 and before
kusano 7d535a
             * [ might be a problem for decompress() and optimize() ]
kusano 7d535a
             */
kusano 7d535a
            (m_len == 2 && op == out) ||
kusano 7d535a
#endif
kusano 7d535a
            (op == out && lit == 0))
kusano 7d535a
        {
kusano 7d535a
            /* a literal */
kusano 7d535a
            m_len = 0;
kusano 7d535a
        }
kusano 7d535a
        else if (m_len == M2_MIN_LEN)
kusano 7d535a
        {
kusano 7d535a
            /* compression ratio improves if we code a literal in some cases */
kusano 7d535a
            if (m_off > MX_MAX_OFFSET && lit >= 4)
kusano 7d535a
                m_len = 0;
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        if (m_len == 0)
kusano 7d535a
        {
kusano 7d535a
    /* a literal */
kusano 7d535a
            lit++;
kusano 7d535a
            swd->max_chain = max_chain;
kusano 7d535a
            r = find_match(c,swd,1,0);
kusano 7d535a
            assert(r == 0);
kusano 7d535a
            continue;
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
    /* a match */
kusano 7d535a
#if defined(SWD_BEST_OFF)
kusano 7d535a
        if (swd->use_best_off)
kusano 7d535a
            better_match(swd,&m_len,&m_off);
kusano 7d535a
#endif
kusano 7d535a
        assert_match(swd,m_len,m_off);
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
        /* shall we try a lazy match ? */
kusano 7d535a
        ahead = 0;
kusano 7d535a
        if (try_lazy <= 0 || m_len >= max_lazy)
kusano 7d535a
        {
kusano 7d535a
            /* no */
kusano 7d535a
            l1 = 0;
kusano 7d535a
            max_ahead = 0;
kusano 7d535a
        }
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            /* yes, try a lazy match */
kusano 7d535a
            l1 = len_of_coded_match(m_len,m_off,lit);
kusano 7d535a
            assert(l1 > 0);
kusano 7d535a
#if 1
kusano 7d535a
            max_ahead = LZO_MIN((lzo_uint)try_lazy, (lzo_uint)l1 - 1);
kusano 7d535a
#else
kusano 7d535a
            max_ahead = LZO_MIN3(try_lazy, l1, m_len - 1);
kusano 7d535a
#endif
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
kusano 7d535a
        while (ahead < max_ahead && c->look > m_len)
kusano 7d535a
        {
kusano 7d535a
            lzo_int lazy_match_min_gain;
kusano 7d535a
kusano 7d535a
            if (m_len >= good_length)
kusano 7d535a
                swd->max_chain = max_chain >> 2;
kusano 7d535a
            else
kusano 7d535a
                swd->max_chain = max_chain;
kusano 7d535a
            r = find_match(c,swd,1,0);
kusano 7d535a
            ahead++;
kusano 7d535a
kusano 7d535a
            assert(r == 0);
kusano 7d535a
            assert(c->look > 0);
kusano 7d535a
            assert(ii + lit + ahead == c->bp);
kusano 7d535a
kusano 7d535a
#if defined(LZO1Z)
kusano 7d535a
            if (m_off == c->last_m_off && c->m_off != c->last_m_off)
kusano 7d535a
                if (m_len >= M2_MIN_LEN && m_len <= M2_MAX_LEN)
kusano 7d535a
                    c->m_len = 0;
kusano 7d535a
#endif
kusano 7d535a
            if (c->m_len < m_len)
kusano 7d535a
                continue;
kusano 7d535a
#if 1
kusano 7d535a
            if (c->m_len == m_len && c->m_off >= m_off)
kusano 7d535a
                continue;
kusano 7d535a
#endif
kusano 7d535a
#if defined(SWD_BEST_OFF)
kusano 7d535a
            if (swd->use_best_off)
kusano 7d535a
                better_match(swd,&c->m_len,&c->m_off);
kusano 7d535a
#endif
kusano 7d535a
            l2 = len_of_coded_match(c->m_len,c->m_off,lit+ahead);
kusano 7d535a
            if (l2 < 0)
kusano 7d535a
                continue;
kusano 7d535a
#if 0
kusano 7d535a
            if (c->m_len == m_len && l2 >= l1)
kusano 7d535a
                continue;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if 1
kusano 7d535a
            /* compressed-data compatibility [see above] */
kusano 7d535a
            l3 = (op == out) ? -1 : len_of_coded_match(ahead,m_off,lit);
kusano 7d535a
#else
kusano 7d535a
            l3 = len_of_coded_match(ahead,m_off,lit);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
            lazy_match_min_gain = min_gain(ahead,lit,lit+ahead,l1,l2,l3);
kusano 7d535a
            if (c->m_len >= m_len + lazy_match_min_gain)
kusano 7d535a
            {
kusano 7d535a
                c->lazy++;
kusano 7d535a
                assert_match(swd,c->m_len,c->m_off);
kusano 7d535a
kusano 7d535a
                if (l3 > 0)
kusano 7d535a
                {
kusano 7d535a
                    /* code previous run */
kusano 7d535a
                    op = code_run(c,op,ii,lit,ahead);
kusano 7d535a
                    lit = 0;
kusano 7d535a
                    /* code shortened match */
kusano 7d535a
                    op = code_match(c,op,ahead,m_off);
kusano 7d535a
                }
kusano 7d535a
                else
kusano 7d535a
                {
kusano 7d535a
                    lit += ahead;
kusano 7d535a
                    assert(ii + lit == c->bp);
kusano 7d535a
                }
kusano 7d535a
                goto lazy_match_done;
kusano 7d535a
            }
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
kusano 7d535a
        assert(ii + lit + ahead == c->bp);
kusano 7d535a
kusano 7d535a
        /* 1 - code run */
kusano 7d535a
        op = code_run(c,op,ii,lit,m_len);
kusano 7d535a
        lit = 0;
kusano 7d535a
kusano 7d535a
        /* 2 - code match */
kusano 7d535a
        op = code_match(c,op,m_len,m_off);
kusano 7d535a
        swd->max_chain = max_chain;
kusano 7d535a
        r = find_match(c,swd,m_len,1+ahead);
kusano 7d535a
        assert(r == 0);
kusano 7d535a
kusano 7d535a
lazy_match_done: ;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
kusano 7d535a
    /* store final run */
kusano 7d535a
    if (lit > 0)
kusano 7d535a
        op = STORE_RUN(c,op,ii,lit);
kusano 7d535a
kusano 7d535a
#if defined(LZO_EOF_CODE)
kusano 7d535a
    *op++ = M4_MARKER | 1;
kusano 7d535a
    *op++ = 0;
kusano 7d535a
    *op++ = 0;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    c->codesize = pd(op, out);
kusano 7d535a
    assert(c->textsize == in_len);
kusano 7d535a
kusano 7d535a
    *out_len = pd(op, out);
kusano 7d535a
kusano 7d535a
    if (c->cb && c->cb->nprogress)
kusano 7d535a
        (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
    printf("%ld %ld -> %ld  %ld: %ld %ld %ld %ld %ld  %ld: %ld %ld %ld  %ld\n",
kusano 7d535a
        (long) c->textsize, (long) in_len, (long) c->codesize,
kusano 7d535a
        c->match_bytes, c->m1a_m, c->m1b_m, c->m2_m, c->m3_m, c->m4_m,
kusano 7d535a
        c->lit_bytes, c->lit1_r, c->lit2_r, c->lit3_r, c->lazy);
kusano 7d535a
#endif
kusano 7d535a
    assert(c->lit_bytes + c->match_bytes == in_len);
kusano 7d535a
kusano 7d535a
    return LZO_E_OK;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
LZO_PUBLIC(int)
kusano 7d535a
lzo1x_999_compress_level    ( const lzo_bytep in , lzo_uint  in_len,
kusano 7d535a
                                    lzo_bytep out, lzo_uintp out_len,
kusano 7d535a
                                    lzo_voidp wrkmem,
kusano 7d535a
                              const lzo_bytep dict, lzo_uint dict_len,
kusano 7d535a
                                    lzo_callback_p cb,
kusano 7d535a
                                    int compression_level )
kusano 7d535a
{
kusano 7d535a
    static const struct
kusano 7d535a
    {
kusano 7d535a
        int try_lazy;
kusano 7d535a
        lzo_uint good_length;
kusano 7d535a
        lzo_uint max_lazy;
kusano 7d535a
        lzo_uint nice_length;
kusano 7d535a
        lzo_uint max_chain;
kusano 7d535a
        lzo_uint32 flags;
kusano 7d535a
    } c[9] = {
kusano 7d535a
        {   0,   0,   0,   8,    4,   0 },      /* faster compression */
kusano 7d535a
        {   0,   0,   0,  16,    8,   0 },
kusano 7d535a
        {   0,   0,   0,  32,   16,   0 },
kusano 7d535a
kusano 7d535a
        {   1,   4,   4,  16,   16,   0 },
kusano 7d535a
        {   1,   8,  16,  32,   32,   0 },
kusano 7d535a
        {   1,   8,  16, 128,  128,   0 },
kusano 7d535a
kusano 7d535a
        {   2,   8,  32, 128,  256,   0 },
kusano 7d535a
        {   2,  32, 128,   F, 2048,   1 },
kusano 7d535a
        {   2,   F,   F,   F, 4096,   1 }       /* max. compression */
kusano 7d535a
    };
kusano 7d535a
kusano 7d535a
    if (compression_level < 1 || compression_level > 9)
kusano 7d535a
        return LZO_E_ERROR;
kusano 7d535a
kusano 7d535a
    compression_level -= 1;
kusano 7d535a
    return lzo1x_999_compress_internal(in, in_len, out, out_len, wrkmem,
kusano 7d535a
                                       dict, dict_len, cb,
kusano 7d535a
                                       c[compression_level].try_lazy,
kusano 7d535a
                                       c[compression_level].good_length,
kusano 7d535a
                                       c[compression_level].max_lazy,
kusano 7d535a
#if 0
kusano 7d535a
                                       c[compression_level].nice_length,
kusano 7d535a
#else
kusano 7d535a
                                       0,
kusano 7d535a
#endif
kusano 7d535a
                                       c[compression_level].max_chain,
kusano 7d535a
                                       c[compression_level].flags);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
LZO_PUBLIC(int)
kusano 7d535a
lzo1x_999_compress_dict     ( const lzo_bytep in , lzo_uint  in_len,
kusano 7d535a
                                    lzo_bytep out, lzo_uintp out_len,
kusano 7d535a
                                    lzo_voidp wrkmem,
kusano 7d535a
                              const lzo_bytep dict, lzo_uint dict_len )
kusano 7d535a
{
kusano 7d535a
    return lzo1x_999_compress_level(in, in_len, out, out_len, wrkmem,
kusano 7d535a
                                    dict, dict_len, 0, 8);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
LZO_PUBLIC(int)
kusano 7d535a
lzo1x_999_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
    return lzo1x_999_compress_level(in, in_len, out, out_len, wrkmem,
kusano 7d535a
                                    NULL, 0, (lzo_callback_p) 0, 8);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a