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_KPDIS_RUN 4
28
29
30
31typedef struct s_xdlclass {
32 struct s_xdlclass *next;
33 unsigned long ha;
34 char const *line;
35 long size;
36 long idx;
37} xdlclass_t;
38
39typedef struct s_xdlclassifier {
40 unsigned int hbits;
41 long hsize;
42 xdlclass_t **rchash;
43 chastore_t ncha;
44 long count;
45} xdlclassifier_t;
46
47
48
49
50static int xdl_init_classifier(xdlclassifier_t *cf, long size);
51static void xdl_free_classifier(xdlclassifier_t *cf);
52static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
53 xrecord_t *rec);
54static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
55 xdlclassifier_t *cf, xdfile_t *xdf);
56static void xdl_free_ctx(xdfile_t *xdf);
57static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
58static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2);
59static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
60static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2);
61
62
63
64
65static int xdl_init_classifier(xdlclassifier_t *cf, long size) {
66 long i;
67
68 cf->hbits = xdl_hashbits((unsigned int) size);
69 cf->hsize = 1 << cf->hbits;
70
71 if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
72
73 return -1;
74 }
75 if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
76
77 xdl_cha_free(&cf->ncha);
78 return -1;
79 }
80 for (i = 0; i < cf->hsize; i++)
81 cf->rchash[i] = NULL;
82
83 cf->count = 0;
84
85 return 0;
86}
87
88
89static void xdl_free_classifier(xdlclassifier_t *cf) {
90
91 xdl_free(cf->rchash);
92 xdl_cha_free(&cf->ncha);
93}
94
95
96static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
97 xrecord_t *rec) {
98 long hi;
99 char const *line;
100 xdlclass_t *rcrec;
101
102 line = rec->ptr;
103 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
104 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
105 if (rcrec->ha == rec->ha && rcrec->size == rec->size &&
106 !memcmp(line, rcrec->line, rec->size))
107 break;
108
109 if (!rcrec) {
110 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
111
112 return -1;
113 }
114 rcrec->idx = cf->count++;
115 rcrec->line = line;
116 rcrec->size = rec->size;
117 rcrec->ha = rec->ha;
118 rcrec->next = cf->rchash[hi];
119 cf->rchash[hi] = rcrec;
120 }
121
122 rec->ha = (unsigned long) rcrec->idx;
123
124 hi = (long) XDL_HASHLONG(rec->ha, hbits);
125 rec->next = rhash[hi];
126 rhash[hi] = rec;
127
128 return 0;
129}
130
131
132static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
133 xdlclassifier_t *cf, xdfile_t *xdf) {
134 unsigned int hbits;
135 long i, nrec, hsize, bsize;
136 unsigned long hav;
137 char const *blk, *cur, *top, *prev;
138 xrecord_t *crec;
139 xrecord_t **recs, **rrecs;
140 xrecord_t **rhash;
141 unsigned long *ha;
142 char *rchg;
143 long *rindex;
144
145 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
146
147 return -1;
148 }
149 if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
150
151 xdl_cha_free(&xdf->rcha);
152 return -1;
153 }
154
155 hbits = xdl_hashbits((unsigned int) narec);
156 hsize = 1 << hbits;
157 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
158
159 xdl_free(recs);
160 xdl_cha_free(&xdf->rcha);
161 return -1;
162 }
163 for (i = 0; i < hsize; i++)
164 rhash[i] = NULL;
165
166 nrec = 0;
167 if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
168 for (top = blk + bsize;;) {
169 if (cur >= top) {
170 if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
171 break;
172 top = blk + bsize;
173 }
174 prev = cur;
175 hav = xdl_hash_record(&cur, top);
176 if (nrec >= narec) {
177 narec *= 2;
178 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
179
180 xdl_free(rhash);
181 xdl_free(recs);
182 xdl_cha_free(&xdf->rcha);
183 return -1;
184 }
185 recs = rrecs;
186 }
187 if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
188
189 xdl_free(rhash);
190 xdl_free(recs);
191 xdl_cha_free(&xdf->rcha);
192 return -1;
193 }
194 crec->ptr = prev;
195 crec->size = (long) (cur - prev);
196 crec->ha = hav;
197 recs[nrec++] = crec;
198
199 if (xdl_classify_record(cf, rhash, hbits, crec) < 0) {
200
201 xdl_free(rhash);
202 xdl_free(recs);
203 xdl_cha_free(&xdf->rcha);
204 return -1;
205 }
206 }
207 }
208
209 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
210
211 xdl_free(rhash);
212 xdl_free(recs);
213 xdl_cha_free(&xdf->rcha);
214 return -1;
215 }
216 memset(rchg, 0, (nrec + 2) * sizeof(char));
217
218 if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
219
220 xdl_free(rchg);
221 xdl_free(rhash);
222 xdl_free(recs);
223 xdl_cha_free(&xdf->rcha);
224 return -1;
225 }
226 if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
227
228 xdl_free(rindex);
229 xdl_free(rchg);
230 xdl_free(rhash);
231 xdl_free(recs);
232 xdl_cha_free(&xdf->rcha);
233 return -1;
234 }
235
236 xdf->nrec = nrec;
237 xdf->recs = recs;
238 xdf->hbits = hbits;
239 xdf->rhash = rhash;
240 xdf->rchg = rchg + 1;
241 xdf->rindex = rindex;
242 xdf->nreff = 0;
243 xdf->ha = ha;
244 xdf->dstart = 0;
245 xdf->dend = nrec - 1;
246
247 return 0;
248}
249
250
251static void xdl_free_ctx(xdfile_t *xdf) {
252
253 xdl_free(xdf->rhash);
254 xdl_free(xdf->rindex);
255 xdl_free(xdf->rchg - 1);
256 xdl_free(xdf->ha);
257 xdl_free(xdf->recs);
258 xdl_cha_free(&xdf->rcha);
259}
260
261
262int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
263 xdfenv_t *xe) {
264 long enl1, enl2;
265 xdlclassifier_t cf;
266
267 enl1 = xdl_guess_lines(mf1) + 1;
268 enl2 = xdl_guess_lines(mf2) + 1;
269
270 if (xdl_init_classifier(&cf, enl1 + enl2 + 1) < 0) {
271
272 return -1;
273 }
274
275 if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
276
277 xdl_free_classifier(&cf);
278 return -1;
279 }
280 if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
281
282 xdl_free_ctx(&xe->xdf1);
283 xdl_free_classifier(&cf);
284 return -1;
285 }
286
287 xdl_free_classifier(&cf);
288
289 if (xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) {
290
291 xdl_free_ctx(&xe->xdf2);
292 xdl_free_ctx(&xe->xdf1);
293 return -1;
294 }
295
296 return 0;
297}
298
299
300void xdl_free_env(xdfenv_t *xe) {
301
302 xdl_free_ctx(&xe->xdf2);
303 xdl_free_ctx(&xe->xdf1);
304}
305
306
307static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
308 long r, rdis, rpdis;
309
310 for (r = 1, rdis = 0, rpdis = 1; (i - r) >= s; r++) {
311 if (!dis[i - r])
312 rdis++;
313 else if (dis[i - r] == 2)
314 rpdis++;
315 else
316 break;
317 }
318 for (r = 1; (i + r) <= e; r++) {
319 if (!dis[i + r])
320 rdis++;
321 else if (dis[i + r] == 2)
322 rpdis++;
323 else
324 break;
325 }
326
327 return rpdis * XDL_KPDIS_RUN < (rpdis + rdis);
328}
329
330
331/*
332 * Try to reduce the problem complexity, discard records that have no
333 * matches on the other file. Also, lines that have multiple matches
334 * might be potentially discarded if they happear in a run of discardable.
335 */
336static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
337 long i, rhi, nreff;
338 unsigned long hav;
339 xrecord_t **recs;
340 xrecord_t *rec;
341 char *dis, *dis1, *dis2;
342
343 if (!(dis = (char *) xdl_malloc((xdf1->nrec + xdf2->nrec + 2) * sizeof(char)))) {
344
345 return -1;
346 }
347 memset(dis, 0, (xdf1->nrec + xdf2->nrec + 2) * sizeof(char));
348 dis1 = dis;
349 dis2 = dis1 + xdf1->nrec + 1;
350
351 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
352 hav = (*recs)->ha;
353 rhi = (long) XDL_HASHLONG(hav, xdf2->hbits);
354 for (rec = xdf2->rhash[rhi]; rec; rec = rec->next)
355 if (rec->ha == hav && ++dis1[i] == 2)
356 break;
357 }
358
359 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
360 hav = (*recs)->ha;
361 rhi = (long) XDL_HASHLONG(hav, xdf1->hbits);
362 for (rec = xdf1->rhash[rhi]; rec; rec = rec->next)
363 if (rec->ha == hav && ++dis2[i] == 2)
364 break;
365 }
366
367 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
368 i <= xdf1->dend; i++, recs++) {
369 if (dis1[i] == 1 ||
370 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
371 xdf1->rindex[nreff] = i;
372 xdf1->ha[nreff] = (*recs)->ha;
373 nreff++;
374 } else
375 xdf1->rchg[i] = 1;
376 }
377 xdf1->nreff = nreff;
378
379 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
380 i <= xdf2->dend; i++, recs++) {
381 if (dis2[i] == 1 ||
382 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
383 xdf2->rindex[nreff] = i;
384 xdf2->ha[nreff] = (*recs)->ha;
385 nreff++;
386 } else
387 xdf2->rchg[i] = 1;
388 }
389 xdf2->nreff = nreff;
390
391 xdl_free(dis);
392
393 return 0;
394}
395
396
397/*
398 * Early trim initial and terminal matching records.
399 */
400static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
401 long i, lim;
402 xrecord_t **recs1, **recs2;
403
404 recs1 = xdf1->recs;
405 recs2 = xdf2->recs;
406 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
407 i++, recs1++, recs2++)
408 if ((*recs1)->ha != (*recs2)->ha)
409 break;
410
411 xdf1->dstart = xdf2->dstart = i;
412
413 recs1 = xdf1->recs + xdf1->nrec - 1;
414 recs2 = xdf2->recs + xdf2->nrec - 1;
415 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
416 if ((*recs1)->ha != (*recs2)->ha)
417 break;
418
419 xdf1->dend = xdf1->nrec - i - 1;
420 xdf2->dend = xdf2->nrec - i - 1;
421
422 return 0;
423}
424
425
426static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) {
427
428 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
429 xdl_cleanup_records(xdf1, xdf2) < 0) {
430
431 return -1;
432 }
433
434 return 0;
435}
436