1 /* Generic infrastructure to implement various diff algorithms (implementation). */
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
32 #include <arraylist.h>
33 #include <diff_main.h>
35 #include "diff_internal.h"
36 #include "diff_debug.h"
39 read_at(FILE *f, off_t at_pos, unsigned char *buf, size_t len)
42 if (fseeko(f, at_pos, SEEK_SET) == -1)
44 r = fread(buf, sizeof(char), len, f);
45 if ((r == 0 || r < len) && ferror(f))
53 buf_cmp(const unsigned char *left, size_t left_len,
54 const unsigned char *right, size_t right_len,
55 bool ignore_whitespace)
59 if (ignore_whitespace) {
61 while (il < left_len && ir < right_len) {
62 unsigned char cl = left[il];
63 unsigned char cr = right[ir];
65 if (isspace(cl) && il < left_len) {
69 if (isspace(cr) && ir < right_len) {
81 while (il < left_len) {
82 unsigned char cl = left[il++];
86 while (ir < right_len) {
87 unsigned char cr = right[ir++];
95 cmp = memcmp(left, right, MIN(left_len, right_len));
98 if (left_len == right_len)
100 return (left_len > right_len) ? 1 : -1;
104 diff_atom_cmp(int *cmp,
105 const struct diff_atom *left,
106 const struct diff_atom *right)
108 off_t remain_left, remain_right;
109 int flags = (left->root->diff_flags | right->root->diff_flags);
110 bool ignore_whitespace = (flags & DIFF_FLAG_IGNORE_WHITESPACE);
112 if (!left->len && !right->len) {
116 if (!ignore_whitespace) {
127 if (left->at != NULL && right->at != NULL) {
128 *cmp = buf_cmp(left->at, left->len, right->at, right->len,
133 remain_left = left->len;
134 remain_right = right->len;
135 while (remain_left > 0 || remain_right > 0) {
136 const size_t chunksz = 8192;
137 unsigned char buf_left[chunksz], buf_right[chunksz];
138 const uint8_t *p_left, *p_right;
139 off_t n_left, n_right;
151 n_left = MIN(chunksz, remain_left);
152 n_right = MIN(chunksz, remain_right);
154 if (left->at == NULL) {
155 r = read_at(left->root->f,
156 left->pos + (left->len - remain_left),
164 p_left = left->at + (left->len - remain_left);
167 if (right->at == NULL) {
168 r = read_at(right->root->f,
169 right->pos + (right->len - remain_right),
177 p_right = right->at + (right->len - remain_right);
180 r = buf_cmp(p_left, n_left, p_right, n_right,
187 remain_left -= n_left;
188 remain_right -= n_right;
196 diff_atom_same(bool *same,
197 const struct diff_atom *left,
198 const struct diff_atom *right)
202 if (left->hash != right->hash) {
206 r = diff_atom_cmp(&cmp, left, right);
215 static struct diff_chunk *
216 diff_state_add_solved_chunk(struct diff_state *state,
217 const struct diff_chunk *chunk)
219 diff_chunk_arraylist_t *result;
220 struct diff_chunk *new_chunk;
221 enum diff_chunk_type last_t;
222 enum diff_chunk_type new_t;
224 /* Append to solved chunks; make sure that adjacent chunks of same type are combined, and that a minus chunk
225 * never directly follows a plus chunk. */
226 result = &state->result->chunks;
228 last_t = diff_chunk_type(&result->head[result->len - 1]);
229 new_t = diff_chunk_type(chunk);
231 debug("Add %s chunk:\n", chunk->solved ? "solved" : "UNSOLVED");
233 debug_dump_atoms(&state->left, chunk->left_start, chunk->left_count);
235 debug_dump_atoms(&state->right, chunk->right_start, chunk->right_count);
237 if (new_t == last_t) {
238 new_chunk = &result->head[result->len - 1];
239 new_chunk->left_count += chunk->left_count;
240 new_chunk->right_count += chunk->right_count;
241 debug(" - added chunk touches previous one of same type, joined:\n");
243 debug_dump_atoms(&state->left, new_chunk->left_start, new_chunk->left_count);
245 debug_dump_atoms(&state->right, new_chunk->right_start, new_chunk->right_count);
246 } else if (last_t == CHUNK_PLUS && new_t == CHUNK_MINUS) {
247 enum diff_chunk_type prev_last_t =
249 diff_chunk_type(&result->head[result->len - 2])
251 /* If a minus-chunk follows a plus-chunk, place it above the plus-chunk->
252 * Is the one before that also a minus? combine. */
253 if (prev_last_t == CHUNK_MINUS) {
254 new_chunk = &result->head[result->len - 2];
255 new_chunk->left_count += chunk->left_count;
256 new_chunk->right_count += chunk->right_count;
258 debug(" - added minus-chunk follows plus-chunk,"
259 " put before that plus-chunk and joined"
260 " with preceding minus-chunk:\n");
262 debug_dump_atoms(&state->left, new_chunk->left_start, new_chunk->left_count);
264 debug_dump_atoms(&state->right, new_chunk->right_start, new_chunk->right_count);
266 ARRAYLIST_INSERT(new_chunk, *result, result->len - 1);
271 debug(" - added minus-chunk follows plus-chunk,"
272 " put before that plus-chunk\n");
275 ARRAYLIST_ADD(new_chunk, *result);
283 /* Even if a left or right side is empty, diff output may need to know the
284 * position in that file.
285 * So left_start or right_start must never be NULL -- pass left_count or
286 * right_count as zero to indicate staying at that position without consuming
289 diff_state_add_chunk(struct diff_state *state, bool solved,
290 struct diff_atom *left_start, unsigned int left_count,
291 struct diff_atom *right_start, unsigned int right_count)
293 diff_chunk_arraylist_t *result = NULL;
294 struct diff_chunk *new_chunk;
295 struct diff_chunk chunk = {
297 .left_start = left_start,
298 .left_count = left_count,
299 .right_start = right_start,
300 .right_count = right_count,
303 if (!solved || state->temp_result.len) {
304 /* Append to temp_result */
305 result = &state->temp_result;
306 debug("append to temp_result %p L %u R %u\n", result,
307 left_count, right_count);
309 debug_dump_atoms(&state->left, left_start, left_count);
311 debug_dump_atoms(&state->right, right_start, right_count);
312 } else if (!state->result->chunks.len) {
313 /* Append to final result */
314 result = &state->result->chunks;
315 debug("Add first chunk:\n");
317 debug_dump_atoms(&state->left, left_start, left_count);
319 debug_dump_atoms(&state->right, right_start, right_count);
322 ARRAYLIST_ADD(new_chunk, *result);
329 return diff_state_add_solved_chunk(state, &chunk);
333 diff_data_init_root(struct diff_data *d, FILE *f, const uint8_t *data,
334 unsigned long long len, int diff_flags)
336 *d = (struct diff_data){
342 .diff_flags = diff_flags,
347 diff_data_init_subsection(struct diff_data *d, struct diff_data *parent,
348 struct diff_atom *from_atom, unsigned int atoms_count)
350 struct diff_atom *last_atom;
352 debug("diff_data %p parent %p from_atom %p atoms_count %u\n",
353 d, parent, from_atom, atoms_count);
354 debug(" from_atom ");
355 debug_dump_atom(parent, NULL, from_atom);
357 if (atoms_count == 0) {
358 *d = (struct diff_data){
363 .root = parent->root,
365 .atoms.len = atoms_count,
371 last_atom = from_atom + atoms_count - 1;
372 *d = (struct diff_data){
374 .pos = from_atom->pos,
375 .data = from_atom->at,
376 .len = (last_atom->pos + last_atom->len) - from_atom->pos,
377 .root = parent->root,
378 .atoms.head = from_atom,
379 .atoms.len = atoms_count,
382 debug("subsection:\n");
387 diff_data_free(struct diff_data *diff_data)
391 if (diff_data->atoms.allocated)
392 ARRAYLIST_FREE(diff_data->atoms);
396 diff_algo_none(const struct diff_algo_config *algo_config,
397 struct diff_state *state)
399 debug("\n** %s\n", __func__);
401 debug_dump(&state->left);
403 debug_dump(&state->right);
404 debug_dump_myers_graph(&state->left, &state->right, NULL, NULL, 0, NULL,
407 /* Add a chunk of equal lines, if any */
408 struct diff_atom *l = state->left.atoms.head;
409 unsigned int l_len = state->left.atoms.len;
410 struct diff_atom *r = state->right.atoms.head;
411 unsigned int r_len = state->right.atoms.len;
412 unsigned int equal_atoms_start = 0;
413 unsigned int equal_atoms_end = 0;
414 unsigned int l_idx = 0;
415 unsigned int r_idx = 0;
417 while (equal_atoms_start < l_len
418 && equal_atoms_start < r_len) {
421 err = diff_atom_same(&same, &l[equal_atoms_start],
422 &r[equal_atoms_start]);
429 while (equal_atoms_end < (l_len - equal_atoms_start)
430 && equal_atoms_end < (r_len - equal_atoms_start)) {
433 err = diff_atom_same(&same, &l[l_len - 1 - equal_atoms_end],
434 &r[r_len - 1 - equal_atoms_end]);
442 /* Add a chunk of equal lines at the start */
443 if (equal_atoms_start) {
444 if (!diff_state_add_chunk(state, true,
445 l, equal_atoms_start,
446 r, equal_atoms_start))
448 l_idx += equal_atoms_start;
449 r_idx += equal_atoms_start;
452 /* Add a "minus" chunk with all lines from the left. */
453 if (equal_atoms_start + equal_atoms_end < l_len) {
454 unsigned int add_len = l_len - equal_atoms_start - equal_atoms_end;
455 if (!diff_state_add_chunk(state, true,
462 /* Add a "plus" chunk with all lines from the right. */
463 if (equal_atoms_start + equal_atoms_end < r_len) {
464 unsigned int add_len = r_len - equal_atoms_start - equal_atoms_end;
465 if (!diff_state_add_chunk(state, true,
472 /* Add a chunk of equal lines at the end */
473 if (equal_atoms_end) {
474 if (!diff_state_add_chunk(state, true,
475 &l[l_idx], equal_atoms_end,
476 &r[r_idx], equal_atoms_end))
484 diff_run_algo(const struct diff_algo_config *algo_config,
485 struct diff_state *state)
489 if (!algo_config || !algo_config->impl
490 || !state->recursion_depth_left
491 || !state->left.atoms.len || !state->right.atoms.len) {
492 debug("Fall back to diff_algo_none():%s%s%s\n",
493 (!algo_config || !algo_config->impl) ? " no-cfg" : "",
494 (!state->recursion_depth_left) ? " max-depth" : "",
495 (!state->left.atoms.len || !state->right.atoms.len)?
497 return diff_algo_none(algo_config, state);
500 ARRAYLIST_FREE(state->temp_result);
501 ARRAYLIST_INIT(state->temp_result, DIFF_RESULT_ALLOC_BLOCKSIZE);
502 rc = algo_config->impl(algo_config, state);
504 case DIFF_RC_USE_DIFF_ALGO_FALLBACK:
505 debug("Got DIFF_RC_USE_DIFF_ALGO_FALLBACK (%p)\n",
506 algo_config->fallback_algo);
507 rc = diff_run_algo(algo_config->fallback_algo, state);
515 /* some error happened */
519 /* Pick up any diff chunks that are still unsolved and feed to
520 * inner_algo. inner_algo will solve unsolved chunks and append to
521 * result, and subsequent solved chunks on this level are then appended
522 * to result afterwards. */
524 for (i = 0; i < state->temp_result.len; i++) {
525 struct diff_chunk *c = &state->temp_result.head[i];
527 diff_state_add_solved_chunk(state, c);
531 /* c is an unsolved chunk, feed to inner_algo */
532 struct diff_state inner_state = {
533 .result = state->result,
534 .recursion_depth_left = state->recursion_depth_left - 1,
535 .kd_buf = state->kd_buf,
536 .kd_buf_size = state->kd_buf_size,
538 diff_data_init_subsection(&inner_state.left, &state->left,
539 c->left_start, c->left_count);
540 diff_data_init_subsection(&inner_state.right, &state->right,
541 c->right_start, c->right_count);
543 rc = diff_run_algo(algo_config->inner_algo, &inner_state);
544 state->kd_buf = inner_state.kd_buf;
545 state->kd_buf_size = inner_state.kd_buf_size;
546 if (rc != DIFF_RC_OK)
552 ARRAYLIST_FREE(state->temp_result);
557 diff_atomize_file(struct diff_data *d,
558 const struct diff_config *config,
559 FILE *f, const uint8_t *data, off_t len, int diff_flags)
561 if (!config->atomize_func)
564 diff_data_init_root(d, f, data, len, diff_flags);
566 return config->atomize_func(config->atomize_func_data, d);
571 diff_main(const struct diff_config *config, struct diff_data *left,
572 struct diff_data *right)
574 struct diff_result *result = malloc(sizeof(struct diff_result));
578 *result = (struct diff_result){};
580 result->right = right;
582 struct diff_state state = {
584 .recursion_depth_left = config->max_recursion_depth ? : 32,
588 diff_data_init_subsection(&state.left, left,
591 diff_data_init_subsection(&state.right, right,
595 result->rc = diff_run_algo(config->algo, &state);
602 diff_result_free(struct diff_result *result)
606 ARRAYLIST_FREE(result->chunks);