kusano 7d535a
/* lzo2a_9x.c -- implementation of the LZO2A-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
kusano 7d535a
#include "config2a.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
#define THRESHOLD       1           /* lower limit for match length */
kusano 7d535a
#define F            2048           /* upper limit for match length */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#define LZO2A
kusano 7d535a
#define LZO_COMPRESS_T  lzo2a_999_t
kusano 7d535a
#define lzo_swd_t       lzo2a_999_swd_t
kusano 7d535a
#include "lzo_mchw.ch"
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if (LZO_CC_BORLANDC && LZO_MM_FLAT)
kusano 7d535a
#  if ((__BORLANDC__) >= 0x0450 && (__BORLANDC__) < 0x0460)
kusano 7d535a
     /* avoid internal compiler error */
kusano 7d535a
#    pragma option -Od
kusano 7d535a
#  endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
#define putbyte(x)      *op++ = LZO_BYTE(x)
kusano 7d535a
kusano 7d535a
#define putbits(j,x) \
kusano 7d535a
    if (k == 0) bitp = op++; \
kusano 7d535a
    SETBITS(j,x); \
kusano 7d535a
    if (k >= 8) { *bitp = LZO_BYTE(MASKBITS(8)); DUMPBITS(8); \
kusano 7d535a
                    if (k > 0) bitp = op++; }
kusano 7d535a
kusano 7d535a
#define putbit(x)       putbits(1,x)
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// this is a public function, but there is no prototype in a header file
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
LZO_EXTERN(int)
kusano 7d535a
lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
kusano 7d535a
                                    lzo_bytep out, lzo_uintp out_len,
kusano 7d535a
                                    lzo_voidp wrkmem,
kusano 7d535a
                                    lzo_callback_p cb,
kusano 7d535a
                                    lzo_uint max_chain );
kusano 7d535a
kusano 7d535a
LZO_PUBLIC(int)
kusano 7d535a
lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
kusano 7d535a
                                    lzo_bytep out, lzo_uintp out_len,
kusano 7d535a
                                    lzo_voidp wrkmem,
kusano 7d535a
                                    lzo_callback_p cb,
kusano 7d535a
                                    lzo_uint max_chain )
