kusano 7d535a
/* lzo_mchw.ch -- matching functions using a window
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
typedef struct
kusano 7d535a
{
kusano 7d535a
    int init;
kusano 7d535a
kusano 7d535a
    lzo_uint look;          /* bytes in lookahead buffer */
kusano 7d535a
kusano 7d535a
    lzo_uint m_len;
kusano 7d535a
    lzo_uint m_off;
kusano 7d535a
kusano 7d535a
    lzo_uint last_m_len;
kusano 7d535a
    lzo_uint last_m_off;
kusano 7d535a
kusano 7d535a
    const lzo_bytep bp;
kusano 7d535a
    const lzo_bytep ip;
kusano 7d535a
    const lzo_bytep in;
kusano 7d535a
    const lzo_bytep in_end;
kusano 7d535a
    lzo_bytep out;
kusano 7d535a
kusano 7d535a
    lzo_callback_p cb;
kusano 7d535a
kusano 7d535a
    lzo_uint textsize;      /* text size counter */
kusano 7d535a
    lzo_uint codesize;      /* code size counter */
kusano 7d535a
    lzo_uint printcount;    /* counter for reporting progress every 1K bytes */
kusano 7d535a
kusano 7d535a
    /* some stats */
kusano 7d535a
    unsigned long lit_bytes;
kusano 7d535a
    unsigned long match_bytes;
kusano 7d535a
    unsigned long rep_bytes;
kusano 7d535a
    unsigned long lazy;
kusano 7d535a
kusano 7d535a
#if defined(LZO1B)
kusano 7d535a
    lzo_uint r1_m_len;
kusano 7d535a
kusano 7d535a
    /* some stats */
kusano 7d535a
    unsigned long r1_r, m3_r, m2_m, m3_m;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if defined(LZO1C)
kusano 7d535a
    lzo_uint r1_m_len;
kusano 7d535a
    lzo_bytep m3;
kusano 7d535a
kusano 7d535a
    /* some stats */
kusano 7d535a
    unsigned long r1_r, m3_r, m2_m, m3_m;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if defined(LZO1F)
kusano 7d535a
    lzo_uint r1_lit;
kusano 7d535a
    lzo_uint r1_m_len;
kusano 7d535a
kusano 7d535a
    /* some stats */
kusano 7d535a
    unsigned long r1_r, m2_m, m3_m;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if defined(LZO1X) || defined(LZO1Y) || defined(LZO1Z)
kusano 7d535a
    lzo_uint r1_lit;
kusano 7d535a
    lzo_uint r1_m_len;
kusano 7d535a
kusano 7d535a
    /* some stats */
kusano 7d535a
    unsigned long m1a_m, m1b_m, m2_m, m3_m, m4_m;
kusano 7d535a
    unsigned long lit1_r, lit2_r, lit3_r;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#if defined(LZO2A)
kusano 7d535a
    /* some stats */
kusano 7d535a
    unsigned long m1, m2, m3, m4;
kusano 7d535a
#endif
kusano 7d535a
}
kusano 7d535a
LZO_COMPRESS_T;
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if (LZO_CC_BORLANDC && LZO_ARCH_I086) && (__BORLANDC__ < 0x0450)
kusano 7d535a
   /* work around a Borland C 3.1 bug */
kusano 7d535a
#  define getbyte(c)  ((c).ip < (c).in_end ? (c).ip +=1, (c).ip[-1] : (-1))
kusano 7d535a
#elif defined(__TURBOC__) && defined(__TOS__)
kusano 7d535a
   /* work around a bug in Turbo C / Pure C (Atari ST) */
kusano 7d535a
#  define getbyte(c)  ((c).ip < (c).in_end ? (int)(unsigned) *((c).ip)++ : (-1))
kusano 7d535a
#else
kusano 7d535a
#  define getbyte(c)  ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include "lzo_swd.ch"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
init_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
kusano 7d535a
             const lzo_bytep dict, lzo_uint dict_len,
kusano 7d535a
             lzo_uint32 flags )
kusano 7d535a
{
kusano 7d535a
    int r;
kusano 7d535a
kusano 7d535a
    assert(!c->init);
kusano 7d535a
    c->init = 1;
kusano 7d535a
kusano 7d535a
    s->c = c;
kusano 7d535a
kusano 7d535a
    c->last_m_len = c->last_m_off = 0;
kusano 7d535a
kusano 7d535a
    c->textsize = c->codesize = c->printcount = 0;
kusano 7d535a
    c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
kusano 7d535a
    c->lazy = 0;
kusano 7d535a
kusano 7d535a
    r = swd_init(s,dict,dict_len);
kusano 7d535a
    if (r != 0)
kusano 7d535a
        return r;
kusano 7d535a
kusano 7d535a
    s->use_best_off = (flags & 1) ? 1 : 0;
kusano 7d535a
    return r;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
//
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
kusano 7d535a
             lzo_uint this_len, lzo_uint skip )
kusano 7d535a
{
kusano 7d535a
    assert(c->init);
kusano 7d535a
kusano 7d535a
    if (skip > 0)
kusano 7d535a
    {
kusano 7d535a
        assert(this_len >= skip);
kusano 7d535a
        swd_accept(s, this_len - skip);
kusano 7d535a
        c->textsize += this_len - skip + 1;
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        assert(this_len <= 1);
kusano 7d535a
        c->textsize += this_len - skip;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    s->m_len = 1;
kusano 7d535a
    s->m_len = THRESHOLD;
kusano 7d535a
#ifdef SWD_BEST_OFF
kusano 7d535a
    if (s->use_best_off)
kusano 7d535a
        lzo_memset(s->best_pos,0,sizeof(s->best_pos));
kusano 7d535a
#endif
kusano 7d535a
    swd_findbest(s);
kusano 7d535a
    c->m_len = s->m_len;
kusano 7d535a
    c->m_off = s->m_off;
kusano 7d535a
kusano 7d535a
    swd_getbyte(s);
kusano 7d535a
kusano 7d535a
    if (s->b_char < 0)
kusano 7d535a
    {
kusano 7d535a
        c->look = 0;
kusano 7d535a
        c->m_len = 0;
kusano 7d535a
        swd_exit(s);
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        c->look = s->look + 1;
kusano 7d535a
    }
kusano 7d535a
    c->bp = c->ip - c->look;
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
    /* brute force match search */
kusano 7d535a
    if (c->m_len > THRESHOLD && c->m_len + 1 <= c->look)
kusano 7d535a
    {
kusano 7d535a
        const lzo_bytep ip = c->bp;
kusano 7d535a
        const lzo_bytep m  = c->bp - c->m_off;
kusano 7d535a
        const lzo_bytep in = c->in;
kusano 7d535a
kusano 7d535a
        if (ip - in > N)
kusano 7d535a
            in = ip - N;
kusano 7d535a
        for (;;)
kusano 7d535a
        {
kusano 7d535a
            while (*in != *ip)
kusano 7d535a
                in++;
kusano 7d535a
            if (in == ip)
kusano 7d535a
                break;
kusano 7d535a
            if (in != m)
kusano 7d535a
                if (lzo_memcmp(in,ip,c->m_len+1) == 0)
kusano 7d535a
                    printf("%p %p %p %5d\n",in,ip,m,c->m_len);
kusano 7d535a
            in++;
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    if (c->cb && c->cb->nprogress && c->textsize > c->printcount)
kusano 7d535a
    {
kusano 7d535a
        (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);
kusano 7d535a
        c->printcount += 1024;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return LZO_E_OK;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a