Blame


1 fe621944 2020-11-10 stsp /* Generic infrastructure to implement various diff algorithms. */
2 fe621944 2020-11-10 stsp /*
3 fe621944 2020-11-10 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 fe621944 2020-11-10 stsp *
5 fe621944 2020-11-10 stsp * Permission to use, copy, modify, and distribute this software for any
6 fe621944 2020-11-10 stsp * purpose with or without fee is hereby granted, provided that the above
7 fe621944 2020-11-10 stsp * copyright notice and this permission notice appear in all copies.
8 fe621944 2020-11-10 stsp *
9 fe621944 2020-11-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 fe621944 2020-11-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 fe621944 2020-11-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 fe621944 2020-11-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 fe621944 2020-11-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 fe621944 2020-11-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 fe621944 2020-11-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 fe621944 2020-11-10 stsp */
17 fe621944 2020-11-10 stsp
18 fe621944 2020-11-10 stsp struct diff_range {
19 fe621944 2020-11-10 stsp int start;
20 fe621944 2020-11-10 stsp int end;
21 fe621944 2020-11-10 stsp };
22 fe621944 2020-11-10 stsp
23 fe621944 2020-11-10 stsp /* List of all possible return codes of a diff invocation. */
24 fe621944 2020-11-10 stsp #define DIFF_RC_USE_DIFF_ALGO_FALLBACK -1
25 fe621944 2020-11-10 stsp #define DIFF_RC_OK 0
26 fe621944 2020-11-10 stsp /* Any positive return values are errno values from sys/errno.h */
27 fe621944 2020-11-10 stsp
28 dea26038 2020-11-18 stsp struct diff_atom {
29 dea26038 2020-11-18 stsp struct diff_data *root; /* back pointer to root diff data */
30 fe621944 2020-11-10 stsp
31 c251eeea 2023-02-20 mark off_t pos; /* set whether memory-mapped or not */
32 c251eeea 2023-02-20 mark const uint8_t *at; /* only set if memory-mapped */
33 dea26038 2020-11-18 stsp off_t len;
34 dea26038 2020-11-18 stsp
35 dea26038 2020-11-18 stsp /* This hash is just a very cheap speed up for finding *mismatching*
36 dea26038 2020-11-18 stsp * atoms. When hashes match, we still need to compare entire atoms to
37 dea26038 2020-11-18 stsp * find out whether they are indeed identical or not.
38 dea26038 2020-11-18 stsp * Calculated over all atom bytes with diff_atom_hash_update(). */
39 dea26038 2020-11-18 stsp unsigned int hash;
40 dea26038 2020-11-18 stsp };
41 dea26038 2020-11-18 stsp
42 f8c2e76a 2022-08-03 stsp /* Mix another atom_byte into the provided hash value and return the result.
43 f8c2e76a 2022-08-03 stsp * The hash value passed in for the first byte of the atom must be zero. */
44 f8c2e76a 2022-08-03 stsp unsigned int
45 f8c2e76a 2022-08-03 stsp diff_atom_hash_update(unsigned int hash, unsigned char atom_byte);
46 f8c2e76a 2022-08-03 stsp
47 dea26038 2020-11-18 stsp /* Compare two atoms for equality. Return 0 on success, or errno on failure.
48 dea26038 2020-11-18 stsp * Set cmp to -1, 0, or 1, just like strcmp(). */
49 dea26038 2020-11-18 stsp int
50 dea26038 2020-11-18 stsp diff_atom_cmp(int *cmp,
51 dea26038 2020-11-18 stsp const struct diff_atom *left,
52 dea26038 2020-11-18 stsp const struct diff_atom *right);
53 dea26038 2020-11-18 stsp
54 dea26038 2020-11-18 stsp
55 dea26038 2020-11-18 stsp /* The atom's index in the entire file. For atoms divided by lines of text, this
56 dea26038 2020-11-18 stsp * yields the line number (starting with 0). Also works for diff_data that
57 dea26038 2020-11-18 stsp * reference only a subsection of a file, always reflecting the global position
58 dea26038 2020-11-18 stsp * in the file (and not the relative position within the subsection). */
59 dea26038 2020-11-18 stsp #define diff_atom_root_idx(DIFF_DATA, ATOM) \
60 dea26038 2020-11-18 stsp ((ATOM) && ((ATOM) >= (DIFF_DATA)->root->atoms.head) \
61 dea26038 2020-11-18 stsp ? (unsigned int)((ATOM) - ((DIFF_DATA)->root->atoms.head)) \
62 dea26038 2020-11-18 stsp : (DIFF_DATA)->root->atoms.len)
63 dea26038 2020-11-18 stsp
64 dea26038 2020-11-18 stsp /* The atom's index within DIFF_DATA. For atoms divided by lines of text, this
65 dea26038 2020-11-18 stsp * yields the line number (starting with 0). */
66 dea26038 2020-11-18 stsp #define diff_atom_idx(DIFF_DATA, ATOM) \
67 dea26038 2020-11-18 stsp ((ATOM) && ((ATOM) >= (DIFF_DATA)->atoms.head) \
68 dea26038 2020-11-18 stsp ? (unsigned int)((ATOM) - ((DIFF_DATA)->atoms.head)) \
69 dea26038 2020-11-18 stsp : (DIFF_DATA)->atoms.len)
70 dea26038 2020-11-18 stsp
71 dea26038 2020-11-18 stsp #define foreach_diff_atom(ATOM, FIRST_ATOM, COUNT) \
72 dea26038 2020-11-18 stsp for ((ATOM) = (FIRST_ATOM); \
73 dea26038 2020-11-18 stsp (ATOM) \
74 dea26038 2020-11-18 stsp && ((ATOM) >= (FIRST_ATOM)) \
75 dea26038 2020-11-18 stsp && ((ATOM) - (FIRST_ATOM) < (COUNT)); \
76 dea26038 2020-11-18 stsp (ATOM)++)
77 dea26038 2020-11-18 stsp
78 dea26038 2020-11-18 stsp #define diff_data_foreach_atom(ATOM, DIFF_DATA) \
79 dea26038 2020-11-18 stsp foreach_diff_atom(ATOM, (DIFF_DATA)->atoms.head, (DIFF_DATA)->atoms.len)
80 dea26038 2020-11-18 stsp
81 dea26038 2020-11-18 stsp #define diff_data_foreach_atom_from(FROM, ATOM, DIFF_DATA) \
82 dea26038 2020-11-18 stsp for ((ATOM) = (FROM); \
83 dea26038 2020-11-18 stsp (ATOM) \
84 dea26038 2020-11-18 stsp && ((ATOM) >= (DIFF_DATA)->atoms.head) \
85 dea26038 2020-11-18 stsp && ((ATOM) - (DIFF_DATA)->atoms.head < (DIFF_DATA)->atoms.len); \
86 dea26038 2020-11-18 stsp (ATOM)++)
87 dea26038 2020-11-18 stsp
88 dea26038 2020-11-18 stsp #define diff_data_foreach_atom_backwards_from(FROM, ATOM, DIFF_DATA) \
89 dea26038 2020-11-18 stsp for ((ATOM) = (FROM); \
90 dea26038 2020-11-18 stsp (ATOM) \
91 dea26038 2020-11-18 stsp && ((ATOM) >= (DIFF_DATA)->atoms.head) \
92 dea26038 2020-11-18 stsp && ((ATOM) - (DIFF_DATA)->atoms.head >= 0); \
93 dea26038 2020-11-18 stsp (ATOM)--)
94 dea26038 2020-11-18 stsp
95 fe621944 2020-11-10 stsp /* For each file, there is a "root" struct diff_data referencing the entire
96 fe621944 2020-11-10 stsp * file, which the atoms are parsed from. In recursion of diff algorithm, there
97 fe621944 2020-11-10 stsp * may be "child" struct diff_data only referencing a subsection of the file,
98 fe621944 2020-11-10 stsp * re-using the atoms parsing. For "root" structs, atoms_allocated will be
99 fe621944 2020-11-10 stsp * nonzero, indicating that the array of atoms is owned by that struct. For
100 fe621944 2020-11-10 stsp * "child" structs, atoms_allocated == 0, to indicate that the struct is
101 fe621944 2020-11-10 stsp * referencing a subset of atoms. */
102 fe621944 2020-11-10 stsp struct diff_data {
103 fe621944 2020-11-10 stsp FILE *f; /* if root diff_data and not memory-mapped */
104 fe621944 2020-11-10 stsp off_t pos; /* if not memory-mapped */
105 fe621944 2020-11-10 stsp const uint8_t *data; /* if memory-mapped */
106 fe621944 2020-11-10 stsp off_t len;
107 fe621944 2020-11-10 stsp
108 b67f3bcb 2020-11-21 stsp int atomizer_flags;
109 fe621944 2020-11-10 stsp ARRAYLIST(struct diff_atom) atoms;
110 fe621944 2020-11-10 stsp struct diff_data *root;
111 fe621944 2020-11-10 stsp struct diff_data *current;
112 fe621944 2020-11-10 stsp void *algo_data;
113 fe621944 2020-11-10 stsp
114 fe621944 2020-11-10 stsp int diff_flags;
115 fe621944 2020-11-10 stsp
116 fe621944 2020-11-10 stsp int err;
117 fe621944 2020-11-10 stsp };
118 fe621944 2020-11-10 stsp
119 b67f3bcb 2020-11-21 stsp /* Flags set by file atomizer. */
120 b67f3bcb 2020-11-21 stsp #define DIFF_ATOMIZER_FOUND_BINARY_DATA 0x00000001
121 b67f3bcb 2020-11-21 stsp
122 b67f3bcb 2020-11-21 stsp /* Flags set by caller of diff_main(). */
123 fe621944 2020-11-10 stsp #define DIFF_FLAG_IGNORE_WHITESPACE 0x00000001
124 fe621944 2020-11-10 stsp #define DIFF_FLAG_SHOW_PROTOTYPES 0x00000002
125 b67f3bcb 2020-11-21 stsp #define DIFF_FLAG_FORCE_TEXT_DATA 0x00000004
126 fe621944 2020-11-10 stsp
127 fe621944 2020-11-10 stsp void diff_data_free(struct diff_data *diff_data);
128 fe621944 2020-11-10 stsp
129 fe621944 2020-11-10 stsp struct diff_chunk;
130 fe621944 2020-11-10 stsp typedef ARRAYLIST(struct diff_chunk) diff_chunk_arraylist_t;
131 fe621944 2020-11-10 stsp
132 fe621944 2020-11-10 stsp struct diff_result {
133 fe621944 2020-11-10 stsp int rc;
134 fe621944 2020-11-10 stsp
135 fe621944 2020-11-10 stsp /*
136 fe621944 2020-11-10 stsp * Pointers to diff data passed in via diff_main.
137 fe621944 2020-11-10 stsp * Do not free these diff_data before freeing the diff_result struct.
138 fe621944 2020-11-10 stsp */
139 fe621944 2020-11-10 stsp struct diff_data *left;
140 fe621944 2020-11-10 stsp struct diff_data *right;
141 fe621944 2020-11-10 stsp
142 fe621944 2020-11-10 stsp diff_chunk_arraylist_t chunks;
143 c251eeea 2023-02-20 mark };
144 c251eeea 2023-02-20 mark
145 c251eeea 2023-02-20 mark enum diff_chunk_type {
146 c251eeea 2023-02-20 mark CHUNK_EMPTY,
147 c251eeea 2023-02-20 mark CHUNK_PLUS,
148 c251eeea 2023-02-20 mark CHUNK_MINUS,
149 c251eeea 2023-02-20 mark CHUNK_SAME,
150 c251eeea 2023-02-20 mark CHUNK_ERROR,
151 fe621944 2020-11-10 stsp };
152 fe621944 2020-11-10 stsp
153 c251eeea 2023-02-20 mark enum diff_chunk_type diff_chunk_type(const struct diff_chunk *c);
154 c251eeea 2023-02-20 mark
155 fe621944 2020-11-10 stsp struct diff_state;
156 fe621944 2020-11-10 stsp
157 fe621944 2020-11-10 stsp /* Signature of a utility function to divide a file into diff atoms.
158 fe621944 2020-11-10 stsp * An example is diff_atomize_text_by_line() in diff_atomize_text.c.
159 fe621944 2020-11-10 stsp *
160 fe621944 2020-11-10 stsp * func_data: context pointer (free to be used by implementation).
161 fe621944 2020-11-10 stsp * d: struct diff_data with d->data and d->len already set up, and
162 b67f3bcb 2020-11-21 stsp * d->atoms to be created and d->atomizer_flags to be set up.
163 fe621944 2020-11-10 stsp */
164 fe621944 2020-11-10 stsp typedef int (*diff_atomize_func_t)(void *func_data, struct diff_data *d);
165 fe621944 2020-11-10 stsp
166 fe621944 2020-11-10 stsp extern int diff_atomize_text_by_line(void *func_data, struct diff_data *d);
167 fe621944 2020-11-10 stsp
168 fe621944 2020-11-10 stsp struct diff_algo_config;
169 fe621944 2020-11-10 stsp typedef int (*diff_algo_impl_t)(
170 fe621944 2020-11-10 stsp const struct diff_algo_config *algo_config, struct diff_state *state);
171 fe621944 2020-11-10 stsp
172 fe621944 2020-11-10 stsp /* Form a result with all left-side removed and all right-side added, i.e. no
173 fe621944 2020-11-10 stsp * actual diff algorithm involved. */
174 fe621944 2020-11-10 stsp int diff_algo_none(const struct diff_algo_config *algo_config,
175 fe621944 2020-11-10 stsp struct diff_state *state);
176 fe621944 2020-11-10 stsp
177 fe621944 2020-11-10 stsp /* Myers Diff tracing from the start all the way through to the end, requiring
178 fe621944 2020-11-10 stsp * quadratic amounts of memory. This can fail if the required space surpasses
179 fe621944 2020-11-10 stsp * algo_config->permitted_state_size. */
180 fe621944 2020-11-10 stsp extern int diff_algo_myers(const struct diff_algo_config *algo_config,
181 fe621944 2020-11-10 stsp struct diff_state *state);
182 fe621944 2020-11-10 stsp
183 fe621944 2020-11-10 stsp /* Myers "Divide et Impera": tracing forwards from the start and backwards from
184 fe621944 2020-11-10 stsp * the end to find a midpoint that divides the problem into smaller chunks.
185 fe621944 2020-11-10 stsp * Requires only linear amounts of memory. */
186 fe621944 2020-11-10 stsp extern int diff_algo_myers_divide(
187 fe621944 2020-11-10 stsp const struct diff_algo_config *algo_config, struct diff_state *state);
188 fe621944 2020-11-10 stsp
189 fe621944 2020-11-10 stsp /* Patience Diff algorithm, which divides a larger diff into smaller chunks. For
190 fe621944 2020-11-10 stsp * very specific scenarios, it may lead to a complete diff result by itself, but
191 fe621944 2020-11-10 stsp * needs a fallback algo to solve chunks that don't have common-unique atoms. */
192 fe621944 2020-11-10 stsp extern int diff_algo_patience(
193 fe621944 2020-11-10 stsp const struct diff_algo_config *algo_config, struct diff_state *state);
194 fe621944 2020-11-10 stsp
195 fe621944 2020-11-10 stsp /* Diff algorithms to use, possibly nested. For example:
196 fe621944 2020-11-10 stsp *
197 fe621944 2020-11-10 stsp * struct diff_algo_config myers, patience, myers_divide;
198 fe621944 2020-11-10 stsp *
199 fe621944 2020-11-10 stsp * myers = (struct diff_algo_config){
200 fe621944 2020-11-10 stsp * .impl = diff_algo_myers,
201 fe621944 2020-11-10 stsp * .permitted_state_size = 32 * 1024 * 1024,
202 fe621944 2020-11-10 stsp * // When too large, do diff_algo_patience:
203 fe621944 2020-11-10 stsp * .fallback_algo = &patience,
204 fe621944 2020-11-10 stsp * };
205 fe621944 2020-11-10 stsp *
206 fe621944 2020-11-10 stsp * const struct diff_algo_config patience = (struct diff_algo_config){
207 fe621944 2020-11-10 stsp * .impl = diff_algo_patience,
208 fe621944 2020-11-10 stsp * // After subdivision, do Patience again:
209 fe621944 2020-11-10 stsp * .inner_algo = &patience,
210 fe621944 2020-11-10 stsp * // If subdivision failed, do Myers Divide et Impera:
211 fe621944 2020-11-10 stsp * .fallback_algo = &myers_then_myers_divide,
212 fe621944 2020-11-10 stsp * };
213 fe621944 2020-11-10 stsp *
214 fe621944 2020-11-10 stsp * const struct diff_algo_config myers_divide = (struct diff_algo_config){
215 fe621944 2020-11-10 stsp * .impl = diff_algo_myers_divide,
216 fe621944 2020-11-10 stsp * // When division succeeded, start from the top:
217 fe621944 2020-11-10 stsp * .inner_algo = &myers_then_myers_divide,
218 fe621944 2020-11-10 stsp * // (fallback_algo = NULL implies diff_algo_none).
219 fe621944 2020-11-10 stsp * };
220 fe621944 2020-11-10 stsp * struct diff_config config = {
221 fe621944 2020-11-10 stsp * .algo = &myers,
222 fe621944 2020-11-10 stsp * ...
223 fe621944 2020-11-10 stsp * };
224 fe621944 2020-11-10 stsp * diff_main(&config, ...);
225 fe621944 2020-11-10 stsp */
226 fe621944 2020-11-10 stsp struct diff_algo_config {
227 fe621944 2020-11-10 stsp diff_algo_impl_t impl;
228 fe621944 2020-11-10 stsp
229 fe621944 2020-11-10 stsp /* Fail this algo if it would use more than this amount of memory, and
230 fe621944 2020-11-10 stsp * instead use fallback_algo (diff_algo_myers). permitted_state_size ==
231 fe621944 2020-11-10 stsp * 0 means no limitation. */
232 fe621944 2020-11-10 stsp size_t permitted_state_size;
233 fe621944 2020-11-10 stsp
234 fe621944 2020-11-10 stsp /* For algorithms that divide into smaller chunks, use this algorithm to
235 fe621944 2020-11-10 stsp * solve the divided chunks. */
236 fe621944 2020-11-10 stsp const struct diff_algo_config *inner_algo;
237 fe621944 2020-11-10 stsp
238 fe621944 2020-11-10 stsp /* If the algorithm fails (e.g. diff_algo_myers_if_small needs too large
239 fe621944 2020-11-10 stsp * state, or diff_algo_patience can't find any common-unique atoms),
240 fe621944 2020-11-10 stsp * then use this algorithm instead. */
241 fe621944 2020-11-10 stsp const struct diff_algo_config *fallback_algo;
242 fe621944 2020-11-10 stsp };
243 fe621944 2020-11-10 stsp
244 fe621944 2020-11-10 stsp struct diff_config {
245 fe621944 2020-11-10 stsp diff_atomize_func_t atomize_func;
246 fe621944 2020-11-10 stsp void *atomize_func_data;
247 fe621944 2020-11-10 stsp
248 fe621944 2020-11-10 stsp const struct diff_algo_config *algo;
249 fe621944 2020-11-10 stsp
250 fe621944 2020-11-10 stsp /* How deep to step into subdivisions of a source file, a paranoia /
251 fe621944 2020-11-10 stsp * safety measure to guard against infinite loops through diff
252 fe621944 2020-11-10 stsp * algorithms. When the maximum recursion is reached, employ
253 fe621944 2020-11-10 stsp * diff_algo_none (i.e. remove all left atoms and add all right atoms).
254 fe621944 2020-11-10 stsp */
255 fe621944 2020-11-10 stsp unsigned int max_recursion_depth;
256 fe621944 2020-11-10 stsp };
257 fe621944 2020-11-10 stsp
258 fe621944 2020-11-10 stsp int diff_atomize_file(struct diff_data *d, const struct diff_config *config,
259 fe621944 2020-11-10 stsp FILE *f, const uint8_t *data, off_t len, int diff_flags);
260 fe621944 2020-11-10 stsp struct diff_result *diff_main(const struct diff_config *config,
261 fe621944 2020-11-10 stsp struct diff_data *left,
262 fe621944 2020-11-10 stsp struct diff_data *right);
263 fe621944 2020-11-10 stsp void diff_result_free(struct diff_result *result);
264 b16ee069 2022-10-11 stsp int diff_result_contains_printable_chunks(struct diff_result *result);