Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/xdiff/xprepare.c
Line
Count
Source
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, see
17
 *  <http://www.gnu.org/licenses/>.
18
 *
19
 *  Davide Libenzi <davidel@xmailserver.org>
20
 *
21
 */
22
23
#include "xinclude.h"
24
25
26
0
#define XDL_KPDIS_RUN 4
27
0
#define XDL_MAX_EQLIMIT 1024
28
0
#define XDL_SIMSCAN_WINDOW 100
29
0
#define XDL_GUESS_NLINES1 256
30
0
#define XDL_GUESS_NLINES2 20
31
32
0
#define DISCARD 0
33
0
#define KEEP 1
34
0
#define INVESTIGATE 2
35
36
typedef struct s_xdlclass {
37
  uint64_t line_hash;
38
  struct s_xdlclass *next;
39
  const uint8_t *ptr;
40
  size_t size;
41
  long idx;
42
  long len1, len2;
43
} xdlclass_t;
44
45
typedef struct s_xdlclassifier {
46
  unsigned int hbits;
47
  long hsize;
48
  xdlclass_t **rchash;
49
  chastore_t ncha;
50
  xdlclass_t **rcrecs;
51
  long alloc;
52
  long count;
53
  long flags;
54
} xdlclassifier_t;
55
56
57
58
59
0
static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
60
0
  cf->flags = flags;
61
62
0
  cf->hbits = xdl_hashbits((unsigned int) size);
63
0
  cf->hsize = 1 << cf->hbits;
64
65
0
  if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
66
67
0
    return -1;
68
0
  }
69
0
  if (!XDL_CALLOC_ARRAY(cf->rchash, cf->hsize)) {
70
71
0
    xdl_cha_free(&cf->ncha);
72
0
    return -1;
73
0
  }
74
75
0
  cf->alloc = size;
76
0
  if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
77
78
0
    xdl_free(cf->rchash);
79
0
    xdl_cha_free(&cf->ncha);
80
0
    return -1;
81
0
  }
82
83
0
  cf->count = 0;
84
85
0
  return 0;
86
0
}
87
88
89
0
static void xdl_free_classifier(xdlclassifier_t *cf) {
90
91
0
  xdl_free(cf->rcrecs);
92
0
  xdl_free(cf->rchash);
93
0
  xdl_cha_free(&cf->ncha);
94
0
}
95
96
97
static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t *rec,
98
0
             uint64_t line_hash) {
99
0
  size_t hi;
100
0
  xdlclass_t *rcrec;
101
102
0
  hi = XDL_HASHLONG(line_hash, cf->hbits);
103
0
  for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
104
0
    if (rcrec->line_hash == line_hash &&
105
0
        xdl_recmatch((const char *)rcrec->ptr, (long)rcrec->size,
106
0
          (const char *)rec->ptr, (long)rec->size, cf->flags))
107
0
      break;
108
109
0
  if (!rcrec) {
110
0
    if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
111
112
0
      return -1;
113
0
    }
114
0
    rcrec->idx = cf->count++;
115
0
    if (XDL_ALLOC_GROW(cf->rcrecs, cf->count, cf->alloc))
116
0
        return -1;
117
0
    cf->rcrecs[rcrec->idx] = rcrec;
118
0
    rcrec->line_hash = line_hash;
119
0
    rcrec->ptr = rec->ptr;
120
0
    rcrec->size = rec->size;
121
0
    rcrec->len1 = rcrec->len2 = 0;
122
0
    rcrec->next = cf->rchash[hi];
123
0
    cf->rchash[hi] = rcrec;
124
0
  }
125
126
0
  (pass == 1) ? rcrec->len1++ : rcrec->len2++;
127
128
0
  rec->minimal_perfect_hash = (size_t)rcrec->idx;
129
130
0
  return 0;
