joachim99@52: /* File I/O for GNU DIFF. joachim99@52: joachim99@68: Modified for KDiff3 by Joachim Eibl 2003, 2004, 2005. joachim99@53: The original file was part of GNU DIFF. joachim99@52: joachim99@52: Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002 joachim99@52: Free Software Foundation, Inc. joachim99@52: joachim99@52: GNU DIFF is free software; you can redistribute it and/or modify joachim99@52: it under the terms of the GNU General Public License as published by joachim99@52: the Free Software Foundation; either version 2, or (at your option) joachim99@52: any later version. joachim99@52: joachim99@52: GNU DIFF is distributed in the hope that it will be useful, joachim99@52: but WITHOUT ANY WARRANTY; without even the implied warranty of joachim99@52: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the joachim99@52: GNU General Public License for more details. joachim99@52: joachim99@52: You should have received a copy of the GNU General Public License joachim99@52: along with this program; see the file COPYING. joachim99@52: If not, write to the Free Software Foundation, joachim99@69: 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. */ joachim99@52: joachim99@52: #include "gnudiff_diff.h" joachim99@52: #include joachim99@52: joachim99@52: /* Rotate an unsigned value to the left. */ joachim99@52: #define ROL(v, n) ((v) << (n) | (v) >> (sizeof (v) * CHAR_BIT - (n))) joachim99@52: joachim99@52: /* Given a hash value and a new character, return a new hash value. */ joachim99@52: #define HASH(h, c) ((c) + ROL (h, 7)) joachim99@52: joachim99@52: /* The type of a hash value. */ joachim99@52: typedef size_t hash_value; joachim99@52: verify (hash_value_is_unsigned, ! TYPE_SIGNED (hash_value)); joachim99@52: joachim99@52: /* Lines are put into equivalence classes of lines that match in lines_differ. joachim99@52: Each equivalence class is represented by one of these structures, joachim99@52: but only while the classes are being computed. joachim99@52: Afterward, each class is represented by a number. */ joachim99@52: struct equivclass joachim99@52: { joachim99@52: lin next; /* Next item in this bucket. */ joachim99@52: hash_value hash; /* Hash of lines in this class. */ joachim99@68: const QChar *line; /* A line that fits this class. */ joachim99@52: size_t length; /* That line's length, not counting its newline. */ joachim99@52: }; joachim99@52: joachim99@52: /* Hash-table: array of buckets, each being a chain of equivalence classes. joachim99@52: buckets[-1] is reserved for incomplete lines. */ joachim99@52: static lin *buckets; joachim99@52: joachim99@52: /* Number of buckets in the hash table array, not counting buckets[-1]. */ joachim99@52: static size_t nbuckets; joachim99@52: joachim99@52: /* Array in which the equivalence classes are allocated. joachim99@52: The bucket-chains go through the elements in this array. joachim99@52: The number of an equivalence class is its index in this array. */ joachim99@52: static struct equivclass *equivs; joachim99@52: joachim99@52: /* Index of first free element in the array `equivs'. */ joachim99@52: static lin equivs_index; joachim99@52: joachim99@52: /* Number of elements allocated in the array `equivs'. */ joachim99@52: static lin equivs_alloc; joachim99@52: joachim99@52: joachim99@52: /* Check for binary files and compare them for exact identity. */ joachim99@52: joachim99@52: /* Return 1 if BUF contains a non text character. joachim99@52: SIZE is the number of characters in BUF. */ joachim99@52: joachim99@52: #define binary_file_p(buf, size) (memchr (buf, 0, size) != 0) joachim99@52: joachim99@69: /* Compare two lines (typically one from each input file) joachim99@69: according to the command line options. joachim99@69: For efficiency, this is invoked only when the lines do not match exactly joachim99@69: but an option like -i might cause us to ignore the difference. joachim99@69: Return nonzero if the lines differ. */ joachim99@69: joachim99@69: bool GnuDiff::lines_differ (const QChar *s1, size_t len1, const QChar *s2, size_t len2 ) joachim99@69: { joachim99@69: const QChar *t1 = s1; joachim99@69: const QChar *t2 = s2; joachim99@69: const QChar *s1end = s1+len1; joachim99@69: const QChar *s2end = s2+len2; joachim99@69: joachim99@69: for ( ; ; ++t1, ++t2 ) joachim99@69: { joachim99@69: /* Test for exact char equality first, since it's a common case. */ joachim99@69: if ( t1!=s1end && t2!=s2end && *t1==*t2 ) joachim99@69: continue; joachim99@69: else joachim99@69: { joachim99@69: while ( t1!=s1end && joachim99@69: ( bIgnoreWhiteSpace && isWhite( *t1 ) || joachim99@69: bIgnoreNumbers && (t1->isDigit() || *t1=='-' || *t1=='.' ))) joachim99@69: { joachim99@69: ++t1; joachim99@69: } joachim99@69: joachim99@69: while ( t2 != s2end && joachim99@69: ( bIgnoreWhiteSpace && isWhite( *t2 ) || joachim99@69: bIgnoreNumbers && (t2->isDigit() || *t2=='-' || *t2=='.' ))) joachim99@69: { joachim99@69: ++t2; joachim99@69: } joachim99@69: joachim99@69: if ( t1!=s1end && t2!=s2end ) joachim99@69: { joachim99@69: if (ignore_case) joachim99@69: { /* Lowercase comparison. */ joachim99@75: if ( t1->toLower() == t2->toLower() ) joachim99@69: continue; joachim99@69: } joachim99@69: else if ( *t1 == *t2 ) joachim99@69: continue; joachim99@69: else joachim99@69: return true; joachim99@69: } joachim99@69: else if ( t1==s1end && t2==s2end ) joachim99@69: return false; joachim99@69: else joachim99@69: return true; joachim99@69: } joachim99@69: } joachim99@69: return false; joachim99@69: } joachim99@69: joachim99@52: joachim99@52: /* Split the file into lines, simultaneously computing the equivalence joachim99@52: class for each line. */ joachim99@52: joachim99@53: void GnuDiff::find_and_hash_each_line (struct file_data *current) joachim99@52: { joachim99@52: hash_value h; joachim99@69: const QChar *p = current->prefix_end; joachim99@68: QChar c; joachim99@52: lin i, *bucket; joachim99@52: size_t length; joachim99@52: joachim99@52: /* Cache often-used quantities in local variables to help the compiler. */ joachim99@68: const QChar **linbuf = current->linbuf; joachim99@52: lin alloc_lines = current->alloc_lines; joachim99@52: lin line = 0; joachim99@52: lin linbuf_base = current->linbuf_base; joachim99@52: lin *cureqs = (lin*)xmalloc (alloc_lines * sizeof *cureqs); joachim99@52: struct equivclass *eqs = equivs; joachim99@52: lin eqs_index = equivs_index; joachim99@52: lin eqs_alloc = equivs_alloc; joachim99@68: const QChar *suffix_begin = current->suffix_begin; joachim99@69: const QChar *bufend = current->buffer + current->buffered; joachim99@52: bool diff_length_compare_anyway = joachim99@52: ignore_white_space != IGNORE_NO_WHITE_SPACE || bIgnoreNumbers; joachim99@52: bool same_length_diff_contents_compare_anyway = joachim99@52: diff_length_compare_anyway | ignore_case; joachim99@52: joachim99@69: while ( p < suffix_begin) joachim99@52: { joachim99@69: const QChar *ip = p; joachim99@52: joachim99@52: h = 0; joachim99@52: joachim99@69: /* Hash this line until we find a newline or bufend is reached. */ joachim99@52: if (ignore_case) joachim99@52: switch (ignore_white_space) joachim99@52: { joachim99@52: case IGNORE_ALL_SPACE: joachim99@69: while ( pbuffered_lines = line; joachim99@52: joachim99@52: for (i = 0; ; i++) joachim99@52: { joachim99@52: /* Record the line start for lines in the suffix that we care about. joachim99@52: Record one more line start than lines, joachim99@52: so that we can compute the length of any buffered line. */ joachim99@52: if (line == alloc_lines) joachim99@52: { joachim99@52: /* Double (alloc_lines - linbuf_base) by adding to alloc_lines. */ joachim99@66: if ((lin)(PTRDIFF_MAX / 3) <= alloc_lines joachim99@66: || (lin)(PTRDIFF_MAX / sizeof *cureqs) <= 2 * alloc_lines - linbuf_base joachim99@66: || (lin)(PTRDIFF_MAX / sizeof *linbuf) <= alloc_lines - linbuf_base) joachim99@52: xalloc_die (); joachim99@52: alloc_lines = 2 * alloc_lines - linbuf_base; joachim99@52: linbuf += linbuf_base; joachim99@68: linbuf = (const QChar**)xrealloc (linbuf, joachim99@52: (alloc_lines - linbuf_base) * sizeof *linbuf); joachim99@52: linbuf -= linbuf_base; joachim99@52: } joachim99@69: linbuf[line] = p; joachim99@52: joachim99@69: if ( p >= bufend) joachim99@52: break; joachim99@52: joachim99@52: if (context <= i && no_diff_means_no_output) joachim99@52: break; joachim99@52: joachim99@52: line++; joachim99@52: joachim99@69: while (plinbuf = linbuf; joachim99@52: current->valid_lines = line; joachim99@52: current->alloc_lines = alloc_lines; joachim99@52: current->equivs = cureqs; joachim99@52: equivs = eqs; joachim99@52: equivs_alloc = eqs_alloc; joachim99@52: equivs_index = eqs_index; joachim99@52: } joachim99@52: joachim99@52: /* We have found N lines in a buffer of size S; guess the joachim99@52: proportionate number of lines that will be found in a buffer of joachim99@52: size T. However, do not guess a number of lines so large that the joachim99@52: resulting line table might cause overflow in size calculations. */ joachim99@52: static lin joachim99@52: guess_lines (lin n, size_t s, size_t t) joachim99@52: { joachim99@52: size_t guessed_bytes_per_line = n < 10 ? 32 : s / (n - 1); joachim99@52: lin guessed_lines = MAX (1, t / guessed_bytes_per_line); joachim99@68: return MIN (guessed_lines, (lin)(PTRDIFF_MAX / (2 * sizeof (QChar *) + 1) - 5)) + 5; joachim99@52: } joachim99@52: joachim99@52: /* Given a vector of two file_data objects, find the identical joachim99@52: prefixes and suffixes of each object. */ joachim99@52: joachim99@53: void GnuDiff::find_identical_ends (struct file_data filevec[]) joachim99@52: { joachim99@69: /* Find identical prefix. */ joachim99@69: const QChar *p0, *p1, *buffer0, *buffer1; joachim99@69: p0 = buffer0 = filevec[0].buffer; joachim99@69: p1 = buffer1 = filevec[1].buffer; joachim99@52: size_t n0, n1; joachim99@52: n0 = filevec[0].buffered; joachim99@52: n1 = filevec[1].buffered; joachim99@69: const QChar* const pEnd0 = p0 + n0; joachim99@69: const QChar* const pEnd1 = p1 + n1; joachim99@52: joachim99@52: if (p0 == p1) joachim99@52: /* The buffers are the same; sentinels won't work. */ joachim99@52: p0 = p1 += n1; joachim99@52: else joachim99@52: { joachim99@69: /* Loop until first mismatch, or end. */ joachim99@69: while ( p0!=pEnd0 && p1!=pEnd1 && *p0 == *p1 ) joachim99@69: { joachim99@69: p0++; joachim99@69: p1++; joachim99@69: } joachim99@52: } joachim99@52: joachim99@52: /* Now P0 and P1 point at the first nonmatching characters. */ joachim99@52: joachim99@69: /* Skip back to last line-beginning in the prefix. */ joachim99@69: while (p0 != buffer0 && (p0[-1] != '\n' )) joachim99@52: p0--, p1--; joachim99@52: joachim99@52: /* Record the prefix. */ joachim99@52: filevec[0].prefix_end = p0; joachim99@52: filevec[1].prefix_end = p1; joachim99@52: joachim99@52: /* Find identical suffix. */ joachim99@52: joachim99@52: /* P0 and P1 point beyond the last chars not yet compared. */ joachim99@52: p0 = buffer0 + n0; joachim99@52: p1 = buffer1 + n1; joachim99@52: joachim99@69: const QChar *end0, *beg0; joachim99@69: end0 = p0; /* Addr of last char in file 0. */ joachim99@52: joachim99@69: /* Get value of P0 at which we should stop scanning backward: joachim99@69: this is when either P0 or P1 points just past the last char joachim99@69: of the identical prefix. */ joachim99@69: beg0 = filevec[0].prefix_end + (n0 < n1 ? 0 : n0 - n1); joachim99@52: joachim99@69: /* Scan back until chars don't match or we reach that point. */ joachim99@69: for (; p0 != beg0; p0--, p1--) joachim99@69: { joachim99@69: if (*p0 != *p1) joachim99@69: { joachim99@69: /* Point at the first char of the matching suffix. */ joachim99@69: beg0 = p0; joachim99@69: break; joachim99@69: } joachim99@69: } joachim99@52: joachim99@69: // Go to the next line (skip last line with a difference) joachim99@69: if ( p0 != end0 ) joachim99@69: { joachim99@69: if (*p0 != *p1) joachim99@69: ++p0; joachim99@69: while ( p0. */ joachim99@52: joachim99@52: static unsigned char const prime_offset[] = joachim99@52: { joachim99@52: 0, 0, 1, 1, 3, 1, 3, 1, 5, 3, 3, 9, 3, 1, 3, 19, 15, 1, 5, 1, 3, 9, 3, joachim99@52: 15, 3, 39, 5, 39, 57, 3, 35, 1, 5, 9, 41, 31, 5, 25, 45, 7, 87, 21, joachim99@52: 11, 57, 17, 55, 21, 115, 59, 81, 27, 129, 47, 111, 33, 55, 5, 13, 27, joachim99@52: 55, 93, 1, 57, 25 joachim99@52: }; joachim99@52: joachim99@52: /* Verify that this host's size_t is not too wide for the above table. */ joachim99@52: joachim99@52: verify (enough_prime_offsets, joachim99@52: sizeof (size_t) * CHAR_BIT <= sizeof prime_offset); joachim99@52: joachim99@52: /* Given a vector of two file_data objects, read the file associated joachim99@52: with each one, and build the table of equivalence classes. joachim99@52: Return nonzero if either file appears to be a binary file. joachim99@52: If PRETEND_BINARY is nonzero, pretend they are binary regardless. */ joachim99@52: joachim99@52: bool joachim99@53: GnuDiff::read_files (struct file_data filevec[], bool /*pretend_binary*/) joachim99@52: { joachim99@52: int i; joachim99@52: joachim99@52: find_identical_ends (filevec); joachim99@52: joachim99@52: equivs_alloc = filevec[0].alloc_lines + filevec[1].alloc_lines + 1; joachim99@66: if ((lin)(PTRDIFF_MAX / sizeof *equivs) <= equivs_alloc) joachim99@52: xalloc_die (); joachim99@52: equivs = (equivclass*)xmalloc (equivs_alloc * sizeof *equivs); joachim99@52: /* Equivalence class 0 is permanently safe for lines that were not joachim99@52: hashed. Real equivalence classes start at 1. */ joachim99@52: equivs_index = 1; joachim99@52: joachim99@52: /* Allocate (one plus) a prime number of hash buckets. Use a prime joachim99@52: number between 1/3 and 2/3 of the value of equiv_allocs, joachim99@52: approximately. */ joachim99@52: for (i = 9; 1 << i < equivs_alloc / 3; i++) joachim99@52: continue; joachim99@52: nbuckets = ((size_t) 1 << i) - prime_offset[i]; joachim99@52: if (PTRDIFF_MAX / sizeof *buckets <= nbuckets) joachim99@52: xalloc_die (); joachim99@52: buckets = (lin*)zalloc ((nbuckets + 1) * sizeof *buckets); joachim99@52: buckets++; joachim99@52: joachim99@52: for (i = 0; i < 2; i++) joachim99@52: find_and_hash_each_line (&filevec[i]); joachim99@52: joachim99@52: filevec[0].equiv_max = filevec[1].equiv_max = equivs_index; joachim99@52: joachim99@52: free (equivs); joachim99@52: free (buckets - 1); joachim99@52: joachim99@52: return 0; joachim99@52: }