ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/liblzf/lzf_c.c
Revision: 1.15
Committed: Fri Nov 2 12:39:20 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.14: +1 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de>
3 *
4 * Redistribution and use in source and binary forms, with or without modifica-
5 * tion, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
16 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
18 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
22 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License version 2 (the "GPL"), in which case the
27 * provisions of the GPL are applicable instead of the above. If you wish to
28 * allow the use of your version of this file only under the terms of the
29 * GPL and not to allow others to use your version of this file under the
30 * BSD license, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the GPL. If
32 * you do not delete the provisions above, a recipient may use your version
33 * of this file under either the BSD or the GPL.
34 */
35
36 #include "lzfP.h"
37
38 #define HSIZE (1 << (HLOG))
39
40 /*
41 * don't play with this unless you benchmark!
42 * decompression is not dependent on the hash function
43 * the hashing function might seem strange, just believe me
44 * it works ;)
45 */
46 #ifndef FRST
47 # define FRST(p) (((p[0]) << 8) | p[1])
48 # define NEXT(v,p) (((v) << 8) | p[2])
49 # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
50 /*# define IDX(h) ((ip[0] * 121 ^ ip[1] * 33 ^ ip[2] * 1) & (HSIZE-1))*/
51 #endif
52 /*
53 * IDX works because it is very similar to a multiplicative hash, e.g.
54 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
55 * the latter is also quite fast on newer CPUs, and compresses similarly.
56 *
57 * the next one is also quite good, albeit slow ;)
58 * (int)(cos(h & 0xffffff) * 1e6)
59 */
60
61 #if 0
62 /* original lzv-like hash function, much worse and thus slower */
63 # define FRST(p) (p[0] << 5) ^ p[1]
64 # define NEXT(v,p) ((v) << 5) ^ p[2]
65 # define IDX(h) ((h) & (HSIZE - 1))
66 #endif
67
68 #define MAX_LIT (1 << 5)
69 #define MAX_OFF (1 << 13)
70 #define MAX_REF ((1 << 8) + (1 << 3))
71
72 #if (__i386 || __amd64) && __GNUC__ >= 3
73 # define lzf_movsb(dst, src, len) \
74 asm ("rep movsb" \
75 : "=D" (dst), "=S" (src), "=c" (len) \
76 : "0" (dst), "1" (src), "2" (len));
77 #endif
78
79 /*
80 * compressed format
81 *
82 * 000LLLLL <L+1> ; literal
83 * LLLooooo oooooooo ; backref L
84 * 111ooooo LLLLLLLL oooooooo ; backref L+7
85 *
86 */
87
88 unsigned int
89 lzf_compress (const void *const in_data, unsigned int in_len,
90 void *out_data, unsigned int out_len
91 #if LZF_STATE_ARG
92 , LZF_STATE htab
93 #endif
94 )
95 {
96 #if !LZF_STATE_ARG
97 LZF_STATE htab;
98 #endif
99 const u8 **hslot;
100 const u8 *ip = (const u8 *)in_data;
101 u8 *op = (u8 *)out_data;
102 const u8 *in_end = ip + in_len;
103 u8 *out_end = op + out_len;
104 const u8 *ref;
105
106 unsigned int hval = FRST (ip);
107 unsigned long off;
108 int lit = 0;
109
110 #if INIT_HTAB
111 memset (htab, 0, sizeof (htab));
112 # if 0
113 for (hslot = htab; hslot < htab + HSIZE; hslot++)
114 *hslot++ = ip;
115 # endif
116 #endif
117
118 for (;;)
119 {
120 if (ip < in_end - 2)
121 {
122 hval = NEXT (hval, ip);
123 hslot = htab + IDX (hval);
124 ref = *hslot; *hslot = ip;
125
126 if (1
127 #if INIT_HTAB && !USE_MEMCPY
128 && ref < ip /* the next test will actually take care of this, but this is faster */
129 #endif
130 && (off = ip - ref - 1) < MAX_OFF
131 && ip + 4 < in_end
132 && ref > (u8 *)in_data
133 #if STRICT_ALIGN
134 && ref[0] == ip[0]
135 && ref[1] == ip[1]
136 && ref[2] == ip[2]
137 #else
138 && *(u16 *)ref == *(u16 *)ip
139 && ref[2] == ip[2]
140 #endif
141 )
142 {
143 /* match found at *ref++ */
144 unsigned int len = 2;
145 unsigned int maxlen = in_end - ip - len;
146 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
147
148 if (op + lit + 1 + 3 >= out_end)
149 return 0;
150
151 do
152 len++;
153 while (len < maxlen && ref[len] == ip[len]);
154
155 if (lit)
156 {
157 *op++ = lit - 1;
158 lit = -lit;
159 do
160 *op++ = ip[lit];
161 while (++lit);
162 }
163
164 len -= 2;
165 ip++;
166
167 if (len < 7)
168 {
169 *op++ = (off >> 8) + (len << 5);
170 }
171 else
172 {
173 *op++ = (off >> 8) + ( 7 << 5);
174 *op++ = len - 7;
175 }
176
177 *op++ = off;
178
179 #if ULTRA_FAST || VERY_FAST
180 ip += len;
181 #if VERY_FAST && !ULTRA_FAST
182 --ip;
183 #endif
184 hval = FRST (ip);
185
186 hval = NEXT (hval, ip);
187 htab[IDX (hval)] = ip;
188 ip++;
189
190 #if VERY_FAST && !ULTRA_FAST
191 hval = NEXT (hval, ip);
192 htab[IDX (hval)] = ip;
193 ip++;
194 #endif
195 #else
196 do
197 {
198 hval = NEXT (hval, ip);
199 htab[IDX (hval)] = ip;
200 ip++;
201 }
202 while (len--);
203 #endif
204 continue;
205 }
206 }
207 else if (ip == in_end)
208 break;
209
210 /* one more literal byte we must copy */
211 lit++;
212 ip++;
213
214 if (lit == MAX_LIT)
215 {
216 if (op + 1 + MAX_LIT >= out_end)
217 return 0;
218
219 *op++ = MAX_LIT - 1;
220
221 #ifdef lzf_movsb
222 ip -= lit;
223 lzf_movsb (op, ip, lit);
224 #else
225 lit = -lit;
226 do
227 *op++ = ip[lit];
228 while (++lit);
229 #endif
230 }
231 }
232
233 if (lit)
234 {
235 if (op + lit + 1 >= out_end)
236 return 0;
237
238 *op++ = lit - 1;
239 #ifdef lzf_movsb
240 ip -= lit;
241 lzf_movsb (op, ip, lit);
242 #else
243 lit = -lit;
244 do
245 *op++ = ip[lit];
246 while (++lit);
247 #endif
248 }
249
250 return op - (u8 *) out_data;
251 }
252