ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/liblzf/lzf_c_best.c
Revision: 1.4
Committed: Sat Jun 27 19:50:25 2015 UTC (8 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.3: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     * Copyright (c) 2000-2012 Marc Alexander Lehmann <schmorp@schmorp.de>
3 root 1.2 *
4 root 1.1 * Redistribution and use in source and binary forms, with or without modifica-
5     * tion, are permitted provided that the following conditions are met:
6 root 1.2 *
7 root 1.1 * 1. Redistributions of source code must retain the above copyright notice,
8     * this list of conditions and the following disclaimer.
9 root 1.2 *
10 root 1.1 * 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 root 1.2 *
14 root 1.1 * 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 HASH(p) (p[0] << 6) ^ (p[1] << 3) ^ p[2]
40    
41     #define MAX_LIT (1 << 5)
42     #define MAX_OFF (1 << 13)
43     #define MAX_REF ((1 << 8) + (1 << 3))
44    
45     #if __GNUC__ >= 3
46     # define expect(expr,value) __builtin_expect ((expr),(value))
47     # define inline inline
48     #else
49     # define expect(expr,value) (expr)
50     # define inline static
51     #endif
52    
53     #define expect_false(expr) expect ((expr) != 0, 0)
54     #define expect_true(expr) expect ((expr) != 0, 1)
55    
56     /*
57     * compressed format
58     *
59     * 000LLLLL <L+1> ; literal, L+1=1..33 octets
60     * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset
61     * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset
62     *
63     */
64    
65     unsigned int
66     lzf_compress_best (const void *const in_data, unsigned int in_len,
67     void *out_data, unsigned int out_len)
68     {
69     const u8 *ip = (const u8 *)in_data;
70     u8 *op = (u8 *)out_data;
71     const u8 *in_end = ip + in_len;
72     u8 *out_end = op + out_len;
73    
74     const u8 *first [1 << (6+8)]; /* most recent occurance of a match */
75 root 1.3 u16 prev [MAX_OFF]; /* how many bytes to go backwards for the next match */
76 root 1.1
77     int lit;
78    
79     if (!in_len || !out_len)
80     return 0;
81    
82     lit = 0; op++; /* start run */
83    
84     lit++; *op++ = *ip++;
85    
86     while (ip < in_end - 2)
87     {
88     int best_l = 0;
89     const u8 *best_p;
90     int e = (in_end - ip < MAX_REF ? in_end - ip : MAX_REF) - 1;
91 root 1.4 unsigned int res = ((unsigned long)ip) & (MAX_OFF - 1);
92 root 1.1 u16 hash = HASH (ip);
93     u16 diff;
94     const u8 *b = ip < (u8 *)in_data + MAX_OFF ? in_data : ip - MAX_OFF;
95     const u8 *p = first [hash];
96     prev [res] = ip - p; /* update ptr to previous hash match */
97     first [hash] = ip; /* first hash match is here */
98    
99     if (p < ip)
100     while (p >= b)
101     {
102     if (p[2] == ip[2]) /* first two bytes almost always match */
103     if (p[best_l] == ip[best_l]) /* new match must be longer than the old one to qualify */
104     if (p[1] == ip[1] && p[0] == ip[0]) /* just to be sure */
105     {
106     int l = 3;
107    
108     while (p[l] == ip[l] && l < e)
109     ++l;
110    
111     if (l >= best_l)
112     {
113     best_p = p;
114     best_l = l;
115     }
116     }
117    
118 root 1.4 diff = prev [((unsigned long)p) & (MAX_OFF - 1)];
119 root 1.3 p = diff ? p - diff : 0;
120 root 1.1 }
121    
122     if (best_l)
123     {
124     int len = best_l;
125     int off = ip - best_p - 1;
126    
127     if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
128     if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
129     return 0;
130    
131     op [- lit - 1] = lit - 1; /* stop run */
132     op -= !lit; /* undo run if length is zero */
133    
134     len -= 2; /* len is now #octets - 1 */
135     ip++;
136    
137     if (len < 7)
138     {
139     *op++ = (off >> 8) + (len << 5);
140     }
141     else
142     {
143     *op++ = (off >> 8) + ( 7 << 5);
144     *op++ = len - 7;
145     }
146    
147     *op++ = off;
148    
149     lit = 0; op++; /* start run */
150    
151     ip += len + 1;
152    
153     if (expect_false (ip >= in_end - 2))
154     break;
155    
156     ip -= len + 1;
157    
158     //printf (" fill %p for %d\n", ip, len);//D
159     do
160     {
161     u16 hash = HASH (ip);
162 root 1.4 res = ((unsigned long)ip) & (MAX_OFF - 1);
163 root 1.1
164     p = first [hash];
165     prev [res] = ip - p; /* update ptr to previous hash match */
166     first [hash] = ip; /* first hash match is here */
167    
168     ip++;
169     }
170     while (len--);
171     }
172     else
173     {
174     /* one more literal byte we must copy */
175     if (expect_false (op >= out_end))
176     return 0;
177    
178     lit++; *op++ = *ip++;
179    
180     if (expect_false (lit == MAX_LIT))
181     {
182     op [- lit - 1] = lit - 1; /* stop run */
183     lit = 0; op++; /* start run */
184     }
185     }
186     }
187    
188     if (op + 3 > out_end) /* at most 3 bytes can be missing here */
189     return 0;
190    
191     while (ip < in_end)
192     {
193     lit++; *op++ = *ip++;
194    
195     if (expect_false (lit == MAX_LIT))
196     {
197     op [- lit - 1] = lit - 1; /* stop run */
198     lit = 0; op++; /* start run */
199     }
200     }
201    
202     op [- lit - 1] = lit - 1; /* end run */
203     op -= !lit; /* undo run if length is zero */
204    
205     return op - (u8 *)out_data;
206     }
207