Blame


1 3b0f3d61 2020-01-22 neels /* Generic infrastructure to implement various diff algorithms (implementation). */
2 3b0f3d61 2020-01-22 neels /*
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 3b0f3d61 2020-01-22 neels *
5 3b0f3d61 2020-01-22 neels * Permission to use, copy, modify, and distribute this software for any
6 3b0f3d61 2020-01-22 neels * purpose with or without fee is hereby granted, provided that the above
7 3b0f3d61 2020-01-22 neels * copyright notice and this permission notice appear in all copies.
8 3b0f3d61 2020-01-22 neels *
9 3b0f3d61 2020-01-22 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 3b0f3d61 2020-01-22 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 3b0f3d61 2020-01-22 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 3b0f3d61 2020-01-22 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 3b0f3d61 2020-01-22 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 3b0f3d61 2020-01-22 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 3b0f3d61 2020-01-22 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 3b0f3d61 2020-01-22 neels */
17 3b0f3d61 2020-01-22 neels
18 3b0f3d61 2020-01-22 neels
19 3b0f3d61 2020-01-22 neels #include <sys/queue.h>
20 732e8ee0 2020-09-20 stsp #include <ctype.h>
21 e10a628a 2020-09-16 stsp #include <errno.h>
22 3b0f3d61 2020-01-22 neels #include <stdint.h>
23 3b0f3d61 2020-01-22 neels #include <stdlib.h>
24 3b0f3d61 2020-01-22 neels #include <stdbool.h>
25 3b0f3d61 2020-01-22 neels #include <stdio.h>
26 3b0f3d61 2020-01-22 neels #include <string.h>
27 3b0f3d61 2020-01-22 neels #include <limits.h>
28 c6eecea3 2020-07-26 stsp #include <unistd.h>
29 3b0f3d61 2020-01-22 neels
30 3b0f3d61 2020-01-22 neels #include <assert.h>
31 3b0f3d61 2020-01-22 neels
32 1dfba055 2020-10-07 stsp #include <arraylist.h>
33 1dfba055 2020-10-07 stsp #include <diff_main.h>
34 3b0f3d61 2020-01-22 neels
35 85ab4559 2020-09-22 stsp #include "diff_internal.h"
36 2a1b94d0 2020-09-26 stsp #include "diff_debug.h"
37 c6eecea3 2020-07-26 stsp
38 b3fb4686 2020-09-20 neels static int
39 7a54ad3a 2020-09-20 stsp read_at(FILE *f, off_t at_pos, unsigned char *buf, size_t len)
40 c6eecea3 2020-07-26 stsp {
41 b3fb4686 2020-09-20 neels int r;
42 7a54ad3a 2020-09-20 stsp if (fseeko(f, at_pos, SEEK_SET) == -1)
43 b3fb4686 2020-09-20 neels return errno;
44 7a54ad3a 2020-09-20 stsp r = fread(buf, sizeof(char), len, f);
45 7a54ad3a 2020-09-20 stsp if ((r == 0 || r < len) && ferror(f))
46 674563ab 2022-10-11 tj return EIO;
47 b3fb4686 2020-09-20 neels if (r != len)
48 b3fb4686 2020-09-20 neels return EIO;
49 b3fb4686 2020-09-20 neels return 0;
50 b3fb4686 2020-09-20 neels }
51 b3fb4686 2020-09-20 neels
52 b3fb4686 2020-09-20 neels static int
53 b3fb4686 2020-09-20 neels buf_cmp(const unsigned char *left, size_t left_len,
54 732e8ee0 2020-09-20 stsp const unsigned char *right, size_t right_len,
55 732e8ee0 2020-09-20 stsp bool ignore_whitespace)
56 b3fb4686 2020-09-20 neels {
57 732e8ee0 2020-09-20 stsp int cmp;
58 732e8ee0 2020-09-20 stsp
59 732e8ee0 2020-09-20 stsp if (ignore_whitespace) {
60 732e8ee0 2020-09-20 stsp int il = 0, ir = 0;
61 732e8ee0 2020-09-20 stsp while (il < left_len && ir < right_len) {
62 732e8ee0 2020-09-20 stsp unsigned char cl = left[il];
63 732e8ee0 2020-09-20 stsp unsigned char cr = right[ir];
64 732e8ee0 2020-09-20 stsp
65 732e8ee0 2020-09-20 stsp if (isspace(cl) && il < left_len) {
66 732e8ee0 2020-09-20 stsp il++;
67 732e8ee0 2020-09-20 stsp continue;
68 732e8ee0 2020-09-20 stsp }
69 732e8ee0 2020-09-20 stsp if (isspace(cr) && ir < right_len) {
70 732e8ee0 2020-09-20 stsp ir++;
71 732e8ee0 2020-09-20 stsp continue;
72 732e8ee0 2020-09-20 stsp }
73 732e8ee0 2020-09-20 stsp
74 732e8ee0 2020-09-20 stsp if (cl > cr)
75 732e8ee0 2020-09-20 stsp return 1;
76 732e8ee0 2020-09-20 stsp if (cr > cl)
77 732e8ee0 2020-09-20 stsp return -1;
78 732e8ee0 2020-09-20 stsp il++;
79 732e8ee0 2020-09-20 stsp ir++;
80 732e8ee0 2020-09-20 stsp }
81 732e8ee0 2020-09-20 stsp while (il < left_len) {
82 732e8ee0 2020-09-20 stsp unsigned char cl = left[il++];
83 732e8ee0 2020-09-20 stsp if (!isspace(cl))
84 732e8ee0 2020-09-20 stsp return 1;
85 732e8ee0 2020-09-20 stsp }
86 732e8ee0 2020-09-20 stsp while (ir < right_len) {
87 732e8ee0 2020-09-20 stsp unsigned char cr = right[ir++];
88 732e8ee0 2020-09-20 stsp if (!isspace(cr))
89 732e8ee0 2020-09-20 stsp return -1;
90 732e8ee0 2020-09-20 stsp }
91 732e8ee0 2020-09-20 stsp
92 732e8ee0 2020-09-20 stsp return 0;
93 732e8ee0 2020-09-20 stsp }
94 732e8ee0 2020-09-20 stsp
95 732e8ee0 2020-09-20 stsp cmp = memcmp(left, right, MIN(left_len, right_len));
96 b3fb4686 2020-09-20 neels if (cmp)
97 b3fb4686 2020-09-20 neels return cmp;
98 b3fb4686 2020-09-20 neels if (left_len == right_len)
99 b3fb4686 2020-09-20 neels return 0;
100 b3fb4686 2020-09-20 neels return (left_len > right_len) ? 1 : -1;
101 b3fb4686 2020-09-20 neels }
102 b3fb4686 2020-09-20 neels
103 b3fb4686 2020-09-20 neels int
104 b3fb4686 2020-09-20 neels diff_atom_cmp(int *cmp,
105 b3fb4686 2020-09-20 neels const struct diff_atom *left,
106 b3fb4686 2020-09-20 neels const struct diff_atom *right)
107 b3fb4686 2020-09-20 neels {
108 c6eecea3 2020-07-26 stsp off_t remain_left, remain_right;
109 ad5b3f85 2020-10-12 neels int flags = (left->root->diff_flags | right->root->diff_flags);
110 00d5652b 2020-09-22 stsp bool ignore_whitespace = (flags & DIFF_FLAG_IGNORE_WHITESPACE);
111 c6eecea3 2020-07-26 stsp
112 74ad6a69 2020-10-22 neels if (!left->len && !right->len) {
113 74ad6a69 2020-10-22 neels *cmp = 0;
114 74ad6a69 2020-10-22 neels return 0;
115 74ad6a69 2020-10-22 neels }
116 732e8ee0 2020-09-20 stsp if (!ignore_whitespace) {
117 732e8ee0 2020-09-20 stsp if (!right->len) {
118 732e8ee0 2020-09-20 stsp *cmp = 1;
119 732e8ee0 2020-09-20 stsp return 0;
120 732e8ee0 2020-09-20 stsp }
121 732e8ee0 2020-09-20 stsp if (!left->len) {
122 732e8ee0 2020-09-20 stsp *cmp = -1;
123 732e8ee0 2020-09-20 stsp return 0;
124 732e8ee0 2020-09-20 stsp }
125 b3fb4686 2020-09-20 neels }
126 c6eecea3 2020-07-26 stsp
127 b3fb4686 2020-09-20 neels if (left->at != NULL && right->at != NULL) {
128 732e8ee0 2020-09-20 stsp *cmp = buf_cmp(left->at, left->len, right->at, right->len,
129 732e8ee0 2020-09-20 stsp ignore_whitespace);
130 b3fb4686 2020-09-20 neels return 0;
131 b3fb4686 2020-09-20 neels }
132 c6eecea3 2020-07-26 stsp
133 c6eecea3 2020-07-26 stsp remain_left = left->len;
134 c6eecea3 2020-07-26 stsp remain_right = right->len;
135 c6eecea3 2020-07-26 stsp while (remain_left > 0 || remain_right > 0) {
136 c6eecea3 2020-07-26 stsp const size_t chunksz = 8192;
137 c6eecea3 2020-07-26 stsp unsigned char buf_left[chunksz], buf_right[chunksz];
138 c6eecea3 2020-07-26 stsp const uint8_t *p_left, *p_right;
139 c6eecea3 2020-07-26 stsp off_t n_left, n_right;
140 c6eecea3 2020-07-26 stsp ssize_t r;
141 c6eecea3 2020-07-26 stsp
142 b3fb4686 2020-09-20 neels if (!remain_right) {
143 b3fb4686 2020-09-20 neels *cmp = 1;
144 b3fb4686 2020-09-20 neels return 0;
145 b3fb4686 2020-09-20 neels }
146 b3fb4686 2020-09-20 neels if (!remain_left) {
147 b3fb4686 2020-09-20 neels *cmp = -1;
148 b3fb4686 2020-09-20 neels return 0;
149 b3fb4686 2020-09-20 neels }
150 b3fb4686 2020-09-20 neels
151 c6eecea3 2020-07-26 stsp n_left = MIN(chunksz, remain_left);
152 c6eecea3 2020-07-26 stsp n_right = MIN(chunksz, remain_right);
153 c6eecea3 2020-07-26 stsp
154 c6eecea3 2020-07-26 stsp if (left->at == NULL) {
155 ad5b3f85 2020-10-12 neels r = read_at(left->root->f,
156 b3fb4686 2020-09-20 neels left->pos + (left->len - remain_left),
157 b3fb4686 2020-09-20 neels buf_left, n_left);
158 b3fb4686 2020-09-20 neels if (r) {
159 b3fb4686 2020-09-20 neels *cmp = 0;
160 b3fb4686 2020-09-20 neels return r;
161 b3fb4686 2020-09-20 neels }
162 c6eecea3 2020-07-26 stsp p_left = buf_left;
163 c6eecea3 2020-07-26 stsp } else {
164 c6eecea3 2020-07-26 stsp p_left = left->at + (left->len - remain_left);
165 c6eecea3 2020-07-26 stsp }
166 c6eecea3 2020-07-26 stsp
167 c6eecea3 2020-07-26 stsp if (right->at == NULL) {
168 ad5b3f85 2020-10-12 neels r = read_at(right->root->f,
169 b3fb4686 2020-09-20 neels right->pos + (right->len - remain_right),
170 b3fb4686 2020-09-20 neels buf_right, n_right);
171 b3fb4686 2020-09-20 neels if (r) {
172 b3fb4686 2020-09-20 neels *cmp = 0;
173 b3fb4686 2020-09-20 neels return r;
174 b3fb4686 2020-09-20 neels }
175 c6eecea3 2020-07-26 stsp p_right = buf_right;
176 c6eecea3 2020-07-26 stsp } else {
177 c6eecea3 2020-07-26 stsp p_right = right->at + (right->len - remain_right);
178 c6eecea3 2020-07-26 stsp }
179 b3fb4686 2020-09-20 neels
180 732e8ee0 2020-09-20 stsp r = buf_cmp(p_left, n_left, p_right, n_right,
181 732e8ee0 2020-09-20 stsp ignore_whitespace);
182 b3fb4686 2020-09-20 neels if (r) {
183 b3fb4686 2020-09-20 neels *cmp = r;
184 b3fb4686 2020-09-20 neels return 0;
185 c6eecea3 2020-07-26 stsp }
186 c6eecea3 2020-07-26 stsp
187 c6eecea3 2020-07-26 stsp remain_left -= n_left;
188 c6eecea3 2020-07-26 stsp remain_right -= n_right;
189 c6eecea3 2020-07-26 stsp }
190 3b0f3d61 2020-01-22 neels
191 b3fb4686 2020-09-20 neels *cmp = 0;
192 b3fb4686 2020-09-20 neels return 0;
193 b3fb4686 2020-09-20 neels }
194 b3fb4686 2020-09-20 neels
195 b3fb4686 2020-09-20 neels int
196 b3fb4686 2020-09-20 neels diff_atom_same(bool *same,
197 b3fb4686 2020-09-20 neels const struct diff_atom *left,
198 b3fb4686 2020-09-20 neels const struct diff_atom *right)
199 b3fb4686 2020-09-20 neels {
200 b3fb4686 2020-09-20 neels int cmp;
201 2fa08c64 2020-10-22 neels int r;
202 2fa08c64 2020-10-22 neels if (left->hash != right->hash) {
203 2fa08c64 2020-10-22 neels *same = false;
204 2fa08c64 2020-10-22 neels return 0;
205 2fa08c64 2020-10-22 neels }
206 2fa08c64 2020-10-22 neels r = diff_atom_cmp(&cmp, left, right);
207 b3fb4686 2020-09-20 neels if (r) {
208 b3fb4686 2020-09-20 neels *same = true;
209 b3fb4686 2020-09-20 neels return r;
210 b3fb4686 2020-09-20 neels }
211 b3fb4686 2020-09-20 neels *same = (cmp == 0);
212 b3fb4686 2020-09-20 neels return 0;
213 c6eecea3 2020-07-26 stsp }
214 c6eecea3 2020-07-26 stsp
215 93f8150a 2020-10-11 neels static struct diff_chunk *
216 93f8150a 2020-10-11 neels diff_state_add_solved_chunk(struct diff_state *state,
217 93f8150a 2020-10-11 neels const struct diff_chunk *chunk)
218 3b0f3d61 2020-01-22 neels {
219 93f8150a 2020-10-11 neels diff_chunk_arraylist_t *result;
220 8546b045 2020-09-20 neels struct diff_chunk *new_chunk;
221 8546b045 2020-09-20 neels enum diff_chunk_type last_t;
222 8546b045 2020-09-20 neels enum diff_chunk_type new_t;
223 f6db3145 2020-10-30 neels struct diff_chunk *last;
224 8546b045 2020-09-20 neels
225 8546b045 2020-09-20 neels /* Append to solved chunks; make sure that adjacent chunks of same type are combined, and that a minus chunk
226 8546b045 2020-09-20 neels * never directly follows a plus chunk. */
227 8546b045 2020-09-20 neels result = &state->result->chunks;
228 8546b045 2020-09-20 neels
229 486215cf 2020-10-30 neels last_t = result->len ? diff_chunk_type(&result->head[result->len - 1])
230 486215cf 2020-10-30 neels : CHUNK_EMPTY;
231 93f8150a 2020-10-11 neels new_t = diff_chunk_type(chunk);
232 8546b045 2020-09-20 neels
233 486215cf 2020-10-30 neels debug("ADD %s chunk #%u:\n", chunk->solved ? "solved" : "UNSOLVED",
234 486215cf 2020-10-30 neels result->len);
235 93f8150a 2020-10-11 neels debug("L\n");
236 93f8150a 2020-10-11 neels debug_dump_atoms(&state->left, chunk->left_start, chunk->left_count);
237 93f8150a 2020-10-11 neels debug("R\n");
238 93f8150a 2020-10-11 neels debug_dump_atoms(&state->right, chunk->right_start, chunk->right_count);
239 4861c9da 2020-10-30 neels
240 4861c9da 2020-10-30 neels if (result->len) {
241 4861c9da 2020-10-30 neels last = &result->head[result->len - 1];
242 4861c9da 2020-10-30 neels assert(chunk->left_start
243 4861c9da 2020-10-30 neels == last->left_start + last->left_count);
244 4861c9da 2020-10-30 neels assert(chunk->right_start
245 4861c9da 2020-10-30 neels == last->right_start + last->right_count);
246 4861c9da 2020-10-30 neels }
247 93f8150a 2020-10-11 neels
248 8546b045 2020-09-20 neels if (new_t == last_t) {
249 8546b045 2020-09-20 neels new_chunk = &result->head[result->len - 1];
250 93f8150a 2020-10-11 neels new_chunk->left_count += chunk->left_count;
251 93f8150a 2020-10-11 neels new_chunk->right_count += chunk->right_count;
252 bb867e68 2020-10-11 neels debug(" - added chunk touches previous one of same type, joined:\n");
253 bb867e68 2020-10-11 neels debug("L\n");
254 bb867e68 2020-10-11 neels debug_dump_atoms(&state->left, new_chunk->left_start, new_chunk->left_count);
255 bb867e68 2020-10-11 neels debug("R\n");
256 bb867e68 2020-10-11 neels debug_dump_atoms(&state->right, new_chunk->right_start, new_chunk->right_count);
257 8546b045 2020-09-20 neels } else if (last_t == CHUNK_PLUS && new_t == CHUNK_MINUS) {
258 93f8150a 2020-10-11 neels enum diff_chunk_type prev_last_t =
259 93f8150a 2020-10-11 neels result->len > 1 ?
260 93f8150a 2020-10-11 neels diff_chunk_type(&result->head[result->len - 2])
261 93f8150a 2020-10-11 neels : CHUNK_EMPTY;
262 93f8150a 2020-10-11 neels /* If a minus-chunk follows a plus-chunk, place it above the plus-chunk->
263 8546b045 2020-09-20 neels * Is the one before that also a minus? combine. */
264 8546b045 2020-09-20 neels if (prev_last_t == CHUNK_MINUS) {
265 8546b045 2020-09-20 neels new_chunk = &result->head[result->len - 2];
266 93f8150a 2020-10-11 neels new_chunk->left_count += chunk->left_count;
267 93f8150a 2020-10-11 neels new_chunk->right_count += chunk->right_count;
268 bb867e68 2020-10-11 neels
269 bb867e68 2020-10-11 neels debug(" - added minus-chunk follows plus-chunk,"
270 bb867e68 2020-10-11 neels " put before that plus-chunk and joined"
271 bb867e68 2020-10-11 neels " with preceding minus-chunk:\n");
272 bb867e68 2020-10-11 neels debug("L\n");
273 bb867e68 2020-10-11 neels debug_dump_atoms(&state->left, new_chunk->left_start, new_chunk->left_count);
274 bb867e68 2020-10-11 neels debug("R\n");
275 bb867e68 2020-10-11 neels debug_dump_atoms(&state->right, new_chunk->right_start, new_chunk->right_count);
276 8546b045 2020-09-20 neels } else {
277 724967e9 2020-10-11 neels ARRAYLIST_INSERT(new_chunk, *result, result->len - 1);
278 8546b045 2020-09-20 neels if (!new_chunk)
279 8546b045 2020-09-20 neels return NULL;
280 93f8150a 2020-10-11 neels *new_chunk = *chunk;
281 f6db3145 2020-10-30 neels
282 f6db3145 2020-10-30 neels /* The new minus chunk indicates to which position on
283 f6db3145 2020-10-30 neels * the right it corresponds, even though it doesn't add
284 f6db3145 2020-10-30 neels * any lines on the right. By moving above a plus chunk,
285 f6db3145 2020-10-30 neels * that position on the right has shifted. */
286 f6db3145 2020-10-30 neels last = &result->head[result->len - 1];
287 f6db3145 2020-10-30 neels new_chunk->right_start = last->right_start;
288 bb867e68 2020-10-11 neels
289 bb867e68 2020-10-11 neels debug(" - added minus-chunk follows plus-chunk,"
290 bb867e68 2020-10-11 neels " put before that plus-chunk\n");
291 8546b045 2020-09-20 neels }
292 f6db3145 2020-10-30 neels
293 f6db3145 2020-10-30 neels /* That last_t == CHUNK_PLUS indicates to which position on the
294 f6db3145 2020-10-30 neels * left it corresponds, even though it doesn't add any lines on
295 f6db3145 2020-10-30 neels * the left. By inserting/extending the prev_last_t ==
296 f6db3145 2020-10-30 neels * CHUNK_MINUS, that position on the left has shifted. */
297 f6db3145 2020-10-30 neels last = &result->head[result->len - 1];
298 f6db3145 2020-10-30 neels last->left_start = new_chunk->left_start
299 f6db3145 2020-10-30 neels + new_chunk->left_count;
300 f6db3145 2020-10-30 neels
301 8546b045 2020-09-20 neels } else {
302 8546b045 2020-09-20 neels ARRAYLIST_ADD(new_chunk, *result);
303 8546b045 2020-09-20 neels if (!new_chunk)
304 8546b045 2020-09-20 neels return NULL;
305 93f8150a 2020-10-11 neels *new_chunk = *chunk;
306 93f8150a 2020-10-11 neels }
307 93f8150a 2020-10-11 neels return new_chunk;
308 93f8150a 2020-10-11 neels }
309 93f8150a 2020-10-11 neels
310 93f8150a 2020-10-11 neels /* Even if a left or right side is empty, diff output may need to know the
311 93f8150a 2020-10-11 neels * position in that file.
312 93f8150a 2020-10-11 neels * So left_start or right_start must never be NULL -- pass left_count or
313 93f8150a 2020-10-11 neels * right_count as zero to indicate staying at that position without consuming
314 93f8150a 2020-10-11 neels * any lines. */
315 93f8150a 2020-10-11 neels struct diff_chunk *
316 93f8150a 2020-10-11 neels diff_state_add_chunk(struct diff_state *state, bool solved,
317 93f8150a 2020-10-11 neels struct diff_atom *left_start, unsigned int left_count,
318 93f8150a 2020-10-11 neels struct diff_atom *right_start, unsigned int right_count)
319 93f8150a 2020-10-11 neels {
320 93f8150a 2020-10-11 neels struct diff_chunk *new_chunk;
321 93f8150a 2020-10-11 neels struct diff_chunk chunk = {
322 93f8150a 2020-10-11 neels .solved = solved,
323 93f8150a 2020-10-11 neels .left_start = left_start,
324 93f8150a 2020-10-11 neels .left_count = left_count,
325 93f8150a 2020-10-11 neels .right_start = right_start,
326 93f8150a 2020-10-11 neels .right_count = right_count,
327 93f8150a 2020-10-11 neels };
328 93f8150a 2020-10-11 neels
329 486215cf 2020-10-30 neels /* An unsolved chunk means store as intermediate result for later
330 486215cf 2020-10-30 neels * re-iteration.
331 486215cf 2020-10-30 neels * If there already are intermediate results, that means even a
332 486215cf 2020-10-30 neels * following solved chunk needs to go to intermediate results, so that
333 486215cf 2020-10-30 neels * it is later put in the final correct position in solved chunks.
334 486215cf 2020-10-30 neels */
335 93f8150a 2020-10-11 neels if (!solved || state->temp_result.len) {
336 93f8150a 2020-10-11 neels /* Append to temp_result */
337 486215cf 2020-10-30 neels debug("ADD %s chunk to temp result:\n",
338 486215cf 2020-10-30 neels chunk.solved ? "solved" : "UNSOLVED");
339 acfce337 2020-10-12 neels debug("L\n");
340 acfce337 2020-10-12 neels debug_dump_atoms(&state->left, left_start, left_count);
341 acfce337 2020-10-12 neels debug("R\n");
342 acfce337 2020-10-12 neels debug_dump_atoms(&state->right, right_start, right_count);
343 486215cf 2020-10-30 neels ARRAYLIST_ADD(new_chunk, state->temp_result);
344 93f8150a 2020-10-11 neels if (!new_chunk)
345 93f8150a 2020-10-11 neels return NULL;
346 8546b045 2020-09-20 neels *new_chunk = chunk;
347 93f8150a 2020-10-11 neels return new_chunk;
348 8546b045 2020-09-20 neels }
349 8546b045 2020-09-20 neels
350 93f8150a 2020-10-11 neels return diff_state_add_solved_chunk(state, &chunk);
351 3b0f3d61 2020-01-22 neels }
352 3b0f3d61 2020-01-22 neels
353 1f690ebe 2022-08-31 mark static void
354 7a54ad3a 2020-09-20 stsp diff_data_init_root(struct diff_data *d, FILE *f, const uint8_t *data,
355 00d5652b 2020-09-22 stsp unsigned long long len, int diff_flags)
356 3b0f3d61 2020-01-22 neels {
357 3b0f3d61 2020-01-22 neels *d = (struct diff_data){
358 7a54ad3a 2020-09-20 stsp .f = f,
359 c6eecea3 2020-07-26 stsp .pos = 0,
360 3b0f3d61 2020-01-22 neels .data = data,
361 3b0f3d61 2020-01-22 neels .len = len,
362 3b0f3d61 2020-01-22 neels .root = d,
363 00d5652b 2020-09-22 stsp .diff_flags = diff_flags,
364 3b0f3d61 2020-01-22 neels };
365 3b0f3d61 2020-01-22 neels }
366 3b0f3d61 2020-01-22 neels
367 61a7b578 2020-05-06 neels void
368 61a7b578 2020-05-06 neels diff_data_init_subsection(struct diff_data *d, struct diff_data *parent,
369 61a7b578 2020-05-06 neels struct diff_atom *from_atom, unsigned int atoms_count)
370 3b0f3d61 2020-01-22 neels {
371 05b5f01f 2020-09-21 stsp struct diff_atom *last_atom;
372 05b5f01f 2020-09-21 stsp
373 de7a2939 2020-10-24 neels debug("diff_data %p parent %p from_atom %p atoms_count %u\n",
374 de7a2939 2020-10-24 neels d, parent, from_atom, atoms_count);
375 de7a2939 2020-10-24 neels debug(" from_atom ");
376 f5a254cc 2020-10-24 neels debug_dump_atom(parent, NULL, from_atom);
377 de7a2939 2020-10-24 neels
378 05b5f01f 2020-09-21 stsp if (atoms_count == 0) {
379 05b5f01f 2020-09-21 stsp *d = (struct diff_data){
380 05b5f01f 2020-09-21 stsp .f = NULL,
381 05b5f01f 2020-09-21 stsp .pos = 0,
382 34570dbe 2020-10-16 stsp .data = NULL,
383 05b5f01f 2020-09-21 stsp .len = 0,
384 05b5f01f 2020-09-21 stsp .root = parent->root,
385 05b5f01f 2020-09-21 stsp .atoms.head = NULL,
386 05b5f01f 2020-09-21 stsp .atoms.len = atoms_count,
387 05b5f01f 2020-09-21 stsp };
388 05b5f01f 2020-09-21 stsp
389 05b5f01f 2020-09-21 stsp return;
390 05b5f01f 2020-09-21 stsp }
391 05b5f01f 2020-09-21 stsp
392 05b5f01f 2020-09-21 stsp last_atom = from_atom + atoms_count - 1;
393 3b0f3d61 2020-01-22 neels *d = (struct diff_data){
394 7a54ad3a 2020-09-20 stsp .f = NULL,
395 c6eecea3 2020-07-26 stsp .pos = from_atom->pos,
396 3b0f3d61 2020-01-22 neels .data = from_atom->at,
397 c6eecea3 2020-07-26 stsp .len = (last_atom->pos + last_atom->len) - from_atom->pos,
398 3b0f3d61 2020-01-22 neels .root = parent->root,
399 3b0f3d61 2020-01-22 neels .atoms.head = from_atom,
400 3b0f3d61 2020-01-22 neels .atoms.len = atoms_count,
401 3b0f3d61 2020-01-22 neels };
402 3b0f3d61 2020-01-22 neels
403 3b0f3d61 2020-01-22 neels debug("subsection:\n");
404 3b0f3d61 2020-01-22 neels debug_dump(d);
405 3b0f3d61 2020-01-22 neels }
406 3b0f3d61 2020-01-22 neels
407 61a7b578 2020-05-06 neels void
408 61a7b578 2020-05-06 neels diff_data_free(struct diff_data *diff_data)
409 3b0f3d61 2020-01-22 neels {
410 3b0f3d61 2020-01-22 neels if (!diff_data)
411 3b0f3d61 2020-01-22 neels return;
412 3b0f3d61 2020-01-22 neels if (diff_data->atoms.allocated)
413 3b0f3d61 2020-01-22 neels ARRAYLIST_FREE(diff_data->atoms);
414 3b0f3d61 2020-01-22 neels }
415 3b0f3d61 2020-01-22 neels
416 3e6cba3a 2020-08-13 stsp int
417 0d27172a 2020-05-06 neels diff_algo_none(const struct diff_algo_config *algo_config,
418 0d27172a 2020-05-06 neels struct diff_state *state)
419 3b0f3d61 2020-01-22 neels {
420 3b0f3d61 2020-01-22 neels debug("\n** %s\n", __func__);
421 3b0f3d61 2020-01-22 neels debug("left:\n");
422 3b0f3d61 2020-01-22 neels debug_dump(&state->left);
423 3b0f3d61 2020-01-22 neels debug("right:\n");
424 3b0f3d61 2020-01-22 neels debug_dump(&state->right);
425 0d27172a 2020-05-06 neels debug_dump_myers_graph(&state->left, &state->right, NULL, NULL, 0, NULL,
426 0d27172a 2020-05-06 neels 0);
427 3b0f3d61 2020-01-22 neels
428 3b0f3d61 2020-01-22 neels /* Add a chunk of equal lines, if any */
429 467a993d 2020-10-11 neels struct diff_atom *l = state->left.atoms.head;
430 467a993d 2020-10-11 neels unsigned int l_len = state->left.atoms.len;
431 467a993d 2020-10-11 neels struct diff_atom *r = state->right.atoms.head;
432 467a993d 2020-10-11 neels unsigned int r_len = state->right.atoms.len;
433 41ff30f3 2020-10-11 neels unsigned int equal_atoms_start = 0;
434 2146cf12 2020-10-11 neels unsigned int equal_atoms_end = 0;
435 13497fff 2020-10-11 neels unsigned int l_idx = 0;
436 13497fff 2020-10-11 neels unsigned int r_idx = 0;
437 467a993d 2020-10-11 neels
438 41ff30f3 2020-10-11 neels while (equal_atoms_start < l_len
439 41ff30f3 2020-10-11 neels && equal_atoms_start < r_len) {
440 467a993d 2020-10-11 neels int err;
441 b3fb4686 2020-09-20 neels bool same;
442 41ff30f3 2020-10-11 neels err = diff_atom_same(&same, &l[equal_atoms_start],
443 e5447b81 2020-10-12 neels &r[equal_atoms_start]);
444 467a993d 2020-10-11 neels if (err)
445 467a993d 2020-10-11 neels return err;
446 b3fb4686 2020-09-20 neels if (!same)
447 b3fb4686 2020-09-20 neels break;
448 41ff30f3 2020-10-11 neels equal_atoms_start++;
449 b3fb4686 2020-09-20 neels }
450 2146cf12 2020-10-11 neels while (equal_atoms_end < (l_len - equal_atoms_start)
451 2146cf12 2020-10-11 neels && equal_atoms_end < (r_len - equal_atoms_start)) {
452 2146cf12 2020-10-11 neels int err;
453 2146cf12 2020-10-11 neels bool same;
454 2146cf12 2020-10-11 neels err = diff_atom_same(&same, &l[l_len - 1 - equal_atoms_end],
455 2146cf12 2020-10-11 neels &r[r_len - 1 - equal_atoms_end]);
456 2146cf12 2020-10-11 neels if (err)
457 2146cf12 2020-10-11 neels return err;
458 2146cf12 2020-10-11 neels if (!same)
459 2146cf12 2020-10-11 neels break;
460 2146cf12 2020-10-11 neels equal_atoms_end++;
461 2146cf12 2020-10-11 neels }
462 2146cf12 2020-10-11 neels
463 2146cf12 2020-10-11 neels /* Add a chunk of equal lines at the start */
464 41ff30f3 2020-10-11 neels if (equal_atoms_start) {
465 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
466 e5447b81 2020-10-12 neels l, equal_atoms_start,
467 e5447b81 2020-10-12 neels r, equal_atoms_start))
468 3e6cba3a 2020-08-13 stsp return ENOMEM;
469 13497fff 2020-10-11 neels l_idx += equal_atoms_start;
470 13497fff 2020-10-11 neels r_idx += equal_atoms_start;
471 3b0f3d61 2020-01-22 neels }
472 3b0f3d61 2020-01-22 neels
473 3b0f3d61 2020-01-22 neels /* Add a "minus" chunk with all lines from the left. */
474 2146cf12 2020-10-11 neels if (equal_atoms_start + equal_atoms_end < l_len) {
475 13497fff 2020-10-11 neels unsigned int add_len = l_len - equal_atoms_start - equal_atoms_end;
476 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
477 13497fff 2020-10-11 neels &l[l_idx], add_len,
478 13497fff 2020-10-11 neels &r[r_idx], 0))
479 13497fff 2020-10-11 neels return ENOMEM;
480 13497fff 2020-10-11 neels l_idx += add_len;
481 3b0f3d61 2020-01-22 neels }
482 3b0f3d61 2020-01-22 neels
483 3b0f3d61 2020-01-22 neels /* Add a "plus" chunk with all lines from the right. */
484 2146cf12 2020-10-11 neels if (equal_atoms_start + equal_atoms_end < r_len) {
485 13497fff 2020-10-11 neels unsigned int add_len = r_len - equal_atoms_start - equal_atoms_end;
486 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
487 9dc0554f 2020-10-12 neels &l[l_idx], 0,
488 9dc0554f 2020-10-12 neels &r[r_idx], add_len))
489 13497fff 2020-10-11 neels return ENOMEM;
490 13497fff 2020-10-11 neels r_idx += add_len;
491 3b0f3d61 2020-01-22 neels }
492 2146cf12 2020-10-11 neels
493 2146cf12 2020-10-11 neels /* Add a chunk of equal lines at the end */
494 2146cf12 2020-10-11 neels if (equal_atoms_end) {
495 2146cf12 2020-10-11 neels if (!diff_state_add_chunk(state, true,
496 13497fff 2020-10-11 neels &l[l_idx], equal_atoms_end,
497 13497fff 2020-10-11 neels &r[r_idx], equal_atoms_end))
498 2146cf12 2020-10-11 neels return ENOMEM;
499 2146cf12 2020-10-11 neels }
500 2146cf12 2020-10-11 neels
501 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;
502 3b0f3d61 2020-01-22 neels }
503 3b0f3d61 2020-01-22 neels
504 1f690ebe 2022-08-31 mark static int
505 0d27172a 2020-05-06 neels diff_run_algo(const struct diff_algo_config *algo_config,
506 0d27172a 2020-05-06 neels struct diff_state *state)
507 3b0f3d61 2020-01-22 neels {
508 3e6cba3a 2020-08-13 stsp int rc;
509 3b0f3d61 2020-01-22 neels
510 0d27172a 2020-05-06 neels if (!algo_config || !algo_config->impl
511 746d94df 2020-10-12 neels || !state->recursion_depth_left
512 746d94df 2020-10-12 neels || !state->left.atoms.len || !state->right.atoms.len) {
513 746d94df 2020-10-12 neels debug("Fall back to diff_algo_none():%s%s%s\n",
514 746d94df 2020-10-12 neels (!algo_config || !algo_config->impl) ? " no-cfg" : "",
515 746d94df 2020-10-12 neels (!state->recursion_depth_left) ? " max-depth" : "",
516 746d94df 2020-10-12 neels (!state->left.atoms.len || !state->right.atoms.len)?
517 746d94df 2020-10-12 neels " trivial" : "");
518 3b0f3d61 2020-01-22 neels return diff_algo_none(algo_config, state);
519 3b0f3d61 2020-01-22 neels }
520 3b0f3d61 2020-01-22 neels
521 746d94df 2020-10-12 neels ARRAYLIST_FREE(state->temp_result);
522 3b0f3d61 2020-01-22 neels ARRAYLIST_INIT(state->temp_result, DIFF_RESULT_ALLOC_BLOCKSIZE);
523 3b0f3d61 2020-01-22 neels rc = algo_config->impl(algo_config, state);
524 3b0f3d61 2020-01-22 neels switch (rc) {
525 3b0f3d61 2020-01-22 neels case DIFF_RC_USE_DIFF_ALGO_FALLBACK:
526 0d27172a 2020-05-06 neels debug("Got DIFF_RC_USE_DIFF_ALGO_FALLBACK (%p)\n",
527 0d27172a 2020-05-06 neels algo_config->fallback_algo);
528 3b0f3d61 2020-01-22 neels rc = diff_run_algo(algo_config->fallback_algo, state);
529 3b0f3d61 2020-01-22 neels goto return_rc;
530 3b0f3d61 2020-01-22 neels
531 3b0f3d61 2020-01-22 neels case DIFF_RC_OK:
532 3b0f3d61 2020-01-22 neels /* continue below */
533 3b0f3d61 2020-01-22 neels break;
534 3b0f3d61 2020-01-22 neels
535 3b0f3d61 2020-01-22 neels default:
536 3b0f3d61 2020-01-22 neels /* some error happened */
537 3b0f3d61 2020-01-22 neels goto return_rc;
538 3b0f3d61 2020-01-22 neels }
539 3b0f3d61 2020-01-22 neels
540 0d27172a 2020-05-06 neels /* Pick up any diff chunks that are still unsolved and feed to
541 0d27172a 2020-05-06 neels * inner_algo. inner_algo will solve unsolved chunks and append to
542 0d27172a 2020-05-06 neels * result, and subsequent solved chunks on this level are then appended
543 0d27172a 2020-05-06 neels * to result afterwards. */
544 3b0f3d61 2020-01-22 neels int i;
545 3b0f3d61 2020-01-22 neels for (i = 0; i < state->temp_result.len; i++) {
546 3b0f3d61 2020-01-22 neels struct diff_chunk *c = &state->temp_result.head[i];
547 3b0f3d61 2020-01-22 neels if (c->solved) {
548 93f8150a 2020-10-11 neels diff_state_add_solved_chunk(state, c);
549 3b0f3d61 2020-01-22 neels continue;
550 3b0f3d61 2020-01-22 neels }
551 3b0f3d61 2020-01-22 neels
552 3b0f3d61 2020-01-22 neels /* c is an unsolved chunk, feed to inner_algo */
553 3b0f3d61 2020-01-22 neels struct diff_state inner_state = {
554 3b0f3d61 2020-01-22 neels .result = state->result,
555 3b0f3d61 2020-01-22 neels .recursion_depth_left = state->recursion_depth_left - 1,
556 8be88dfa 2020-10-21 stsp .kd_buf = state->kd_buf,
557 8be88dfa 2020-10-21 stsp .kd_buf_size = state->kd_buf_size,
558 3b0f3d61 2020-01-22 neels };
559 0d27172a 2020-05-06 neels diff_data_init_subsection(&inner_state.left, &state->left,
560 0d27172a 2020-05-06 neels c->left_start, c->left_count);
561 0d27172a 2020-05-06 neels diff_data_init_subsection(&inner_state.right, &state->right,
562 0d27172a 2020-05-06 neels c->right_start, c->right_count);
563 3b0f3d61 2020-01-22 neels
564 3b0f3d61 2020-01-22 neels rc = diff_run_algo(algo_config->inner_algo, &inner_state);
565 8be88dfa 2020-10-21 stsp state->kd_buf = inner_state.kd_buf;
566 8be88dfa 2020-10-21 stsp state->kd_buf_size = inner_state.kd_buf_size;
567 3b0f3d61 2020-01-22 neels if (rc != DIFF_RC_OK)
568 3b0f3d61 2020-01-22 neels goto return_rc;
569 3b0f3d61 2020-01-22 neels }
570 3b0f3d61 2020-01-22 neels
571 3b0f3d61 2020-01-22 neels rc = DIFF_RC_OK;
572 3b0f3d61 2020-01-22 neels return_rc:
573 3b0f3d61 2020-01-22 neels ARRAYLIST_FREE(state->temp_result);
574 3b0f3d61 2020-01-22 neels return rc;
575 3b0f3d61 2020-01-22 neels }
576 c16dde50 2020-10-22 stsp
577 c16dde50 2020-10-22 stsp int
578 c16dde50 2020-10-22 stsp diff_atomize_file(struct diff_data *d,
579 c16dde50 2020-10-22 stsp const struct diff_config *config,
580 c16dde50 2020-10-22 stsp FILE *f, const uint8_t *data, off_t len, int diff_flags)
581 c16dde50 2020-10-22 stsp {
582 c16dde50 2020-10-22 stsp if (!config->atomize_func)
583 c16dde50 2020-10-22 stsp return EINVAL;
584 3b0f3d61 2020-01-22 neels
585 c16dde50 2020-10-22 stsp diff_data_init_root(d, f, data, len, diff_flags);
586 c16dde50 2020-10-22 stsp
587 c16dde50 2020-10-22 stsp return config->atomize_func(config->atomize_func_data, d);
588 c16dde50 2020-10-22 stsp
589 c16dde50 2020-10-22 stsp }
590 c16dde50 2020-10-22 stsp
591 61a7b578 2020-05-06 neels struct diff_result *
592 c16dde50 2020-10-22 stsp diff_main(const struct diff_config *config, struct diff_data *left,
593 c16dde50 2020-10-22 stsp struct diff_data *right)
594 3b0f3d61 2020-01-22 neels {
595 3b0f3d61 2020-01-22 neels struct diff_result *result = malloc(sizeof(struct diff_result));
596 3b0f3d61 2020-01-22 neels if (!result)
597 3b0f3d61 2020-01-22 neels return NULL;
598 3b0f3d61 2020-01-22 neels
599 3b0f3d61 2020-01-22 neels *result = (struct diff_result){};
600 c16dde50 2020-10-22 stsp result->left = left;
601 c16dde50 2020-10-22 stsp result->right = right;
602 3b0f3d61 2020-01-22 neels
603 3b0f3d61 2020-01-22 neels struct diff_state state = {
604 3b0f3d61 2020-01-22 neels .result = result,
605 35eae7fa 2022-08-31 mark .recursion_depth_left = config->max_recursion_depth ?
606 35eae7fa 2022-08-31 mark config->max_recursion_depth : UINT_MAX,
607 8be88dfa 2020-10-21 stsp .kd_buf = NULL,
608 8be88dfa 2020-10-21 stsp .kd_buf_size = 0,
609 3b0f3d61 2020-01-22 neels };
610 c16dde50 2020-10-22 stsp diff_data_init_subsection(&state.left, left,
611 c16dde50 2020-10-22 stsp left->atoms.head,
612 c16dde50 2020-10-22 stsp left->atoms.len);
613 c16dde50 2020-10-22 stsp diff_data_init_subsection(&state.right, right,
614 c16dde50 2020-10-22 stsp right->atoms.head,
615 c16dde50 2020-10-22 stsp right->atoms.len);
616 3b0f3d61 2020-01-22 neels
617 3b0f3d61 2020-01-22 neels result->rc = diff_run_algo(config->algo, &state);
618 8be88dfa 2020-10-21 stsp free(state.kd_buf);
619 3b0f3d61 2020-01-22 neels
620 3b0f3d61 2020-01-22 neels return result;
621 3b0f3d61 2020-01-22 neels }
622 3b0f3d61 2020-01-22 neels
623 61a7b578 2020-05-06 neels void
624 61a7b578 2020-05-06 neels diff_result_free(struct diff_result *result)
625 3b0f3d61 2020-01-22 neels {
626 3b0f3d61 2020-01-22 neels if (!result)
627 3b0f3d61 2020-01-22 neels return;
628 3b0f3d61 2020-01-22 neels ARRAYLIST_FREE(result->chunks);
629 3b0f3d61 2020-01-22 neels free(result);
630 9f5da091 2022-10-10 tj }
631 9f5da091 2022-10-10 tj
632 9f5da091 2022-10-10 tj int
633 9f5da091 2022-10-10 tj diff_result_contains_printable_chunks(struct diff_result *result)
634 9f5da091 2022-10-10 tj {
635 9f5da091 2022-10-10 tj struct diff_chunk *c;
636 9f5da091 2022-10-10 tj enum diff_chunk_type t;
637 9f5da091 2022-10-10 tj
638 9f5da091 2022-10-10 tj for (int i = 0; i < result->chunks.len; i++) {
639 9f5da091 2022-10-10 tj c = &result->chunks.head[i];
640 9f5da091 2022-10-10 tj t = diff_chunk_type(c);
641 9f5da091 2022-10-10 tj if (t == CHUNK_MINUS || t == CHUNK_PLUS)
642 9f5da091 2022-10-10 tj return 1;
643 9f5da091 2022-10-10 tj }
644 9f5da091 2022-10-10 tj
645 9f5da091 2022-10-10 tj return 0;
646 3b0f3d61 2020-01-22 neels }