aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_rewrite_diff2.c
blob: 5af72ba1d2fdf79857e179c100534e4083524897 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* See LICENSE file for copyright and license details. */
#include "common.h"


#define REPETITION_MAX ((1U << (sizeof(int) * CHAR_BIT - CHAR_BIT)) - 1U)


static int
allocate(struct libpatch_diff2 **diff, size_t *difflen, size_t *r, size_t *w, int change)
{
#define GROWTH 32U

	void *new;
	size_t size;

	if (*w == *r) {
		if (GROWTH > SIZE_MAX / sizeof(**diff) - *difflen) {
			errno = ENOMEM;
			return -1;
		}
		size = (*difflen + GROWTH) * sizeof(**diff);
		new = realloc(*diff, size);
		if (!new)
			return -1;
		*diff = new;
		memmove(&(*diff)[*r + GROWTH], &(*diff)[*r], *difflen - *r);
		*difflen += GROWTH;
		*r += GROWTH;
	}

	(*diff)[*w].change = (unsigned char)change;
	(*diff)[*w].repetition = 0;
	*w += 1;
	return 0;
}


static int
putchange(struct libpatch_diff2 **diff, size_t *difflen, size_t *r, size_t *w, int change, size_t count)
{
	if (!count)
		return 0;
	if (*w && (*diff)[*w - 1].change == change && (*diff)[*w - 1].repetition < REPETITION_MAX)
		goto start;
	do {
		if (allocate(diff, difflen, r, w, change))
			return -1;
	start:
		if (count > (size_t)(REPETITION_MAX - (*diff)[*w - 1].repetition)) {
			count -= (size_t)(REPETITION_MAX - (*diff)[*w - 1].repetition);
			(*diff)[*w - 1].repetition = REPETITION_MAX;
		} else {
			(*diff)[*w - 1].repetition += count;
			break;
		}
	} while (count);
	return 0;
}


static int
put(struct libpatch_diff2 **diff, size_t *difflen,
    const struct libpatch_diff2_spec *spec, size_t *r, size_t *w,
    size_t common, size_t onlyfile1, size_t onlyfile2, size_t changed)
{
	int a, b;
	size_t an, bn, i;
	size_t context, a_context, z_context;

	if (common) {
		if (spec->full_context) {
			return putchange(diff, difflen, r, w, LIBPATCH_DIFF2_CONTEXT, common);
		} else if (*w > 0 && *r < *difflen) {
			context = MAX(spec->context_after + spec->context_before, spec->context_between);
			if (common <= context)
				return putchange(diff, difflen, r, w, LIBPATCH_DIFF2_CONTEXT, common);
			common -= a_context = MIN(spec->context_after, common);
			common -= z_context = MIN(spec->context_before, common);
			return -(putchange(diff, difflen, r, w, LIBPATCH_DIFF2_CONTEXT, a_context) ||
			         putchange(diff, difflen, r, w, LIBPATCH_DIFF2_SKIP, common) ||
			         putchange(diff, difflen, r, w, LIBPATCH_DIFF2_CONTEXT, z_context));
		} else if (*w > 0) {
			common -= context = MIN(spec->context_after, common);
			return -(putchange(diff, difflen, r, w, LIBPATCH_DIFF2_CONTEXT, context) ||
			         putchange(diff, difflen, r, w, LIBPATCH_DIFF2_SKIP, common));
		} else if (*r < *difflen) {
			common -= context = MIN(spec->context_before, common);
			return -(putchange(diff, difflen, r, w, LIBPATCH_DIFF2_SKIP, common) ||
			         putchange(diff, difflen, r, w, LIBPATCH_DIFF2_CONTEXT, context));
		} else {
			return putchange(diff, difflen, r, w, LIBPATCH_DIFF2_SKIP, common);
		}

	} else {
		if (!spec->keep_split_changes) {
			changed += MIN(onlyfile1, onlyfile2);
			onlyfile1 -= changed;
			onlyfile2 -= changed;
		}

		if (spec->single_file_change_order == LIBPATCH_DIFF2_FILE1_ONLY_FIRST) {
			a = LIBPATCH_DIFF2_FILE1_ONLY;
			an = onlyfile1;
			b = LIBPATCH_DIFF2_FILE2_ONLY;
			bn = onlyfile2;
		} else {
			a = LIBPATCH_DIFF2_FILE2_ONLY;
			an = onlyfile2;
			b = LIBPATCH_DIFF2_FILE1_ONLY;
			bn = onlyfile1;
		}

		if (spec->two_file_change_order == LIBPATCH_DIFF2_TWO_FILE_CHANGES_BEFORE_SINGLE_FILE)
			if (putchange(diff, difflen, r, w, LIBPATCH_DIFF2_TWO_FILE_CHANGE, changed))
				return -1;

		if (putchange(diff, difflen, r, w, a, an))
			return -1;

		if (spec->two_file_change_order == LIBPATCH_DIFF2_NO_TWO_FILE_CHANGES) {
			if (putchange(diff, difflen, r, w, a, changed))
				return -1;
			if (putchange(diff, difflen, r, w, b, changed))
				return -1;
		} else if (spec->two_file_change_order == LIBPATCH_DIFF2_NO_TWO_FILE_CHANGES_INTERLEAVE) {
			for (i = 0; i < changed; i++) {
				if (putchange(diff, difflen, r, w, a, 1))
					return -1;
				if (putchange(diff, difflen, r, w, b, 1))
					return -1;
			}
		}

		if (spec->two_file_change_order == LIBPATCH_DIFF2_TWO_FILE_CHANGES_BETWEEN_SINGLE_FILE)
			if (putchange(diff, difflen, r, w, LIBPATCH_DIFF2_TWO_FILE_CHANGE, changed))
				return -1;

		if (putchange(diff, difflen, r, w, b, bn))
			return -1;

		if (spec->two_file_change_order == LIBPATCH_DIFF2_TWO_FILE_CHANGES_AFTER_SINGLE_FILE)
			if (putchange(diff, difflen, r, w, LIBPATCH_DIFF2_TWO_FILE_CHANGE, changed))
				return -1;

		return 0;
	}
}


