1 /* lzo_dict.h -- dictionary definitions for the the LZO library
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer
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 /* WARNING: this file should *not* be used by applications. It is
30 part of the implementation of the library and is subject
36 #define __LZO_DICT_H 1
44 /***********************************************************************
46 ************************************************************************/
48 /* dictionary needed for compression */
49 #if !defined(D_BITS) && defined(DBITS)
53 # error "D_BITS is not defined"
56 # define D_SIZE LZO_SIZE(D_BITS)
57 # define D_MASK LZO_MASK(D_BITS)
59 # define D_SIZE LZO_USIZE(D_BITS)
60 # define D_MASK LZO_UMASK(D_BITS)
62 #define D_HIGH ((D_MASK >> 1) + 1)
65 /* dictionary depth */
69 #define DD_SIZE LZO_SIZE(DD_BITS)
70 #define DD_MASK LZO_MASK(DD_BITS)
72 /* dictionary length */
74 # define DL_BITS (D_BITS - DD_BITS)
77 # define DL_SIZE LZO_SIZE(DL_BITS)
78 # define DL_MASK LZO_MASK(DL_BITS)
80 # define DL_SIZE LZO_USIZE(DL_BITS)
81 # define DL_MASK LZO_UMASK(DL_BITS)
85 #if (D_BITS != DL_BITS + DD_BITS)
86 # error "D_BITS does not match"
88 #if (D_BITS < 6 || D_BITS > 18)
89 # error "invalid D_BITS"
91 #if (DL_BITS < 6 || DL_BITS > 20)
92 # error "invalid DL_BITS"
94 #if (DD_BITS < 0 || DD_BITS > 6)
95 # error "invalid DD_BITS"
99 #if !defined(DL_MIN_LEN)
100 # define DL_MIN_LEN 3
102 #if !defined(DL_SHIFT)
103 # define DL_SHIFT ((DL_BITS + (DL_MIN_LEN - 1)) / DL_MIN_LEN)
108 /***********************************************************************
110 ************************************************************************/
112 #define LZO_HASH_GZIP 1
113 #define LZO_HASH_GZIP_INCREMENTAL 2
114 #define LZO_HASH_LZO_INCREMENTAL_A 3
115 #define LZO_HASH_LZO_INCREMENTAL_B 4
117 #if !defined(LZO_HASH)
118 # error "choose a hashing strategy"
124 #if (DL_MIN_LEN == 3)
125 # define _DV2_A(p,shift1,shift2) \
126 (((( (lzo_xint)((p)[0]) << shift1) ^ (p)[1]) << shift2) ^ (p)[2])
127 # define _DV2_B(p,shift1,shift2) \
128 (((( (lzo_xint)((p)[2]) << shift1) ^ (p)[1]) << shift2) ^ (p)[0])
129 # define _DV3_B(p,shift1,shift2,shift3) \
130 ((_DV2_B((p)+1,shift1,shift2) << (shift3)) ^ (p)[0])
131 #elif (DL_MIN_LEN == 2)
132 # define _DV2_A(p,shift1,shift2) \
133 (( (lzo_xint)(p[0]) << shift1) ^ p[1])
134 # define _DV2_B(p,shift1,shift2) \
135 (( (lzo_xint)(p[1]) << shift1) ^ p[2])
137 # error "invalid DL_MIN_LEN"
139 #define _DV_A(p,shift) _DV2_A(p,shift,shift)
140 #define _DV_B(p,shift) _DV2_B(p,shift,shift)
141 #define DA2(p,s1,s2) \
142 (((((lzo_xint)((p)[2]) << (s2)) + (p)[1]) << (s1)) + (p)[0])
143 #define DS2(p,s1,s2) \
144 (((((lzo_xint)((p)[2]) << (s2)) - (p)[1]) << (s1)) - (p)[0])
145 #define DX2(p,s1,s2) \
146 (((((lzo_xint)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
147 #define DA3(p,s1,s2,s3) ((DA2((p)+1,s2,s3) << (s1)) + (p)[0])
148 #define DS3(p,s1,s2,s3) ((DS2((p)+1,s2,s3) << (s1)) - (p)[0])
149 #define DX3(p,s1,s2,s3) ((DX2((p)+1,s2,s3) << (s1)) ^ (p)[0])
150 #define DMS(v,s) ((lzo_uint) (((v) & (D_MASK >> (s))) << (s)))
151 #define DM(v) DMS(v,0)
154 #if (LZO_HASH == LZO_HASH_GZIP)
155 /* hash function like in gzip/zlib (deflate) */
156 # define _DINDEX(dv,p) (_DV_A((p),DL_SHIFT))
158 #elif (LZO_HASH == LZO_HASH_GZIP_INCREMENTAL)
159 /* incremental hash like in gzip/zlib (deflate) */
160 # define __LZO_HASH_INCREMENTAL 1
161 # define DVAL_FIRST(dv,p) dv = _DV_A((p),DL_SHIFT)
162 # define DVAL_NEXT(dv,p) dv = (((dv) << DL_SHIFT) ^ p[2])
163 # define _DINDEX(dv,p) (dv)
164 # define DVAL_LOOKAHEAD DL_MIN_LEN
166 #elif (LZO_HASH == LZO_HASH_LZO_INCREMENTAL_A)
167 /* incremental LZO hash version A */
168 # define __LZO_HASH_INCREMENTAL 1
169 # define DVAL_FIRST(dv,p) dv = _DV_A((p),5)
170 # define DVAL_NEXT(dv,p) \
171 dv ^= (lzo_xint)(p[-1]) << (2*5); dv = (((dv) << 5) ^ p[2])
172 # define _DINDEX(dv,p) ((DMUL(0x9f5f,dv)) >> 5)
173 # define DVAL_LOOKAHEAD DL_MIN_LEN
175 #elif (LZO_HASH == LZO_HASH_LZO_INCREMENTAL_B)
176 /* incremental LZO hash version B */
177 # define __LZO_HASH_INCREMENTAL 1
178 # define DVAL_FIRST(dv,p) dv = _DV_B((p),5)
179 # define DVAL_NEXT(dv,p) \
180 dv ^= p[-1]; dv = (((dv) >> 5) ^ ((lzo_xint)(p[2]) << (2*5)))
181 # define _DINDEX(dv,p) ((DMUL(0x9f5f,dv)) >> 5)
182 # define DVAL_LOOKAHEAD DL_MIN_LEN
185 # error "choose a hashing strategy"
190 #define DINDEX(dv,p) ((lzo_uint)((_DINDEX(dv,p)) & DL_MASK) << DD_BITS)
192 #if !defined(DINDEX1) && defined(D_INDEX1)
193 #define DINDEX1 D_INDEX1
195 #if !defined(DINDEX2) && defined(D_INDEX2)
196 #define DINDEX2 D_INDEX2
201 #if !defined(__LZO_HASH_INCREMENTAL)
202 # define DVAL_FIRST(dv,p) ((void) 0)
203 # define DVAL_NEXT(dv,p) ((void) 0)
204 # define DVAL_LOOKAHEAD 0
208 #if !defined(DVAL_ASSERT)
209 #if defined(__LZO_HASH_INCREMENTAL) && !defined(NDEBUG)
210 #if (LZO_CC_CLANG || (LZO_CC_GNUC >= 0x020700ul) || LZO_CC_LLVM)
211 static void __attribute__((__unused__
))
215 DVAL_ASSERT(lzo_xint dv
, const lzo_bytep p
)
219 assert(DINDEX(dv
,p
) == DINDEX(df
,p
));
222 # define DVAL_ASSERT(dv,p) ((void) 0)
228 /***********************************************************************
229 // dictionary updating
230 ************************************************************************/
232 #if (LZO_DICT_USE_PTR)
233 # define DENTRY(p,in) (p)
234 # define GINDEX(m_pos,m_off,dict,dindex,in) m_pos = dict[dindex]
236 # define DENTRY(p,in) ((lzo_dict_t) pd(p, in))
237 # define GINDEX(m_pos,m_off,dict,dindex,in) m_off = dict[dindex]
243 # define UPDATE_D(dict,drun,dv,p,in) dict[ DINDEX(dv,p) ] = DENTRY(p,in)
244 # define UPDATE_I(dict,drun,index,p,in) dict[index] = DENTRY(p,in)
245 # define UPDATE_P(ptr,drun,p,in) (ptr)[0] = DENTRY(p,in)
249 # define UPDATE_D(dict,drun,dv,p,in) \
250 dict[ DINDEX(dv,p) + drun++ ] = DENTRY(p,in); drun &= DD_MASK
251 # define UPDATE_I(dict,drun,index,p,in) \
252 dict[ (index) + drun++ ] = DENTRY(p,in); drun &= DD_MASK
253 # define UPDATE_P(ptr,drun,p,in) \
254 (ptr) [ drun++ ] = DENTRY(p,in); drun &= DD_MASK
259 /***********************************************************************
261 ************************************************************************/
263 #if (LZO_DICT_USE_PTR)
265 /* m_pos is either NULL or a valid pointer */
266 #define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
267 (m_pos == NULL || (m_off = pd(ip, m_pos)) > max_offset)
269 /* m_pos may point anywhere... */
270 #define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
271 (BOUNDS_CHECKING_OFF_IN_EXPR(( \
272 m_pos = ip - (lzo_uint) PTR_DIFF(ip,m_pos), \
273 PTR_LT(m_pos,in) || \
274 (m_off = (lzo_uint) PTR_DIFF(ip,m_pos)) == 0 || \
275 m_off > max_offset )))
279 #define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
281 ((m_off = pd(ip, in) - m_off) > max_offset) || \
282 (m_pos = (ip) - (m_off), 0) )
284 #define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
285 (pd(ip, in) <= m_off || \
286 ((m_off = pd(ip, in) - m_off) > max_offset) || \
287 (m_pos = (ip) - (m_off), 0) )
292 #if (LZO_DETERMINISTIC)
293 # define LZO_CHECK_MPOS LZO_CHECK_MPOS_DET
295 # define LZO_CHECK_MPOS LZO_CHECK_MPOS_NON_DET
304 #endif /* already included */