home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 March / PCO3_97.ISO / filesbbs / os2 / lzo026.arj / LZO026.ZIP / lzo-0.26 / src / lzo1b_cc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-11  |  3.9 KB  |  159 lines

  1. /* lzo1b_cc.c -- LZO1B compression internal entry point
  2.  
  3.    This file is part of the LZO real-time data compression library.
  4.  
  5.    Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
  6.  
  7.    The LZO library is free software; you can redistribute it and/or
  8.    modify it under the terms of the GNU General Public License as
  9.    published by the Free Software Foundation; either version 2 of
  10.    the License, or (at your option) any later version.
  11.  
  12.    The LZO library is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with the LZO library; see the file COPYING.
  19.    If not, write to the Free Software Foundation, Inc.,
  20.    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21.  
  22.    Markus F.X.J. Oberhumer
  23.    markus.oberhumer@jk.uni-linz.ac.at
  24.  */
  25.  
  26.  
  27. #define LZO_NEED_DICT_H
  28. #include "config1b.h"
  29.  
  30.  
  31. /***********************************************************************
  32. // compression internal entry point.
  33. ************************************************************************/
  34.  
  35. int _lzo1b_do_compress   ( const lzo_byte *in,  lzo_uint  in_len,
  36.                                  lzo_byte *out, lzo_uint *out_len,
  37.                                  lzo_voidp wrkmem,
  38.                                  lzo_compress_t func )
  39. {
  40.     int r;
  41. #if defined(LZO_TEST_COMPRESS_OVERRUN)
  42.     lzo_uint avail_out = *out_len;
  43. #endif
  44.  
  45.  
  46.     /* sanity check */
  47.     if (!lzo_assert(LZO1B_MEM_COMPRESS >= D_SIZE * lzo_sizeof(lzo_byte *)))
  48.         return LZO_E_ERROR;
  49.  
  50.  
  51. #if defined(LZO_COLLECT_STATS)
  52.     _lzo1b_stats_init(lzo_stats);
  53.     lzo_stats->in_len = in_len;
  54. #endif
  55.  
  56.  
  57.     /* don't try to compress a block that's too short */
  58.     if (in_len <= 0)
  59.     {
  60.         *out_len = 0;
  61.         r = LZO_E_OK;
  62.     }
  63.     else if (in_len <= MIN_LOOKAHEAD + 1)
  64.     {
  65. #if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
  66.         *out_len = 0;
  67.         r = LZO_E_NOT_COMPRESSIBLE;
  68. #else
  69.         *out_len = STORE_RUN(out,in,in_len) - out;
  70.         r = (*out_len > in_len) ? LZO_E_OK : LZO_E_ERROR;
  71. #endif
  72.     }
  73.     else
  74.         r = func(in,in_len,out,out_len,wrkmem);
  75.  
  76.  
  77. #if defined(LZO_EOF_CODE)
  78. #if defined(LZO_TEST_COMPRESS_OVERRUN)
  79.     if (r == LZO_E_OK && avail_out - *out_len < 3)
  80.         r = LZO_E_COMPRESS_OVERRUN;
  81. #endif
  82.     if (r == LZO_E_OK)
  83.     {
  84.         lzo_byte *op = out + *out_len;
  85.         *op++ = M3_MARKER | 1;
  86.         *op++ = 0;
  87.         *op++ = 0;
  88.         *out_len += 3;
  89.     }
  90. #endif
  91.  
  92.  
  93. #if defined(LZO_COLLECT_STATS)
  94.     lzo_stats->out_len = *out_len;
  95.     lzo_stats->match_bytes =
  96.        1 * lzo_stats->m1_matches + 2 * lzo_stats->m2_matches +
  97.        3 * lzo_stats->m3_matches + 4 * lzo_stats->m4_matches;
  98.     _lzo1b_stats_calc(lzo_stats);
  99. #endif
  100.  
  101.     return r;
  102. }
  103.  
  104.  
  105. /***********************************************************************
  106. // note: this is not thread safe, but as it is used for finetuning only
  107. //       we don't care
  108. ************************************************************************/
  109.  
  110. #undef lzo_stats
  111. /* lzo_stats_t is still defined */
  112.  
  113.  
  114. #if defined(LZO_COLLECT_STATS)
  115.  
  116. static lzo_stats_t lzo_statistics;
  117. lzo_stats_t * const lzo1b_stats = &lzo_statistics;
  118.  
  119.  
  120. void _lzo1b_stats_init(lzo_stats_t *lzo_stats)
  121. {
  122.     memset(lzo_stats,0,sizeof(*lzo_stats));
  123. }
  124.  
  125.  
  126. void _lzo1b_stats_calc(lzo_stats_t *lzo_stats)
  127. {
  128.     lzo_stats->matches = 
  129.        lzo_stats->m1_matches + lzo_stats->m2_matches +
  130.        lzo_stats->m3_matches + lzo_stats->m4_matches;
  131.  
  132.     lzo_stats->literal_overhead = lzo_stats->lit_runs + 
  133.        2 * (lzo_stats->r0short_runs + lzo_stats->r0fast_runs + 
  134.             lzo_stats->r0long_runs);
  135.     lzo_stats->literal_bytes = lzo_stats->literals +
  136.        lzo_stats->literal_overhead;
  137.  
  138. #if 0
  139.     assert(lzo_stats->match_bytes + lzo_stats->literal_bytes ==
  140.        lzo_stats->out_len);
  141. #endif
  142.  
  143.     lzo_stats->m2_matches -= lzo_stats->r1_matches;
  144.     lzo_stats->m2_match[M2_MIN_LEN] -= lzo_stats->r1_matches;
  145.  
  146.     if (lzo_stats->literals > 0)
  147.         lzo_stats->literal_overhead_percent =
  148.            100.0 * lzo_stats->literal_overhead / lzo_stats->literals;
  149. }
  150.  
  151.  
  152. #endif
  153.  
  154.  
  155. /*
  156. vi:ts=4
  157. */
  158.  
  159.