kusano 7d535a
/* lzo1b_c.ch -- implementation of the LZO1B 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
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
#if !defined(LZO_HAVE_R1) && !defined(LZO_NO_R1)
kusano 7d535a
#  define LZO_HAVE_R1
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if !defined(LZO_HAVE_M3) && !defined(LZO_NO_M3)
kusano 7d535a
#  if (M3O_BITS < 8)
kusano 7d535a
#    define LZO_HAVE_M3
kusano 7d535a
#  endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
#define MI
kusano 7d535a
#define SI      MI
kusano 7d535a
#if (DD_BITS > 0)
kusano 7d535a
#define DI      ++ii; DVAL_NEXT(dv,ii); UPDATE_D(dict,drun,dv,ii,in); MI
kusano 7d535a
#define XI      assert(ii < ip); ii = ip; DVAL_FIRST(dv,(ip));
kusano 7d535a
#else
kusano 7d535a
#define DI      ++ii; DINDEX1(dindex,ii); UPDATE_I(dict,0,dindex,ii,in); MI
kusano 7d535a
#define XI      assert(ii < ip); ii = ip;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// compress a block of data.
kusano 7d535a
//
kusano 7d535a
// I really apologize for this spaghetti code.
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
LZO_PRIVATE(int)
kusano 7d535a
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
#if (DD_BITS > 0)
kusano 7d535a
#if defined(__LZO_HASH_INCREMENTAL)
kusano 7d535a
    lzo_xint dv;
kusano 7d535a
#endif
kusano 7d535a
    unsigned drun = 0;
kusano 7d535a
#endif
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 - MIN_LOOKAHEAD;
kusano 7d535a
    const lzo_bytep ii;
kusano 7d535a
#if defined(LZO_HAVE_R1)
kusano 7d535a
    const lzo_bytep r1 = ip_end;    /* pointer for R1 match (none yet) */
kusano 7d535a
#endif
kusano 7d535a
#if defined(LZO_HAVE_M3)
kusano 7d535a
    lzo_bytep m3 = out + 1;         /* pointer after last m3/m4 match */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    lzo_dict_p const dict = (lzo_dict_p) wrkmem;
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if defined(LZO_COLLECT_STATS)
kusano 7d535a
    lzo_stats->r_bits   = R_BITS;
kusano 7d535a
    lzo_stats->m3o_bits = M3O_BITS;
kusano 7d535a
    lzo_stats->dd_bits  = DD_BITS;
kusano 7d535a
    lzo_stats->clevel   = CLEVEL;
kusano 7d535a
    lzo_stats->d_bits   = D_BITS;
kusano 7d535a
    lzo_stats->min_lookahead  = MIN_LOOKAHEAD;
kusano 7d535a
    lzo_stats->max_lookbehind = MAX_LOOKBEHIND;
kusano 7d535a
    lzo_stats->compress_id    = LZO_CPP_MACRO_EXPAND(COMPRESS_ID);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    /* init dictionary */
kusano 7d535a
#if defined(LZO_DETERMINISTIC)
kusano 7d535a
    BZERO8_PTR(wrkmem,sizeof(lzo_dict_t),D_SIZE);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
    op = out;
kusano 7d535a
    ip = in;
kusano 7d535a
    ii = ip;            /* point to start of current literal run */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if (DD_BITS > 0)
kusano 7d535a
    DVAL_FIRST(dv,ip);
kusano 7d535a
    UPDATE_D(dict,drun,dv,ip,in);
kusano 7d535a
    ip++;
kusano 7d535a
    DVAL_NEXT(dv,ip);
kusano 7d535a
#else
kusano 7d535a
    ip++;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    assert(ip < ip_end);
kusano 7d535a
    for (;;)
