1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2000-2007 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, |
8 | * this list of conditions and the following disclaimer. |
8 | * this list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
|
|
14 | * 3. The name of the author may not be used to endorse or promote products |
|
|
15 | * derived from this software without specific prior written permission. |
|
|
16 | * |
13 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
… | |
… | |
48 | */ |
45 | */ |
49 | #ifndef FRST |
46 | #ifndef FRST |
50 | # define FRST(p) (((p[0]) << 8) | p[1]) |
47 | # define FRST(p) (((p[0]) << 8) | p[1]) |
51 | # define NEXT(v,p) (((v) << 8) | p[2]) |
48 | # define NEXT(v,p) (((v) << 8) | p[2]) |
52 | # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) |
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))*/ |
53 | #endif |
51 | #endif |
54 | /* |
52 | /* |
55 | * 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. |
56 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
54 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
57 | * the latter is also quite fast on newer CPUs, and sligthly better |
55 | * the latter is also quite fast on newer CPUs, and compresses similarly. |
58 | * |
56 | * |
59 | * the next one is also quite good, albeit slow ;) |
57 | * the next one is also quite good, albeit slow ;) |
60 | * (int)(cos(h & 0xffffff) * 1e6) |
58 | * (int)(cos(h & 0xffffff) * 1e6) |
61 | */ |
59 | */ |
62 | |
60 | |
… | |
… | |
67 | # define IDX(h) ((h) & (HSIZE - 1)) |
65 | # define IDX(h) ((h) & (HSIZE - 1)) |
68 | #endif |
66 | #endif |
69 | |
67 | |
70 | #define MAX_LIT (1 << 5) |
68 | #define MAX_LIT (1 << 5) |
71 | #define MAX_OFF (1 << 13) |
69 | #define MAX_OFF (1 << 13) |
72 | #define MAX_REF ((1 << 8) + (1 << 3)) |
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 |
73 | |
78 | |
74 | /* |
79 | /* |
75 | * compressed format |
80 | * compressed format |
76 | * |
81 | * |
77 | * 000LLLLL <L+1> ; literal |
82 | * 000LLLLL <L+1> ; literal |
… | |
… | |
82 | |
87 | |
83 | unsigned int |
88 | unsigned int |
84 | lzf_compress (const void *const in_data, unsigned int in_len, |
89 | lzf_compress (const void *const in_data, unsigned int in_len, |
85 | void *out_data, unsigned int out_len |
90 | void *out_data, unsigned int out_len |
86 | #if LZF_STATE_ARG |
91 | #if LZF_STATE_ARG |
87 | , LZF_STATE *htab |
92 | , LZF_STATE htab |
88 | #endif |
93 | #endif |
89 | ) |
94 | ) |
90 | { |
95 | { |
91 | #if !LZF_STATE_ARG |
96 | #if !LZF_STATE_ARG |
92 | LZF_STATE htab; |
97 | LZF_STATE htab; |
… | |
… | |
101 | unsigned int hval = FRST (ip); |
106 | unsigned int hval = FRST (ip); |
102 | unsigned long off; |
107 | unsigned long off; |
103 | int lit = 0; |
108 | int lit = 0; |
104 | |
109 | |
105 | #if INIT_HTAB |
110 | #if INIT_HTAB |
106 | # if USE_MEMCPY |
|
|
107 | memset (htab, 0, sizeof (htab)); |
111 | memset (htab, 0, sizeof (htab)); |
108 | # else |
112 | # if 0 |
109 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
113 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
110 | *hslot++ = ip; |
114 | *hslot++ = ip; |
111 | # endif |
115 | # endif |
112 | #endif |
116 | #endif |
113 | |
117 | |
114 | for (;;) |
118 | for (;;) |
115 | { |
119 | { |
… | |
… | |
211 | { |
215 | { |
212 | if (op + 1 + MAX_LIT >= out_end) |
216 | if (op + 1 + MAX_LIT >= out_end) |
213 | return 0; |
217 | return 0; |
214 | |
218 | |
215 | *op++ = MAX_LIT - 1; |
219 | *op++ = MAX_LIT - 1; |
216 | #if USE_MEMCPY |
220 | |
217 | memcpy (op, ip - MAX_LIT, MAX_LIT); |
221 | #ifdef lzf_movsb |
218 | op += MAX_LIT; |
222 | ip -= lit; |
219 | lit = 0; |
223 | lzf_movsb (op, ip, lit); |
220 | #else |
224 | #else |
221 | lit = -lit; |
225 | lit = -lit; |
222 | do |
226 | do |
223 | *op++ = ip[lit]; |
227 | *op++ = ip[lit]; |
224 | while (++lit); |
228 | while (++lit); |
… | |
… | |
230 | { |
234 | { |
231 | if (op + lit + 1 >= out_end) |
235 | if (op + lit + 1 >= out_end) |
232 | return 0; |
236 | return 0; |
233 | |
237 | |
234 | *op++ = lit - 1; |
238 | *op++ = lit - 1; |
|
|
239 | #ifdef lzf_movsb |
|
|
240 | ip -= lit; |
|
|
241 | lzf_movsb (op, ip, lit); |
|
|
242 | #else |
235 | lit = -lit; |
243 | lit = -lit; |
236 | do |
244 | do |
237 | *op++ = ip[lit]; |
245 | *op++ = ip[lit]; |
238 | while (++lit); |
246 | while (++lit); |
|
|
247 | #endif |
239 | } |
248 | } |
240 | |
249 | |
241 | return op - (u8 *) out_data; |
250 | return op - (u8 *) out_data; |
242 | } |
251 | } |
|
|
252 | |