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