131
0
}
132
133
134
static void xdl_free_ctx(xdfile_t *xdf)
135
0
{
136
0
  xdl_free(xdf->reference_index);
137
0
  xdl_free(xdf->changed - 1);
138
0
  xdl_free(xdf->recs);
139
0
}
140
141
142
static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
143
0
         xdlclassifier_t *cf, xdfile_t *xdf) {
144
0
  long bsize;
145
0
  uint64_t hav;
146
0
  uint8_t const *blk, *cur, *top, *prev;
147
0
  xrecord_t *crec;
148
149
0
  xdf->reference_index = NULL;
150
0
  xdf->changed = NULL;
151
0
  xdf->recs = NULL;
152
153
0
  if (!XDL_ALLOC_ARRAY(xdf->recs, narec))
154
0
    goto abort;
155
156
0
  xdf->nrec = 0;
157
0
  if ((cur = blk = xdl_mmfile_first(mf, &bsize))) {
158
0
    for (top = blk + bsize; cur < top; ) {
159
0
      prev = cur;
160
0
      hav = xdl_hash_record(&cur, top, xpp->flags);
161
0
      if (XDL_ALLOC_GROW(xdf->recs, (long)xdf->nrec + 1, narec))
162
0
        goto abort;
163
0
      crec = &xdf->recs[xdf->nrec++];
164
0
      crec->ptr = prev;
165
0
      crec->size = cur - prev;
166
0
      if (xdl_classify_record(pass, cf, crec, hav) < 0)
167
0
        goto abort;
168
0
    }
169
0
  }
170
171
0
  if (!XDL_CALLOC_ARRAY(xdf->changed, xdf->nrec + 2))
172
0
    goto abort;
173
174
0
  xdf->changed += 1;
175
0
  xdf->nreff = 0;
176
0
  xdf->dstart = 0;
177
0
  xdf->dend = xdf->nrec - 1;
178
179
0
  return 0;
180
181
0
abort:
182
0
  xdl_free_ctx(xdf);
183
0
  return -1;
184
0
}
185
186
187
0
void xdl_free_env(xdfenv_t *xe) {
188
189
0
  xdl_free_ctx(&xe->xdf2);
190
0
  xdl_free_ctx(&xe->xdf1);
191
0
}
192
193
194
0
static bool xdl_clean_mmatch(uint8_t const *action, ptrdiff_t i, ptrdiff_t len) {
195
0
  ptrdiff_t r, rdis0, rpdis0, rdis1, rpdis1;
196
0
  ptrdiff_t s = 0, e = len - 1;
197
198
  /*
199
   * Limits the window that is examined during the similar-lines
200
   * scan. The loops below stops when action[i - r] == KEEP
201
   * (line that has no match), but there are corner cases where
202
   * the loop proceed all the way to the extremities by causing
203
   * huge performance penalties in case of big files.
204
   */
205
0
  if (i - s > XDL_SIMSCAN_WINDOW)
206
0
    s = i - XDL_SIMSCAN_WINDOW;
207
0
  if (e - i > XDL_SIMSCAN_WINDOW)
208
0
    e = i + XDL_SIMSCAN_WINDOW;
209
210
  /*
211
   * Scans the lines before 'i' to find a run of lines that either
212
   * have no match (action[j] == DISCARD) or have multiple matches
213
   * (action[j] == INVESTIGATE). Note that we always call this
214
   * function with action[i] == INVESTIGATE, so the current line
215
   * (i) is already a multimatch line.
216
   */
217
0
  for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
218
0
    if (action[i - r] == DISCARD)
219
0
      rdis0++;
220
0
    else if (action[i - r] == INVESTIGATE)
221
0
      rpdis0++;
222
0
    else if (action[i - r] == KEEP)
223
0
      break;
224
0
    else
225
0
      BUG("Illegal value for action[i - r]");
226
0
  }
227
  /*
228
   * If the run before the line 'i' found only multimatch lines,
229
   * we return false and hence we don't make the current line (i)
230
   * discarded. We want to discard multimatch lines only when
231
   * they appear in the middle of runs with nomatch lines
232
   * (action[j] == DISCARD).
233
   */
234
0
  if (rdis0 == 0)
235
0
    return 0;
236
0
  for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
237
0
    if (action[i + r] == DISCARD)
238
0
      rdis1++;
239
0
    else if (action[i + r] == INVESTIGATE)
240
0
      rpdis1++;
241
0
    else if (action[i + r] == KEEP)
242
0
      break;
243
0
    else
244
0
      BUG("Illegal value for action[i + r]");
245
0
  }
