ed7ad2041c6f81df8b0191374c5b0e4e91683a70
   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 */
  22
  23#include "xinclude.h"
  24
  25
  26
  27#define XDL_MAX_COST_MIN 256
  28#define XDL_HEUR_MIN_COST 256
  29#define XDL_LINE_MAX (long)((1UL << (8 * sizeof(long) - 1)) - 1)
  30#define XDL_SNAKE_CNT 20
  31#define XDL_K_HEUR 4
  32
  33
  34
  35typedef struct s_xdpsplit {
  36        long i1, i2;
  37        int min_lo, min_hi;
  38} xdpsplit_t;
  39
  40
  41
  42
  43static long xdl_split(unsigned long const *ha1, long off1, long lim1,
  44                      unsigned long const *ha2, long off2, long lim2,
  45                      long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
  46                      xdalgoenv_t *xenv);
  47static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2);
  48static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags);
  49
  50
  51
  52
  53
  54/*
  55 * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers.
  56 * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both
  57 * the forward diagonal starting from (off1, off2) and the backward diagonal
  58 * starting from (lim1, lim2). If the K values on the same diagonal crosses
  59 * returns the furthest point of reach. We might end up having to expensive
  60 * cases using this algorithm is full, so a little bit of heuristic is needed
  61 * to cut the search and to return a suboptimal point.
  62 */
  63static long xdl_split(unsigned long const *ha1, long off1, long lim1,
  64                      unsigned long const *ha2, long off2, long lim2,
  65                      long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
  66                      xdalgoenv_t *xenv) {
  67        long dmin = off1 - lim2, dmax = lim1 - off2;
  68        long fmid = off1 - off2, bmid = lim1 - lim2;
  69        long odd = (fmid - bmid) & 1;
  70        long fmin = fmid, fmax = fmid;
  71        long bmin = bmid, bmax = bmid;
  72        long ec, d, i1, i2, prev1, best, dd, v, k;
  73
  74        /*
  75         * Set initial diagonal values for both forward and backward path.
  76         */
  77        kvdf[fmid] = off1;
  78        kvdb[bmid] = lim1;
  79
  80        for (ec = 1;; ec++) {
  81                int got_snake = 0;
  82
  83                /*
  84                 * We need to extent the diagonal "domain" by one. If the next
  85                 * values exits the box boundaries we need to change it in the
  86                 * opposite direction because (max - min) must be a power of two.
  87                 * Also we initialize the extenal K value to -1 so that we can
  88                 * avoid extra conditions check inside the core loop.
  89                 */
  90                if (fmin > dmin)
  91                        kvdf[--fmin - 1] = -1;
  92                else
  93                        ++fmin;
  94                if (fmax < dmax)
  95                        kvdf[++fmax + 1] = -1;
  96                else
  97                        --fmax;
  98
  99                for (d = fmax; d >= fmin; d -= 2) {
 100                        if (kvdf[d - 1] >= kvdf[d + 1])
 101                                i1 = kvdf[d - 1] + 1;
 102                        else
 103                                i1 = kvdf[d + 1];
 104                        prev1 = i1;
 105                        i2 = i1 - d;
 106                        for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++);
 107                        if (i1 - prev1 > xenv->snake_cnt)
 108                                got_snake = 1;
 109                        kvdf[d] = i1;
 110                        if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) {
 111                                spl->i1 = i1;
 112                                spl->i2 = i2;
 113                                spl->min_lo = spl->min_hi = 1;
 114                                return ec;
 115                        }
 116                }
 117
 118                /*
 119                 * We need to extent the diagonal "domain" by one. If the next
 120                 * values exits the box boundaries we need to change it in the
 121                 * opposite direction because (max - min) must be a power of two.
 122                 * Also we initialize the extenal K value to -1 so that we can
 123                 * avoid extra conditions check inside the core loop.
 124                 */
 125                if (bmin > dmin)
 126                        kvdb[--bmin - 1] = XDL_LINE_MAX;
 127                else
 128                        ++bmin;
 129                if (bmax < dmax)
 130                        kvdb[++bmax + 1] = XDL_LINE_MAX;
 131                else
 132                        --bmax;
 133
 134                for (d = bmax; d >= bmin; d -= 2) {
 135                        if (kvdb[d - 1] < kvdb[d + 1])
 136                                i1 = kvdb[d - 1];
 137                        else
 138                                i1 = kvdb[d + 1] - 1;
 139                        prev1 = i1;
 140                        i2 = i1 - d;
 141                        for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--);
 142                        if (prev1 - i1 > xenv->snake_cnt)
 143                                got_snake = 1;
 144                        kvdb[d] = i1;
 145                        if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) {
 146                                spl->i1 = i1;
 147                                spl->i2 = i2;
 148                                spl->min_lo = spl->min_hi = 1;
 149                                return ec;
 150                        }
 151                }
 152
 153                if (need_min)
 154                        continue;
 155
 156                /*
 157                 * If the edit cost is above the heuristic trigger and if
 158                 * we got a good snake, we sample current diagonals to see
 159                 * if some of the, have reached an "interesting" path. Our
 160                 * measure is a function of the distance from the diagonal
 161                 * corner (i1 + i2) penalized with the distance from the
 162                 * mid diagonal itself. If this value is above the current
 163                 * edit cost times a magic factor (XDL_K_HEUR) we consider
 164                 * it interesting.
 165                 */
 166                if (got_snake && ec > xenv->heur_min) {
 167                        for (best = 0, d = fmax; d >= fmin; d -= 2) {
 168                                dd = d > fmid ? d - fmid: fmid - d;
 169                                i1 = kvdf[d];
 170                                i2 = i1 - d;
 171                                v = (i1 - off1) + (i2 - off2) - dd;
 172
 173                                if (v > XDL_K_HEUR * ec && v > best &&
 174                                    off1 + xenv->snake_cnt <= i1 && i1 < lim1 &&
 175                                    off2 + xenv->snake_cnt <= i2 && i2 < lim2) {
 176                                        for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++)
 177                                                if (k == xenv->snake_cnt) {
 178                                                        best = v;
 179                                                        spl->i1 = i1;
 180                                                        spl->i2 = i2;
 181                                                        break;
 182                                                }
 183                                }
 184                        }
 185                        if (best > 0) {
 186                                spl->min_lo = 1;
 187                                spl->min_hi = 0;
 188                                return ec;
 189                        }
 190
 191                        for (best = 0, d = bmax; d >= bmin; d -= 2) {
 192                                dd = d > bmid ? d - bmid: bmid - d;
 193                                i1 = kvdb[d];
 194                                i2 = i1 - d;
 195                                v = (lim1 - i1) + (lim2 - i2) - dd;
 196
 197                                if (v > XDL_K_HEUR * ec && v > best &&
 198                                    off1 < i1 && i1 <= lim1 - xenv->snake_cnt &&
 199                                    off2 < i2 && i2 <= lim2 - xenv->snake_cnt) {
 200                                        for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++)
 201                                                if (k == xenv->snake_cnt - 1) {
 202                                                        best = v;
 203                                                        spl->i1 = i1;
 204                                                        spl->i2 = i2;
 205                                                        break;
 206                                                }
 207                                }
 208                        }
 209                        if (best > 0) {
 210                                spl->min_lo = 0;
 211                                spl->min_hi = 1;
 212                                return ec;
 213                        }
 214                }
 215
 216                /*
 217                 * Enough is enough. We spent too much time here and now we collect
 218                 * the furthest reaching path using the (i1 + i2) measure.
 219                 */
 220                if (ec >= xenv->mxcost) {
 221                        long fbest, fbest1, bbest, bbest1;
 222
 223                        fbest = fbest1 = -1;
 224                        for (d = fmax; d >= fmin; d -= 2) {
 225                                i1 = XDL_MIN(kvdf[d], lim1);
 226                                i2 = i1 - d;
 227                                if (lim2 < i2)
 228                                        i1 = lim2 + d, i2 = lim2;
 229                                if (fbest < i1 + i2) {
 230                                        fbest = i1 + i2;
 231                                        fbest1 = i1;
 232                                }
 233                        }
 234
 235                        bbest = bbest1 = XDL_LINE_MAX;
 236                        for (d = bmax; d >= bmin; d -= 2) {
 237                                i1 = XDL_MAX(off1, kvdb[d]);
 238                                i2 = i1 - d;
 239                                if (i2 < off2)
 240                                        i1 = off2 + d, i2 = off2;
 241                                if (i1 + i2 < bbest) {
 242                                        bbest = i1 + i2;
 243                                        bbest1 = i1;
 244                                }
 245                        }
 246
 247                        if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) {
 248                                spl->i1 = fbest1;
 249                                spl->i2 = fbest - fbest1;
 250                                spl->min_lo = 1;
 251                                spl->min_hi = 0;
 252                        } else {
 253                                spl->i1 = bbest1;
 254                                spl->i2 = bbest - bbest1;
 255                                spl->min_lo = 0;
 256                                spl->min_hi = 1;
 257                        }
 258                        return ec;
 259                }
 260        }
 261
 262        return -1;
 263}
 264
 265
 266/*
 267 * Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling
 268 * the box splitting function. Note that the real job (marking changed lines)
 269 * is done in the two boundary reaching checks.
 270 */
 271int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,
 272                 diffdata_t *dd2, long off2, long lim2,
 273                 long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) {
 274        unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha;
 275
 276        /*
 277         * Shrink the box by walking through each diagonal snake (SW and NE).
 278         */
 279        for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++);
 280        for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--);
 281
 282        /*
 283         * If one dimension is empty, then all records on the other one must
 284         * be obviously changed.
 285         */
 286        if (off1 == lim1) {
 287                char *rchg2 = dd2->rchg;
 288                long *rindex2 = dd2->rindex;
 289
 290                for (; off2 < lim2; off2++)
 291                        rchg2[rindex2[off2]] = 1;
 292        } else if (off2 == lim2) {
 293                char *rchg1 = dd1->rchg;
 294                long *rindex1 = dd1->rindex;
 295
 296                for (; off1 < lim1; off1++)
 297                        rchg1[rindex1[off1]] = 1;
 298        } else {
 299                long ec;
 300                xdpsplit_t spl;
 301                spl.i1 = spl.i2 = 0;
 302
 303                /*
 304                 * Divide ...
 305                 */
 306                if ((ec = xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb,
 307                                    need_min, &spl, xenv)) < 0) {
 308
 309                        return -1;
 310                }
 311
 312                /*
 313                 * ... et Impera.
 314                 */
 315                if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2,
 316                                 kvdf, kvdb, spl.min_lo, xenv) < 0 ||
 317                    xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2,
 318                                 kvdf, kvdb, spl.min_hi, xenv) < 0) {
 319
 320                        return -1;
 321                }
 322        }
 323
 324        return 0;
 325}
 326
 327
 328int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 329                xdfenv_t *xe) {
 330        long ndiags;
 331        long *kvd, *kvdf, *kvdb;
 332        xdalgoenv_t xenv;
 333        diffdata_t dd1, dd2;
 334
 335        if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
 336
 337                return -1;
 338        }
 339
 340        /*
 341         * Allocate and setup K vectors to be used by the differential algorithm.
 342         * One is to store the forward path and one to store the backward path.
 343         */
 344        ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
 345        if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
 346
 347                xdl_free_env(xe);
 348                return -1;
 349        }
 350        kvdf = kvd;
 351        kvdb = kvdf + ndiags;
 352        kvdf += xe->xdf2.nreff + 1;
 353        kvdb += xe->xdf2.nreff + 1;
 354
 355        xenv.mxcost = xdl_bogosqrt(ndiags);
 356        if (xenv.mxcost < XDL_MAX_COST_MIN)
 357                xenv.mxcost = XDL_MAX_COST_MIN;
 358        xenv.snake_cnt = XDL_SNAKE_CNT;
 359        xenv.heur_min = XDL_HEUR_MIN_COST;
 360
 361        dd1.nrec = xe->xdf1.nreff;
 362        dd1.ha = xe->xdf1.ha;
 363        dd1.rchg = xe->xdf1.rchg;
 364        dd1.rindex = xe->xdf1.rindex;
 365        dd2.nrec = xe->xdf2.nreff;
 366        dd2.ha = xe->xdf2.ha;
 367        dd2.rchg = xe->xdf2.rchg;
 368        dd2.rindex = xe->xdf2.rindex;
 369
 370        if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec,
 371                         kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) {
 372
 373                xdl_free(kvd);
 374                xdl_free_env(xe);
 375                return -1;
 376        }
 377
 378        xdl_free(kvd);
 379
 380        return 0;
 381}
 382
 383
 384static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) {
 385        xdchange_t *xch;
 386
 387        if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t))))
 388                return NULL;
 389
 390        xch->next = xscr;
 391        xch->i1 = i1;
 392        xch->i2 = i2;
 393        xch->chg1 = chg1;
 394        xch->chg2 = chg2;
 395
 396        return xch;
 397}
 398
 399
 400static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
 401        long ix, ixo, ixs, ixref, grpsiz, nrec = xdf->nrec;
 402        char *rchg = xdf->rchg, *rchgo = xdfo->rchg;
 403        xrecord_t **recs = xdf->recs;
 404
 405        /*
 406         * This is the same of what GNU diff does. Move back and forward
 407         * change groups for a consistent and pretty diff output. This also
 408         * helps in finding joineable change groups and reduce the diff size.
 409         */
 410        for (ix = ixo = 0;;) {
 411                /*
 412                 * Find the first changed line in the to-be-compacted file.
 413                 * We need to keep track of both indexes, so if we find a
 414                 * changed lines group on the other file, while scanning the
 415                 * to-be-compacted file, we need to skip it properly. Note
 416                 * that loops that are testing for changed lines on rchg* do
 417                 * not need index bounding since the array is prepared with
 418                 * a zero at position -1 and N.
 419                 */
 420                for (; ix < nrec && !rchg[ix]; ix++)
 421                        while (rchgo[ixo++]);
 422                if (ix == nrec)
 423                        break;
 424
 425                /*
 426                 * Record the start of a changed-group in the to-be-compacted file
 427                 * and find the end of it, on both to-be-compacted and other file
 428                 * indexes (ix and ixo).
 429                 */
 430                ixs = ix;
 431                for (ix++; rchg[ix]; ix++);
 432                for (; rchgo[ixo]; ixo++);
 433
 434                do {
 435                        grpsiz = ix - ixs;
 436
 437                        /*
 438                         * If the line before the current change group, is equal to
 439                         * the last line of the current change group, shift backward
 440                         * the group.
 441                         */
 442                        while (ixs > 0 && recs[ixs - 1]->ha == recs[ix - 1]->ha &&
 443                               xdl_recmatch(recs[ixs - 1]->ptr, recs[ixs - 1]->size, recs[ix - 1]->ptr, recs[ix - 1]->size, flags)) {
 444                                rchg[--ixs] = 1;
 445                                rchg[--ix] = 0;
 446
 447                                /*
 448                                 * This change might have joined two change groups,
 449                                 * so we try to take this scenario in account by moving
 450                                 * the start index accordingly (and so the other-file
 451                                 * end-of-group index).
 452                                 */
 453                                for (; rchg[ixs - 1]; ixs--);
 454                                while (rchgo[--ixo]);
 455                        }
 456
 457                        /*
 458                         * Record the end-of-group position in case we are matched
 459                         * with a group of changes in the other file (that is, the
 460                         * change record before the enf-of-group index in the other
 461                         * file is set).
 462                         */
 463                        ixref = rchgo[ixo - 1] ? ix: nrec;
 464
 465                        /*
 466                         * If the first line of the current change group, is equal to
 467                         * the line next of the current change group, shift forward
 468                         * the group.
 469                         */
 470                        while (ix < nrec && recs[ixs]->ha == recs[ix]->ha &&
 471                               xdl_recmatch(recs[ixs]->ptr, recs[ixs]->size, recs[ix]->ptr, recs[ix]->size, flags)) {
 472                                rchg[ixs++] = 0;
 473                                rchg[ix++] = 1;
 474
 475                                /*
 476                                 * This change might have joined two change groups,
 477                                 * so we try to take this scenario in account by moving
 478                                 * the start index accordingly (and so the other-file
 479                                 * end-of-group index). Keep tracking the reference
 480                                 * index in case we are shifting together with a
 481                                 * corresponding group of changes in the other file.
 482                                 */
 483                                for (; rchg[ix]; ix++);
 484                                while (rchgo[++ixo])
 485                                        ixref = ix;
 486                        }
 487                } while (grpsiz != ix - ixs);
 488
 489                /*
 490                 * Try to move back the possibly merged group of changes, to match
 491                 * the recorded postion in the other file.
 492                 */
 493                while (ixref < ix) {
 494                        rchg[--ixs] = 1;
 495                        rchg[--ix] = 0;
 496                        while (rchgo[--ixo]);
 497                }
 498        }
 499
 500        return 0;
 501}
 502
 503
 504int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) {
 505        xdchange_t *cscr = NULL, *xch;
 506        char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg;
 507        long i1, i2, l1, l2;
 508
 509        /*
 510         * Trivial. Collects "groups" of changes and creates an edit script.
 511         */
 512        for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--)
 513                if (rchg1[i1 - 1] || rchg2[i2 - 1]) {
 514                        for (l1 = i1; rchg1[i1 - 1]; i1--);
 515                        for (l2 = i2; rchg2[i2 - 1]; i2--);
 516
 517                        if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) {
 518                                xdl_free_script(cscr);
 519                                return -1;
 520                        }
 521                        cscr = xch;
 522                }
 523
 524        *xscr = cscr;
 525
 526        return 0;
 527}
 528
 529
 530void xdl_free_script(xdchange_t *xscr) {
 531        xdchange_t *xch;
 532
 533        while ((xch = xscr) != NULL) {
 534                xscr = xscr->next;
 535                xdl_free(xch);
 536        }
 537}
 538
 539
 540int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 541             xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
 542        xdchange_t *xscr;
 543        xdfenv_t xe;
 544
 545        if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
 546
 547                return -1;
 548        }
 549        if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
 550            xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
 551            xdl_build_script(&xe, &xscr) < 0) {
 552
 553                xdl_free_env(&xe);
 554                return -1;
 555        }
 556        if (xscr) {
 557                if (xdl_emit_diff(&xe, xscr, ecb, xecfg) < 0) {
 558
 559                        xdl_free_script(xscr);
 560                        xdl_free_env(&xe);
 561                        return -1;
 562                }
 563                xdl_free_script(xscr);
 564        }
 565        xdl_free_env(&xe);
 566
 567        return 0;
 568}
 569