Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / lzo / src / lzo1b_sm.ch
blob89ba3a77d74203e09db0b565ff285aa67089dbba
1 /* lzo1b_sm.ch -- implementation of the LZO1B compression algorithm
3    This file is part of the LZO real-time data compression library.
5    Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
6    Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
7    Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
8    Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
9    Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
10    Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
11    Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
12    Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
13    Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
14    Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
15    Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
16    Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
17    Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
18    Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
19    Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
20    Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
21    All Rights Reserved.
23    The LZO library is free software; you can redistribute it and/or
24    modify it under the terms of the GNU General Public License as
25    published by the Free Software Foundation; either version 2 of
26    the License, or (at your option) any later version.
28    The LZO library is distributed in the hope that it will be useful,
29    but WITHOUT ANY WARRANTY; without even the implied warranty of
30    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31    GNU General Public License for more details.
33    You should have received a copy of the GNU General Public License
34    along with the LZO library; see the file COPYING.
35    If not, write to the Free Software Foundation, Inc.,
36    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38    Markus F.X.J. Oberhumer
39    <markus@oberhumer.com>
40    http://www.oberhumer.com/opensource/lzo/
41  */
44 /* WARNING: this file should *not* be used by applications. It is
45    part of the implementation of the library and is subject
46    to change.
47  */
51 /***********************************************************************
52 // search for a match
53 ************************************************************************/
55 #if (DD_BITS == 0)
57         /* search ip in the dictionary */
58         DINDEX1(dindex,ip);
59         GINDEX(m_pos,m_off,dict,dindex,in);
60         if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M3_MAX_OFFSET))
61             goto literal;
62 #if 1
63         if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
64             goto try_match;
65         DINDEX2(dindex,ip);
66 #endif
67         GINDEX(m_pos,m_off,dict,dindex,in);
68         if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M3_MAX_OFFSET))
69             goto literal;
70         if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
71             goto try_match;
72         goto literal;
75 #else /* (DD_BITS == 0) */
79         /* search ip in the deepened dictionary */
80         {
81             lzo_dict_p d = &dict [ DINDEX(dv,ip) ];
82             const lzo_bytep ip_sav;
83             unsigned j = DD_SIZE;
84             lzo_uint x_len;
85             LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, x_off, 0);
87             DVAL_ASSERT(dv,ip);
89             ip_sav = ip;
90             m_len = 0;
91             do {
92 #if !defined(NDEBUG)
93                 const lzo_bytep z_pos = NULL;
94 #endif
95 #if (LZO_DICT_USE_PTR)
96                 m_pos = *d;
97                 assert((z_pos = m_pos) == *d);
98 #if (LZO_DETERMINISTIC)
99                 assert(m_pos == NULL || m_pos >= in);
100                 assert(m_pos == NULL || m_pos < ip);
101 #endif
102 #else
103                 x_off = *d;
104 #endif
105                 assert(ip == ip_sav);
107                 if (LZO_CHECK_MPOS(m_pos,x_off,in,ip,MAX_OFFSET))
108 #if (CLEVEL == 9)
109                     *d = DENTRY(ip,in);
110 #else
111                     ((void)(0));
112 #endif
113                 else if (m_pos[m_len] != ip[m_len])
114                     ((void)(0));
115                 else if (*m_pos++ == *ip++ && *m_pos++ == *ip++ && *m_pos++ == *ip++)
116                 {
117 #if !(LZO_DICT_USE_PTR)
118                     assert((z_pos = ip - 3 - x_off) == (m_pos - 3));
119 #endif
120                     /* a match */
121                     if (MATCH_M2)
122                     {
123                         x_len = pd((ip - 1), ip_sav);
124                         if (x_len > m_len)
125                         {
126                             m_len = x_len;
127                             m_off = x_off;
128                             assert((m_pos_sav = z_pos) != NULL);
129                         }
130 #if (CLEVEL == 9)
131                         /* try to find a closer match */
132                         else if (x_len == m_len && x_off < m_off)
133                         {
134                             m_off = x_off;
135                             assert((m_pos_sav = z_pos) != NULL);
136                         }
137 #endif
138                     }
139                     else
140                     {
141                         assert((ip - ip_sav) == M2_MAX_LEN + 1);
142 #if (CLEVEL == 9)
143 #if defined(MATCH_IP_END)
144                         {
145                             const lzo_bytep end;
146                             end = MATCH_IP_END;
147                             while (ip < end  &&  *m_pos == *ip)
148                                 m_pos++, ip++;
149                             assert(ip <= in_end);
150                             x_len = pd(ip, ip_sav);
151                         }
152                         if (x_len > m_len)
153                         {
154                             m_len = x_len;
155                             m_off = x_off;
156                             assert((m_pos_sav = z_pos) != NULL);
157                             if (ip >= MATCH_IP_END)
158                             {
159                                 ip = ip_sav;
160 #if 0
161                                 /* not needed - we are at the end */
162                                 d -= DD_SIZE - j;
163                                 assert(d == &dict [ DINDEX(dv,ip) ]);
164                                 UPDATE_P(d,drun,ip,in);
165 #endif
166                                 goto match;
167                             }
168                         }
169                         else if (x_len == m_len && x_off < m_off)
170                         {
171                             m_off = x_off;
172                             assert((m_pos_sav = z_pos) != NULL);
173                         }
174 #else
175                         /* try to find a closer match */
176                         if (m_len < M2_MAX_LEN + 1 || x_off < m_off)
177                         {
178                             m_len = M2_MAX_LEN + 1;
179                             m_off = x_off;
180                             assert((m_pos_sav = z_pos) != NULL);
181                         }
182 #endif
183 #else
184                         /* don't search for a longer/closer match */
185                         m_len = M2_MAX_LEN + 1;
186                         m_off = x_off;
187                         assert((m_pos_sav = z_pos) != NULL);
188                         ip = ip_sav;
189                         d -= DD_SIZE - j;
190                         assert(d == &dict [ DINDEX(dv,ip) ]);
191                         UPDATE_P(d,drun,ip,in);
192                         goto match;
193 #endif
194                     }
195                     ip = ip_sav;
196                 }
197                 else
198                     ip = ip_sav;
199                 d++;
200             } while (--j > 0);
201             assert(ip == ip_sav);
203             d -= DD_SIZE;
204             assert(d == &dict [ DINDEX(dv,ip) ]);
205             UPDATE_P(d,drun,ip,in);
206         }
208 #endif /* (DD_BITS == 0) */
212 vi:ts=4:et