kusano 7d535a
{
kusano 7d535a
    lzo_bytep op;
kusano 7d535a
    lzo_bytep bitp = 0;
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
    lzo_uint32 b = 0;       /* bit buffer */
kusano 7d535a
    unsigned k = 0;         /* bits in bit buffer */
kusano 7d535a
kusano 7d535a
    /* sanity check */
kusano 7d535a
    LZO_COMPILE_TIME_ASSERT(LZO2A_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
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->cb = cb;
kusano 7d535a
    c->m1 = c->m2 = c->m3 = c->m4 = 0;
kusano 7d535a
kusano 7d535a
    op = out;
kusano 7d535a
kusano 7d535a
    r = init_match(c,swd,NULL,0,0);
kusano 7d535a
    if (r != 0)
kusano 7d535a
        return r;
kusano 7d535a
    if (max_chain > 0)
kusano 7d535a
        swd->max_chain = max_chain;
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
        int lazy_match_min_gain = 0;
kusano 7d535a
        int extra1 = 0;
kusano 7d535a
        int extra2 = 0;
kusano 7d535a
        lzo_uint ahead = 0;
kusano 7d535a
kusano 7d535a
        LZO_UNUSED(extra1);
kusano 7d535a
kusano 7d535a
        m_len = c->m_len;
kusano 7d535a
        m_off = c->m_off;
kusano 7d535a
kusano 7d535a
#if (N >= 8192)
kusano 7d535a
        if (m_off >= 8192)
kusano 7d535a
        {
kusano 7d535a
            if (m_len < M3_MIN_LEN)
kusano 7d535a
                m_len = 0;
kusano 7d535a
            else
kusano 7d535a
                lazy_match_min_gain = 1;
kusano 7d535a
        }
kusano 7d535a
        else
kusano 7d535a
#endif
kusano 7d535a
        if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
kusano 7d535a
        {
kusano 7d535a
            lazy_match_min_gain = 2;
kusano 7d535a
            extra1 = 3;
kusano 7d535a
            extra2 = 2;
kusano 7d535a
        }
kusano 7d535a
        else if (m_len >= 10)
kusano 7d535a
            lazy_match_min_gain = 1;
kusano 7d535a
        else if (m_len >= 3)
kusano 7d535a
        {
kusano 7d535a
            lazy_match_min_gain = 1;
kusano 7d535a
            extra1 = 1;
kusano 7d535a
        }
kusano 7d535a
        else
kusano 7d535a
            m_len = 0;
kusano 7d535a
kusano 7d535a
kusano 7d535a
        /* try a lazy match */
kusano 7d535a
        if (lazy_match_min_gain > 0 && c->look > m_len)
kusano 7d535a
        {
kusano 7d535a
            int lit = swd->b_char;
kusano 7d535a
kusano 7d535a
            r = find_match(c,swd,1,0);
kusano 7d535a
            assert(r == 0);
kusano 7d535a
            assert(c->look > 0);
kusano 7d535a
kusano 7d535a
#if (N >= 8192)
kusano 7d535a
            if (m_off < 8192 && c->m_off >= 8192)
kusano 7d535a
                lazy_match_min_gain += extra1;
kusano 7d535a
            else
kusano 7d535a
#endif
kusano 7d535a
            if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
kusano 7d535a
            {
kusano 7d535a
                if (!(c->m_len >= M1_MIN_LEN &&
kusano 7d535a
                      c->m_len <= M1_MAX_LEN && c->m_off <= 256))
kusano 7d535a
                    lazy_match_min_gain += extra2;
kusano 7d535a
            }
kusano 7d535a
            if (c->m_len >= M1_MIN_LEN &&
kusano 7d535a
                c->m_len <= M1_MAX_LEN && c->m_off <= 256)
kusano 7d535a
            {
kusano 7d535a
                    lazy_match_min_gain -= 1;
kusano 7d535a
            }
kusano 7d535a
kusano 7d535a
            if (lazy_match_min_gain < 1)
kusano 7d535a
                lazy_match_min_gain = 1;
kusano 7d535a
kusano 7d535a
            if (c->m_len >= m_len + lazy_match_min_gain)
kusano 7d535a
            {
kusano 7d535a
                c->lazy++;
kusano 7d535a
#if !defined(NDEBUG)
kusano 7d535a
                m_len = c->m_len;
kusano 7d535a
                m_off = c->m_off;
kusano 7d535a
                assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
kusano 7d535a
                                  m_len) == 0);
kusano 7d535a
                assert(m_len >= 3 || (m_len >= 2 && m_off <= 256));
kusano 7d535a
#endif
kusano 7d535a
                /* code literal */
kusano 7d535a
                putbit(0);
kusano 7d535a
                putbyte(lit);
kusano 7d535a
                c->lit_bytes++;
kusano 7d535a
                continue;
kusano 7d535a
            }
kusano 7d535a
            else
kusano 7d535a
                ahead = 1;
kusano 7d535a
            assert(m_len > 0);
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
kusano 7d535a
        if (m_len == 0)
kusano 7d535a
        {
kusano 7d535a
            /* a literal */
kusano 7d535a
            putbit(0);
kusano 7d535a
            putbyte(swd->b_char);
kusano 7d535a
            c->lit_bytes++;
kusano 7d535a
            r = find_match(c,swd,1,0);
kusano 7d535a
            assert(r == 0);
kusano 7d535a
        }
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            assert(m_len >= M1_MIN_LEN);
kusano 7d535a
            assert(m_off > 0);
kusano 7d535a
            assert(m_off <= N);
kusano 7d535a
kusano 7d535a
            /* 2 - code match */
kusano 7d535a
            if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
kusano 7d535a
            {
kusano 7d535a
                putbit(1);
kusano 7d535a
                putbit(0);
kusano 7d535a
                putbits(2,m_len - M1_MIN_LEN);
kusano 7d535a
                putbyte(m_off - 1);
kusano 7d535a
                c->m1++;
kusano 7d535a
            }
kusano 7d535a
#if (N >= 8192)
kusano 7d535a
            else if (m_off >= 8192)
kusano 7d535a
            {
kusano 7d535a
                unsigned len = m_len;
kusano 7d535a
                assert(m_len >= M3_MIN_LEN);
kusano 7d535a
                putbit(1);
kusano 7d535a
                putbit(1);
kusano 7d535a
                putbyte(m_off & 31);
kusano 7d535a
                putbyte(m_off >> 5);
kusano 7d535a
                putbit(1);
kusano 7d535a
                len -= M3_MIN_LEN - 1;
kusano 7d535a
                while (len > 255)
kusano 7d535a
                {
kusano 7d535a
                    len -= 255;
kusano 7d535a
                    putbyte(0);
kusano 7d535a
                }
kusano 7d535a
                putbyte(len);
kusano 7d535a
                c->m4++;
kusano 7d535a
            }
kusano 7d535a
#endif
kusano 7d535a
            else
kusano 7d535a
            {
kusano 7d535a
                assert(m_len >= 3);
kusano 7d535a
kusano 7d535a
                putbit(1);
kusano 7d535a
                putbit(1);
kusano 7d535a
                if (m_len <= 9)
kusano 7d535a
                {
kusano 7d535a
                    putbyte(((m_len - 2) << 5) | (m_off & 31));
kusano 7d535a
                    putbyte(m_off >> 5);
kusano 7d535a
                    c->m2++;
kusano 7d535a
                }
kusano 7d535a
                else
kusano 7d535a
                {
kusano 7d535a
                    lzo_uint len = m_len;
kusano 7d535a
                    putbyte(m_off & 31);
kusano 7d535a
                    putbyte(m_off >> 5);
kusano 7d535a
#if (N >= 8192)
kusano 7d535a
                    putbit(0);
kusano 7d535a
#endif
kusano 7d535a
                    len -= 10 - 1;
kusano 7d535a
                    while (len > 255)
kusano 7d535a
                    {
kusano 7d535a
                        len -= 255;
kusano 7d535a
                        putbyte(0);
kusano 7d535a
                    }
kusano 7d535a
                    putbyte(len);
kusano 7d535a
                    c->m3++;
kusano 7d535a
                }
kusano 7d535a
            }
kusano 7d535a
            r = find_match(c,swd,m_len,1+ahead);
kusano 7d535a
            assert(r == 0);
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        c->codesize = pd(op, out);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
#if defined(LZO_EOF_CODE)
kusano 7d535a
    /* code EOF code */
kusano 7d535a
    putbit(1);
kusano 7d535a
    putbit(1);
kusano 7d535a
    putbyte(1 << 5);
kusano 7d535a
    putbyte(0);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    /* flush remaining bits */
kusano 7d535a
    assert(k < CHAR_BIT);
kusano 7d535a
    if (k > 0)
kusano 7d535a
    {
kusano 7d535a
        assert(b == MASKBITS(k));
kusano 7d535a
        assert(op - bitp > 1);
kusano 7d535a
        *bitp = LZO_BYTE(MASKBITS(k));
kusano 7d535a
        DUMPBITS(k);
kusano 7d535a
        assert(b == 0);
kusano 7d535a
        assert(k == 0);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    assert(c->textsize == in_len);
kusano 7d535a
    c->codesize = pd(op, out);
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\n",
kusano 7d535a
        (long) c->textsize, (long) c->codesize,
kusano 7d535a
        c->lit_bytes, c->m1, c->m2, c->m3, c->m4, c->lazy);
kusano 7d535a
#endif
kusano 7d535a
    return LZO_E_OK;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
LZO_PUBLIC(int)
kusano 7d535a
lzo2a_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 lzo2a_999_compress_callback(in,in_len,out,out_len,wrkmem,
kusano 7d535a
                                       (lzo_callback_p) 0, 0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a