int
libpatch_rewrite_diff2(struct libpatch_diff2 **diff, size_t *difflen,
                       const struct libpatch_diff2_spec *spec)
{
	size_t r, w = 0;
	size_t saved0 = 0, saved1 = 0, saved2 = 0, saved3 = 0;

	for (r = 0; r < *difflen; r++) {
		if ((*diff)[r].change == LIBPATCH_DIFF2_SKIP ||
		    (*diff)[r].change == LIBPATCH_DIFF2_CONTEXT) {
			if (saved1 || saved2) {
				if (put(diff, difflen, spec, &r, &w, saved0, saved1, saved2, saved3))
					return -1;
				saved1 = 0;
				saved2 = 0;
				saved3 = 0;
			}
			saved0 += (size_t)(*diff)[r].repetition;
		} else {
			if (saved0) {
				if (put(diff, difflen, spec, &r, &w, saved0, saved1, saved2, saved3))
					return -1;
				saved0 = 0;
			}
			if (spec->keep_split_changes) {
				if ((*diff)[r].change == LIBPATCH_DIFF2_FILE1_ONLY)
					saved1 += (size_t)(*diff)[r].repetition;
				else if ((*diff)[r].change == LIBPATCH_DIFF2_FILE2_ONLY)
					saved2 += (size_t)(*diff)[r].repetition;
				else
					saved3 += (size_t)(*diff)[r].repetition;
			} else {
				if ((*diff)[r].change != LIBPATCH_DIFF2_FILE2_ONLY)
					saved1 += (size_t)(*diff)[r].repetition;
				if ((*diff)[r].change != LIBPATCH_DIFF2_FILE1_ONLY)
					saved2 += (size_t)(*diff)[r].repetition;
			}
		}
	}

	if (put(diff, difflen, spec, &r, &w, saved0, saved1, saved2, saved3))
		return -1;

	*difflen = w;
	return 0;
}