lzf_c_best.c

filter: Statementstmt 77.0% 57 / 74 Branchbran 55.7% 29 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.2% 86 / 126 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« LZF.xs ↑ Summary lzf_d.c »
1 /*
2 * Copyright (c) 2000-2012 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 HASH(p) (p[0] << 6) ^ (p[1] << 3) ^ p[2]
40  
41 #if __GNUC__ >= 3
42 # define expect(expr,value) __builtin_expect ((expr),(value))
43 # define inline inline
44 #else
45 # define expect(expr,value) (expr)
46 # define inline static
47 #endif
48  
49 #define expect_false(expr) expect ((expr) != 0, 0)
50 #define expect_true(expr) expect ((expr) != 0, 1)
51  
52 /*
53 * compressed format
54 *
55 * 000LLLLL <L+1> ; literal, L+1=1..33 octets
56 * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset
57 * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset
58 *
59 */
60  
61 unsigned int
62 158 lzf_compress_best (const void *const in_data, unsigned int in_len,
63 void *out_data, unsigned int out_len
64 #if LZF_STATE_ARG
65 , LZF_STATE_BEST state
66 #endif
67 )
68 {
69 158 const u8 *ip = (const u8 *)in_data;
70 158 u8 *op = (u8 *)out_data;
71 158 const u8 *in_end = ip + in_len;
72 158 u8 *out_end = op + out_len;
73  
74 #if !LZF_STATE_ARG
75 LZF_STATE_BEST state;
76 #endif
77 #define STATE state[0]
78  
79 int lit;
80  
81 158 ▶ if (!in_len || !out_len)
Branch: 2 branches
BranchTrueFalseTotal
if (!in_len || !out_len) 158 0 158
if (!in_len || !out_len) 0 158 158
82 0 return 0;
83  
84 158 lit = 0; op++; /* start run */
85  
86 158 lit++; *op++ = *ip++;
87  
88 1577 ▶ while (ip < in_end - 2)
Branch: 1 branch
BranchTrueFalseTotal
while (ip < in_end - 2) 1577 0 1577
89 {
90 1577 int best_l = 0;
91 const u8 *best_p;
92 1577 int e = (in_end - ip < LZF_MAX_REF ? in_end - ip : LZF_MAX_REF) - 1;
93 1577 unsigned int res = ((unsigned long)ip) & (LZF_MAX_OFF - 1);
94 1577 u16 hash = HASH (ip);
95 u16 diff;
96 1577 ▶ const u8 *b = ip < (u8 *)in_data + LZF_MAX_OFF ? in_data : ip - LZF_MAX_OFF;
Branch: 1 branch
BranchTrueFalseTotal
const u8 *b = ip < (u8 *)in_data + LZF_MAX_OFF ? in_data : ip - LZF_MAX_OFF; 0 1577 1577
97 1577 const u8 *p = STATE.first [hash];
98 1577 STATE.prev [res] = ip - p; /* update ptr to previous hash match */
99 1577 STATE.first [hash] = ip; /* first hash match is here */
100  
101 1577 ▶ if (p < ip)
Branch: 1 branch
BranchTrueFalseTotal
if (p < ip) 748 829 1577
102 827 ▶ while (p >= b)
Branch: 1 branch
BranchTrueFalseTotal
while (p >= b) 79 748 827
103 {
104 79 ▶ if (p[2] == ip[2]) /* first two bytes almost always match */
Branch: 1 branch
BranchTrueFalseTotal
if (p[2] == ip[2]) /* first two bytes almost always match */ 79 0 79
105 79 ▶ if (p[best_l] == ip[best_l]) /* new match must be longer than the old one to qualify */
Branch: 1 branch
BranchTrueFalseTotal
if (p[best_l] == ip[best_l]) /* new match must be longer than the old one to qualify */ 79 0 79
106 79 ▶ if (p[1] == ip[1] && p[0] == ip[0]) /* just to be sure */
Branch: 2 branches
BranchTrueFalseTotal
if (p[1] == ip[1] && p[0] == ip[0]) /* just to be sure */ 79 0 79
if (p[1] == ip[1] && p[0] == ip[0]) /* just to be sure */ 79 0 79
107 {
108 79 int l = 3;
109  
110 713 ▶ while (p[l] == ip[l] && l < e)
Branch: 2 branches
BranchTrueFalseTotal
while (p[l] == ip[l] && l < e) 713 0 713
while (p[l] == ip[l] && l < e) 634 79 713
111 634 ++l;
112  
113 79 ▶ if (l >= best_l)
Branch: 1 branch
BranchTrueFalseTotal
if (l >= best_l) 79 0 79
114 {
115 79 best_p = p;
116 79 best_l = l;
117 }
118 }
119  
120 79 diff = STATE.prev [((unsigned long)p) & (LZF_MAX_OFF - 1)];
121 79 ▶ p = diff ? p - diff : 0;
Branch: 1 branch
BranchTrueFalseTotal
p = diff ? p - diff : 0; 79 0 79
122 }
123  
124 1577 ▶ if (best_l)
Branch: 1 branch
BranchTrueFalseTotal
if (best_l) 79 1498 1577
125 {
126 79 int len = best_l;
127 79 int off = ip - best_p - 1;
128  
129 79 ▶ if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */ 0 79 79
130 0 ▶ if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
Branch: 2 branches
BranchTrueFalseTotal
if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */ 0 0 0
if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */ 0 0 0
131 0 return 0;
132  
133 79 op [- lit - 1] = lit - 1; /* stop run */
134 79 op -= !lit; /* undo run if length is zero */
135  
136 79 len -= 2; /* len is now #octets - 1 */
137 79 ip++;
138  
139 79 ▶ if (len < 7)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 7) 0 79 79
140 {
141 0 *op++ = (off >> 8) + (len << 5);
142 }
143 else
144 {
145 79 *op++ = (off >> 8) + ( 7 << 5);
146 79 *op++ = len - 7;
147 }
148  
149 79 *op++ = off;
150  
151 79 lit = 0; op++; /* start run */
152  
153 79 ip += len + 1;
154  
155 79 ▶ if (expect_false (ip >= in_end - 2))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (ip >= in_end - 2)) 79 0 79
156 79 break;
157  
158 0 ip -= len + 1;
159  
160 //printf (" fill %p for %d\n", ip, len);//D
161 do
162 {
163 0 u16 hash = HASH (ip);
164 0 res = ((unsigned long)ip) & (LZF_MAX_OFF - 1);
165  
166 0 p = STATE.first [hash];
167 0 STATE.prev [res] = ip - p; /* update ptr to previous hash match */
168 0 STATE.first [hash] = ip; /* first hash match is here */
169  
170 0 ip++;
171 }
172 0 ▶ while (len--);
Branch: 1 branch
BranchTrueFalseTotal
while (len--); 0 0 0
173 }
174 else
175 {
176 /* one more literal byte we must copy */
177 1498 ▶ if (expect_false (op >= out_end))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (op >= out_end)) 79 1419 1498
178 79 return 0;
179  
180 1419 lit++; *op++ = *ip++;
181  
182 1419 ▶ if (expect_false (lit == LZF_MAX_LIT))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (lit == LZF_MAX_LIT)) 0 1419 1419
183 {
184 0 op [- lit - 1] = lit - 1; /* stop run */
185 0 lit = 0; op++; /* start run */
186 }
187 }
188 }
189  
190 79 ▶ if (op + 3 > out_end) /* at most 3 bytes can be missing here */
Branch: 1 branch
BranchTrueFalseTotal
if (op + 3 > out_end) /* at most 3 bytes can be missing here */ 0 79 79
191 0 return 0;
192  
193 158 ▶ while (ip < in_end)
Branch: 1 branch
BranchTrueFalseTotal
while (ip < in_end) 79 79 158
194 {
195 79 lit++; *op++ = *ip++;
196  
197 79 ▶ if (expect_false (lit == LZF_MAX_LIT))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (lit == LZF_MAX_LIT)) 0 79 79
198 {
199 0 op [- lit - 1] = lit - 1; /* stop run */
200 0 lit = 0; op++; /* start run */
201 }
202 }
203  
204 79 op [- lit - 1] = lit - 1; /* end run */
205 79 op -= !lit; /* undo run if length is zero */
206  
207 79 return op - (u8 *)out_data;
208  
209 #undef STATE
210 }
211  
212  
« LZF.xs ↑ Summary lzf_d.c »