Blob


1 /* Generic infrastructure to implement various diff algorithms. */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
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.
8 *
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.
16 */
18 struct diff_range {
19 int start;
20 int end;
21 };
23 /* List of all possible return codes of a diff invocation. */
24 #define DIFF_RC_USE_DIFF_ALGO_FALLBACK -1
25 #define DIFF_RC_OK 0
26 /* Any positive return values are errno values from sys/errno.h */
28 struct diff_atom;
30 /* For each file, there is a "root" struct diff_data referencing the entire
31 * file, which the atoms are parsed from. In recursion of diff algorithm, there
32 * may be "child" struct diff_data only referencing a subsection of the file,
33 * re-using the atoms parsing. For "root" structs, atoms_allocated will be
34 * nonzero, indicating that the array of atoms is owned by that struct. For
35 * "child" structs, atoms_allocated == 0, to indicate that the struct is
36 * referencing a subset of atoms. */
37 struct diff_data {
38 FILE *f; /* if root diff_data and not memory-mapped */
39 off_t pos; /* if not memory-mapped */
40 const uint8_t *data; /* if memory-mapped */
41 off_t len;
43 ARRAYLIST(struct diff_atom) atoms;
44 struct diff_data *root;
45 struct diff_data *current;
46 void *algo_data;
48 int diff_flags;
50 int err;
51 };
53 #define DIFF_FLAG_IGNORE_WHITESPACE 0x00000001
55 void diff_data_free(struct diff_data *diff_data);
57 struct diff_chunk;
58 typedef ARRAYLIST(struct diff_chunk) diff_chunk_arraylist_t;
60 struct diff_result {
61 int rc;
62 struct diff_data left;
63 struct diff_data right;
64 diff_chunk_arraylist_t chunks;
65 };
67 struct diff_state;
69 /* Signature of a utility function to divide both source files into diff atoms.
70 * It is possible that a (future) algorithm requires both source files to decide
71 * on atom split points, hence this gets both left and right to atomize at the
72 * same time.
73 * An example is diff_atomize_text_by_line() in diff_atomize_text.c.
74 *
75 * func_data: context pointer (free to be used by implementation).
76 * left: struct diff_data with left->data and left->len already set up, and
77 * left->atoms to be created.
78 * right: struct diff_data with right->data and right->len already set up, and
79 * right->atoms to be created.
80 */
81 typedef int (*diff_atomize_func_t)(void *func_data,
82 struct diff_data *left,
83 struct diff_data *right);
85 extern int diff_atomize_text_by_line(void *func_data,
86 struct diff_data *left,
87 struct diff_data *right);
89 struct diff_algo_config;
90 typedef int (*diff_algo_impl_t)(
91 const struct diff_algo_config *algo_config, struct diff_state *state);
93 /* Form a result with all left-side removed and all right-side added, i.e. no
94 * actual diff algorithm involved. */
95 int diff_algo_none(const struct diff_algo_config *algo_config,
96 struct diff_state *state);
98 /* Myers Diff tracing from the start all the way through to the end, requiring
99 * quadratic amounts of memory. This can fail if the required space surpasses
100 * algo_config->permitted_state_size. */
101 extern int diff_algo_myers(const struct diff_algo_config *algo_config,
102 struct diff_state *state);
104 /* Myers "Divide et Impera": tracing forwards from the start and backwards from
105 * the end to find a midpoint that divides the problem into smaller chunks.
106 * Requires only linear amounts of memory. */
107 extern int diff_algo_myers_divide(
108 const struct diff_algo_config *algo_config, struct diff_state *state);
110 /* Patience Diff algorithm, which divides a larger diff into smaller chunks. For
111 * very specific scenarios, it may lead to a complete diff result by itself, but
112 * needs a fallback algo to solve chunks that don't have common-unique atoms. */
113 extern int diff_algo_patience(
114 const struct diff_algo_config *algo_config, struct diff_state *state);
116 /* Diff algorithms to use, possibly nested. For example:
118 * struct diff_algo_config myers, patience, myers_divide;
120 * myers = (struct diff_algo_config){
121 * .impl = diff_algo_myers,
122 * .permitted_state_size = 32 * 1024 * 1024,
123 * // When too large, do diff_algo_patience:
124 * .fallback_algo = &patience,
125 * };
127 * const struct diff_algo_config patience = (struct diff_algo_config){
128 * .impl = diff_algo_patience,
129 * // After subdivision, do Patience again:
130 * .inner_algo = &patience,
131 * // If subdivision failed, do Myers Divide et Impera:
132 * .fallback_algo = &myers_then_myers_divide,
133 * };
135 * const struct diff_algo_config myers_divide = (struct diff_algo_config){
136 * .impl = diff_algo_myers_divide,
137 * // When division succeeded, start from the top:
138 * .inner_algo = &myers_then_myers_divide,
139 * // (fallback_algo = NULL implies diff_algo_none).
140 * };
141 * struct diff_config config = {
142 * .algo = &myers,
143 * ...
144 * };
145 * diff_main(&config, ...);
146 */
147 struct diff_algo_config {
148 diff_algo_impl_t impl;
150 /* Fail this algo if it would use more than this amount of memory, and
151 * instead use fallback_algo (diff_algo_myers). permitted_state_size ==
152 * 0 means no limitation. */
153 size_t permitted_state_size;
155 /* For algorithms that divide into smaller chunks, use this algorithm to
156 * solve the divided chunks. */
157 const struct diff_algo_config *inner_algo;
159 /* If the algorithm fails (e.g. diff_algo_myers_if_small needs too large
160 * state, or diff_algo_patience can't find any common-unique atoms),
161 * then use this algorithm instead. */
162 const struct diff_algo_config *fallback_algo;
163 };
165 struct diff_config {
166 diff_atomize_func_t atomize_func;
167 void *atomize_func_data;
169 const struct diff_algo_config *algo;
171 /* How deep to step into subdivisions of a source file, a paranoia /
172 * safety measure to guard against infinite loops through diff
173 * algorithms. When the maximum recursion is reached, employ
174 * diff_algo_none (i.e. remove all left atoms and add all right atoms).
175 */
176 unsigned int max_recursion_depth;
177 };
179 struct diff_result *diff_main(const struct diff_config *config,
180 FILE *left_f, const uint8_t *left_data,
181 off_t left_len,
182 FILE *right_f, const uint8_t *right_data,
183 off_t right_len, int diff_flags);
184 void diff_result_free(struct diff_result *result);