246
  /*
247
   * If the run after the line 'i' found only multimatch lines,
248
   * we return false and hence we don't make the current line (i)
249
   * discarded.
250
   */
251
0
  if (rdis1 == 0)
252
0
    return false;
253
0
  rdis1 += rdis0;
254
0
  rpdis1 += rpdis0;
255
256
0
  return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
257
0
}
258
259
260
/*
261
 * Try to reduce the problem complexity, discard records that have no
262
 * matches on the other file. Also, lines that have multiple matches
263
 * might be potentially discarded if they appear in a run of discardable.
264
 */
265
0
static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
266
0
  ptrdiff_t i, nm, mlim1, mlim2;
267
0
  xdlclass_t *rcrec;
268
0
  uint8_t *action1 = NULL, *action2 = NULL;
269
0
  bool need_min = !!(cf->flags & XDF_NEED_MINIMAL);
270
0
  int ret = 0;
271
0
  ptrdiff_t off = xdf1->dstart;
272
0
  ptrdiff_t len1 = xdf1->dend - off + 1;
273
0
  ptrdiff_t len2 = xdf2->dend - off + 1;
274
275
  /*
276
   * Create temporary arrays that will help us decide if
277
   * changed[i] should remain false, or become true.
278
   */
279
0
  if (!XDL_CALLOC_ARRAY(action1, len1) ||
280
0
      !XDL_CALLOC_ARRAY(action2, len2) ||
281
0
      !XDL_ALLOC_ARRAY(xdf1->reference_index, len1) ||
282
0
      !XDL_ALLOC_ARRAY(xdf2->reference_index, len2))
283
0
  {
284
0
    ret = -1;
285
0
    goto cleanup;
286
0
  }
287
288
  /*
289
   * Initialize temporary arrays with DISCARD, KEEP, or INVESTIGATE.
290
   */
291
0
  if (need_min) {
292
    /* i.e. infinity */
293
0
    mlim1 = PTRDIFF_MAX;
294
0
  } else {
295
0
    mlim1 = xdl_bogosqrt((uint64_t)xdf1->nrec);
296
0
    if (mlim1 > XDL_MAX_EQLIMIT)
297
0
      mlim1 = XDL_MAX_EQLIMIT;
298
0
  }
299
0
  for (i = 0; i < len1; i++) {
300
0
    size_t mph1 = xdf1->recs[i + off].minimal_perfect_hash;
301
0
    rcrec = cf->rcrecs[mph1];
302
0
    nm = rcrec ? rcrec->len2 : 0;
303
0
    if (nm == 0)
304
0
      action1[i] = DISCARD;
305
0
    else if (nm < mlim1)
306
0
      action1[i] = KEEP;
307
0
    else /* nm >= mlim1 */
308
0
      action1[i] = INVESTIGATE;
309
0
  }
310
311
0
  if (need_min) {
312
    /* i.e. infinity */
313
0
    mlim2 = PTRDIFF_MAX;
314
0
  } else {
315
0
    mlim2 = xdl_bogosqrt((uint64_t)xdf2->nrec);
316
0
    if (mlim2 > XDL_MAX_EQLIMIT)
317
0
      mlim2 = XDL_MAX_EQLIMIT;
318
0
  }
319
0
  for (i = 0; i < len2; i++) {
320
0
    size_t mph2 = xdf2->recs[i + off].minimal_perfect_hash;
321
0
    rcrec = cf->rcrecs[mph2];
322
0
    nm = rcrec ? rcrec->len1 : 0;
323
0
    if (nm == 0)
324
0
      action2[i] = DISCARD;
325
0
    else if (nm < mlim2)
326
0
      action2[i] = KEEP;
327
0
    else /* nm >= mlim2 */
328
0
      action2[i] = INVESTIGATE;
329
0
  }
330
331
  /*
332
   * Use temporary arrays to decide if changed[i] should remain
333
   * false, or become true.
334
   */
335
0
  xdf1->nreff = 0;
