Tizen 2.1 base
[external/lzo2.git] / src / lzo_mchw.ch
1 /* lzo_mchw.ch -- matching functions using a window
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 /***********************************************************************
42 //
43 ************************************************************************/
44
45 typedef struct
46 {
47     int init;
48
49     lzo_uint look;          /* bytes in lookahead buffer */
50
51     lzo_uint m_len;
52     lzo_uint m_off;
53
54     lzo_uint last_m_len;
55     lzo_uint last_m_off;
56
57     const lzo_bytep bp;
58     const lzo_bytep ip;
59     const lzo_bytep in;
60     const lzo_bytep in_end;
61     lzo_bytep out;
62
63     lzo_callback_p cb;
64
65     lzo_uint textsize;      /* text size counter */
66     lzo_uint codesize;      /* code size counter */
67     lzo_uint printcount;    /* counter for reporting progress every 1K bytes */
68
69     /* some stats */
70     unsigned long lit_bytes;
71     unsigned long match_bytes;
72     unsigned long rep_bytes;
73     unsigned long lazy;
74
75 #if defined(LZO1B)
76     lzo_uint r1_m_len;
77
78     /* some stats */
79     unsigned long r1_r, m3_r, m2_m, m3_m;
80 #endif
81
82 #if defined(LZO1C)
83     lzo_uint r1_m_len;
84     lzo_bytep m3;
85
86     /* some stats */
87     unsigned long r1_r, m3_r, m2_m, m3_m;
88 #endif
89
90 #if defined(LZO1F)
91     lzo_uint r1_lit;
92     lzo_uint r1_m_len;
93
94     /* some stats */
95     unsigned long r1_r, m2_m, m3_m;
96 #endif
97
98 #if defined(LZO1X) || defined(LZO1Y) || defined(LZO1Z)
99     lzo_uint r1_lit;
100     lzo_uint r1_m_len;
101
102     /* some stats */
103     unsigned long m1a_m, m1b_m, m2_m, m3_m, m4_m;
104     unsigned long lit1_r, lit2_r, lit3_r;
105 #endif
106
107 #if defined(LZO2A)
108     /* some stats */
109     unsigned long m1, m2, m3, m4;
110 #endif
111 }
112 LZO_COMPRESS_T;
113
114
115 #if (LZO_CC_BORLANDC && LZO_ARCH_I086) && (__BORLANDC__ < 0x0450)
116    /* work around a Borland C 3.1 bug */
117 #  define getbyte(c)  ((c).ip < (c).in_end ? (c).ip +=1, (c).ip[-1] : (-1))
118 #elif defined(__TURBOC__) && defined(__TOS__)
119    /* work around a bug in Turbo C / Pure C (Atari ST) */
120 #  define getbyte(c)  ((c).ip < (c).in_end ? (int)(unsigned) *((c).ip)++ : (-1))
121 #else
122 #  define getbyte(c)  ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
123 #endif
124
125 #include "lzo_swd.ch"
126
127
128 /***********************************************************************
129 //
130 ************************************************************************/
131
132 static int
133 init_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
134              const lzo_bytep dict, lzo_uint dict_len,
135              lzo_uint32 flags )
136 {
137     int r;
138
139     assert(!c->init);
140     c->init = 1;
141
142     s->c = c;
143
144     c->last_m_len = c->last_m_off = 0;
145
146     c->textsize = c->codesize = c->printcount = 0;
147     c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
148     c->lazy = 0;
149
150     r = swd_init(s,dict,dict_len);
151     if (r != 0)
152         return r;
153
154     s->use_best_off = (flags & 1) ? 1 : 0;
155     return r;
156 }
157
158
159 /***********************************************************************
160 //
161 ************************************************************************/
162
163 static int
164 find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
165              lzo_uint this_len, lzo_uint skip )
166 {
167     assert(c->init);
168
169     if (skip > 0)
170     {
171         assert(this_len >= skip);
172         swd_accept(s, this_len - skip);
173         c->textsize += this_len - skip + 1;
174     }
175     else
176     {
177         assert(this_len <= 1);
178         c->textsize += this_len - skip;
179     }
180
181     s->m_len = 1;
182     s->m_len = THRESHOLD;
183 #ifdef SWD_BEST_OFF
184     if (s->use_best_off)
185         lzo_memset(s->best_pos,0,sizeof(s->best_pos));
186 #endif
187     swd_findbest(s);
188     c->m_len = s->m_len;
189     c->m_off = s->m_off;
190
191     swd_getbyte(s);
192
193     if (s->b_char < 0)
194     {
195         c->look = 0;
196         c->m_len = 0;
197         swd_exit(s);
198     }
199     else
200     {
201         c->look = s->look + 1;
202     }
203     c->bp = c->ip - c->look;
204
205 #if 0
206     /* brute force match search */
207     if (c->m_len > THRESHOLD && c->m_len + 1 <= c->look)
208     {
209         const lzo_bytep ip = c->bp;
210         const lzo_bytep m  = c->bp - c->m_off;
211         const lzo_bytep in = c->in;
212
213         if (ip - in > N)
214             in = ip - N;
215         for (;;)
216         {
217             while (*in != *ip)
218                 in++;
219             if (in == ip)
220                 break;
221             if (in != m)
222                 if (lzo_memcmp(in,ip,c->m_len+1) == 0)
223                     printf("%p %p %p %5d\n",in,ip,m,c->m_len);
224             in++;
225         }
226     }
227 #endif
228
229     if (c->cb && c->cb->nprogress && c->textsize > c->printcount)
230     {
231         (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);
232         c->printcount += 1024;
233     }
234
235     return LZO_E_OK;
236 }
237
238
239 /*
240 vi:ts=4:et
241 */
242