1 3b0f3d61 2020-01-22 neels /* Generic infrastructure to implement various diff algorithms. */
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
18 3b0f3d61 2020-01-22 neels #pragma once
20 3b0f3d61 2020-01-22 neels #include <stdint.h>
21 3b0f3d61 2020-01-22 neels #include <stdlib.h>
22 3b0f3d61 2020-01-22 neels #include <stdbool.h>
23 3b0f3d61 2020-01-22 neels #include <string.h>
24 3b0f3d61 2020-01-22 neels #include <errno.h>
26 3b0f3d61 2020-01-22 neels #include <diff/arraylist.h>
28 54fa8228 2020-01-27 neels #ifndef MAX
29 54fa8228 2020-01-27 neels #define MAX(A,B) ((A)>(B)?(A):(B))
31 54fa8228 2020-01-27 neels #ifndef MIN
32 54fa8228 2020-01-27 neels #define MIN(A,B) ((A)<(B)?(A):(B))
35 54fa8228 2020-01-27 neels struct range {
40 54fa8228 2020-01-27 neels static inline bool range_empty(const struct range *r)
42 54fa8228 2020-01-27 neels return r->start == r->end;
45 54fa8228 2020-01-27 neels static inline bool ranges_touch(const struct range *a, const struct range *b)
47 54fa8228 2020-01-27 neels return (a->end >= b->start) && (a->start <= b->end);
50 54fa8228 2020-01-27 neels static inline void ranges_merge(struct range *a, const struct range *b)
52 54fa8228 2020-01-27 neels *a = (struct range){
53 54fa8228 2020-01-27 neels .start = MIN(a->start, b->start),
54 54fa8228 2020-01-27 neels .end = MAX(a->end, b->end),
58 54fa8228 2020-01-27 neels static inline int range_len(const struct range *r)
62 54fa8228 2020-01-27 neels return r->end - r->start;
65 3b0f3d61 2020-01-22 neels /* List of all possible return codes of a diff invocation. */
66 3b0f3d61 2020-01-22 neels enum diff_rc {
67 3b0f3d61 2020-01-22 neels DIFF_RC_USE_DIFF_ALGO_FALLBACK = -1,
68 3b0f3d61 2020-01-22 neels DIFF_RC_OK = 0,
69 3b0f3d61 2020-01-22 neels DIFF_RC_ENOTSUP = ENOTSUP,
70 3b0f3d61 2020-01-22 neels DIFF_RC_ENOMEM = ENOMEM,
71 3b0f3d61 2020-01-22 neels DIFF_RC_EINVAL = EINVAL,
74 3b0f3d61 2020-01-22 neels struct diff_atom {
75 3b0f3d61 2020-01-22 neels const uint8_t *at;
76 3b0f3d61 2020-01-22 neels size_t len;
78 3b0f3d61 2020-01-22 neels /* This hash is just a very cheap speed up for finding *mismatching* atoms. When hashes match, we still need to
79 3b0f3d61 2020-01-22 neels * compare entire atoms to find out whether they are indeed identical or not. */
80 3b0f3d61 2020-01-22 neels unsigned int hash;
82 3b0f3d61 2020-01-22 neels /* State for the Patience Diff algorithm */
83 3b0f3d61 2020-01-22 neels /* TODO: keep a separate array for the patience state */
85 3b0f3d61 2020-01-22 neels bool unique_here;
86 3b0f3d61 2020-01-22 neels bool unique_in_both;
87 3b0f3d61 2020-01-22 neels struct diff_atom *pos_in_other;
88 3b0f3d61 2020-01-22 neels struct diff_atom *prev_stack;
89 bb7fb738 2020-01-27 neels struct range identical_lines;
90 3b0f3d61 2020-01-22 neels } patience;
93 3b0f3d61 2020-01-22 neels static inline bool diff_atom_same(const struct diff_atom *left, const struct diff_atom *right)
95 3b0f3d61 2020-01-22 neels return left->hash == right->hash
96 3b0f3d61 2020-01-22 neels && left->len == right->len
97 3b0f3d61 2020-01-22 neels && memcmp(left->at, right->at, left->len) == 0;
100 3b0f3d61 2020-01-22 neels /* For each file, there is a "root" struct diff_data referencing the entire file, which the atoms are parsed from. In
101 3b0f3d61 2020-01-22 neels * recursion of diff algorithm, there may be "child" struct diff_data only referencing a subsection of the file,
102 3b0f3d61 2020-01-22 neels * re-using the atoms parsing. For "root" structs, atoms_allocated will be nonzero, indicating that the array of atoms
103 3b0f3d61 2020-01-22 neels * is owned by that struct. For "child" structs, atoms_allocated == 0, to indicate that the struct is referencing a
104 3b0f3d61 2020-01-22 neels * subset of atoms. */
105 3b0f3d61 2020-01-22 neels struct diff_data {
106 3b0f3d61 2020-01-22 neels const uint8_t *data;
107 3b0f3d61 2020-01-22 neels size_t len;
108 3b0f3d61 2020-01-22 neels ARRAYLIST(struct diff_atom) atoms;
109 3b0f3d61 2020-01-22 neels struct diff_data *root;
112 3b0f3d61 2020-01-22 neels void diff_data_free(struct diff_data *diff_data);
114 3b0f3d61 2020-01-22 neels /* The atom's index in the entire file. For atoms divided by lines of text, this yields the line number (starting with
115 3b0f3d61 2020-01-22 neels * 0). Also works for diff_data that reference only a subsection of a file, always reflecting the global position in
116 3b0f3d61 2020-01-22 neels * the file (and not the relative position within the subsection). */
117 3b0f3d61 2020-01-22 neels #define diff_atom_root_idx(DIFF_DATA, ATOM) \
118 15ac50e2 2020-05-05 neels ((ATOM) && ((ATOM) >= (DIFF_DATA)->root->atoms.head) \
119 15ac50e2 2020-05-05 neels ? (unsigned int)((ATOM) - ((DIFF_DATA)->root->atoms.head)) \
120 15ac50e2 2020-05-05 neels : (DIFF_DATA)->root->atoms.len)
122 3b0f3d61 2020-01-22 neels /* The atom's index within DIFF_DATA. For atoms divided by lines of text, this yields the line number (starting with
124 3b0f3d61 2020-01-22 neels #define diff_atom_idx(DIFF_DATA, ATOM) \
125 15ac50e2 2020-05-05 neels ((ATOM) && ((ATOM) >= (DIFF_DATA)->atoms.head) \
126 15ac50e2 2020-05-05 neels ? (unsigned int)((ATOM) - ((DIFF_DATA)->atoms.head)) \
127 15ac50e2 2020-05-05 neels : (DIFF_DATA)->atoms.len)
129 3b0f3d61 2020-01-22 neels #define foreach_diff_atom(ATOM, FIRST_ATOM, COUNT) \
130 3b0f3d61 2020-01-22 neels for ((ATOM) = (FIRST_ATOM); \
131 3b0f3d61 2020-01-22 neels (ATOM) && ((ATOM) >= (FIRST_ATOM)) && ((ATOM) - (FIRST_ATOM) < (COUNT)); \
134 3b0f3d61 2020-01-22 neels #define diff_data_foreach_atom(ATOM, DIFF_DATA) \
135 3b0f3d61 2020-01-22 neels foreach_diff_atom(ATOM, (DIFF_DATA)->atoms.head, (DIFF_DATA)->atoms.len)
137 3b0f3d61 2020-01-22 neels #define diff_data_foreach_atom_from(FROM, ATOM, DIFF_DATA) \
138 3b0f3d61 2020-01-22 neels for ((ATOM) = (FROM); \
139 3b0f3d61 2020-01-22 neels (ATOM) && ((ATOM) >= (DIFF_DATA)->atoms.head) && ((ATOM) - (DIFF_DATA)->atoms.head < (DIFF_DATA)->atoms.len); \
142 3b0f3d61 2020-01-22 neels /* A diff chunk represents a set of atoms on the left and/or a set of atoms on the right.
144 3b0f3d61 2020-01-22 neels * If solved == false:
145 3b0f3d61 2020-01-22 neels * The diff algorithm has divided the source file, and this is a chunk that the inner_algo should run on next.
146 3b0f3d61 2020-01-22 neels * The lines on the left should be diffed against the lines on the right.
147 3b0f3d61 2020-01-22 neels * (If there are no left lines or no right lines, it implies solved == true, because there is nothing to diff.)
149 3b0f3d61 2020-01-22 neels * If solved == true:
150 3b0f3d61 2020-01-22 neels * If there are only left atoms, it is a chunk removing atoms from the left ("a minus chunk").
151 3b0f3d61 2020-01-22 neels * If there are only right atoms, it is a chunk adding atoms from the right ("a plus chunk").
152 3b0f3d61 2020-01-22 neels * If there are both left and right lines, it is a chunk of equal content on both sides,
153 3b0f3d61 2020-01-22 neels * and left_count == right_count:
156 3b0f3d61 2020-01-22 neels * - bar }-- diff_chunk{ left_start = &left.atoms.head[0], left_count = 3,
157 3b0f3d61 2020-01-22 neels * - baz } right_start = NULL, right_count = 0 }
159 3b0f3d61 2020-01-22 neels * goo }-- diff_chunk{ left_start = &left.atoms.head[3], left_count = 3,
160 3b0f3d61 2020-01-22 neels * zoo } right_start = &right.atoms.head[0], right_count = 3 }
162 3b0f3d61 2020-01-22 neels * +roo }-- diff_chunk{ left_start = NULL, left_count = 0,
163 3b0f3d61 2020-01-22 neels * +too } right_start = &right.atoms.head[3], right_count = 3 }
166 3b0f3d61 2020-01-22 neels struct diff_chunk {
167 3b0f3d61 2020-01-22 neels bool solved;
168 3b0f3d61 2020-01-22 neels struct diff_atom *left_start;
169 3b0f3d61 2020-01-22 neels unsigned int left_count;
170 3b0f3d61 2020-01-22 neels struct diff_atom *right_start;
171 3b0f3d61 2020-01-22 neels unsigned int right_count;
174 3b0f3d61 2020-01-22 neels typedef ARRAYLIST(struct diff_chunk) diff_chunk_arraylist_t;
175 3b0f3d61 2020-01-22 neels #define DIFF_RESULT_ALLOC_BLOCKSIZE 128
177 3b0f3d61 2020-01-22 neels struct diff_result {
178 3b0f3d61 2020-01-22 neels enum diff_rc rc;
179 3b0f3d61 2020-01-22 neels struct diff_data left;
180 3b0f3d61 2020-01-22 neels struct diff_data right;
181 3b0f3d61 2020-01-22 neels diff_chunk_arraylist_t chunks;
184 3b0f3d61 2020-01-22 neels struct diff_state {
185 3b0f3d61 2020-01-22 neels /* The final result passed to the original diff caller. */
186 3b0f3d61 2020-01-22 neels struct diff_result *result;
188 3b0f3d61 2020-01-22 neels /* The root diff_data is in result->left,right, these are (possibly) subsections of the root data. */
189 3b0f3d61 2020-01-22 neels struct diff_data left;
190 3b0f3d61 2020-01-22 neels struct diff_data right;
192 3b0f3d61 2020-01-22 neels unsigned int recursion_depth_left;
194 3b0f3d61 2020-01-22 neels /* Remaining chunks from one diff algorithm pass, if any solved == false chunks came up. */
195 3b0f3d61 2020-01-22 neels diff_chunk_arraylist_t temp_result;
198 3b0f3d61 2020-01-22 neels struct diff_chunk *diff_state_add_chunk(struct diff_state *state, bool solved,
199 3b0f3d61 2020-01-22 neels struct diff_atom *left_start, unsigned int left_count,
200 3b0f3d61 2020-01-22 neels struct diff_atom *right_start, unsigned int right_count);
202 3b0f3d61 2020-01-22 neels /* Signature of a utility function to divide both source files into diff atoms.
203 3b0f3d61 2020-01-22 neels * It is possible that a (future) algorithm requires both source files to decide on atom split points, hence this gets
204 3b0f3d61 2020-01-22 neels * both left and right to atomize at the same time.
205 3b0f3d61 2020-01-22 neels * An example is diff_atomize_text_by_line() in diff_atomize_text.c.
207 3b0f3d61 2020-01-22 neels * func_data: context pointer (free to be used by implementation).
208 3b0f3d61 2020-01-22 neels * left: struct diff_data with left->data and left->len already set up, and left->atoms to be created.
209 3b0f3d61 2020-01-22 neels * right: struct diff_data with right->data and right->len already set up, and right->atoms to be created.
211 3b0f3d61 2020-01-22 neels typedef enum diff_rc (*diff_atomize_func_t)(void *func_data, struct diff_data *left, struct diff_data *right);
213 3b0f3d61 2020-01-22 neels extern enum diff_rc diff_atomize_text_by_line(void *func_data, struct diff_data *left, struct diff_data *right);
215 3b0f3d61 2020-01-22 neels struct diff_algo_config;
216 3b0f3d61 2020-01-22 neels typedef enum diff_rc (*diff_algo_impl_t)(const struct diff_algo_config *algo_config, struct diff_state *state);
218 3b0f3d61 2020-01-22 neels /* Form a result with all left-side removed and all right-side added, i.e. no actual diff algorithm involved. */
219 3b0f3d61 2020-01-22 neels enum diff_rc diff_algo_none(const struct diff_algo_config *algo_config, struct diff_state *state);
221 3b0f3d61 2020-01-22 neels /* Myers Diff tracing from the start all the way through to the end, requiring quadratic amounts of memory. This can
222 3b0f3d61 2020-01-22 neels * fail if the required space surpasses algo_config->permitted_state_size. */
223 3b0f3d61 2020-01-22 neels extern enum diff_rc diff_algo_myers(const struct diff_algo_config *algo_config, struct diff_state *state);
225 3b0f3d61 2020-01-22 neels /* Myers "Divide et Impera": tracing forwards from the start and backwards from the end to find a midpoint that divides
226 3b0f3d61 2020-01-22 neels * the problem into smaller chunks. Requires only linear amounts of memory. */
227 3b0f3d61 2020-01-22 neels extern enum diff_rc diff_algo_myers_divide(const struct diff_algo_config *algo_config, struct diff_state *state);
229 3b0f3d61 2020-01-22 neels /* Patience Diff algorithm, which divides a larger diff into smaller chunks. For very specific scenarios, it may lead to
230 3b0f3d61 2020-01-22 neels * a complete diff result by itself, but needs a fallback algo to solve chunks that don't have common-unique atoms. */
231 3b0f3d61 2020-01-22 neels extern enum diff_rc diff_algo_patience(const struct diff_algo_config *algo_config, struct diff_state *state);
233 3b0f3d61 2020-01-22 neels /* Diff algorithms to use, possibly nested. For example:
235 3b0f3d61 2020-01-22 neels * struct diff_algo_config myers, patience, myers_divide;
237 3b0f3d61 2020-01-22 neels * myers = (struct diff_algo_config){
238 3b0f3d61 2020-01-22 neels * .impl = diff_algo_myers,
239 3b0f3d61 2020-01-22 neels * .permitted_state_size = 32 * 1024 * 1024,
240 3b0f3d61 2020-01-22 neels * .fallback_algo = &patience, // when too large, do diff_algo_patience
243 3b0f3d61 2020-01-22 neels * patience = (struct diff_algo_config){
244 3b0f3d61 2020-01-22 neels * .impl = diff_algo_patience,
245 3b0f3d61 2020-01-22 neels * .inner_algo = &patience, // After subdivision, do Patience again.
246 3b0f3d61 2020-01-22 neels * .fallback_algo = &myers_divide, // If subdivision failed, do Myers Divide et Impera.
249 3b0f3d61 2020-01-22 neels * myers_divide = (struct diff_algo_config){
250 3b0f3d61 2020-01-22 neels * .impl = diff_algo_myers_divide,
251 3b0f3d61 2020-01-22 neels * .inner_algo = &myers, // When division succeeded, start from the top.
252 3b0f3d61 2020-01-22 neels * // (fallback_algo = NULL implies diff_algo_none).
255 3b0f3d61 2020-01-22 neels * struct diff_config config = {
256 3b0f3d61 2020-01-22 neels * .algo = &myers,
259 3b0f3d61 2020-01-22 neels * diff_main(&config, ...);
261 3b0f3d61 2020-01-22 neels struct diff_algo_config {
262 3b0f3d61 2020-01-22 neels diff_algo_impl_t impl;
264 3b0f3d61 2020-01-22 neels /* Fail this algo if it would use more than this amount of memory, and instead use fallback_algo
265 3b0f3d61 2020-01-22 neels * (diff_algo_myers). permitted_state_size == 0 means no limitation. */
266 3b0f3d61 2020-01-22 neels size_t permitted_state_size;
268 3b0f3d61 2020-01-22 neels /* For algorithms that divide into smaller chunks, use this algorithm to solve the divided chunks. */
269 3b0f3d61 2020-01-22 neels const struct diff_algo_config *inner_algo;
271 3b0f3d61 2020-01-22 neels /* If the algorithm fails (e.g. diff_algo_myers_if_small needs too large state, or diff_algo_patience can't find
272 3b0f3d61 2020-01-22 neels * any common-unique atoms), then use this algorithm instead. */
273 3b0f3d61 2020-01-22 neels const struct diff_algo_config *fallback_algo;
276 3b0f3d61 2020-01-22 neels struct diff_config {
277 3b0f3d61 2020-01-22 neels diff_atomize_func_t atomize_func;
278 3b0f3d61 2020-01-22 neels void *atomize_func_data;
280 3b0f3d61 2020-01-22 neels const struct diff_algo_config *algo;
282 3b0f3d61 2020-01-22 neels /* How deep to step into subdivisions of a source file, a paranoia / safety measure to guard against infinite
283 3b0f3d61 2020-01-22 neels * loops through diff algorithms. When the maximum recursion is reached, employ diff_algo_none (i.e. remove all
284 3b0f3d61 2020-01-22 neels * left atoms and add all right atoms). */
285 3b0f3d61 2020-01-22 neels unsigned int max_recursion_depth;
288 3b0f3d61 2020-01-22 neels struct diff_result *diff_main(const struct diff_config *config,
289 3b0f3d61 2020-01-22 neels const uint8_t *left_data, size_t left_len,
290 3b0f3d61 2020-01-22 neels const uint8_t *right_data, size_t right_len);
291 3b0f3d61 2020-01-22 neels void diff_result_free(struct diff_result *result);