336
0
  for (i = 0; i < len1; i++) {
337
0
    uint8_t action = action1[i];
338
339
0
    if (action == INVESTIGATE) {
340
0
      if (!xdl_clean_mmatch(action1, i, len1))
341
0
        action = KEEP;
342
0
      else
343
0
        action = DISCARD;
344
0
    }
345
346
0
    if (action == KEEP) {
347
0
      xdf1->reference_index[xdf1->nreff++] = i + off;
348
      /* changed[i] remains false */
349
0
    } else if (action == DISCARD) {
350
0
      xdf1->changed[i + off] = true;
351
0
    } else {
352
0
      BUG("Illegal state for action");
353
0
    }
354
0
  }
355
356
0
  xdf2->nreff = 0;
357
0
  for (i = 0; i < len2; i++) {
358
0
    uint8_t action = action2[i];
359
360
0
    if (action == INVESTIGATE) {
361
0
      if (!xdl_clean_mmatch(action2, i, len2))
362
0
        action = KEEP;
363
0
      else
364
0
        action = DISCARD;
365
0
    }
366
367
0
    if (action == KEEP) {
368
0
      xdf2->reference_index[xdf2->nreff++] = i + off;
369
      /* changed[i] remains false */
370
0
    } else if (action == DISCARD) {
371
0
      xdf2->changed[i + off] = true;
372
0
    } else {
373
0
      BUG("Illegal state for action");
374
0
    }
375
0
  }
376
377
0
cleanup:
378
0
  xdl_free(action1);
379
0
  xdl_free(action2);
380
381
0
  return ret;
382
0
}
383
384
385
/*
386
 * Early trim initial and terminal matching records.
387
 */
388
0
static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
389
0
  long i, lim;
390
0
  xrecord_t *recs1, *recs2;
391
392
0
  recs1 = xdf1->recs;
393
0
  recs2 = xdf2->recs;
394
0
  for (i = 0, lim = (long)XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
395
0
       i++, recs1++, recs2++)
396
0
    if (recs1->minimal_perfect_hash != recs2->minimal_perfect_hash)
397
0
      break;
398
399
0
  xdf1->dstart = xdf2->dstart = i;
400
401
0
  recs1 = xdf1->recs + xdf1->nrec - 1;
402
0
  recs2 = xdf2->recs + xdf2->nrec - 1;
403
0
  for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
404
0
    if (recs1->minimal_perfect_hash != recs2->minimal_perfect_hash)
405
0
      break;
406
407
0
  xdf1->dend = (long)xdf1->nrec - i - 1;
408
0
  xdf2->dend = (long)xdf2->nrec - i - 1;
409
410
0
  return 0;
411
0
}
412
413
414
0
static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
415
416
0
  if (xdl_trim_ends(xdf1, xdf2) < 0 ||
417
0
      xdl_cleanup_records(cf, xdf1, xdf2) < 0) {
418
419
0
    return -1;
420
0
  }
421
422
0
  return 0;
423
0
}
424
425
int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
426
0
        xdfenv_t *xe) {
427
0
  long enl1, enl2, sample;
428
0
  xdlclassifier_t cf;
429
430
0
  memset(&cf, 0, sizeof(cf));
431
432
  /*
433
   * For histogram diff, we can afford a smaller sample size and
434
   * thus a poorer estimate of the number of lines, as the hash
435
   * table (rhash) won't be filled up/grown. The number of lines
436
   * (nrecs) will be updated correctly anyway by
437
   * xdl_prepare_ctx().
438
   */
439
0
  sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF
440
0
      ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1);
441
442
0
  enl1 = xdl_guess_lines(mf1, sample) + 1;
443
0
  enl2 = xdl_guess_lines(mf2, sample) + 1;
444
445
0
  if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
446
0
    return -1;
447
448
0
  if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
449
450
0
    xdl_free_classifier(&cf);
451
0
    return -1;
452
0
  }
453
0
  if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
454
455
0
    xdl_free_ctx(&xe->xdf1);
456
0
    xdl_free_classifier(&cf);
457
0
    return -1;
458
0
  }
459
460
0
  if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
461
0
      (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
462
0
      xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
463
464
0
    xdl_free_ctx(&xe->xdf2);
465
0
    xdl_free_ctx(&xe->xdf1);
466
0
    xdl_free_classifier(&cf);
467
0
    return -1;
468
0
  }
469
470
0
  xdl_free_classifier(&cf);
471
472
0
  return 0;
473
0
}