2 fe621944 2020-11-10 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
3 fe621944 2020-11-10 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 fe621944 2020-11-10 stsp #include <sys/mman.h>
19 574ed2c3 2017-11-29 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
22 574ed2c3 2017-11-29 stsp #include <errno.h>
23 574ed2c3 2017-11-29 stsp #include <stdio.h>
24 574ed2c3 2017-11-29 stsp #include <stdlib.h>
25 574ed2c3 2017-11-29 stsp #include <string.h>
27 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
29 7d283eee 2017-11-29 stsp #include "got_object.h"
30 fe621944 2020-11-10 stsp #include "got_opentemp.h"
31 fe621944 2020-11-10 stsp #include "got_error.h"
32 25ec7006 2022-07-01 thomas #include "got_diff.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
36 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_patience;
37 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_myers_divide;
38 fe621944 2020-11-10 stsp const struct diff_algo_config patience;
39 fe621944 2020-11-10 stsp const struct diff_algo_config myers_divide;
41 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
42 fe621944 2020-11-10 stsp .impl = diff_algo_myers,
43 fe621944 2020-11-10 stsp .permitted_state_size = 1024 * 1024 * sizeof(int),
44 fe621944 2020-11-10 stsp .fallback_algo = &patience,
47 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_myers_divide =
48 fe621944 2020-11-10 stsp (struct diff_algo_config){
49 fe621944 2020-11-10 stsp .impl = diff_algo_myers,
50 fe621944 2020-11-10 stsp .permitted_state_size = 1024 * 1024 * sizeof(int),
51 fe621944 2020-11-10 stsp .fallback_algo = &myers_divide,
54 fe621944 2020-11-10 stsp const struct diff_algo_config patience = (struct diff_algo_config){
55 fe621944 2020-11-10 stsp .impl = diff_algo_patience,
56 fe621944 2020-11-10 stsp /* After subdivision, do Patience again: */
57 fe621944 2020-11-10 stsp .inner_algo = &patience,
58 fe621944 2020-11-10 stsp /* If subdivision failed, do Myers Divide et Impera: */
59 fe621944 2020-11-10 stsp .fallback_algo = &myers_then_myers_divide,
62 fe621944 2020-11-10 stsp const struct diff_algo_config myers_divide = (struct diff_algo_config){
63 fe621944 2020-11-10 stsp .impl = diff_algo_myers_divide,
64 fe621944 2020-11-10 stsp /* When division succeeded, start from the top: */
65 fe621944 2020-11-10 stsp .inner_algo = &myers_then_myers_divide,
66 fe621944 2020-11-10 stsp /* (fallback_algo = NULL implies diff_algo_none). */
69 fe621944 2020-11-10 stsp /* If the state for a forward-Myers is small enough, use Myers, otherwise first
70 fe621944 2020-11-10 stsp * do a Myers-divide. */
71 fe621944 2020-11-10 stsp const struct diff_config diff_config_myers_then_myers_divide = {
72 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
73 fe621944 2020-11-10 stsp .algo = &myers_then_myers_divide,
76 fe621944 2020-11-10 stsp /* If the state for a forward-Myers is small enough, use Myers, otherwise first
77 fe621944 2020-11-10 stsp * do a Patience. */
78 fe621944 2020-11-10 stsp const struct diff_config diff_config_myers_then_patience = {
79 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
80 fe621944 2020-11-10 stsp .algo = &myers_then_patience,
83 fe621944 2020-11-10 stsp /* Directly force Patience as a first divider of the source file. */
84 fe621944 2020-11-10 stsp const struct diff_config diff_config_patience = {
85 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
86 fe621944 2020-11-10 stsp .algo = &patience,
89 fe621944 2020-11-10 stsp /* Directly force Patience as a first divider of the source file. */
90 fe621944 2020-11-10 stsp const struct diff_config diff_config_no_algo = {
91 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
94 fe621944 2020-11-10 stsp const struct got_error *
95 dd2e2f52 2022-07-01 thomas got_diffreg_close(char *p1, size_t size1, char *p2, size_t size2)
97 fe621944 2020-11-10 stsp const struct got_error *err = NULL;
99 fe621944 2020-11-10 stsp if (p1 && munmap(p1, size1) == -1 && err == NULL)
100 fe621944 2020-11-10 stsp err = got_error_from_errno("munmap");
101 fe621944 2020-11-10 stsp if (p2 && munmap(p2, size2) == -1 && err == NULL)
102 fe621944 2020-11-10 stsp err = got_error_from_errno("munmap");
103 fe621944 2020-11-10 stsp return err;
106 cca5682e 2020-11-18 stsp const struct got_error *
107 cca5682e 2020-11-18 stsp got_diff_get_config(struct diff_config **cfg,
108 cca5682e 2020-11-18 stsp enum got_diff_algorithm algorithm,
109 cca5682e 2020-11-18 stsp diff_atomize_func_t atomize_func, void *atomize_func_data)
111 cca5682e 2020-11-18 stsp *cfg = calloc(1, sizeof(**cfg));
112 cca5682e 2020-11-18 stsp if (*cfg == NULL)
113 cca5682e 2020-11-18 stsp return got_error_from_errno("calloc");
115 fe621944 2020-11-10 stsp switch (algorithm) {
116 fe621944 2020-11-10 stsp case GOT_DIFF_ALGORITHM_PATIENCE:
117 cca5682e 2020-11-18 stsp (*cfg)->algo = &patience;
119 fe621944 2020-11-10 stsp case GOT_DIFF_ALGORITHM_MYERS:
120 cca5682e 2020-11-18 stsp (*cfg)->algo = &myers_then_myers_divide;
123 cca5682e 2020-11-18 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad diff algorithm");
126 cca5682e 2020-11-18 stsp if (atomize_func) {
127 cca5682e 2020-11-18 stsp (*cfg)->atomize_func = atomize_func;
128 cca5682e 2020-11-18 stsp (*cfg)->atomize_func_data = atomize_func_data;
130 cca5682e 2020-11-18 stsp (*cfg)->atomize_func = diff_atomize_text_by_line;
132 cca5682e 2020-11-18 stsp (*cfg)->max_recursion_depth = 0; /* use default recursion depth */
134 cca5682e 2020-11-18 stsp return NULL;
137 7d283eee 2017-11-29 stsp const struct got_error *
138 72254787 2020-11-18 stsp got_diff_prepare_file(FILE *f, char **p, size_t *size,
139 fe621944 2020-11-10 stsp struct diff_data *diff_data, const struct diff_config *cfg,
140 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff)
142 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
143 fe621944 2020-11-10 stsp struct stat st;
144 fe621944 2020-11-10 stsp int diff_flags = 0, rc;
148 fe621944 2020-11-10 stsp diff_flags |= DIFF_FLAG_SHOW_PROTOTYPES;
149 fe621944 2020-11-10 stsp if (ignore_whitespace)
150 fe621944 2020-11-10 stsp diff_flags |= DIFF_FLAG_IGNORE_WHITESPACE;
151 64453f7e 2020-11-21 stsp if (force_text_diff)
152 64453f7e 2020-11-21 stsp diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
154 72254787 2020-11-18 stsp if (fstat(fileno(f), &st) == -1) {
155 72254787 2020-11-18 stsp err = got_error_from_errno("fstat");
158 72254787 2020-11-18 stsp #ifndef GOT_DIFF_NO_MMAP
159 72254787 2020-11-18 stsp *p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE,
160 72254787 2020-11-18 stsp fileno(f), 0);
161 72254787 2020-11-18 stsp if (*p == MAP_FAILED)
163 72254787 2020-11-18 stsp *p = NULL; /* fall back on file I/O */
165 72254787 2020-11-18 stsp rc = diff_atomize_file(diff_data, cfg, f, *p, st.st_size, diff_flags);
167 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_atomize_file");
172 fe621944 2020-11-10 stsp diff_data_free(diff_data);
174 fe621944 2020-11-10 stsp *size = st.st_size;
175 fe621944 2020-11-10 stsp return err;
178 fe621944 2020-11-10 stsp const struct got_error *
179 fe621944 2020-11-10 stsp got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
180 64453f7e 2020-11-21 stsp enum got_diff_algorithm algorithm, int ignore_whitespace,
181 64453f7e 2020-11-21 stsp int force_text_diff)
183 fe621944 2020-11-10 stsp const struct got_error *err = NULL;
184 cca5682e 2020-11-18 stsp struct diff_config *cfg = NULL;
185 fe621944 2020-11-10 stsp char *p1 = NULL, *p2 = NULL;
186 fe621944 2020-11-10 stsp size_t size1, size2;
187 fe621944 2020-11-10 stsp struct diff_data d_left, d_right;
188 fe621944 2020-11-10 stsp struct diff_data *left, *right;
189 fe621944 2020-11-10 stsp struct diff_result *diff_result;
191 fe621944 2020-11-10 stsp if (diffreg_result) {
192 fe621944 2020-11-10 stsp *diffreg_result = calloc(1, sizeof(**diffreg_result));
193 fe621944 2020-11-10 stsp if (*diffreg_result == NULL)
194 fe621944 2020-11-10 stsp return got_error_from_errno("calloc");
195 fe621944 2020-11-10 stsp left = &(*diffreg_result)->left;
196 fe621944 2020-11-10 stsp right = &(*diffreg_result)->right;
198 fe621944 2020-11-10 stsp memset(&d_left, 0, sizeof(d_left));
199 fe621944 2020-11-10 stsp memset(&d_right, 0, sizeof(d_right));
200 fe621944 2020-11-10 stsp left = &d_left;
201 fe621944 2020-11-10 stsp right = &d_right;
204 cca5682e 2020-11-18 stsp err = got_diff_get_config(&cfg, algorithm, NULL, NULL);
208 72254787 2020-11-18 stsp err = got_diff_prepare_file(f1, &p1, &size1, left, cfg,
209 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
213 72254787 2020-11-18 stsp err = got_diff_prepare_file(f2, &p2, &size2, right, cfg,
214 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
218 fe621944 2020-11-10 stsp diff_result = diff_main(cfg, left, right);
219 fe621944 2020-11-10 stsp if (diff_result == NULL) {
220 fe621944 2020-11-10 stsp err = got_error_set_errno(ENOMEM, "malloc");
223 fe621944 2020-11-10 stsp if (diff_result->rc != DIFF_RC_OK) {
224 fe621944 2020-11-10 stsp err = got_error_set_errno(diff_result->rc, "diff");
228 fe621944 2020-11-10 stsp if (diffreg_result) {
229 fe621944 2020-11-10 stsp (*diffreg_result)->result = diff_result;
230 fe621944 2020-11-10 stsp (*diffreg_result)->map1 = p1;
231 fe621944 2020-11-10 stsp (*diffreg_result)->size1 = size1;
232 fe621944 2020-11-10 stsp (*diffreg_result)->map2 = p2;
233 fe621944 2020-11-10 stsp (*diffreg_result)->size2 = size2;
237 fe621944 2020-11-10 stsp if (diffreg_result == NULL) {
238 fe621944 2020-11-10 stsp diff_data_free(left);
239 fe621944 2020-11-10 stsp diff_data_free(right);
242 dd2e2f52 2022-07-01 thomas got_diffreg_close(p1, size1, p2, size2);
243 fe621944 2020-11-10 stsp if (diffreg_result) {
244 fe621944 2020-11-10 stsp diff_data_free(left);
245 fe621944 2020-11-10 stsp diff_data_free(right);
246 fe621944 2020-11-10 stsp free(*diffreg_result);
247 fe621944 2020-11-10 stsp *diffreg_result = NULL;
251 fe621944 2020-11-10 stsp return err;
254 fe621944 2020-11-10 stsp const struct got_error *
255 82c78e96 2022-08-06 thomas got_diffreg_output(struct got_diff_line **lines, size_t *nlines,
256 1cb46f00 2020-11-21 stsp struct got_diffreg_result *diff_result, int f1_exists, int f2_exists,
257 fe621944 2020-11-10 stsp const char *path1, const char *path2,
258 fe621944 2020-11-10 stsp enum got_diff_output_format output_format, int context_lines, FILE *outfile)
260 fe621944 2020-11-10 stsp struct diff_input_info info = {
261 fe621944 2020-11-10 stsp .left_path = path1,
262 fe621944 2020-11-10 stsp .right_path = path2,
263 1cb46f00 2020-11-21 stsp .flags = 0,
266 fe621944 2020-11-10 stsp struct diff_output_info *output_info;
268 1cb46f00 2020-11-21 stsp if (!f1_exists)
269 1cb46f00 2020-11-21 stsp info.flags |= DIFF_INPUT_LEFT_NONEXISTENT;
270 1cb46f00 2020-11-21 stsp if (!f2_exists)
271 1cb46f00 2020-11-21 stsp info.flags |= DIFF_INPUT_RIGHT_NONEXISTENT;
273 fe621944 2020-11-10 stsp switch (output_format) {
274 fe621944 2020-11-10 stsp case GOT_DIFF_OUTPUT_UNIDIFF:
275 fe621944 2020-11-10 stsp rc = diff_output_unidiff(
276 82c78e96 2022-08-06 thomas lines ? &output_info : NULL, outfile, &info,
277 fe621944 2020-11-10 stsp diff_result->result, context_lines);
278 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
279 fe621944 2020-11-10 stsp return got_error_set_errno(rc, "diff_output_unidiff");
281 a42e5f4f 2022-09-02 thomas case GOT_DIFF_OUTPUT_PLAIN:
282 a42e5f4f 2022-09-02 thomas rc = diff_output_plain(lines ? &output_info : NULL,
283 fe621944 2020-11-10 stsp outfile, &info, diff_result->result);
284 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
285 fe621944 2020-11-10 stsp return got_error_set_errno(rc, "diff_output_edscript");
290 82c78e96 2022-08-06 thomas if (lines && *lines) {
291 fe621944 2020-11-10 stsp if (output_info->line_offsets.len > 0) {
292 82c78e96 2022-08-06 thomas struct got_diff_line *p;
293 82c78e96 2022-08-06 thomas off_t prev_offset = 0, *o;
294 82c78e96 2022-08-06 thomas uint8_t *o2;
295 fe621944 2020-11-10 stsp int i, len;
296 fe621944 2020-11-10 stsp if (*nlines > 0) {
297 82c78e96 2022-08-06 thomas prev_offset = (*lines)[*nlines - 1].offset;
299 fe621944 2020-11-10 stsp * First line offset is always zero. Skip it
300 fe621944 2020-11-10 stsp * when appending to a pre-populated array.
302 fe621944 2020-11-10 stsp o = &output_info->line_offsets.head[1];
303 82c78e96 2022-08-06 thomas o2 = &output_info->line_types.head[1];
304 fe621944 2020-11-10 stsp len = output_info->line_offsets.len - 1;
306 fe621944 2020-11-10 stsp o = &output_info->line_offsets.head[0];
307 82c78e96 2022-08-06 thomas o2 = &output_info->line_types.head[0];
308 fe621944 2020-11-10 stsp len = output_info->line_offsets.len;
310 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + len, sizeof(**lines));
311 fe621944 2020-11-10 stsp if (p == NULL)
312 fe621944 2020-11-10 stsp return got_error_from_errno("calloc");
313 82c78e96 2022-08-06 thomas for (i = 0; i < len; i++) {
314 82c78e96 2022-08-06 thomas p[*nlines + i].offset = o[i] + prev_offset;
315 82c78e96 2022-08-06 thomas p[*nlines + i].type = o2[i];
317 82c78e96 2022-08-06 thomas *lines = p;
318 fe621944 2020-11-10 stsp *nlines += len;
320 fe621944 2020-11-10 stsp diff_output_info_free(output_info);
323 fe621944 2020-11-10 stsp return NULL;
326 fe621944 2020-11-10 stsp const struct got_error *
327 fe621944 2020-11-10 stsp got_diffreg_result_free(struct got_diffreg_result *diffreg_result)
329 fe621944 2020-11-10 stsp const struct got_error *err;
331 fe621944 2020-11-10 stsp diff_result_free(diffreg_result->result);
332 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->left);
333 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->right);
334 dd2e2f52 2022-07-01 thomas err = got_diffreg_close(diffreg_result->map1, diffreg_result->size1,
335 fe621944 2020-11-10 stsp diffreg_result->map2, diffreg_result->size2);
336 fe621944 2020-11-10 stsp free(diffreg_result);
337 fe621944 2020-11-10 stsp return err;
340 fe621944 2020-11-10 stsp const struct got_error *
341 fe621944 2020-11-10 stsp got_diffreg_result_free_left(struct got_diffreg_result *diffreg_result)
343 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->left);
344 fe621944 2020-11-10 stsp memset(&diffreg_result->left, 0, sizeof(diffreg_result->left));
345 dd2e2f52 2022-07-01 thomas return got_diffreg_close(diffreg_result->map1, diffreg_result->size1,
346 dd2e2f52 2022-07-01 thomas NULL, 0);
349 fe621944 2020-11-10 stsp const struct got_error *
350 fe621944 2020-11-10 stsp got_diffreg_result_free_right(struct got_diffreg_result *diffreg_result)
352 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->right);
353 fe621944 2020-11-10 stsp memset(&diffreg_result->right, 0, sizeof(diffreg_result->right));
354 dd2e2f52 2022-07-01 thomas return got_diffreg_close(NULL, 0, diffreg_result->map2,
355 dd2e2f52 2022-07-01 thomas diffreg_result->size2);