Tizen 2.1 base
[external/lzo2.git] / src / lzo1b_sm.ch
1 /* lzo1b_sm.ch -- implementation of the LZO1B compression algorithm
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 /* WARNING: this file should *not* be used by applications. It is
42    part of the implementation of the library and is subject
43    to change.
44  */
45
46
47
48 /***********************************************************************
49 // search for a match
50 ************************************************************************/
51
52 #if (DD_BITS == 0)
53
54         /* search ip in the dictionary */
55         DINDEX1(dindex,ip);
56         GINDEX(m_pos,m_off,dict,dindex,in);
57         if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M3_MAX_OFFSET))
58             goto literal;
59 #if 1
60         if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
61             goto try_match;
62         DINDEX2(dindex,ip);
63 #endif
64         GINDEX(m_pos,m_off,dict,dindex,in);
65         if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M3_MAX_OFFSET))
66             goto literal;
67         if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
68             goto try_match;
69         goto literal;
70
71
72 #else /* (DD_BITS == 0) */
73
74
75
76         /* search ip in the deepened dictionary */
77         {
78             lzo_dict_p d = &dict [ DINDEX(dv,ip) ];
79             const lzo_bytep ip_sav;
80             unsigned j = DD_SIZE;
81             lzo_uint x_len;
82             lzo_uint x_off;
83
84             DVAL_ASSERT(dv,ip);
85
86             ip_sav = ip;
87             m_len = 0;
88             do {
89 #if !defined(NDEBUG)
90                 const lzo_bytep z_pos = NULL;
91 #endif
92 #if defined(LZO_DICT_USE_PTR)
93                 m_pos = *d;
94                 assert((z_pos = m_pos) == *d);
95 #if defined(LZO_DETERMINISTIC)
96                 assert(m_pos == NULL || m_pos >= in);
97                 assert(m_pos == NULL || m_pos < ip);
98 #endif
99 #else
100                 x_off = *d;
101 #endif
102                 assert(ip == ip_sav);
103
104                 if (LZO_CHECK_MPOS(m_pos,x_off,in,ip,MAX_OFFSET))
105 #if (CLEVEL == 9)
106                     *d = DENTRY(ip,in);
107 #else
108                     ((void)(0));
109 #endif
110                 else if (m_pos[m_len] != ip[m_len])
111                     ((void)(0));
112                 else if (*m_pos++ == *ip++ && *m_pos++ == *ip++ && *m_pos++ == *ip++)
113                 {
114 #if !defined(LZO_DICT_USE_PTR)
115                     assert((z_pos = ip - 3 - x_off) == (m_pos - 3));
116 #endif
117                     /* a match */
118                     if (MATCH_M2)
119                     {
120                         x_len = pd((ip - 1), ip_sav);
121                         if (x_len > m_len)
122                         {
123                             m_len = x_len;
124                             m_off = x_off;
125                             assert((m_pos_sav = z_pos) != NULL);
126                         }
127 #if (CLEVEL == 9)
128                         /* try to find a closer match */
129                         else if (x_len == m_len && x_off < m_off)
130                         {
131                             m_off = x_off;
132                             assert((m_pos_sav = z_pos) != NULL);
133                         }
134 #endif
135                     }
136                     else
137                     {
138                         assert((ip - ip_sav) == M2_MAX_LEN + 1);
139 #if (CLEVEL == 9)
140 #if defined(MATCH_IP_END)
141                         {
142                             const lzo_bytep end;
143                             end = MATCH_IP_END;
144                             while (ip < end  &&  *m_pos == *ip)
145                                 m_pos++, ip++;
146                             assert(ip <= in_end);
147                             x_len = pd(ip, ip_sav);
148                         }
149                         if (x_len > m_len)
150                         {
151                             m_len = x_len;
152                             m_off = x_off;
153                             assert((m_pos_sav = z_pos) != NULL);
154                             if (ip >= MATCH_IP_END)
155                             {
156                                 ip = ip_sav;
157 #if 0
158                                 /* not needed - we are at the end */
159                                 d -= DD_SIZE - j;
160                                 assert(d == &dict [ DINDEX(dv,ip) ]);
161                                 UPDATE_P(d,drun,ip,in);
162 #endif
163                                 goto match;
164                             }
165                         }
166                         else if (x_len == m_len && x_off < m_off)
167                         {
168                             m_off = x_off;
169                             assert((m_pos_sav = z_pos) != NULL);
170                         }
171 #else
172                         /* try to find a closer match */
173                         if (m_len < M2_MAX_LEN + 1 || x_off < m_off)
174                         {
175                             m_len = M2_MAX_LEN + 1;
176                             m_off = x_off;
177                             assert((m_pos_sav = z_pos) != NULL);
178                         }
179 #endif
180 #else
181                         /* don't search for a longer/closer match */
182                         m_len = M2_MAX_LEN + 1;
183                         m_off = x_off;
184                         assert((m_pos_sav = z_pos) != NULL);
185                         ip = ip_sav;
186                         d -= DD_SIZE - j;
187                         assert(d == &dict [ DINDEX(dv,ip) ]);
188                         UPDATE_P(d,drun,ip,in);
189                         goto match;
190 #endif
191                     }
192                     ip = ip_sav;
193                 }
194                 else
195                     ip = ip_sav;
196                 d++;
197             } while (--j > 0);
198             assert(ip == ip_sav);
199
200             d -= DD_SIZE;
201             assert(d == &dict [ DINDEX(dv,ip) ]);
202             UPDATE_P(d,drun,ip,in);
203         }
204
205 #endif /* (DD_BITS == 0) */
206
207
208 /*
209 vi:ts=4:et
210 */