summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/lzf_c.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:52:54 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:52:54 +0100
commitdcacc00e3750300617ba6e16eb346713f91a783a (patch)
tree38e2d4fb5ed9d119711d4295c6eda4b014af73fd /examples/redis-unstable/src/lzf_c.c
parent58dac10aeb8f5a041c46bddbeaf4c7966a99b998 (diff)
downloadcrep-dcacc00e3750300617ba6e16eb346713f91a783a.tar.gz
Remove testing data
Diffstat (limited to 'examples/redis-unstable/src/lzf_c.c')
-rw-r--r--examples/redis-unstable/src/lzf_c.c309
1 files changed, 0 insertions, 309 deletions
diff --git a/examples/redis-unstable/src/lzf_c.c b/examples/redis-unstable/src/lzf_c.c
deleted file mode 100644
index 9f858a2..0000000
--- a/examples/redis-unstable/src/lzf_c.c
+++ /dev/null
@@ -1,309 +0,0 @@
1/*
2 * Copyright (c) 2000-2010 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 ("GPL") version 2 or any later version,
27 * in which case the provisions of the GPL are applicable instead of
28 * the above. If you wish to allow the use of your version of this file
29 * only under the terms of the GPL and not to allow others to use your
30 * version of this file under the BSD license, indicate your decision
31 * by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL. If you do not delete the
33 * provisions above, a recipient may use your version of this file under
34 * either the BSD or the GPL.
35 */
36
37#include "lzfP.h"
38
39#define HSIZE (1 << (HLOG))
40
41/*
42 * don't play with this unless you benchmark!
43 * the data format is not dependent on the hash function.
44 * the hash function might seem strange, just believe me,
45 * it works ;)
46 */
47#ifndef FRST
48# define FRST(p) (((p[0]) << 8) | p[1])
49# define NEXT(v,p) (((v) << 8) | p[2])
50# if ULTRA_FAST
51# define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1))
52# elif VERY_FAST
53# define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
54# else
55# define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
56# endif
57#endif
58/*
59 * IDX works because it is very similar to a multiplicative hash, e.g.
60 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
61 * the latter is also quite fast on newer CPUs, and compresses similarly.
62 *
63 * the next one is also quite good, albeit slow ;)
64 * (int)(cos(h & 0xffffff) * 1e6)
65 */
66
67#if 0
68/* original lzv-like hash function, much worse and thus slower */
69# define FRST(p) (p[0] << 5) ^ p[1]
70# define NEXT(v,p) ((v) << 5) ^ p[2]
71# define IDX(h) ((h) & (HSIZE - 1))
72#endif
73
74#define MAX_LIT (1 << 5)
75#define MAX_OFF (1 << 13)
76#define MAX_REF ((1 << 8) + (1 << 3))
77
78#if __GNUC__ >= 3
79# define expect(expr,value) __builtin_expect ((expr),(value))
80# define inline inline
81#else
82# define expect(expr,value) (expr)
83# define inline static
84#endif
85
86#define expect_false(expr) expect ((expr) != 0, 0)
87#define expect_true(expr) expect ((expr) != 0, 1)
88
89#if defined(__has_attribute)
90# if __has_attribute(no_sanitize)
91# define NO_SANITIZE(sanitizer) __attribute__((no_sanitize(sanitizer)))
92# endif
93#endif
94
95#if !defined(NO_SANITIZE)
96# define NO_SANITIZE(sanitizer)
97#endif
98
99#if defined(__clang__)
100#define NO_SANITIZE_MSAN(sanitizer) NO_SANITIZE(sanitizer)
101#else
102#define NO_SANITIZE_MSAN(sanitizer)
103#endif
104
105/*
106 * compressed format
107 *
108 * 000LLLLL <L+1> ; literal, L+1=1..33 octets
109 * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset
110 * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset
111 *
112 */
113NO_SANITIZE("alignment")
114NO_SANITIZE_MSAN("memory")
115size_t
116lzf_compress (const void *const in_data, size_t in_len,
117 void *out_data, size_t out_len
118#if LZF_STATE_ARG
119 , LZF_STATE htab
120#endif
121 )
122{
123#if !LZF_STATE_ARG
124 LZF_STATE htab;
125#endif
126 const u8 *ip = (const u8 *)in_data;
127 u8 *op = (u8 *)out_data;
128 const u8 *in_end = ip + in_len;
129 u8 *out_end = op + out_len;
130 const u8 *ref;
131
132 /* off requires a type wide enough to hold a general pointer difference.
133 * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only
134 * works for differences within a single object). We also assume that no
135 * no bit pattern traps. Since the only platform that is both non-POSIX
136 * and fails to support both assumptions is windows 64 bit, we make a
137 * special workaround for it.
138 */
139#if defined (WIN32) && defined (_M_X64)
140 unsigned _int64 off; /* workaround for missing POSIX compliance */
141#else
142 size_t off;
143#endif
144 unsigned int hval;
145 int lit;
146
147 if (!in_len || !out_len)
148 return 0;
149
150#if INIT_HTAB
151 memset (htab, 0, sizeof (htab));
152#endif
153
154 lit = 0; op++; /* start run */
155
156 hval = FRST (ip);
157 while (ip < in_end - 2)
158 {
159 LZF_HSLOT *hslot;
160
161 hval = NEXT (hval, ip);
162 hslot = htab + IDX (hval);
163 ref = *hslot ? (*hslot + LZF_HSLOT_BIAS) : NULL; /* avoid applying zero offset to null pointer */
164 *hslot = ip - LZF_HSLOT_BIAS;
165
166 if (1
167#if INIT_HTAB
168 && ref < ip /* the next test will actually take care of this, but this is faster */
169#endif
170 && (off = ip - ref - 1) < MAX_OFF
171 && ref > (u8 *)in_data
172 && ref[2] == ip[2]
173#if STRICT_ALIGN
174 && ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])
175#else
176 && *(u16 *)ref == *(u16 *)ip
177#endif
178 )
179 {
180 /* match found at *ref++ */
181 unsigned int len = 2;
182 size_t maxlen = in_end - ip - len;
183 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
184
185 if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
186 if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
187 return 0;
188
189 op [- lit - 1] = lit - 1; /* stop run */
190 op -= !lit; /* undo run if length is zero */
191
192 for (;;)
193 {
194 if (expect_true (maxlen > 16))
195 {
196 len++; if (ref [len] != ip [len]) break;
197 len++; if (ref [len] != ip [len]) break;
198 len++; if (ref [len] != ip [len]) break;
199 len++; if (ref [len] != ip [len]) break;
200
201 len++; if (ref [len] != ip [len]) break;
202 len++; if (ref [len] != ip [len]) break;
203 len++; if (ref [len] != ip [len]) break;
204 len++; if (ref [len] != ip [len]) break;
205
206 len++; if (ref [len] != ip [len]) break;
207 len++; if (ref [len] != ip [len]) break;
208 len++; if (ref [len] != ip [len]) break;
209 len++; if (ref [len] != ip [len]) break;
210
211 len++; if (ref [len] != ip [len]) break;
212 len++; if (ref [len] != ip [len]) break;
213 len++; if (ref [len] != ip [len]) break;
214 len++; if (ref [len] != ip [len]) break;
215 }
216
217 do
218 len++;
219 while (len < maxlen && ref[len] == ip[len]);
220
221 break;
222 }
223
224 len -= 2; /* len is now #octets - 1 */
225 ip++;
226
227 if (len < 7)
228 {
229 *op++ = (off >> 8) + (len << 5);
230 }
231 else
232 {
233 *op++ = (off >> 8) + ( 7 << 5);
234 *op++ = len - 7;
235 }
236
237 *op++ = off;
238
239 lit = 0; op++; /* start run */
240
241 ip += len + 1;
242
243 if (expect_false (ip >= in_end - 2))
244 break;
245
246#if ULTRA_FAST || VERY_FAST
247 --ip;
248# if VERY_FAST && !ULTRA_FAST
249 --ip;
250# endif
251 hval = FRST (ip);
252
253 hval = NEXT (hval, ip);
254 htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
255 ip++;
256
257# if VERY_FAST && !ULTRA_FAST
258 hval = NEXT (hval, ip);
259 htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
260 ip++;
261# endif
262#else
263 ip -= len + 1;
264
265 do
266 {
267 hval = NEXT (hval, ip);
268 htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
269 ip++;
270 }
271 while (len--);
272#endif
273 }
274 else
275 {
276 /* one more literal byte we must copy */
277 if (expect_false (op >= out_end))
278 return 0;
279
280 lit++; *op++ = *ip++;
281
282 if (expect_false (lit == MAX_LIT))
283 {
284 op [- lit - 1] = lit - 1; /* stop run */
285 lit = 0; op++; /* start run */
286 }
287 }
288 }
289
290 if (op + 3 > out_end) /* at most 3 bytes can be missing here */
291 return 0;
292
293 while (ip < in_end)
294 {
295 lit++; *op++ = *ip++;
296
297 if (expect_false (lit == MAX_LIT))
298 {
299 op [- lit - 1] = lit - 1; /* stop run */
300 lit = 0; op++; /* start run */
301 }
302 }
303
304 op [- lit - 1] = lit - 1; /* end run */
305 op -= !lit; /* undo run if length is zero */
306
307 return op - (u8 *)out_data;
308}
309