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;
46 int diff_flags;
47 };
49 #define DIFF_FLAG_IGNORE_WHITESPACE 0x00000001
51 void diff_data_free(struct diff_data *diff_data);
53 struct diff_chunk;
54 typedef ARRAYLIST(struct diff_chunk) diff_chunk_arraylist_t;
56 struct diff_result {
57 int rc;
58 struct diff_data left;
59 struct diff_data right;
60 diff_chunk_arraylist_t chunks;
61 };
63 struct diff_state;
65 /* Signature of a utility function to divide both source files into diff atoms.
66 * It is possible that a (future) algorithm requires both source files to decide
67 * on atom split points, hence this gets both left and right to atomize at the
68 * same time.
69 * An example is diff_atomize_text_by_line() in diff_atomize_text.c.
70 *
71 * func_data: context pointer (free to be used by implementation).
72 * left: struct diff_data with left->data and left->len already set up, and
73 * left->atoms to be created.
74 * right: struct diff_data with right->data and right->len already set up, and
75 * right->atoms to be created.
76 */
77 typedef int (*diff_atomize_func_t)(void *func_data,
78 struct diff_data *left,
79 struct diff_data *right);
81 extern int diff_atomize_text_by_line(void *func_data,
82 struct diff_data *left,
83 struct diff_data *right);
85 struct diff_algo_config;
86 typedef int (*diff_algo_impl_t)(
87 const struct diff_algo_config *algo_config, struct diff_state *state);
89 /* Form a result with all left-side removed and all right-side added, i.e. no
90 * actual diff algorithm involved. */
91 int diff_algo_none(const struct diff_algo_config *algo_config,
92 struct diff_state *state);
94 /* Myers Diff tracing from the start all the way through to the end, requiring
95 * quadratic amounts of memory. This can fail if the required space surpasses
96 * algo_config->permitted_state_size. */
97 extern int diff_algo_myers(const struct diff_algo_config *algo_config,
98 struct diff_state *state);
100 /* Myers "Divide et Impera": tracing forwards from the start and backwards from
101 * the end to find a midpoint that divides the problem into smaller chunks.
102 * Requires only linear amounts of memory. */
103 extern int diff_algo_myers_divide(
104 const struct diff_algo_config *algo_config, struct diff_state *state);
106 /* Patience Diff algorithm, which divides a larger diff into smaller chunks. For
107 * very specific scenarios, it may lead to a complete diff result by itself, but
108 * needs a fallback algo to solve chunks that don't have common-unique atoms. */
109 extern int diff_algo_patience(
110 const struct diff_algo_config *algo_config, struct diff_state *state);
112 /* Diff algorithms to use, possibly nested. For example:
114 * struct diff_algo_config myers, patience, myers_divide;
116 * myers = (struct diff_algo_config){
117 * .impl = diff_algo_myers,
118 * .permitted_state_size = 32 * 1024 * 1024,
119 * // When too large, do diff_algo_patience:
120 * .fallback_algo = &patience,
121 * };
123 * const struct diff_algo_config patience = (struct diff_algo_config){
124 * .impl = diff_algo_patience,
125 * // After subdivision, do Patience again:
126 * .inner_algo = &patience,
127 * // If subdivision failed, do Myers Divide et Impera:
128 * .fallback_algo = &myers_then_myers_divide,
129 * };
131 * const struct diff_algo_config myers_divide = (struct diff_algo_config){
132 * .impl = diff_algo_myers_divide,
133 * // When division succeeded, start from the top:
134 * .inner_algo = &myers_then_myers_divide,
135 * // (fallback_algo = NULL implies diff_algo_none).
136 * };
137 * struct diff_config config = {
138 * .algo = &myers,
139 * ...
140 * };
141 * diff_main(&config, ...);
142 */
143 struct diff_algo_config {
144 diff_algo_impl_t impl;
146 /* Fail this algo if it would use more than this amount of memory, and
147 * instead use fallback_algo (diff_algo_myers). permitted_state_size ==
148 * 0 means no limitation. */
149 size_t permitted_state_size;
151 /* For algorithms that divide into smaller chunks, use this algorithm to
152 * solve the divided chunks. */
153 const struct diff_algo_config *inner_algo;
155 /* If the algorithm fails (e.g. diff_algo_myers_if_small needs too large
156 * state, or diff_algo_patience can't find any common-unique atoms),
157 * then use this algorithm instead. */
158 const struct diff_algo_config *fallback_algo;
159 };
161 struct diff_config {
162 diff_atomize_func_t atomize_func;
163 void *atomize_func_data;
165 const struct diff_algo_config *algo;
167 /* How deep to step into subdivisions of a source file, a paranoia /
168 * safety measure to guard against infinite loops through diff
169 * algorithms. When the maximum recursion is reached, employ
170 * diff_algo_none (i.e. remove all left atoms and add all right atoms).
171 */
172 unsigned int max_recursion_depth;
173 };
175 struct diff_result *diff_main(const struct diff_config *config,
176 FILE *left_f, const uint8_t *left_data,
177 off_t left_len,
178 FILE *right_f, const uint8_t *right_data,
179 off_t right_len, int diff_flags);
180 void diff_result_free(struct diff_result *result);