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_KPDIS_RUN 4
28#define XDL_MAX_EQLIMIT 1024
2930
31
32
typedef struct s_xdlclass {
33struct s_xdlclass *next;
34unsigned long ha;
35char const *line;
36long size;
37long idx;
38} xdlclass_t;
3940
typedef struct s_xdlclassifier {
41unsigned int hbits;
42long hsize;
43xdlclass_t **rchash;
44chastore_t ncha;
45long count;
46} xdlclassifier_t;
4748
49
50
51
static int xdl_init_classifier(xdlclassifier_t *cf, long size);
52static void xdl_free_classifier(xdlclassifier_t *cf);
53static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
54xrecord_t *rec);
55static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
56xdlclassifier_t *cf, xdfile_t *xdf);
57static void xdl_free_ctx(xdfile_t *xdf);
58static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
59static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2);
60static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
61static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2);
6263
64
65
66
static int xdl_init_classifier(xdlclassifier_t *cf, long size) {
67long i;
6869
cf->hbits = xdl_hashbits((unsigned int) size);
70cf->hsize = 1 << cf->hbits;
7172
if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
7374
return -1;
75}
76if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
7778
xdl_cha_free(&cf->ncha);
79return -1;
80}
81for (i = 0; i < cf->hsize; i++)
82cf->rchash[i] = NULL;
8384
cf->count = 0;
8586
return 0;
87}
8889
90
static void xdl_free_classifier(xdlclassifier_t *cf) {
9192
xdl_free(cf->rchash);
93xdl_cha_free(&cf->ncha);
94}
9596
97
static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
98xrecord_t *rec) {
99long hi;
100char const *line;
101xdlclass_t *rcrec;
102103
line = rec->ptr;
104hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
105for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
106if (rcrec->ha == rec->ha && rcrec->size == rec->size &&
107!memcmp(line, rcrec->line, rec->size))
108break;
109110
if (!rcrec) {
111if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
112113
return -1;
114}
115rcrec->idx = cf->count++;
116rcrec->line = line;
117rcrec->size = rec->size;
118rcrec->ha = rec->ha;
119rcrec->next = cf->rchash[hi];
120cf->rchash[hi] = rcrec;
121}
122123
rec->ha = (unsigned long) rcrec->idx;
124125
hi = (long) XDL_HASHLONG(rec->ha, hbits);
126rec->next = rhash[hi];
127rhash[hi] = rec;
128129
return 0;
130}
131132
133
static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
134xdlclassifier_t *cf, xdfile_t *xdf) {
135unsigned int hbits;
136long i, nrec, hsize, bsize;
137unsigned long hav;
138char const *blk, *cur, *top, *prev;
139xrecord_t *crec;
140xrecord_t **recs, **rrecs;
141xrecord_t **rhash;
142unsigned long *ha;
143char *rchg;
144long *rindex;
145146
if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
147148
return -1;
149}
150if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
151152
xdl_cha_free(&xdf->rcha);
153return -1;
154}
155156
hbits = xdl_hashbits((unsigned int) narec);
157hsize = 1 << hbits;
158if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
159160
xdl_free(recs);
161xdl_cha_free(&xdf->rcha);
162return -1;
163}
164for (i = 0; i < hsize; i++)
165rhash[i] = NULL;
166167
nrec = 0;
168if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
169for (top = blk + bsize;;) {
170if (cur >= top) {
171if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
172break;
173top = blk + bsize;
174}
175prev = cur;
176hav = xdl_hash_record(&cur, top);
177if (nrec >= narec) {
178narec *= 2;
179if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
180181
xdl_free(rhash);
182xdl_free(recs);
183xdl_cha_free(&xdf->rcha);
184return -1;
185}
186recs = rrecs;
187}
188if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
189190
xdl_free(rhash);
191xdl_free(recs);
192xdl_cha_free(&xdf->rcha);
193return -1;
194}
195crec->ptr = prev;
196crec->size = (long) (cur - prev);
197crec->ha = hav;
198recs[nrec++] = crec;
199200
if (xdl_classify_record(cf, rhash, hbits, crec) < 0) {
201202
xdl_free(rhash);
203xdl_free(recs);
204xdl_cha_free(&xdf->rcha);
205return -1;
206}
207}
208}
209210
if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
211212
xdl_free(rhash);
213xdl_free(recs);
214xdl_cha_free(&xdf->rcha);
215return -1;
216}
217memset(rchg, 0, (nrec + 2) * sizeof(char));
218219
if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
220221
xdl_free(rchg);
222xdl_free(rhash);
223xdl_free(recs);
224xdl_cha_free(&xdf->rcha);
225return -1;
226}
227if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
228229
xdl_free(rindex);
230xdl_free(rchg);
231xdl_free(rhash);
232xdl_free(recs);
233xdl_cha_free(&xdf->rcha);
234return -1;
235}
236237
xdf->nrec = nrec;
238xdf->recs = recs;
239xdf->hbits = hbits;
240xdf->rhash = rhash;
241xdf->rchg = rchg + 1;
242xdf->rindex = rindex;
243xdf->nreff = 0;
244xdf->ha = ha;
245xdf->dstart = 0;
246xdf->dend = nrec - 1;
247248
return 0;
249}
250251
252
static void xdl_free_ctx(xdfile_t *xdf) {
253254
xdl_free(xdf->rhash);
255xdl_free(xdf->rindex);
256xdl_free(xdf->rchg - 1);
257xdl_free(xdf->ha);
258xdl_free(xdf->recs);
259xdl_cha_free(&xdf->rcha);
260}
261262
263
int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
264xdfenv_t *xe) {
265long enl1, enl2;
266xdlclassifier_t cf;
267268
enl1 = xdl_guess_lines(mf1) + 1;
269enl2 = xdl_guess_lines(mf2) + 1;
270271
if (xdl_init_classifier(&cf, enl1 + enl2 + 1) < 0) {
272273
return -1;
274}
275276
if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
277278
xdl_free_classifier(&cf);
279return -1;
280}
281if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
282283
xdl_free_ctx(&xe->xdf1);
284xdl_free_classifier(&cf);
285return -1;
286}
287288
xdl_free_classifier(&cf);
289290
if (xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) {
291292
xdl_free_ctx(&xe->xdf2);
293xdl_free_ctx(&xe->xdf1);
294return -1;
295}
296297
return 0;
298}
299300
301
void xdl_free_env(xdfenv_t *xe) {
302303
xdl_free_ctx(&xe->xdf2);
304xdl_free_ctx(&xe->xdf1);
305}
306307
308
static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
309long r, rdis0, rpdis0, rdis1, rpdis1;
310311
/*
312* Scans the lines before 'i' to find a run of lines that either
313* have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
314* Note that we always call this function with dis[i] > 1, so the
315* current line (i) is already a multimatch line.
316*/
317for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
318if (!dis[i - r])
319rdis0++;
320else if (dis[i - r] == 2)
321rpdis0++;
322else
323break;
324}
325/*
326* If the run before the line 'i' found only multimatch lines, we
327* return 0 and hence we don't make the current line (i) discarded.
328* We want to discard multimatch lines only when they appear in the
329* middle of runs with nomatch lines (dis[j] == 0).
330*/
331if (rdis0 == 0)
332return 0;
333for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
334if (!dis[i + r])
335rdis1++;
336else if (dis[i + r] == 2)
337rpdis1++;
338else
339break;
340}
341/*
342* If the run after the line 'i' found only multimatch lines, we
343* return 0 and hence we don't make the current line (i) discarded.
344*/
345if (rdis1 == 0)
346return 0;
347rdis1 += rdis0;
348rpdis1 += rpdis0;
349350
return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
351}
352353
354
/*
355* Try to reduce the problem complexity, discard records that have no
356* matches on the other file. Also, lines that have multiple matches
357* might be potentially discarded if they happear in a run of discardable.
358*/
359static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
360long i, nm, rhi, nreff, mlim;
361unsigned long hav;
362xrecord_t **recs;
363xrecord_t *rec;
364char *dis, *dis1, *dis2;
365366
if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
367368
return -1;
369}
370memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
371dis1 = dis;
372dis2 = dis1 + xdf1->nrec + 1;
373374
if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
375mlim = XDL_MAX_EQLIMIT;
376for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
377hav = (*recs)->ha;
378rhi = (long) XDL_HASHLONG(hav, xdf2->hbits);
379for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next)
380if (rec->ha == hav && ++nm == mlim)
381break;
382dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
383}
384385
if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
386mlim = XDL_MAX_EQLIMIT;
387for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
388hav = (*recs)->ha;
389rhi = (long) XDL_HASHLONG(hav, xdf1->hbits);
390for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next)
391if (rec->ha == hav && ++nm == mlim)
392break;
393dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
394}
395396
for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
397i <= xdf1->dend; i++, recs++) {
398if (dis1[i] == 1 ||
399(dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
400xdf1->rindex[nreff] = i;
401xdf1->ha[nreff] = (*recs)->ha;
402nreff++;
403} else
404xdf1->rchg[i] = 1;
405}
406xdf1->nreff = nreff;
407408
for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
409i <= xdf2->dend; i++, recs++) {
410if (dis2[i] == 1 ||
411(dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
412xdf2->rindex[nreff] = i;
413xdf2->ha[nreff] = (*recs)->ha;
414nreff++;
415} else
416xdf2->rchg[i] = 1;
417}
418xdf2->nreff = nreff;
419420
xdl_free(dis);
421422
return 0;
423}
424425
426
/*
427* Early trim initial and terminal matching records.
428*/
429static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
430long i, lim;
431xrecord_t **recs1, **recs2;
432433
recs1 = xdf1->recs;
434recs2 = xdf2->recs;
435for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
436i++, recs1++, recs2++)
437if ((*recs1)->ha != (*recs2)->ha)
438break;
439440
xdf1->dstart = xdf2->dstart = i;
441442
recs1 = xdf1->recs + xdf1->nrec - 1;
443recs2 = xdf2->recs + xdf2->nrec - 1;
444for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
445if ((*recs1)->ha != (*recs2)->ha)
446break;
447448
xdf1->dend = xdf1->nrec - i - 1;
449xdf2->dend = xdf2->nrec - i - 1;
450451
return 0;
452}
453454
455
static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) {
456457
if (xdl_trim_ends(xdf1, xdf2) < 0 ||
458xdl_cleanup_records(xdf1, xdf2) < 0) {
459460
return -1;
461}
462463
return 0;
464}
465