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