1/*
2* LibXDiff by Davide Libenzi ( File Differential Library )
3* Copyright (C) 2003 Davide Libenzi
4*
5* This library is free software; you can redistribute it and/or
6* modify it under the terms of the GNU Lesser General Public
7* License as published by the Free Software Foundation; either
8* version 2.1 of the License, or (at your option) any later version.
9*
10* This library is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13* Lesser General Public License for more details.
14*
15* You should have received a copy of the GNU Lesser General Public
16* License along with this library; if not, write to the Free Software
17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*
19* Davide Libenzi <davidel@xmailserver.org>
20*
21*/
2223
#include "xinclude.h"
2425
26
27
#define XDL_MAX_COST_MIN 256
28#define XDL_HEUR_MIN_COST 256
29#define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1)
30#define XDL_SNAKE_CNT 20
31#define XDL_K_HEUR 4
3233
34
35
typedef struct s_xdpsplit {
36long i1, i2;
37int min_lo, min_hi;
38} xdpsplit_t;
3940
41
42
43
static long xdl_split(unsigned long const *ha1, long off1, long lim1,
44unsigned long const *ha2, long off2, long lim2,
45long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
46xdalgoenv_t *xenv);
47static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2);
4849
50
51
52
53
/*
54* See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers.
55* Basically considers a "box" (off1, off2, lim1, lim2) and scan from both
56* the forward diagonal starting from (off1, off2) and the backward diagonal
57* starting from (lim1, lim2). If the K values on the same diagonal crosses
58* returns the furthest point of reach. We might end up having to expensive
59* cases using this algorithm is full, so a little bit of heuristic is needed
60* to cut the search and to return a suboptimal point.
61*/
62static long xdl_split(unsigned long const *ha1, long off1, long lim1,
63unsigned long const *ha2, long off2, long lim2,
64long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
65xdalgoenv_t *xenv) {
66long dmin = off1 - lim2, dmax = lim1 - off2;
67long fmid = off1 - off2, bmid = lim1 - lim2;
68long odd = (fmid - bmid) & 1;
69long fmin = fmid, fmax = fmid;
70long bmin = bmid, bmax = bmid;
71long ec, d, i1, i2, prev1, best, dd, v, k;
7273
/*
74* Set initial diagonal values for both forward and backward path.
75*/
76kvdf[fmid] = off1;
77kvdb[bmid] = lim1;
7879
for (ec = 1;; ec++) {
80int got_snake = 0;
8182
/*
83* We need to extent the diagonal "domain" by one. If the next
84* values exits the box boundaries we need to change it in the
85* opposite direction because (max - min) must be a power of two.
86* Also we initialize the external K value to -1 so that we can
87* avoid extra conditions check inside the core loop.
88*/
89if (fmin > dmin)
90kvdf[--fmin - 1] = -1;
91else
92++fmin;
93if (fmax < dmax)
94kvdf[++fmax + 1] = -1;
95else
96--fmax;
9798
for (d = fmax; d >= fmin; d -= 2) {
99if (kvdf[d - 1] >= kvdf[d + 1])
100i1 = kvdf[d - 1] + 1;
101else
102i1 = kvdf[d + 1];
103prev1 = i1;
104i2 = i1 - d;
105for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++);
106if (i1 - prev1 > xenv->snake_cnt)
107got_snake = 1;
108kvdf[d] = i1;
109if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) {
110spl->i1 = i1;
111spl->i2 = i2;
112spl->min_lo = spl->min_hi = 1;
113return ec;
114}
115}
116117
/*
118* We need to extent the diagonal "domain" by one. If the next
119* values exits the box boundaries we need to change it in the
120* opposite direction because (max - min) must be a power of two.
121* Also we initialize the external K value to -1 so that we can
122* avoid extra conditions check inside the core loop.
123*/
124if (bmin > dmin)
125kvdb[--bmin - 1] = XDL_LINE_MAX;
126else
127++bmin;
128if (bmax < dmax)
129kvdb[++bmax + 1] = XDL_LINE_MAX;
130else
131--bmax;
132133
for (d = bmax; d >= bmin; d -= 2) {
134if (kvdb[d - 1] < kvdb[d + 1])
135i1 = kvdb[d - 1];
136else
137i1 = kvdb[d + 1] - 1;
138prev1 = i1;
139i2 = i1 - d;
140for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--);
141if (prev1 - i1 > xenv->snake_cnt)
142got_snake = 1;
143kvdb[d] = i1;
144if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) {
145spl->i1 = i1;
146spl->i2 = i2;
147spl->min_lo = spl->min_hi = 1;
148return ec;
149}
150}
151152
if (need_min)
153continue;
154155
/*
156* If the edit cost is above the heuristic trigger and if
157* we got a good snake, we sample current diagonals to see
158* if some of the, have reached an "interesting" path. Our
159* measure is a function of the distance from the diagonal
160* corner (i1 + i2) penalized with the distance from the
161* mid diagonal itself. If this value is above the current
162* edit cost times a magic factor (XDL_K_HEUR) we consider
163* it interesting.
164*/
165if (got_snake && ec > xenv->heur_min) {
166for (best = 0, d = fmax; d >= fmin; d -= 2) {
167dd = d > fmid ? d - fmid: fmid - d;
168i1 = kvdf[d];
169i2 = i1 - d;
170v = (i1 - off1) + (i2 - off2) - dd;
171172
if (v > XDL_K_HEUR * ec && v > best &&
173off1 + xenv->snake_cnt <= i1 && i1 < lim1 &&
174off2 + xenv->snake_cnt <= i2 && i2 < lim2) {
175for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++)
176if (k == xenv->snake_cnt) {
177best = v;
178spl->i1 = i1;
179spl->i2 = i2;
180break;
181}
182}
183}
184if (best > 0) {
185spl->min_lo = 1;
186spl->min_hi = 0;
187return ec;
188}
189190
for (best = 0, d = bmax; d >= bmin; d -= 2) {
191dd = d > bmid ? d - bmid: bmid - d;
192i1 = kvdb[d];
193i2 = i1 - d;
194v = (lim1 - i1) + (lim2 - i2) - dd;
195196
if (v > XDL_K_HEUR * ec && v > best &&
197off1 < i1 && i1 <= lim1 - xenv->snake_cnt &&
198off2 < i2 && i2 <= lim2 - xenv->snake_cnt) {
199for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++)
200if (k == xenv->snake_cnt - 1) {
201best = v;
202spl->i1 = i1;
203spl->i2 = i2;
204break;
205}
206}
207}
208if (best > 0) {
209spl->min_lo = 0;
210spl->min_hi = 1;
211return ec;
212}
213}
214215
/*
216* Enough is enough. We spent too much time here and now we collect
217* the furthest reaching path using the (i1 + i2) measure.
218*/
219if (ec >= xenv->mxcost) {
220long fbest, fbest1, bbest, bbest1;
221222
fbest = fbest1 = -1;
223for (d = fmax; d >= fmin; d -= 2) {
224i1 = XDL_MIN(kvdf[d], lim1);
225i2 = i1 - d;
226if (lim2 < i2)
227i1 = lim2 + d, i2 = lim2;
228if (fbest < i1 + i2) {
229fbest = i1 + i2;
230fbest1 = i1;
231}
232}
233234
bbest = bbest1 = XDL_LINE_MAX;
235for (d = bmax; d >= bmin; d -= 2) {
236i1 = XDL_MAX(off1, kvdb[d]);
237i2 = i1 - d;
238if (i2 < off2)
239i1 = off2 + d, i2 = off2;
240if (i1 + i2 < bbest) {
241bbest = i1 + i2;
242bbest1 = i1;
243}
244}
245246
if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) {
247spl->i1 = fbest1;
248spl->i2 = fbest - fbest1;
249spl->min_lo = 1;
250spl->min_hi = 0;
251} else {
252spl->i1 = bbest1;
253spl->i2 = bbest - bbest1;
254spl->min_lo = 0;
255spl->min_hi = 1;
256}
257return ec;
258}
259}
260}
261262
263
/*
264* Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling
265* the box splitting function. Note that the real job (marking changed lines)
266* is done in the two boundary reaching checks.
267*/
268int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,
269diffdata_t *dd2, long off2, long lim2,
270long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) {
271unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha;
272273
/*
274* Shrink the box by walking through each diagonal snake (SW and NE).
275*/
276for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++);
277for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--);
278279
/*
280* If one dimension is empty, then all records on the other one must
281* be obviously changed.
282*/
283if (off1 == lim1) {
284char *rchg2 = dd2->rchg;
285long *rindex2 = dd2->rindex;
286287
for (; off2 < lim2; off2++)
288rchg2[rindex2[off2]] = 1;
289} else if (off2 == lim2) {
290char *rchg1 = dd1->rchg;
291long *rindex1 = dd1->rindex;
292293
for (; off1 < lim1; off1++)
294rchg1[rindex1[off1]] = 1;
295} else {
296xdpsplit_t spl;
297spl.i1 = spl.i2 = 0;
298299
/*
300* Divide ...
301*/
302if (xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb,
303need_min, &spl, xenv) < 0) {
304305
return -1;
306}
307308
/*
309* ... et Impera.
310*/
311if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2,
312kvdf, kvdb, spl.min_lo, xenv) < 0 ||
313xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2,
314kvdf, kvdb, spl.min_hi, xenv) < 0) {
315316
return -1;
317}
318}
319320
return 0;
321}
322323
324
int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
325xdfenv_t *xe) {
326long ndiags;
327long *kvd, *kvdf, *kvdb;
328xdalgoenv_t xenv;
329diffdata_t dd1, dd2;
330331
if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF)
332return xdl_do_patience_diff(mf1, mf2, xpp, xe);
333334
if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF)
335return xdl_do_histogram_diff(mf1, mf2, xpp, xe);
336337
if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
338339
return -1;
340}
341342
/*
343* Allocate and setup K vectors to be used by the differential algorithm.
344* One is to store the forward path and one to store the backward path.
345*/
346ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
347if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
348349
xdl_free_env(xe);
350return -1;
351}
352kvdf = kvd;
353kvdb = kvdf + ndiags;
354kvdf += xe->xdf2.nreff + 1;
355kvdb += xe->xdf2.nreff + 1;
356357
xenv.mxcost = xdl_bogosqrt(ndiags);
358if (xenv.mxcost < XDL_MAX_COST_MIN)
359xenv.mxcost = XDL_MAX_COST_MIN;
360xenv.snake_cnt = XDL_SNAKE_CNT;
361xenv.heur_min = XDL_HEUR_MIN_COST;
362363
dd1.nrec = xe->xdf1.nreff;
364dd1.ha = xe->xdf1.ha;
365dd1.rchg = xe->xdf1.rchg;
366dd1.rindex = xe->xdf1.rindex;
367dd2.nrec = xe->xdf2.nreff;
368dd2.ha = xe->xdf2.ha;
369dd2.rchg = xe->xdf2.rchg;
370dd2.rindex = xe->xdf2.rindex;
371372
if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec,
373kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) {
374375
xdl_free(kvd);
376xdl_free_env(xe);
377return -1;
378}
379380
xdl_free(kvd);
381382
return 0;
383}
384385
386
static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) {
387xdchange_t *xch;
388389
if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t))))
390return NULL;
391392
xch->next = xscr;
393xch->i1 = i1;
394xch->i2 = i2;
395xch->chg1 = chg1;
396xch->chg2 = chg2;
397xch->ignore = 0;
398399
return xch;
400}
401402
403
static int is_blank_line(xrecord_t **recs, long ix, long flags)
404{
405return xdl_blankline(recs[ix]->ptr, recs[ix]->size, flags);
406}
407408
static int recs_match(xrecord_t **recs, long ixs, long ix, long flags)
409{
410return (recs[ixs]->ha == recs[ix]->ha &&
411xdl_recmatch(recs[ixs]->ptr, recs[ixs]->size,
412recs[ix]->ptr, recs[ix]->size,
413flags));
414}
415416
int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
417long ix, ixo, ixs, ixref, grpsiz, nrec = xdf->nrec;
418char *rchg = xdf->rchg, *rchgo = xdfo->rchg;
419unsigned int blank_lines;
420xrecord_t **recs = xdf->recs;
421422
/*
423* This is the same of what GNU diff does. Move back and forward
424* change groups for a consistent and pretty diff output. This also
425* helps in finding joinable change groups and reduce the diff size.
426*/
427for (ix = ixo = 0;;) {
428/*
429* Find the first changed line in the to-be-compacted file.
430* We need to keep track of both indexes, so if we find a
431* changed lines group on the other file, while scanning the
432* to-be-compacted file, we need to skip it properly. Note
433* that loops that are testing for changed lines on rchg* do
434* not need index bounding since the array is prepared with
435* a zero at position -1 and N.
436*/
437for (; ix < nrec && !rchg[ix]; ix++)
438while (rchgo[ixo++]);
439if (ix == nrec)
440break;
441442
/*
443* Record the start of a changed-group in the to-be-compacted file
444* and find the end of it, on both to-be-compacted and other file
445* indexes (ix and ixo).
446*/
447ixs = ix;
448for (ix++; rchg[ix]; ix++);
449for (; rchgo[ixo]; ixo++);
450451
do {
452grpsiz = ix - ixs;
453blank_lines = 0;
454455
/*
456* If the line before the current change group, is equal to
457* the last line of the current change group, shift backward
458* the group.
459*/
460while (ixs > 0 && recs_match(recs, ixs - 1, ix - 1, flags)) {
461rchg[--ixs] = 1;
462rchg[--ix] = 0;
463464
/*
465* This change might have joined two change groups,
466* so we try to take this scenario in account by moving
467* the start index accordingly (and so the other-file
468* end-of-group index).
469*/
470for (; rchg[ixs - 1]; ixs--);
471while (rchgo[--ixo]);
472}
473474
/*
475* Record the end-of-group position in case we are matched
476* with a group of changes in the other file (that is, the
477* change record before the end-of-group index in the other
478* file is set).
479*/
480ixref = rchgo[ixo - 1] ? ix: nrec;
481482
/*
483* If the first line of the current change group, is equal to
484* the line next of the current change group, shift forward
485* the group.
486*/
487while (ix < nrec && recs_match(recs, ixs, ix, flags)) {
488blank_lines += is_blank_line(recs, ix, flags);
489490
rchg[ixs++] = 0;
491rchg[ix++] = 1;
492493
/*
494* This change might have joined two change groups,
495* so we try to take this scenario in account by moving
496* the start index accordingly (and so the other-file
497* end-of-group index). Keep tracking the reference
498* index in case we are shifting together with a
499* corresponding group of changes in the other file.
500*/
501for (; rchg[ix]; ix++);
502while (rchgo[++ixo])
503ixref = ix;
504}
505} while (grpsiz != ix - ixs);
506507
/*
508* Try to move back the possibly merged group of changes, to match
509* the recorded position in the other file.
510*/
511while (ixref < ix) {
512rchg[--ixs] = 1;
513rchg[--ix] = 0;
514while (rchgo[--ixo]);
515}
516517
/*
518* If a group can be moved back and forth, see if there is a
519* blank line in the moving space. If there is a blank line,
520* make sure the last blank line is the end of the group.
521*
522* As we already shifted the group forward as far as possible
523* in the earlier loop, we need to shift it back only if at all.
524*/
525if ((flags & XDF_COMPACTION_HEURISTIC) && blank_lines) {
526while (ixs > 0 &&
527!is_blank_line(recs, ix - 1, flags) &&
528recs_match(recs, ixs - 1, ix - 1, flags)) {
529rchg[--ixs] = 1;
530rchg[--ix] = 0;
531while (rchgo[--ixo]);
532}
533}
534}
535536
return 0;
537}
538539
540
int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) {
541xdchange_t *cscr = NULL, *xch;
542char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg;
543long i1, i2, l1, l2;
544545
/*
546* Trivial. Collects "groups" of changes and creates an edit script.
547*/
548for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--)
549if (rchg1[i1 - 1] || rchg2[i2 - 1]) {
550for (l1 = i1; rchg1[i1 - 1]; i1--);
551for (l2 = i2; rchg2[i2 - 1]; i2--);
552553
if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) {
554xdl_free_script(cscr);
555return -1;
556}
557cscr = xch;
558}
559560
*xscr = cscr;
561562
return 0;
563}
564565
566
void xdl_free_script(xdchange_t *xscr) {
567xdchange_t *xch;
568569
while ((xch = xscr) != NULL) {
570xscr = xscr->next;
571xdl_free(xch);
572}
573}
574575
static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
576xdemitconf_t const *xecfg)
577{
578xdchange_t *xch, *xche;
579580
for (xch = xscr; xch; xch = xche->next) {
581xche = xdl_get_hunk(&xch, xecfg);
582if (!xch)
583break;
584if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1,
585xch->i2, xche->i2 + xche->chg2 - xch->i2,
586ecb->priv) < 0)
587return -1;
588}
589return 0;
590}
591592
static void xdl_mark_ignorable(xdchange_t *xscr, xdfenv_t *xe, long flags)
593{
594xdchange_t *xch;
595596
for (xch = xscr; xch; xch = xch->next) {
597int ignore = 1;
598xrecord_t **rec;
599long i;
600601
rec = &xe->xdf1.recs[xch->i1];
602for (i = 0; i < xch->chg1 && ignore; i++)
603ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags);
604605
rec = &xe->xdf2.recs[xch->i2];
606for (i = 0; i < xch->chg2 && ignore; i++)
607ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags);
608609
xch->ignore = ignore;
610}
611}
612613
int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
614xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
615xdchange_t *xscr;
616xdfenv_t xe;
617emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff;
618619
if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
620621
return -1;
622}
623if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
624xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
625xdl_build_script(&xe, &xscr) < 0) {
626627
xdl_free_env(&xe);
628return -1;
629}
630if (xscr) {
631if (xpp->flags & XDF_IGNORE_BLANK_LINES)
632xdl_mark_ignorable(xscr, &xe, xpp->flags);
633634
if (ef(&xe, xscr, ecb, xecfg) < 0) {
635636
xdl_free_script(xscr);
637xdl_free_env(&xe);
638return -1;
639}
640xdl_free_script(xscr);
641}
642xdl_free_env(&xe);
643644
return 0;
645}