ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/liblzf/lzf_c.c
(Generate patch)

Comparing liblzf/lzf_c.c (file contents):
Revision 1.1 by root, Sun Jun 9 22:41:34 2002 UTC vs.
Revision 1.11 by root, Fri Feb 16 22:13:43 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines