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
54 #define DIFF_FLAG_SHOW_PROTOTYPES 0x00000002
56 void diff_data_free(struct diff_data *diff_data);
58 struct diff_chunk;
59 typedef ARRAYLIST(struct diff_chunk) diff_chunk_arraylist_t;
61 struct diff_result {
62 int rc;
64 /*
65 * Pointers to diff data passed in via diff_main.
66 * Do not free these diff_data before freeing the diff_result struct.
67 */
68 struct diff_data *left;
69 struct diff_data *right;
71 diff_chunk_arraylist_t chunks;
72 };
74 struct diff_state;
76 /* Signature of a utility function to divide a file into diff atoms.
77 * An example is diff_atomize_text_by_line() in diff_atomize_text.c.
78 *
79 * func_data: context pointer (free to be used by implementation).
80 * d: struct diff_data with d->data and d->len already set up, and
81 * d->atoms to be created.
82 */
83 typedef int (*diff_atomize_func_t)(void *func_data, struct diff_data *d);
85 extern int diff_atomize_text_by_line(void *func_data, struct diff_data *d);
87 struct diff_algo_config;
88 typedef int (*diff_algo_impl_t)(
89 const struct diff_algo_config *algo_config, struct diff_state *state);
91 /* Form a result with all left-side removed and all right-side added, i.e. no
92 * actual diff algorithm involved. */
93 int diff_algo_none(const struct diff_algo_config *algo_config,
94 struct diff_state *state);
96 /* Myers Diff tracing from the start all the way through to the end, requiring
97 * quadratic amounts of memory. This can fail if the required space surpasses
98 * algo_config->permitted_state_size. */
99 extern int diff_algo_myers(const struct diff_algo_config *algo_config,
100 struct diff_state *state);
102 /* Myers "Divide et Impera": tracing forwards from the start and backwards from
103 * the end to find a midpoint that divides the problem into smaller chunks.
104 * Requires only linear amounts of memory. */
105 extern int diff_algo_myers_divide(
106 const struct diff_algo_config *algo_config, struct diff_state *state);
108 /* Patience Diff algorithm, which divides a larger diff into smaller chunks. For
109 * very specific scenarios, it may lead to a complete diff result by itself, but
110 * needs a fallback algo to solve chunks that don't have common-unique atoms. */
111 extern int diff_algo_patience(
112 const struct diff_algo_config *algo_config, struct diff_state *state);
114 /* Diff algorithms to use, possibly nested. For example:
116 * struct diff_algo_config myers, patience, myers_divide;
118 * myers = (struct diff_algo_config){
119 * .impl = diff_algo_myers,
120 * .permitted_state_size = 32 * 1024 * 1024,
121 * // When too large, do diff_algo_patience:
122 * .fallback_algo = &patience,
123 * };
125 * const struct diff_algo_config patience = (struct diff_algo_config){
126 * .impl = diff_algo_patience,
127 * // After subdivision, do Patience again:
128 * .inner_algo = &patience,
129 * // If subdivision failed, do Myers Divide et Impera:
130 * .fallback_algo = &myers_then_myers_divide,
131 * };
133 * const struct diff_algo_config myers_divide = (struct diff_algo_config){
134 * .impl = diff_algo_myers_divide,
135 * // When division succeeded, start from the top:
136 * .inner_algo = &myers_then_myers_divide,
137 * // (fallback_algo = NULL implies diff_algo_none).
138 * };
139 * struct diff_config config = {
140 * .algo = &myers,
141 * ...
142 * };
143 * diff_main(&config, ...);
144 */
145 struct diff_algo_config {
146 diff_algo_impl_t impl;
148 /* Fail this algo if it would use more than this amount of memory, and
149 * instead use fallback_algo (diff_algo_myers). permitted_state_size ==
150 * 0 means no limitation. */
151 size_t permitted_state_size;
153 /* For algorithms that divide into smaller chunks, use this algorithm to
154 * solve the divided chunks. */
155 const struct diff_algo_config *inner_algo;
157 /* If the algorithm fails (e.g. diff_algo_myers_if_small needs too large
158 * state, or diff_algo_patience can't find any common-unique atoms),
159 * then use this algorithm instead. */
160 const struct diff_algo_config *fallback_algo;
161 };
163 struct diff_config {
164 diff_atomize_func_t atomize_func;
165 void *atomize_func_data;
167 const struct diff_algo_config *algo;
169 /* How deep to step into subdivisions of a source file, a paranoia /
170 * safety measure to guard against infinite loops through diff
171 * algorithms. When the maximum recursion is reached, employ
172 * diff_algo_none (i.e. remove all left atoms and add all right atoms).
173 */
174 unsigned int max_recursion_depth;
175 };
177 int diff_atomize_file(struct diff_data *d, const struct diff_config *config,
178 FILE *f, const uint8_t *data, off_t len, int diff_flags);
179 struct diff_result *diff_main(const struct diff_config *config,
180 struct diff_data *left,
181 struct diff_data *right);
182 void diff_result_free(struct diff_result *result);