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.6 by pcg, Tue Apr 20 18:11:58 2004 UTC

1/* 1/*
2 * Copyright (c) 2000 Marc Alexander Lehmann <pcg@goof.com> 2 * Copyright (c) 2000-2003 Marc Alexander Lehmann <pcg@goof.com>
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))
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 */
39#define FRST(p) (((p[0]) << 8) + p[1]) 49#define FRST(p) (((p[0]) << 8) + p[1])
40#define NEXT(v,p) (((v) << 8) + p[2]) 50#define NEXT(v,p) (((v) << 8) + p[2])
41#define IDX(h) ((((h ^ (h << 4)) >> (3*8 - HLOG)) + h*3) & (HSIZE - 1)) 51#define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) + h*3) & (HSIZE - 1))
42/* 52/*
43 * IDX works because it is very similar to a multiplicative hash, e.g. 53 * IDX works because it is very similar to a multiplicative hash, e.g.
44 * (h * 57321 >> (3*8 - HLOG)) 54 * (h * 57321 >> (3*8 - HLOG))
45 * the next one is also quite good, albeit slow ;) 55 * the next one is also quite good, albeit slow ;)
46 * (int)(cos(h & 0xffffff) * 1e6) 56 * (int)(cos(h & 0xffffff) * 1e6)
47 */ 57 */
48 58
49#if 0 59#if 0
50/* original lzv-like hash function */ 60/* original lzv-like hash function */
51# define FRST(p) (p[0] << 5) ^ p[1] 61# define FRST(p) (p[0] << 5) ^ p[1]
52# define NEXT(v,p) (v << 5) ^ p[2] 62# define NEXT(v,p) ((v) << 5) ^ p[2]
53# define IDX(h) (h) & (HSIZE - 1) 63# define IDX(h) ((h) & (HSIZE - 1))
54#endif 64#endif
55 65
56#define MAX_LIT (1 << 5) 66#define MAX_LIT (1 << 5)
57#define MAX_OFF (1 << 13) 67#define MAX_OFF (1 << 13)
58#define MAX_REF ((1 << 8) + (1 << 3)) 68#define MAX_REF ((1 << 8) + (1 << 3))
66 * 76 *
67 */ 77 */
68 78
69unsigned int 79unsigned int
70lzf_compress (const void *const in_data, unsigned int in_len, 80lzf_compress (const void *const in_data, unsigned int in_len,
71 void *out_data, unsigned int out_len) 81 void *out_data, unsigned int out_len
82#if LZF_STATE_ARG
83 , LZF_STATE *htab
84#endif
85 )
72{ 86{
73 const u8 *htab[HSIZE]; 87#if !LZF_STATE_ARG
88 LZF_STATE htab;
89#endif
74 const u8 **hslot; 90 const u8 **hslot;
75 const u8 *ip = in_data; 91 const u8 *ip = (const u8 *)in_data;
76 u8 *op = out_data; 92 u8 *op = (u8 *)out_data;
77 const u8 *in_end = ip + in_len; 93 const u8 *in_end = ip + in_len;
78 u8 *out_end = op + out_len; 94 u8 *out_end = op + out_len;
79 const u8 *ref; 95 const u8 *ref;
80 96
81 unsigned int hval = FRST (ip); 97 unsigned int hval = FRST (ip);
89 for (hslot = htab; hslot < htab + HSIZE; hslot++) 105 for (hslot = htab; hslot < htab + HSIZE; hslot++)
90 *hslot++ = ip; 106 *hslot++ = ip;
91# endif 107# endif
92#endif 108#endif
93 109
94 do 110 for (;;)
95 { 111 {
112 if (ip < in_end - 2)
113 {
96 hval = NEXT (hval, ip); 114 hval = NEXT (hval, ip);
97 hslot = htab + IDX (hval); 115 hslot = htab + IDX (hval);
98 ref = *hslot; *hslot = ip; 116 ref = *hslot; *hslot = ip;
99 117
100 if (1 118 if (1
101#if INIT_HTAB && !USE_MEMCPY 119#if INIT_HTAB && !USE_MEMCPY
102 && ref < ip /* the next test will actually take care of this, but it is faster */ 120 && ref < ip /* the next test will actually take care of this, but this is faster */
103#endif 121#endif
104 && (off = ip - ref - 1) < MAX_OFF 122 && (off = ip - ref - 1) < MAX_OFF
105 && ip + 4 < in_end 123 && ip + 4 < in_end
106 && ref > (u8 *)in_data 124 && ref > (u8 *)in_data
107#if STRICT_ALIGN 125#if STRICT_ALIGN
108 && ref[0] == ip[0] 126 && ref[0] == ip[0]
109 && ref[1] == ip[1] 127 && ref[1] == ip[1]
110 && ref[2] == ip[2] 128 && ref[2] == ip[2]
111#else 129#else
112 && *(u16 *)ref == *(u16 *)ip 130 && *(u16 *)ref == *(u16 *)ip
113 && ref[2] == ip[2] 131 && ref[2] == ip[2]
114#endif 132#endif
115 ) 133 )
116 { 134 {
117 /* match found at *ref++ */ 135 /* match found at *ref++ */
118 unsigned int len = 2; 136 unsigned int len = 2;
119 unsigned int maxlen = in_end - ip - len; 137 unsigned int maxlen = in_end - ip - len;
120 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 138 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
121 139
122 do 140 do
123 len++; 141 len++;
124 while (len < maxlen && ref[len] == ip[len]); 142 while (len < maxlen && ref[len] == ip[len]);
125 143
126 if (op + lit + 1 + 3 >= out_end) 144 if (op + lit + 1 + 3 >= out_end)
127 return 0; 145 return 0;
128 146
129 if (lit) 147 if (lit)
130 { 148 {
131 *op++ = lit - 1; 149 *op++ = lit - 1;
132 lit = -lit; 150 lit = -lit;
133 do 151 do
134 *op++ = ip[lit]; 152 *op++ = ip[lit];
135 while (++lit); 153 while (++lit);
136 } 154 }
137 155
138 len -= 2; 156 len -= 2;
139 ip++; 157 ip++;
140 158
141 if (len < 7) 159 if (len < 7)
142 { 160 {
143 *op++ = (off >> 8) + (len << 5); 161 *op++ = (off >> 8) + (len << 5);
144 } 162 }
145 else 163 else
146 { 164 {
147 *op++ = (off >> 8) + ( 7 << 5); 165 *op++ = (off >> 8) + ( 7 << 5);
148 *op++ = len - 7; 166 *op++ = len - 7;
149 } 167 }
150 168
151 *op++ = off; 169 *op++ = off;
152 170
153#if ULTRA_FAST 171#if ULTRA_FAST
154 ip += len; 172 ip += len;
155 hval = FRST (ip); 173 hval = FRST (ip);
156 hval = NEXT (hval, ip); 174 hval = NEXT (hval, ip);
157 htab[IDX (hval)] = ip; 175 htab[IDX (hval)] = ip;
158 ip++; 176 ip++;
159#else 177#else
160 do 178 do
161 { 179 {
162 hval = NEXT (hval, ip); 180 hval = NEXT (hval, ip);
163 htab[IDX (hval)] = ip; 181 htab[IDX (hval)] = ip;
182 ip++;
183 }
184 while (len--);
185#endif
186 continue;
187 }
188 }
189 else if (ip == in_end)
190 break;
191
192 /* one more literal byte we must copy */
193 lit++;
164 ip++; 194 ip++;
165 }
166 while (len--);
167#endif
168 }
169 else
170 {
171 /* one more literal byte we must copy */
172 lit++;
173 ip++;
174 195
175 if (lit == MAX_LIT) 196 if (lit == MAX_LIT)
176 { 197 {
177 if (op + 1 + MAX_LIT >= out_end) 198 if (op + 1 + MAX_LIT >= out_end)
178 return 0; 199 return 0;
179 200
180 *op++ = MAX_LIT - 1; 201 *op++ = MAX_LIT - 1;
181#if USE_MEMCPY 202#if USE_MEMCPY
182 memcpy (op, ip - MAX_LIT, MAX_LIT); 203 memcpy (op, ip - MAX_LIT, MAX_LIT);
183 op += MAX_LIT; 204 op += MAX_LIT;
184 lit = 0; 205 lit = 0;
185#else 206#else
186 lit = -lit; 207 lit = -lit;
187 do 208 do
188 *op++ = ip[lit]; 209 *op++ = ip[lit];
189 while (++lit); 210 while (++lit);
190#endif 211#endif
191 } 212 }
192 }
193 } 213 }
194 while (ip < in_end);
195 214
196 if (lit) 215 if (lit)
197 { 216 {
198 if (op + lit + 1 >= out_end) 217 if (op + lit + 1 >= out_end)
199 return 0; 218 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines