lzo: update to 2.09
[tomato.git] / release / src / router / lzo / src / lzo1c_cc.c
blobf7044250a2aca1e4313b0e77b3347f9a137b9eed
1 /* lzo1c_cc.c -- LZO1C compression internal entry point
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer
6 All Rights Reserved.
8 The LZO library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 The LZO library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the LZO library; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzo/
29 #define LZO_NEED_DICT_H 1
30 #include "config1c.h"
33 /***********************************************************************
34 // compression internal entry point.
35 ************************************************************************/
37 LZO_LOCAL_IMPL(int)
38 _lzo1c_do_compress ( const lzo_bytep in, lzo_uint in_len,
39 lzo_bytep out, lzo_uintp out_len,
40 lzo_voidp wrkmem,
41 lzo_compress_t func )
43 int r;
44 #if defined(LZO_TEST_COMPRESS_OVERRUN)
45 lzo_uint avail_out = *out_len;
46 #endif
49 #if (LZO_COLLECT_STATS)
50 _lzo1c_stats_init(lzo_stats);
51 lzo_stats->in_len = in_len;
52 #endif
55 /* don't try to compress a block that's too short */
56 if (in_len == 0)
58 *out_len = 0;
59 r = LZO_E_OK;
61 else if (in_len <= MIN_LOOKAHEAD + 1)
63 #if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
64 *out_len = 0;
65 r = LZO_E_NOT_COMPRESSIBLE;
66 #else
67 *out_len = pd(STORE_RUN(out,in,in_len), out);
68 r = (*out_len > in_len) ? LZO_E_OK : LZO_E_ERROR;
69 #endif
71 else
72 r = func(in,in_len,out,out_len,wrkmem);
75 #if defined(LZO_EOF_CODE)
76 #if defined(LZO_TEST_COMPRESS_OVERRUN)
77 if (r == LZO_E_OK && avail_out - *out_len < 3)
78 r = LZO_E_COMPRESS_OVERRUN;
79 #endif
80 if (r == LZO_E_OK)
82 lzo_bytep op = out + *out_len;
83 op[0] = M3_MARKER | 1;
84 op[1] = 0;
85 op[2] = 0;
86 *out_len += 3;
88 #endif
91 #if (LZO_COLLECT_STATS)
92 lzo_stats->out_len = *out_len;
93 lzo_stats->match_bytes =
94 1 * lzo_stats->m1_matches + 2 * lzo_stats->m2_matches +
95 3 * lzo_stats->m3_matches + 4 * lzo_stats->m4_matches;
96 _lzo1c_stats_calc(lzo_stats);
97 #endif
99 return r;
103 /***********************************************************************
104 // note: this is not thread safe, but as it is used for finetuning only
105 // we don't care
106 ************************************************************************/
108 #undef lzo_stats
109 /* lzo_stats_t is still defined */
112 #if (LZO_COLLECT_STATS)
114 static lzo_stats_t lzo_statistics;
115 lzo_stats_t * const lzo1c_stats = &lzo_statistics;
118 void _lzo1c_stats_init(lzo_stats_t *lzo_stats)
120 lzo_memset(lzo_stats,0,sizeof(*lzo_stats));
124 void _lzo1c_stats_calc(lzo_stats_t *lzo_stats)
126 lzo_stats->matches =
127 lzo_stats->m1_matches + lzo_stats->m2_matches +
128 lzo_stats->m3_matches + lzo_stats->m4_matches;
130 lzo_stats->literal_overhead = lzo_stats->lit_runs +
131 2 * (lzo_stats->r0short_runs + lzo_stats->r0fast_runs +
132 lzo_stats->r0long_runs);
133 lzo_stats->literal_bytes = lzo_stats->literals +
134 lzo_stats->literal_overhead;
136 #if 0
137 assert(lzo_stats->match_bytes + lzo_stats->literal_bytes ==
138 lzo_stats->out_len);
139 #endif
141 lzo_stats->m2_matches -= lzo_stats->r1_matches;
142 lzo_stats->m2_match[M2_MIN_LEN] -= lzo_stats->r1_matches;
144 if (lzo_stats->literals > 0)
145 lzo_stats->literal_overhead_percent =
146 100.0 * lzo_stats->literal_overhead / lzo_stats->literals;
150 #endif
153 /* vim:set ts=4 sw=4 et: */