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 #ifndef MAX
19 fe621944 2020-11-10 stsp #define MAX(A,B) ((A)>(B)?(A):(B))
20 fe621944 2020-11-10 stsp #endif
21 fe621944 2020-11-10 stsp #ifndef MIN
22 fe621944 2020-11-10 stsp #define MIN(A,B) ((A)<(B)?(A):(B))
23 fe621944 2020-11-10 stsp #endif
24 fe621944 2020-11-10 stsp
25 fe621944 2020-11-10 stsp static inline bool
26 fe621944 2020-11-10 stsp diff_range_empty(const struct diff_range *r)
27 fe621944 2020-11-10 stsp {
28 fe621944 2020-11-10 stsp return r->start == r->end;
29 fe621944 2020-11-10 stsp }
30 fe621944 2020-11-10 stsp
31 fe621944 2020-11-10 stsp static inline bool
32 fe621944 2020-11-10 stsp diff_ranges_touch(const struct diff_range *a, const struct diff_range *b)
33 fe621944 2020-11-10 stsp {
34 fe621944 2020-11-10 stsp return (a->end >= b->start) && (a->start <= b->end);
35 fe621944 2020-11-10 stsp }
36 fe621944 2020-11-10 stsp
37 fe621944 2020-11-10 stsp static inline void
38 fe621944 2020-11-10 stsp diff_ranges_merge(struct diff_range *a, const struct diff_range *b)
39 fe621944 2020-11-10 stsp {
40 fe621944 2020-11-10 stsp *a = (struct diff_range){
41 fe621944 2020-11-10 stsp .start = MIN(a->start, b->start),
42 fe621944 2020-11-10 stsp .end = MAX(a->end, b->end),
43 fe621944 2020-11-10 stsp };
44 fe621944 2020-11-10 stsp }
45 fe621944 2020-11-10 stsp
46 fe621944 2020-11-10 stsp static inline int
47 fe621944 2020-11-10 stsp diff_range_len(const struct diff_range *r)
48 fe621944 2020-11-10 stsp {
49 fe621944 2020-11-10 stsp if (!r)
50 fe621944 2020-11-10 stsp return 0;
51 fe621944 2020-11-10 stsp return r->end - r->start;
52 fe621944 2020-11-10 stsp }
53 fe621944 2020-11-10 stsp
54 fe621944 2020-11-10 stsp /* List of all possible return codes of a diff invocation. */
55 fe621944 2020-11-10 stsp #define DIFF_RC_USE_DIFF_ALGO_FALLBACK -1
56 fe621944 2020-11-10 stsp #define DIFF_RC_OK 0
57 fe621944 2020-11-10 stsp /* Any positive return values are errno values from sys/errno.h */
58 fe621944 2020-11-10 stsp
59 fe621944 2020-11-10 stsp struct diff_data;
60 fe621944 2020-11-10 stsp
61 fe621944 2020-11-10 stsp struct diff_atom {
62 fe621944 2020-11-10 stsp struct diff_data *root; /* back pointer to root diff data */
63 fe621944 2020-11-10 stsp
64 fe621944 2020-11-10 stsp off_t pos; /* if not memory-mapped */
65 fe621944 2020-11-10 stsp const uint8_t *at; /* if memory-mapped */
66 fe621944 2020-11-10 stsp off_t len;
67 fe621944 2020-11-10 stsp
68 fe621944 2020-11-10 stsp /* This hash is just a very cheap speed up for finding *mismatching*
69 fe621944 2020-11-10 stsp * atoms. When hashes match, we still need to compare entire atoms to
70 fe621944 2020-11-10 stsp * find out whether they are indeed identical or not. */
71 fe621944 2020-11-10 stsp unsigned int hash;
72 fe621944 2020-11-10 stsp };
73 fe621944 2020-11-10 stsp
74 fe621944 2020-11-10 stsp int
75 fe621944 2020-11-10 stsp diff_atom_cmp(int *cmp,
76 fe621944 2020-11-10 stsp const struct diff_atom *left,
77 fe621944 2020-11-10 stsp const struct diff_atom *right);
78 fe621944 2020-11-10 stsp
79 fe621944 2020-11-10 stsp /* Indicate whether two given diff atoms match. */
80 fe621944 2020-11-10 stsp int
81 fe621944 2020-11-10 stsp diff_atom_same(bool *same,
82 fe621944 2020-11-10 stsp const struct diff_atom *left,
83 fe621944 2020-11-10 stsp const struct diff_atom *right);
84 fe621944 2020-11-10 stsp
85 fe621944 2020-11-10 stsp /* The atom's index in the entire file. For atoms divided by lines of text, this
86 fe621944 2020-11-10 stsp * yields the line number (starting with 0). Also works for diff_data that
87 fe621944 2020-11-10 stsp * reference only a subsection of a file, always reflecting the global position
88 fe621944 2020-11-10 stsp * in the file (and not the relative position within the subsection). */
89 fe621944 2020-11-10 stsp #define diff_atom_root_idx(DIFF_DATA, ATOM) \
90 fe621944 2020-11-10 stsp ((ATOM) && ((ATOM) >= (DIFF_DATA)->root->atoms.head) \
91 fe621944 2020-11-10 stsp ? (unsigned int)((ATOM) - ((DIFF_DATA)->root->atoms.head)) \
92 fe621944 2020-11-10 stsp : (DIFF_DATA)->root->atoms.len)
93 fe621944 2020-11-10 stsp
94 fe621944 2020-11-10 stsp /* The atom's index within DIFF_DATA. For atoms divided by lines of text, this
95 fe621944 2020-11-10 stsp * yields the line number (starting with 0). */
96 fe621944 2020-11-10 stsp #define diff_atom_idx(DIFF_DATA, ATOM) \
97 fe621944 2020-11-10 stsp ((ATOM) && ((ATOM) >= (DIFF_DATA)->atoms.head) \
98 fe621944 2020-11-10 stsp ? (unsigned int)((ATOM) - ((DIFF_DATA)->atoms.head)) \
99 fe621944 2020-11-10 stsp : (DIFF_DATA)->atoms.len)
100 fe621944 2020-11-10 stsp
101 fe621944 2020-11-10 stsp #define foreach_diff_atom(ATOM, FIRST_ATOM, COUNT) \
102 fe621944 2020-11-10 stsp for ((ATOM) = (FIRST_ATOM); \
103 fe621944 2020-11-10 stsp (ATOM) \
104 fe621944 2020-11-10 stsp && ((ATOM) >= (FIRST_ATOM)) \
105 fe621944 2020-11-10 stsp && ((ATOM) - (FIRST_ATOM) < (COUNT)); \
106 fe621944 2020-11-10 stsp (ATOM)++)
107 fe621944 2020-11-10 stsp
108 fe621944 2020-11-10 stsp #define diff_data_foreach_atom(ATOM, DIFF_DATA) \
109 fe621944 2020-11-10 stsp foreach_diff_atom(ATOM, (DIFF_DATA)->atoms.head, (DIFF_DATA)->atoms.len)
110 fe621944 2020-11-10 stsp
111 fe621944 2020-11-10 stsp #define diff_data_foreach_atom_from(FROM, ATOM, DIFF_DATA) \
112 fe621944 2020-11-10 stsp for ((ATOM) = (FROM); \
113 fe621944 2020-11-10 stsp (ATOM) \
114 fe621944 2020-11-10 stsp && ((ATOM) >= (DIFF_DATA)->atoms.head) \
115 fe621944 2020-11-10 stsp && ((ATOM) - (DIFF_DATA)->atoms.head < (DIFF_DATA)->atoms.len); \
116 fe621944 2020-11-10 stsp (ATOM)++)
117 fe621944 2020-11-10 stsp
118 fe621944 2020-11-10 stsp #define diff_data_foreach_atom_backwards_from(FROM, ATOM, DIFF_DATA) \
119 fe621944 2020-11-10 stsp for ((ATOM) = (FROM); \
120 fe621944 2020-11-10 stsp (ATOM) \
121 fe621944 2020-11-10 stsp && ((ATOM) >= (DIFF_DATA)->atoms.head) \
122 fe621944 2020-11-10 stsp && ((ATOM) - (DIFF_DATA)->atoms.head >= 0); \
123 fe621944 2020-11-10 stsp (ATOM)--)
124 fe621944 2020-11-10 stsp
125 fe621944 2020-11-10 stsp /* A diff chunk represents a set of atoms on the left and/or a set of atoms on
126 fe621944 2020-11-10 stsp * the right.
127 fe621944 2020-11-10 stsp *
128 fe621944 2020-11-10 stsp * If solved == false:
129 fe621944 2020-11-10 stsp * The diff algorithm has divided the source file, and this is a chunk that the
130 fe621944 2020-11-10 stsp * inner_algo should run on next.
131 fe621944 2020-11-10 stsp * The lines on the left should be diffed against the lines on the right.
132 fe621944 2020-11-10 stsp * (If there are no left lines or no right lines, it implies solved == true,
133 fe621944 2020-11-10 stsp * because there is nothing to diff.)
134 fe621944 2020-11-10 stsp *
135 fe621944 2020-11-10 stsp * If solved == true:
136 fe621944 2020-11-10 stsp * If there are only left atoms, it is a chunk removing atoms from the left ("a
137 fe621944 2020-11-10 stsp * minus chunk").
138 fe621944 2020-11-10 stsp * If there are only right atoms, it is a chunk adding atoms from the right ("a
139 fe621944 2020-11-10 stsp * plus chunk").
140 fe621944 2020-11-10 stsp * If there are both left and right lines, it is a chunk of equal content on
141 fe621944 2020-11-10 stsp * both sides, and left_count == right_count:
142 fe621944 2020-11-10 stsp *
143 fe621944 2020-11-10 stsp * - foo }
144 fe621944 2020-11-10 stsp * - bar }-- diff_chunk{ left_start = &left.atoms.head[0], left_count = 3,
145 fe621944 2020-11-10 stsp * - baz } right_start = NULL, right_count = 0 }
146 fe621944 2020-11-10 stsp * moo }
147 fe621944 2020-11-10 stsp * goo }-- diff_chunk{ left_start = &left.atoms.head[3], left_count = 3,
148 fe621944 2020-11-10 stsp * zoo } right_start = &right.atoms.head[0], right_count = 3 }
149 fe621944 2020-11-10 stsp * +loo }
150 fe621944 2020-11-10 stsp * +roo }-- diff_chunk{ left_start = NULL, left_count = 0,
151 fe621944 2020-11-10 stsp * +too } right_start = &right.atoms.head[3], right_count = 3 }
152 fe621944 2020-11-10 stsp *
153 fe621944 2020-11-10 stsp */
154 fe621944 2020-11-10 stsp struct diff_chunk {
155 fe621944 2020-11-10 stsp bool solved;
156 fe621944 2020-11-10 stsp struct diff_atom *left_start;
157 fe621944 2020-11-10 stsp unsigned int left_count;
158 fe621944 2020-11-10 stsp struct diff_atom *right_start;
159 fe621944 2020-11-10 stsp unsigned int right_count;
160 fe621944 2020-11-10 stsp };
161 fe621944 2020-11-10 stsp
162 fe621944 2020-11-10 stsp #define DIFF_RESULT_ALLOC_BLOCKSIZE 128
163 fe621944 2020-11-10 stsp
164 fe621944 2020-11-10 stsp enum diff_chunk_type {
165 fe621944 2020-11-10 stsp CHUNK_EMPTY,
166 fe621944 2020-11-10 stsp CHUNK_PLUS,
167 fe621944 2020-11-10 stsp CHUNK_MINUS,
168 fe621944 2020-11-10 stsp CHUNK_SAME,
169 fe621944 2020-11-10 stsp CHUNK_ERROR,
170 fe621944 2020-11-10 stsp };
171 fe621944 2020-11-10 stsp
172 fe621944 2020-11-10 stsp static inline enum diff_chunk_type
173 fe621944 2020-11-10 stsp diff_chunk_type(const struct diff_chunk *chunk)
174 fe621944 2020-11-10 stsp {
175 fe621944 2020-11-10 stsp if (!chunk->left_count && !chunk->right_count)
176 fe621944 2020-11-10 stsp return CHUNK_EMPTY;
177 fe621944 2020-11-10 stsp if (!chunk->solved)
178 fe621944 2020-11-10 stsp return CHUNK_ERROR;
179 fe621944 2020-11-10 stsp if (!chunk->right_count)
180 fe621944 2020-11-10 stsp return CHUNK_MINUS;
181 fe621944 2020-11-10 stsp if (!chunk->left_count)
182 fe621944 2020-11-10 stsp return CHUNK_PLUS;
183 fe621944 2020-11-10 stsp if (chunk->left_count != chunk->right_count)
184 fe621944 2020-11-10 stsp return CHUNK_ERROR;
185 fe621944 2020-11-10 stsp return CHUNK_SAME;
186 fe621944 2020-11-10 stsp }
187 fe621944 2020-11-10 stsp
188 fe621944 2020-11-10 stsp struct diff_chunk_context;
189 fe621944 2020-11-10 stsp
190 fe621944 2020-11-10 stsp bool
191 fe621944 2020-11-10 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc);
192 fe621944 2020-11-10 stsp
193 fe621944 2020-11-10 stsp bool
194 fe621944 2020-11-10 stsp diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
195 fe621944 2020-11-10 stsp const struct diff_chunk_context *other);
196 fe621944 2020-11-10 stsp
197 fe621944 2020-11-10 stsp void
198 fe621944 2020-11-10 stsp diff_chunk_contexts_merge(struct diff_chunk_context *cc,
199 fe621944 2020-11-10 stsp const struct diff_chunk_context *other);
200 fe621944 2020-11-10 stsp
201 fe621944 2020-11-10 stsp struct diff_state {
202 fe621944 2020-11-10 stsp /* The final result passed to the original diff caller. */
203 fe621944 2020-11-10 stsp struct diff_result *result;
204 fe621944 2020-11-10 stsp
205 fe621944 2020-11-10 stsp /* The root diff_data is in result->left,right, these are (possibly)
206 fe621944 2020-11-10 stsp * subsections of the root data. */
207 fe621944 2020-11-10 stsp struct diff_data left;
208 fe621944 2020-11-10 stsp struct diff_data right;
209 fe621944 2020-11-10 stsp
210 fe621944 2020-11-10 stsp unsigned int recursion_depth_left;
211 fe621944 2020-11-10 stsp
212 fe621944 2020-11-10 stsp /* Remaining chunks from one diff algorithm pass, if any solved == false
213 fe621944 2020-11-10 stsp * chunks came up. */
214 fe621944 2020-11-10 stsp diff_chunk_arraylist_t temp_result;
215 fe621944 2020-11-10 stsp
216 fe621944 2020-11-10 stsp /* State buffer used by Myers algorithm. */
217 fe621944 2020-11-10 stsp int *kd_buf;
218 fe621944 2020-11-10 stsp size_t kd_buf_size; /* in units of sizeof(int), not bytes */
219 fe621944 2020-11-10 stsp };
220 fe621944 2020-11-10 stsp
221 fe621944 2020-11-10 stsp struct diff_chunk *diff_state_add_chunk(struct diff_state *state, bool solved,
222 fe621944 2020-11-10 stsp struct diff_atom *left_start,
223 fe621944 2020-11-10 stsp unsigned int left_count,
224 fe621944 2020-11-10 stsp struct diff_atom *right_start,
225 fe621944 2020-11-10 stsp unsigned int right_count);
226 fe621944 2020-11-10 stsp
227 fe621944 2020-11-10 stsp struct diff_output_info;
228 fe621944 2020-11-10 stsp
229 fe621944 2020-11-10 stsp int diff_output_lines(struct diff_output_info *output_info, FILE *dest,
230 fe621944 2020-11-10 stsp const char *prefix, struct diff_atom *start_atom,
231 fe621944 2020-11-10 stsp unsigned int count);
232 fe621944 2020-11-10 stsp
233 fe621944 2020-11-10 stsp int diff_output_trailing_newline_msg(struct diff_output_info *outinfo,
234 fe621944 2020-11-10 stsp FILE *dest,
235 fe621944 2020-11-10 stsp const struct diff_chunk *c);
236 fe621944 2020-11-10 stsp int diff_output_match_function_prototype(char **prototype,
237 fe621944 2020-11-10 stsp const struct diff_result *result,
238 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc);
239 fe621944 2020-11-10 stsp
240 fe621944 2020-11-10 stsp struct diff_output_info *diff_output_info_alloc(void);
241 fe621944 2020-11-10 stsp
242 fe621944 2020-11-10 stsp void
243 fe621944 2020-11-10 stsp diff_data_init_subsection(struct diff_data *d, struct diff_data *parent,
244 fe621944 2020-11-10 stsp struct diff_atom *from_atom, unsigned int atoms_count);