xdiff.c

filter: Statementstmt 46.7% 800 / 1712 Branchbran 34.8% 403 / 1158 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 41.9% 1203 / 2870 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Diff/LibXDiff.pm ↑ Summary LibXDiff.xs »
1 #define PACKAGE_VERSION "0.23"
2 /*
3 * LibXDiff by Davide Libenzi ( File Differential Library )
4 * Copyright (C) 2003 Davide Libenzi
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Davide Libenzi <davidel@xmailserver.org>
21 *
22 */
23  
24 #include "xinclude.h"
25  
26  
27  
28 #define XDL_MAX_COST_MIN 256
29 #define XDL_HEUR_MIN_COST 256
30 #define XDL_LINE_MAX (long)((1UL << (8 * sizeof(long) - 1)) - 1)
31 #define XDL_SNAKE_CNT 20
32 #define XDL_K_HEUR 4
33  
34  
35  
36 typedef struct s_xdpsplit {
37 long i1, i2;
38 int min_lo, min_hi;
39 } xdpsplit_t;
40  
41  
42  
43 /*
44 * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers.
45 * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both
46 * the forward diagonal starting from (off1, off2) and the backward diagonal
47 * starting from (lim1, lim2). If the K values on the same diagonal crosses
48 * returns the furthest point of reach. We might end up having to expensive
49 * cases using this algorithm is full, so a little bit of heuristic is needed
50 * to cut the search and to return a suboptimal point.
51 */
52 0 static long xdl_split(unsigned long const *ha1, long off1, long lim1,
53 unsigned long const *ha2, long off2, long lim2,
54 long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
55 xdalgoenv_t *xenv) {
56 0 long dmin = off1 - lim2, dmax = lim1 - off2;
57 0 long fmid = off1 - off2, bmid = lim1 - lim2;
58 0 long odd = (fmid - bmid) & 1;
59 0 long fmin = fmid, fmax = fmid;
60 0 long bmin = bmid, bmax = bmid;
61 long ec, d, i1, i2, prev1, best, dd, v, k;
62  
63 /*
64 * Set initial diagonal values for both forward and backward path.
65 */
66 0 kvdf[fmid] = off1;
67 0 kvdb[bmid] = lim1;
68  
69 0 for (ec = 1;; ec++) {
70 0 int got_snake = 0;
71  
72 /*
73 * We need to extent the diagonal "domain" by one. If the next
74 * values exits the box boundaries we need to change it in the
75 * opposite direction because (max - min) must be a power of two.
76 * Also we initialize the extenal K value to -1 so that we can
77 * avoid extra conditions check inside the core loop.
78 */
79 0 ▶ if (fmin > dmin)
Branch: 1 branch
BranchTrueFalseTotal
if (fmin > dmin) 0 0 0
80 0 kvdf[--fmin - 1] = -1;
81 else
82 0 ++fmin;
83 0 ▶ if (fmax < dmax)
Branch: 1 branch
BranchTrueFalseTotal
if (fmax < dmax) 0 0 0
84 0 kvdf[++fmax + 1] = -1;
85 else
86 0 --fmax;
87  
88 0 ▶ for (d = fmax; d >= fmin; d -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = fmax; d >= fmin; d -= 2) { 0 0 0
89 0 ▶ if (kvdf[d - 1] >= kvdf[d + 1])
Branch: 1 branch
BranchTrueFalseTotal
if (kvdf[d - 1] >= kvdf[d + 1]) 0 0 0
90 0 i1 = kvdf[d - 1] + 1;
91 else
92 0 i1 = kvdf[d + 1];
93 0 prev1 = i1;
94 0 i2 = i1 - d;
95 0 ▶ for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++);
Branch: 3 branches
BranchTrueFalseTotal
for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++); 0 0 0
for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++); 0 0 0
for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++); 0 0 0
96 0 ▶ if (i1 - prev1 > xenv->snake_cnt)
Branch: 1 branch
BranchTrueFalseTotal
if (i1 - prev1 > xenv->snake_cnt) 0 0 0
97 0 got_snake = 1;
98 0 kvdf[d] = i1;
99 0 ▶ if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) {
Branch: 4 branches
BranchTrueFalseTotal
if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) { 0 0 0
if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) { 0 0 0
if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) { 0 0 0
if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) { 0 0 0
100 0 spl->i1 = i1;
101 0 spl->i2 = i2;
102 0 spl->min_lo = spl->min_hi = 1;
103 0 return ec;
104 }
105 }
106  
107 /*
108 * We need to extent the diagonal "domain" by one. If the next
109 * values exits the box boundaries we need to change it in the
110 * opposite direction because (max - min) must be a power of two.
111 * Also we initialize the extenal K value to -1 so that we can
112 * avoid extra conditions check inside the core loop.
113 */
114 0 ▶ if (bmin > dmin)
Branch: 1 branch
BranchTrueFalseTotal
if (bmin > dmin) 0 0 0
115 0 kvdb[--bmin - 1] = XDL_LINE_MAX;
116 else
117 0 ++bmin;
118 0 ▶ if (bmax < dmax)
Branch: 1 branch
BranchTrueFalseTotal
if (bmax < dmax) 0 0 0
119 0 kvdb[++bmax + 1] = XDL_LINE_MAX;
120 else
121 0 --bmax;
122  
123 0 ▶ for (d = bmax; d >= bmin; d -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = bmax; d >= bmin; d -= 2) { 0 0 0
124 0 ▶ if (kvdb[d - 1] < kvdb[d + 1])
Branch: 1 branch
BranchTrueFalseTotal
if (kvdb[d - 1] < kvdb[d + 1]) 0 0 0
125 0 i1 = kvdb[d - 1];
126 else
127 0 i1 = kvdb[d + 1] - 1;
128 0 prev1 = i1;
129 0 i2 = i1 - d;
130 0 ▶ for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--);
Branch: 3 branches
BranchTrueFalseTotal
for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--); 0 0 0
for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--); 0 0 0
for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--); 0 0 0
131 0 ▶ if (prev1 - i1 > xenv->snake_cnt)
Branch: 1 branch
BranchTrueFalseTotal
if (prev1 - i1 > xenv->snake_cnt) 0 0 0
132 0 got_snake = 1;
133 0 kvdb[d] = i1;
134 0 ▶ if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) {
Branch: 4 branches
BranchTrueFalseTotal
if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) { 0 0 0
if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) { 0 0 0
if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) { 0 0 0
if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) { 0 0 0
135 0 spl->i1 = i1;
136 0 spl->i2 = i2;
137 0 spl->min_lo = spl->min_hi = 1;
138 0 return ec;
139 }
140 }
141  
142 0 ▶ if (need_min)
Branch: 1 branch
BranchTrueFalseTotal
if (need_min) 0 0 0
143 0 continue;
144  
145 /*
146 * If the edit cost is above the heuristic trigger and if
147 * we got a good snake, we sample current diagonals to see
148 * if some of the, have reached an "interesting" path. Our
149 * measure is a function of the distance from the diagonal
150 * corner (i1 + i2) penalized with the distance from the
151 * mid diagonal itself. If this value is above the current
152 * edit cost times a magic factor (XDL_K_HEUR) we consider
153 * it interesting.
154 */
155 0 ▶ if (got_snake && ec > xenv->heur_min) {
Branch: 2 branches
BranchTrueFalseTotal
if (got_snake && ec > xenv->heur_min) { 0 0 0
if (got_snake && ec > xenv->heur_min) { 0 0 0
156 0 ▶ for (best = 0, d = fmax; d >= fmin; d -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (best = 0, d = fmax; d >= fmin; d -= 2) { 0 0 0
157 0 ▶ dd = d > fmid ? d - fmid: fmid - d;
Branch: 1 branch
BranchTrueFalseTotal
dd = d > fmid ? d - fmid: fmid - d; 0 0 0
158 0 i1 = kvdf[d];
159 0 i2 = i1 - d;
160 0 v = (i1 - off1) + (i2 - off2) - dd;
161  
162 0 ▶ if (v > XDL_K_HEUR * ec && v > best &&
Branch: 2 branches
BranchTrueFalseTotal
if (v > XDL_K_HEUR * ec && v > best && 0 0 0
if (v > XDL_K_HEUR * ec && v > best && 0 0 0
163 0 ▶ off1 + xenv->snake_cnt <= i1 && i1 < lim1 &&
Branch: 2 branches
BranchTrueFalseTotal
off1 + xenv->snake_cnt <= i1 && i1 < lim1 && 0 0 0
off1 + xenv->snake_cnt <= i1 && i1 < lim1 && 0 0 0
164 0 ▶ off2 + xenv->snake_cnt <= i2 && i2 < lim2) {
Branch: 2 branches
BranchTrueFalseTotal
off2 + xenv->snake_cnt <= i2 && i2 < lim2) { 0 0 0
off2 + xenv->snake_cnt <= i2 && i2 < lim2) { 0 0 0
165 0 ▶ for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++) 0 0 0
166 0 ▶ if (k == xenv->snake_cnt) {
Branch: 1 branch
BranchTrueFalseTotal
if (k == xenv->snake_cnt) { 0 0 0
167 0 best = v;
168 0 spl->i1 = i1;
169 0 spl->i2 = i2;
170 0 break;
171 }
172 }
173 }
174 0 ▶ if (best > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (best > 0) { 0 0 0
175 0 spl->min_lo = 1;
176 0 spl->min_hi = 0;
177 0 return ec;
178 }
179  
180 0 ▶ for (best = 0, d = bmax; d >= bmin; d -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (best = 0, d = bmax; d >= bmin; d -= 2) { 0 0 0
181 0 ▶ dd = d > bmid ? d - bmid: bmid - d;
Branch: 1 branch
BranchTrueFalseTotal
dd = d > bmid ? d - bmid: bmid - d; 0 0 0
182 0 i1 = kvdb[d];
183 0 i2 = i1 - d;
184 0 v = (lim1 - i1) + (lim2 - i2) - dd;
185  
186 0 ▶ if (v > XDL_K_HEUR * ec && v > best &&
Branch: 3 branches
BranchTrueFalseTotal
if (v > XDL_K_HEUR * ec && v > best && 0 0 0
if (v > XDL_K_HEUR * ec && v > best && 0 0 0
if (v > XDL_K_HEUR * ec && v > best && 0 0 0
187 0 ▶ off1 < i1 && i1 <= lim1 - xenv->snake_cnt &&
Branch: 2 branches
BranchTrueFalseTotal
off1 < i1 && i1 <= lim1 - xenv->snake_cnt && 0 0 0
off1 < i1 && i1 <= lim1 - xenv->snake_cnt && 0 0 0
188 0 ▶ off2 < i2 && i2 <= lim2 - xenv->snake_cnt) {
Branch: 1 branch
BranchTrueFalseTotal
off2 < i2 && i2 <= lim2 - xenv->snake_cnt) { 0 0 0
189 0 ▶ for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++) 0 0 0
190 0 ▶ if (k == xenv->snake_cnt - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (k == xenv->snake_cnt - 1) { 0 0 0
191 0 best = v;
192 0 spl->i1 = i1;
193 0 spl->i2 = i2;
194 0 break;
195 }
196 }
197 }
198 0 ▶ if (best > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (best > 0) { 0 0 0
199 0 spl->min_lo = 0;
200 0 spl->min_hi = 1;
201 0 return ec;
202 }
203 }
204  
205 /*
206 * Enough is enough. We spent too much time here and now we collect
207 * the furthest reaching path using the (i1 + i2) measure.
208 */
209 0 ▶ if (ec >= xenv->mxcost) {
Branch: 1 branch
BranchTrueFalseTotal
if (ec >= xenv->mxcost) { 0 0 0
210 long fbest, fbest1, bbest, bbest1;
211  
212 0 fbest = -1;
213 0 ▶ for (d = fmax; d >= fmin; d -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = fmax; d >= fmin; d -= 2) { 0 0 0
214 0 i1 = XDL_MIN(kvdf[d], lim1);
215 0 i2 = i1 - d;
216 0 ▶ if (lim2 < i2)
Branch: 1 branch
BranchTrueFalseTotal
if (lim2 < i2) 0 0 0
217 0 i1 = lim2 + d, i2 = lim2;
218 0 ▶ if (fbest < i1 + i2) {
Branch: 1 branch
BranchTrueFalseTotal
if (fbest < i1 + i2) { 0 0 0
219 0 fbest = i1 + i2;
220 0 fbest1 = i1;
221 }
222 }
223  
224 0 bbest = XDL_LINE_MAX;
225 0 ▶ for (d = bmax; d >= bmin; d -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = bmax; d >= bmin; d -= 2) { 0 0 0
226 0 i1 = XDL_MAX(off1, kvdb[d]);
227 0 i2 = i1 - d;
228 0 ▶ if (i2 < off2)
Branch: 1 branch
BranchTrueFalseTotal
if (i2 < off2) 0 0 0
229 0 i1 = off2 + d, i2 = off2;
230 0 ▶ if (i1 + i2 < bbest) {
Branch: 1 branch
BranchTrueFalseTotal
if (i1 + i2 < bbest) { 0 0 0
231 0 bbest = i1 + i2;
232 0 bbest1 = i1;
233 }
234 }
235  
236 0 ▶ if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) { 0 0 0
237 0 spl->i1 = fbest1;
238 0 spl->i2 = fbest - fbest1;
239 0 spl->min_lo = 1;
240 0 spl->min_hi = 0;
241 } else {
242 0 spl->i1 = bbest1;
243 0 spl->i2 = bbest - bbest1;
244 0 spl->min_lo = 0;
245 0 spl->min_hi = 1;
246 }
247 0 return ec;
248 }
249 }
250  
251 return -1;
252 }
253  
254  
255 /*
256 * Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling
257 * the box splitting function. Note that the real job (marking changed lines)
258 * is done in the two boundary reaching checks.
259 */
260 2 int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,
261 diffdata_t *dd2, long off2, long lim2,
262 long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) {
263 2 unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha;
264  
265 /*
266 * Shrink the box by walking through each diagonal snake (SW and NE).
267 */
268 3 ▶ for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++);
Branch: 3 branches
BranchTrueFalseTotal
for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++); 1 2 3
for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++); 1 0 1
for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++); 1 0 1
269 2 ▶ for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--);
Branch: 3 branches
BranchTrueFalseTotal
for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--); 0 2 2
for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--); 0 0 0
for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--); 0 0 0
270  
271 /*
272 * If one dimension is empty, then all records on the other one must
273 * be obviously changed.
274 */
275 2 ▶ if (off1 == lim1) {
Branch: 1 branch
BranchTrueFalseTotal
if (off1 == lim1) { 2 0 2
276 2 char *rchg2 = dd2->rchg;
277 2 long *rindex2 = dd2->rindex;
278  
279 2 ▶ for (; off2 < lim2; off2++)
Branch: 1 branch
BranchTrueFalseTotal
for (; off2 < lim2; off2++) 0 2 2
280 0 rchg2[rindex2[off2]] = 1;
281 0 ▶ } else if (off2 == lim2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (off2 == lim2) { 0 0 0
282 0 char *rchg1 = dd1->rchg;
283 0 long *rindex1 = dd1->rindex;
284  
285 0 ▶ for (; off1 < lim1; off1++)
Branch: 1 branch
BranchTrueFalseTotal
for (; off1 < lim1; off1++) 0 0 0
286 0 rchg1[rindex1[off1]] = 1;
287 } else {
288 long ec;
289 xdpsplit_t spl;
290  
291 /*
292 * Divide ...
293 */
294 0 ▶ if ((ec = xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb,
Branch: 1 branch
BranchTrueFalseTotal
if ((ec = xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb, 0 0 0
295 need_min, &spl, xenv)) < 0) {
296  
297 0 return -1;
298 }
299  
300 /*
301 * ... et Impera.
302 */
303 0 ▶ if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2,
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2, 0 0 0
304 0 ▶ kvdf, kvdb, spl.min_lo, xenv) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
kvdf, kvdb, spl.min_lo, xenv) < 0 || 0 0 0
305 0 xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2,
306 kvdf, kvdb, spl.min_hi, xenv) < 0) {
307  
308 0 return -1;
309 }
310 }
311  
312 2 return 0;
313 }
314  
315  
316 2 int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
317 xdfenv_t *xe) {
318 long ndiags;
319 long *kvd, *kvdf, *kvdb;
320 xdalgoenv_t xenv;
321 diffdata_t dd1, dd2;
322  
323 2 ▶ if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) { 0 2 2
324  
325 0 return -1;
326 }
327  
328 /*
329 * Allocate and setup K vectors to be used by the differential algorithm.
330 * One is to store the forward path and one to store the backward path.
331 */
332 2 ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
333 2 ▶ if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) { 0 2 2
334  
335 0 xdl_free_env(xe);
336 0 return -1;
337 }
338 2 kvdf = kvd;
339 2 kvdb = kvdf + ndiags;
340 2 kvdf += xe->xdf2.nreff + 1;
341 2 kvdb += xe->xdf2.nreff + 1;
342  
343 2 xenv.mxcost = xdl_bogosqrt(ndiags);
344 2 ▶ if (xenv.mxcost < XDL_MAX_COST_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (xenv.mxcost < XDL_MAX_COST_MIN) 2 0 2
345 2 xenv.mxcost = XDL_MAX_COST_MIN;
346 2 xenv.snake_cnt = XDL_SNAKE_CNT;
347 2 xenv.heur_min = XDL_HEUR_MIN_COST;
348  
349 2 dd1.nrec = xe->xdf1.nreff;
350 2 dd1.ha = xe->xdf1.ha;
351 2 dd1.rchg = xe->xdf1.rchg;
352 2 dd1.rindex = xe->xdf1.rindex;
353 2 dd2.nrec = xe->xdf2.nreff;
354 2 dd2.ha = xe->xdf2.ha;
355 2 dd2.rchg = xe->xdf2.rchg;
356 2 dd2.rindex = xe->xdf2.rindex;
357  
358 2 ▶ if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec,
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec, 0 2 2
359 2 kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) {
360  
361 0 xdl_free(kvd);
362 0 xdl_free_env(xe);
363 0 return -1;
364 }
365  
366 2 xdl_free(kvd);
367  
368 2 return 0;
369 }
370  
371  
372 3 static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) {
373 xdchange_t *xch;
374  
375 3 ▶ if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t))))
Branch: 1 branch
BranchTrueFalseTotal
if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t)))) 0 3 3
376 0 return NULL;
377  
378 3 xch->next = xscr;
379 3 xch->i1 = i1;
380 3 xch->i2 = i2;
381 3 xch->chg1 = chg1;
382 3 xch->chg2 = chg2;
383  
384 3 return xch;
385 }
386  
387  
388 4 static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo) {
389 4 long ix, ixo, ixs, ixref, grpsiz, nrec = xdf->nrec;
390 4 char *rchg = xdf->rchg, *rchgo = xdfo->rchg;
391 4 xrecord_t **recs = xdf->recs;
392  
393 /*
394 * This is the same of what GNU diff does. Move back and forward
395 * change groups for a consistent and pretty diff output. This also
396 * helps in finding joineable change groups and reduce the diff size.
397 */
398 4 for (ix = ixo = 0;;) {
399 /*
400 * Find the first changed line in the to-be-compacted file.
401 * We need to keep track of both indexes, so if we find a
402 * changed lines group on the other file, while scanning the
403 * to-be-compacted file, we need to skip it properly. Note
404 * that loops that are testing for changed lines on rchg* do
405 * not need index bounding since the array is prepared with
406 * a zero at position -1 and N.
407 */
408 13 ▶ for (; ix < nrec && !rchg[ix]; ix++)
Branch: 2 branches
BranchTrueFalseTotal
for (; ix < nrec && !rchg[ix]; ix++) 9 4 13
for (; ix < nrec && !rchg[ix]; ix++) 4 5 9
409 4 ▶ while (rchgo[ixo++]);
Branch: 1 branch
BranchTrueFalseTotal
while (rchgo[ixo++]); 0 4 4
410 9 ▶ if (ix == nrec)
Branch: 1 branch
BranchTrueFalseTotal
if (ix == nrec) 4 5 9
411 4 break;
412  
413 /*
414 * Record the start of a changed-group in the to-be-compacted file
415 * and find the end of it, on both to-be-compacted and other file
416 * indexes (ix and ixo).
417 */
418 5 ixs = ix;
419 5 ▶ for (ix++; rchg[ix]; ix++);
Branch: 1 branch
BranchTrueFalseTotal
for (ix++; rchg[ix]; ix++); 0 5 5
420 9 ▶ for (; rchgo[ixo]; ixo++);
Branch: 1 branch
BranchTrueFalseTotal
for (; rchgo[ixo]; ixo++); 4 5 9
421  
422 do {
423 5 grpsiz = ix - ixs;
424  
425 /*
426 * If the line before the current change group, is equal to
427 * the last line of the current change group, shift backward
428 * the group.
429 */
430 5 ▶ while (ixs > 0 && recs[ixs - 1]->ha == recs[ix - 1]->ha &&
Branch: 2 branches
BranchTrueFalseTotal
while (ixs > 0 && recs[ixs - 1]->ha == recs[ix - 1]->ha && 3 2 5
while (ixs > 0 && recs[ixs - 1]->ha == recs[ix - 1]->ha && 0 3 3
431 0 ▶ XDL_RECMATCH(recs[ixs - 1], recs[ix - 1])) {
Branch: 2 branches
BranchTrueFalseTotal
XDL_RECMATCH(recs[ixs - 1], recs[ix - 1])) { 0 0 0
XDL_RECMATCH(recs[ixs - 1], recs[ix - 1])) { 0 0 0
432 0 rchg[--ixs] = 1;
433 0 rchg[--ix] = 0;
434  
435 /*
436 * This change might have joined two change groups,
437 * so we try to take this scenario in account by moving
438 * the start index accordingly (and so the other-file
439 * end-of-group index).
440 */
441 0 ▶ for (; rchg[ixs - 1]; ixs--);
Branch: 1 branch
BranchTrueFalseTotal
for (; rchg[ixs - 1]; ixs--); 0 0 0
442 0 ▶ while (rchgo[--ixo]);
Branch: 1 branch
BranchTrueFalseTotal
while (rchgo[--ixo]); 0 0 0
443 }
444  
445 /*
446 * Record the end-of-group position in case we are matched
447 * with a group of changes in the other file (that is, the
448 * change record before the enf-of-group index in the other
449 * file is set).
450 */
451 5 ▶ ixref = rchgo[ixo - 1] ? ix: nrec;
Branch: 1 branch
BranchTrueFalseTotal
ixref = rchgo[ixo - 1] ? ix: nrec; 4 1 5
452  
453 /*
454 * If the first line of the current change group, is equal to
455 * the line next of the current change group, shift forward
456 * the group.
457 */
458 5 ▶ while (ix < nrec && recs[ixs]->ha == recs[ix]->ha &&
Branch: 2 branches
BranchTrueFalseTotal
while (ix < nrec && recs[ixs]->ha == recs[ix]->ha && 2 3 5
while (ix < nrec && recs[ixs]->ha == recs[ix]->ha && 0 2 2
459 0 ▶ XDL_RECMATCH(recs[ixs], recs[ix])) {
Branch: 2 branches
BranchTrueFalseTotal
XDL_RECMATCH(recs[ixs], recs[ix])) { 0 0 0
XDL_RECMATCH(recs[ixs], recs[ix])) { 0 0 0
460 0 rchg[ixs++] = 0;
461 0 rchg[ix++] = 1;
462  
463 /*
464 * This change might have joined two change groups,
465 * so we try to take this scenario in account by moving
466 * the start index accordingly (and so the other-file
467 * end-of-group index). Keep tracking the reference
468 * index in case we are shifting together with a
469 * corresponding group of changes in the other file.
470 */
471 0 ▶ for (; rchg[ix]; ix++);
Branch: 1 branch
BranchTrueFalseTotal
for (; rchg[ix]; ix++); 0 0 0
472 0 ▶ while (rchgo[++ixo])
Branch: 1 branch
BranchTrueFalseTotal
while (rchgo[++ixo]) 0 0 0
473 0 ixref = ix;
474 }
475 5 ▶ } while (grpsiz != ix - ixs);
Branch: 1 branch
BranchTrueFalseTotal
} while (grpsiz != ix - ixs); 0 5 5
476  
477 /*
478 * Try to move back the possibly merged group of changes, to match
479 * the recorded postion in the other file.
480 */
481 5 ▶ while (ixref < ix) {
Branch: 1 branch
BranchTrueFalseTotal
while (ixref < ix) { 0 5 5
482 0 rchg[--ixs] = 1;
483 0 rchg[--ix] = 0;
484 0 ▶ while (rchgo[--ixo]);
Branch: 1 branch
BranchTrueFalseTotal
while (rchgo[--ixo]); 0 0 0
485 }
486 }
487  
488 4 return 0;
489 }
490  
491  
492 2 int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) {
493 2 xdchange_t *cscr = NULL, *xch;
494 2 char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg;
495 long i1, i2, l1, l2;
496  
497 /*
498 * Trivial. Collects "groups" of changes and creates an edit script.
499 */
500 6 ▶ for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--)
Branch: 2 branches
BranchTrueFalseTotal
for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--) 4 2 6
for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--) 0 2 2
501 4 ▶ if (rchg1[i1 - 1] || rchg2[i2 - 1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (rchg1[i1 - 1] || rchg2[i2 - 1]) { 2 2 4
if (rchg1[i1 - 1] || rchg2[i2 - 1]) { 1 1 2
502 5 ▶ for (l1 = i1; rchg1[i1 - 1]; i1--);
Branch: 1 branch
BranchTrueFalseTotal
for (l1 = i1; rchg1[i1 - 1]; i1--); 2 3 5
503 6 ▶ for (l2 = i2; rchg2[i2 - 1]; i2--);
Branch: 1 branch
BranchTrueFalseTotal
for (l2 = i2; rchg2[i2 - 1]; i2--); 3 3 6
504  
505 3 ▶ if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) { 0 3 3
506 0 xdl_free_script(cscr);
507 0 return -1;
508 }
509 3 cscr = xch;
510 }
511  
512 2 *xscr = cscr;
513  
514 2 return 0;
515 }
516  
517  
518 2 void xdl_free_script(xdchange_t *xscr) {
519 xdchange_t *xch;
520  
521 5 ▶ while ((xch = xscr) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((xch = xscr) != NULL) { 3 2 5
522 3 xscr = xscr->next;
523 3 xdl_free(xch);
524 }
525 2 }
526  
527  
528 2 int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
529 xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
530 xdchange_t *xscr;
531 xdfenv_t xe;
532  
533 2 ▶ if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) { 0 2 2
534  
535 0 return -1;
536 }
537 4 if (xdl_change_compact(&xe.xdf1, &xe.xdf2) < 0 ||
538 4 ▶ xdl_change_compact(&xe.xdf2, &xe.xdf1) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
xdl_change_compact(&xe.xdf2, &xe.xdf1) < 0 || 0 2 2
539 2 xdl_build_script(&xe, &xscr) < 0) {
540  
541 0 xdl_free_env(&xe);
542 0 return -1;
543 }
544 2 ▶ if (xscr) {
Branch: 1 branch
BranchTrueFalseTotal
if (xscr) { 2 0 2
545 2 ▶ if (xdl_emit_diff(&xe, xscr, ecb, xecfg) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_diff(&xe, xscr, ecb, xecfg) < 0) { 0 2 2
546  
547 0 xdl_free_script(xscr);
548 0 xdl_free_env(&xe);
549 0 return -1;
550 }
551 2 xdl_free_script(xscr);
552 }
553 2 xdl_free_env(&xe);
554  
555 2 return 0;
556 }
557  
558 /*
559 * LibXDiff by Davide Libenzi ( File Differential Library )
560 * Copyright (C) 2003 Davide Libenzi
561 *
562 * This library is free software; you can redistribute it and/or
563 * modify it under the terms of the GNU Lesser General Public
564 * License as published by the Free Software Foundation; either
565 * version 2.1 of the License, or (at your option) any later version.
566 *
567 * This library is distributed in the hope that it will be useful,
568 * but WITHOUT ANY WARRANTY; without even the implied warranty of
569 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
570 * Lesser General Public License for more details.
571 *
572 * You should have received a copy of the GNU Lesser General Public
573 * License along with this library; if not, write to the Free Software
574 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
575 *
576 * Davide Libenzi <davidel@xmailserver.org>
577 *
578 */
579  
580 #include "xinclude.h"
581  
582  
583 #define XDL_KPDIS_RUN 4
584 #define XDL_MAX_EQLIMIT 1024
585 #define XDL_SIMSCAN_WINDOWN 100
586  
587  
588 typedef struct s_xdlclass {
589 struct s_xdlclass *next;
590 unsigned long ha;
591 char const *line;
592 long size;
593 long idx;
594 } xdlclass_t;
595  
596 typedef struct s_xdlclassifier {
597 unsigned int hbits;
598 long hsize;
599 xdlclass_t **rchash;
600 chastore_t ncha;
601 long count;
602 } xdlclassifier_t;
603  
604  
605  
606 2 static int xdl_init_classifier(xdlclassifier_t *cf, long size) {
607 long i;
608  
609 2 cf->hbits = xdl_hashbits((unsigned int) size);
610 2 cf->hsize = 1 << cf->hbits;
611  
612 2 ▶ if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) { 0 2 2
613  
614 0 return -1;
615 }
616 2 ▶ if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) { 0 2 2
617  
618 0 xdl_cha_free(&cf->ncha);
619 0 return -1;
620 }
621 26 ▶ for (i = 0; i < cf->hsize; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cf->hsize; i++) 24 2 26
622 24 cf->rchash[i] = NULL;
623  
624 2 cf->count = 0;
625  
626 2 return 0;
627 }
628  
629  
630 2 static void xdl_free_classifier(xdlclassifier_t *cf) {
631 2 xdl_free(cf->rchash);
632 2 xdl_cha_free(&cf->ncha);
633 2 }
634  
635  
636 9 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
637 xrecord_t *rec) {
638 long hi;
639 char const *line;
640 xdlclass_t *rcrec;
641  
642 9 line = rec->ptr;
643 9 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
644 9 ▶ for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
Branch: 1 branch
BranchTrueFalseTotal
for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next) 2 7 9
645 2 ▶ if (rcrec->ha == rec->ha && rcrec->size == rec->size &&
Branch: 2 branches
BranchTrueFalseTotal
if (rcrec->ha == rec->ha && rcrec->size == rec->size && 2 0 2
if (rcrec->ha == rec->ha && rcrec->size == rec->size && 2 0 2
646 2 ▶ !memcmp(line, rcrec->line, rec->size))
Branch: 1 branch
BranchTrueFalseTotal
!memcmp(line, rcrec->line, rec->size)) 2 0 2
647 2 break;
648  
649 9 ▶ if (!rcrec) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rcrec) { 7 2 9
650 7 ▶ if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(rcrec = xdl_cha_alloc(&cf->ncha))) { 0 7 7
651  
652 0 return -1;
653 }
654 7 rcrec->idx = cf->count++;
655 7 rcrec->line = line;
656 7 rcrec->size = rec->size;
657 7 rcrec->ha = rec->ha;
658 7 rcrec->next = cf->rchash[hi];
659 7 cf->rchash[hi] = rcrec;
660 }
661  
662 9 rec->ha = (unsigned long) rcrec->idx;
663  
664 9 hi = (long) XDL_HASHLONG(rec->ha, hbits);
665 9 rec->next = rhash[hi];
666 9 rhash[hi] = rec;
667  
668 9 return 0;
669 }
670  
671  
672 4 static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
673 xdlclassifier_t *cf, xdfile_t *xdf) {
674 unsigned int hbits;
675 long i, nrec, hsize, bsize;
676 unsigned long hav;
677 char const *blk, *cur, *top, *prev;
678 xrecord_t *crec;
679 xrecord_t **recs, **rrecs;
680 xrecord_t **rhash;
681 unsigned long *ha;
682 char *rchg;
683 long *rindex;
684  
685 4 ▶ if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) { 0 4 4
686  
687 0 return -1;
688 }
689 4 ▶ if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) { 0 4 4
690  
691 0 xdl_cha_free(&xdf->rcha);
692 0 return -1;
693 }
694  
695 4 hbits = xdl_hashbits((unsigned int) narec);
696 4 hsize = 1 << hbits;
697 4 ▶ if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) { 0 4 4
698  
699 0 xdl_free(recs);
700 0 xdl_cha_free(&xdf->rcha);
701 0 return -1;
702 }
703 28 ▶ for (i = 0; i < hsize; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hsize; i++) 24 4 28
704 24 rhash[i] = NULL;
705  
706 4 nrec = 0;
707 4 ▶ if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) { 4 0 4
708 4 for (top = blk + bsize;;) {
709 13 ▶ if (cur >= top) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur >= top) { 4 9 13
710 4 ▶ if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(cur = blk = xdl_mmfile_next(mf, &bsize))) 4 0 4
711 4 break;
712 0 top = blk + bsize;
713 }
714 9 prev = cur;
715 9 hav = xdl_hash_record(&cur, top);
716 9 ▶ if (nrec >= narec) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrec >= narec) { 0 9 9
717 0 narec *= 2;
718 0 ▶ if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) { 0 0 0
719  
720 0 xdl_free(rhash);
721 0 xdl_free(recs);
722 0 xdl_cha_free(&xdf->rcha);
723 0 return -1;
724 }
725 0 recs = rrecs;
726 }
727 9 ▶ if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(crec = xdl_cha_alloc(&xdf->rcha))) { 0 9 9
728  
729 0 xdl_free(rhash);
730 0 xdl_free(recs);
731 0 xdl_cha_free(&xdf->rcha);
732 0 return -1;
733 }
734 9 crec->ptr = prev;
735 9 crec->size = (long) (cur - prev);
736 9 crec->ha = hav;
737 9 recs[nrec++] = crec;
738  
739 9 ▶ if (xdl_classify_record(cf, rhash, hbits, crec) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_classify_record(cf, rhash, hbits, crec) < 0) { 0 9 9
740  
741 0 xdl_free(rhash);
742 0 xdl_free(recs);
743 0 xdl_cha_free(&xdf->rcha);
744 0 return -1;
745 }
746 }
747 }
748  
749 4 ▶ if (!(rchg = (char *) xdl_malloc(nrec + 2))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(rchg = (char *) xdl_malloc(nrec + 2))) { 0 4 4
750  
751 0 xdl_free(rhash);
752 0 xdl_free(recs);
753 0 xdl_cha_free(&xdf->rcha);
754 0 return -1;
755 }
756 4 memset(rchg, 0, nrec + 2);
757  
758 4 ▶ if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) { 0 4 4
759  
760 0 xdl_free(rchg);
761 0 xdl_free(rhash);
762 0 xdl_free(recs);
763 0 xdl_cha_free(&xdf->rcha);
764 0 return -1;
765 }
766 4 ▶ if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) { 0 4 4
767  
768 0 xdl_free(rindex);
769 0 xdl_free(rchg);
770 0 xdl_free(rhash);
771 0 xdl_free(recs);
772 0 xdl_cha_free(&xdf->rcha);
773 0 return -1;
774 }
775  
776 4 xdf->nrec = nrec;
777 4 xdf->recs = recs;
778 4 xdf->hbits = hbits;
779 4 xdf->rhash = rhash;
780 4 xdf->rchg = rchg + 1;
781 4 xdf->rindex = rindex;
782 4 xdf->nreff = 0;
783 4 xdf->ha = ha;
784 4 xdf->dstart = 0;
785 4 xdf->dend = nrec - 1;
786  
787 4 return 0;
788 }
789  
790  
791 4 static void xdl_free_ctx(xdfile_t *xdf) {
792 4 xdl_free(xdf->rhash);
793 4 xdl_free(xdf->rindex);
794 4 xdl_free(xdf->rchg - 1);
795 4 xdl_free(xdf->ha);
796 4 xdl_free(xdf->recs);
797 4 xdl_cha_free(&xdf->rcha);
798 4 }
799  
800  
801 0 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
802 long r, rdis0, rpdis0, rdis1, rpdis1;
803  
804 /*
805 * Limits the window the is examined during the similar-lines
806 * scan. The loops below stops when dis[i - r] == 1 (line that
807 * has no match), but there are corner cases where the loop
808 * proceed all the way to the extremities by causing huge
809 * performance penalties in case of big files.
810 */
811 0 ▶ if (i - s > XDL_SIMSCAN_WINDOWN)
Branch: 1 branch
BranchTrueFalseTotal
if (i - s > XDL_SIMSCAN_WINDOWN) 0 0 0
812 0 s = i - XDL_SIMSCAN_WINDOWN;
813 0 ▶ if (e - i > XDL_SIMSCAN_WINDOWN)
Branch: 1 branch
BranchTrueFalseTotal
if (e - i > XDL_SIMSCAN_WINDOWN) 0 0 0
814 0 e = i + XDL_SIMSCAN_WINDOWN;
815  
816 /*
817 * Scans the lines before 'i' to find a run of lines that either
818 * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
819 * Note that we always call this function with dis[i] > 1, so the
820 * current line (i) is already a multimatch line.
821 */
822 0 ▶ for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) { 0 0 0
823 0 ▶ if (!dis[i - r])
Branch: 1 branch
BranchTrueFalseTotal
if (!dis[i - r]) 0 0 0
824 0 rdis0++;
825 0 ▶ else if (dis[i - r] == 2)
Branch: 1 branch
BranchTrueFalseTotal
else if (dis[i - r] == 2) 0 0 0
826 0 rpdis0++;
827 else
828 0 break;
829 }
830 /*
831 * If the run before the line 'i' found only multimatch lines, we
832 * return 0 and hence we don't make the current line (i) discarded.
833 * We want to discard multimatch lines only when they appear in the
834 * middle of runs with nomatch lines (dis[j] == 0).
835 */
836 0 ▶ if (rdis0 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rdis0 == 0) 0 0 0
837 0 return 0;
838 0 ▶ for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) { 0 0 0
839 0 ▶ if (!dis[i + r])
Branch: 1 branch
BranchTrueFalseTotal
if (!dis[i + r]) 0 0 0
840 0 rdis1++;
841 0 ▶ else if (dis[i + r] == 2)
Branch: 1 branch
BranchTrueFalseTotal
else if (dis[i + r] == 2) 0 0 0
842 0 rpdis1++;
843 else
844 0 break;
845 }
846 /*
847 * If the run after the line 'i' found only multimatch lines, we
848 * return 0 and hence we don't make the current line (i) discarded.
849 */
850 0 ▶ if (rdis1 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rdis1 == 0) 0 0 0
851 0 return 0;
852 0 rdis1 += rdis0;
853 0 rpdis1 += rpdis0;
854  
855 0 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
856 }
857  
858  
859 /*
860 * Try to reduce the problem complexity, discard records that have no
861 * matches on the other file. Also, lines that have multiple matches
862 * might be potentially discarded if they happear in a run of discardable.
863 */
864 2 static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
865 long i, nm, rhi, nreff, mlim;
866 unsigned long hav;
867 xrecord_t **recs;
868 xrecord_t *rec;
869 char *dis, *dis1, *dis2;
870  
871 2 ▶ if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) { 0 2 2
872  
873 0 return -1;
874 }
875 2 memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
876 2 dis1 = dis;
877 2 dis2 = dis1 + xdf1->nrec + 1;
878  
879 2 ▶ if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
Branch: 1 branch
BranchTrueFalseTotal
if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT) 0 2 2
880 0 mlim = XDL_MAX_EQLIMIT;
881 5 ▶ for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) { 3 2 5
882 3 hav = (*recs)->ha;
883 3 rhi = (long) XDL_HASHLONG(hav, xdf2->hbits);
884 4 ▶ for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next)
Branch: 1 branch
BranchTrueFalseTotal
for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next) 1 3 4
885 1 ▶ if (rec->ha == hav && ++nm == mlim)
Branch: 2 branches
BranchTrueFalseTotal
if (rec->ha == hav && ++nm == mlim) 1 0 1
if (rec->ha == hav && ++nm == mlim) 0 1 1
886 0 break;
887 3 ▶ dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
Branch: 2 branches
BranchTrueFalseTotal
dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1; 1 2 3
dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1; 0 1 1
888 }
889  
890 2 ▶ if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
Branch: 1 branch
BranchTrueFalseTotal
if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT) 0 2 2
891 0 mlim = XDL_MAX_EQLIMIT;
892 6 ▶ for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) { 4 2 6
893 4 hav = (*recs)->ha;
894 4 rhi = (long) XDL_HASHLONG(hav, xdf1->hbits);
895 5 ▶ for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next)
Branch: 1 branch
BranchTrueFalseTotal
for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next) 1 4 5
896 1 ▶ if (rec->ha == hav && ++nm == mlim)
Branch: 2 branches
BranchTrueFalseTotal
if (rec->ha == hav && ++nm == mlim) 1 0 1
if (rec->ha == hav && ++nm == mlim) 0 1 1
897 0 break;
898 4 ▶ dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
Branch: 2 branches
BranchTrueFalseTotal
dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1; 1 3 4
dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1; 0 1 1
899 }
900  
901 2 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
902 5 ▶ i <= xdf1->dend; i++, recs++) {
Branch: 1 branch
BranchTrueFalseTotal
i <= xdf1->dend; i++, recs++) { 3 2 5
903 3 ▶ if (dis1[i] == 1 ||
Branch: 1 branch
BranchTrueFalseTotal
if (dis1[i] == 1 || 2 1 3
904 2 ▶ (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
Branch: 2 branches
BranchTrueFalseTotal
(dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) { 0 2 2
(dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) { 0 0 0
905 1 xdf1->rindex[nreff] = i;
906 1 xdf1->ha[nreff] = (*recs)->ha;
907 1 nreff++;
908 } else
909 2 xdf1->rchg[i] = 1;
910 }
911 2 xdf1->nreff = nreff;
912  
913 2 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
914 6 ▶ i <= xdf2->dend; i++, recs++) {
Branch: 1 branch
BranchTrueFalseTotal
i <= xdf2->dend; i++, recs++) { 4 2 6
915 4 ▶ if (dis2[i] == 1 ||
Branch: 1 branch
BranchTrueFalseTotal
if (dis2[i] == 1 || 3 1 4
916 3 ▶ (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
Branch: 2 branches
BranchTrueFalseTotal
(dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) { 0 3 3
(dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) { 0 0 0
917 1 xdf2->rindex[nreff] = i;
918 1 xdf2->ha[nreff] = (*recs)->ha;
919 1 nreff++;
920 } else
921 3 xdf2->rchg[i] = 1;
922 }
923 2 xdf2->nreff = nreff;
924  
925 2 xdl_free(dis);
926  
927 2 return 0;
928 }
929  
930  
931 /*
932 * Early trim initial and terminal matching records.
933 */
934 2 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
935 long i, lim;
936 xrecord_t **recs1, **recs2;
937  
938 2 recs1 = xdf1->recs;
939 2 recs2 = xdf2->recs;
940 3 ▶ for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim; 3 0 3
941 1 i++, recs1++, recs2++)
942 3 ▶ if ((*recs1)->ha != (*recs2)->ha)
Branch: 1 branch
BranchTrueFalseTotal
if ((*recs1)->ha != (*recs2)->ha) 2 1 3
943 2 break;
944  
945 2 xdf1->dstart = xdf2->dstart = i;
946  
947 2 recs1 = xdf1->recs + xdf1->nrec - 1;
948 2 recs2 = xdf2->recs + xdf2->nrec - 1;
949 2 ▶ for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
Branch: 1 branch
BranchTrueFalseTotal
for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--) 2 0 2
950 2 ▶ if ((*recs1)->ha != (*recs2)->ha)
Branch: 1 branch
BranchTrueFalseTotal
if ((*recs1)->ha != (*recs2)->ha) 2 0 2
951 2 break;
952  
953 2 xdf1->dend = xdf1->nrec - i - 1;
954 2 xdf2->dend = xdf2->nrec - i - 1;
955  
956 2 return 0;
957 }
958  
959  
960 2 static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) {
961 4 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
962 2 xdl_cleanup_records(xdf1, xdf2) < 0) {
963  
964 0 return -1;
965 }
966  
967 2 return 0;
968 }
969  
970  
971 2 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
972 xdfenv_t *xe) {
973 long enl1, enl2;
974 xdlclassifier_t cf;
975  
976 2 enl1 = xdl_guess_lines(mf1) + 1;
977 2 enl2 = xdl_guess_lines(mf2) + 1;
978  
979 2 ▶ if (xdl_init_classifier(&cf, enl1 + enl2 + 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_init_classifier(&cf, enl1 + enl2 + 1) < 0) { 0 2 2
980  
981 0 return -1;
982 }
983  
984 2 ▶ if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) { 0 2 2
985  
986 0 xdl_free_classifier(&cf);
987 0 return -1;
988 }
989 2 ▶ if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) { 0 2 2
990  
991 0 xdl_free_ctx(&xe->xdf1);
992 0 xdl_free_classifier(&cf);
993 0 return -1;
994 }
995  
996 2 xdl_free_classifier(&cf);
997  
998 2 ▶ if (xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) { 0 2 2
999  
1000 0 xdl_free_ctx(&xe->xdf2);
1001 0 xdl_free_ctx(&xe->xdf1);
1002 0 return -1;
1003 }
1004  
1005 2 return 0;
1006 }
1007  
1008  
1009 2 void xdl_free_env(xdfenv_t *xe) {
1010 2 xdl_free_ctx(&xe->xdf2);
1011 2 xdl_free_ctx(&xe->xdf1);
1012 2 }
1013  
1014 /*
1015 * LibXDiff by Davide Libenzi ( File Differential Library )
1016 * Copyright (C) 2003 Davide Libenzi
1017 *
1018 * This library is free software; you can redistribute it and/or
1019 * modify it under the terms of the GNU Lesser General Public
1020 * License as published by the Free Software Foundation; either
1021 * version 2.1 of the License, or (at your option) any later version.
1022 *
1023 * This library is distributed in the hope that it will be useful,
1024 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1026 * Lesser General Public License for more details.
1027 *
1028 * You should have received a copy of the GNU Lesser General Public
1029 * License along with this library; if not, write to the Free Software
1030 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1031 *
1032 * Davide Libenzi <davidel@xmailserver.org>
1033 *
1034 */
1035  
1036 #include "xinclude.h"
1037  
1038  
1039  
1040 #define XDL_MAX_FUZZ 3
1041 #define XDL_MIN_SYNCLINES 4
1042  
1043  
1044  
1045 typedef struct s_recinfo {
1046 char const *ptr;
1047 long size;
1048 } recinfo_t;
1049  
1050 typedef struct s_recfile {
1051 mmfile_t *mf;
1052 long nrec;
1053 recinfo_t *recs;
1054 } recfile_t;
1055  
1056 typedef struct s_hunkinfo {
1057 long s1, s2;
1058 long c1, c2;
1059 long cmn, radd, rdel, pctx, sctx;
1060 } hunkinfo_t;
1061  
1062 typedef struct s_patchstats {
1063 long adds, dels;
1064 } patchstats_t;
1065  
1066 typedef struct s_patch {
1067 recfile_t rf;
1068 hunkinfo_t hi;
1069 long hkrec;
1070 long hklen;
1071 long flags;
1072 patchstats_t ps;
1073 int fuzzies;
1074 } patch_t;
1075  
1076  
1077  
1078  
1079 static int xdl_load_hunk_info(char const *line, long size, hunkinfo_t *hki);
1080 static int xdl_init_recfile(mmfile_t *mf, int ispatch, recfile_t *rf);
1081 static void xdl_free_recfile(recfile_t *rf);
1082 static char const *xdl_recfile_get(recfile_t *rf, long irec, long *size);
1083 static int xdl_init_patch(mmfile_t *mf, long flags, patch_t *pch);
1084 static void xdl_free_patch(patch_t *pch);
1085 static int xdl_load_hunk(patch_t *pch, long hkrec);
1086 static int xdl_first_hunk(patch_t *pch);
1087 static int xdl_next_hunk(patch_t *pch);
1088 static int xdl_line_match(patch_t *pch, const char *s, long ns, char const *m, long nm);
1089 static int xdl_hunk_match(recfile_t *rf, long irec, patch_t *pch, int mode, int fuzz);
1090 static int xdl_find_hunk(recfile_t *rf, long ibase, patch_t *pch, int mode,
1091 int fuzz, long *hkpos, int *exact);
1092 static int xdl_emit_rfile_line(recfile_t *rf, long line, xdemitcb_t *ecb);
1093 static int xdl_flush_section(recfile_t *rf, long start, long top, xdemitcb_t *ecb);
1094 static int xdl_apply_hunk(recfile_t *rf, long hkpos, patch_t *pch, int mode,
1095 long *ibase, xdemitcb_t *ecb);
1096 static int xdl_reject_hunk(recfile_t *rf, patch_t *pch, int mode,
1097 xdemitcb_t *rjecb);
1098 static int xdl_process_hunk(recfile_t *rff, patch_t *pch, long *ibase, int mode,
1099 xdemitcb_t *ecb, xdemitcb_t *rjecb);
1100  
1101  
1102  
1103  
1104 2 static int xdl_load_hunk_info(char const *line, long size, hunkinfo_t *hki) {
1105 char const *next;
1106  
1107 /*
1108 * The diff header format should be:
1109 *
1110 * @@ -OP,OC +NP,NC @@
1111 *
1112 * Unfortunately some software avoid to emit OP or/and NP in case
1113 * of not existing old or new file (it should be mitted as zero).
1114 * We need to handle both syntaxes.
1115 */
1116 2 ▶ if (memcmp(line, "@@ -", 4))
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(line, "@@ -", 4)) 0 2 2
1117 0 return -1;
1118 2 line += 4;
1119 2 size -= 4;
1120  
1121 2 ▶ if (!size || !XDL_ISDIGIT(*line))
Branch: 3 branches
BranchTrueFalseTotal
if (!size || !XDL_ISDIGIT(*line)) 2 0 2
if (!size || !XDL_ISDIGIT(*line)) 2 0 2
if (!size || !XDL_ISDIGIT(*line)) 0 2 2
1122 0 return -1;
1123 2 hki->s1 = xdl_atol(line, &next);
1124 2 size -= next - line;
1125 2 line = next;
1126 2 ▶ if (!size)
Branch: 1 branch
BranchTrueFalseTotal
if (!size) 0 2 2
1127 0 return -1;
1128 2 ▶ if (*line == ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (*line == ',') { 2 0 2
1129 2 size--, line++;
1130 2 ▶ if (!size || !XDL_ISDIGIT(*line))
Branch: 3 branches
BranchTrueFalseTotal
if (!size || !XDL_ISDIGIT(*line)) 2 0 2
if (!size || !XDL_ISDIGIT(*line)) 2 0 2
if (!size || !XDL_ISDIGIT(*line)) 0 2 2
1131 0 return -1;
1132 2 hki->c1 = xdl_atol(line, &next);
1133 2 size -= next - line;
1134 2 line = next;
1135 2 ▶ if (!size || *line != ' ')
Branch: 2 branches
BranchTrueFalseTotal
if (!size || *line != ' ') 2 0 2
if (!size || *line != ' ') 0 2 2
1136 0 return -1;
1137 2 size--, line++;
1138 0 ▶ } else if (*line == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*line == ' ') { 0 0 0
1139 0 size--, line++;
1140 0 hki->c1 = hki->s1;
1141 0 hki->s1 = 0;
1142 } else
1143 0 return -1;
1144  
1145 2 ▶ if (!size || *line != '+')
Branch: 2 branches
BranchTrueFalseTotal
if (!size || *line != '+') 2 0 2
if (!size || *line != '+') 0 2 2
1146 0 return -1;
1147 2 size--, line++;
1148 2 ▶ if (!size || !XDL_ISDIGIT(*line))
Branch: 3 branches
BranchTrueFalseTotal
if (!size || !XDL_ISDIGIT(*line)) 2 0 2
if (!size || !XDL_ISDIGIT(*line)) 2 0 2
if (!size || !XDL_ISDIGIT(*line)) 0 2 2
1149 0 return -1;
1150 2 hki->s2 = xdl_atol(line, &next);
1151 2 size -= next - line;
1152 2 line = next;
1153 2 ▶ if (!size)
Branch: 1 branch
BranchTrueFalseTotal
if (!size) 0 2 2
1154 0 return -1;
1155 2 ▶ if (*line == ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (*line == ',') { 2 0 2
1156 2 size--, line++;
1157 2 ▶ if (!size || !XDL_ISDIGIT(*line))
Branch: 3 branches
BranchTrueFalseTotal
if (!size || !XDL_ISDIGIT(*line)) 2 0 2
if (!size || !XDL_ISDIGIT(*line)) 2 0 2
if (!size || !XDL_ISDIGIT(*line)) 0 2 2
1158 0 return -1;
1159 2 hki->c2 = xdl_atol(line, &next);
1160 2 size -= next - line;
1161 2 line = next;
1162 2 ▶ if (!size || *line != ' ')
Branch: 2 branches
BranchTrueFalseTotal
if (!size || *line != ' ') 2 0 2
if (!size || *line != ' ') 0 2 2
1163 0 return -1;
1164 2 size--, line++;
1165 0 ▶ } else if (*line == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*line == ' ') { 0 0 0
1166 0 size--, line++;
1167 0 hki->c2 = hki->s2;
1168 0 hki->s2 = 0;
1169 } else
1170 0 return -1;
1171 2 ▶ if (size < 2 || memcmp(line, "@@", 2) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (size < 2 || memcmp(line, "@@", 2) != 0) 2 0 2
if (size < 2 || memcmp(line, "@@", 2) != 0) 0 2 2
1172 0 return -1;
1173  
1174 /*
1175 * We start from zero, so decrement by one unless it's the special position
1176 * '0' inside the unified diff (new or deleted file).
1177 */
1178 2 ▶ if (hki->s1 > 0 && hki->c1 > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (hki->s1 > 0 && hki->c1 > 0) 2 0 2
if (hki->s1 > 0 && hki->c1 > 0) 2 0 2
1179 2 hki->s1--;
1180 2 ▶ if (hki->s2 > 0 && hki->c2 > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (hki->s2 > 0 && hki->c2 > 0) 2 0 2
if (hki->s2 > 0 && hki->c2 > 0) 2 0 2
1181 2 hki->s2--;
1182  
1183 2 return 0;
1184 }
1185  
1186  
1187 4 static int xdl_init_recfile(mmfile_t *mf, int ispatch, recfile_t *rf) {
1188 long narec, nrec, bsize;
1189 recinfo_t *recs, *rrecs;
1190 char const *blk, *cur, *top, *eol;
1191  
1192 4 narec = xdl_guess_lines(mf);
1193 4 ▶ if (!(recs = (recinfo_t *) xdl_malloc(narec * sizeof(recinfo_t)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(recs = (recinfo_t *) xdl_malloc(narec * sizeof(recinfo_t)))) { 0 4 4
1194  
1195 0 return -1;
1196 }
1197 4 nrec = 0;
1198 4 ▶ if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) { 4 0 4
1199 4 for (top = blk + bsize;;) {
1200 19 ▶ if (cur >= top) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur >= top) { 4 15 19
1201 4 ▶ if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(cur = blk = xdl_mmfile_next(mf, &bsize))) 4 0 4
1202 4 break;
1203 0 top = blk + bsize;
1204 }
1205 15 ▶ if (nrec >= narec) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrec >= narec) { 0 15 15
1206 0 narec *= 2;
1207 0 ▶ if (!(rrecs = (recinfo_t *)
Branch: 1 branch
BranchTrueFalseTotal
if (!(rrecs = (recinfo_t *) 0 0 0
1208 0 xdl_realloc(recs, narec * sizeof(recinfo_t)))) {
1209  
1210 0 xdl_free(recs);
1211 0 return -1;
1212 }
1213 0 recs = rrecs;
1214 }
1215 15 recs[nrec].ptr = cur;
1216 15 ▶ if (!(eol = memchr(cur, '\n', top - cur)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(eol = memchr(cur, '\n', top - cur))) 1 14 15
1217 1 eol = top - 1;
1218 15 recs[nrec].size = (long) (eol - cur) + 1;
1219 15 ▶ if (ispatch && *cur == '\\' && nrec > 0 && recs[nrec - 1].size > 0 &&
Branch: 4 branches
BranchTrueFalseTotal
if (ispatch && *cur == '\\' && nrec > 0 && recs[nrec - 1].size > 0 && 11 4 15
if (ispatch && *cur == '\\' && nrec > 0 && recs[nrec - 1].size > 0 && 2 9 11
if (ispatch && *cur == '\\' && nrec > 0 && recs[nrec - 1].size > 0 && 2 0 2
if (ispatch && *cur == '\\' && nrec > 0 && recs[nrec - 1].size > 0 && 2 0 2
1220 2 ▶ recs[nrec - 1].ptr[recs[nrec - 1].size - 1] == '\n')
Branch: 1 branch
BranchTrueFalseTotal
recs[nrec - 1].ptr[recs[nrec - 1].size - 1] == '\n') 2 0 2
1221 2 recs[nrec - 1].size--;
1222 else
1223 13 nrec++;
1224 15 cur = eol + 1;
1225 }
1226 }
1227 4 rf->mf = mf;
1228 4 rf->nrec = nrec;
1229 4 rf->recs = recs;
1230  
1231 4 return 0;
1232 }
1233  
1234  
1235 4 static void xdl_free_recfile(recfile_t *rf) {
1236  
1237 4 xdl_free(rf->recs);
1238 4 }
1239  
1240  
1241 32 static char const *xdl_recfile_get(recfile_t *rf, long irec, long *size) {
1242  
1243 32 ▶ if (irec < 0 || irec >= rf->nrec)
Branch: 2 branches
BranchTrueFalseTotal
if (irec < 0 || irec >= rf->nrec) 32 0 32
if (irec < 0 || irec >= rf->nrec) 4 28 32
1244 4 return NULL;
1245 28 *size = rf->recs[irec].size;
1246  
1247 28 return rf->recs[irec].ptr;
1248 }
1249  
1250  
1251 2 static int xdl_init_patch(mmfile_t *mf, long flags, patch_t *pch) {
1252  
1253 2 ▶ if (xdl_init_recfile(mf, 1, &pch->rf) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_init_recfile(mf, 1, &pch->rf) < 0) { 0 2 2
1254  
1255 0 return -1;
1256 }
1257 2 pch->hkrec = 0;
1258 2 pch->hklen = 0;
1259 2 pch->flags = flags;
1260 2 pch->ps.adds = pch->ps.dels = 0;
1261 2 pch->fuzzies = 0;
1262  
1263 2 return 0;
1264 }
1265  
1266  
1267 2 static void xdl_free_patch(patch_t *pch) {
1268  
1269 2 xdl_free_recfile(&pch->rf);
1270 2 }
1271  
1272  
1273 4 static int xdl_load_hunk(patch_t *pch, long hkrec) {
1274 long size, i, nb;
1275 char const *line;
1276  
1277 0 for (;; hkrec++) {
1278 4 pch->hkrec = hkrec;
1279 4 ▶ if (!(line = xdl_recfile_get(&pch->rf, pch->hkrec, &size)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(line = xdl_recfile_get(&pch->rf, pch->hkrec, &size))) 2 2 4
1280 2 return 0;
1281 2 ▶ if (*line == '@')
Branch: 1 branch
BranchTrueFalseTotal
if (*line == '@') 2 0 2
1282 2 break;
1283 }
1284 2 ▶ if (xdl_load_hunk_info(line, size, &pch->hi) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_load_hunk_info(line, size, &pch->hi) < 0) { 0 2 2
1285  
1286 0 return -1;
1287 }
1288 2 pch->hi.cmn = pch->hi.radd = pch->hi.rdel = pch->hi.pctx = pch->hi.sctx = 0;
1289 2 for (i = pch->hkrec + 1, nb = 0;
1290 9 ▶ (line = xdl_recfile_get(&pch->rf, i, &size)) != NULL; i++) {
Branch: 1 branch
BranchTrueFalseTotal
(line = xdl_recfile_get(&pch->rf, i, &size)) != NULL; i++) { 7 2 9
1291 7 ▶ if (*line == '@' || *line == '\n')
Branch: 2 branches
BranchTrueFalseTotal
if (*line == '@' || *line == '\n') 7 0 7
if (*line == '@' || *line == '\n') 7 0 7
1292 break;
1293 7 ▶ if (*line == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (*line == ' ') { 2 5 7
1294 2 nb++;
1295 2 pch->hi.cmn++;
1296 5 ▶ } else if (*line == '+') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*line == '+') { 3 2 5
1297 3 ▶ if (pch->hi.radd + pch->hi.rdel == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pch->hi.radd + pch->hi.rdel == 0) 0 3 3
1298 0 pch->hi.pctx = nb;
1299 3 nb = 0;
1300 3 pch->hi.radd++;
1301 2 ▶ } else if (*line == '-') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*line == '-') { 2 0 2
1302 2 ▶ if (pch->hi.radd + pch->hi.rdel == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pch->hi.radd + pch->hi.rdel == 0) 2 0 2
1303 2 pch->hi.pctx = nb;
1304 2 nb = 0;
1305 2 pch->hi.rdel++;
1306 } else {
1307  
1308 0 return -1;
1309 }
1310 }
1311 2 pch->hi.sctx = nb;
1312 2 ▶ if (pch->hi.cmn + pch->hi.radd != pch->hi.c2 ||
Branch: 1 branch
BranchTrueFalseTotal
if (pch->hi.cmn + pch->hi.radd != pch->hi.c2 || 2 0 2
1313 2 ▶ pch->hi.cmn + pch->hi.rdel != pch->hi.c1) {
Branch: 1 branch
BranchTrueFalseTotal
pch->hi.cmn + pch->hi.rdel != pch->hi.c1) { 0 2 2
1314  
1315 0 return -1;
1316 }
1317 2 pch->hklen = i - pch->hkrec - 1;
1318  
1319 2 return 1;
1320 }
1321  
1322  
1323 2 static int xdl_first_hunk(patch_t *pch) {
1324  
1325 2 return xdl_load_hunk(pch, 0);
1326 }
1327  
1328  
1329 2 static int xdl_next_hunk(patch_t *pch) {
1330  
1331 2 return xdl_load_hunk(pch, pch->hkrec + pch->hklen + 1);
1332 }
1333  
1334  
1335 4 static int xdl_line_match(patch_t *pch, const char *s, long ns, char const *m, long nm) {
1336  
1337 7 ▶ for (; ns > 0 && (s[ns - 1] == '\r' || s[ns - 1] == '\n'); ns--);
Branch: 3 branches
BranchTrueFalseTotal
for (; ns > 0 && (s[ns - 1] == '\r' || s[ns - 1] == '\n'); ns--); 7 0 7
for (; ns > 0 && (s[ns - 1] == '\r' || s[ns - 1] == '\n'); ns--); 0 7 7
for (; ns > 0 && (s[ns - 1] == '\r' || s[ns - 1] == '\n'); ns--); 3 4 7
1338 7 ▶ for (; nm > 0 && (m[nm - 1] == '\r' || m[nm - 1] == '\n'); nm--);
Branch: 3 branches
BranchTrueFalseTotal
for (; nm > 0 && (m[nm - 1] == '\r' || m[nm - 1] == '\n'); nm--); 7 0 7
for (; nm > 0 && (m[nm - 1] == '\r' || m[nm - 1] == '\n'); nm--); 0 7 7
for (; nm > 0 && (m[nm - 1] == '\r' || m[nm - 1] == '\n'); nm--); 3 4 7
1339 4 ▶ if (pch->flags & XDL_PATCH_IGNOREBSPACE) {
Branch: 1 branch
BranchTrueFalseTotal
if (pch->flags & XDL_PATCH_IGNOREBSPACE) { 0 4 4
1340 0 ▶ for (; ns > 0 && (*s == ' ' || *s == '\t'); ns--, s++);
Branch: 3 branches
BranchTrueFalseTotal
for (; ns > 0 && (*s == ' ' || *s == '\t'); ns--, s++); 0 0 0
for (; ns > 0 && (*s == ' ' || *s == '\t'); ns--, s++); 0 0 0
for (; ns > 0 && (*s == ' ' || *s == '\t'); ns--, s++); 0 0 0
1341 0 ▶ for (; ns > 0 && (s[ns - 1] == ' ' || s[ns - 1] == '\t'); ns--);
Branch: 3 branches
BranchTrueFalseTotal
for (; ns > 0 && (s[ns - 1] == ' ' || s[ns - 1] == '\t'); ns--); 0 0 0
for (; ns > 0 && (s[ns - 1] == ' ' || s[ns - 1] == '\t'); ns--); 0 0 0
for (; ns > 0 && (s[ns - 1] == ' ' || s[ns - 1] == '\t'); ns--); 0 0 0
1342 0 ▶ for (; nm > 0 && (*m == ' ' || *m == '\t'); nm--, m++);
Branch: 3 branches
BranchTrueFalseTotal
for (; nm > 0 && (*m == ' ' || *m == '\t'); nm--, m++); 0 0 0
for (; nm > 0 && (*m == ' ' || *m == '\t'); nm--, m++); 0 0 0
for (; nm > 0 && (*m == ' ' || *m == '\t'); nm--, m++); 0 0 0
1343 0 ▶ for (; nm > 0 && (m[nm - 1] == ' ' || m[nm - 1] == '\t'); nm--);
Branch: 3 branches
BranchTrueFalseTotal
for (; nm > 0 && (m[nm - 1] == ' ' || m[nm - 1] == '\t'); nm--); 0 0 0
for (; nm > 0 && (m[nm - 1] == ' ' || m[nm - 1] == '\t'); nm--); 0 0 0
for (; nm > 0 && (m[nm - 1] == ' ' || m[nm - 1] == '\t'); nm--); 0 0 0
1344 }
1345  
1346 4 ▶ return ns == nm && memcmp(s, m, ns) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return ns == nm && memcmp(s, m, ns) == 0; 4 0 4
return ns == nm && memcmp(s, m, ns) == 0; 4 0 4
1347 }
1348  
1349  
1350 2 static int xdl_hunk_match(recfile_t *rf, long irec, patch_t *pch, int mode, int fuzz) {
1351 long i, j, z, fsize, psize, ptop, pfuzz, sfuzz, misses;
1352 char const *fline, *pline;
1353  
1354 /*
1355 * Limit fuzz to not be greater than the prefix and suffix context.
1356 */
1357 2 pfuzz = fuzz < pch->hi.pctx ? fuzz: pch->hi.pctx;
1358 2 sfuzz = fuzz < pch->hi.sctx ? fuzz: pch->hi.sctx;
1359  
1360 /*
1361 * First loop through the prefix fuzz area. In this loop we simply
1362 * note mismatching lines. We allow missing lines here, that is,
1363 * some prefix context lines are missing.
1364 */
1365 2 for (z = pfuzz, misses = 0, i = irec, j = pch->hkrec + 1,
1366 2 ptop = pch->hkrec + 1 + pch->hklen - sfuzz;
1367 2 ▶ z > 0 && i < rf->nrec && j < ptop; i++, j++, z--) {
Branch: 3 branches
BranchTrueFalseTotal
z > 0 && i < rf->nrec && j < ptop; i++, j++, z--) { 0 2 2
z > 0 && i < rf->nrec && j < ptop; i++, j++, z--) { 0 0 0
z > 0 && i < rf->nrec && j < ptop; i++, j++, z--) { 0 0 0
1368 0 ▶ if (!(pline = xdl_recfile_get(&pch->rf, j, &psize)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(pline = xdl_recfile_get(&pch->rf, j, &psize))) 0 0 0
1369 0 return 0;
1370 0 if (!(fline = xdl_recfile_get(rf, i, &fsize)) ||
1371 0 !xdl_line_match(pch, fline, fsize, pline + 1, psize - 1))
1372 0 misses++;
1373 }
1374 2 ▶ if (misses > fuzz)
Branch: 1 branch
BranchTrueFalseTotal
if (misses > fuzz) 0 2 2
1375 0 return 0;
1376  
1377 /*
1378 * Strict match loop.
1379 */
1380 6 ▶ for (; i < rf->nrec && j < ptop; i++, j++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; i < rf->nrec && j < ptop; i++, j++) { 4 2 6
for (; i < rf->nrec && j < ptop; i++, j++) { 4 0 4
1381 5 ▶ for (; j < ptop; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; j < ptop; j++) { 5 0 5
1382 5 ▶ if (!(pline = xdl_recfile_get(&pch->rf, j, &psize)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(pline = xdl_recfile_get(&pch->rf, j, &psize))) 0 5 5
1383 0 return 0;
1384 5 ▶ if (*pline == ' ' || *pline == mode)
Branch: 2 branches
BranchTrueFalseTotal
if (*pline == ' ' || *pline == mode) 3 2 5
if (*pline == ' ' || *pline == mode) 1 2 3
1385 break;
1386 }
1387 4 ▶ if (j == ptop)
Branch: 1 branch
BranchTrueFalseTotal
if (j == ptop) 0 4 4
1388 0 break;
1389 8 if (!(fline = xdl_recfile_get(rf, i, &fsize)) ||
1390 4 !xdl_line_match(pch, fline, fsize, pline + 1, psize - 1))
1391 0 return 0;
1392 }
1393 4 ▶ for (; j < ptop; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (; j < ptop; j++) 2 2 4
1394 2 ▶ if (!(pline = xdl_recfile_get(&pch->rf, j, &psize)) ||
Branch: 1 branch
BranchTrueFalseTotal
if (!(pline = xdl_recfile_get(&pch->rf, j, &psize)) || 2 0 2
1395 2 ▶ *pline == ' ' || *pline == mode)
Branch: 2 branches
BranchTrueFalseTotal
*pline == ' ' || *pline == mode) 2 0 2
*pline == ' ' || *pline == mode) 0 2 2
1396 0 return 0;
1397  
1398 /*
1399 * Finally loop through the suffix fuzz area. In this loop we simply
1400 * note mismatching lines. We allow missing lines here, that is,
1401 * some suffix context lines are missing.
1402 */
1403 2 ▶ for (z = sfuzz; z > 0 && i < rf->nrec; i++, j++, z--) {
Branch: 2 branches
BranchTrueFalseTotal
for (z = sfuzz; z > 0 && i < rf->nrec; i++, j++, z--) { 0 2 2
for (z = sfuzz; z > 0 && i < rf->nrec; i++, j++, z--) { 0 0 0
1404 0 ▶ if (!(pline = xdl_recfile_get(&pch->rf, j, &psize)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(pline = xdl_recfile_get(&pch->rf, j, &psize))) 0 0 0
1405 0 return 0;
1406 0 if (!(fline = xdl_recfile_get(rf, i, &fsize)) ||
1407 0 !xdl_line_match(pch, fline, fsize, pline + 1, psize - 1))
1408 0 misses++;
1409 }
1410  
1411 2 return misses <= fuzz;
1412 }
1413  
1414  
1415 2 static int xdl_find_hunk(recfile_t *rf, long ibase, patch_t *pch, int mode,
1416 int fuzz, long *hkpos, int *exact) {
1417 long hpos, hlen, i, j;
1418 long pos[2];
1419  
1420 2 ▶ hpos = mode == '-' ? pch->hi.s1: pch->hi.s2;
Branch: 1 branch
BranchTrueFalseTotal
hpos = mode == '-' ? pch->hi.s1: pch->hi.s2; 2 0 2
1421 2 ▶ hlen = mode == '-' ? pch->hi.cmn + pch->hi.rdel: pch->hi.cmn + pch->hi.radd;
Branch: 1 branch
BranchTrueFalseTotal
hlen = mode == '-' ? pch->hi.cmn + pch->hi.rdel: pch->hi.cmn + pch->hi.radd; 2 0 2
1422 2 ▶ if (xdl_hunk_match(rf, hpos, pch, mode, fuzz)) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_hunk_match(rf, hpos, pch, mode, fuzz)) { 2 0 2
1423 2 *hkpos = hpos;
1424 2 *exact = 1;
1425 2 return 1;
1426 }
1427 0 for (i = 1;; i++) {
1428 /*
1429 * We allow a negative starting hunk position, up to the
1430 * number of prefix context lines.
1431 */
1432 0 j = 0;
1433 0 ▶ if (hpos - i >= ibase - pch->hi.pctx)
Branch: 1 branch
BranchTrueFalseTotal
if (hpos - i >= ibase - pch->hi.pctx) 0 0 0
1434 0 pos[j++] = hpos - i;
1435 0 ▶ if (hpos + i + hlen <= rf->nrec)
Branch: 1 branch
BranchTrueFalseTotal
if (hpos + i + hlen <= rf->nrec) 0 0 0
1436 0 pos[j++] = hpos + i;
1437 0 ▶ if (!j)
Branch: 1 branch
BranchTrueFalseTotal
if (!j) 0 0 0
1438 0 break;
1439 0 ▶ for (j--; j >= 0; j--)
Branch: 1 branch
BranchTrueFalseTotal
for (j--; j >= 0; j--) 0 0 0
1440 0 ▶ if (xdl_hunk_match(rf, pos[j], pch, mode, fuzz)) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_hunk_match(rf, pos[j], pch, mode, fuzz)) { 0 0 0
1441 0 *hkpos = pos[j];
1442 0 *exact = 0;
1443 0 return 1;
1444 }
1445 }
1446  
1447 0 return 0;
1448 }
1449  
1450  
1451 2 static int xdl_emit_rfile_line(recfile_t *rf, long line, xdemitcb_t *ecb) {
1452 mmbuffer_t mb;
1453  
1454 4 if (!(mb.ptr = (char *) xdl_recfile_get(rf, line, &mb.size)) ||
1455 2 ecb->outf(ecb->priv, &mb, 1) < 0) {
1456  
1457 0 return -1;
1458 }
1459  
1460 2 return 0;
1461 }
1462  
1463  
1464 4 static int xdl_flush_section(recfile_t *rf, long start, long top, xdemitcb_t *ecb) {
1465 long i;
1466  
1467 5 ▶ for (i = start; i <= top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i <= top; i++) { 1 4 5
1468 1 ▶ if (xdl_emit_rfile_line(rf, i, ecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_rfile_line(rf, i, ecb) < 0) { 0 1 1
1469  
1470 0 return -1;
1471 }
1472 }
1473  
1474 4 return 0;
1475 }
1476  
1477  
1478 2 static int xdl_apply_hunk(recfile_t *rf, long hkpos, patch_t *pch, int mode,
1479 long *ibase, xdemitcb_t *ecb) {
1480 long j, psize, ptop;
1481 char const *pline;
1482 mmbuffer_t mb;
1483  
1484 /*
1485 * The hunk starting position (hkpos) can be negative, up to the number
1486 * of prefix context lines. Since this function only emit the core of
1487 * the hunk (the remaining lines are flushed by xdl_flush_section() calls)
1488 * we need to normalize it by adding the number of prefix context lines.
1489 * The normalized value of the starting position is then greater/equal
1490 * to zero.
1491 */
1492 2 hkpos += pch->hi.pctx;
1493 2 ▶ if (xdl_flush_section(rf, *ibase, hkpos - 1, ecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_flush_section(rf, *ibase, hkpos - 1, ecb) < 0) { 0 2 2
1494  
1495 0 return -1;
1496 }
1497 2 *ibase = hkpos;
1498 2 for (j = pch->hkrec + 1 + pch->hi.pctx,
1499 8 ▶ ptop = pch->hkrec + 1 + pch->hklen - pch->hi.sctx; j < ptop; j++) {
Branch: 1 branch
BranchTrueFalseTotal
ptop = pch->hkrec + 1 + pch->hklen - pch->hi.sctx; j < ptop; j++) { 6 2 8
1500 6 ▶ if (!(pline = xdl_recfile_get(&pch->rf, j, &psize))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(pline = xdl_recfile_get(&pch->rf, j, &psize))) { 0 6 6
1501  
1502 0 return -1;
1503 }
1504 6 ▶ if (*pline == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (*pline == ' ') { 1 5 6
1505 1 ▶ if (xdl_emit_rfile_line(rf, *ibase, ecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_rfile_line(rf, *ibase, ecb) < 0) { 0 1 1
1506  
1507 0 return -1;
1508 }
1509 1 (*ibase)++;
1510 5 ▶ } else if (*pline != mode) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*pline != mode) { 3 2 5
1511 3 mb.ptr = (char *) pline + 1;
1512 3 mb.size = psize - 1;
1513 3 ▶ if (ecb->outf(ecb->priv, &mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, &mb, 1) < 0) { 0 3 3
1514  
1515 0 return -1;
1516 }
1517 3 pch->ps.adds++;
1518 } else {
1519 2 (*ibase)++;
1520 2 pch->ps.dels++;
1521 }
1522 }
1523  
1524 2 return 0;
1525 }
1526  
1527  
1528 0 static int xdl_reject_hunk(recfile_t *rf, patch_t *pch, int mode,
1529 xdemitcb_t *rjecb) {
1530 long i, size, s1, s2, c1, c2;
1531 char const *line, *pre;
1532 mmbuffer_t mb;
1533  
1534 0 ▶ if (mode == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == '-') { 0 0 0
1535 0 s1 = pch->hi.s1;
1536 0 s2 = pch->hi.s2;
1537 0 c1 = pch->hi.c1;
1538 0 c2 = pch->hi.c2;
1539 } else {
1540 0 s1 = pch->hi.s2;
1541 0 s2 = pch->hi.s1;
1542 0 c1 = pch->hi.c2;
1543 0 c2 = pch->hi.c1;
1544 }
1545 0 s1 += pch->ps.adds - pch->ps.dels;
1546 0 ▶ if (xdl_emit_hunk_hdr(s1 + 1, c1, s2 + 1, c2, rjecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_hunk_hdr(s1 + 1, c1, s2 + 1, c2, rjecb) < 0) { 0 0 0
1547  
1548 0 return -1;
1549 }
1550 0 for (i = pch->hkrec + 1;
1551 0 ▶ (line = xdl_recfile_get(&pch->rf, i, &size)) != NULL; i++) {
Branch: 1 branch
BranchTrueFalseTotal
(line = xdl_recfile_get(&pch->rf, i, &size)) != NULL; i++) { 0 0 0
1552 0 ▶ if (*line == '@' || *line == '\n')
Branch: 2 branches
BranchTrueFalseTotal
if (*line == '@' || *line == '\n') 0 0 0
if (*line == '@' || *line == '\n') 0 0 0
1553 break;
1554 0 ▶ if (mode == '-' || *line == ' ') {
Branch: 2 branches
BranchTrueFalseTotal
if (mode == '-' || *line == ' ') { 0 0 0
if (mode == '-' || *line == ' ') { 0 0 0
1555 0 mb.ptr = (char *) line;
1556 0 mb.size = size;
1557 0 ▶ if (rjecb->outf(rjecb->priv, &mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rjecb->outf(rjecb->priv, &mb, 1) < 0) { 0 0 0
1558  
1559 0 return -1;
1560 }
1561 } else {
1562 0 ▶ pre = *line == '+' ? "-": "+";
Branch: 1 branch
BranchTrueFalseTotal
pre = *line == '+' ? "-": "+"; 0 0 0
1563 0 ▶ if (xdl_emit_diffrec(line + 1, size - 1, pre, strlen(pre),
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_diffrec(line + 1, size - 1, pre, strlen(pre), 0 0 0
1564 rjecb) < 0) {
1565  
1566 0 return -1;
1567 }
1568 }
1569 }
1570  
1571 0 return 0;
1572 }
1573  
1574  
1575 2 static int xdl_process_hunk(recfile_t *rff, patch_t *pch, long *ibase, int mode,
1576 xdemitcb_t *ecb, xdemitcb_t *rjecb) {
1577 int fuzz, exact, hlen, maxfuzz;
1578 long hkpos;
1579  
1580 2 ▶ hlen = mode == '-' ? pch->hi.cmn + pch->hi.rdel: pch->hi.cmn + pch->hi.radd;
Branch: 1 branch
BranchTrueFalseTotal
hlen = mode == '-' ? pch->hi.cmn + pch->hi.rdel: pch->hi.cmn + pch->hi.radd; 2 0 2
1581 2 maxfuzz = XDL_MAX_FUZZ;
1582 2 ▶ if (hlen - maxfuzz < XDL_MIN_SYNCLINES)
Branch: 1 branch
BranchTrueFalseTotal
if (hlen - maxfuzz < XDL_MIN_SYNCLINES) 2 0 2
1583 2 maxfuzz = hlen - XDL_MIN_SYNCLINES;
1584 2 ▶ if (maxfuzz < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (maxfuzz < 0) 2 0 2
1585 2 maxfuzz = 0;
1586 2 ▶ for (fuzz = 0; fuzz <= maxfuzz; fuzz++) {
Branch: 1 branch
BranchTrueFalseTotal
for (fuzz = 0; fuzz <= maxfuzz; fuzz++) { 2 0 2
1587 2 ▶ if (xdl_find_hunk(rff, *ibase, pch, mode, fuzz,
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_find_hunk(rff, *ibase, pch, mode, fuzz, 2 0 2
1588 &hkpos, &exact)) {
1589 2 ▶ if (xdl_apply_hunk(rff, hkpos, pch, mode,
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_apply_hunk(rff, hkpos, pch, mode, 0 2 2
1590 ibase, ecb) < 0) {
1591  
1592 0 return -1;
1593 }
1594 2 ▶ if (!exact || fuzz)
Branch: 2 branches
BranchTrueFalseTotal
if (!exact || fuzz) 2 0 2
if (!exact || fuzz) 0 2 2
1595 0 pch->fuzzies++;
1596  
1597 2 return 0;
1598 }
1599 }
1600 0 ▶ if (xdl_reject_hunk(rff, pch, mode, rjecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_reject_hunk(rff, pch, mode, rjecb) < 0) { 0 0 0
1601  
1602 0 return -1;
1603 }
1604  
1605 0 return 0;
1606 }
1607  
1608  
1609 2 int xdl_patch(mmfile_t *mf, mmfile_t *mfp, int mode, xdemitcb_t *ecb,
1610 xdemitcb_t *rjecb) {
1611 int hkres, exact;
1612 long hkpos, ibase;
1613 recfile_t rff;
1614 patch_t pch;
1615  
1616 2 ▶ if (xdl_init_recfile(mf, 0, &rff) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_init_recfile(mf, 0, &rff) < 0) { 0 2 2
1617  
1618 0 return -1;
1619 }
1620 2 ▶ if (xdl_init_patch(mfp, mode & ~XDL_PATCH_MODEMASK, &pch) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_init_patch(mfp, mode & ~XDL_PATCH_MODEMASK, &pch) < 0) { 0 2 2
1621  
1622 0 xdl_free_recfile(&rff);
1623 0 return -1;
1624 }
1625 2 mode &= XDL_PATCH_MODEMASK;
1626 2 ibase = 0;
1627 2 ▶ if ((hkres = xdl_first_hunk(&pch)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((hkres = xdl_first_hunk(&pch)) > 0) { 2 0 2
1628 do {
1629 2 ▶ if (xdl_process_hunk(&rff, &pch, &ibase, mode,
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_process_hunk(&rff, &pch, &ibase, mode, 0 2 2
1630 ecb, rjecb) < 0) {
1631 0 xdl_free_patch(&pch);
1632 0 xdl_free_recfile(&rff);
1633 0 return -1;
1634 }
1635 2 ▶ } while ((hkres = xdl_next_hunk(&pch)) > 0);
Branch: 1 branch
BranchTrueFalseTotal
} while ((hkres = xdl_next_hunk(&pch)) > 0); 0 2 2
1636 }
1637 2 ▶ if (hkres < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hkres < 0) { 0 2 2
1638  
1639 0 xdl_free_patch(&pch);
1640 0 xdl_free_recfile(&rff);
1641 0 return -1;
1642 }
1643 2 ▶ if (xdl_flush_section(&rff, ibase, rff.nrec - 1, ecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_flush_section(&rff, ibase, rff.nrec - 1, ecb) < 0) { 0 2 2
1644  
1645 0 xdl_free_patch(&pch);
1646 0 xdl_free_recfile(&rff);
1647 0 return -1;
1648 }
1649 2 xdl_free_patch(&pch);
1650 2 xdl_free_recfile(&rff);
1651  
1652 2 return pch.fuzzies;
1653 }
1654  
1655 /*
1656 * LibXDiff by Davide Libenzi ( File Differential Library )
1657 * Copyright (C) 2003 Davide Libenzi
1658 *
1659 * This library is free software; you can redistribute it and/or
1660 * modify it under the terms of the GNU Lesser General Public
1661 * License as published by the Free Software Foundation; either
1662 * version 2.1 of the License, or (at your option) any later version.
1663 *
1664 * This library is distributed in the hope that it will be useful,
1665 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1666 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1667 * Lesser General Public License for more details.
1668 *
1669 * You should have received a copy of the GNU Lesser General Public
1670 * License along with this library; if not, write to the Free Software
1671 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1672 *
1673 * Davide Libenzi <davidel@xmailserver.org>
1674 *
1675 */
1676  
1677 #include "xinclude.h"
1678  
1679  
1680 #define XDL_MERGE3_BLKSIZE (1024 * 8)
1681 #define XDL_MERGE3_CTXLEN 3
1682  
1683  
1684  
1685 0 int xdl_merge3(mmfile_t *mmfo, mmfile_t *mmf1, mmfile_t *mmf2, xdemitcb_t *ecb,
1686 xdemitcb_t *rjecb) {
1687 xpparam_t xpp;
1688 xdemitconf_t xecfg;
1689 xdemitcb_t xecb;
1690 mmfile_t mmfp;
1691  
1692 0 ▶ if (xdl_init_mmfile(&mmfp, XDL_MERGE3_BLKSIZE, XDL_MMF_ATOMIC) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_init_mmfile(&mmfp, XDL_MERGE3_BLKSIZE, XDL_MMF_ATOMIC) < 0) { 0 0 0
1693  
1694 0 return -1;
1695 }
1696  
1697 0 xpp.flags = 0;
1698  
1699 0 xecfg.ctxlen = XDL_MERGE3_CTXLEN;
1700  
1701 0 xecb.priv = &mmfp;
1702 0 xecb.outf = xdl_mmfile_outf;
1703  
1704 0 ▶ if (xdl_diff(mmfo, mmf2, &xpp, &xecfg, &xecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_diff(mmfo, mmf2, &xpp, &xecfg, &xecb) < 0) { 0 0 0
1705  
1706 0 xdl_free_mmfile(&mmfp);
1707 0 return -1;
1708 }
1709  
1710 0 ▶ if (xdl_patch(mmf1, &mmfp, XDL_PATCH_NORMAL, ecb, rjecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_patch(mmf1, &mmfp, XDL_PATCH_NORMAL, ecb, rjecb) < 0) { 0 0 0
1711  
1712 0 xdl_free_mmfile(&mmfp);
1713 0 return -1;
1714 }
1715  
1716 0 xdl_free_mmfile(&mmfp);
1717  
1718 0 return 0;
1719 }
1720  
1721 /*
1722 * LibXDiff by Davide Libenzi ( File Differential Library )
1723 * Copyright (C) 2003 Davide Libenzi
1724 *
1725 * This library is free software; you can redistribute it and/or
1726 * modify it under the terms of the GNU Lesser General Public
1727 * License as published by the Free Software Foundation; either
1728 * version 2.1 of the License, or (at your option) any later version.
1729 *
1730 * This library is distributed in the hope that it will be useful,
1731 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1732 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1733 * Lesser General Public License for more details.
1734 *
1735 * You should have received a copy of the GNU Lesser General Public
1736 * License along with this library; if not, write to the Free Software
1737 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1738 *
1739 * Davide Libenzi <davidel@xmailserver.org>
1740 *
1741 */
1742  
1743 #include "xinclude.h"
1744  
1745  
1746 7 static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) {
1747  
1748 7 *rec = xdf->recs[ri]->ptr;
1749  
1750 7 return xdf->recs[ri]->size;
1751 }
1752  
1753  
1754 7 static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) {
1755 7 long size, psize = strlen(pre);
1756 char const *rec;
1757  
1758 7 size = xdl_get_rec(xdf, ri, &rec);
1759 7 ▶ if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) { 0 7 7
1760  
1761 0 return -1;
1762 }
1763  
1764 7 return 0;
1765 }
1766  
1767  
1768 /*
1769 * Starting at the passed change atom, find the latest change atom to be included
1770 * inside the differential hunk according to the specified configuration.
1771 */
1772 2 static xdchange_t *xdl_get_hunk(xdchange_t *xscr, xdemitconf_t const *xecfg) {
1773 xdchange_t *xch, *xchp;
1774  
1775 3 ▶ for (xchp = xscr, xch = xscr->next; xch; xchp = xch, xch = xch->next)
Branch: 1 branch
BranchTrueFalseTotal
for (xchp = xscr, xch = xscr->next; xch; xchp = xch, xch = xch->next) 1 2 3
1776 1 ▶ if (xch->i1 - (xchp->i1 + xchp->chg1) > 2 * xecfg->ctxlen)
Branch: 1 branch
BranchTrueFalseTotal
if (xch->i1 - (xchp->i1 + xchp->chg1) > 2 * xecfg->ctxlen) 0 1 1
1777 0 break;
1778  
1779 2 return xchp;
1780 }
1781  
1782  
1783 2 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
1784 xdemitconf_t const *xecfg) {
1785 long s1, s2, e1, e2, lctx;
1786 xdchange_t *xch, *xche;
1787  
1788 4 ▶ for (xch = xche = xscr; xch; xch = xche->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (xch = xche = xscr; xch; xch = xche->next) { 2 2 4
1789 2 xche = xdl_get_hunk(xch, xecfg);
1790  
1791 2 s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
1792 2 s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
1793  
1794 2 lctx = xecfg->ctxlen;
1795 2 lctx = XDL_MIN(lctx, xe->xdf1.nrec - (xche->i1 + xche->chg1));
1796 2 lctx = XDL_MIN(lctx, xe->xdf2.nrec - (xche->i2 + xche->chg2));
1797  
1798 2 e1 = xche->i1 + xche->chg1 + lctx;
1799 2 e2 = xche->i2 + xche->chg2 + lctx;
1800  
1801 /*
1802 * Emit current hunk header.
1803 */
1804 2 ▶ if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2, ecb) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2, ecb) < 0) 0 2 2
1805 0 return -1;
1806  
1807 /*
1808 * Emit pre-context.
1809 */
1810 3 ▶ for (; s1 < xch->i1; s1++)
Branch: 1 branch
BranchTrueFalseTotal
for (; s1 < xch->i1; s1++) 1 2 3
1811 1 ▶ if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0) 0 1 1
1812 0 return -1;
1813  
1814 2 for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) {
1815 /*
1816 * Merge previous with current change atom.
1817 */
1818 4 ▶ for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++)
Branch: 2 branches
BranchTrueFalseTotal
for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++) 1 3 4
for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++) 1 0 1
1819 1 ▶ if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0) 0 1 1
1820 0 return -1;
1821  
1822 /*
1823 * Removes lines from the first file.
1824 */
1825 5 ▶ for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++)
Branch: 1 branch
BranchTrueFalseTotal
for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++) 2 3 5
1826 2 ▶ if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0) 0 2 2
1827 0 return -1;
1828  
1829 /*
1830 * Adds lines from the second file.
1831 */
1832 6 ▶ for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++)
Branch: 1 branch
BranchTrueFalseTotal
for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++) 3 3 6
1833 3 ▶ if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0) 0 3 3
1834 0 return -1;
1835  
1836 3 ▶ if (xch == xche)
Branch: 1 branch
BranchTrueFalseTotal
if (xch == xche) 2 1 3
1837 2 break;
1838 1 s1 = xch->i1 + xch->chg1;
1839 1 s2 = xch->i2 + xch->chg2;
1840 }
1841  
1842 /*
1843 * Emit post-context.
1844 */
1845 2 ▶ for (s1 = xche->i1 + xche->chg1; s1 < e1; s1++)
Branch: 1 branch
BranchTrueFalseTotal
for (s1 = xche->i1 + xche->chg1; s1 < e1; s1++) 0 2 2
1846 0 ▶ if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0) 0 0 0
1847 0 return -1;
1848 }
1849  
1850 2 return 0;
1851 }
1852  
1853 /*
1854 * LibXDiff by Davide Libenzi ( File Differential Library )
1855 * Copyright (C) 2003 Davide Libenzi
1856 *
1857 * This library is free software; you can redistribute it and/or
1858 * modify it under the terms of the GNU Lesser General Public
1859 * License as published by the Free Software Foundation; either
1860 * version 2.1 of the License, or (at your option) any later version.
1861 *
1862 * This library is distributed in the hope that it will be useful,
1863 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1864 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1865 * Lesser General Public License for more details.
1866 *
1867 * You should have received a copy of the GNU Lesser General Public
1868 * License along with this library; if not, write to the Free Software
1869 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1870 *
1871 * Davide Libenzi <davidel@xmailserver.org>
1872 *
1873 */
1874  
1875 #include "xinclude.h"
1876  
1877  
1878  
1879 #if !defined(HAVE_MEMCHR)
1880  
1881 0 void *memchr(void const *p, int c, long n) {
1882 0 char const *pc = p;
1883  
1884 0 ▶ for (; n; n--, pc++)
Branch: 1 branch
BranchTrueFalseTotal
for (; n; n--, pc++) 0 0 0
1885 0 ▶ if (*pc == (char) c)
Branch: 1 branch
BranchTrueFalseTotal
if (*pc == (char) c) 0 0 0
1886 0 return pc;
1887 0 return NULL;
1888 }
1889  
1890 #endif /* #if !defined(HAVE_MEMCHR) */
1891  
1892  
1893 #if !defined(HAVE_MEMCMP)
1894  
1895 0 int memcmp(void const *p1, void const *p2, long n) {
1896 0 char const *pc1 = p1, *pc2 = p2;
1897  
1898 0 ▶ for (; n; n--, pc1++, pc2++)
Branch: 1 branch
BranchTrueFalseTotal
for (; n; n--, pc1++, pc2++) 0 0 0
1899 0 ▶ if (*pc1 != *pc2)
Branch: 1 branch
BranchTrueFalseTotal
if (*pc1 != *pc2) 0 0 0
1900 0 return *pc1 - *pc2;
1901 0 return 0;
1902 }
1903  
1904 #endif /* #if !defined(HAVE_MEMCMP) */
1905  
1906  
1907 #if !defined(HAVE_MEMCPY)
1908  
1909 0 void *memcpy(void *d, void const *s, long n) {
1910 0 char *dc = d;
1911 0 char const *sc = s;
1912  
1913 0 ▶ for (; n; n--, dc++, sc++)
Branch: 1 branch
BranchTrueFalseTotal
for (; n; n--, dc++, sc++) 0 0 0
1914 0 *dc = *sc;
1915 0 return d;
1916 }
1917  
1918 #endif /* #if !defined(HAVE_MEMCPY) */
1919  
1920  
1921 #if !defined(HAVE_MEMSET)
1922  
1923 0 void *memset(void *d, int c, long n) {
1924 0 char *dc = d;
1925  
1926 0 ▶ for (; n; n--, dc++)
Branch: 1 branch
BranchTrueFalseTotal
for (; n; n--, dc++) 0 0 0
1927 0 *dc = (char) c;
1928 0 return d;
1929 }
1930  
1931 #endif /* #if !defined(HAVE_MEMSET) */
1932  
1933  
1934 #if !defined(HAVE_STRLEN)
1935  
1936 0 long strlen(char const *s) {
1937 char const *tmp;
1938  
1939 0 ▶ for (tmp = s; *s; s++);
Branch: 1 branch
BranchTrueFalseTotal
for (tmp = s; *s; s++); 0 0 0
1940 0 return (long) (s - tmp);
1941 }
1942  
1943 #endif /* #if !defined(HAVE_STRLEN) */
1944  
1945 /*
1946 * LibXDiff by Davide Libenzi ( File Differential Library )
1947 * Copyright (C) 2003 Davide Libenzi
1948 *
1949 * This library is free software; you can redistribute it and/or
1950 * modify it under the terms of the GNU Lesser General Public
1951 * License as published by the Free Software Foundation; either
1952 * version 2.1 of the License, or (at your option) any later version.
1953 *
1954 * This library is distributed in the hope that it will be useful,
1955 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1956 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1957 * Lesser General Public License for more details.
1958 *
1959 * You should have received a copy of the GNU Lesser General Public
1960 * License along with this library; if not, write to the Free Software
1961 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1962 *
1963 * Davide Libenzi <davidel@xmailserver.org>
1964 *
1965 */
1966  
1967 #include "xinclude.h"
1968  
1969  
1970  
1971 #define XDL_GUESS_NLINES 256
1972  
1973  
1974  
1975  
1976 6 long xdl_bogosqrt(long n) {
1977 long i;
1978  
1979 /*
1980 * Classical integer square root approximation using shifts.
1981 */
1982 14 ▶ for (i = 1; n > 0; n >>= 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; n > 0; n >>= 2) 8 6 14
1983 8 i <<= 1;
1984  
1985 6 return i;
1986 }
1987  
1988  
1989 7 int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
1990 xdemitcb_t *ecb) {
1991 7 int i = 2;
1992 mmbuffer_t mb[3];
1993  
1994 7 mb[0].ptr = (char *) pre;
1995 7 mb[0].size = psize;
1996 7 mb[1].ptr = (char *) rec;
1997 7 mb[1].size = size;
1998 7 ▶ if (size > 0 && rec[size - 1] != '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (size > 0 && rec[size - 1] != '\n') { 7 0 7
if (size > 0 && rec[size - 1] != '\n') { 2 5 7
1999 2 mb[2].ptr = (char *) "\n\\ No newline at end of file\n";
2000 2 mb[2].size = strlen(mb[2].ptr);
2001 2 i++;
2002 }
2003 7 ▶ if (ecb->outf(ecb->priv, mb, i) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, i) < 0) { 0 7 7
2004  
2005 0 return -1;
2006 }
2007  
2008 7 return 0;
2009 }
2010  
2011  
2012 24 int xdl_init_mmfile(mmfile_t *mmf, long bsize, unsigned long flags) {
2013  
2014 24 mmf->flags = flags;
2015 24 mmf->head = mmf->tail = NULL;
2016 24 mmf->bsize = bsize;
2017 24 mmf->fsize = 0;
2018 24 mmf->rcur = mmf->wcur = NULL;
2019 24 mmf->rpos = 0;
2020  
2021 24 return 0;
2022 }
2023  
2024  
2025 32 void xdl_free_mmfile(mmfile_t *mmf) {
2026 mmblock_t *cur, *tmp;
2027  
2028 52 ▶ for (cur = mmf->head; (tmp = cur) != NULL;) {
Branch: 1 branch
BranchTrueFalseTotal
for (cur = mmf->head; (tmp = cur) != NULL;) { 20 32 52
2029 20 cur = cur->next;
2030 20 xdl_free(tmp);
2031 }
2032 32 }
2033  
2034  
2035 2 int xdl_mmfile_iscompact(mmfile_t *mmf) {
2036  
2037 2 return mmf->head == mmf->tail;
2038 }
2039  
2040  
2041 8 int xdl_seek_mmfile(mmfile_t *mmf, long off) {
2042 long bsize;
2043  
2044 8 ▶ if (xdl_mmfile_first(mmf, &bsize)) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_mmfile_first(mmf, &bsize)) { 6 2 8
2045 do {
2046 6 ▶ if (off < bsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (off < bsize) { 6 0 6
2047 6 mmf->rpos = off;
2048 6 return 0;
2049 }
2050 0 off -= bsize;
2051 0 ▶ } while (xdl_mmfile_next(mmf, &bsize));
Branch: 1 branch
BranchTrueFalseTotal
} while (xdl_mmfile_next(mmf, &bsize)); 0 0 0
2052 }
2053  
2054 2 return -1;
2055 }
2056  
2057  
2058 8 long xdl_read_mmfile(mmfile_t *mmf, void *data, long size) {
2059 long rsize, csize;
2060 8 char *ptr = data;
2061 mmblock_t *rcur;
2062  
2063 14 ▶ for (rsize = 0, rcur = mmf->rcur; rcur && rsize < size;) {
Branch: 2 branches
BranchTrueFalseTotal
for (rsize = 0, rcur = mmf->rcur; rcur && rsize < size;) { 12 2 14
for (rsize = 0, rcur = mmf->rcur; rcur && rsize < size;) { 6 6 12
2064 6 ▶ if (mmf->rpos >= rcur->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (mmf->rpos >= rcur->size) { 0 6 6
2065 0 ▶ if (!(mmf->rcur = rcur = rcur->next))
Branch: 1 branch
BranchTrueFalseTotal
if (!(mmf->rcur = rcur = rcur->next)) 0 0 0
2066 0 break;
2067 0 mmf->rpos = 0;
2068 }
2069 6 csize = XDL_MIN(size - rsize, rcur->size - mmf->rpos);
2070 6 memcpy(ptr, rcur->ptr + mmf->rpos, csize);
2071 6 rsize += csize;
2072 6 ptr += csize;
2073 6 mmf->rpos += csize;
2074 }
2075  
2076 8 return rsize;
2077 }
2078  
2079  
2080 12 long xdl_write_mmfile(mmfile_t *mmf, void const *data, long size) {
2081 long wsize, bsize, csize;
2082 mmblock_t *wcur;
2083  
2084 24 ▶ for (wsize = 0; wsize < size;) {
Branch: 1 branch
BranchTrueFalseTotal
for (wsize = 0; wsize < size;) { 12 12 24
2085 12 wcur = mmf->wcur;
2086 12 ▶ if (wcur && (wcur->flags & XDL_MMB_READONLY))
Branch: 2 branches
BranchTrueFalseTotal
if (wcur && (wcur->flags & XDL_MMB_READONLY)) 0 12 12
if (wcur && (wcur->flags & XDL_MMB_READONLY)) 0 0 0
2087 0 return wsize;
2088 12 ▶ if (!wcur || wcur->size == wcur->bsize ||
Branch: 2 branches
BranchTrueFalseTotal
if (!wcur || wcur->size == wcur->bsize || 0 12 12
if (!wcur || wcur->size == wcur->bsize || 0 0 0
2089 0 ▶ (mmf->flags & XDL_MMF_ATOMIC && wcur->size + size > wcur->bsize)) {
Branch: 2 branches
BranchTrueFalseTotal
(mmf->flags & XDL_MMF_ATOMIC && wcur->size + size > wcur->bsize)) { 0 0 0
(mmf->flags & XDL_MMF_ATOMIC && wcur->size + size > wcur->bsize)) { 0 0 0
2090 12 bsize = XDL_MAX(mmf->bsize, size);
2091 12 ▶ if (!(wcur = (mmblock_t *) xdl_malloc(sizeof(mmblock_t) + bsize))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(wcur = (mmblock_t *) xdl_malloc(sizeof(mmblock_t) + bsize))) { 0 12 12
2092  
2093 0 return wsize;
2094 }
2095 12 wcur->flags = 0;
2096 12 wcur->ptr = (char *) wcur + sizeof(mmblock_t);
2097 12 wcur->size = 0;
2098 12 wcur->bsize = bsize;
2099 12 wcur->next = NULL;
2100 12 ▶ if (!mmf->head)
Branch: 1 branch
BranchTrueFalseTotal
if (!mmf->head) 12 0 12
2101 12 mmf->head = wcur;
2102 12 ▶ if (mmf->tail)
Branch: 1 branch
BranchTrueFalseTotal
if (mmf->tail) 0 12 12
2103 0 mmf->tail->next = wcur;
2104 12 mmf->tail = wcur;
2105 12 mmf->wcur = wcur;
2106 }
2107 12 csize = XDL_MIN(size - wsize, wcur->bsize - wcur->size);
2108 12 memcpy(wcur->ptr + wcur->size, (char const *) data + wsize, csize);
2109 12 wsize += csize;
2110 12 wcur->size += csize;
2111 12 mmf->fsize += csize;
2112 }
2113  
2114 12 return size;
2115 }
2116  
2117  
2118 17 long xdl_writem_mmfile(mmfile_t *mmf, mmbuffer_t *mb, int nbuf) {
2119 int i;
2120 long size;
2121 char *data;
2122  
2123 44 ▶ for (i = 0, size = 0; i < nbuf; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, size = 0; i < nbuf; i++) 27 17 44
2124 27 size += mb[i].size;
2125 17 ▶ if (!(data = (char *) xdl_mmfile_writeallocate(mmf, size)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(data = (char *) xdl_mmfile_writeallocate(mmf, size))) 0 17 17
2126 0 return -1;
2127 44 ▶ for (i = 0; i < nbuf; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nbuf; i++) { 27 17 44
2128 27 memcpy(data, mb[i].ptr, mb[i].size);
2129 27 data += mb[i].size;
2130 }
2131  
2132 17 return size;
2133 }
2134  
2135  
2136 21 void *xdl_mmfile_writeallocate(mmfile_t *mmf, long size) {
2137 long bsize;
2138 mmblock_t *wcur;
2139 char *blk;
2140  
2141 21 ▶ if (!(wcur = mmf->wcur) || wcur->size + size > wcur->bsize) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(wcur = mmf->wcur) || wcur->size + size > wcur->bsize) { 11 10 21
if (!(wcur = mmf->wcur) || wcur->size + size > wcur->bsize) { 0 11 11
2142 10 bsize = XDL_MAX(mmf->bsize, size);
2143 10 ▶ if (!(wcur = (mmblock_t *) xdl_malloc(sizeof(mmblock_t) + bsize))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(wcur = (mmblock_t *) xdl_malloc(sizeof(mmblock_t) + bsize))) { 0 10 10
2144  
2145 0 return NULL;
2146 }
2147 10 wcur->flags = 0;
2148 10 wcur->ptr = (char *) wcur + sizeof(mmblock_t);
2149 10 wcur->size = 0;
2150 10 wcur->bsize = bsize;
2151 10 wcur->next = NULL;
2152 10 ▶ if (!mmf->head)
Branch: 1 branch
BranchTrueFalseTotal
if (!mmf->head) 10 0 10
2153 10 mmf->head = wcur;
2154 10 ▶ if (mmf->tail)
Branch: 1 branch
BranchTrueFalseTotal
if (mmf->tail) 0 10 10
2155 0 mmf->tail->next = wcur;
2156 10 mmf->tail = wcur;
2157 10 mmf->wcur = wcur;
2158 }
2159  
2160 21 blk = wcur->ptr + wcur->size;
2161 21 wcur->size += size;
2162 21 mmf->fsize += size;
2163  
2164 21 return blk;
2165 }
2166  
2167  
2168 0 long xdl_mmfile_ptradd(mmfile_t *mmf, char *ptr, long size, unsigned long flags) {
2169 mmblock_t *wcur;
2170  
2171 0 ▶ if (!(wcur = (mmblock_t *) xdl_malloc(sizeof(mmblock_t)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(wcur = (mmblock_t *) xdl_malloc(sizeof(mmblock_t)))) { 0 0 0
2172  
2173 0 return -1;
2174 }
2175 0 wcur->flags = flags;
2176 0 wcur->ptr = ptr;
2177 0 wcur->size = wcur->bsize = size;
2178 0 wcur->next = NULL;
2179 0 ▶ if (!mmf->head)
Branch: 1 branch
BranchTrueFalseTotal
if (!mmf->head) 0 0 0
2180 0 mmf->head = wcur;
2181 0 ▶ if (mmf->tail)
Branch: 1 branch
BranchTrueFalseTotal
if (mmf->tail) 0 0 0
2182 0 mmf->tail->next = wcur;
2183 0 mmf->tail = wcur;
2184 0 mmf->wcur = wcur;
2185  
2186 0 mmf->fsize += size;
2187  
2188 0 return size;
2189 }
2190  
2191  
2192 0 long xdl_copy_mmfile(mmfile_t *mmf, long size, xdemitcb_t *ecb) {
2193 long rsize, csize;
2194 mmblock_t *rcur;
2195 mmbuffer_t mb;
2196  
2197 0 ▶ for (rsize = 0, rcur = mmf->rcur; rcur && rsize < size;) {
Branch: 2 branches
BranchTrueFalseTotal
for (rsize = 0, rcur = mmf->rcur; rcur && rsize < size;) { 0 0 0
for (rsize = 0, rcur = mmf->rcur; rcur && rsize < size;) { 0 0 0
2198 0 ▶ if (mmf->rpos >= rcur->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (mmf->rpos >= rcur->size) { 0 0 0
2199 0 ▶ if (!(mmf->rcur = rcur = rcur->next))
Branch: 1 branch
BranchTrueFalseTotal
if (!(mmf->rcur = rcur = rcur->next)) 0 0 0
2200 0 break;
2201 0 mmf->rpos = 0;
2202 }
2203 0 csize = XDL_MIN(size - rsize, rcur->size - mmf->rpos);
2204 0 mb.ptr = rcur->ptr + mmf->rpos;
2205 0 mb.size = csize;
2206 0 ▶ if (ecb->outf(ecb->priv, &mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, &mb, 1) < 0) { 0 0 0
2207  
2208 0 return rsize;
2209 }
2210 0 rsize += csize;
2211 0 mmf->rpos += csize;
2212 }
2213  
2214 0 return rsize;
2215 }
2216  
2217  
2218 32 void *xdl_mmfile_first(mmfile_t *mmf, long *size) {
2219  
2220 32 ▶ if (!(mmf->rcur = mmf->head))
Branch: 1 branch
BranchTrueFalseTotal
if (!(mmf->rcur = mmf->head)) 2 30 32
2221 2 return NULL;
2222  
2223 30 *size = mmf->rcur->size;
2224  
2225 30 return mmf->rcur->ptr;
2226 }
2227  
2228  
2229 22 void *xdl_mmfile_next(mmfile_t *mmf, long *size) {
2230  
2231 22 ▶ if (!mmf->rcur || !(mmf->rcur = mmf->rcur->next))
Branch: 2 branches
BranchTrueFalseTotal
if (!mmf->rcur || !(mmf->rcur = mmf->rcur->next)) 22 0 22
if (!mmf->rcur || !(mmf->rcur = mmf->rcur->next)) 22 0 22
2232 22 return NULL;
2233  
2234 0 *size = mmf->rcur->size;
2235  
2236 0 return mmf->rcur->ptr;
2237 }
2238  
2239  
2240 21 long xdl_mmfile_size(mmfile_t *mmf) {
2241  
2242 21 return mmf->fsize;
2243 }
2244  
2245  
2246 0 int xdl_mmfile_cmp(mmfile_t *mmf1, mmfile_t *mmf2) {
2247 int cres;
2248 long size, bsize1, bsize2, size1, size2;
2249 char const *blk1, *cur1, *top1;
2250 char const *blk2, *cur2, *top2;
2251  
2252 0 ▶ if ((cur1 = blk1 = xdl_mmfile_first(mmf1, &bsize1)) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((cur1 = blk1 = xdl_mmfile_first(mmf1, &bsize1)) != NULL) 0 0 0
2253 0 top1 = blk1 + bsize1;
2254 0 ▶ if ((cur2 = blk2 = xdl_mmfile_first(mmf2, &bsize2)) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((cur2 = blk2 = xdl_mmfile_first(mmf2, &bsize2)) != NULL) 0 0 0
2255 0 top2 = blk2 + bsize2;
2256 0 ▶ if (!cur1) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cur1) { 0 0 0
2257 0 ▶ if (!cur2 || xdl_mmfile_size(mmf2) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!cur2 || xdl_mmfile_size(mmf2) == 0) 0 0 0
if (!cur2 || xdl_mmfile_size(mmf2) == 0) 0 0 0
2258 0 return 0;
2259 0 return -*cur2;
2260 0 ▶ } else if (!cur2)
Branch: 1 branch
BranchTrueFalseTotal
} else if (!cur2) 0 0 0
2261 0 ▶ return xdl_mmfile_size(mmf1) ? *cur1: 0;
Branch: 1 branch
BranchTrueFalseTotal
return xdl_mmfile_size(mmf1) ? *cur1: 0; 0 0 0
2262 for (;;) {
2263 0 ▶ if (cur1 >= top1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur1 >= top1) { 0 0 0
2264 0 ▶ if ((cur1 = blk1 = xdl_mmfile_next(mmf1, &bsize1)) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((cur1 = blk1 = xdl_mmfile_next(mmf1, &bsize1)) != NULL) 0 0 0
2265 0 top1 = blk1 + bsize1;
2266 }
2267 0 ▶ if (cur2 >= top2) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur2 >= top2) { 0 0 0
2268 0 ▶ if ((cur2 = blk2 = xdl_mmfile_next(mmf2, &bsize2)) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((cur2 = blk2 = xdl_mmfile_next(mmf2, &bsize2)) != NULL) 0 0 0
2269 0 top2 = blk2 + bsize2;
2270 }
2271 0 ▶ if (!cur1) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cur1) { 0 0 0
2272 0 ▶ if (!cur2)
Branch: 1 branch
BranchTrueFalseTotal
if (!cur2) 0 0 0
2273 0 break;
2274 0 return -*cur2;
2275 0 ▶ } else if (!cur2)
Branch: 1 branch
BranchTrueFalseTotal
} else if (!cur2) 0 0 0
2276 0 return *cur1;
2277 0 size1 = top1 - cur1;
2278 0 size2 = top2 - cur2;
2279 0 size = XDL_MIN(size1, size2);
2280 0 ▶ if ((cres = memcmp(cur1, cur2, size)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((cres = memcmp(cur1, cur2, size)) != 0) 0 0 0
2281 0 return cres;
2282 0 cur1 += size;
2283 0 cur2 += size;
2284 }
2285  
2286 0 return 0;
2287 }
2288  
2289  
2290 4 int xdl_mmfile_compact(mmfile_t *mmfo, mmfile_t *mmfc, long bsize, unsigned long flags) {
2291 4 long fsize = xdl_mmfile_size(mmfo), size;
2292 char *data;
2293 char const *blk;
2294  
2295 4 ▶ if (xdl_init_mmfile(mmfc, bsize, flags) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_init_mmfile(mmfc, bsize, flags) < 0) { 0 4 4
2296  
2297 0 return -1;
2298 }
2299 4 ▶ if (!(data = (char *) xdl_mmfile_writeallocate(mmfc, fsize))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(data = (char *) xdl_mmfile_writeallocate(mmfc, fsize))) { 0 4 4
2300  
2301 0 xdl_free_mmfile(mmfc);
2302 0 return -1;
2303 }
2304 4 ▶ if ((blk = (char const *) xdl_mmfile_first(mmfo, &size)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((blk = (char const *) xdl_mmfile_first(mmfo, &size)) != NULL) { 4 0 4
2305 do {
2306 4 memcpy(data, blk, size);
2307 4 data += size;
2308 4 ▶ } while ((blk = (char const *) xdl_mmfile_next(mmfo, &size)) != NULL);
Branch: 1 branch
BranchTrueFalseTotal
} while ((blk = (char const *) xdl_mmfile_next(mmfo, &size)) != NULL); 0 4 4
2309 }
2310  
2311 4 return 0;
2312 }
2313  
2314  
2315 0 int xdl_mmfile_outf(void *priv, mmbuffer_t *mb, int nbuf) {
2316 0 mmfile_t *mmf = priv;
2317  
2318 0 ▶ if (xdl_writem_mmfile(mmf, mb, nbuf) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_writem_mmfile(mmf, mb, nbuf) < 0) { 0 0 0
2319  
2320 0 return -1;
2321 }
2322  
2323 0 return 0;
2324 }
2325  
2326  
2327 7 int xdl_cha_init(chastore_t *cha, long isize, long icount) {
2328  
2329 7 cha->head = cha->tail = NULL;
2330 7 cha->isize = isize;
2331 7 cha->nsize = icount * isize;
2332 7 cha->ancur = cha->sncur = NULL;
2333 7 cha->scurr = 0;
2334  
2335 7 return 0;
2336 }
2337  
2338  
2339 7 void xdl_cha_free(chastore_t *cha) {
2340 chanode_t *cur, *tmp;
2341  
2342 18 ▶ for (cur = cha->head; (tmp = cur) != NULL;) {
Branch: 1 branch
BranchTrueFalseTotal
for (cur = cha->head; (tmp = cur) != NULL;) { 11 7 18
2343 11 cur = cur->next;
2344 11 xdl_free(tmp);
2345 }
2346 7 }
2347  
2348  
2349 80 void *xdl_cha_alloc(chastore_t *cha) {
2350 chanode_t *ancur;
2351 void *data;
2352  
2353 80 ▶ if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) { 73 7 80
if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) { 4 69 73
2354 11 ▶ if (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) { 0 11 11
2355  
2356 0 return NULL;
2357 }
2358 11 ancur->icurr = 0;
2359 11 ancur->next = NULL;
2360 11 ▶ if (cha->tail)
Branch: 1 branch
BranchTrueFalseTotal
if (cha->tail) 4 7 11
2361 4 cha->tail->next = ancur;
2362 11 ▶ if (!cha->head)
Branch: 1 branch
BranchTrueFalseTotal
if (!cha->head) 7 4 11
2363 7 cha->head = ancur;
2364 11 cha->tail = ancur;
2365 11 cha->ancur = ancur;
2366 }
2367  
2368 80 data = (char *) ancur + sizeof(chanode_t) + ancur->icurr;
2369 80 ancur->icurr += cha->isize;
2370  
2371 80 return data;
2372 }
2373  
2374  
2375 0 void *xdl_cha_first(chastore_t *cha) {
2376 chanode_t *sncur;
2377  
2378 0 ▶ if (!(cha->sncur = sncur = cha->head))
Branch: 1 branch
BranchTrueFalseTotal
if (!(cha->sncur = sncur = cha->head)) 0 0 0
2379 0 return NULL;
2380  
2381 0 cha->scurr = 0;
2382  
2383 0 return (char *) sncur + sizeof(chanode_t) + cha->scurr;
2384 }
2385  
2386  
2387 0 void *xdl_cha_next(chastore_t *cha) {
2388 chanode_t *sncur;
2389  
2390 0 ▶ if (!(sncur = cha->sncur))
Branch: 1 branch
BranchTrueFalseTotal
if (!(sncur = cha->sncur)) 0 0 0
2391 0 return NULL;
2392 0 cha->scurr += cha->isize;
2393 0 ▶ if (cha->scurr == sncur->icurr) {
Branch: 1 branch
BranchTrueFalseTotal
if (cha->scurr == sncur->icurr) { 0 0 0
2394 0 ▶ if (!(sncur = cha->sncur = sncur->next))
Branch: 1 branch
BranchTrueFalseTotal
if (!(sncur = cha->sncur = sncur->next)) 0 0 0
2395 0 return NULL;
2396 0 cha->scurr = 0;
2397 }
2398  
2399 0 return (char *) sncur + sizeof(chanode_t) + cha->scurr;
2400 }
2401  
2402  
2403 8 long xdl_guess_lines(mmfile_t *mf) {
2404 8 long nl = 0, size, tsize = 0;
2405 char const *data, *cur, *top;
2406  
2407 8 ▶ if ((cur = data = xdl_mmfile_first(mf, &size)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cur = data = xdl_mmfile_first(mf, &size)) != NULL) { 8 0 8
2408 32 ▶ for (top = data + size; nl < XDL_GUESS_NLINES;) {
Branch: 1 branch
BranchTrueFalseTotal
for (top = data + size; nl < XDL_GUESS_NLINES;) { 32 0 32
2409 32 ▶ if (cur >= top) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur >= top) { 8 24 32
2410 8 tsize += (long) (cur - data);
2411 8 ▶ if (!(cur = data = xdl_mmfile_next(mf, &size)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(cur = data = xdl_mmfile_next(mf, &size))) 8 0 8
2412 8 break;
2413 0 top = data + size;
2414 }
2415 24 nl++;
2416 24 ▶ if (!(cur = memchr(cur, '\n', top - cur)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(cur = memchr(cur, '\n', top - cur))) 3 21 24
2417 3 cur = top;
2418 else
2419 21 cur++;
2420 }
2421 8 tsize += (long) (cur - data);
2422 }
2423  
2424 8 ▶ if (nl && tsize)
Branch: 2 branches
BranchTrueFalseTotal
if (nl && tsize) 8 0 8
if (nl && tsize) 8 0 8
2425 8 nl = xdl_mmfile_size(mf) / (tsize / nl);
2426  
2427 8 return nl + 1;
2428 }
2429  
2430  
2431 9 unsigned long xdl_hash_record(char const **data, char const *top) {
2432 9 unsigned long ha = 5381;
2433 9 char const *ptr = *data;
2434  
2435 48 ▶ for (; ptr < top && *ptr != '\n'; ptr++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; ptr < top && *ptr != '\n'; ptr++) { 46 2 48
for (; ptr < top && *ptr != '\n'; ptr++) { 39 7 46
2436 39 ha += (ha << 5);
2437 39 ha ^= (unsigned long) *ptr;
2438 }
2439 9 ▶ *data = ptr < top ? ptr + 1: ptr;
Branch: 1 branch
BranchTrueFalseTotal
*data = ptr < top ? ptr + 1: ptr; 7 2 9
2440  
2441 9 return ha;
2442 }
2443  
2444  
2445 7 unsigned int xdl_hashbits(unsigned int size) {
2446 7 unsigned int val = 1, bits = 0;
2447  
2448 31 ▶ for (; val < size && bits < CHAR_BIT * sizeof(unsigned int); val <<= 1, bits++);
Branch: 2 branches
BranchTrueFalseTotal
for (; val < size && bits < CHAR_BIT * sizeof(unsigned int); val <<= 1, bits++); 24 7 31
for (; val < size && bits < CHAR_BIT * sizeof(unsigned int); val <<= 1, bits++); 24 0 24
2449 7 ▶ return bits ? bits: 1;
Branch: 1 branch
BranchTrueFalseTotal
return bits ? bits: 1; 7 0 7
2450 }
2451  
2452  
2453 8 int xdl_num_out(char *out, long val) {
2454 8 char *ptr, *str = out;
2455 char buf[32];
2456  
2457 8 ptr = buf + sizeof(buf) - 1;
2458 8 *ptr = '\0';
2459 8 ▶ if (val < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val < 0) { 0 8 8
2460 0 *--ptr = '-';
2461 0 val = -val;
2462 }
2463 16 ▶ for (; val && ptr > buf; val /= 10)
Branch: 2 branches
BranchTrueFalseTotal
for (; val && ptr > buf; val /= 10) 8 8 16
for (; val && ptr > buf; val /= 10) 8 0 8
2464 8 *--ptr = "0123456789"[val % 10];
2465 8 ▶ if (*ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr) 8 0 8
2466 16 ▶ for (; *ptr; ptr++, str++)
Branch: 1 branch
BranchTrueFalseTotal
for (; *ptr; ptr++, str++) 8 8 16
2467 8 *str = *ptr;
2468 else
2469 0 *str++ = '0';
2470 8 *str = '\0';
2471  
2472 8 return str - out;
2473 }
2474  
2475  
2476 8 long xdl_atol(char const *str, char const **next) {
2477 long val, base;
2478 char const *top;
2479  
2480 16 ▶ for (top = str; XDL_ISDIGIT(*top); top++);
Branch: 2 branches
BranchTrueFalseTotal
for (top = str; XDL_ISDIGIT(*top); top++); 8 8 16
for (top = str; XDL_ISDIGIT(*top); top++); 8 0 8
2481 8 ▶ if (next)
Branch: 1 branch
BranchTrueFalseTotal
if (next) 8 0 8
2482 8 *next = top;
2483 16 ▶ for (val = 0, base = 1, top--; top >= str; top--, base *= 10)
Branch: 1 branch
BranchTrueFalseTotal
for (val = 0, base = 1, top--; top >= str; top--, base *= 10) 8 8 16
2484 8 val += base * (long)(*top - '0');
2485 8 return val;
2486 }
2487  
2488  
2489 2 int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) {
2490 2 int nb = 0;
2491 mmbuffer_t mb;
2492 char buf[128];
2493  
2494 2 memcpy(buf, "@@ -", 4);
2495 2 nb += 4;
2496  
2497 2 ▶ nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1);
Branch: 1 branch
BranchTrueFalseTotal
nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1); 0 2 2
2498  
2499 2 memcpy(buf + nb, ",", 1);
2500 2 nb += 1;
2501  
2502 2 nb += xdl_num_out(buf + nb, c1);
2503  
2504 2 memcpy(buf + nb, " +", 2);
2505 2 nb += 2;
2506  
2507 2 ▶ nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1);
Branch: 1 branch
BranchTrueFalseTotal
nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1); 0 2 2
2508  
2509 2 memcpy(buf + nb, ",", 1);
2510 2 nb += 1;
2511  
2512 2 nb += xdl_num_out(buf + nb, c2);
2513  
2514 2 memcpy(buf + nb, " @@\n", 4);
2515 2 nb += 4;
2516  
2517 2 mb.ptr = buf;
2518 2 mb.size = nb;
2519 2 ▶ if (ecb->outf(ecb->priv, &mb, 1) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, &mb, 1) < 0) 0 2 2
2520 0 return -1;
2521  
2522 2 return 0;
2523 }
2524  
2525 /*
2526 * LibXDiff by Davide Libenzi ( File Differential Library )
2527 * Copyright (C) 2003 Davide Libenzi
2528 *
2529 * This library is free software; you can redistribute it and/or
2530 * modify it under the terms of the GNU Lesser General Public
2531 * License as published by the Free Software Foundation; either
2532 * version 2.1 of the License, or (at your option) any later version.
2533 *
2534 * This library is distributed in the hope that it will be useful,
2535 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2536 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2537 * Lesser General Public License for more details.
2538 *
2539 * You should have received a copy of the GNU Lesser General Public
2540 * License along with this library; if not, write to the Free Software
2541 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2542 *
2543 * Davide Libenzi <davidel@xmailserver.org>
2544 *
2545 */
2546  
2547 #include "xinclude.h"
2548  
2549  
2550  
2551 /* largest prime smaller than 65536 */
2552 #define BASE 65521L
2553  
2554 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
2555 #define NMAX 5552
2556  
2557  
2558 #define DO1(buf, i) { s1 += buf[i]; s2 += s1; }
2559 #define DO2(buf, i) DO1(buf, i); DO1(buf, i + 1);
2560 #define DO4(buf, i) DO2(buf, i); DO2(buf, i + 2);
2561 #define DO8(buf, i) DO4(buf, i); DO4(buf, i + 4);
2562 #define DO16(buf) DO8(buf, 0); DO8(buf, 8);
2563  
2564  
2565  
2566 1090 unsigned long xdl_adler32(unsigned long adler, unsigned char const *buf,
2567 unsigned int len) {
2568 int k;
2569 1090 unsigned long s1 = adler & 0xffff;
2570 1090 unsigned long s2 = (adler >> 16) & 0xffff;
2571  
2572 1090 ▶ if (!buf)
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) 0 1090 1090
2573 0 return 1;
2574  
2575 2180 ▶ while (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (len > 0) { 1090 1090 2180
2576 1090 k = len < NMAX ? len :NMAX;
2577 1090 len -= k;
2578 2291 ▶ while (k >= 16) {
Branch: 1 branch
BranchTrueFalseTotal
while (k >= 16) { 1201 1090 2291
2579 1201 DO16(buf);
2580 1201 buf += 16;
2581 1201 k -= 16;
2582 }
2583 1090 ▶ if (k != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (k != 0) 15 1075 1090
2584 do {
2585 120 s1 += *buf++;
2586 120 s2 += s1;
2587 120 ▶ } while (--k);
Branch: 1 branch
BranchTrueFalseTotal
} while (--k); 105 15 120
2588 1090 s1 %= BASE;
2589 1090 s2 %= BASE;
2590 }
2591  
2592 1090 return (s2 << 16) | s1;
2593 }
2594  
2595 /*
2596 * LibXDiff by Davide Libenzi ( File Differential Library )
2597 * Copyright (C) 2003 Davide Libenzi
2598 *
2599 * This library is free software; you can redistribute it and/or
2600 * modify it under the terms of the GNU Lesser General Public
2601 * License as published by the Free Software Foundation; either
2602 * version 2.1 of the License, or (at your option) any later version.
2603 *
2604 * This library is distributed in the hope that it will be useful,
2605 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2606 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2607 * Lesser General Public License for more details.
2608 *
2609 * You should have received a copy of the GNU Lesser General Public
2610 * License along with this library; if not, write to the Free Software
2611 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2612 *
2613 * Davide Libenzi <davidel@xmailserver.org>
2614 *
2615 */
2616  
2617 #include "xinclude.h"
2618  
2619  
2620 typedef struct s_bdrecord {
2621 struct s_bdrecord *next;
2622 unsigned long fp;
2623 char const *ptr;
2624 } bdrecord_t;
2625  
2626 typedef struct s_bdfile {
2627 char const *data, *top;
2628 chastore_t cha;
2629 unsigned int fphbits;
2630 bdrecord_t **fphash;
2631 } bdfile_t;
2632  
2633  
2634  
2635 1 static int xdl_prepare_bdfile(mmbuffer_t *mmb, long fpbsize, bdfile_t *bdf) {
2636 unsigned int fphbits;
2637 long i, size, hsize;
2638 char const *base, *data, *top;
2639 bdrecord_t *brec;
2640 bdrecord_t **fphash;
2641  
2642 1 fphbits = xdl_hashbits((unsigned int) (mmb->size / fpbsize) + 1);
2643 1 hsize = 1 << fphbits;
2644 1 ▶ if (!(fphash = (bdrecord_t **) xdl_malloc(hsize * sizeof(bdrecord_t *)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(fphash = (bdrecord_t **) xdl_malloc(hsize * sizeof(bdrecord_t *)))) { 0 1 1
2645  
2646 0 return -1;
2647 }
2648 129 ▶ for (i = 0; i < hsize; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hsize; i++) 128 1 129
2649 128 fphash[i] = NULL;
2650  
2651 1 ▶ if (xdl_cha_init(&bdf->cha, sizeof(bdrecord_t), hsize / 4 + 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_cha_init(&bdf->cha, sizeof(bdrecord_t), hsize / 4 + 1) < 0) { 0 1 1
2652  
2653 0 xdl_free(fphash);
2654 0 return -1;
2655 }
2656  
2657 1 ▶ if (!(size = mmb->size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(size = mmb->size)) { 0 1 1
2658 0 bdf->data = bdf->top = NULL;
2659 } else {
2660 1 bdf->data = data = base = mmb->ptr;
2661 1 bdf->top = top = mmb->ptr + mmb->size;
2662  
2663 1 ▶ if ((data += (size / fpbsize) * fpbsize) == top)
Branch: 1 branch
BranchTrueFalseTotal
if ((data += (size / fpbsize) * fpbsize) == top) 1 0 1
2664 1 data -= fpbsize;
2665  
2666 65 ▶ for (; data >= base; data -= fpbsize) {
Branch: 1 branch
BranchTrueFalseTotal
for (; data >= base; data -= fpbsize) { 64 1 65
2667 64 ▶ if (!(brec = (bdrecord_t *) xdl_cha_alloc(&bdf->cha))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(brec = (bdrecord_t *) xdl_cha_alloc(&bdf->cha))) { 0 64 64
2668  
2669 0 xdl_cha_free(&bdf->cha);
2670 0 xdl_free(fphash);
2671 0 return -1;
2672 }
2673  
2674 128 brec->fp = xdl_adler32(0, (unsigned char const *) data,
2675 64 XDL_MIN(fpbsize, (long) (top - data)));
2676 64 brec->ptr = data;
2677  
2678 64 i = (long) XDL_HASHLONG(brec->fp, fphbits);
2679 64 brec->next = fphash[i];
2680 64 fphash[i] = brec;
2681 }
2682 }
2683  
2684 1 bdf->fphbits = fphbits;
2685 1 bdf->fphash = fphash;
2686  
2687 1 return 0;
2688 }
2689  
2690  
2691 1 static void xdl_free_bdfile(bdfile_t *bdf) {
2692  
2693 1 xdl_free(bdf->fphash);
2694 1 xdl_cha_free(&bdf->cha);
2695 1 }
2696  
2697  
2698 1 unsigned long xdl_mmb_adler32(mmbuffer_t *mmb) {
2699  
2700 1 ▶ return mmb->size ? xdl_adler32(0, (unsigned char const *) mmb->ptr, mmb->size): 0;
Branch: 1 branch
BranchTrueFalseTotal
return mmb->size ? xdl_adler32(0, (unsigned char const *) mmb->ptr, mmb->size): 0; 1 0 1
2701 }
2702  
2703  
2704 1 unsigned long xdl_mmf_adler32(mmfile_t *mmf) {
2705 1 unsigned long fp = 0;
2706 long size;
2707 char const *blk;
2708  
2709 1 ▶ if ((blk = (char const *) xdl_mmfile_first(mmf, &size)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((blk = (char const *) xdl_mmfile_first(mmf, &size)) != NULL) { 1 0 1
2710 do {
2711 1 fp = xdl_adler32(fp, (unsigned char const *) blk, size);
2712 1 ▶ } while ((blk = (char const *) xdl_mmfile_next(mmf, &size)) != NULL);
Branch: 1 branch
BranchTrueFalseTotal
} while ((blk = (char const *) xdl_mmfile_next(mmf, &size)) != NULL); 0 1 1
2713 }
2714 1 return fp;
2715 }
2716  
2717  
2718 1 int xdl_bdiff_mb(mmbuffer_t *mmb1, mmbuffer_t *mmb2, bdiffparam_t const *bdp, xdemitcb_t *ecb) {
2719 long i, rsize, size, bsize, csize, msize, moff;
2720 unsigned long fp;
2721 char const *blk, *base, *data, *top, *ptr1, *ptr2;
2722 bdrecord_t *brec;
2723 bdfile_t bdf;
2724 mmbuffer_t mb[2];
2725 unsigned char cpybuf[32];
2726  
2727 1 ▶ if ((bsize = bdp->bsize) < XDL_MIN_BLKSIZE)
Branch: 1 branch
BranchTrueFalseTotal
if ((bsize = bdp->bsize) < XDL_MIN_BLKSIZE) 0 1 1
2728 0 bsize = XDL_MIN_BLKSIZE;
2729 1 ▶ if (xdl_prepare_bdfile(mmb1, bsize, &bdf) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_prepare_bdfile(mmb1, bsize, &bdf) < 0) { 0 1 1
2730  
2731 0 return -1;
2732 }
2733  
2734 /*
2735 * Prepare and emit the binary patch file header. It will be used
2736 * to verify that that file being patched matches in size and fingerprint
2737 * the one that generated the patch.
2738 */
2739 1 fp = xdl_mmb_adler32(mmb1);
2740 1 size = mmb1->size;
2741 1 XDL_LE32_PUT(cpybuf, fp);
2742 1 XDL_LE32_PUT(cpybuf + 4, size);
2743  
2744 1 mb[0].ptr = (char *) cpybuf;
2745 1 mb[0].size = 4 + 4;
2746  
2747 1 ▶ if (ecb->outf(ecb->priv, mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, 1) < 0) { 0 1 1
2748  
2749 0 xdl_free_bdfile(&bdf);
2750 0 return -1;
2751 }
2752  
2753 1 ▶ if ((blk = (char const *) mmb2->ptr) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((blk = (char const *) mmb2->ptr) != NULL) { 1 0 1
2754 1 size = mmb2->size;
2755 1025 ▶ for (base = data = blk, top = data + size; data < top;) {
Branch: 1 branch
BranchTrueFalseTotal
for (base = data = blk, top = data + size; data < top;) { 1024 1 1025
2756 1024 rsize = XDL_MIN(bsize, (long) (top - data));
2757 1024 fp = xdl_adler32(0, (unsigned char const *) data, rsize);
2758  
2759 1024 i = (long) XDL_HASHLONG(fp, bdf.fphbits);
2760 1559 ▶ for (msize = 0, brec = bdf.fphash[i]; brec; brec = brec->next)
Branch: 1 branch
BranchTrueFalseTotal
for (msize = 0, brec = bdf.fphash[i]; brec; brec = brec->next) 535 1024 1559
2761 535 ▶ if (brec->fp == fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (brec->fp == fp) { 0 535 535
2762 0 csize = XDL_MIN((long) (top - data), (long) (bdf.top - brec->ptr));
2763 0 ▶ for (ptr1 = brec->ptr, ptr2 = data; csize && *ptr1 == *ptr2;
Branch: 2 branches
BranchTrueFalseTotal
for (ptr1 = brec->ptr, ptr2 = data; csize && *ptr1 == *ptr2; 0 0 0
for (ptr1 = brec->ptr, ptr2 = data; csize && *ptr1 == *ptr2; 0 0 0
2764 0 csize--, ptr1++, ptr2++);
2765  
2766 0 ▶ if ((csize = (long) (ptr1 - brec->ptr)) > msize) {
Branch: 1 branch
BranchTrueFalseTotal
if ((csize = (long) (ptr1 - brec->ptr)) > msize) { 0 0 0
2767 0 moff = (long) (brec->ptr - bdf.data);
2768 0 msize = csize;
2769 }
2770 }
2771  
2772 1024 ▶ if (msize < XDL_COPYOP_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (msize < XDL_COPYOP_SIZE) { 1024 0 1024
2773 1024 data++;
2774 } else {
2775 0 ▶ if (data > base) {
Branch: 1 branch
BranchTrueFalseTotal
if (data > base) { 0 0 0
2776 0 i = (long) (data - base);
2777 0 ▶ if (i > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > 255) { 0 0 0
2778 0 cpybuf[0] = XDL_BDOP_INSB;
2779 0 XDL_LE32_PUT(cpybuf + 1, i);
2780  
2781 0 mb[0].ptr = (char *) cpybuf;
2782 0 mb[0].size = XDL_INSBOP_SIZE;
2783 } else {
2784 0 cpybuf[0] = XDL_BDOP_INS;
2785 0 cpybuf[1] = (unsigned char) i;
2786  
2787 0 mb[0].ptr = (char *) cpybuf;
2788 0 mb[0].size = 2;
2789 }
2790 0 mb[1].ptr = (char *) base;
2791 0 mb[1].size = i;
2792  
2793 0 ▶ if (ecb->outf(ecb->priv, mb, 2) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, 2) < 0) { 0 0 0
2794  
2795 0 xdl_free_bdfile(&bdf);
2796 0 return -1;
2797 }
2798 }
2799  
2800 0 data += msize;
2801  
2802 0 cpybuf[0] = XDL_BDOP_CPY;
2803 0 XDL_LE32_PUT(cpybuf + 1, moff);
2804 0 XDL_LE32_PUT(cpybuf + 5, msize);
2805  
2806 0 mb[0].ptr = (char *) cpybuf;
2807 0 mb[0].size = XDL_COPYOP_SIZE;
2808  
2809 0 ▶ if (ecb->outf(ecb->priv, mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, 1) < 0) { 0 0 0
2810  
2811 0 xdl_free_bdfile(&bdf);
2812 0 return -1;
2813 }
2814 0 base = data;
2815 }
2816 }
2817 1 ▶ if (data > base) {
Branch: 1 branch
BranchTrueFalseTotal
if (data > base) { 1 0 1
2818 1 i = (long) (data - base);
2819 1 ▶ if (i > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > 255) { 1 0 1
2820 1 cpybuf[0] = XDL_BDOP_INSB;
2821 1 XDL_LE32_PUT(cpybuf + 1, i);
2822  
2823 1 mb[0].ptr = (char *) cpybuf;
2824 1 mb[0].size = XDL_INSBOP_SIZE;
2825 } else {
2826 0 cpybuf[0] = XDL_BDOP_INS;
2827 0 cpybuf[1] = (unsigned char) i;
2828  
2829 0 mb[0].ptr = (char *) cpybuf;
2830 0 mb[0].size = 2;
2831 }
2832 1 mb[1].ptr = (char *) base;
2833 1 mb[1].size = i;
2834  
2835 1 ▶ if (ecb->outf(ecb->priv, mb, 2) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, 2) < 0) { 0 1 1
2836  
2837 0 xdl_free_bdfile(&bdf);
2838 0 return -1;
2839 }
2840 }
2841 }
2842  
2843 1 xdl_free_bdfile(&bdf);
2844  
2845 1 return 0;
2846 }
2847  
2848  
2849 1 int xdl_bdiff(mmfile_t *mmf1, mmfile_t *mmf2, bdiffparam_t const *bdp, xdemitcb_t *ecb) {
2850 mmbuffer_t mmb1, mmb2;
2851  
2852 1 ▶ if (!xdl_mmfile_iscompact(mmf1) || !xdl_mmfile_iscompact(mmf2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!xdl_mmfile_iscompact(mmf1) || !xdl_mmfile_iscompact(mmf2)) { 1 0 1
if (!xdl_mmfile_iscompact(mmf1) || !xdl_mmfile_iscompact(mmf2)) { 0 1 1
2853  
2854 0 return -1;
2855 }
2856  
2857 1 ▶ if ((mmb1.ptr = (char *) xdl_mmfile_first(mmf1, &mmb1.size)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((mmb1.ptr = (char *) xdl_mmfile_first(mmf1, &mmb1.size)) == NULL) 0 1 1
2858 0 mmb1.size = 0;
2859 1 ▶ if ((mmb2.ptr = (char *) xdl_mmfile_first(mmf2, &mmb2.size)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((mmb2.ptr = (char *) xdl_mmfile_first(mmf2, &mmb2.size)) == NULL) 0 1 1
2860 0 mmb2.size = 0;
2861  
2862 1 return xdl_bdiff_mb(&mmb1, &mmb2, bdp, ecb);
2863 }
2864  
2865  
2866 0 long xdl_bdiff_tgsize(mmfile_t *mmfp) {
2867 0 long tgsize = 0, size, off, csize;
2868 char const *blk;
2869 unsigned char const *data, *top;
2870  
2871 0 ▶ if ((blk = (char const *) xdl_mmfile_first(mmfp, &size)) == NULL ||
Branch: 1 branch
BranchTrueFalseTotal
if ((blk = (char const *) xdl_mmfile_first(mmfp, &size)) == NULL || 0 0 0
2872 0 ▶ size < XDL_BPATCH_HDR_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
size < XDL_BPATCH_HDR_SIZE) { 0 0 0
2873  
2874 0 return -1;
2875 }
2876 0 blk += XDL_BPATCH_HDR_SIZE;
2877 0 size -= XDL_BPATCH_HDR_SIZE;
2878  
2879 do {
2880 0 for (data = (unsigned char const *) blk, top = data + size;
2881 0 ▶ data < top;) {
Branch: 1 branch
BranchTrueFalseTotal
data < top;) { 0 0 0
2882 0 ▶ if (*data == XDL_BDOP_INS) {
Branch: 1 branch
BranchTrueFalseTotal
if (*data == XDL_BDOP_INS) { 0 0 0
2883 0 data++;
2884 0 csize = (long) *data++;
2885 0 tgsize += csize;
2886 0 data += csize;
2887 0 ▶ } else if (*data == XDL_BDOP_INSB) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*data == XDL_BDOP_INSB) { 0 0 0
2888 0 data++;
2889 0 XDL_LE32_GET(data, csize);
2890 0 data += 4;
2891 0 tgsize += csize;
2892 0 data += csize;
2893 0 ▶ } else if (*data == XDL_BDOP_CPY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*data == XDL_BDOP_CPY) { 0 0 0
2894 0 data++;
2895 0 XDL_LE32_GET(data, off);
2896 0 data += 4;
2897 0 XDL_LE32_GET(data, csize);
2898 0 data += 4;
2899 0 tgsize += csize;
2900 } else {
2901  
2902 0 return -1;
2903 }
2904 }
2905 0 ▶ } while ((blk = (char const *) xdl_mmfile_next(mmfp, &size)) != NULL);
Branch: 1 branch
BranchTrueFalseTotal
} while ((blk = (char const *) xdl_mmfile_next(mmfp, &size)) != NULL); 0 0 0
2906  
2907 0 return tgsize;
2908 }
2909  
2910 /*
2911 * LibXDiff by Davide Libenzi ( File Differential Library )
2912 * Copyright (C) 2003 Davide Libenzi
2913 *
2914 * This library is free software; you can redistribute it and/or
2915 * modify it under the terms of the GNU Lesser General Public
2916 * License as published by the Free Software Foundation; either
2917 * version 2.1 of the License, or (at your option) any later version.
2918 *
2919 * This library is distributed in the hope that it will be useful,
2920 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2921 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2922 * Lesser General Public License for more details.
2923 *
2924 * You should have received a copy of the GNU Lesser General Public
2925 * License along with this library; if not, write to the Free Software
2926 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2927 *
2928 * Davide Libenzi <davidel@xmailserver.org>
2929 *
2930 */
2931  
2932 #include "xinclude.h"
2933  
2934  
2935 #define XDL_MOBF_MINALLOC 128
2936  
2937  
2938 typedef struct s_mmoffbuffer {
2939 long off, size;
2940 char *ptr;
2941 } mmoffbuffer_t;
2942  
2943  
2944  
2945 0 static int xdl_copy_range(mmfile_t *mmf, long off, long size, xdemitcb_t *ecb) {
2946 0 ▶ if (xdl_seek_mmfile(mmf, off) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_seek_mmfile(mmf, off) < 0) { 0 0 0
2947  
2948 0 return -1;
2949 }
2950 0 ▶ if (xdl_copy_mmfile(mmf, size, ecb) != size) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_copy_mmfile(mmf, size, ecb) != size) { 0 0 0
2951  
2952 0 return -1;
2953 }
2954  
2955 0 return 0;
2956 }
2957  
2958  
2959 1 int xdl_bpatch(mmfile_t *mmf, mmfile_t *mmfp, xdemitcb_t *ecb) {
2960 long size, off, csize, osize;
2961 unsigned long fp, ofp;
2962 char const *blk;
2963 unsigned char const *data, *top;
2964 mmbuffer_t mb;
2965  
2966 1 ▶ if ((blk = (char const *) xdl_mmfile_first(mmfp, &size)) == NULL ||
Branch: 1 branch
BranchTrueFalseTotal
if ((blk = (char const *) xdl_mmfile_first(mmfp, &size)) == NULL || 1 0 1
2967 1 ▶ size < XDL_BPATCH_HDR_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
size < XDL_BPATCH_HDR_SIZE) { 0 1 1
2968  
2969 0 return -1;
2970 }
2971 1 ofp = xdl_mmf_adler32(mmf);
2972 1 osize = xdl_mmfile_size(mmf);
2973 1 XDL_LE32_GET(blk, fp);
2974 1 XDL_LE32_GET(blk + 4, csize);
2975 1 ▶ if (fp != ofp || csize != osize) {
Branch: 2 branches
BranchTrueFalseTotal
if (fp != ofp || csize != osize) { 1 0 1
if (fp != ofp || csize != osize) { 0 1 1
2976  
2977 0 return -1;
2978 }
2979  
2980 1 blk += XDL_BPATCH_HDR_SIZE;
2981 1 size -= XDL_BPATCH_HDR_SIZE;
2982  
2983 do {
2984 1 for (data = (unsigned char const *) blk, top = data + size;
2985 2 ▶ data < top;) {
Branch: 1 branch
BranchTrueFalseTotal
data < top;) { 1 1 2
2986 1 ▶ if (*data == XDL_BDOP_INS) {
Branch: 1 branch
BranchTrueFalseTotal
if (*data == XDL_BDOP_INS) { 0 1 1
2987 0 data++;
2988  
2989 0 mb.size = (long) *data++;
2990 0 mb.ptr = (char *) data;
2991 0 data += mb.size;
2992  
2993 0 ▶ if (ecb->outf(ecb->priv, &mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, &mb, 1) < 0) { 0 0 0
2994  
2995 0 return -1;
2996 }
2997 1 ▶ } else if (*data == XDL_BDOP_INSB) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*data == XDL_BDOP_INSB) { 1 0 1
2998 1 data++;
2999 1 XDL_LE32_GET(data, csize);
3000 1 data += 4;
3001  
3002 1 mb.size = csize;
3003 1 mb.ptr = (char *) data;
3004 1 data += mb.size;
3005  
3006 1 ▶ if (ecb->outf(ecb->priv, &mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, &mb, 1) < 0) { 0 1 1
3007  
3008 0 return -1;
3009 }
3010 0 ▶ } else if (*data == XDL_BDOP_CPY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*data == XDL_BDOP_CPY) { 0 0 0
3011 0 data++;
3012 0 XDL_LE32_GET(data, off);
3013 0 data += 4;
3014 0 XDL_LE32_GET(data, csize);
3015 0 data += 4;
3016  
3017 0 ▶ if (xdl_copy_range(mmf, off, csize, ecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_copy_range(mmf, off, csize, ecb) < 0) { 0 0 0
3018  
3019 0 return -1;
3020 }
3021 } else {
3022  
3023 0 return -1;
3024 }
3025 }
3026 1 ▶ } while ((blk = (char const *) xdl_mmfile_next(mmfp, &size)) != NULL);
Branch: 1 branch
BranchTrueFalseTotal
} while ((blk = (char const *) xdl_mmfile_next(mmfp, &size)) != NULL); 0 1 1
3027  
3028 1 return 0;
3029 }
3030  
3031  
3032 0 static unsigned long xdl_mmob_adler32(mmoffbuffer_t *obf, int n) {
3033 unsigned long ha;
3034  
3035 0 ▶ for (ha = 0; n > 0; n--, obf++)
Branch: 1 branch
BranchTrueFalseTotal
for (ha = 0; n > 0; n--, obf++) 0 0 0
3036 0 ha = xdl_adler32(ha, (unsigned char const *) obf->ptr, obf->size);
3037  
3038 0 return ha;
3039 }
3040  
3041  
3042 0 static long xdl_mmob_size(mmoffbuffer_t *obf, int n) {
3043  
3044 0 ▶ return n > 0 ? obf[n - 1].off + obf[n - 1].size: 0;
Branch: 1 branch
BranchTrueFalseTotal
return n > 0 ? obf[n - 1].off + obf[n - 1].size: 0; 0 0 0
3045 }
3046  
3047  
3048 0 static mmoffbuffer_t *xdl_mmob_new(mmoffbuffer_t **probf, int *pnobf, int *paobf) {
3049 int aobf;
3050 mmoffbuffer_t *cobf, *rrobf;
3051  
3052 0 ▶ if (*pnobf >= *paobf) {
Branch: 1 branch
BranchTrueFalseTotal
if (*pnobf >= *paobf) { 0 0 0
3053 0 aobf = 2 * (*paobf) + 1;
3054 0 ▶ if ((rrobf = (mmoffbuffer_t *)
Branch: 1 branch
BranchTrueFalseTotal
if ((rrobf = (mmoffbuffer_t *) 0 0 0
3055 0 xdl_realloc(*probf, aobf * sizeof(mmoffbuffer_t))) == NULL) {
3056  
3057 0 return NULL;
3058 }
3059 0 *probf = rrobf;
3060 0 *paobf = aobf;
3061 }
3062 0 cobf = (*probf) + (*pnobf);
3063 0 (*pnobf)++;
3064  
3065 0 return cobf;
3066 }
3067  
3068  
3069 0 static int xdl_mmob_find_cntr(mmoffbuffer_t *obf, int n, long off) {
3070 int i, lo, hi;
3071  
3072 0 ▶ for (lo = -1, hi = n; hi - lo > 1;) {
Branch: 1 branch
BranchTrueFalseTotal
for (lo = -1, hi = n; hi - lo > 1;) { 0 0 0
3073 0 i = (hi + lo) / 2;
3074 0 ▶ if (off < obf[i].off)
Branch: 1 branch
BranchTrueFalseTotal
if (off < obf[i].off) 0 0 0
3075 0 hi = i;
3076 else
3077 0 lo = i;
3078 }
3079  
3080 0 ▶ return (lo >= 0 && off >= obf[lo].off && off < obf[lo].off + obf[lo].size) ? lo: -1;
Branch: 3 branches
BranchTrueFalseTotal
return (lo >= 0 && off >= obf[lo].off && off < obf[lo].off + obf[lo].size) ? lo: -1; 0 0 0
return (lo >= 0 && off >= obf[lo].off && off < obf[lo].off + obf[lo].size) ? lo: -1; 0 0 0
return (lo >= 0 && off >= obf[lo].off && off < obf[lo].off + obf[lo].size) ? lo: -1; 0 0 0
3081 }
3082  
3083  
3084 0 static int xdl_bmerge(mmoffbuffer_t *obf, int n, mmbuffer_t *mbfp, mmoffbuffer_t **probf,
3085 int *pnobf) {
3086 int i, aobf, nobf;
3087 long ooff, off, csize;
3088 unsigned long fp, ofp;
3089 unsigned char const *data, *top;
3090 mmoffbuffer_t *robf, *cobf;
3091  
3092 0 ▶ if (mbfp->size < XDL_BPATCH_HDR_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (mbfp->size < XDL_BPATCH_HDR_SIZE) { 0 0 0
3093  
3094 0 return -1;
3095 }
3096 0 data = (unsigned char const *) mbfp->ptr;
3097 0 top = data + mbfp->size;
3098  
3099 0 ofp = xdl_mmob_adler32(obf, n);
3100 0 XDL_LE32_GET(data, fp);
3101 0 data += 4;
3102 0 XDL_LE32_GET(data, csize);
3103 0 data += 4;
3104 0 ▶ if (fp != ofp || csize != xdl_mmob_size(obf, n)) {
Branch: 2 branches
BranchTrueFalseTotal
if (fp != ofp || csize != xdl_mmob_size(obf, n)) { 0 0 0
if (fp != ofp || csize != xdl_mmob_size(obf, n)) { 0 0 0
3105  
3106 0 return -1;
3107 }
3108 0 aobf = XDL_MOBF_MINALLOC;
3109 0 nobf = 0;
3110 0 ▶ if ((robf = (mmoffbuffer_t *) xdl_malloc(aobf * sizeof(mmoffbuffer_t))) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((robf = (mmoffbuffer_t *) xdl_malloc(aobf * sizeof(mmoffbuffer_t))) == NULL) { 0 0 0
3111  
3112 0 return -1;
3113 }
3114  
3115 0 ▶ for (ooff = 0; data < top;) {
Branch: 1 branch
BranchTrueFalseTotal
for (ooff = 0; data < top;) { 0 0 0
3116 0 ▶ if (*data == XDL_BDOP_INS) {
Branch: 1 branch
BranchTrueFalseTotal
if (*data == XDL_BDOP_INS) { 0 0 0
3117 0 data++;
3118  
3119 0 ▶ if ((cobf = xdl_mmob_new(&robf, &nobf, &aobf)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cobf = xdl_mmob_new(&robf, &nobf, &aobf)) == NULL) { 0 0 0
3120  
3121 0 xdl_free(robf);
3122 0 return -1;
3123 }
3124 0 cobf->off = ooff;
3125 0 cobf->size = (long) *data++;
3126 0 cobf->ptr = (char *) data;
3127  
3128 0 data += cobf->size;
3129 0 ooff += cobf->size;
3130 0 ▶ } else if (*data == XDL_BDOP_INSB) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*data == XDL_BDOP_INSB) { 0 0 0
3131 0 data++;
3132 0 XDL_LE32_GET(data, csize);
3133 0 data += 4;
3134  
3135 0 ▶ if ((cobf = xdl_mmob_new(&robf, &nobf, &aobf)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cobf = xdl_mmob_new(&robf, &nobf, &aobf)) == NULL) { 0 0 0
3136  
3137 0 xdl_free(robf);
3138 0 return -1;
3139 }
3140 0 cobf->off = ooff;
3141 0 cobf->size = csize;
3142 0 cobf->ptr = (char *) data;
3143  
3144 0 data += cobf->size;
3145 0 ooff += cobf->size;
3146 0 ▶ } else if (*data == XDL_BDOP_CPY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*data == XDL_BDOP_CPY) { 0 0 0
3147 0 data++;
3148 0 XDL_LE32_GET(data, off);
3149 0 data += 4;
3150 0 XDL_LE32_GET(data, csize);
3151 0 data += 4;
3152  
3153 0 ▶ if ((i = xdl_mmob_find_cntr(obf, n, off)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((i = xdl_mmob_find_cntr(obf, n, off)) < 0) { 0 0 0
3154  
3155 0 xdl_free(robf);
3156 0 return -1;
3157 }
3158 0 off -= obf[i].off;
3159 0 ▶ for (; i < n && csize > 0; i++, off = 0) {
Branch: 2 branches
BranchTrueFalseTotal
for (; i < n && csize > 0; i++, off = 0) { 0 0 0
for (; i < n && csize > 0; i++, off = 0) { 0 0 0
3160 0 ▶ if ((cobf = xdl_mmob_new(&robf, &nobf, &aobf)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cobf = xdl_mmob_new(&robf, &nobf, &aobf)) == NULL) { 0 0 0
3161  
3162 0 xdl_free(robf);
3163 0 return -1;
3164 }
3165 0 cobf->off = ooff;
3166 0 cobf->size = XDL_MIN(csize, obf[i].size - off);
3167 0 cobf->ptr = obf[i].ptr + off;
3168  
3169 0 ooff += cobf->size;
3170 0 csize -= cobf->size;
3171 }
3172 0 ▶ if (csize > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (csize > 0) { 0 0 0
3173  
3174 0 xdl_free(robf);
3175 0 return -1;
3176 }
3177 } else {
3178  
3179 0 xdl_free(robf);
3180 0 return -1;
3181 }
3182 }
3183 0 *probf = robf;
3184 0 *pnobf = nobf;
3185  
3186 0 return 0;
3187 }
3188  
3189  
3190 0 static int xdl_bmerge_synt(mmoffbuffer_t *obf, int n, xdemitcb_t *ecb) {
3191 int i;
3192 mmbuffer_t *mb;
3193  
3194 0 ▶ if ((mb = (mmbuffer_t *) xdl_malloc(n * sizeof(mmbuffer_t))) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((mb = (mmbuffer_t *) xdl_malloc(n * sizeof(mmbuffer_t))) == NULL) { 0 0 0
3195  
3196 0 return -1;
3197 }
3198 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
3199 0 mb[i].ptr = obf[i].ptr;
3200 0 mb[i].size = obf[i].size;
3201 }
3202 0 ▶ if (ecb->outf(ecb->priv, mb, n) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, n) < 0) { 0 0 0
3203  
3204 0 xdl_free(mb);
3205 0 return -1;
3206 }
3207 0 xdl_free(mb);
3208  
3209 0 return 0;
3210 }
3211  
3212  
3213 0 int xdl_bpatch_multi(mmbuffer_t *base, mmbuffer_t *mbpch, int n, xdemitcb_t *ecb) {
3214 int i, nobf, fnobf;
3215 mmoffbuffer_t *obf, *fobf;
3216  
3217 0 nobf = 1;
3218 0 ▶ if ((obf = (mmoffbuffer_t *) xdl_malloc(nobf * sizeof(mmoffbuffer_t))) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((obf = (mmoffbuffer_t *) xdl_malloc(nobf * sizeof(mmoffbuffer_t))) == NULL) { 0 0 0
3219  
3220 0 return -1;
3221 }
3222 0 obf->off = 0;
3223 0 obf->ptr = base->ptr;
3224 0 obf->size = base->size;
3225 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
3226 0 ▶ if (xdl_bmerge(obf, nobf, &mbpch[i], &fobf, &fnobf) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_bmerge(obf, nobf, &mbpch[i], &fobf, &fnobf) < 0) { 0 0 0
3227  
3228 0 xdl_free(obf);
3229 0 return -1;
3230 }
3231 0 xdl_free(obf);
3232  
3233 0 obf = fobf;
3234 0 nobf = fnobf;
3235 }
3236 0 ▶ if (xdl_bmerge_synt(obf, nobf, ecb) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xdl_bmerge_synt(obf, nobf, ecb) < 0) { 0 0 0
3237  
3238 0 xdl_free(obf);
3239 0 return -1;
3240 }
3241 0 xdl_free(obf);
3242  
3243 0 return 0;
3244 }
3245  
3246 /*
3247 * LibXDiff by Davide Libenzi ( File Differential Library )
3248 * Copyright (C) 2003 Davide Libenzi
3249 *
3250 * This library is free software; you can redistribute it and/or
3251 * modify it under the terms of the GNU Lesser General Public
3252 * License as published by the Free Software Foundation; either
3253 * version 2.1 of the License, or (at your option) any later version.
3254 *
3255 * This library is distributed in the hope that it will be useful,
3256 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3257 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3258 * Lesser General Public License for more details.
3259 *
3260 * You should have received a copy of the GNU Lesser General Public
3261 * License along with this library; if not, write to the Free Software
3262 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3263 *
3264 * Davide Libenzi <davidel@xmailserver.org>
3265 *
3266 */
3267  
3268 #include "xinclude.h"
3269  
3270  
3271 char libxdiff_version[] = "LibXDiff v" PACKAGE_VERSION " by Davide Libenzi <davide@xmailserver.org>";
3272  
3273 /*
3274 * LibXDiff by Davide Libenzi ( File Differential Library )
3275 * Copyright (C) 2003 Davide Libenzi
3276 *
3277 * This library is free software; you can redistribute it and/or
3278 * modify it under the terms of the GNU Lesser General Public
3279 * License as published by the Free Software Foundation; either
3280 * version 2.1 of the License, or (at your option) any later version.
3281 *
3282 * This library is distributed in the hope that it will be useful,
3283 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3284 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3285 * Lesser General Public License for more details.
3286 *
3287 * You should have received a copy of the GNU Lesser General Public
3288 * License along with this library; if not, write to the Free Software
3289 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3290 *
3291 * Davide Libenzi <davidel@xmailserver.org>
3292 *
3293 */
3294  
3295 #include "xinclude.h"
3296  
3297  
3298  
3299 static memallocator_t xmalt = {NULL, NULL, NULL};
3300  
3301  
3302  
3303 2 int xdl_set_allocator(memallocator_t const *malt) {
3304 2 xmalt = *malt;
3305 2 return 0;
3306 }
3307  
3308  
3309 67 void *xdl_malloc(unsigned int size) {
3310 67 ▶ return xmalt.malloc ? xmalt.malloc(xmalt.priv, size): NULL;
Branch: 1 branch
BranchTrueFalseTotal
return xmalt.malloc ? xmalt.malloc(xmalt.priv, size): NULL; 67 0 67
3311 }
3312  
3313  
3314 65 void xdl_free(void *ptr) {
3315 65 ▶ if (xmalt.free)
Branch: 1 branch
BranchTrueFalseTotal
if (xmalt.free) 65 0 65
3316 65 xmalt.free(xmalt.priv, ptr);
3317 65 }
3318  
3319  
3320 0 void *xdl_realloc(void *ptr, unsigned int size) {
3321 0 ▶ return xmalt.realloc ? xmalt.realloc(xmalt.priv, ptr, size): NULL;
Branch: 1 branch
BranchTrueFalseTotal
return xmalt.realloc ? xmalt.realloc(xmalt.priv, ptr, size): NULL; 0 0 0
3322 }
3323  
3324 /*
3325 * xrabdiff by Davide Libenzi (Rabin's polynomial fingerprint based delta generator)
3326 * Copyright (C) 2006 Davide Libenzi
3327 *
3328 * This program is free software; you can redistribute it and/or modify
3329 * it under the terms of the GNU General Public License as published by
3330 * the Free Software Foundation; either version 2 of the License, or
3331 * (at your option) any later version.
3332 *
3333 * This program is distributed in the hope that it will be useful,
3334 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3335 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3336 * GNU General Public License for more details.
3337 *
3338 * You should have received a copy of the GNU General Public License
3339 * along with this program; if not, write to the Free Software
3340 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3341 *
3342 * Davide Libenzi <davidel@xmailserver.org>
3343 *
3344 *
3345 * Hints, ideas and code for the implementation came from:
3346 *
3347 * Rabin's original paper: http://www.xmailserver.org/rabin.pdf
3348 * Chan & Lu's paper: http://www.xmailserver.org/rabin_impl.pdf
3349 * Broder's paper: http://www.xmailserver.org/rabin_apps.pdf
3350 * LBFS source code: http://www.fs.net/sfswww/lbfs/
3351 * Geert Bosch's post: http://marc.theaimsgroup.com/?l=git&m=114565424620771&w=2
3352 *
3353 */
3354  
3355 #include "xinclude.h"
3356  
3357  
3358 #if !defined(XRABPLY_TYPE32) && !defined(XRABPLY_TYPE64)
3359 #define XRABPLY_TYPE64 long long
3360 #define XV64(v) ((xply_word) v ## ULL)
3361 #endif
3362  
3363 #include "xrabply.c"
3364  
3365  
3366  
3367 #define XRAB_SLIDE(v, c) do { \
3368 if (++wpos == XRAB_WNDSIZE) wpos = 0; \
3369 v ^= U[wbuf[wpos]]; \
3370 wbuf[wpos] = (c); \
3371 v = ((v << 8) | (c)) ^ T[v >> XRAB_SHIFT]; \
3372 } while (0)
3373  
3374  
3375 #define XRAB_MINCPYSIZE 12
3376 #define XRAB_WBITS (sizeof(xply_word) * 8)
3377  
3378  
3379  
3380 typedef struct s_xrabctx {
3381 long idxsize;
3382 long *idx;
3383 unsigned char const *data;
3384 long size;
3385 } xrabctx_t;
3386  
3387 typedef struct s_xrabcpyi {
3388 long src;
3389 long tgt;
3390 long len;
3391 } xrabcpyi_t;
3392  
3393 typedef struct s_xrabcpyi_arena {
3394 long cnt, size;
3395 xrabcpyi_t *acpy;
3396 } xrabcpyi_arena_t;
3397  
3398  
3399  
3400 0 static void xrab_init_cpyarena(xrabcpyi_arena_t *aca) {
3401 0 aca->cnt = aca->size = 0;
3402 0 aca->acpy = NULL;
3403 0 }
3404  
3405  
3406 0 static void xrab_free_cpyarena(xrabcpyi_arena_t *aca) {
3407 0 xdl_free(aca->acpy);
3408 0 }
3409  
3410  
3411 0 static int xrab_add_cpy(xrabcpyi_arena_t *aca, xrabcpyi_t const *rcpy) {
3412 long size;
3413 xrabcpyi_t *acpy;
3414  
3415 0 ▶ if (aca->cnt >= aca->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (aca->cnt >= aca->size) { 0 0 0
3416 0 size = 2 * aca->size + 1024;
3417 0 ▶ if ((acpy = (xrabcpyi_t *)
Branch: 1 branch
BranchTrueFalseTotal
if ((acpy = (xrabcpyi_t *) 0 0 0
3418 0 xdl_realloc(aca->acpy, size * sizeof(xrabcpyi_t))) == NULL)
3419 0 return -1;
3420 0 aca->acpy = acpy;
3421 0 aca->size = size;
3422 }
3423 0 aca->acpy[aca->cnt++] = *rcpy;
3424  
3425 0 return 0;
3426 }
3427  
3428  
3429 0 static long xrab_cmnseq(unsigned char const *data, long start, long size) {
3430 0 unsigned char ch = data[start];
3431 unsigned char const *ptr, *top;
3432  
3433 0 ▶ for (ptr = data + start + 1, top = data + size; ptr < top && ch == *ptr; ptr++);
Branch: 2 branches
BranchTrueFalseTotal
for (ptr = data + start + 1, top = data + size; ptr < top && ch == *ptr; ptr++); 0 0 0
for (ptr = data + start + 1, top = data + size; ptr < top && ch == *ptr; ptr++); 0 0 0
3434  
3435 0 return (long) (ptr - (data + start + 1));
3436 }
3437  
3438  
3439 0 static int xrab_build_ctx(unsigned char const *data, long size, xrabctx_t *ctx) {
3440 0 long i, isize, idxsize, seq, wpos = 0;
3441 0 xply_word fp = 0, mask;
3442 unsigned char ch;
3443 unsigned char const *ptr, *eot;
3444 long *idx;
3445 unsigned char wbuf[XRAB_WNDSIZE];
3446 long maxoffs[256];
3447 long maxseq[256];
3448 xply_word maxfp[256];
3449  
3450 0 memset(wbuf, 0, sizeof(wbuf));
3451 0 memset(maxseq, 0, sizeof(maxseq));
3452 0 isize = 2 * (size / XRAB_WNDSIZE);
3453 0 ▶ for (idxsize = 1; idxsize < isize; idxsize <<= 1);
Branch: 1 branch
BranchTrueFalseTotal
for (idxsize = 1; idxsize < isize; idxsize <<= 1); 0 0 0
3454 0 mask = (xply_word) (idxsize - 1);
3455 0 ▶ if ((idx = (long *) xdl_malloc(idxsize * sizeof(long))) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((idx = (long *) xdl_malloc(idxsize * sizeof(long))) == NULL) 0 0 0
3456 0 return -1;
3457 0 memset(idx, 0, idxsize * sizeof(long));
3458 0 ▶ for (i = 0; i + XRAB_WNDSIZE < size; i += XRAB_WNDSIZE) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + XRAB_WNDSIZE < size; i += XRAB_WNDSIZE) { 0 0 0
3459 /*
3460 * Generate a brand new hash for the current window. Here we could
3461 * try to perform pseudo-loop unroll by 4 blocks if necessary, and
3462 * if we force XRAB_WNDSIZE to be a multiple of 4, we could reduce
3463 * the branch occurence inside XRAB_SLIDE by a factor of 4.
3464 */
3465 0 ▶ for (ptr = data + i, eot = ptr + XRAB_WNDSIZE; ptr < eot; ptr++)
Branch: 1 branch
BranchTrueFalseTotal
for (ptr = data + i, eot = ptr + XRAB_WNDSIZE; ptr < eot; ptr++) 0 0 0
3466 0 ▶ XRAB_SLIDE(fp, *ptr);
Branch: 1 branch
BranchTrueFalseTotal
XRAB_SLIDE(fp, *ptr); 0 0 0
3467  
3468 /*
3469 * Try to scan for single value scans, and store them in the
3470 * array according to the longest one. Before we do a fast check
3471 * to avoid calling xrab_cmnseq() when not necessary.
3472 */
3473 0 if ((ch = data[i]) == data[i + XRAB_WNDSIZE - 1] &&
3474 0 (seq = xrab_cmnseq(data, i, size)) > XRAB_WNDSIZE &&
3475 0 ▶ seq > maxseq[ch]) {
Branch: 1 branch
BranchTrueFalseTotal
seq > maxseq[ch]) { 0 0 0
3476 0 maxseq[ch] = seq;
3477 0 maxfp[ch] = fp;
3478 0 maxoffs[ch] = i + XRAB_WNDSIZE;
3479 0 seq = (seq / XRAB_WNDSIZE) * XRAB_WNDSIZE;
3480 0 i += seq - XRAB_WNDSIZE;
3481 } else
3482 0 idx[fp & mask] = i + XRAB_WNDSIZE;
3483 }
3484  
3485 /*
3486 * Restore back the logest sequences by overwriting target hash buckets.
3487 */
3488 0 ▶ for (i = 0; i < 256; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) 0 0 0
3489 0 ▶ if (maxseq[i])
Branch: 1 branch
BranchTrueFalseTotal
if (maxseq[i]) 0 0 0
3490 0 idx[maxfp[i] & mask] = maxoffs[i];
3491 0 ctx->idxsize = idxsize;
3492 0 ctx->idx = idx;
3493 0 ctx->data = data;
3494 0 ctx->size = size;
3495  
3496 0 return 0;
3497 }
3498  
3499  
3500 0 static void xrab_free_ctx(xrabctx_t *ctx) {
3501  
3502 0 xdl_free(ctx->idx);
3503 0 }
3504  
3505  
3506 0 static int xrab_diff(unsigned char const *data, long size, xrabctx_t *ctx,
3507 xrabcpyi_arena_t *aca) {
3508 0 long i, offs, ssize, src, tgt, esrc, etgt, wpos = 0;
3509 0 xply_word fp = 0, mask;
3510 long const *idx;
3511 unsigned char const *sdata;
3512 xrabcpyi_t rcpy;
3513 unsigned char wbuf[XRAB_WNDSIZE];
3514  
3515 0 xrab_init_cpyarena(aca);
3516 0 memset(wbuf, 0, sizeof(wbuf));
3517 0 ▶ for (i = 0; i < XRAB_WNDSIZE - 1 && i < size; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < XRAB_WNDSIZE - 1 && i < size; i++) 0 0 0
for (i = 0; i < XRAB_WNDSIZE - 1 && i < size; i++) 0 0 0
3518 0 ▶ XRAB_SLIDE(fp, data[i]);
Branch: 1 branch
BranchTrueFalseTotal
XRAB_SLIDE(fp, data[i]); 0 0 0
3519 0 idx = ctx->idx;
3520 0 sdata = ctx->data;
3521 0 ssize = ctx->size;
3522 0 mask = (xply_word) (ctx->idxsize - 1);
3523 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
3524 0 unsigned char ch = data[i++];
3525  
3526 0 ▶ XRAB_SLIDE(fp, ch);
Branch: 1 branch
BranchTrueFalseTotal
XRAB_SLIDE(fp, ch); 0 0 0
3527 0 offs = idx[fp & mask];
3528  
3529 /*
3530 * Fast check here to probabilistically reduce false positives
3531 * that would trigger the slow path below.
3532 */
3533 0 ▶ if (offs == 0 || ch != sdata[offs - 1])
Branch: 2 branches
BranchTrueFalseTotal
if (offs == 0 || ch != sdata[offs - 1]) 0 0 0
if (offs == 0 || ch != sdata[offs - 1]) 0 0 0
3534 0 continue;
3535  
3536 /*
3537 * Stretch the match both sides as far as possible.
3538 */
3539 0 src = offs - 1;
3540 0 tgt = i - 1;
3541 0 ▶ for (; tgt > 0 && src > 0 && data[tgt - 1] == sdata[src - 1];
Branch: 3 branches
BranchTrueFalseTotal
for (; tgt > 0 && src > 0 && data[tgt - 1] == sdata[src - 1]; 0 0 0
for (; tgt > 0 && src > 0 && data[tgt - 1] == sdata[src - 1]; 0 0 0
for (; tgt > 0 && src > 0 && data[tgt - 1] == sdata[src - 1]; 0 0 0
3542 0 tgt--, src--);
3543 0 esrc = offs;
3544 0 etgt = i;
3545 0 ▶ for (; etgt < size && esrc < ssize && data[etgt] == sdata[esrc];
Branch: 3 branches
BranchTrueFalseTotal
for (; etgt < size && esrc < ssize && data[etgt] == sdata[esrc]; 0 0 0
for (; etgt < size && esrc < ssize && data[etgt] == sdata[esrc]; 0 0 0
for (; etgt < size && esrc < ssize && data[etgt] == sdata[esrc]; 0 0 0
3546 0 etgt++, esrc++);
3547  
3548 /*
3549 * Avoid considering copies smaller than the XRAB_MINCPYSIZE
3550 * threshold.
3551 */
3552 0 ▶ if (etgt - tgt >= XRAB_MINCPYSIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (etgt - tgt >= XRAB_MINCPYSIZE) { 0 0 0
3553 0 rcpy.src = src;
3554 0 rcpy.tgt = tgt;
3555 0 rcpy.len = etgt - tgt;
3556 0 ▶ if (xrab_add_cpy(aca, &rcpy) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (xrab_add_cpy(aca, &rcpy) < 0) { 0 0 0
3557 0 xrab_free_cpyarena(aca);
3558 0 return -1;
3559 }
3560  
3561 /*
3562 * Fill up the new window and exit with 'i' properly set on exit.
3563 */
3564 0 ▶ for (i = etgt - XRAB_WNDSIZE; i < etgt; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = etgt - XRAB_WNDSIZE; i < etgt; i++) 0 0 0
3565 0 ▶ XRAB_SLIDE(fp, data[i]);
Branch: 1 branch
BranchTrueFalseTotal
XRAB_SLIDE(fp, data[i]); 0 0 0
3566 }
3567 }
3568  
3569 0 return 0;
3570 }
3571  
3572  
3573 0 static int xrab_tune_cpyarena(unsigned char const *data, long size, xrabctx_t *ctx,
3574 xrabcpyi_arena_t *aca) {
3575 long i, cpos;
3576 xrabcpyi_t *rcpy;
3577  
3578 0 ▶ for (cpos = size, i = aca->cnt - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (cpos = size, i = aca->cnt - 1; i >= 0; i--) { 0 0 0
3579 0 rcpy = aca->acpy + i;
3580 0 ▶ if (rcpy->tgt >= cpos)
Branch: 1 branch
BranchTrueFalseTotal
if (rcpy->tgt >= cpos) 0 0 0
3581 0 rcpy->len = 0;
3582 0 ▶ else if (rcpy->tgt + rcpy->len > cpos) {
Branch: 1 branch
BranchTrueFalseTotal
else if (rcpy->tgt + rcpy->len > cpos) { 0 0 0
3583 0 ▶ if ((rcpy->len = cpos - rcpy->tgt) >= XRAB_MINCPYSIZE)
Branch: 1 branch
BranchTrueFalseTotal
if ((rcpy->len = cpos - rcpy->tgt) >= XRAB_MINCPYSIZE) 0 0 0
3584 0 cpos = rcpy->tgt;
3585 else
3586 0 rcpy->len = 0;
3587 } else
3588 0 cpos = rcpy->tgt;
3589 }
3590  
3591 0 return 0;
3592 }
3593  
3594  
3595 0 int xdl_rabdiff_mb(mmbuffer_t *mmb1, mmbuffer_t *mmb2, xdemitcb_t *ecb) {
3596 long i, cpos, size;
3597 unsigned long fp;
3598 xrabcpyi_t *rcpy;
3599 xrabctx_t ctx;
3600 xrabcpyi_arena_t aca;
3601 mmbuffer_t mb[2];
3602 unsigned char cpybuf[32];
3603  
3604 0 fp = xdl_mmb_adler32(mmb1);
3605 0 ▶ if (xrab_build_ctx((unsigned char const *) mmb1->ptr, mmb1->size,
Branch: 1 branch
BranchTrueFalseTotal
if (xrab_build_ctx((unsigned char const *) mmb1->ptr, mmb1->size, 0 0 0
3606 &ctx) < 0)
3607 0 return -1;
3608 0 ▶ if (xrab_diff((unsigned char const *) mmb2->ptr, mmb2->size, &ctx,
Branch: 1 branch
BranchTrueFalseTotal
if (xrab_diff((unsigned char const *) mmb2->ptr, mmb2->size, &ctx, 0 0 0
3609 &aca) < 0) {
3610 0 xrab_free_ctx(&ctx);
3611 0 return -1;
3612 }
3613 0 xrab_tune_cpyarena((unsigned char const *) mmb2->ptr, mmb2->size, &ctx,
3614 &aca);
3615 0 xrab_free_ctx(&ctx);
3616  
3617 /*
3618 * Prepare and emit the binary patch file header. It will be used
3619 * to verify that that file being patched matches in size and fingerprint
3620 * the one that generated the patch.
3621 */
3622 0 size = mmb1->size;
3623 0 XDL_LE32_PUT(cpybuf, fp);
3624 0 XDL_LE32_PUT(cpybuf + 4, size);
3625  
3626 0 mb[0].ptr = (char *) cpybuf;
3627 0 mb[0].size = 4 + 4;
3628 0 ▶ if (ecb->outf(ecb->priv, mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, 1) < 0) { 0 0 0
3629 0 xrab_free_cpyarena(&aca);
3630 0 return -1;
3631 }
3632 0 ▶ for (cpos = 0, i = 0; i < aca.cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (cpos = 0, i = 0; i < aca.cnt; i++) { 0 0 0
3633 0 rcpy = aca.acpy + i;
3634 0 ▶ if (rcpy->len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rcpy->len == 0) 0 0 0
3635 0 continue;
3636 0 ▶ if (cpos < rcpy->tgt) {
Branch: 1 branch
BranchTrueFalseTotal
if (cpos < rcpy->tgt) { 0 0 0
3637 0 size = rcpy->tgt - cpos;
3638 0 ▶ if (size > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > 255) { 0 0 0
3639 0 cpybuf[0] = XDL_BDOP_INSB;
3640 0 XDL_LE32_PUT(cpybuf + 1, size);
3641 0 mb[0].ptr = (char *) cpybuf;
3642 0 mb[0].size = XDL_INSBOP_SIZE;
3643 } else {
3644 0 cpybuf[0] = XDL_BDOP_INS;
3645 0 cpybuf[1] = (unsigned char) size;
3646 0 mb[0].ptr = (char *) cpybuf;
3647 0 mb[0].size = 2;
3648 }
3649 0 mb[1].ptr = mmb2->ptr + cpos;
3650 0 mb[1].size = size;
3651 0 ▶ if (ecb->outf(ecb->priv, mb, 2) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, 2) < 0) { 0 0 0
3652 0 xrab_free_cpyarena(&aca);
3653 0 return -1;
3654 }
3655 0 cpos = rcpy->tgt;
3656 }
3657 0 cpybuf[0] = XDL_BDOP_CPY;
3658 0 XDL_LE32_PUT(cpybuf + 1, rcpy->src);
3659 0 XDL_LE32_PUT(cpybuf + 5, rcpy->len);
3660 0 mb[0].ptr = (char *) cpybuf;
3661 0 mb[0].size = XDL_COPYOP_SIZE;
3662 0 ▶ if (ecb->outf(ecb->priv, mb, 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, 1) < 0) { 0 0 0
3663 0 xrab_free_cpyarena(&aca);
3664 0 return -1;
3665 }
3666 0 cpos += rcpy->len;
3667 }
3668 0 xrab_free_cpyarena(&aca);
3669 0 ▶ if (cpos < mmb2->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (cpos < mmb2->size) { 0 0 0
3670 0 size = mmb2->size - cpos;
3671 0 ▶ if (size > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > 255) { 0 0 0
3672 0 cpybuf[0] = XDL_BDOP_INSB;
3673 0 XDL_LE32_PUT(cpybuf + 1, size);
3674 0 mb[0].ptr = (char *) cpybuf;
3675 0 mb[0].size = XDL_INSBOP_SIZE;
3676 } else {
3677 0 cpybuf[0] = XDL_BDOP_INS;
3678 0 cpybuf[1] = (unsigned char) size;
3679 0 mb[0].ptr = (char *) cpybuf;
3680 0 mb[0].size = 2;
3681 }
3682 0 mb[1].ptr = mmb2->ptr + cpos;
3683 0 mb[1].size = size;
3684 0 ▶ if (ecb->outf(ecb->priv, mb, 2) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ecb->outf(ecb->priv, mb, 2) < 0) 0 0 0
3685 0 return -1;
3686 }
3687  
3688 0 return 0;
3689 }
3690  
3691  
3692 0 int xdl_rabdiff(mmfile_t *mmf1, mmfile_t *mmf2, xdemitcb_t *ecb) {
3693 mmbuffer_t mmb1, mmb2;
3694  
3695 0 ▶ if (!xdl_mmfile_iscompact(mmf1) || !xdl_mmfile_iscompact(mmf2))
Branch: 2 branches
BranchTrueFalseTotal
if (!xdl_mmfile_iscompact(mmf1) || !xdl_mmfile_iscompact(mmf2)) 0 0 0
if (!xdl_mmfile_iscompact(mmf1) || !xdl_mmfile_iscompact(mmf2)) 0 0 0
3696 0 return -1;
3697 0 ▶ if ((mmb1.ptr = (char *) xdl_mmfile_first(mmf1, &mmb1.size)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((mmb1.ptr = (char *) xdl_mmfile_first(mmf1, &mmb1.size)) == NULL) 0 0 0
3698 0 mmb1.size = 0;
3699 0 ▶ if ((mmb2.ptr = (char *) xdl_mmfile_first(mmf2, &mmb2.size)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((mmb2.ptr = (char *) xdl_mmfile_first(mmf2, &mmb2.size)) == NULL) 0 0 0
3700 0 mmb2.size = 0;
3701  
3702 0 return xdl_rabdiff_mb(&mmb1, &mmb2, ecb);
3703 }
3704  
« blib/lib/Diff/LibXDiff.pm ↑ Summary LibXDiff.xs »