Blame


1 574ed2c3 2017-11-29 stsp /* $OpenBSD: diffreg.c,v 1.91 2016/03/01 20:57:35 natano Exp $ */
2 574ed2c3 2017-11-29 stsp
3 574ed2c3 2017-11-29 stsp /*
4 574ed2c3 2017-11-29 stsp * Copyright (C) Caldera International Inc. 2001-2002.
5 574ed2c3 2017-11-29 stsp * All rights reserved.
6 574ed2c3 2017-11-29 stsp *
7 574ed2c3 2017-11-29 stsp * Redistribution and use in source and binary forms, with or without
8 574ed2c3 2017-11-29 stsp * modification, are permitted provided that the following conditions
9 574ed2c3 2017-11-29 stsp * are met:
10 574ed2c3 2017-11-29 stsp * 1. Redistributions of source code and documentation must retain the above
11 574ed2c3 2017-11-29 stsp * copyright notice, this list of conditions and the following disclaimer.
12 574ed2c3 2017-11-29 stsp * 2. Redistributions in binary form must reproduce the above copyright
13 574ed2c3 2017-11-29 stsp * notice, this list of conditions and the following disclaimer in the
14 574ed2c3 2017-11-29 stsp * documentation and/or other materials provided with the distribution.
15 574ed2c3 2017-11-29 stsp * 3. All advertising materials mentioning features or use of this software
16 574ed2c3 2017-11-29 stsp * must display the following acknowledgement:
17 574ed2c3 2017-11-29 stsp * This product includes software developed or owned by Caldera
18 574ed2c3 2017-11-29 stsp * International, Inc.
19 574ed2c3 2017-11-29 stsp * 4. Neither the name of Caldera International, Inc. nor the names of other
20 574ed2c3 2017-11-29 stsp * contributors may be used to endorse or promote products derived from
21 574ed2c3 2017-11-29 stsp * this software without specific prior written permission.
22 574ed2c3 2017-11-29 stsp *
23 574ed2c3 2017-11-29 stsp * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24 574ed2c3 2017-11-29 stsp * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25 574ed2c3 2017-11-29 stsp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 574ed2c3 2017-11-29 stsp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 574ed2c3 2017-11-29 stsp * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28 574ed2c3 2017-11-29 stsp * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 574ed2c3 2017-11-29 stsp * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 574ed2c3 2017-11-29 stsp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 574ed2c3 2017-11-29 stsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 574ed2c3 2017-11-29 stsp * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 574ed2c3 2017-11-29 stsp * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 574ed2c3 2017-11-29 stsp * POSSIBILITY OF SUCH DAMAGE.
35 574ed2c3 2017-11-29 stsp */
36 574ed2c3 2017-11-29 stsp /*-
37 574ed2c3 2017-11-29 stsp * Copyright (c) 1991, 1993
38 574ed2c3 2017-11-29 stsp * The Regents of the University of California. All rights reserved.
39 574ed2c3 2017-11-29 stsp *
40 574ed2c3 2017-11-29 stsp * Redistribution and use in source and binary forms, with or without
41 574ed2c3 2017-11-29 stsp * modification, are permitted provided that the following conditions
42 574ed2c3 2017-11-29 stsp * are met:
43 574ed2c3 2017-11-29 stsp * 1. Redistributions of source code must retain the above copyright
44 574ed2c3 2017-11-29 stsp * notice, this list of conditions and the following disclaimer.
45 574ed2c3 2017-11-29 stsp * 2. Redistributions in binary form must reproduce the above copyright
46 574ed2c3 2017-11-29 stsp * notice, this list of conditions and the following disclaimer in the
47 574ed2c3 2017-11-29 stsp * documentation and/or other materials provided with the distribution.
48 574ed2c3 2017-11-29 stsp * 3. Neither the name of the University nor the names of its contributors
49 574ed2c3 2017-11-29 stsp * may be used to endorse or promote products derived from this software
50 574ed2c3 2017-11-29 stsp * without specific prior written permission.
51 574ed2c3 2017-11-29 stsp *
52 574ed2c3 2017-11-29 stsp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 574ed2c3 2017-11-29 stsp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 574ed2c3 2017-11-29 stsp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 574ed2c3 2017-11-29 stsp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 574ed2c3 2017-11-29 stsp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 574ed2c3 2017-11-29 stsp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 574ed2c3 2017-11-29 stsp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 574ed2c3 2017-11-29 stsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 574ed2c3 2017-11-29 stsp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 574ed2c3 2017-11-29 stsp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 574ed2c3 2017-11-29 stsp * SUCH DAMAGE.
63 574ed2c3 2017-11-29 stsp *
64 574ed2c3 2017-11-29 stsp * @(#)diffreg.c 8.1 (Berkeley) 6/6/93
65 574ed2c3 2017-11-29 stsp */
66 574ed2c3 2017-11-29 stsp
67 574ed2c3 2017-11-29 stsp #include <sys/stat.h>
68 574ed2c3 2017-11-29 stsp #include <sys/wait.h>
69 7d283eee 2017-11-29 stsp #include <sys/queue.h>
70 574ed2c3 2017-11-29 stsp
71 574ed2c3 2017-11-29 stsp #include <ctype.h>
72 574ed2c3 2017-11-29 stsp #include <err.h>
73 574ed2c3 2017-11-29 stsp #include <errno.h>
74 574ed2c3 2017-11-29 stsp #include <fcntl.h>
75 574ed2c3 2017-11-29 stsp #include <paths.h>
76 cb74ff21 2017-11-30 stsp #include <stdarg.h>
77 574ed2c3 2017-11-29 stsp #include <stddef.h>
78 574ed2c3 2017-11-29 stsp #include <stdint.h>
79 574ed2c3 2017-11-29 stsp #include <stdio.h>
80 574ed2c3 2017-11-29 stsp #include <stdlib.h>
81 574ed2c3 2017-11-29 stsp #include <string.h>
82 574ed2c3 2017-11-29 stsp #include <unistd.h>
83 574ed2c3 2017-11-29 stsp #include <limits.h>
84 7d283eee 2017-11-29 stsp #include <sha1.h>
85 7d283eee 2017-11-29 stsp #include <zlib.h>
86 574ed2c3 2017-11-29 stsp
87 7d283eee 2017-11-29 stsp #include "got_error.h"
88 7d283eee 2017-11-29 stsp #include "got_object.h"
89 7d283eee 2017-11-29 stsp #include "got_diff.h"
90 7d283eee 2017-11-29 stsp
91 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
92 574ed2c3 2017-11-29 stsp
93 574ed2c3 2017-11-29 stsp #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
94 574ed2c3 2017-11-29 stsp #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
95 574ed2c3 2017-11-29 stsp
96 574ed2c3 2017-11-29 stsp /*
97 574ed2c3 2017-11-29 stsp * diff - compare two files.
98 574ed2c3 2017-11-29 stsp */
99 574ed2c3 2017-11-29 stsp
100 574ed2c3 2017-11-29 stsp /*
101 574ed2c3 2017-11-29 stsp * Uses an algorithm due to Harold Stone, which finds
102 574ed2c3 2017-11-29 stsp * a pair of longest identical subsequences in the two
103 574ed2c3 2017-11-29 stsp * files.
104 574ed2c3 2017-11-29 stsp *
105 574ed2c3 2017-11-29 stsp * The major goal is to generate the match vector J.
106 574ed2c3 2017-11-29 stsp * J[i] is the index of the line in file1 corresponding
107 574ed2c3 2017-11-29 stsp * to line i file0. J[i] = 0 if there is no
108 574ed2c3 2017-11-29 stsp * such line in file1.
109 574ed2c3 2017-11-29 stsp *
110 574ed2c3 2017-11-29 stsp * Lines are hashed so as to work in core. All potential
111 574ed2c3 2017-11-29 stsp * matches are located by sorting the lines of each file
112 574ed2c3 2017-11-29 stsp * on the hash (called ``value''). In particular, this
113 574ed2c3 2017-11-29 stsp * collects the equivalence classes in file1 together.
114 574ed2c3 2017-11-29 stsp * Subroutine equiv replaces the value of each line in
115 574ed2c3 2017-11-29 stsp * file0 by the index of the first element of its
116 574ed2c3 2017-11-29 stsp * matching equivalence in (the reordered) file1.
117 574ed2c3 2017-11-29 stsp * To save space equiv squeezes file1 into a single
118 574ed2c3 2017-11-29 stsp * array member in which the equivalence classes
119 574ed2c3 2017-11-29 stsp * are simply concatenated, except that their first
120 574ed2c3 2017-11-29 stsp * members are flagged by changing sign.
121 574ed2c3 2017-11-29 stsp *
122 574ed2c3 2017-11-29 stsp * Next the indices that point into member are unsorted into
123 574ed2c3 2017-11-29 stsp * array class according to the original order of file0.
124 574ed2c3 2017-11-29 stsp *
125 574ed2c3 2017-11-29 stsp * The cleverness lies in routine stone. This marches
126 574ed2c3 2017-11-29 stsp * through the lines of file0, developing a vector klist
127 574ed2c3 2017-11-29 stsp * of "k-candidates". At step i a k-candidate is a matched
128 574ed2c3 2017-11-29 stsp * pair of lines x,y (x in file0 y in file1) such that
129 574ed2c3 2017-11-29 stsp * there is a common subsequence of length k
130 574ed2c3 2017-11-29 stsp * between the first i lines of file0 and the first y
131 574ed2c3 2017-11-29 stsp * lines of file1, but there is no such subsequence for
132 574ed2c3 2017-11-29 stsp * any smaller y. x is the earliest possible mate to y
133 574ed2c3 2017-11-29 stsp * that occurs in such a subsequence.
134 574ed2c3 2017-11-29 stsp *
135 574ed2c3 2017-11-29 stsp * Whenever any of the members of the equivalence class of
136 574ed2c3 2017-11-29 stsp * lines in file1 matable to a line in file0 has serial number
137 574ed2c3 2017-11-29 stsp * less than the y of some k-candidate, that k-candidate
138 574ed2c3 2017-11-29 stsp * with the smallest such y is replaced. The new
139 574ed2c3 2017-11-29 stsp * k-candidate is chained (via pred) to the current
140 574ed2c3 2017-11-29 stsp * k-1 candidate so that the actual subsequence can
141 574ed2c3 2017-11-29 stsp * be recovered. When a member has serial number greater
142 574ed2c3 2017-11-29 stsp * that the y of all k-candidates, the klist is extended.
143 574ed2c3 2017-11-29 stsp * At the end, the longest subsequence is pulled out
144 574ed2c3 2017-11-29 stsp * and placed in the array J by unravel
145 574ed2c3 2017-11-29 stsp *
146 574ed2c3 2017-11-29 stsp * With J in hand, the matches there recorded are
147 574ed2c3 2017-11-29 stsp * check'ed against reality to assure that no spurious
148 574ed2c3 2017-11-29 stsp * matches have crept in due to hashing. If they have,
149 574ed2c3 2017-11-29 stsp * they are broken, and "jackpot" is recorded--a harmless
150 574ed2c3 2017-11-29 stsp * matter except that a true match for a spuriously
151 574ed2c3 2017-11-29 stsp * mated line may now be unnecessarily reported as a change.
152 574ed2c3 2017-11-29 stsp *
153 574ed2c3 2017-11-29 stsp * Much of the complexity of the program comes simply
154 574ed2c3 2017-11-29 stsp * from trying to minimize core utilization and
155 574ed2c3 2017-11-29 stsp * maximize the range of doable problems by dynamically
156 574ed2c3 2017-11-29 stsp * allocating what is needed and reusing what is not.
157 574ed2c3 2017-11-29 stsp * The core requirements for problems larger than somewhat
158 574ed2c3 2017-11-29 stsp * are (in words) 2*length(file0) + length(file1) +
159 574ed2c3 2017-11-29 stsp * 3*(number of k-candidates installed), typically about
160 574ed2c3 2017-11-29 stsp * 6n words for files of length n.
161 574ed2c3 2017-11-29 stsp */
162 574ed2c3 2017-11-29 stsp
163 574ed2c3 2017-11-29 stsp struct cand {
164 574ed2c3 2017-11-29 stsp int x;
165 574ed2c3 2017-11-29 stsp int y;
166 574ed2c3 2017-11-29 stsp int pred;
167 574ed2c3 2017-11-29 stsp };
168 574ed2c3 2017-11-29 stsp
169 574ed2c3 2017-11-29 stsp struct line {
170 574ed2c3 2017-11-29 stsp int serial;
171 574ed2c3 2017-11-29 stsp int value;
172 8020fd50 2017-11-29 stsp };
173 574ed2c3 2017-11-29 stsp
174 cb74ff21 2017-11-30 stsp static void diff_output(FILE *, const char *, ...);
175 404c43c4 2018-06-21 stsp static int output(FILE *, struct got_diff_changes *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int);
176 ed9e98a8 2017-11-29 stsp static void check(struct got_diff_state *, FILE *, FILE *, int);
177 acb209ee 2019-02-08 stsp static void range(FILE *, int, int, char *);
178 cb74ff21 2017-11-30 stsp static void uni_range(FILE *, int, int);
179 404c43c4 2018-06-21 stsp static void dump_unified_vec(FILE *, struct got_diff_changes *, struct got_diff_state *, struct got_diff_args *, FILE *, FILE *, int);
180 322260e1 2018-01-26 mpi static int prepare(struct got_diff_state *, int, FILE *, off_t, int);
181 ed9e98a8 2017-11-29 stsp static void prune(struct got_diff_state *);
182 574ed2c3 2017-11-29 stsp static void equiv(struct line *, int, struct line *, int, int *);
183 ed9e98a8 2017-11-29 stsp static void unravel(struct got_diff_state *, int);
184 322260e1 2018-01-26 mpi static int unsort(struct line *, int, int *);
185 404c43c4 2018-06-21 stsp static int change(FILE *, struct got_diff_changes *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int, int, int, int, int *);
186 574ed2c3 2017-11-29 stsp static void sort(struct line *, int);
187 cb74ff21 2017-11-30 stsp static void print_header(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, const char *);
188 574ed2c3 2017-11-29 stsp static int asciifile(FILE *);
189 14ed98fd 2019-08-18 stsp static int fetch(FILE *, struct got_diff_state *, struct got_diff_args *, long *, int, int, FILE *, int, int);
190 322260e1 2018-01-26 mpi static int newcand(struct got_diff_state *, int, int, int, int *);
191 ed9e98a8 2017-11-29 stsp static int search(struct got_diff_state *, int *, int, int);
192 574ed2c3 2017-11-29 stsp static int skipline(FILE *);
193 574ed2c3 2017-11-29 stsp static int isqrt(int);
194 ed9e98a8 2017-11-29 stsp static int stone(struct got_diff_state *, int *, int, int *, int *, int);
195 ed9e98a8 2017-11-29 stsp static int readhash(struct got_diff_state *, FILE *, int);
196 1c7f0520 2017-11-29 stsp static int files_differ(struct got_diff_state *, FILE *, FILE *, int);
197 ed9e98a8 2017-11-29 stsp static char *match_function(struct got_diff_state *, const long *, int, FILE *);
198 574ed2c3 2017-11-29 stsp
199 574ed2c3 2017-11-29 stsp /*
200 574ed2c3 2017-11-29 stsp * chrtran points to one of 2 translation tables: cup2low if folding upper to
201 574ed2c3 2017-11-29 stsp * lower case clow2low if not folding case
202 574ed2c3 2017-11-29 stsp */
203 574ed2c3 2017-11-29 stsp u_char clow2low[256] = {
204 574ed2c3 2017-11-29 stsp 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
205 574ed2c3 2017-11-29 stsp 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
206 574ed2c3 2017-11-29 stsp 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
207 574ed2c3 2017-11-29 stsp 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
208 574ed2c3 2017-11-29 stsp 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
209 574ed2c3 2017-11-29 stsp 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
210 574ed2c3 2017-11-29 stsp 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
211 574ed2c3 2017-11-29 stsp 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
212 574ed2c3 2017-11-29 stsp 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
213 574ed2c3 2017-11-29 stsp 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
214 574ed2c3 2017-11-29 stsp 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
215 574ed2c3 2017-11-29 stsp 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
216 574ed2c3 2017-11-29 stsp 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
217 574ed2c3 2017-11-29 stsp 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
218 574ed2c3 2017-11-29 stsp 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
219 574ed2c3 2017-11-29 stsp 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
220 574ed2c3 2017-11-29 stsp 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
221 574ed2c3 2017-11-29 stsp 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
222 574ed2c3 2017-11-29 stsp 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
223 574ed2c3 2017-11-29 stsp 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
224 574ed2c3 2017-11-29 stsp 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
225 574ed2c3 2017-11-29 stsp 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
226 574ed2c3 2017-11-29 stsp 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
227 574ed2c3 2017-11-29 stsp 0xfd, 0xfe, 0xff
228 574ed2c3 2017-11-29 stsp };
229 574ed2c3 2017-11-29 stsp
230 574ed2c3 2017-11-29 stsp u_char cup2low[256] = {
231 574ed2c3 2017-11-29 stsp 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
232 574ed2c3 2017-11-29 stsp 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
233 574ed2c3 2017-11-29 stsp 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
234 574ed2c3 2017-11-29 stsp 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
235 574ed2c3 2017-11-29 stsp 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
236 574ed2c3 2017-11-29 stsp 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x60, 0x61,
237 574ed2c3 2017-11-29 stsp 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
238 574ed2c3 2017-11-29 stsp 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
239 574ed2c3 2017-11-29 stsp 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x60, 0x61, 0x62,
240 574ed2c3 2017-11-29 stsp 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
241 574ed2c3 2017-11-29 stsp 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
242 574ed2c3 2017-11-29 stsp 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
243 574ed2c3 2017-11-29 stsp 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
244 574ed2c3 2017-11-29 stsp 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
245 574ed2c3 2017-11-29 stsp 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
246 574ed2c3 2017-11-29 stsp 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
247 574ed2c3 2017-11-29 stsp 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
248 574ed2c3 2017-11-29 stsp 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
249 574ed2c3 2017-11-29 stsp 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
250 574ed2c3 2017-11-29 stsp 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
251 574ed2c3 2017-11-29 stsp 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
252 574ed2c3 2017-11-29 stsp 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
253 574ed2c3 2017-11-29 stsp 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
254 574ed2c3 2017-11-29 stsp 0xfd, 0xfe, 0xff
255 574ed2c3 2017-11-29 stsp };
256 574ed2c3 2017-11-29 stsp
257 cb74ff21 2017-11-30 stsp static void
258 cb74ff21 2017-11-30 stsp diff_output(FILE *outfile, const char *fmt, ...)
259 cb74ff21 2017-11-30 stsp {
260 cb74ff21 2017-11-30 stsp va_list ap;
261 cb74ff21 2017-11-30 stsp
262 404c43c4 2018-06-21 stsp if (outfile == NULL)
263 404c43c4 2018-06-21 stsp return;
264 404c43c4 2018-06-21 stsp
265 cb74ff21 2017-11-30 stsp va_start(ap, fmt);
266 cb74ff21 2017-11-30 stsp vfprintf(outfile, fmt, ap);
267 cb74ff21 2017-11-30 stsp va_end(ap);
268 dc424a06 2019-08-07 stsp }
269 dc424a06 2019-08-07 stsp
270 dc424a06 2019-08-07 stsp void
271 dc424a06 2019-08-07 stsp got_diff_state_free(struct got_diff_state *ds)
272 dc424a06 2019-08-07 stsp {
273 dc424a06 2019-08-07 stsp free(ds->J);
274 dc424a06 2019-08-07 stsp free(ds->member);
275 dc424a06 2019-08-07 stsp free(ds->class);
276 dc424a06 2019-08-07 stsp free(ds->clist);
277 dc424a06 2019-08-07 stsp free(ds->klist);
278 dc424a06 2019-08-07 stsp free(ds->ixold);
279 dc424a06 2019-08-07 stsp free(ds->ixnew);
280 cb74ff21 2017-11-30 stsp }
281 cb74ff21 2017-11-30 stsp
282 7d283eee 2017-11-29 stsp const struct got_error *
283 f9d67749 2017-11-30 stsp got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
284 404c43c4 2018-06-21 stsp struct got_diff_args *args, struct got_diff_state *ds, FILE *outfile,
285 404c43c4 2018-06-21 stsp struct got_diff_changes *changes)
286 574ed2c3 2017-11-29 stsp {
287 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
288 df51fc4e 2018-04-02 stsp int i, *p;
289 df51fc4e 2018-04-02 stsp long *lp;
290 7d283eee 2017-11-29 stsp
291 7d283eee 2017-11-29 stsp *rval = D_SAME;
292 ed9e98a8 2017-11-29 stsp ds->anychange = 0;
293 ed9e98a8 2017-11-29 stsp ds->lastline = 0;
294 ed9e98a8 2017-11-29 stsp ds->lastmatchline = 0;
295 ed9e98a8 2017-11-29 stsp ds->context_vec_ptr = ds->context_vec_start - 1;
296 4a8520aa 2018-10-18 stsp ds->max_context = GOT_DIFF_MAX_CONTEXT;
297 ed9e98a8 2017-11-29 stsp if (flags & D_IGNORECASE)
298 ed9e98a8 2017-11-29 stsp ds->chrtran = cup2low;
299 574ed2c3 2017-11-29 stsp else
300 ed9e98a8 2017-11-29 stsp ds->chrtran = clow2low;
301 1c7f0520 2017-11-29 stsp if (S_ISDIR(ds->stb1.st_mode) != S_ISDIR(ds->stb2.st_mode)) {
302 1c7f0520 2017-11-29 stsp *rval = (S_ISDIR(ds->stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
303 7d283eee 2017-11-29 stsp return NULL;
304 7d283eee 2017-11-29 stsp }
305 ecf6e46e 2018-04-01 stsp if (flags & D_EMPTY1) {
306 574ed2c3 2017-11-29 stsp f1 = fopen(_PATH_DEVNULL, "r");
307 bb5dcc10 2018-04-01 stsp if (f1 == NULL) {
308 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", _PATH_DEVNULL);
309 bb5dcc10 2018-04-01 stsp goto closem;
310 bb5dcc10 2018-04-01 stsp }
311 ecf6e46e 2018-04-01 stsp }
312 f9d67749 2017-11-30 stsp else if (f1 == NULL) {
313 8ba9a219 2017-11-29 stsp args->status |= 2;
314 574ed2c3 2017-11-29 stsp goto closem;
315 574ed2c3 2017-11-29 stsp }
316 574ed2c3 2017-11-29 stsp
317 ecf6e46e 2018-04-01 stsp if (flags & D_EMPTY2) {
318 574ed2c3 2017-11-29 stsp f2 = fopen(_PATH_DEVNULL, "r");
319 bb5dcc10 2018-04-01 stsp if (f2 == NULL) {
320 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", _PATH_DEVNULL);
321 bb5dcc10 2018-04-01 stsp goto closem;
322 bb5dcc10 2018-04-01 stsp }
323 ecf6e46e 2018-04-01 stsp } else if (f2 == NULL) {
324 8ba9a219 2017-11-29 stsp args->status |= 2;
325 574ed2c3 2017-11-29 stsp goto closem;
326 574ed2c3 2017-11-29 stsp }
327 574ed2c3 2017-11-29 stsp
328 1c7f0520 2017-11-29 stsp switch (files_differ(ds, f1, f2, flags)) {
329 574ed2c3 2017-11-29 stsp case 0:
330 574ed2c3 2017-11-29 stsp goto closem;
331 574ed2c3 2017-11-29 stsp case 1:
332 574ed2c3 2017-11-29 stsp break;
333 574ed2c3 2017-11-29 stsp default:
334 574ed2c3 2017-11-29 stsp /* error */
335 8ba9a219 2017-11-29 stsp args->status |= 2;
336 574ed2c3 2017-11-29 stsp goto closem;
337 574ed2c3 2017-11-29 stsp }
338 574ed2c3 2017-11-29 stsp
339 574ed2c3 2017-11-29 stsp if ((flags & D_FORCEASCII) == 0 &&
340 574ed2c3 2017-11-29 stsp (!asciifile(f1) || !asciifile(f2))) {
341 7d283eee 2017-11-29 stsp *rval = D_BINARY;
342 8ba9a219 2017-11-29 stsp args->status |= 1;
343 574ed2c3 2017-11-29 stsp goto closem;
344 574ed2c3 2017-11-29 stsp }
345 322260e1 2018-01-26 mpi if (prepare(ds, 0, f1, ds->stb1.st_size, flags)) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("prepare");
347 322260e1 2018-01-26 mpi goto closem;
348 322260e1 2018-01-26 mpi }
349 322260e1 2018-01-26 mpi if (prepare(ds, 1, f2, ds->stb2.st_size, flags)) {
350 638f9024 2019-05-13 stsp err = got_error_from_errno("prepare");
351 322260e1 2018-01-26 mpi goto closem;
352 322260e1 2018-01-26 mpi }
353 574ed2c3 2017-11-29 stsp
354 ed9e98a8 2017-11-29 stsp prune(ds);
355 ed9e98a8 2017-11-29 stsp sort(ds->sfile[0], ds->slen[0]);
356 ed9e98a8 2017-11-29 stsp sort(ds->sfile[1], ds->slen[1]);
357 574ed2c3 2017-11-29 stsp
358 ed9e98a8 2017-11-29 stsp ds->member = (int *)ds->file[1];
359 ed9e98a8 2017-11-29 stsp equiv(ds->sfile[0], ds->slen[0], ds->sfile[1], ds->slen[1], ds->member);
360 df51fc4e 2018-04-02 stsp p = reallocarray(ds->member, ds->slen[1] + 2, sizeof(*ds->member));
361 df51fc4e 2018-04-02 stsp if (p == NULL) {
362 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
363 7d283eee 2017-11-29 stsp goto closem;
364 7d283eee 2017-11-29 stsp }
365 df51fc4e 2018-04-02 stsp ds->member = p;
366 574ed2c3 2017-11-29 stsp
367 ed9e98a8 2017-11-29 stsp ds->class = (int *)ds->file[0];
368 322260e1 2018-01-26 mpi if (unsort(ds->sfile[0], ds->slen[0], ds->class)) {
369 638f9024 2019-05-13 stsp err = got_error_from_errno("unsort");
370 322260e1 2018-01-26 mpi goto closem;
371 322260e1 2018-01-26 mpi }
372 df51fc4e 2018-04-02 stsp p = reallocarray(ds->class, ds->slen[0] + 2, sizeof(*ds->class));
373 df51fc4e 2018-04-02 stsp if (p == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
375 7d283eee 2017-11-29 stsp goto closem;
376 7d283eee 2017-11-29 stsp }
377 df51fc4e 2018-04-02 stsp ds->class = p;
378 574ed2c3 2017-11-29 stsp
379 ed9e98a8 2017-11-29 stsp ds->klist = calloc(ds->slen[0] + 2, sizeof(*ds->klist));
380 ed9e98a8 2017-11-29 stsp if (ds->klist == NULL) {
381 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
382 7d283eee 2017-11-29 stsp goto closem;
383 7d283eee 2017-11-29 stsp }
384 ed9e98a8 2017-11-29 stsp ds->clen = 0;
385 ed9e98a8 2017-11-29 stsp ds->clistlen = 100;
386 ed9e98a8 2017-11-29 stsp ds->clist = calloc(ds->clistlen, sizeof(*ds->clist));
387 ed9e98a8 2017-11-29 stsp if (ds->clist == NULL) {
388 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
389 7d283eee 2017-11-29 stsp goto closem;
390 7d283eee 2017-11-29 stsp }
391 ed9e98a8 2017-11-29 stsp i = stone(ds, ds->class, ds->slen[0], ds->member, ds->klist, flags);
392 0a585a0d 2018-03-17 stsp if (i < 0) {
393 638f9024 2019-05-13 stsp err = got_error_from_errno("stone");
394 322260e1 2018-01-26 mpi goto closem;
395 322260e1 2018-01-26 mpi }
396 574ed2c3 2017-11-29 stsp
397 df51fc4e 2018-04-02 stsp p = reallocarray(ds->J, ds->len[0] + 2, sizeof(*ds->J));
398 df51fc4e 2018-04-02 stsp if (p == NULL) {
399 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
400 7d283eee 2017-11-29 stsp goto closem;
401 7d283eee 2017-11-29 stsp }
402 df51fc4e 2018-04-02 stsp ds->J = p;
403 ed9e98a8 2017-11-29 stsp unravel(ds, ds->klist[i]);
404 574ed2c3 2017-11-29 stsp
405 df51fc4e 2018-04-02 stsp lp = reallocarray(ds->ixold, ds->len[0] + 2, sizeof(*ds->ixold));
406 df51fc4e 2018-04-02 stsp if (lp == NULL) {
407 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
408 7d283eee 2017-11-29 stsp goto closem;
409 7d283eee 2017-11-29 stsp }
410 df51fc4e 2018-04-02 stsp ds->ixold = lp;
411 df51fc4e 2018-04-02 stsp lp = reallocarray(ds->ixnew, ds->len[1] + 2, sizeof(*ds->ixnew));
412 df51fc4e 2018-04-02 stsp if (lp == NULL) {
413 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
414 7d283eee 2017-11-29 stsp goto closem;
415 7d283eee 2017-11-29 stsp }
416 df51fc4e 2018-04-02 stsp ds->ixnew = lp;
417 ed9e98a8 2017-11-29 stsp check(ds, f1, f2, flags);
418 404c43c4 2018-06-21 stsp if (output(outfile, changes, ds, args, args->label[0], f1,
419 404c43c4 2018-06-21 stsp args->label[1], f2, flags))
420 638f9024 2019-05-13 stsp err = got_error_from_errno("output");
421 574ed2c3 2017-11-29 stsp closem:
422 ed9e98a8 2017-11-29 stsp if (ds->anychange) {
423 8ba9a219 2017-11-29 stsp args->status |= 1;
424 7d283eee 2017-11-29 stsp if (*rval == D_SAME)
425 7d283eee 2017-11-29 stsp *rval = D_DIFFER;
426 574ed2c3 2017-11-29 stsp }
427 8cbe5809 2019-05-21 stsp if ((flags & D_EMPTY1) && f1) {
428 8cbe5809 2019-05-21 stsp if (fclose(f1) != 0 && err == NULL)
429 8cbe5809 2019-05-21 stsp err = got_error_from_errno("fclose");
430 8cbe5809 2019-05-21 stsp }
431 8cbe5809 2019-05-21 stsp if ((flags & D_EMPTY2) && f2) {
432 8cbe5809 2019-05-21 stsp if (fclose(f2) != 0 && err == NULL)
433 8cbe5809 2019-05-21 stsp err = got_error_from_errno("fclose");
434 8cbe5809 2019-05-21 stsp }
435 7d283eee 2017-11-29 stsp return (err);
436 574ed2c3 2017-11-29 stsp }
437 574ed2c3 2017-11-29 stsp
438 574ed2c3 2017-11-29 stsp /*
439 574ed2c3 2017-11-29 stsp * Check to see if the given files differ.
440 574ed2c3 2017-11-29 stsp * Returns 0 if they are the same, 1 if different, and -1 on error.
441 574ed2c3 2017-11-29 stsp * XXX - could use code from cmp(1) [faster]
442 574ed2c3 2017-11-29 stsp */
443 574ed2c3 2017-11-29 stsp static int
444 1c7f0520 2017-11-29 stsp files_differ(struct got_diff_state *ds, FILE *f1, FILE *f2, int flags)
445 574ed2c3 2017-11-29 stsp {
446 574ed2c3 2017-11-29 stsp char buf1[BUFSIZ], buf2[BUFSIZ];
447 574ed2c3 2017-11-29 stsp size_t i, j;
448 574ed2c3 2017-11-29 stsp
449 1c7f0520 2017-11-29 stsp if ((flags & (D_EMPTY1|D_EMPTY2)) || ds->stb1.st_size != ds->stb2.st_size ||
450 1c7f0520 2017-11-29 stsp (ds->stb1.st_mode & S_IFMT) != (ds->stb2.st_mode & S_IFMT))
451 574ed2c3 2017-11-29 stsp return (1);
452 574ed2c3 2017-11-29 stsp for (;;) {
453 574ed2c3 2017-11-29 stsp i = fread(buf1, 1, sizeof(buf1), f1);
454 574ed2c3 2017-11-29 stsp j = fread(buf2, 1, sizeof(buf2), f2);
455 574ed2c3 2017-11-29 stsp if ((!i && ferror(f1)) || (!j && ferror(f2)))
456 574ed2c3 2017-11-29 stsp return (-1);
457 574ed2c3 2017-11-29 stsp if (i != j)
458 574ed2c3 2017-11-29 stsp return (1);
459 574ed2c3 2017-11-29 stsp if (i == 0)
460 574ed2c3 2017-11-29 stsp return (0);
461 574ed2c3 2017-11-29 stsp if (memcmp(buf1, buf2, i) != 0)
462 574ed2c3 2017-11-29 stsp return (1);
463 574ed2c3 2017-11-29 stsp }
464 574ed2c3 2017-11-29 stsp }
465 574ed2c3 2017-11-29 stsp
466 322260e1 2018-01-26 mpi static int
467 ed9e98a8 2017-11-29 stsp prepare(struct got_diff_state *ds, int i, FILE *fd, off_t filesize, int flags)
468 574ed2c3 2017-11-29 stsp {
469 df51fc4e 2018-04-02 stsp struct line *p, *q;
470 574ed2c3 2017-11-29 stsp int j, h;
471 574ed2c3 2017-11-29 stsp size_t sz;
472 574ed2c3 2017-11-29 stsp
473 574ed2c3 2017-11-29 stsp rewind(fd);
474 574ed2c3 2017-11-29 stsp
475 574ed2c3 2017-11-29 stsp sz = (filesize <= SIZE_MAX ? filesize : SIZE_MAX) / 25;
476 574ed2c3 2017-11-29 stsp if (sz < 100)
477 574ed2c3 2017-11-29 stsp sz = 100;
478 574ed2c3 2017-11-29 stsp
479 322260e1 2018-01-26 mpi p = calloc(sz + 3, sizeof(*p));
480 322260e1 2018-01-26 mpi if (p == NULL)
481 322260e1 2018-01-26 mpi return (-1);
482 ed9e98a8 2017-11-29 stsp for (j = 0; (h = readhash(ds, fd, flags));) {
483 574ed2c3 2017-11-29 stsp if (j == sz) {
484 574ed2c3 2017-11-29 stsp sz = sz * 3 / 2;
485 df51fc4e 2018-04-02 stsp q = reallocarray(p, sz + 3, sizeof(*p));
486 df51fc4e 2018-04-02 stsp if (q == NULL) {
487 df51fc4e 2018-04-02 stsp free(p);
488 322260e1 2018-01-26 mpi return (-1);
489 df51fc4e 2018-04-02 stsp }
490 df51fc4e 2018-04-02 stsp p = q;
491 574ed2c3 2017-11-29 stsp }
492 574ed2c3 2017-11-29 stsp p[++j].value = h;
493 574ed2c3 2017-11-29 stsp }
494 ed9e98a8 2017-11-29 stsp ds->len[i] = j;
495 ed9e98a8 2017-11-29 stsp ds->file[i] = p;
496 322260e1 2018-01-26 mpi
497 322260e1 2018-01-26 mpi return (0);
498 574ed2c3 2017-11-29 stsp }
499 574ed2c3 2017-11-29 stsp
500 574ed2c3 2017-11-29 stsp static void
501 ed9e98a8 2017-11-29 stsp prune(struct got_diff_state *ds)
502 574ed2c3 2017-11-29 stsp {
503 574ed2c3 2017-11-29 stsp int i, j;
504 574ed2c3 2017-11-29 stsp
505 ed9e98a8 2017-11-29 stsp for (ds->pref = 0; ds->pref < ds->len[0] && ds->pref < ds->len[1] &&
506 ed9e98a8 2017-11-29 stsp ds->file[0][ds->pref + 1].value == ds->file[1][ds->pref + 1].value;
507 ed9e98a8 2017-11-29 stsp ds->pref++)
508 574ed2c3 2017-11-29 stsp ;
509 ed9e98a8 2017-11-29 stsp for (ds->suff = 0; ds->suff < ds->len[0] - ds->pref && ds->suff < ds->len[1] - ds->pref &&
510 ed9e98a8 2017-11-29 stsp ds->file[0][ds->len[0] - ds->suff].value == ds->file[1][ds->len[1] - ds->suff].value;
511 ed9e98a8 2017-11-29 stsp ds->suff++)
512 574ed2c3 2017-11-29 stsp ;
513 574ed2c3 2017-11-29 stsp for (j = 0; j < 2; j++) {
514 ed9e98a8 2017-11-29 stsp ds->sfile[j] = ds->file[j] + ds->pref;
515 ed9e98a8 2017-11-29 stsp ds->slen[j] = ds->len[j] - ds->pref - ds->suff;
516 ed9e98a8 2017-11-29 stsp for (i = 0; i <= ds->slen[j]; i++)
517 ed9e98a8 2017-11-29 stsp ds->sfile[j][i].serial = i;
518 574ed2c3 2017-11-29 stsp }
519 574ed2c3 2017-11-29 stsp }
520 574ed2c3 2017-11-29 stsp
521 574ed2c3 2017-11-29 stsp static void
522 574ed2c3 2017-11-29 stsp equiv(struct line *a, int n, struct line *b, int m, int *c)
523 574ed2c3 2017-11-29 stsp {
524 574ed2c3 2017-11-29 stsp int i, j;
525 574ed2c3 2017-11-29 stsp
526 574ed2c3 2017-11-29 stsp i = j = 1;
527 574ed2c3 2017-11-29 stsp while (i <= n && j <= m) {
528 574ed2c3 2017-11-29 stsp if (a[i].value < b[j].value)
529 574ed2c3 2017-11-29 stsp a[i++].value = 0;
530 574ed2c3 2017-11-29 stsp else if (a[i].value == b[j].value)
531 574ed2c3 2017-11-29 stsp a[i++].value = j;
532 574ed2c3 2017-11-29 stsp else
533 574ed2c3 2017-11-29 stsp j++;
534 574ed2c3 2017-11-29 stsp }
535 574ed2c3 2017-11-29 stsp while (i <= n)
536 574ed2c3 2017-11-29 stsp a[i++].value = 0;
537 574ed2c3 2017-11-29 stsp b[m + 1].value = 0;
538 574ed2c3 2017-11-29 stsp j = 0;
539 574ed2c3 2017-11-29 stsp while (++j <= m) {
540 574ed2c3 2017-11-29 stsp c[j] = -b[j].serial;
541 574ed2c3 2017-11-29 stsp while (b[j + 1].value == b[j].value) {
542 574ed2c3 2017-11-29 stsp j++;
543 574ed2c3 2017-11-29 stsp c[j] = b[j].serial;
544 574ed2c3 2017-11-29 stsp }
545 574ed2c3 2017-11-29 stsp }
546 574ed2c3 2017-11-29 stsp c[j] = -1;
547 574ed2c3 2017-11-29 stsp }
548 574ed2c3 2017-11-29 stsp
549 574ed2c3 2017-11-29 stsp /* Code taken from ping.c */
550 574ed2c3 2017-11-29 stsp static int
551 574ed2c3 2017-11-29 stsp isqrt(int n)
552 574ed2c3 2017-11-29 stsp {
553 574ed2c3 2017-11-29 stsp int y, x = 1;
554 574ed2c3 2017-11-29 stsp
555 574ed2c3 2017-11-29 stsp if (n == 0)
556 574ed2c3 2017-11-29 stsp return (0);
557 574ed2c3 2017-11-29 stsp
558 574ed2c3 2017-11-29 stsp do { /* newton was a stinker */
559 574ed2c3 2017-11-29 stsp y = x;
560 574ed2c3 2017-11-29 stsp x = n / x;
561 574ed2c3 2017-11-29 stsp x += y;
562 574ed2c3 2017-11-29 stsp x /= 2;
563 574ed2c3 2017-11-29 stsp } while ((x - y) > 1 || (x - y) < -1);
564 574ed2c3 2017-11-29 stsp
565 574ed2c3 2017-11-29 stsp return (x);
566 574ed2c3 2017-11-29 stsp }
567 574ed2c3 2017-11-29 stsp
568 574ed2c3 2017-11-29 stsp static int
569 ed9e98a8 2017-11-29 stsp stone(struct got_diff_state *ds, int *a, int n, int *b, int *c, int flags)
570 574ed2c3 2017-11-29 stsp {
571 574ed2c3 2017-11-29 stsp int i, k, y, j, l;
572 574ed2c3 2017-11-29 stsp int oldc, tc, oldl, sq;
573 574ed2c3 2017-11-29 stsp u_int numtries, bound;
574 322260e1 2018-01-26 mpi int error;
575 574ed2c3 2017-11-29 stsp
576 574ed2c3 2017-11-29 stsp if (flags & D_MINIMAL)
577 574ed2c3 2017-11-29 stsp bound = UINT_MAX;
578 574ed2c3 2017-11-29 stsp else {
579 574ed2c3 2017-11-29 stsp sq = isqrt(n);
580 574ed2c3 2017-11-29 stsp bound = MAXIMUM(256, sq);
581 574ed2c3 2017-11-29 stsp }
582 574ed2c3 2017-11-29 stsp
583 574ed2c3 2017-11-29 stsp k = 0;
584 322260e1 2018-01-26 mpi c[0] = newcand(ds, 0, 0, 0, &error);
585 322260e1 2018-01-26 mpi if (error)
586 322260e1 2018-01-26 mpi return -1;
587 574ed2c3 2017-11-29 stsp for (i = 1; i <= n; i++) {
588 574ed2c3 2017-11-29 stsp j = a[i];
589 574ed2c3 2017-11-29 stsp if (j == 0)
590 574ed2c3 2017-11-29 stsp continue;
591 574ed2c3 2017-11-29 stsp y = -b[j];
592 574ed2c3 2017-11-29 stsp oldl = 0;
593 574ed2c3 2017-11-29 stsp oldc = c[0];
594 574ed2c3 2017-11-29 stsp numtries = 0;
595 574ed2c3 2017-11-29 stsp do {
596 ed9e98a8 2017-11-29 stsp if (y <= ds->clist[oldc].y)
597 574ed2c3 2017-11-29 stsp continue;
598 ed9e98a8 2017-11-29 stsp l = search(ds, c, k, y);
599 574ed2c3 2017-11-29 stsp if (l != oldl + 1)
600 574ed2c3 2017-11-29 stsp oldc = c[l - 1];
601 574ed2c3 2017-11-29 stsp if (l <= k) {
602 ed9e98a8 2017-11-29 stsp if (ds->clist[c[l]].y <= y)
603 574ed2c3 2017-11-29 stsp continue;
604 574ed2c3 2017-11-29 stsp tc = c[l];
605 322260e1 2018-01-26 mpi c[l] = newcand(ds, i, y, oldc, &error);
606 322260e1 2018-01-26 mpi if (error)
607 322260e1 2018-01-26 mpi return -1;
608 574ed2c3 2017-11-29 stsp oldc = tc;
609 574ed2c3 2017-11-29 stsp oldl = l;
610 574ed2c3 2017-11-29 stsp numtries++;
611 574ed2c3 2017-11-29 stsp } else {
612 322260e1 2018-01-26 mpi c[l] = newcand(ds, i, y, oldc, &error);
613 322260e1 2018-01-26 mpi if (error)
614 322260e1 2018-01-26 mpi return -1;
615 574ed2c3 2017-11-29 stsp k++;
616 574ed2c3 2017-11-29 stsp break;
617 574ed2c3 2017-11-29 stsp }
618 574ed2c3 2017-11-29 stsp } while ((y = b[++j]) > 0 && numtries < bound);
619 574ed2c3 2017-11-29 stsp }
620 574ed2c3 2017-11-29 stsp return (k);
621 574ed2c3 2017-11-29 stsp }
622 574ed2c3 2017-11-29 stsp
623 574ed2c3 2017-11-29 stsp static int
624 322260e1 2018-01-26 mpi newcand(struct got_diff_state *ds, int x, int y, int pred, int *errorp)
625 574ed2c3 2017-11-29 stsp {
626 574ed2c3 2017-11-29 stsp struct cand *q;
627 574ed2c3 2017-11-29 stsp
628 ed9e98a8 2017-11-29 stsp if (ds->clen == ds->clistlen) {
629 ed9e98a8 2017-11-29 stsp ds->clistlen = ds->clistlen * 11 / 10;
630 df51fc4e 2018-04-02 stsp q = reallocarray(ds->clist, ds->clistlen, sizeof(*ds->clist));
631 df51fc4e 2018-04-02 stsp if (q == NULL) {
632 322260e1 2018-01-26 mpi *errorp = -1;
633 df51fc4e 2018-04-02 stsp free(ds->clist);
634 df51fc4e 2018-04-02 stsp ds->clist = NULL;
635 322260e1 2018-01-26 mpi return 0;
636 322260e1 2018-01-26 mpi }
637 df51fc4e 2018-04-02 stsp ds->clist = q;
638 574ed2c3 2017-11-29 stsp }
639 ed9e98a8 2017-11-29 stsp q = ds->clist + ds->clen;
640 574ed2c3 2017-11-29 stsp q->x = x;
641 574ed2c3 2017-11-29 stsp q->y = y;
642 574ed2c3 2017-11-29 stsp q->pred = pred;
643 322260e1 2018-01-26 mpi *errorp = 0;
644 ed9e98a8 2017-11-29 stsp return (ds->clen++);
645 574ed2c3 2017-11-29 stsp }
646 574ed2c3 2017-11-29 stsp
647 574ed2c3 2017-11-29 stsp static int
648 ed9e98a8 2017-11-29 stsp search(struct got_diff_state *ds, int *c, int k, int y)
649 574ed2c3 2017-11-29 stsp {
650 574ed2c3 2017-11-29 stsp int i, j, l, t;
651 574ed2c3 2017-11-29 stsp
652 ed9e98a8 2017-11-29 stsp if (ds->clist[c[k]].y < y) /* quick look for typical case */
653 574ed2c3 2017-11-29 stsp return (k + 1);
654 574ed2c3 2017-11-29 stsp i = 0;
655 574ed2c3 2017-11-29 stsp j = k + 1;
656 574ed2c3 2017-11-29 stsp for (;;) {
657 574ed2c3 2017-11-29 stsp l = (i + j) / 2;
658 574ed2c3 2017-11-29 stsp if (l <= i)
659 574ed2c3 2017-11-29 stsp break;
660 ed9e98a8 2017-11-29 stsp t = ds->clist[c[l]].y;
661 574ed2c3 2017-11-29 stsp if (t > y)
662 574ed2c3 2017-11-29 stsp j = l;
663 574ed2c3 2017-11-29 stsp else if (t < y)
664 574ed2c3 2017-11-29 stsp i = l;
665 574ed2c3 2017-11-29 stsp else
666 574ed2c3 2017-11-29 stsp return (l);
667 574ed2c3 2017-11-29 stsp }
668 574ed2c3 2017-11-29 stsp return (l + 1);
669 574ed2c3 2017-11-29 stsp }
670 574ed2c3 2017-11-29 stsp
671 574ed2c3 2017-11-29 stsp static void
672 ed9e98a8 2017-11-29 stsp unravel(struct got_diff_state *ds, int p)
673 574ed2c3 2017-11-29 stsp {
674 574ed2c3 2017-11-29 stsp struct cand *q;
675 574ed2c3 2017-11-29 stsp int i;
676 574ed2c3 2017-11-29 stsp
677 ed9e98a8 2017-11-29 stsp for (i = 0; i <= ds->len[0]; i++)
678 ed9e98a8 2017-11-29 stsp ds->J[i] = i <= ds->pref ? i :
679 ed9e98a8 2017-11-29 stsp i > ds->len[0] - ds->suff ? i + ds->len[1] - ds->len[0] : 0;
680 ed9e98a8 2017-11-29 stsp for (q = ds->clist + p; q->y != 0; q = ds->clist + q->pred)
681 ed9e98a8 2017-11-29 stsp ds->J[q->x + ds->pref] = q->y + ds->pref;
682 574ed2c3 2017-11-29 stsp }
683 574ed2c3 2017-11-29 stsp
684 574ed2c3 2017-11-29 stsp /*
685 574ed2c3 2017-11-29 stsp * Check does double duty:
686 574ed2c3 2017-11-29 stsp * 1. ferret out any fortuitous correspondences due
687 574ed2c3 2017-11-29 stsp * to confounding by hashing (which result in "jackpot")
688 574ed2c3 2017-11-29 stsp * 2. collect random access indexes to the two files
689 574ed2c3 2017-11-29 stsp */
690 574ed2c3 2017-11-29 stsp static void
691 ed9e98a8 2017-11-29 stsp check(struct got_diff_state *ds, FILE *f1, FILE *f2, int flags)
692 574ed2c3 2017-11-29 stsp {
693 574ed2c3 2017-11-29 stsp int i, j, jackpot, c, d;
694 574ed2c3 2017-11-29 stsp long ctold, ctnew;
695 574ed2c3 2017-11-29 stsp
696 574ed2c3 2017-11-29 stsp rewind(f1);
697 574ed2c3 2017-11-29 stsp rewind(f2);
698 574ed2c3 2017-11-29 stsp j = 1;
699 ed9e98a8 2017-11-29 stsp ds->ixold[0] = ds->ixnew[0] = 0;
700 574ed2c3 2017-11-29 stsp jackpot = 0;
701 574ed2c3 2017-11-29 stsp ctold = ctnew = 0;
702 ed9e98a8 2017-11-29 stsp for (i = 1; i <= ds->len[0]; i++) {
703 ed9e98a8 2017-11-29 stsp if (ds->J[i] == 0) {
704 ed9e98a8 2017-11-29 stsp ds->ixold[i] = ctold += skipline(f1);
705 574ed2c3 2017-11-29 stsp continue;
706 574ed2c3 2017-11-29 stsp }
707 ed9e98a8 2017-11-29 stsp while (j < ds->J[i]) {
708 ed9e98a8 2017-11-29 stsp ds->ixnew[j] = ctnew += skipline(f2);
709 574ed2c3 2017-11-29 stsp j++;
710 574ed2c3 2017-11-29 stsp }
711 574ed2c3 2017-11-29 stsp if (flags & (D_FOLDBLANKS|D_IGNOREBLANKS|D_IGNORECASE)) {
712 574ed2c3 2017-11-29 stsp for (;;) {
713 574ed2c3 2017-11-29 stsp c = getc(f1);
714 574ed2c3 2017-11-29 stsp d = getc(f2);
715 574ed2c3 2017-11-29 stsp /*
716 574ed2c3 2017-11-29 stsp * GNU diff ignores a missing newline
717 574ed2c3 2017-11-29 stsp * in one file for -b or -w.
718 574ed2c3 2017-11-29 stsp */
719 574ed2c3 2017-11-29 stsp if (flags & (D_FOLDBLANKS|D_IGNOREBLANKS)) {
720 574ed2c3 2017-11-29 stsp if (c == EOF && d == '\n') {
721 574ed2c3 2017-11-29 stsp ctnew++;
722 574ed2c3 2017-11-29 stsp break;
723 574ed2c3 2017-11-29 stsp } else if (c == '\n' && d == EOF) {
724 574ed2c3 2017-11-29 stsp ctold++;
725 574ed2c3 2017-11-29 stsp break;
726 574ed2c3 2017-11-29 stsp }
727 574ed2c3 2017-11-29 stsp }
728 574ed2c3 2017-11-29 stsp ctold++;
729 574ed2c3 2017-11-29 stsp ctnew++;
730 574ed2c3 2017-11-29 stsp if ((flags & D_FOLDBLANKS) && isspace(c) &&
731 574ed2c3 2017-11-29 stsp isspace(d)) {
732 574ed2c3 2017-11-29 stsp do {
733 574ed2c3 2017-11-29 stsp if (c == '\n')
734 574ed2c3 2017-11-29 stsp break;
735 574ed2c3 2017-11-29 stsp ctold++;
736 574ed2c3 2017-11-29 stsp } while (isspace(c = getc(f1)));
737 574ed2c3 2017-11-29 stsp do {
738 574ed2c3 2017-11-29 stsp if (d == '\n')
739 574ed2c3 2017-11-29 stsp break;
740 574ed2c3 2017-11-29 stsp ctnew++;
741 574ed2c3 2017-11-29 stsp } while (isspace(d = getc(f2)));
742 574ed2c3 2017-11-29 stsp } else if ((flags & D_IGNOREBLANKS)) {
743 574ed2c3 2017-11-29 stsp while (isspace(c) && c != '\n') {
744 574ed2c3 2017-11-29 stsp c = getc(f1);
745 574ed2c3 2017-11-29 stsp ctold++;
746 574ed2c3 2017-11-29 stsp }
747 574ed2c3 2017-11-29 stsp while (isspace(d) && d != '\n') {
748 574ed2c3 2017-11-29 stsp d = getc(f2);
749 574ed2c3 2017-11-29 stsp ctnew++;
750 574ed2c3 2017-11-29 stsp }
751 574ed2c3 2017-11-29 stsp }
752 ed9e98a8 2017-11-29 stsp if (ds->chrtran[c] != ds->chrtran[d]) {
753 574ed2c3 2017-11-29 stsp jackpot++;
754 ed9e98a8 2017-11-29 stsp ds->J[i] = 0;
755 574ed2c3 2017-11-29 stsp if (c != '\n' && c != EOF)
756 574ed2c3 2017-11-29 stsp ctold += skipline(f1);
757 574ed2c3 2017-11-29 stsp if (d != '\n' && c != EOF)
758 574ed2c3 2017-11-29 stsp ctnew += skipline(f2);
759 574ed2c3 2017-11-29 stsp break;
760 574ed2c3 2017-11-29 stsp }
761 574ed2c3 2017-11-29 stsp if (c == '\n' || c == EOF)
762 574ed2c3 2017-11-29 stsp break;
763 574ed2c3 2017-11-29 stsp }
764 574ed2c3 2017-11-29 stsp } else {
765 574ed2c3 2017-11-29 stsp for (;;) {
766 574ed2c3 2017-11-29 stsp ctold++;
767 574ed2c3 2017-11-29 stsp ctnew++;
768 574ed2c3 2017-11-29 stsp if ((c = getc(f1)) != (d = getc(f2))) {
769 574ed2c3 2017-11-29 stsp /* jackpot++; */
770 ed9e98a8 2017-11-29 stsp ds->J[i] = 0;
771 574ed2c3 2017-11-29 stsp if (c != '\n' && c != EOF)
772 574ed2c3 2017-11-29 stsp ctold += skipline(f1);
773 574ed2c3 2017-11-29 stsp if (d != '\n' && c != EOF)
774 574ed2c3 2017-11-29 stsp ctnew += skipline(f2);
775 574ed2c3 2017-11-29 stsp break;
776 574ed2c3 2017-11-29 stsp }
777 574ed2c3 2017-11-29 stsp if (c == '\n' || c == EOF)
778 574ed2c3 2017-11-29 stsp break;
779 574ed2c3 2017-11-29 stsp }
780 574ed2c3 2017-11-29 stsp }
781 ed9e98a8 2017-11-29 stsp ds->ixold[i] = ctold;
782 ed9e98a8 2017-11-29 stsp ds->ixnew[j] = ctnew;
783 574ed2c3 2017-11-29 stsp j++;
784 574ed2c3 2017-11-29 stsp }
785 ed9e98a8 2017-11-29 stsp for (; j <= ds->len[1]; j++)
786 ed9e98a8 2017-11-29 stsp ds->ixnew[j] = ctnew += skipline(f2);
787 574ed2c3 2017-11-29 stsp /*
788 574ed2c3 2017-11-29 stsp * if (jackpot)
789 574ed2c3 2017-11-29 stsp * fprintf(stderr, "jackpot\n");
790 574ed2c3 2017-11-29 stsp */
791 574ed2c3 2017-11-29 stsp }
792 574ed2c3 2017-11-29 stsp
793 574ed2c3 2017-11-29 stsp /* shellsort CACM #201 */
794 574ed2c3 2017-11-29 stsp static void
795 574ed2c3 2017-11-29 stsp sort(struct line *a, int n)
796 574ed2c3 2017-11-29 stsp {
797 574ed2c3 2017-11-29 stsp struct line *ai, *aim, w;
798 574ed2c3 2017-11-29 stsp int j, m = 0, k;
799 574ed2c3 2017-11-29 stsp
800 574ed2c3 2017-11-29 stsp if (n == 0)
801 574ed2c3 2017-11-29 stsp return;
802 574ed2c3 2017-11-29 stsp for (j = 1; j <= n; j *= 2)
803 574ed2c3 2017-11-29 stsp m = 2 * j - 1;
804 574ed2c3 2017-11-29 stsp for (m /= 2; m != 0; m /= 2) {
805 574ed2c3 2017-11-29 stsp k = n - m;
806 574ed2c3 2017-11-29 stsp for (j = 1; j <= k; j++) {
807 574ed2c3 2017-11-29 stsp for (ai = &a[j]; ai > a; ai -= m) {
808 574ed2c3 2017-11-29 stsp aim = &ai[m];
809 574ed2c3 2017-11-29 stsp if (aim < ai)
810 574ed2c3 2017-11-29 stsp break; /* wraparound */
811 574ed2c3 2017-11-29 stsp if (aim->value > ai[0].value ||
812 574ed2c3 2017-11-29 stsp (aim->value == ai[0].value &&
813 574ed2c3 2017-11-29 stsp aim->serial > ai[0].serial))
814 574ed2c3 2017-11-29 stsp break;
815 574ed2c3 2017-11-29 stsp w.value = ai[0].value;
816 574ed2c3 2017-11-29 stsp ai[0].value = aim->value;
817 574ed2c3 2017-11-29 stsp aim->value = w.value;
818 574ed2c3 2017-11-29 stsp w.serial = ai[0].serial;
819 574ed2c3 2017-11-29 stsp ai[0].serial = aim->serial;
820 574ed2c3 2017-11-29 stsp aim->serial = w.serial;
821 574ed2c3 2017-11-29 stsp }
822 574ed2c3 2017-11-29 stsp }
823 574ed2c3 2017-11-29 stsp }
824 574ed2c3 2017-11-29 stsp }
825 574ed2c3 2017-11-29 stsp
826 322260e1 2018-01-26 mpi static int
827 574ed2c3 2017-11-29 stsp unsort(struct line *f, int l, int *b)
828 574ed2c3 2017-11-29 stsp {
829 574ed2c3 2017-11-29 stsp int *a, i;
830 574ed2c3 2017-11-29 stsp
831 322260e1 2018-01-26 mpi a = calloc(l + 1, sizeof(*a));
832 322260e1 2018-01-26 mpi if (a == NULL)
833 322260e1 2018-01-26 mpi return (-1);
834 574ed2c3 2017-11-29 stsp for (i = 1; i <= l; i++)
835 574ed2c3 2017-11-29 stsp a[f[i].serial] = f[i].value;
836 574ed2c3 2017-11-29 stsp for (i = 1; i <= l; i++)
837 574ed2c3 2017-11-29 stsp b[i] = a[i];
838 574ed2c3 2017-11-29 stsp free(a);
839 322260e1 2018-01-26 mpi
840 322260e1 2018-01-26 mpi return (0);
841 574ed2c3 2017-11-29 stsp }
842 574ed2c3 2017-11-29 stsp
843 574ed2c3 2017-11-29 stsp static int
844 574ed2c3 2017-11-29 stsp skipline(FILE *f)
845 574ed2c3 2017-11-29 stsp {
846 574ed2c3 2017-11-29 stsp int i, c;
847 574ed2c3 2017-11-29 stsp
848 574ed2c3 2017-11-29 stsp for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
849 574ed2c3 2017-11-29 stsp continue;
850 574ed2c3 2017-11-29 stsp return (i);
851 574ed2c3 2017-11-29 stsp }
852 574ed2c3 2017-11-29 stsp
853 322260e1 2018-01-26 mpi static int
854 404c43c4 2018-06-21 stsp output(FILE *outfile, struct got_diff_changes *changes,
855 404c43c4 2018-06-21 stsp struct got_diff_state *ds, struct got_diff_args *args,
856 f9d67749 2017-11-30 stsp const char *file1, FILE *f1, const char *file2, FILE *f2, int flags)
857 574ed2c3 2017-11-29 stsp {
858 574ed2c3 2017-11-29 stsp int m, i0, i1, j0, j1;
859 322260e1 2018-01-26 mpi int error = 0;
860 574ed2c3 2017-11-29 stsp
861 574ed2c3 2017-11-29 stsp rewind(f1);
862 574ed2c3 2017-11-29 stsp rewind(f2);
863 ed9e98a8 2017-11-29 stsp m = ds->len[0];
864 ed9e98a8 2017-11-29 stsp ds->J[0] = 0;
865 ed9e98a8 2017-11-29 stsp ds->J[m + 1] = ds->len[1] + 1;
866 0165cdf0 2018-04-02 stsp for (i0 = 1; i0 <= m; i0 = i1 + 1) {
867 0165cdf0 2018-04-02 stsp while (i0 <= m && ds->J[i0] == ds->J[i0 - 1] + 1)
868 0165cdf0 2018-04-02 stsp i0++;
869 0165cdf0 2018-04-02 stsp j0 = ds->J[i0 - 1] + 1;
870 0165cdf0 2018-04-02 stsp i1 = i0 - 1;
871 0165cdf0 2018-04-02 stsp while (i1 < m && ds->J[i1 + 1] == 0)
872 0165cdf0 2018-04-02 stsp i1++;
873 0165cdf0 2018-04-02 stsp j1 = ds->J[i1 + 1] - 1;
874 0165cdf0 2018-04-02 stsp ds->J[i1] = j1;
875 404c43c4 2018-06-21 stsp error = change(outfile, changes, ds, args, file1, f1, file2, f2,
876 0165cdf0 2018-04-02 stsp i0, i1, j0, j1, &flags);
877 0165cdf0 2018-04-02 stsp if (error)
878 0165cdf0 2018-04-02 stsp return (error);
879 574ed2c3 2017-11-29 stsp }
880 322260e1 2018-01-26 mpi if (m == 0) {
881 404c43c4 2018-06-21 stsp error = change(outfile, changes, ds, args, file1, f1, file2, f2,
882 404c43c4 2018-06-21 stsp 1, 0, 1, ds->len[1], &flags);
883 322260e1 2018-01-26 mpi if (error)
884 322260e1 2018-01-26 mpi return (error);
885 322260e1 2018-01-26 mpi }
886 acb209ee 2019-02-08 stsp if (ds->anychange != 0 && args->diff_format == D_UNIFIED)
887 404c43c4 2018-06-21 stsp dump_unified_vec(outfile, changes, ds, args, f1, f2, flags);
888 322260e1 2018-01-26 mpi
889 322260e1 2018-01-26 mpi return (0);
890 574ed2c3 2017-11-29 stsp }
891 574ed2c3 2017-11-29 stsp
892 574ed2c3 2017-11-29 stsp static void
893 acb209ee 2019-02-08 stsp range(FILE *outfile, int a, int b, char *separator)
894 acb209ee 2019-02-08 stsp {
895 acb209ee 2019-02-08 stsp diff_output(outfile, "%d", a > b ? b : a);
896 acb209ee 2019-02-08 stsp if (a < b)
897 acb209ee 2019-02-08 stsp diff_output(outfile, "%s%d", separator, b);
898 acb209ee 2019-02-08 stsp }
899 acb209ee 2019-02-08 stsp
900 acb209ee 2019-02-08 stsp static void
901 cb74ff21 2017-11-30 stsp uni_range(FILE *outfile, int a, int b)
902 574ed2c3 2017-11-29 stsp {
903 574ed2c3 2017-11-29 stsp if (a < b)
904 cb74ff21 2017-11-30 stsp diff_output(outfile, "%d,%d", a, b - a + 1);
905 574ed2c3 2017-11-29 stsp else if (a == b)
906 cb74ff21 2017-11-30 stsp diff_output(outfile, "%d", b);
907 574ed2c3 2017-11-29 stsp else
908 cb74ff21 2017-11-30 stsp diff_output(outfile, "%d,0", b);
909 574ed2c3 2017-11-29 stsp }
910 574ed2c3 2017-11-29 stsp
911 574ed2c3 2017-11-29 stsp /*
912 574ed2c3 2017-11-29 stsp * Indicate that there is a difference between lines a and b of the from file
913 574ed2c3 2017-11-29 stsp * to get to lines c to d of the to file. If a is greater then b then there
914 574ed2c3 2017-11-29 stsp * are no lines in the from file involved and this means that there were
915 574ed2c3 2017-11-29 stsp * lines appended (beginning at b). If c is greater than d then there are
916 574ed2c3 2017-11-29 stsp * lines missing from the to file.
917 574ed2c3 2017-11-29 stsp */
918 322260e1 2018-01-26 mpi static int
919 404c43c4 2018-06-21 stsp change(FILE *outfile, struct got_diff_changes *changes,
920 404c43c4 2018-06-21 stsp struct got_diff_state *ds, struct got_diff_args *args,
921 f9d67749 2017-11-30 stsp const char *file1, FILE *f1, const char *file2, FILE *f2,
922 ed9e98a8 2017-11-29 stsp int a, int b, int c, int d, int *pflags)
923 574ed2c3 2017-11-29 stsp {
924 574ed2c3 2017-11-29 stsp int i;
925 574ed2c3 2017-11-29 stsp
926 c48ceb4c 2018-04-02 stsp if (a > b && c > d)
927 322260e1 2018-01-26 mpi return (0);
928 a2a848a2 2018-04-02 stsp
929 574ed2c3 2017-11-29 stsp if (*pflags & D_HEADER) {
930 cb74ff21 2017-11-30 stsp diff_output(outfile, "%s %s %s\n", args->diffargs, file1, file2);
931 574ed2c3 2017-11-29 stsp *pflags &= ~D_HEADER;
932 574ed2c3 2017-11-29 stsp }
933 84e11851 2018-04-02 stsp if (args->diff_format == D_UNIFIED) {
934 574ed2c3 2017-11-29 stsp /*
935 574ed2c3 2017-11-29 stsp * Allocate change records as needed.
936 574ed2c3 2017-11-29 stsp */
937 ed9e98a8 2017-11-29 stsp if (ds->context_vec_ptr == ds->context_vec_end - 1) {
938 df51fc4e 2018-04-02 stsp struct context_vec *cvp;
939 76735683 2018-04-02 stsp ptrdiff_t offset;
940 76735683 2018-04-02 stsp offset = ds->context_vec_ptr - ds->context_vec_start;
941 76735683 2018-04-02 stsp ds->max_context <<= 1;
942 df51fc4e 2018-04-02 stsp cvp = reallocarray(ds->context_vec_start,
943 df51fc4e 2018-04-02 stsp ds->max_context, sizeof(*ds->context_vec_start));
944 df51fc4e 2018-04-02 stsp if (cvp == NULL) {
945 df51fc4e 2018-04-02 stsp free(ds->context_vec_start);
946 322260e1 2018-01-26 mpi return (-1);
947 df51fc4e 2018-04-02 stsp }
948 df51fc4e 2018-04-02 stsp ds->context_vec_start = cvp;
949 76735683 2018-04-02 stsp ds->context_vec_end = ds->context_vec_start +
950 76735683 2018-04-02 stsp ds->max_context;
951 ed9e98a8 2017-11-29 stsp ds->context_vec_ptr = ds->context_vec_start + offset;
952 574ed2c3 2017-11-29 stsp }
953 ed9e98a8 2017-11-29 stsp if (ds->anychange == 0) {
954 574ed2c3 2017-11-29 stsp /*
955 574ed2c3 2017-11-29 stsp * Print the context/unidiff header first time through.
956 574ed2c3 2017-11-29 stsp */
957 cb74ff21 2017-11-30 stsp print_header(outfile, ds, args, file1, file2);
958 ed9e98a8 2017-11-29 stsp ds->anychange = 1;
959 8ba9a219 2017-11-29 stsp } else if (a > ds->context_vec_ptr->b + (2 * args->diff_context) + 1 &&
960 8ba9a219 2017-11-29 stsp c > ds->context_vec_ptr->d + (2 * args->diff_context) + 1) {
961 574ed2c3 2017-11-29 stsp /*
962 574ed2c3 2017-11-29 stsp * If this change is more than 'diff_context' lines from the
963 574ed2c3 2017-11-29 stsp * previous change, dump the record and reset it.
964 574ed2c3 2017-11-29 stsp */
965 404c43c4 2018-06-21 stsp dump_unified_vec(outfile, changes, ds, args, f1, f2,
966 404c43c4 2018-06-21 stsp *pflags);
967 574ed2c3 2017-11-29 stsp }
968 ed9e98a8 2017-11-29 stsp ds->context_vec_ptr++;
969 ed9e98a8 2017-11-29 stsp ds->context_vec_ptr->a = a;
970 ed9e98a8 2017-11-29 stsp ds->context_vec_ptr->b = b;
971 ed9e98a8 2017-11-29 stsp ds->context_vec_ptr->c = c;
972 ed9e98a8 2017-11-29 stsp ds->context_vec_ptr->d = d;
973 322260e1 2018-01-26 mpi return (0);
974 574ed2c3 2017-11-29 stsp }
975 ed9e98a8 2017-11-29 stsp if (ds->anychange == 0)
976 ed9e98a8 2017-11-29 stsp ds->anychange = 1;
977 1afa8e43 2018-04-02 stsp if (args->diff_format == D_BRIEF)
978 322260e1 2018-01-26 mpi return (0);
979 acb209ee 2019-02-08 stsp if (args->diff_format == D_NORMAL) {
980 acb209ee 2019-02-08 stsp range(outfile, a, b, ",");
981 acb209ee 2019-02-08 stsp diff_output(outfile, "%c", a > b ? 'a' : c > d ? 'd' : 'c');
982 acb209ee 2019-02-08 stsp range(outfile, c, d, ",");
983 acb209ee 2019-02-08 stsp diff_output(outfile, "\n");
984 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixold, a, b, f1, '<', *pflags);
985 acb209ee 2019-02-08 stsp if (a <= b && c <= d)
986 acb209ee 2019-02-08 stsp diff_output(outfile, "---\n");
987 acb209ee 2019-02-08 stsp }
988 acb209ee 2019-02-08 stsp i = fetch(outfile, ds, args, ds->ixnew, c, d, f2,
989 14ed98fd 2019-08-18 stsp args->diff_format == D_NORMAL ? '>' : '\0', *pflags);
990 322260e1 2018-01-26 mpi return (0);
991 574ed2c3 2017-11-29 stsp }
992 574ed2c3 2017-11-29 stsp
993 574ed2c3 2017-11-29 stsp static int
994 cb74ff21 2017-11-30 stsp fetch(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
995 14ed98fd 2019-08-18 stsp long *f, int a, int b, FILE *lb, int ch, int flags)
996 574ed2c3 2017-11-29 stsp {
997 574ed2c3 2017-11-29 stsp int i, j, c, lastc, col, nc;
998 574ed2c3 2017-11-29 stsp
999 574ed2c3 2017-11-29 stsp if (a > b)
1000 574ed2c3 2017-11-29 stsp return (0);
1001 574ed2c3 2017-11-29 stsp for (i = a; i <= b; i++) {
1002 574ed2c3 2017-11-29 stsp fseek(lb, f[i - 1], SEEK_SET);
1003 574ed2c3 2017-11-29 stsp nc = f[i] - f[i - 1];
1004 c48ceb4c 2018-04-02 stsp if (ch != '\0') {
1005 cb74ff21 2017-11-30 stsp diff_output(outfile, "%c", ch);
1006 acb209ee 2019-02-08 stsp if (args->Tflag && (args->diff_format == D_UNIFIED ||
1007 acb209ee 2019-02-08 stsp args->diff_format == D_NORMAL))
1008 cb74ff21 2017-11-30 stsp diff_output(outfile, "\t");
1009 8ba9a219 2017-11-29 stsp else if (args->diff_format != D_UNIFIED)
1010 cb74ff21 2017-11-30 stsp diff_output(outfile, " ");
1011 574ed2c3 2017-11-29 stsp }
1012 574ed2c3 2017-11-29 stsp col = 0;
1013 574ed2c3 2017-11-29 stsp for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
1014 574ed2c3 2017-11-29 stsp if ((c = getc(lb)) == EOF) {
1015 1afa8e43 2018-04-02 stsp diff_output(outfile, "\n\\ No newline at end of "
1016 1afa8e43 2018-04-02 stsp "file\n");
1017 574ed2c3 2017-11-29 stsp return (0);
1018 574ed2c3 2017-11-29 stsp }
1019 574ed2c3 2017-11-29 stsp if (c == '\t' && (flags & D_EXPANDTABS)) {
1020 574ed2c3 2017-11-29 stsp do {
1021 cb74ff21 2017-11-30 stsp diff_output(outfile, " ");
1022 574ed2c3 2017-11-29 stsp } while (++col & 7);
1023 574ed2c3 2017-11-29 stsp } else {
1024 cb74ff21 2017-11-30 stsp diff_output(outfile, "%c", c);
1025 574ed2c3 2017-11-29 stsp col++;
1026 574ed2c3 2017-11-29 stsp }
1027 574ed2c3 2017-11-29 stsp }
1028 574ed2c3 2017-11-29 stsp }
1029 574ed2c3 2017-11-29 stsp return (0);
1030 574ed2c3 2017-11-29 stsp }
1031 574ed2c3 2017-11-29 stsp
1032 574ed2c3 2017-11-29 stsp /*
1033 574ed2c3 2017-11-29 stsp * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1034 574ed2c3 2017-11-29 stsp */
1035 574ed2c3 2017-11-29 stsp static int
1036 ed9e98a8 2017-11-29 stsp readhash(struct got_diff_state *ds, FILE *f, int flags)
1037 574ed2c3 2017-11-29 stsp {
1038 574ed2c3 2017-11-29 stsp int i, t, space;
1039 574ed2c3 2017-11-29 stsp int sum;
1040 574ed2c3 2017-11-29 stsp
1041 574ed2c3 2017-11-29 stsp sum = 1;
1042 574ed2c3 2017-11-29 stsp space = 0;
1043 574ed2c3 2017-11-29 stsp if ((flags & (D_FOLDBLANKS|D_IGNOREBLANKS)) == 0) {
1044 574ed2c3 2017-11-29 stsp if (flags & D_IGNORECASE)
1045 574ed2c3 2017-11-29 stsp for (i = 0; (t = getc(f)) != '\n'; i++) {
1046 574ed2c3 2017-11-29 stsp if (t == EOF) {
1047 574ed2c3 2017-11-29 stsp if (i == 0)
1048 574ed2c3 2017-11-29 stsp return (0);
1049 574ed2c3 2017-11-29 stsp break;
1050 574ed2c3 2017-11-29 stsp }
1051 ed9e98a8 2017-11-29 stsp sum = sum * 127 + ds->chrtran[t];
1052 574ed2c3 2017-11-29 stsp }
1053 574ed2c3 2017-11-29 stsp else
1054 574ed2c3 2017-11-29 stsp for (i = 0; (t = getc(f)) != '\n'; i++) {
1055 574ed2c3 2017-11-29 stsp if (t == EOF) {
1056 574ed2c3 2017-11-29 stsp if (i == 0)
1057 574ed2c3 2017-11-29 stsp return (0);
1058 574ed2c3 2017-11-29 stsp break;
1059 574ed2c3 2017-11-29 stsp }
1060 574ed2c3 2017-11-29 stsp sum = sum * 127 + t;
1061 574ed2c3 2017-11-29 stsp }
1062 574ed2c3 2017-11-29 stsp } else {
1063 574ed2c3 2017-11-29 stsp for (i = 0;;) {
1064 574ed2c3 2017-11-29 stsp switch (t = getc(f)) {
1065 574ed2c3 2017-11-29 stsp case '\t':
1066 574ed2c3 2017-11-29 stsp case '\r':
1067 574ed2c3 2017-11-29 stsp case '\v':
1068 574ed2c3 2017-11-29 stsp case '\f':
1069 574ed2c3 2017-11-29 stsp case ' ':
1070 574ed2c3 2017-11-29 stsp space++;
1071 574ed2c3 2017-11-29 stsp continue;
1072 574ed2c3 2017-11-29 stsp default:
1073 574ed2c3 2017-11-29 stsp if (space && (flags & D_IGNOREBLANKS) == 0) {
1074 574ed2c3 2017-11-29 stsp i++;
1075 574ed2c3 2017-11-29 stsp space = 0;
1076 574ed2c3 2017-11-29 stsp }
1077 ed9e98a8 2017-11-29 stsp sum = sum * 127 + ds->chrtran[t];
1078 574ed2c3 2017-11-29 stsp i++;
1079 574ed2c3 2017-11-29 stsp continue;
1080 574ed2c3 2017-11-29 stsp case EOF:
1081 574ed2c3 2017-11-29 stsp if (i == 0)
1082 574ed2c3 2017-11-29 stsp return (0);
1083 574ed2c3 2017-11-29 stsp /* FALLTHROUGH */
1084 574ed2c3 2017-11-29 stsp case '\n':
1085 574ed2c3 2017-11-29 stsp break;
1086 574ed2c3 2017-11-29 stsp }
1087 574ed2c3 2017-11-29 stsp break;
1088 574ed2c3 2017-11-29 stsp }
1089 574ed2c3 2017-11-29 stsp }
1090 574ed2c3 2017-11-29 stsp /*
1091 574ed2c3 2017-11-29 stsp * There is a remote possibility that we end up with a zero sum.
1092 574ed2c3 2017-11-29 stsp * Zero is used as an EOF marker, so return 1 instead.
1093 574ed2c3 2017-11-29 stsp */
1094 574ed2c3 2017-11-29 stsp return (sum == 0 ? 1 : sum);
1095 574ed2c3 2017-11-29 stsp }
1096 574ed2c3 2017-11-29 stsp
1097 574ed2c3 2017-11-29 stsp static int
1098 574ed2c3 2017-11-29 stsp asciifile(FILE *f)
1099 574ed2c3 2017-11-29 stsp {
1100 574ed2c3 2017-11-29 stsp unsigned char buf[BUFSIZ];
1101 574ed2c3 2017-11-29 stsp size_t cnt;
1102 574ed2c3 2017-11-29 stsp
1103 574ed2c3 2017-11-29 stsp if (f == NULL)
1104 574ed2c3 2017-11-29 stsp return (1);
1105 574ed2c3 2017-11-29 stsp
1106 574ed2c3 2017-11-29 stsp rewind(f);
1107 574ed2c3 2017-11-29 stsp cnt = fread(buf, 1, sizeof(buf), f);
1108 574ed2c3 2017-11-29 stsp return (memchr(buf, '\0', cnt) == NULL);
1109 574ed2c3 2017-11-29 stsp }
1110 574ed2c3 2017-11-29 stsp
1111 574ed2c3 2017-11-29 stsp #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
1112 574ed2c3 2017-11-29 stsp
1113 574ed2c3 2017-11-29 stsp static char *
1114 ed9e98a8 2017-11-29 stsp match_function(struct got_diff_state *ds, const long *f, int pos, FILE *fp)
1115 574ed2c3 2017-11-29 stsp {
1116 574ed2c3 2017-11-29 stsp unsigned char buf[FUNCTION_CONTEXT_SIZE];
1117 574ed2c3 2017-11-29 stsp size_t nc;
1118 ed9e98a8 2017-11-29 stsp int last = ds->lastline;
1119 574ed2c3 2017-11-29 stsp char *state = NULL;
1120 574ed2c3 2017-11-29 stsp
1121 ed9e98a8 2017-11-29 stsp ds->lastline = pos;
1122 574ed2c3 2017-11-29 stsp while (pos > last) {
1123 574ed2c3 2017-11-29 stsp fseek(fp, f[pos - 1], SEEK_SET);
1124 574ed2c3 2017-11-29 stsp nc = f[pos] - f[pos - 1];
1125 574ed2c3 2017-11-29 stsp if (nc >= sizeof(buf))
1126 574ed2c3 2017-11-29 stsp nc = sizeof(buf) - 1;
1127 574ed2c3 2017-11-29 stsp nc = fread(buf, 1, nc, fp);
1128 574ed2c3 2017-11-29 stsp if (nc > 0) {
1129 574ed2c3 2017-11-29 stsp buf[nc] = '\0';
1130 574ed2c3 2017-11-29 stsp buf[strcspn(buf, "\n")] = '\0';
1131 574ed2c3 2017-11-29 stsp if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1132 574ed2c3 2017-11-29 stsp if (begins_with(buf, "private:")) {
1133 574ed2c3 2017-11-29 stsp if (!state)
1134 574ed2c3 2017-11-29 stsp state = " (private)";
1135 574ed2c3 2017-11-29 stsp } else if (begins_with(buf, "protected:")) {
1136 574ed2c3 2017-11-29 stsp if (!state)
1137 574ed2c3 2017-11-29 stsp state = " (protected)";
1138 574ed2c3 2017-11-29 stsp } else if (begins_with(buf, "public:")) {
1139 574ed2c3 2017-11-29 stsp if (!state)
1140 574ed2c3 2017-11-29 stsp state = " (public)";
1141 574ed2c3 2017-11-29 stsp } else {
1142 ed9e98a8 2017-11-29 stsp strlcpy(ds->lastbuf, buf, sizeof ds->lastbuf);
1143 574ed2c3 2017-11-29 stsp if (state)
1144 ed9e98a8 2017-11-29 stsp strlcat(ds->lastbuf, state,
1145 ed9e98a8 2017-11-29 stsp sizeof ds->lastbuf);
1146 ed9e98a8 2017-11-29 stsp ds->lastmatchline = pos;
1147 ed9e98a8 2017-11-29 stsp return ds->lastbuf;
1148 574ed2c3 2017-11-29 stsp }
1149 574ed2c3 2017-11-29 stsp }
1150 574ed2c3 2017-11-29 stsp }
1151 574ed2c3 2017-11-29 stsp pos--;
1152 574ed2c3 2017-11-29 stsp }
1153 ed9e98a8 2017-11-29 stsp return ds->lastmatchline > 0 ? ds->lastbuf : NULL;
1154 574ed2c3 2017-11-29 stsp }
1155 574ed2c3 2017-11-29 stsp
1156 574ed2c3 2017-11-29 stsp /* dump accumulated "unified" diff changes */
1157 574ed2c3 2017-11-29 stsp static void
1158 404c43c4 2018-06-21 stsp dump_unified_vec(FILE *outfile, struct got_diff_changes *changes,
1159 404c43c4 2018-06-21 stsp struct got_diff_state *ds, struct got_diff_args *args,
1160 8ba9a219 2017-11-29 stsp FILE *f1, FILE *f2, int flags)
1161 574ed2c3 2017-11-29 stsp {
1162 ed9e98a8 2017-11-29 stsp struct context_vec *cvp = ds->context_vec_start;
1163 574ed2c3 2017-11-29 stsp int lowa, upb, lowc, upd;
1164 574ed2c3 2017-11-29 stsp int a, b, c, d;
1165 574ed2c3 2017-11-29 stsp char ch, *f;
1166 574ed2c3 2017-11-29 stsp
1167 ed9e98a8 2017-11-29 stsp if (ds->context_vec_start > ds->context_vec_ptr)
1168 574ed2c3 2017-11-29 stsp return;
1169 574ed2c3 2017-11-29 stsp
1170 574ed2c3 2017-11-29 stsp b = d = 0; /* gcc */
1171 8ba9a219 2017-11-29 stsp lowa = MAXIMUM(1, cvp->a - args->diff_context);
1172 8ba9a219 2017-11-29 stsp upb = MINIMUM(ds->len[0], ds->context_vec_ptr->b + args->diff_context);
1173 8ba9a219 2017-11-29 stsp lowc = MAXIMUM(1, cvp->c - args->diff_context);
1174 8ba9a219 2017-11-29 stsp upd = MINIMUM(ds->len[1], ds->context_vec_ptr->d + args->diff_context);
1175 574ed2c3 2017-11-29 stsp
1176 cb74ff21 2017-11-30 stsp diff_output(outfile, "@@ -");
1177 cb74ff21 2017-11-30 stsp uni_range(outfile, lowa, upb);
1178 cb74ff21 2017-11-30 stsp diff_output(outfile, " +");
1179 cb74ff21 2017-11-30 stsp uni_range(outfile, lowc, upd);
1180 cb74ff21 2017-11-30 stsp diff_output(outfile, " @@");
1181 574ed2c3 2017-11-29 stsp if ((flags & D_PROTOTYPE)) {
1182 ed9e98a8 2017-11-29 stsp f = match_function(ds, ds->ixold, lowa-1, f1);
1183 574ed2c3 2017-11-29 stsp if (f != NULL)
1184 cb74ff21 2017-11-30 stsp diff_output(outfile, " %s", f);
1185 574ed2c3 2017-11-29 stsp }
1186 cb74ff21 2017-11-30 stsp diff_output(outfile, "\n");
1187 574ed2c3 2017-11-29 stsp
1188 574ed2c3 2017-11-29 stsp /*
1189 574ed2c3 2017-11-29 stsp * Output changes in "unified" diff format--the old and new lines
1190 574ed2c3 2017-11-29 stsp * are printed together.
1191 574ed2c3 2017-11-29 stsp */
1192 ed9e98a8 2017-11-29 stsp for (; cvp <= ds->context_vec_ptr; cvp++) {
1193 404c43c4 2018-06-21 stsp if (changes) {
1194 404c43c4 2018-06-21 stsp struct got_diff_change *change;
1195 404c43c4 2018-06-21 stsp change = calloc(1, sizeof(*change));
1196 404c43c4 2018-06-21 stsp if (change) {
1197 404c43c4 2018-06-21 stsp memcpy(&change->cv, cvp, sizeof(change->cv));
1198 404c43c4 2018-06-21 stsp SIMPLEQ_INSERT_TAIL(&changes->entries, change,
1199 404c43c4 2018-06-21 stsp entry);
1200 404c43c4 2018-06-21 stsp changes->nchanges++;
1201 404c43c4 2018-06-21 stsp }
1202 404c43c4 2018-06-21 stsp }
1203 404c43c4 2018-06-21 stsp
1204 574ed2c3 2017-11-29 stsp a = cvp->a;
1205 574ed2c3 2017-11-29 stsp b = cvp->b;
1206 574ed2c3 2017-11-29 stsp c = cvp->c;
1207 574ed2c3 2017-11-29 stsp d = cvp->d;
1208 574ed2c3 2017-11-29 stsp
1209 574ed2c3 2017-11-29 stsp /*
1210 574ed2c3 2017-11-29 stsp * c: both new and old changes
1211 574ed2c3 2017-11-29 stsp * d: only changes in the old file
1212 574ed2c3 2017-11-29 stsp * a: only changes in the new file
1213 574ed2c3 2017-11-29 stsp */
1214 574ed2c3 2017-11-29 stsp if (a <= b && c <= d)
1215 574ed2c3 2017-11-29 stsp ch = 'c';
1216 574ed2c3 2017-11-29 stsp else
1217 574ed2c3 2017-11-29 stsp ch = (a <= b) ? 'd' : 'a';
1218 574ed2c3 2017-11-29 stsp
1219 574ed2c3 2017-11-29 stsp switch (ch) {
1220 574ed2c3 2017-11-29 stsp case 'c':
1221 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixold, lowa, a - 1, f1, ' ', flags);
1222 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixold, a, b, f1, '-', flags);
1223 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixnew, c, d, f2, '+', flags);
1224 574ed2c3 2017-11-29 stsp break;
1225 574ed2c3 2017-11-29 stsp case 'd':
1226 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixold, lowa, a - 1, f1, ' ', flags);
1227 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixold, a, b, f1, '-', flags);
1228 574ed2c3 2017-11-29 stsp break;
1229 574ed2c3 2017-11-29 stsp case 'a':
1230 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixnew, lowc, c - 1, f2, ' ', flags);
1231 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixnew, c, d, f2, '+', flags);
1232 574ed2c3 2017-11-29 stsp break;
1233 574ed2c3 2017-11-29 stsp }
1234 574ed2c3 2017-11-29 stsp lowa = b + 1;
1235 574ed2c3 2017-11-29 stsp lowc = d + 1;
1236 574ed2c3 2017-11-29 stsp }
1237 14ed98fd 2019-08-18 stsp fetch(outfile, ds, args, ds->ixnew, d + 1, upd, f2, ' ', flags);
1238 574ed2c3 2017-11-29 stsp
1239 ed9e98a8 2017-11-29 stsp ds->context_vec_ptr = ds->context_vec_start - 1;
1240 574ed2c3 2017-11-29 stsp }
1241 574ed2c3 2017-11-29 stsp
1242 dc424a06 2019-08-07 stsp void
1243 dc424a06 2019-08-07 stsp got_diff_dump_change(FILE *outfile, struct got_diff_change *change,
1244 dc424a06 2019-08-07 stsp struct got_diff_state *ds, struct got_diff_args *args,
1245 dc424a06 2019-08-07 stsp FILE *f1, FILE *f2, int diff_flags)
1246 dc424a06 2019-08-07 stsp {
1247 dc424a06 2019-08-07 stsp ds->context_vec_ptr = &change->cv;
1248 dc424a06 2019-08-07 stsp ds->context_vec_start = &change->cv;
1249 dc424a06 2019-08-07 stsp ds->context_vec_end = &change->cv;
1250 dc424a06 2019-08-07 stsp
1251 dc424a06 2019-08-07 stsp /* XXX TODO needs error checking */
1252 dc424a06 2019-08-07 stsp dump_unified_vec(outfile, NULL, ds, args, f1, f2, diff_flags);
1253 dc424a06 2019-08-07 stsp }
1254 dc424a06 2019-08-07 stsp
1255 574ed2c3 2017-11-29 stsp static void
1256 cb74ff21 2017-11-30 stsp print_header(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
1257 8ba9a219 2017-11-29 stsp const char *file1, const char *file2)
1258 574ed2c3 2017-11-29 stsp {
1259 8ba9a219 2017-11-29 stsp if (args->label[0] != NULL)
1260 84e11851 2018-04-02 stsp diff_output(outfile, "--- %s\n", args->label[0]);
1261 574ed2c3 2017-11-29 stsp else
1262 84e11851 2018-04-02 stsp diff_output(outfile, "--- %s\t%s", file1,
1263 84e11851 2018-04-02 stsp ctime(&ds->stb1.st_mtime));
1264 8ba9a219 2017-11-29 stsp if (args->label[1] != NULL)
1265 84e11851 2018-04-02 stsp diff_output(outfile, "+++ %s\n", args->label[1]);
1266 574ed2c3 2017-11-29 stsp else
1267 84e11851 2018-04-02 stsp diff_output(outfile, "+++ %s\t%s", file2,
1268 84e11851 2018-04-02 stsp ctime(&ds->stb2.st_mtime));
1269 574ed2c3 2017-11-29 stsp }