kusano 7d535a
/* lzo1x_c.ch -- implementation of the LZO1[XY]-1 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
// compress a block of data.
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
static __lzo_noinline lzo_uint
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
    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 - M2_MAX_LEN - 5;
kusano 7d535a
    const lzo_bytep ii;
kusano 7d535a
    lzo_dict_p const dict = (lzo_dict_p) wrkmem;
kusano 7d535a
kusano 7d535a
    op = out;
kusano 7d535a
    ip = in;
kusano 7d535a
    ii = ip;
kusano 7d535a
kusano 7d535a
    ip += 4;
kusano 7d535a
    for (;;)
kusano 7d535a
    {
kusano 7d535a
        register const lzo_bytep m_pos;
kusano 7d535a
        lzo_uint m_off;
kusano 7d535a
        lzo_uint m_len;
kusano 7d535a
        lzo_uint dindex;
kusano 7d535a
kusano 7d535a
        DINDEX1(dindex,ip);
kusano 7d535a
        GINDEX(m_pos,m_off,dict,dindex,in);
kusano 7d535a
        if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
kusano 7d535a
            goto literal;
kusano 7d535a
#if 1
kusano 7d535a
        if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
kusano 7d535a
            goto try_match;
kusano 7d535a
        DINDEX2(dindex,ip);
kusano 7d535a
#endif
kusano 7d535a
        GINDEX(m_pos,m_off,dict,dindex,in);
kusano 7d535a
        if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
kusano 7d535a
            goto literal;
kusano 7d535a
        if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
kusano 7d535a
            goto try_match;
kusano 7d535a
        goto literal;
kusano 7d535a
kusano 7d535a
kusano 7d535a
try_match:
kusano 7d535a
#if 1 && defined(LZO_UNALIGNED_OK_2)
kusano 7d535a
        if (* (const lzo_ushortp) m_pos != * (const lzo_ushortp) ip)
kusano 7d535a
#else
kusano 7d535a
        if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
kusano 7d535a
#endif
kusano 7d535a
        {
kusano 7d535a
        }
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            if __lzo_likely(m_pos[2] == ip[2])
kusano 7d535a
            {
kusano 7d535a
#if 0
kusano 7d535a
                if (m_off <= M2_MAX_OFFSET)
kusano 7d535a
                    goto match;
kusano 7d535a
                if (lit <= 3)
kusano 7d535a
                    goto match;
kusano 7d535a
                if (lit == 3)           /* better compression, but slower */
kusano 7d535a
                {
kusano 7d535a
                    assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
kusano 7d535a
                    *op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
kusano 7d535a
                    goto code_match;
kusano 7d535a
                }
kusano 7d535a
                if (m_pos[3] == ip[3])
kusano 7d535a
#endif
kusano 7d535a
                    goto match;
kusano 7d535a
            }
kusano 7d535a
            else
kusano 7d535a
            {
kusano 7d535a
                /* still need a better way for finding M1 matches */
kusano 7d535a
#if 0
kusano 7d535a
                /* a M1 match */
kusano 7d535a
#if 0
kusano 7d535a
                if (m_off <= M1_MAX_OFFSET && lit > 0 && lit <= 3)
kusano 7d535a
#else
kusano 7d535a
                if (m_off <= M1_MAX_OFFSET && lit == 3)
kusano 7d535a
#endif
kusano 7d535a
                {
kusano 7d535a
                    register lzo_uint t;
kusano 7d535a
kusano 7d535a
                    t = lit;
kusano 7d535a
                    assert(op - 2 > out); op[-2] |= LZO_BYTE(t);
kusano 7d535a
                    do *op++ = *ii++; while (--t > 0);
kusano 7d535a
                    assert(ii == ip);
kusano 7d535a
                    m_off -= 1;
kusano 7d535a
                    *op++ = LZO_BYTE(M1_MARKER | ((m_off & 3) << 2));
kusano 7d535a
                    *op++ = LZO_BYTE(m_off >> 2);
kusano 7d535a
                    ip += 2;
kusano 7d535a
                    goto match_done;
kusano 7d535a
                }
kusano 7d535a
#endif
kusano 7d535a
            }
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
kusano 7d535a
    /* a literal */
