Blame


1 1dfba055 2020-10-07 stsp /* Generic infrastructure to implement various diff algorithms. */
2 1dfba055 2020-10-07 stsp /*
3 1dfba055 2020-10-07 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 1dfba055 2020-10-07 stsp *
5 1dfba055 2020-10-07 stsp * Permission to use, copy, modify, and distribute this software for any
6 1dfba055 2020-10-07 stsp * purpose with or without fee is hereby granted, provided that the above
7 1dfba055 2020-10-07 stsp * copyright notice and this permission notice appear in all copies.
8 1dfba055 2020-10-07 stsp *
9 1dfba055 2020-10-07 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1dfba055 2020-10-07 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1dfba055 2020-10-07 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1dfba055 2020-10-07 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1dfba055 2020-10-07 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1dfba055 2020-10-07 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1dfba055 2020-10-07 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1dfba055 2020-10-07 stsp */
17 1dfba055 2020-10-07 stsp
18 1dfba055 2020-10-07 stsp struct diff_range {
19 1dfba055 2020-10-07 stsp int start;
20 1dfba055 2020-10-07 stsp int end;
21 1dfba055 2020-10-07 stsp };
22 1dfba055 2020-10-07 stsp
23 1dfba055 2020-10-07 stsp /* List of all possible return codes of a diff invocation. */
24 1dfba055 2020-10-07 stsp #define DIFF_RC_USE_DIFF_ALGO_FALLBACK -1
25 1dfba055 2020-10-07 stsp #define DIFF_RC_OK 0
26 1dfba055 2020-10-07 stsp /* Any positive return values are errno values from sys/errno.h */
27 1dfba055 2020-10-07 stsp
28 1dfba055 2020-10-07 stsp struct diff_atom;
29 1dfba055 2020-10-07 stsp
30 1dfba055 2020-10-07 stsp /* For each file, there is a "root" struct diff_data referencing the entire
31 1dfba055 2020-10-07 stsp * file, which the atoms are parsed from. In recursion of diff algorithm, there
32 1dfba055 2020-10-07 stsp * may be "child" struct diff_data only referencing a subsection of the file,
33 1dfba055 2020-10-07 stsp * re-using the atoms parsing. For "root" structs, atoms_allocated will be
34 1dfba055 2020-10-07 stsp * nonzero, indicating that the array of atoms is owned by that struct. For
35 1dfba055 2020-10-07 stsp * "child" structs, atoms_allocated == 0, to indicate that the struct is
36 1dfba055 2020-10-07 stsp * referencing a subset of atoms. */
37 1dfba055 2020-10-07 stsp struct diff_data {
38 1dfba055 2020-10-07 stsp FILE *f; /* if root diff_data and not memory-mapped */
39 1dfba055 2020-10-07 stsp off_t pos; /* if not memory-mapped */
40 1dfba055 2020-10-07 stsp const uint8_t *data; /* if memory-mapped */
41 1dfba055 2020-10-07 stsp off_t len;
42 1dfba055 2020-10-07 stsp
43 1dfba055 2020-10-07 stsp ARRAYLIST(struct diff_atom) atoms;
44 1dfba055 2020-10-07 stsp struct diff_data *root;
45 1dfba055 2020-10-07 stsp
46 1dfba055 2020-10-07 stsp int diff_flags;
47 1dfba055 2020-10-07 stsp };
48 1dfba055 2020-10-07 stsp
49 1dfba055 2020-10-07 stsp #define DIFF_FLAG_IGNORE_WHITESPACE 0x00000001
50 1dfba055 2020-10-07 stsp
51 1dfba055 2020-10-07 stsp void diff_data_free(struct diff_data *diff_data);
52 1dfba055 2020-10-07 stsp
53 1dfba055 2020-10-07 stsp struct diff_chunk;
54 1dfba055 2020-10-07 stsp typedef ARRAYLIST(struct diff_chunk) diff_chunk_arraylist_t;
55 1dfba055 2020-10-07 stsp
56 1dfba055 2020-10-07 stsp struct diff_result {
57 1dfba055 2020-10-07 stsp int rc;
58 1dfba055 2020-10-07 stsp struct diff_data left;
59 1dfba055 2020-10-07 stsp struct diff_data right;
60 1dfba055 2020-10-07 stsp diff_chunk_arraylist_t chunks;
61 1dfba055 2020-10-07 stsp };
62 1dfba055 2020-10-07 stsp
63 1dfba055 2020-10-07 stsp struct diff_state;
64 1dfba055 2020-10-07 stsp
65 1dfba055 2020-10-07 stsp /* Signature of a utility function to divide both source files into diff atoms.
66 1dfba055 2020-10-07 stsp * It is possible that a (future) algorithm requires both source files to decide
67 1dfba055 2020-10-07 stsp * on atom split points, hence this gets both left and right to atomize at the
68 1dfba055 2020-10-07 stsp * same time.
69 1dfba055 2020-10-07 stsp * An example is diff_atomize_text_by_line() in diff_atomize_text.c.
70 1dfba055 2020-10-07 stsp *
71 1dfba055 2020-10-07 stsp * func_data: context pointer (free to be used by implementation).
72 1dfba055 2020-10-07 stsp * left: struct diff_data with left->data and left->len already set up, and
73 1dfba055 2020-10-07 stsp * left->atoms to be created.
74 1dfba055 2020-10-07 stsp * right: struct diff_data with right->data and right->len already set up, and
75 1dfba055 2020-10-07 stsp * right->atoms to be created.
76 1dfba055 2020-10-07 stsp */
77 1dfba055 2020-10-07 stsp typedef int (*diff_atomize_func_t)(void *func_data,
78 1dfba055 2020-10-07 stsp struct diff_data *left,
79 1dfba055 2020-10-07 stsp struct diff_data *right);
80 1dfba055 2020-10-07 stsp
81 1dfba055 2020-10-07 stsp extern int diff_atomize_text_by_line(void *func_data,
82 1dfba055 2020-10-07 stsp struct diff_data *left,
83 1dfba055 2020-10-07 stsp struct diff_data *right);
84 1dfba055 2020-10-07 stsp
85 1dfba055 2020-10-07 stsp struct diff_algo_config;
86 1dfba055 2020-10-07 stsp typedef int (*diff_algo_impl_t)(
87 1dfba055 2020-10-07 stsp const struct diff_algo_config *algo_config, struct diff_state *state);
88 1dfba055 2020-10-07 stsp
89 1dfba055 2020-10-07 stsp /* Form a result with all left-side removed and all right-side added, i.e. no
90 1dfba055 2020-10-07 stsp * actual diff algorithm involved. */
91 1dfba055 2020-10-07 stsp int diff_algo_none(const struct diff_algo_config *algo_config,
92 0c70b22b 2020-10-11 neels struct diff_state *state);
93 1dfba055 2020-10-07 stsp
94 1dfba055 2020-10-07 stsp /* Myers Diff tracing from the start all the way through to the end, requiring
95 1dfba055 2020-10-07 stsp * quadratic amounts of memory. This can fail if the required space surpasses
96 1dfba055 2020-10-07 stsp * algo_config->permitted_state_size. */
97 1dfba055 2020-10-07 stsp extern int diff_algo_myers(const struct diff_algo_config *algo_config,
98 0c70b22b 2020-10-11 neels struct diff_state *state);
99 1dfba055 2020-10-07 stsp
100 1dfba055 2020-10-07 stsp /* Myers "Divide et Impera": tracing forwards from the start and backwards from
101 1dfba055 2020-10-07 stsp * the end to find a midpoint that divides the problem into smaller chunks.
102 1dfba055 2020-10-07 stsp * Requires only linear amounts of memory. */
103 1dfba055 2020-10-07 stsp extern int diff_algo_myers_divide(
104 1dfba055 2020-10-07 stsp const struct diff_algo_config *algo_config, struct diff_state *state);
105 1dfba055 2020-10-07 stsp
106 1dfba055 2020-10-07 stsp /* Patience Diff algorithm, which divides a larger diff into smaller chunks. For
107 1dfba055 2020-10-07 stsp * very specific scenarios, it may lead to a complete diff result by itself, but
108 1dfba055 2020-10-07 stsp * needs a fallback algo to solve chunks that don't have common-unique atoms. */
109 1dfba055 2020-10-07 stsp extern int diff_algo_patience(
110 1dfba055 2020-10-07 stsp const struct diff_algo_config *algo_config, struct diff_state *state);
111 1dfba055 2020-10-07 stsp
112 1dfba055 2020-10-07 stsp /* Diff algorithms to use, possibly nested. For example:
113 1dfba055 2020-10-07 stsp *
114 1dfba055 2020-10-07 stsp * struct diff_algo_config myers, patience, myers_divide;
115 1dfba055 2020-10-07 stsp *
116 1dfba055 2020-10-07 stsp * myers = (struct diff_algo_config){
117 1dfba055 2020-10-07 stsp * .impl = diff_algo_myers,
118 1dfba055 2020-10-07 stsp * .permitted_state_size = 32 * 1024 * 1024,
119 1dfba055 2020-10-07 stsp * // When too large, do diff_algo_patience:
120 1dfba055 2020-10-07 stsp * .fallback_algo = &patience,
121 1dfba055 2020-10-07 stsp * };
122 1dfba055 2020-10-07 stsp *
123 1dfba055 2020-10-07 stsp * const struct diff_algo_config patience = (struct diff_algo_config){
124 1dfba055 2020-10-07 stsp * .impl = diff_algo_patience,
125 1dfba055 2020-10-07 stsp * // After subdivision, do Patience again:
126 1dfba055 2020-10-07 stsp * .inner_algo = &patience,
127 1dfba055 2020-10-07 stsp * // If subdivision failed, do Myers Divide et Impera:
128 1dfba055 2020-10-07 stsp * .fallback_algo = &myers_then_myers_divide,
129 1dfba055 2020-10-07 stsp * };
130 1dfba055 2020-10-07 stsp *
131 1dfba055 2020-10-07 stsp * const struct diff_algo_config myers_divide = (struct diff_algo_config){
132 1dfba055 2020-10-07 stsp * .impl = diff_algo_myers_divide,
133 1dfba055 2020-10-07 stsp * // When division succeeded, start from the top:
134 1dfba055 2020-10-07 stsp * .inner_algo = &myers_then_myers_divide,
135 1dfba055 2020-10-07 stsp * // (fallback_algo = NULL implies diff_algo_none).
136 1dfba055 2020-10-07 stsp * };
137 1dfba055 2020-10-07 stsp * struct diff_config config = {
138 1dfba055 2020-10-07 stsp * .algo = &myers,
139 1dfba055 2020-10-07 stsp * ...
140 1dfba055 2020-10-07 stsp * };
141 1dfba055 2020-10-07 stsp * diff_main(&config, ...);
142 1dfba055 2020-10-07 stsp */
143 1dfba055 2020-10-07 stsp struct diff_algo_config {
144 1dfba055 2020-10-07 stsp diff_algo_impl_t impl;
145 1dfba055 2020-10-07 stsp
146 1dfba055 2020-10-07 stsp /* Fail this algo if it would use more than this amount of memory, and
147 1dfba055 2020-10-07 stsp * instead use fallback_algo (diff_algo_myers). permitted_state_size ==
148 1dfba055 2020-10-07 stsp * 0 means no limitation. */
149 1dfba055 2020-10-07 stsp size_t permitted_state_size;
150 1dfba055 2020-10-07 stsp
151 1dfba055 2020-10-07 stsp /* For algorithms that divide into smaller chunks, use this algorithm to
152 1dfba055 2020-10-07 stsp * solve the divided chunks. */
153 1dfba055 2020-10-07 stsp const struct diff_algo_config *inner_algo;
154 1dfba055 2020-10-07 stsp
155 1dfba055 2020-10-07 stsp /* If the algorithm fails (e.g. diff_algo_myers_if_small needs too large
156 1dfba055 2020-10-07 stsp * state, or diff_algo_patience can't find any common-unique atoms),
157 1dfba055 2020-10-07 stsp * then use this algorithm instead. */
158 1dfba055 2020-10-07 stsp const struct diff_algo_config *fallback_algo;
159 1dfba055 2020-10-07 stsp };
160 1dfba055 2020-10-07 stsp
161 1dfba055 2020-10-07 stsp struct diff_config {
162 1dfba055 2020-10-07 stsp diff_atomize_func_t atomize_func;
163 1dfba055 2020-10-07 stsp void *atomize_func_data;
164 1dfba055 2020-10-07 stsp
165 1dfba055 2020-10-07 stsp const struct diff_algo_config *algo;
166 1dfba055 2020-10-07 stsp
167 1dfba055 2020-10-07 stsp /* How deep to step into subdivisions of a source file, a paranoia /
168 1dfba055 2020-10-07 stsp * safety measure to guard against infinite loops through diff
169 1dfba055 2020-10-07 stsp * algorithms. When the maximum recursion is reached, employ
170 1dfba055 2020-10-07 stsp * diff_algo_none (i.e. remove all left atoms and add all right atoms).
171 1dfba055 2020-10-07 stsp */
172 1dfba055 2020-10-07 stsp unsigned int max_recursion_depth;
173 1dfba055 2020-10-07 stsp };
174 1dfba055 2020-10-07 stsp
175 1dfba055 2020-10-07 stsp struct diff_result *diff_main(const struct diff_config *config,
176 1dfba055 2020-10-07 stsp FILE *left_f, const uint8_t *left_data,
177 1dfba055 2020-10-07 stsp off_t left_len,
178 1dfba055 2020-10-07 stsp FILE *right_f, const uint8_t *right_data,
179 1dfba055 2020-10-07 stsp off_t right_len, int diff_flags);
180 1dfba055 2020-10-07 stsp void diff_result_free(struct diff_result *result);