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

# User Rev Content
1 root 1.1 /*
2 root 1.15 * Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de>
3 root 1.1 *
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 pcg 1.6 *
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 root 1.1 */
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 root 1.10 #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 root 1.13 /*# define IDX(h) ((ip[0] * 121 ^ ip[1] * 33 ^ ip[2] * 1) & (HSIZE-1))*/
51 root 1.10 #endif
52 root 1.1 /*
53     * IDX works because it is very similar to a multiplicative hash, e.g.
54 root 1.7 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
55 root 1.12 * the latter is also quite fast on newer CPUs, and compresses similarly.
56 root 1.7 *
57 root 1.1 * the next one is also quite good, albeit slow ;)
58     * (int)(cos(h & 0xffffff) * 1e6)
59     */
60    
61     #if 0
62 root 1.7 /* original lzv-like hash function, much worse and thus slower */
63 root 1.1 # define FRST(p) (p[0] << 5) ^ p[1]
64 root 1.2 # define NEXT(v,p) ((v) << 5) ^ p[2]
65     # define IDX(h) ((h) & (HSIZE - 1))
66 root 1.1 #endif
67    
68     #define MAX_LIT (1 << 5)
69     #define MAX_OFF (1 << 13)
70 root 1.12 #define MAX_REF ((1 << 8) + (1 << 3))
71 root 1.1
72 root 1.14 #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 root 1.1 /*
80     * compressed format
81     *
82     * 000LLLLL <L+1> ; literal
83 root 1.10 * LLLooooo oooooooo ; backref L
84     * 111ooooo LLLLLLLL oooooooo ; backref L+7
85 root 1.1 *
86     */
87    
88     unsigned int
89     lzf_compress (const void *const in_data, unsigned int in_len,
90 pcg 1.4 void *out_data, unsigned int out_len
91 pcg 1.5 #if LZF_STATE_ARG
92 root 1.11 , LZF_STATE htab
93 pcg 1.4 #endif
94     )
95 root 1.1 {
96 pcg 1.5 #if !LZF_STATE_ARG
97 pcg 1.4 LZF_STATE htab;
98     #endif
99 root 1.1 const u8 **hslot;
100 pcg 1.4 const u8 *ip = (const u8 *)in_data;
101     u8 *op = (u8 *)out_data;
102 root 1.1 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 root 1.14 memset (htab, 0, sizeof (htab));
112     # if 0
113     for (hslot = htab; hslot < htab + HSIZE; hslot++)
114     *hslot++ = ip;
115 root 1.1 # endif
116     #endif
117    
118 pcg 1.5 for (;;)
119 root 1.1 {
120 pcg 1.5 if (ip < in_end - 2)
121     {
122     hval = NEXT (hval, ip);
123     hslot = htab + IDX (hval);
124     ref = *hslot; *hslot = ip;
125 root 1.1
126 pcg 1.5 if (1
127 root 1.1 #if INIT_HTAB && !USE_MEMCPY
128 pcg 1.5 && ref < ip /* the next test will actually take care of this, but this is faster */
129 root 1.1 #endif
130 pcg 1.5 && (off = ip - ref - 1) < MAX_OFF
131     && ip + 4 < in_end
132     && ref > (u8 *)in_data
133 root 1.1 #if STRICT_ALIGN
134 pcg 1.5 && ref[0] == ip[0]
135     && ref[1] == ip[1]
136     && ref[2] == ip[2]
137 root 1.1 #else
138 pcg 1.5 && *(u16 *)ref == *(u16 *)ip
139     && ref[2] == ip[2]
140 root 1.1 #endif
141 pcg 1.5 )
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 root 1.9 if (op + lit + 1 + 3 >= out_end)
149     return 0;
150    
151 pcg 1.5 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 root 1.1
177 pcg 1.5 *op++ = off;
178 root 1.1
179 root 1.9 #if ULTRA_FAST || VERY_FAST
180 pcg 1.5 ip += len;
181 root 1.9 #if VERY_FAST && !ULTRA_FAST
182     --ip;
183     #endif
184 pcg 1.5 hval = FRST (ip);
185 root 1.9
186     hval = NEXT (hval, ip);
187     htab[IDX (hval)] = ip;
188     ip++;
189    
190     #if VERY_FAST && !ULTRA_FAST
191 pcg 1.5 hval = NEXT (hval, ip);
192     htab[IDX (hval)] = ip;
193     ip++;
194 root 1.9 #endif
195 root 1.1 #else
196 pcg 1.5 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 root 1.1
219 pcg 1.5 *op++ = MAX_LIT - 1;
220 root 1.14
221     #ifdef lzf_movsb
222     ip -= lit;
223     lzf_movsb (op, ip, lit);
224 root 1.1 #else
225 pcg 1.5 lit = -lit;
226     do
227     *op++ = ip[lit];
228     while (++lit);
229 root 1.1 #endif
230 pcg 1.5 }
231 root 1.1 }
232    
233     if (lit)
234     {
235     if (op + lit + 1 >= out_end)
236     return 0;
237    
238     *op++ = lit - 1;
239 root 1.14 #ifdef lzf_movsb
240     ip -= lit;
241     lzf_movsb (op, ip, lit);
242     #else
243 root 1.1 lit = -lit;
244     do
245     *op++ = ip[lit];
246     while (++lit);
247 root 1.14 #endif
248 root 1.1 }
249    
250     return op - (u8 *) out_data;
251     }
252 root 1.14