kusano 7d535a
literal:
kusano 7d535a
        UPDATE_I(dict,0,dindex,ip,in);
kusano 7d535a
        ++ip;
kusano 7d535a
        if __lzo_unlikely(ip >= ip_end)
kusano 7d535a
            break;
kusano 7d535a
        continue;
kusano 7d535a
kusano 7d535a
kusano 7d535a
    /* a match */
kusano 7d535a
match:
kusano 7d535a
        UPDATE_I(dict,0,dindex,ip,in);
kusano 7d535a
        /* store current literal run */
kusano 7d535a
        if (pd(ip,ii) > 0)
kusano 7d535a
        {
kusano 7d535a
            register lzo_uint t = pd(ip,ii);
kusano 7d535a
kusano 7d535a
            if (t <= 3)
kusano 7d535a
            {
kusano 7d535a
                assert(op - 2 > out);
kusano 7d535a
                op[-2] |= LZO_BYTE(t);
kusano 7d535a
            }
kusano 7d535a
            else if (t <= 18)
kusano 7d535a
                *op++ = LZO_BYTE(t - 3);
kusano 7d535a
            else
kusano 7d535a
            {
kusano 7d535a
                register 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
            }
kusano 7d535a
            do *op++ = *ii++; while (--t > 0);
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        /* code the match */
kusano 7d535a
        assert(ii == ip);
kusano 7d535a
        ip += 3;
kusano 7d535a
        if (m_pos[3] != *ip++ || m_pos[4] != *ip++ || m_pos[5] != *ip++ ||
kusano 7d535a
            m_pos[6] != *ip++ || m_pos[7] != *ip++ || m_pos[8] != *ip++
kusano 7d535a
#ifdef LZO1Y
kusano 7d535a
            || m_pos[ 9] != *ip++ || m_pos[10] != *ip++ || m_pos[11] != *ip++
kusano 7d535a
            || m_pos[12] != *ip++ || m_pos[13] != *ip++ || m_pos[14] != *ip++
kusano 7d535a
#endif
kusano 7d535a
           )
kusano 7d535a
        {
kusano 7d535a
            --ip;
kusano 7d535a
            m_len = pd(ip, ii);
kusano 7d535a
            assert(m_len >= 3); assert(m_len <= M2_MAX_LEN);
kusano 7d535a
kusano 7d535a
            if (m_off <= M2_MAX_OFFSET)
kusano 7d535a
            {
kusano 7d535a
                m_off -= 1;
kusano 7d535a
#if defined(LZO1X)
kusano 7d535a
                *op++ = LZO_BYTE(((m_len - 1) << 5) | ((m_off & 7) << 2));
kusano 7d535a
                *op++ = LZO_BYTE(m_off >> 3);
kusano 7d535a
#elif defined(LZO1Y)
kusano 7d535a
                *op++ = LZO_BYTE(((m_len + 1) << 4) | ((m_off & 3) << 2));
kusano 7d535a
                *op++ = LZO_BYTE(m_off >> 2);
kusano 7d535a
#endif
kusano 7d535a
            }
kusano 7d535a
            else if (m_off <= M3_MAX_OFFSET)
kusano 7d535a
            {
kusano 7d535a
                m_off -= 1;
kusano 7d535a
                *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
kusano 7d535a
                goto m3_m4_offset;
kusano 7d535a
            }
kusano 7d535a
            else
kusano 7d535a
#if defined(LZO1X)
kusano 7d535a
            {
kusano 7d535a
                m_off -= 0x4000;
kusano 7d535a
                assert(m_off > 0); assert(m_off <= 0x7fff);
kusano 7d535a
                *op++ = LZO_BYTE(M4_MARKER |
kusano 7d535a
                                 ((m_off & 0x4000) >> 11) | (m_len - 2));
kusano 7d535a
                goto m3_m4_offset;
kusano 7d535a
            }
kusano 7d535a
#elif defined(LZO1Y)
kusano 7d535a
                goto m4_match;
kusano 7d535a
#endif
kusano 7d535a
        }
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            {
kusano 7d535a
                const lzo_bytep end = in_end;
kusano 7d535a
                const lzo_bytep m = m_pos + M2_MAX_LEN + 1;
kusano 7d535a
                while (ip < end && *m == *ip)
kusano 7d535a
                    m++, ip++;
kusano 7d535a
                m_len = pd(ip, ii);
kusano 7d535a
            }
kusano 7d535a
            assert(m_len > M2_MAX_LEN);
kusano 7d535a
kusano 7d535a
            if (m_off <= M3_MAX_OFFSET)
kusano 7d535a
            {
kusano 7d535a
                m_off -= 1;
kusano 7d535a
                if (m_len <= 33)
kusano 7d535a
                    *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
kusano 7d535a
                else
kusano 7d535a
                {
kusano 7d535a
                    m_len -= 33;
kusano 7d535a
                    *op++ = M3_MARKER | 0;
kusano 7d535a
                    goto m3_m4_len;
kusano 7d535a
                }
kusano 7d535a
            }
kusano 7d535a
            else
kusano 7d535a
            {
kusano 7d535a
#if defined(LZO1Y)
kusano 7d535a
m4_match:
kusano 7d535a
#endif
kusano 7d535a
                m_off -= 0x4000;
kusano 7d535a
                assert(m_off > 0); assert(m_off <= 0x7fff);
kusano 7d535a
                if (m_len <= M4_MAX_LEN)
kusano 7d535a
                    *op++ = LZO_BYTE(M4_MARKER |
kusano 7d535a
                                     ((m_off & 0x4000) >> 11) | (m_len - 2));
kusano 7d535a
                else
kusano 7d535a
                {
kusano 7d535a
                    m_len -= M4_MAX_LEN;
kusano 7d535a
                    *op++ = LZO_BYTE(M4_MARKER | ((m_off & 0x4000) >> 11));
kusano 7d535a
m3_m4_len:
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
            }
kusano 7d535a
kusano 7d535a
m3_m4_offset:
kusano 7d535a
            *op++ = LZO_BYTE((m_off & 63) << 2);
kusano 7d535a
            *op++ = LZO_BYTE(m_off >> 6);
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
match_done:
kusano 7d535a
#endif
kusano 7d535a
        ii = ip;
kusano 7d535a
        if __lzo_unlikely(ip >= ip_end)
kusano 7d535a
            break;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    *out_len = pd(op, out);
kusano 7d535a
    return pd(in_end,ii);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// public entry point
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
LZO_PUBLIC(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
    lzo_bytep op = out;
kusano 7d535a
    lzo_uint t;
kusano 7d535a
kusano 7d535a
    if __lzo_unlikely(in_len <= M2_MAX_LEN + 5)
kusano 7d535a
        t = in_len;
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        t = do_compress(in,in_len,op,out_len,wrkmem);
kusano 7d535a
        op += *out_len;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    if (t > 0)
kusano 7d535a
    {
kusano 7d535a
        const lzo_bytep ii = in + in_len - t;
kusano 7d535a
kusano 7d535a
        if (op == out && t <= 238)
kusano 7d535a
            *op++ = LZO_BYTE(17 + t);
kusano 7d535a
        else if (t <= 3)
kusano 7d535a
            op[-2] |= LZO_BYTE(t);
kusano 7d535a
        else if (t <= 18)
kusano 7d535a
            *op++ = LZO_BYTE(t - 3);
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
        }
kusano 7d535a
        do *op++ = *ii++; while (--t > 0);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    *op++ = M4_MARKER | 1;
kusano 7d535a
    *op++ = 0;
kusano 7d535a
    *op++ = 0;
kusano 7d535a
kusano 7d535a
    *out_len = pd(op, out);
kusano 7d535a
    return LZO_E_OK;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a