kusano 7d535a
/* lzo1b_cc.c -- LZO1B compression internal entry point
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
#define LZO_NEED_DICT_H
kusano 7d535a
#include "config1b.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// compression internal entry point.
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
int _lzo1b_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
                                 lzo_compress_t func )
kusano 7d535a
{
kusano 7d535a
    int r;
kusano 7d535a
#if defined(LZO_TEST_COMPRESS_OVERRUN)
kusano 7d535a
    lzo_uint avail_out = *out_len;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if defined(LZO_COLLECT_STATS)
kusano 7d535a
    _lzo1b_stats_init(lzo_stats);
kusano 7d535a
    lzo_stats->in_len = in_len;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
    /* don't try to compress a block that's too short */
kusano 7d535a
    if (in_len <= 0)
kusano 7d535a
    {
kusano 7d535a
        *out_len = 0;
kusano 7d535a
        r = LZO_E_OK;
kusano 7d535a
    }
kusano 7d535a
    else if (in_len <= MIN_LOOKAHEAD + 1)
kusano 7d535a
    {
kusano 7d535a
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
kusano 7d535a
        *out_len = 0;
kusano 7d535a
        r = LZO_E_NOT_COMPRESSIBLE;
kusano 7d535a
#else
kusano 7d535a
        *out_len = pd(STORE_RUN(out,in,in_len), out);
kusano 7d535a
        r = (*out_len > in_len) ? LZO_E_OK : LZO_E_ERROR;
kusano 7d535a
#endif
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
        r = func(in,in_len,out,out_len,wrkmem);
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if defined(LZO_EOF_CODE)
kusano 7d535a
#if defined(LZO_TEST_COMPRESS_OVERRUN)
kusano 7d535a
    if (r == LZO_E_OK && avail_out - *out_len < 3)
kusano 7d535a
        r = LZO_E_COMPRESS_OVERRUN;
kusano 7d535a
#endif
kusano 7d535a
    if (r == LZO_E_OK)
kusano 7d535a
    {
kusano 7d535a
        lzo_bytep op = out + *out_len;
kusano 7d535a
        *op++ = M3_MARKER | 1;
kusano 7d535a
        *op++ = 0;
kusano 7d535a
        *op++ = 0;
kusano 7d535a
        *out_len += 3;
kusano 7d535a
    }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if defined(LZO_COLLECT_STATS)
kusano 7d535a
    lzo_stats->out_len = *out_len;
kusano 7d535a
    lzo_stats->match_bytes =
kusano 7d535a
       1 * lzo_stats->m1_matches + 2 * lzo_stats->m2_matches +
kusano 7d535a
       3 * lzo_stats->m3_matches + 4 * lzo_stats->m4_matches;
kusano 7d535a
    _lzo1b_stats_calc(lzo_stats);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    return r;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************************************************
kusano 7d535a
// note: this is not thread safe, but as it is used for finetuning only
kusano 7d535a
//       we don't care
kusano 7d535a
************************************************************************/
kusano 7d535a
kusano 7d535a
#undef lzo_stats
kusano 7d535a
/* lzo_stats_t is still defined */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if defined(LZO_COLLECT_STATS)
kusano 7d535a
kusano 7d535a
static lzo_stats_t lzo_statistics;
kusano 7d535a
lzo_stats_t * const lzo1b_stats = &lzo_statistics;
kusano 7d535a
kusano 7d535a
kusano 7d535a
void _lzo1b_stats_init(lzo_stats_t *lzo_stats)
kusano 7d535a
{
kusano 7d535a
    lzo_memset(lzo_stats,0,sizeof(*lzo_stats));
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
void _lzo1b_stats_calc(lzo_stats_t *lzo_stats)
kusano 7d535a
{
kusano 7d535a
    lzo_stats->matches =
kusano 7d535a
       lzo_stats->m1_matches + lzo_stats->m2_matches +
kusano 7d535a
       lzo_stats->m3_matches + lzo_stats->m4_matches;
kusano 7d535a
kusano 7d535a
    lzo_stats->literal_overhead = lzo_stats->lit_runs +
kusano 7d535a
       2 * (lzo_stats->r0short_runs + lzo_stats->r0fast_runs +
kusano 7d535a
            lzo_stats->r0long_runs);
kusano 7d535a
    lzo_stats->literal_bytes = lzo_stats->literals +
kusano 7d535a
       lzo_stats->literal_overhead;
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
    assert(lzo_stats->match_bytes + lzo_stats->literal_bytes ==
kusano 7d535a
       lzo_stats->out_len);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    lzo_stats->m2_matches -= lzo_stats->r1_matches;
kusano 7d535a
    lzo_stats->m2_match[M2_MIN_LEN] -= lzo_stats->r1_matches;
kusano 7d535a
kusano 7d535a
    if (lzo_stats->literals > 0)
kusano 7d535a
        lzo_stats->literal_overhead_percent =
kusano 7d535a
           100.0 * lzo_stats->literal_overhead / lzo_stats->literals;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a