kusano 7d535a
    {
kusano 7d535a
        const lzo_bytep m_pos;
kusano 7d535a
#if !defined(NDEBUG)
kusano 7d535a
        const lzo_bytep m_pos_sav = NULL;
kusano 7d535a
#endif
kusano 7d535a
        lzo_uint m_off;
kusano 7d535a
#if (DD_BITS == 0)
kusano 7d535a
        lzo_uint dindex;
kusano 7d535a
#endif
kusano 7d535a
        lzo_uint m_len;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// search for a match
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
#if !defined(LZO_SEARCH_MATCH_INCLUDE_FILE)
kusano 7d535a
#  define LZO_SEARCH_MATCH_INCLUDE_FILE     "lzo1b_sm.ch"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include LZO_SEARCH_MATCH_INCLUDE_FILE
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if !defined(LZO_TEST_MATCH_INCLUDE_FILE)
kusano 7d535a
#  define LZO_TEST_MATCH_INCLUDE_FILE       "lzo1b_tm.ch"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include LZO_TEST_MATCH_INCLUDE_FILE
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// found a literal
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
kusano 7d535a
    /* a literal */
kusano 7d535a
literal:
kusano 7d535a
#if (DD_BITS == 0)
kusano 7d535a
        UPDATE_I(dict,0,dindex,ip,in);
kusano 7d535a
#endif
kusano 7d535a
        if (++ip >= ip_end)
kusano 7d535a
            break;
kusano 7d535a
#if (DD_BITS > 0)
kusano 7d535a
        DVAL_NEXT(dv,ip);
kusano 7d535a
#endif
kusano 7d535a
        continue;
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// found a match
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
match:
kusano 7d535a
#if (DD_BITS == 0)
kusano 7d535a
        UPDATE_I(dict,0,dindex,ip,in);
kusano 7d535a
#endif
kusano 7d535a
        /* we have found a match of at least M2_MIN_LEN */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if !defined(LZO_CODE_RUN_INCLUDE_FILE)
kusano 7d535a
#  define LZO_CODE_RUN_INCLUDE_FILE     "lzo1b_cr.ch"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include LZO_CODE_RUN_INCLUDE_FILE
kusano 7d535a
kusano 7d535a
kusano 7d535a
        /* ii now points to the start of the current match */
kusano 7d535a
        assert(ii == ip);
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// code the match
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
#if !defined(LZO_CODE_MATCH_INCLUDE_FILE)
kusano 7d535a
#  define LZO_CODE_MATCH_INCLUDE_FILE   "lzo1b_cm.ch"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include LZO_CODE_MATCH_INCLUDE_FILE
kusano 7d535a
kusano 7d535a
kusano 7d535a
        /* ii now points to the start of the next literal run */
kusano 7d535a
        assert(ii == ip);
kusano 7d535a
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// end of block
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
    assert(ip <= in_end);
kusano 7d535a
kusano 7d535a
#if defined(LZO_COLLECT_STATS)
kusano 7d535a
    {
kusano 7d535a
        lzo_uint i;
kusano 7d535a
        const lzo_bytep p;
kusano 7d535a
kusano 7d535a
        for (i = 0; i < D_SIZE; i++)
kusano 7d535a
        {
kusano 7d535a
            p = dict[i];
kusano 7d535a
            if (BOUNDS_CHECKING_OFF_IN_EXPR(p == NULL || p < in || p > in_end))
kusano 7d535a
                lzo_stats->unused_dict_entries++;
kusano 7d535a
        }
kusano 7d535a
        lzo_stats->unused_dict_entries_percent =
kusano 7d535a
            100.0 * lzo_stats->unused_dict_entries / D_SIZE;
kusano 7d535a
    }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
kusano 7d535a
    /* return if op == out to indicate that we
kusano 7d535a
     * couldn't compress and didn't copy anything.
kusano 7d535a
     */
kusano 7d535a
    if (op == out)
kusano 7d535a
    {
kusano 7d535a
        *out_len = 0;
kusano 7d535a
        return LZO_E_NOT_COMPRESSIBLE;
kusano 7d535a
    }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    /* store the final literal run */
kusano 7d535a
    if (pd(in_end,ii) > 0)
kusano 7d535a
    {
kusano 7d535a
        lzo_uint t = pd(in_end,ii);
kusano 7d535a
        op = STORE_RUN(op,ii,t);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    *out_len = pd(op, out);
kusano 7d535a
    return LZO_E_OK;                /* compression went ok */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/