xref: /netbsd-src/external/gpl2/diffutils/dist/src/io.c (revision 75f6d617e282811cb173c2ccfbf5df0dd71f7045)
1*75f6d617Schristos /*	$NetBSD: io.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2*75f6d617Schristos 
3*75f6d617Schristos /* File I/O for GNU DIFF.
4*75f6d617Schristos 
5*75f6d617Schristos    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002
6*75f6d617Schristos    Free Software Foundation, Inc.
7*75f6d617Schristos 
8*75f6d617Schristos    This file is part of GNU DIFF.
9*75f6d617Schristos 
10*75f6d617Schristos    GNU DIFF is free software; you can redistribute it and/or modify
11*75f6d617Schristos    it under the terms of the GNU General Public License as published by
12*75f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
13*75f6d617Schristos    any later version.
14*75f6d617Schristos 
15*75f6d617Schristos    GNU DIFF is distributed in the hope that it will be useful,
16*75f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
17*75f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*75f6d617Schristos    GNU General Public License for more details.
19*75f6d617Schristos 
20*75f6d617Schristos    You should have received a copy of the GNU General Public License
21*75f6d617Schristos    along with this program; see the file COPYING.
22*75f6d617Schristos    If not, write to the Free Software Foundation,
23*75f6d617Schristos    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
24*75f6d617Schristos 
25*75f6d617Schristos #include "diff.h"
26*75f6d617Schristos #include <cmpbuf.h>
27*75f6d617Schristos #include <regex.h>
28*75f6d617Schristos #include <setmode.h>
29*75f6d617Schristos #include <xalloc.h>
30*75f6d617Schristos 
31*75f6d617Schristos /* Rotate an unsigned value to the left.  */
32*75f6d617Schristos #define ROL(v, n) ((v) << (n) | (v) >> (sizeof (v) * CHAR_BIT - (n)))
33*75f6d617Schristos 
34*75f6d617Schristos /* Given a hash value and a new character, return a new hash value.  */
35*75f6d617Schristos #define HASH(h, c) ((c) + ROL (h, 7))
36*75f6d617Schristos 
37*75f6d617Schristos /* The type of a hash value.  */
38*75f6d617Schristos typedef size_t hash_value;
39*75f6d617Schristos verify (hash_value_is_unsigned, ! TYPE_SIGNED (hash_value));
40*75f6d617Schristos 
41*75f6d617Schristos /* Lines are put into equivalence classes of lines that match in lines_differ.
42*75f6d617Schristos    Each equivalence class is represented by one of these structures,
43*75f6d617Schristos    but only while the classes are being computed.
44*75f6d617Schristos    Afterward, each class is represented by a number.  */
45*75f6d617Schristos struct equivclass
46*75f6d617Schristos {
47*75f6d617Schristos   lin next;		/* Next item in this bucket.  */
48*75f6d617Schristos   hash_value hash;	/* Hash of lines in this class.  */
49*75f6d617Schristos   char const *line;	/* A line that fits this class.  */
50*75f6d617Schristos   size_t length;	/* That line's length, not counting its newline.  */
51*75f6d617Schristos };
52*75f6d617Schristos 
53*75f6d617Schristos /* Hash-table: array of buckets, each being a chain of equivalence classes.
54*75f6d617Schristos    buckets[-1] is reserved for incomplete lines.  */
55*75f6d617Schristos static lin *buckets;
56*75f6d617Schristos 
57*75f6d617Schristos /* Number of buckets in the hash table array, not counting buckets[-1].  */
58*75f6d617Schristos static size_t nbuckets;
59*75f6d617Schristos 
60*75f6d617Schristos /* Array in which the equivalence classes are allocated.
61*75f6d617Schristos    The bucket-chains go through the elements in this array.
62*75f6d617Schristos    The number of an equivalence class is its index in this array.  */
63*75f6d617Schristos static struct equivclass *equivs;
64*75f6d617Schristos 
65*75f6d617Schristos /* Index of first free element in the array `equivs'.  */
66*75f6d617Schristos static lin equivs_index;
67*75f6d617Schristos 
68*75f6d617Schristos /* Number of elements allocated in the array `equivs'.  */
69*75f6d617Schristos static lin equivs_alloc;
70*75f6d617Schristos 
71*75f6d617Schristos /* Read a block of data into a file buffer, checking for EOF and error.  */
72*75f6d617Schristos 
73*75f6d617Schristos void
file_block_read(struct file_data * current,size_t size)74*75f6d617Schristos file_block_read (struct file_data *current, size_t size)
75*75f6d617Schristos {
76*75f6d617Schristos   if (size && ! current->eof)
77*75f6d617Schristos     {
78*75f6d617Schristos       size_t s = block_read (current->desc,
79*75f6d617Schristos 			     FILE_BUFFER (current) + current->buffered, size);
80*75f6d617Schristos       if (s == SIZE_MAX)
81*75f6d617Schristos 	pfatal_with_name (current->name);
82*75f6d617Schristos       current->buffered += s;
83*75f6d617Schristos       current->eof = s < size;
84*75f6d617Schristos     }
85*75f6d617Schristos }
86*75f6d617Schristos 
87*75f6d617Schristos /* Check for binary files and compare them for exact identity.  */
88*75f6d617Schristos 
89*75f6d617Schristos /* Return 1 if BUF contains a non text character.
90*75f6d617Schristos    SIZE is the number of characters in BUF.  */
91*75f6d617Schristos 
92*75f6d617Schristos #define binary_file_p(buf, size) (memchr (buf, 0, size) != 0)
93*75f6d617Schristos 
94*75f6d617Schristos /* Get ready to read the current file.
95*75f6d617Schristos    Return nonzero if SKIP_TEST is zero,
96*75f6d617Schristos    and if it appears to be a binary file.  */
97*75f6d617Schristos 
98*75f6d617Schristos static bool
sip(struct file_data * current,bool skip_test)99*75f6d617Schristos sip (struct file_data *current, bool skip_test)
100*75f6d617Schristos {
101*75f6d617Schristos   /* If we have a nonexistent file at this stage, treat it as empty.  */
102*75f6d617Schristos   if (current->desc < 0)
103*75f6d617Schristos     {
104*75f6d617Schristos       /* Leave room for a sentinel.  */
105*75f6d617Schristos       current->bufsize = sizeof (word);
106*75f6d617Schristos       current->buffer = xmalloc (current->bufsize);
107*75f6d617Schristos     }
108*75f6d617Schristos   else
109*75f6d617Schristos     {
110*75f6d617Schristos       current->bufsize = buffer_lcm (sizeof (word),
111*75f6d617Schristos 				     STAT_BLOCKSIZE (current->stat),
112*75f6d617Schristos 				     PTRDIFF_MAX - 2 * sizeof (word));
113*75f6d617Schristos       current->buffer = xmalloc (current->bufsize);
114*75f6d617Schristos 
115*75f6d617Schristos       if (! skip_test)
116*75f6d617Schristos 	{
117*75f6d617Schristos 	  /* Check first part of file to see if it's a binary file.  */
118*75f6d617Schristos 
119*75f6d617Schristos 	  bool was_binary = set_binary_mode (current->desc, 1);
120*75f6d617Schristos 	  off_t buffered;
121*75f6d617Schristos 	  file_block_read (current, current->bufsize);
122*75f6d617Schristos 	  buffered = current->buffered;
123*75f6d617Schristos 
124*75f6d617Schristos 	  if (! was_binary)
125*75f6d617Schristos 	    {
126*75f6d617Schristos 	      /* Revert to text mode and seek back to the beginning to
127*75f6d617Schristos 		 reread the file.  Use relative seek, since file
128*75f6d617Schristos 		 descriptors like stdin might not start at offset
129*75f6d617Schristos 		 zero.  */
130*75f6d617Schristos 
131*75f6d617Schristos 	      if (lseek (current->desc, - buffered, SEEK_CUR) == -1)
132*75f6d617Schristos 		pfatal_with_name (current->name);
133*75f6d617Schristos 	      set_binary_mode (current->desc, 0);
134*75f6d617Schristos 	      current->buffered = 0;
135*75f6d617Schristos 	      current->eof = 0;
136*75f6d617Schristos 	    }
137*75f6d617Schristos 
138*75f6d617Schristos 	  return binary_file_p (current->buffer, buffered);
139*75f6d617Schristos 	}
140*75f6d617Schristos     }
141*75f6d617Schristos 
142*75f6d617Schristos   current->buffered = 0;
143*75f6d617Schristos   current->eof = 0;
144*75f6d617Schristos   return 0;
145*75f6d617Schristos }
146*75f6d617Schristos 
147*75f6d617Schristos /* Slurp the rest of the current file completely into memory.  */
148*75f6d617Schristos 
149*75f6d617Schristos static void
slurp(struct file_data * current)150*75f6d617Schristos slurp (struct file_data *current)
151*75f6d617Schristos {
152*75f6d617Schristos   size_t cc;
153*75f6d617Schristos 
154*75f6d617Schristos   if (current->desc < 0)
155*75f6d617Schristos     {
156*75f6d617Schristos       /* The file is nonexistent.  */
157*75f6d617Schristos       return;
158*75f6d617Schristos     }
159*75f6d617Schristos 
160*75f6d617Schristos   if (S_ISREG (current->stat.st_mode))
161*75f6d617Schristos     {
162*75f6d617Schristos       /* It's a regular file; slurp in the rest all at once.  */
163*75f6d617Schristos 
164*75f6d617Schristos       /* Get the size out of the stat block.
165*75f6d617Schristos 	 Allocate just enough room for appended newline plus word sentinel,
166*75f6d617Schristos 	 plus word-alignment since we want the buffer word-aligned.  */
167*75f6d617Schristos       size_t file_size = current->stat.st_size;
168*75f6d617Schristos       cc = file_size + 2 * sizeof (word) - file_size % sizeof (word);
169*75f6d617Schristos       if (file_size != current->stat.st_size || cc < file_size
170*75f6d617Schristos 	  || PTRDIFF_MAX <= cc)
171*75f6d617Schristos 	xalloc_die ();
172*75f6d617Schristos 
173*75f6d617Schristos       if (current->bufsize < cc)
174*75f6d617Schristos 	{
175*75f6d617Schristos 	  current->bufsize = cc;
176*75f6d617Schristos 	  current->buffer = xrealloc (current->buffer, cc);
177*75f6d617Schristos 	}
178*75f6d617Schristos 
179*75f6d617Schristos       /* Try to read at least 1 more byte than the size indicates, to
180*75f6d617Schristos 	 detect whether the file is growing.  This is a nicety for
181*75f6d617Schristos 	 users who run 'diff' on files while they are changing.  */
182*75f6d617Schristos 
183*75f6d617Schristos       if (current->buffered <= file_size)
184*75f6d617Schristos 	{
185*75f6d617Schristos 	  file_block_read (current, file_size + 1 - current->buffered);
186*75f6d617Schristos 	  if (current->buffered <= file_size)
187*75f6d617Schristos 	    return;
188*75f6d617Schristos 	}
189*75f6d617Schristos     }
190*75f6d617Schristos 
191*75f6d617Schristos   /* It's not a regular file, or it's a growing regular file; read it,
192*75f6d617Schristos      growing the buffer as needed.  */
193*75f6d617Schristos 
194*75f6d617Schristos   file_block_read (current, current->bufsize - current->buffered);
195*75f6d617Schristos 
196*75f6d617Schristos   if (current->buffered)
197*75f6d617Schristos     {
198*75f6d617Schristos       while (current->buffered == current->bufsize)
199*75f6d617Schristos 	{
200*75f6d617Schristos 	  if (PTRDIFF_MAX / 2 - sizeof (word) < current->bufsize)
201*75f6d617Schristos 	    xalloc_die ();
202*75f6d617Schristos 	  current->bufsize *= 2;
203*75f6d617Schristos 	  current->buffer = xrealloc (current->buffer, current->bufsize);
204*75f6d617Schristos 	  file_block_read (current, current->bufsize - current->buffered);
205*75f6d617Schristos 	}
206*75f6d617Schristos 
207*75f6d617Schristos       /* Allocate just enough room for appended newline plus word
208*75f6d617Schristos 	 sentinel, plus word-alignment.  */
209*75f6d617Schristos       cc = current->buffered + 2 * sizeof (word);
210*75f6d617Schristos       current->bufsize = cc - cc % sizeof (word);
211*75f6d617Schristos       current->buffer = xrealloc (current->buffer, current->bufsize);
212*75f6d617Schristos     }
213*75f6d617Schristos }
214*75f6d617Schristos 
215*75f6d617Schristos /* Split the file into lines, simultaneously computing the equivalence
216*75f6d617Schristos    class for each line.  */
217*75f6d617Schristos 
218*75f6d617Schristos static void
find_and_hash_each_line(struct file_data * current)219*75f6d617Schristos find_and_hash_each_line (struct file_data *current)
220*75f6d617Schristos {
221*75f6d617Schristos   hash_value h;
222*75f6d617Schristos   unsigned char const *p = (unsigned char const *) current->prefix_end;
223*75f6d617Schristos   unsigned char c;
224*75f6d617Schristos   lin i, *bucket;
225*75f6d617Schristos   size_t length;
226*75f6d617Schristos 
227*75f6d617Schristos   /* Cache often-used quantities in local variables to help the compiler.  */
228*75f6d617Schristos   char const **linbuf = current->linbuf;
229*75f6d617Schristos   lin alloc_lines = current->alloc_lines;
230*75f6d617Schristos   lin line = 0;
231*75f6d617Schristos   lin linbuf_base = current->linbuf_base;
232*75f6d617Schristos   lin *cureqs = xmalloc (alloc_lines * sizeof *cureqs);
233*75f6d617Schristos   struct equivclass *eqs = equivs;
234*75f6d617Schristos   lin eqs_index = equivs_index;
235*75f6d617Schristos   lin eqs_alloc = equivs_alloc;
236*75f6d617Schristos   char const *suffix_begin = current->suffix_begin;
237*75f6d617Schristos   char const *bufend = FILE_BUFFER (current) + current->buffered;
238*75f6d617Schristos   bool diff_length_compare_anyway =
239*75f6d617Schristos     ignore_white_space != IGNORE_NO_WHITE_SPACE;
240*75f6d617Schristos   bool same_length_diff_contents_compare_anyway =
241*75f6d617Schristos     diff_length_compare_anyway | ignore_case;
242*75f6d617Schristos 
243*75f6d617Schristos   while ((char const *) p < suffix_begin)
244*75f6d617Schristos     {
245*75f6d617Schristos       char const *ip = (char const *) p;
246*75f6d617Schristos 
247*75f6d617Schristos       h = 0;
248*75f6d617Schristos 
249*75f6d617Schristos       /* Hash this line until we find a newline.  */
250*75f6d617Schristos       if (ignore_case)
251*75f6d617Schristos 	switch (ignore_white_space)
252*75f6d617Schristos 	  {
253*75f6d617Schristos 	  case IGNORE_ALL_SPACE:
254*75f6d617Schristos 	    while ((c = *p++) != '\n')
255*75f6d617Schristos 	      if (! ISSPACE (c))
256*75f6d617Schristos 		h = HASH (h, TOLOWER (c));
257*75f6d617Schristos 	    break;
258*75f6d617Schristos 
259*75f6d617Schristos 	  case IGNORE_SPACE_CHANGE:
260*75f6d617Schristos 	    while ((c = *p++) != '\n')
261*75f6d617Schristos 	      {
262*75f6d617Schristos 		if (ISSPACE (c))
263*75f6d617Schristos 		  {
264*75f6d617Schristos 		    do
265*75f6d617Schristos 		      if ((c = *p++) == '\n')
266*75f6d617Schristos 			goto hashing_done;
267*75f6d617Schristos 		    while (ISSPACE (c));
268*75f6d617Schristos 
269*75f6d617Schristos 		    h = HASH (h, ' ');
270*75f6d617Schristos 		  }
271*75f6d617Schristos 
272*75f6d617Schristos 		/* C is now the first non-space.  */
273*75f6d617Schristos 		h = HASH (h, TOLOWER (c));
274*75f6d617Schristos 	      }
275*75f6d617Schristos 	    break;
276*75f6d617Schristos 
277*75f6d617Schristos 	  case IGNORE_TAB_EXPANSION:
278*75f6d617Schristos 	    {
279*75f6d617Schristos 	      size_t column = 0;
280*75f6d617Schristos 	      while ((c = *p++) != '\n')
281*75f6d617Schristos 		{
282*75f6d617Schristos 		  int repetitions = 1;
283*75f6d617Schristos 
284*75f6d617Schristos 		  switch (c)
285*75f6d617Schristos 		    {
286*75f6d617Schristos 		    case '\b':
287*75f6d617Schristos 		      column -= 0 < column;
288*75f6d617Schristos 		      break;
289*75f6d617Schristos 
290*75f6d617Schristos 		    case '\t':
291*75f6d617Schristos 		      c = ' ';
292*75f6d617Schristos 		      repetitions = TAB_WIDTH - column % TAB_WIDTH;
293*75f6d617Schristos 		      column += repetitions;
294*75f6d617Schristos 		      break;
295*75f6d617Schristos 
296*75f6d617Schristos 		    case '\r':
297*75f6d617Schristos 		      column = 0;
298*75f6d617Schristos 		      break;
299*75f6d617Schristos 
300*75f6d617Schristos 		    default:
301*75f6d617Schristos 		      c = TOLOWER (c);
302*75f6d617Schristos 		      column++;
303*75f6d617Schristos 		      break;
304*75f6d617Schristos 		    }
305*75f6d617Schristos 
306*75f6d617Schristos 		  do
307*75f6d617Schristos 		    h = HASH (h, c);
308*75f6d617Schristos 		  while (--repetitions != 0);
309*75f6d617Schristos 		}
310*75f6d617Schristos 	    }
311*75f6d617Schristos 	    break;
312*75f6d617Schristos 
313*75f6d617Schristos 	  default:
314*75f6d617Schristos 	    while ((c = *p++) != '\n')
315*75f6d617Schristos 	      h = HASH (h, TOLOWER (c));
316*75f6d617Schristos 	    break;
317*75f6d617Schristos 	  }
318*75f6d617Schristos       else
319*75f6d617Schristos 	switch (ignore_white_space)
320*75f6d617Schristos 	  {
321*75f6d617Schristos 	  case IGNORE_ALL_SPACE:
322*75f6d617Schristos 	    while ((c = *p++) != '\n')
323*75f6d617Schristos 	      if (! ISSPACE (c))
324*75f6d617Schristos 		h = HASH (h, c);
325*75f6d617Schristos 	    break;
326*75f6d617Schristos 
327*75f6d617Schristos 	  case IGNORE_SPACE_CHANGE:
328*75f6d617Schristos 	    while ((c = *p++) != '\n')
329*75f6d617Schristos 	      {
330*75f6d617Schristos 		if (ISSPACE (c))
331*75f6d617Schristos 		  {
332*75f6d617Schristos 		    do
333*75f6d617Schristos 		      if ((c = *p++) == '\n')
334*75f6d617Schristos 			goto hashing_done;
335*75f6d617Schristos 		    while (ISSPACE (c));
336*75f6d617Schristos 
337*75f6d617Schristos 		    h = HASH (h, ' ');
338*75f6d617Schristos 		  }
339*75f6d617Schristos 
340*75f6d617Schristos 		/* C is now the first non-space.  */
341*75f6d617Schristos 		h = HASH (h, c);
342*75f6d617Schristos 	      }
343*75f6d617Schristos 	    break;
344*75f6d617Schristos 
345*75f6d617Schristos 	  case IGNORE_TAB_EXPANSION:
346*75f6d617Schristos 	    {
347*75f6d617Schristos 	      size_t column = 0;
348*75f6d617Schristos 	      while ((c = *p++) != '\n')
349*75f6d617Schristos 		{
350*75f6d617Schristos 		  int repetitions = 1;
351*75f6d617Schristos 
352*75f6d617Schristos 		  switch (c)
353*75f6d617Schristos 		    {
354*75f6d617Schristos 		    case '\b':
355*75f6d617Schristos 		      column -= 0 < column;
356*75f6d617Schristos 		      break;
357*75f6d617Schristos 
358*75f6d617Schristos 		    case '\t':
359*75f6d617Schristos 		      c = ' ';
360*75f6d617Schristos 		      repetitions = TAB_WIDTH - column % TAB_WIDTH;
361*75f6d617Schristos 		      column += repetitions;
362*75f6d617Schristos 		      break;
363*75f6d617Schristos 
364*75f6d617Schristos 		    case '\r':
365*75f6d617Schristos 		      column = 0;
366*75f6d617Schristos 		      break;
367*75f6d617Schristos 
368*75f6d617Schristos 		    default:
369*75f6d617Schristos 		      column++;
370*75f6d617Schristos 		      break;
371*75f6d617Schristos 		    }
372*75f6d617Schristos 
373*75f6d617Schristos 		  do
374*75f6d617Schristos 		    h = HASH (h, c);
375*75f6d617Schristos 		  while (--repetitions != 0);
376*75f6d617Schristos 		}
377*75f6d617Schristos 	    }
378*75f6d617Schristos 	    break;
379*75f6d617Schristos 
380*75f6d617Schristos 	  default:
381*75f6d617Schristos 	    while ((c = *p++) != '\n')
382*75f6d617Schristos 	      h = HASH (h, c);
383*75f6d617Schristos 	    break;
384*75f6d617Schristos 	  }
385*75f6d617Schristos 
386*75f6d617Schristos    hashing_done:;
387*75f6d617Schristos 
388*75f6d617Schristos       bucket = &buckets[h % nbuckets];
389*75f6d617Schristos       length = (char const *) p - ip - 1;
390*75f6d617Schristos 
391*75f6d617Schristos       if ((char const *) p == bufend
392*75f6d617Schristos 	  && current->missing_newline
393*75f6d617Schristos 	  && ROBUST_OUTPUT_STYLE (output_style))
394*75f6d617Schristos 	{
395*75f6d617Schristos 	  /* This line is incomplete.  If this is significant,
396*75f6d617Schristos 	     put the line into buckets[-1].  */
397*75f6d617Schristos 	  if (ignore_white_space < IGNORE_SPACE_CHANGE)
398*75f6d617Schristos 	    bucket = &buckets[-1];
399*75f6d617Schristos 
400*75f6d617Schristos 	  /* Omit the inserted newline when computing linbuf later.  */
401*75f6d617Schristos 	  p--;
402*75f6d617Schristos 	  bufend = suffix_begin = (char const *) p;
403*75f6d617Schristos 	}
404*75f6d617Schristos 
405*75f6d617Schristos       for (i = *bucket;  ;  i = eqs[i].next)
406*75f6d617Schristos 	if (!i)
407*75f6d617Schristos 	  {
408*75f6d617Schristos 	    /* Create a new equivalence class in this bucket.  */
409*75f6d617Schristos 	    i = eqs_index++;
410*75f6d617Schristos 	    if (i == eqs_alloc)
411*75f6d617Schristos 	      {
412*75f6d617Schristos 		if (PTRDIFF_MAX / (2 * sizeof *eqs) <= eqs_alloc)
413*75f6d617Schristos 		  xalloc_die ();
414*75f6d617Schristos 		eqs_alloc *= 2;
415*75f6d617Schristos 		eqs = xrealloc (eqs, eqs_alloc * sizeof *eqs);
416*75f6d617Schristos 	      }
417*75f6d617Schristos 	    eqs[i].next = *bucket;
418*75f6d617Schristos 	    eqs[i].hash = h;
419*75f6d617Schristos 	    eqs[i].line = ip;
420*75f6d617Schristos 	    eqs[i].length = length;
421*75f6d617Schristos 	    *bucket = i;
422*75f6d617Schristos 	    break;
423*75f6d617Schristos 	  }
424*75f6d617Schristos 	else if (eqs[i].hash == h)
425*75f6d617Schristos 	  {
426*75f6d617Schristos 	    char const *eqline = eqs[i].line;
427*75f6d617Schristos 
428*75f6d617Schristos 	    /* Reuse existing class if lines_differ reports the lines
429*75f6d617Schristos                equal.  */
430*75f6d617Schristos 	    if (eqs[i].length == length)
431*75f6d617Schristos 	      {
432*75f6d617Schristos 		/* Reuse existing equivalence class if the lines are identical.
433*75f6d617Schristos 		   This detects the common case of exact identity
434*75f6d617Schristos 		   faster than lines_differ would.  */
435*75f6d617Schristos 		if (memcmp (eqline, ip, length) == 0)
436*75f6d617Schristos 		  break;
437*75f6d617Schristos 		if (!same_length_diff_contents_compare_anyway)
438*75f6d617Schristos 		  continue;
439*75f6d617Schristos 	      }
440*75f6d617Schristos 	    else if (!diff_length_compare_anyway)
441*75f6d617Schristos 	      continue;
442*75f6d617Schristos 
443*75f6d617Schristos 	    if (! lines_differ (eqline, ip))
444*75f6d617Schristos 	      break;
445*75f6d617Schristos 	  }
446*75f6d617Schristos 
447*75f6d617Schristos       /* Maybe increase the size of the line table.  */
448*75f6d617Schristos       if (line == alloc_lines)
449*75f6d617Schristos 	{
450*75f6d617Schristos 	  /* Double (alloc_lines - linbuf_base) by adding to alloc_lines.  */
451*75f6d617Schristos 	  if (PTRDIFF_MAX / 3 <= alloc_lines
452*75f6d617Schristos 	      || PTRDIFF_MAX / sizeof *cureqs <= 2 * alloc_lines - linbuf_base
453*75f6d617Schristos 	      || PTRDIFF_MAX / sizeof *linbuf <= alloc_lines - linbuf_base)
454*75f6d617Schristos 	    xalloc_die ();
455*75f6d617Schristos 	  alloc_lines = 2 * alloc_lines - linbuf_base;
456*75f6d617Schristos 	  cureqs = xrealloc (cureqs, alloc_lines * sizeof *cureqs);
457*75f6d617Schristos 	  linbuf += linbuf_base;
458*75f6d617Schristos 	  linbuf = xrealloc (linbuf,
459*75f6d617Schristos 			     (alloc_lines - linbuf_base) * sizeof *linbuf);
460*75f6d617Schristos 	  linbuf -= linbuf_base;
461*75f6d617Schristos 	}
462*75f6d617Schristos       linbuf[line] = ip;
463*75f6d617Schristos       cureqs[line] = i;
464*75f6d617Schristos       ++line;
465*75f6d617Schristos     }
466*75f6d617Schristos 
467*75f6d617Schristos   current->buffered_lines = line;
468*75f6d617Schristos 
469*75f6d617Schristos   for (i = 0;  ;  i++)
470*75f6d617Schristos     {
471*75f6d617Schristos       /* Record the line start for lines in the suffix that we care about.
472*75f6d617Schristos 	 Record one more line start than lines,
473*75f6d617Schristos 	 so that we can compute the length of any buffered line.  */
474*75f6d617Schristos       if (line == alloc_lines)
475*75f6d617Schristos 	{
476*75f6d617Schristos 	  /* Double (alloc_lines - linbuf_base) by adding to alloc_lines.  */
477*75f6d617Schristos 	  if (PTRDIFF_MAX / 3 <= alloc_lines
478*75f6d617Schristos 	      || PTRDIFF_MAX / sizeof *cureqs <= 2 * alloc_lines - linbuf_base
479*75f6d617Schristos 	      || PTRDIFF_MAX / sizeof *linbuf <= alloc_lines - linbuf_base)
480*75f6d617Schristos 	    xalloc_die ();
481*75f6d617Schristos 	  alloc_lines = 2 * alloc_lines - linbuf_base;
482*75f6d617Schristos 	  linbuf += linbuf_base;
483*75f6d617Schristos 	  linbuf = xrealloc (linbuf,
484*75f6d617Schristos 			     (alloc_lines - linbuf_base) * sizeof *linbuf);
485*75f6d617Schristos 	  linbuf -= linbuf_base;
486*75f6d617Schristos 	}
487*75f6d617Schristos       linbuf[line] = (char const *) p;
488*75f6d617Schristos 
489*75f6d617Schristos       if ((char const *) p == bufend)
490*75f6d617Schristos 	break;
491*75f6d617Schristos 
492*75f6d617Schristos       if (context <= i && no_diff_means_no_output)
493*75f6d617Schristos 	break;
494*75f6d617Schristos 
495*75f6d617Schristos       line++;
496*75f6d617Schristos 
497*75f6d617Schristos       while (*p++ != '\n')
498*75f6d617Schristos 	continue;
499*75f6d617Schristos     }
500*75f6d617Schristos 
501*75f6d617Schristos   /* Done with cache in local variables.  */
502*75f6d617Schristos   current->linbuf = linbuf;
503*75f6d617Schristos   current->valid_lines = line;
504*75f6d617Schristos   current->alloc_lines = alloc_lines;
505*75f6d617Schristos   current->equivs = cureqs;
506*75f6d617Schristos   equivs = eqs;
507*75f6d617Schristos   equivs_alloc = eqs_alloc;
508*75f6d617Schristos   equivs_index = eqs_index;
509*75f6d617Schristos }
510*75f6d617Schristos 
511*75f6d617Schristos /* Prepare the text.  Make sure the text end is initialized.
512*75f6d617Schristos    Make sure text ends in a newline,
513*75f6d617Schristos    but remember that we had to add one.
514*75f6d617Schristos    Strip trailing CRs, if that was requested.  */
515*75f6d617Schristos 
516*75f6d617Schristos static void
prepare_text(struct file_data * current)517*75f6d617Schristos prepare_text (struct file_data *current)
518*75f6d617Schristos {
519*75f6d617Schristos   size_t buffered = current->buffered;
520*75f6d617Schristos   char *p = FILE_BUFFER (current);
521*75f6d617Schristos   char *dst;
522*75f6d617Schristos 
523*75f6d617Schristos   if (buffered == 0 || p[buffered - 1] == '\n')
524*75f6d617Schristos     current->missing_newline = 0;
525*75f6d617Schristos   else
526*75f6d617Schristos     {
527*75f6d617Schristos       p[buffered++] = '\n';
528*75f6d617Schristos       current->missing_newline = 1;
529*75f6d617Schristos     }
530*75f6d617Schristos 
531*75f6d617Schristos   if (!p)
532*75f6d617Schristos     return;
533*75f6d617Schristos 
534*75f6d617Schristos   /* Don't use uninitialized storage when planting or using sentinels.  */
535*75f6d617Schristos   memset (p + buffered, 0, sizeof (word));
536*75f6d617Schristos 
537*75f6d617Schristos   if (strip_trailing_cr && (dst = memchr (p, '\r', buffered)))
538*75f6d617Schristos     {
539*75f6d617Schristos       char const *src = dst;
540*75f6d617Schristos       char const *srclim = p + buffered;
541*75f6d617Schristos 
542*75f6d617Schristos       do
543*75f6d617Schristos 	dst += ! ((*dst = *src++) == '\r' && *src == '\n');
544*75f6d617Schristos       while (src < srclim);
545*75f6d617Schristos 
546*75f6d617Schristos       buffered -= src - dst;
547*75f6d617Schristos     }
548*75f6d617Schristos 
549*75f6d617Schristos   current->buffered = buffered;
550*75f6d617Schristos }
551*75f6d617Schristos 
552*75f6d617Schristos /* We have found N lines in a buffer of size S; guess the
553*75f6d617Schristos    proportionate number of lines that will be found in a buffer of
554*75f6d617Schristos    size T.  However, do not guess a number of lines so large that the
555*75f6d617Schristos    resulting line table might cause overflow in size calculations.  */
556*75f6d617Schristos static lin
guess_lines(lin n,size_t s,size_t t)557*75f6d617Schristos guess_lines (lin n, size_t s, size_t t)
558*75f6d617Schristos {
559*75f6d617Schristos   size_t guessed_bytes_per_line = n < 10 ? 32 : s / (n - 1);
560*75f6d617Schristos   lin guessed_lines = MAX (1, t / guessed_bytes_per_line);
561*75f6d617Schristos   return MIN (guessed_lines, PTRDIFF_MAX / (2 * sizeof (char *) + 1) - 5) + 5;
562*75f6d617Schristos }
563*75f6d617Schristos 
564*75f6d617Schristos /* Given a vector of two file_data objects, find the identical
565*75f6d617Schristos    prefixes and suffixes of each object.  */
566*75f6d617Schristos 
567*75f6d617Schristos static void
find_identical_ends(struct file_data filevec[])568*75f6d617Schristos find_identical_ends (struct file_data filevec[])
569*75f6d617Schristos {
570*75f6d617Schristos   word *w0, *w1;
571*75f6d617Schristos   char *p0, *p1, *buffer0, *buffer1;
572*75f6d617Schristos   char const *end0, *beg0;
573*75f6d617Schristos   char const **linbuf0, **linbuf1;
574*75f6d617Schristos   lin i, lines;
575*75f6d617Schristos   size_t n0, n1;
576*75f6d617Schristos   lin alloc_lines0, alloc_lines1;
577*75f6d617Schristos   lin buffered_prefix, prefix_count, prefix_mask;
578*75f6d617Schristos   lin middle_guess, suffix_guess;
579*75f6d617Schristos 
580*75f6d617Schristos   slurp (&filevec[0]);
581*75f6d617Schristos   prepare_text (&filevec[0]);
582*75f6d617Schristos   if (filevec[0].desc != filevec[1].desc)
583*75f6d617Schristos     {
584*75f6d617Schristos       slurp (&filevec[1]);
585*75f6d617Schristos       prepare_text (&filevec[1]);
586*75f6d617Schristos     }
587*75f6d617Schristos   else
588*75f6d617Schristos     {
589*75f6d617Schristos       filevec[1].buffer = filevec[0].buffer;
590*75f6d617Schristos       filevec[1].bufsize = filevec[0].bufsize;
591*75f6d617Schristos       filevec[1].buffered = filevec[0].buffered;
592*75f6d617Schristos       filevec[1].missing_newline = filevec[0].missing_newline;
593*75f6d617Schristos     }
594*75f6d617Schristos 
595*75f6d617Schristos   /* Find identical prefix.  */
596*75f6d617Schristos 
597*75f6d617Schristos   w0 = filevec[0].buffer;
598*75f6d617Schristos   w1 = filevec[1].buffer;
599*75f6d617Schristos   p0 = buffer0 = (char *) w0;
600*75f6d617Schristos   p1 = buffer1 = (char *) w1;
601*75f6d617Schristos   n0 = filevec[0].buffered;
602*75f6d617Schristos   n1 = filevec[1].buffered;
603*75f6d617Schristos 
604*75f6d617Schristos   if (p0 == p1)
605*75f6d617Schristos     /* The buffers are the same; sentinels won't work.  */
606*75f6d617Schristos     p0 = p1 += n1;
607*75f6d617Schristos   else
608*75f6d617Schristos     {
609*75f6d617Schristos       /* Insert end sentinels, in this case characters that are guaranteed
610*75f6d617Schristos 	 to make the equality test false, and thus terminate the loop.  */
611*75f6d617Schristos 
612*75f6d617Schristos       if (n0 < n1)
613*75f6d617Schristos 	p0[n0] = ~p1[n0];
614*75f6d617Schristos       else
615*75f6d617Schristos 	p1[n1] = ~p0[n1];
616*75f6d617Schristos 
617*75f6d617Schristos       /* Loop until first mismatch, or to the sentinel characters.  */
618*75f6d617Schristos 
619*75f6d617Schristos       /* Compare a word at a time for speed.  */
620*75f6d617Schristos       while (*w0 == *w1)
621*75f6d617Schristos 	w0++, w1++;
622*75f6d617Schristos 
623*75f6d617Schristos       /* Do the last few bytes of comparison a byte at a time.  */
624*75f6d617Schristos       p0 = (char *) w0;
625*75f6d617Schristos       p1 = (char *) w1;
626*75f6d617Schristos       while (*p0 == *p1)
627*75f6d617Schristos 	p0++, p1++;
628*75f6d617Schristos 
629*75f6d617Schristos       /* Don't mistakenly count missing newline as part of prefix.  */
630*75f6d617Schristos       if (ROBUST_OUTPUT_STYLE (output_style)
631*75f6d617Schristos 	  && ((buffer0 + n0 - filevec[0].missing_newline < p0)
632*75f6d617Schristos 	      !=
633*75f6d617Schristos 	      (buffer1 + n1 - filevec[1].missing_newline < p1)))
634*75f6d617Schristos 	p0--, p1--;
635*75f6d617Schristos     }
636*75f6d617Schristos 
637*75f6d617Schristos   /* Now P0 and P1 point at the first nonmatching characters.  */
638*75f6d617Schristos 
639*75f6d617Schristos   /* Skip back to last line-beginning in the prefix,
640*75f6d617Schristos      and then discard up to HORIZON_LINES lines from the prefix.  */
641*75f6d617Schristos   i = horizon_lines;
642*75f6d617Schristos   while (p0 != buffer0 && (p0[-1] != '\n' || i--))
643*75f6d617Schristos     p0--, p1--;
644*75f6d617Schristos 
645*75f6d617Schristos   /* Record the prefix.  */
646*75f6d617Schristos   filevec[0].prefix_end = p0;
647*75f6d617Schristos   filevec[1].prefix_end = p1;
648*75f6d617Schristos 
649*75f6d617Schristos   /* Find identical suffix.  */
650*75f6d617Schristos 
651*75f6d617Schristos   /* P0 and P1 point beyond the last chars not yet compared.  */
652*75f6d617Schristos   p0 = buffer0 + n0;
653*75f6d617Schristos   p1 = buffer1 + n1;
654*75f6d617Schristos 
655*75f6d617Schristos   if (! ROBUST_OUTPUT_STYLE (output_style)
656*75f6d617Schristos       || filevec[0].missing_newline == filevec[1].missing_newline)
657*75f6d617Schristos     {
658*75f6d617Schristos       end0 = p0;	/* Addr of last char in file 0.  */
659*75f6d617Schristos 
660*75f6d617Schristos       /* Get value of P0 at which we should stop scanning backward:
661*75f6d617Schristos 	 this is when either P0 or P1 points just past the last char
662*75f6d617Schristos 	 of the identical prefix.  */
663*75f6d617Schristos       beg0 = filevec[0].prefix_end + (n0 < n1 ? 0 : n0 - n1);
664*75f6d617Schristos 
665*75f6d617Schristos       /* Scan back until chars don't match or we reach that point.  */
666*75f6d617Schristos       for (; p0 != beg0; p0--, p1--)
667*75f6d617Schristos 	if (*p0 != *p1)
668*75f6d617Schristos 	  {
669*75f6d617Schristos 	    /* Point at the first char of the matching suffix.  */
670*75f6d617Schristos 	    beg0 = p0;
671*75f6d617Schristos 	    break;
672*75f6d617Schristos 	  }
673*75f6d617Schristos 
674*75f6d617Schristos       /* Are we at a line-beginning in both files?  If not, add the rest of
675*75f6d617Schristos 	 this line to the main body.  Discard up to HORIZON_LINES lines from
676*75f6d617Schristos 	 the identical suffix.  Also, discard one extra line,
677*75f6d617Schristos 	 because shift_boundaries may need it.  */
678*75f6d617Schristos       i = horizon_lines + !((buffer0 == p0 || p0[-1] == '\n')
679*75f6d617Schristos 			    &&
680*75f6d617Schristos 			    (buffer1 == p1 || p1[-1] == '\n'));
681*75f6d617Schristos       while (i-- && p0 != end0)
682*75f6d617Schristos 	while (*p0++ != '\n')
683*75f6d617Schristos 	  continue;
684*75f6d617Schristos 
685*75f6d617Schristos       p1 += p0 - beg0;
686*75f6d617Schristos     }
687*75f6d617Schristos 
688*75f6d617Schristos   /* Record the suffix.  */
689*75f6d617Schristos   filevec[0].suffix_begin = p0;
690*75f6d617Schristos   filevec[1].suffix_begin = p1;
691*75f6d617Schristos 
692*75f6d617Schristos   /* Calculate number of lines of prefix to save.
693*75f6d617Schristos 
694*75f6d617Schristos      prefix_count == 0 means save the whole prefix;
695*75f6d617Schristos      we need this for options like -D that output the whole file,
696*75f6d617Schristos      or for enormous contexts (to avoid worrying about arithmetic overflow).
697*75f6d617Schristos      We also need it for options like -F that output some preceding line;
698*75f6d617Schristos      at least we will need to find the last few lines,
699*75f6d617Schristos      but since we don't know how many, it's easiest to find them all.
700*75f6d617Schristos 
701*75f6d617Schristos      Otherwise, prefix_count != 0.  Save just prefix_count lines at start
702*75f6d617Schristos      of the line buffer; they'll be moved to the proper location later.
703*75f6d617Schristos      Handle 1 more line than the context says (because we count 1 too many),
704*75f6d617Schristos      rounded up to the next power of 2 to speed index computation.  */
705*75f6d617Schristos 
706*75f6d617Schristos   if (no_diff_means_no_output && ! function_regexp.fastmap
707*75f6d617Schristos       && context < LIN_MAX / 4 && context < n0)
708*75f6d617Schristos     {
709*75f6d617Schristos       middle_guess = guess_lines (0, 0, p0 - filevec[0].prefix_end);
710*75f6d617Schristos       suffix_guess = guess_lines (0, 0, buffer0 + n0 - p0);
711*75f6d617Schristos       for (prefix_count = 1;  prefix_count <= context;  prefix_count *= 2)
712*75f6d617Schristos 	continue;
713*75f6d617Schristos       alloc_lines0 = (prefix_count + middle_guess
714*75f6d617Schristos 		      + MIN (context, suffix_guess));
715*75f6d617Schristos     }
716*75f6d617Schristos   else
717*75f6d617Schristos     {
718*75f6d617Schristos       prefix_count = 0;
719*75f6d617Schristos       alloc_lines0 = guess_lines (0, 0, n0);
720*75f6d617Schristos     }
721*75f6d617Schristos 
722*75f6d617Schristos   prefix_mask = prefix_count - 1;
723*75f6d617Schristos   lines = 0;
724*75f6d617Schristos   linbuf0 = xmalloc (alloc_lines0 * sizeof *linbuf0);
725*75f6d617Schristos   p0 = buffer0;
726*75f6d617Schristos 
727*75f6d617Schristos   /* If the prefix is needed, find the prefix lines.  */
728*75f6d617Schristos   if (! (no_diff_means_no_output
729*75f6d617Schristos 	 && filevec[0].prefix_end == p0
730*75f6d617Schristos 	 && filevec[1].prefix_end == p1))
731*75f6d617Schristos     {
732*75f6d617Schristos       end0 = filevec[0].prefix_end;
733*75f6d617Schristos       while (p0 != end0)
734*75f6d617Schristos 	{
735*75f6d617Schristos 	  lin l = lines++ & prefix_mask;
736*75f6d617Schristos 	  if (l == alloc_lines0)
737*75f6d617Schristos 	    {
738*75f6d617Schristos 	      if (PTRDIFF_MAX / (2 * sizeof *linbuf0) <= alloc_lines0)
739*75f6d617Schristos 		xalloc_die ();
740*75f6d617Schristos 	      alloc_lines0 *= 2;
741*75f6d617Schristos 	      linbuf0 = xrealloc (linbuf0, alloc_lines0 * sizeof *linbuf0);
742*75f6d617Schristos 	    }
743*75f6d617Schristos 	  linbuf0[l] = p0;
744*75f6d617Schristos 	  while (*p0++ != '\n')
745*75f6d617Schristos 	    continue;
746*75f6d617Schristos 	}
747*75f6d617Schristos     }
748*75f6d617Schristos   buffered_prefix = prefix_count && context < lines ? context : lines;
749*75f6d617Schristos 
750*75f6d617Schristos   /* Allocate line buffer 1.  */
751*75f6d617Schristos 
752*75f6d617Schristos   middle_guess = guess_lines (lines, p0 - buffer0, p1 - filevec[1].prefix_end);
753*75f6d617Schristos   suffix_guess = guess_lines (lines, p0 - buffer0, buffer1 + n1 - p1);
754*75f6d617Schristos   alloc_lines1 = buffered_prefix + middle_guess + MIN (context, suffix_guess);
755*75f6d617Schristos   if (alloc_lines1 < buffered_prefix
756*75f6d617Schristos       || PTRDIFF_MAX / sizeof *linbuf1 <= alloc_lines1)
757*75f6d617Schristos     xalloc_die ();
758*75f6d617Schristos   linbuf1 = xmalloc (alloc_lines1 * sizeof *linbuf1);
759*75f6d617Schristos 
760*75f6d617Schristos   if (buffered_prefix != lines)
761*75f6d617Schristos     {
762*75f6d617Schristos       /* Rotate prefix lines to proper location.  */
763*75f6d617Schristos       for (i = 0;  i < buffered_prefix;  i++)
764*75f6d617Schristos 	linbuf1[i] = linbuf0[(lines - context + i) & prefix_mask];
765*75f6d617Schristos       for (i = 0;  i < buffered_prefix;  i++)
766*75f6d617Schristos 	linbuf0[i] = linbuf1[i];
767*75f6d617Schristos     }
768*75f6d617Schristos 
769*75f6d617Schristos   /* Initialize line buffer 1 from line buffer 0.  */
770*75f6d617Schristos   for (i = 0; i < buffered_prefix; i++)
771*75f6d617Schristos     linbuf1[i] = linbuf0[i] - buffer0 + buffer1;
772*75f6d617Schristos 
773*75f6d617Schristos   /* Record the line buffer, adjusted so that
774*75f6d617Schristos      linbuf[0] points at the first differing line.  */
775*75f6d617Schristos   filevec[0].linbuf = linbuf0 + buffered_prefix;
776*75f6d617Schristos   filevec[1].linbuf = linbuf1 + buffered_prefix;
777*75f6d617Schristos   filevec[0].linbuf_base = filevec[1].linbuf_base = - buffered_prefix;
778*75f6d617Schristos   filevec[0].alloc_lines = alloc_lines0 - buffered_prefix;
779*75f6d617Schristos   filevec[1].alloc_lines = alloc_lines1 - buffered_prefix;
780*75f6d617Schristos   filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
781*75f6d617Schristos }
782*75f6d617Schristos 
783*75f6d617Schristos /* If 1 < k, then (2**k - prime_offset[k]) is the largest prime less
784*75f6d617Schristos    than 2**k.  This table is derived from Chris K. Caldwell's list
785*75f6d617Schristos    <http://www.utm.edu/research/primes/lists/2small/>.  */
786*75f6d617Schristos 
787*75f6d617Schristos static unsigned char const prime_offset[] =
788*75f6d617Schristos {
789*75f6d617Schristos   0, 0, 1, 1, 3, 1, 3, 1, 5, 3, 3, 9, 3, 1, 3, 19, 15, 1, 5, 1, 3, 9, 3,
790*75f6d617Schristos   15, 3, 39, 5, 39, 57, 3, 35, 1, 5, 9, 41, 31, 5, 25, 45, 7, 87, 21,
791*75f6d617Schristos   11, 57, 17, 55, 21, 115, 59, 81, 27, 129, 47, 111, 33, 55, 5, 13, 27,
792*75f6d617Schristos   55, 93, 1, 57, 25
793*75f6d617Schristos };
794*75f6d617Schristos 
795*75f6d617Schristos /* Verify that this host's size_t is not too wide for the above table.  */
796*75f6d617Schristos 
797*75f6d617Schristos verify (enough_prime_offsets,
798*75f6d617Schristos 	sizeof (size_t) * CHAR_BIT <= sizeof prime_offset);
799*75f6d617Schristos 
800*75f6d617Schristos /* Given a vector of two file_data objects, read the file associated
801*75f6d617Schristos    with each one, and build the table of equivalence classes.
802*75f6d617Schristos    Return nonzero if either file appears to be a binary file.
803*75f6d617Schristos    If PRETEND_BINARY is nonzero, pretend they are binary regardless.  */
804*75f6d617Schristos 
805*75f6d617Schristos bool
read_files(struct file_data filevec[],bool pretend_binary)806*75f6d617Schristos read_files (struct file_data filevec[], bool pretend_binary)
807*75f6d617Schristos {
808*75f6d617Schristos   int i;
809*75f6d617Schristos   bool skip_test = text | pretend_binary;
810*75f6d617Schristos   bool appears_binary = pretend_binary | sip (&filevec[0], skip_test);
811*75f6d617Schristos 
812*75f6d617Schristos   if (filevec[0].desc != filevec[1].desc)
813*75f6d617Schristos     appears_binary |= sip (&filevec[1], skip_test | appears_binary);
814*75f6d617Schristos   else
815*75f6d617Schristos     {
816*75f6d617Schristos       filevec[1].buffer = filevec[0].buffer;
817*75f6d617Schristos       filevec[1].bufsize = filevec[0].bufsize;
818*75f6d617Schristos       filevec[1].buffered = filevec[0].buffered;
819*75f6d617Schristos     }
820*75f6d617Schristos   if (appears_binary)
821*75f6d617Schristos     {
822*75f6d617Schristos       set_binary_mode (filevec[0].desc, 1);
823*75f6d617Schristos       set_binary_mode (filevec[1].desc, 1);
824*75f6d617Schristos       return 1;
825*75f6d617Schristos     }
826*75f6d617Schristos 
827*75f6d617Schristos   find_identical_ends (filevec);
828*75f6d617Schristos 
829*75f6d617Schristos   equivs_alloc = filevec[0].alloc_lines + filevec[1].alloc_lines + 1;
830*75f6d617Schristos   if (PTRDIFF_MAX / sizeof *equivs <= equivs_alloc)
831*75f6d617Schristos     xalloc_die ();
832*75f6d617Schristos   equivs = xmalloc (equivs_alloc * sizeof *equivs);
833*75f6d617Schristos   /* Equivalence class 0 is permanently safe for lines that were not
834*75f6d617Schristos      hashed.  Real equivalence classes start at 1.  */
835*75f6d617Schristos   equivs_index = 1;
836*75f6d617Schristos 
837*75f6d617Schristos   /* Allocate (one plus) a prime number of hash buckets.  Use a prime
838*75f6d617Schristos      number between 1/3 and 2/3 of the value of equiv_allocs,
839*75f6d617Schristos      approximately.  */
840*75f6d617Schristos   for (i = 9; (size_t) 1 << i < equivs_alloc / 3; i++)
841*75f6d617Schristos     continue;
842*75f6d617Schristos   nbuckets = ((size_t) 1 << i) - prime_offset[i];
843*75f6d617Schristos   if (PTRDIFF_MAX / sizeof *buckets <= nbuckets)
844*75f6d617Schristos     xalloc_die ();
845*75f6d617Schristos   buckets = zalloc ((nbuckets + 1) * sizeof *buckets);
846*75f6d617Schristos   buckets++;
847*75f6d617Schristos 
848*75f6d617Schristos   for (i = 0; i < 2; i++)
849*75f6d617Schristos     find_and_hash_each_line (&filevec[i]);
850*75f6d617Schristos 
851*75f6d617Schristos   filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
852*75f6d617Schristos 
853*75f6d617Schristos   free (equivs);
854*75f6d617Schristos   free (buckets - 1);
855*75f6d617Schristos 
856*75f6d617Schristos   return 0;
857*75f6d617Schristos }
858