Show More
@@ -1,373 +1,369 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | bdiff.c - efficient binary diff extension for Mercurial |
|
3 | 3 | |
|
4 | 4 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms of |
|
7 | 7 | the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | |
|
9 | 9 | Based roughly on Python difflib |
|
10 | 10 | */ |
|
11 | 11 | |
|
12 | 12 | #include <Python.h> |
|
13 | 13 | #include <stdlib.h> |
|
14 | 14 | #include <string.h> |
|
15 | 15 | |
|
16 | #ifdef __hpux | |
|
17 | #define inline | |
|
18 | #endif | |
|
19 | ||
|
20 | #ifdef __SUNPRO_C | |
|
16 | #if defined __hpux || defined __SUNPRO_C || defined _AIX | |
|
21 | 17 | # define inline |
|
22 | 18 | #endif |
|
23 | 19 | |
|
24 | 20 | #ifdef _WIN32 |
|
25 | 21 | #ifdef _MSC_VER |
|
26 | 22 | #define inline __inline |
|
27 | 23 | typedef unsigned long uint32_t; |
|
28 | 24 | #else |
|
29 | 25 | #include <stdint.h> |
|
30 | 26 | #endif |
|
31 | 27 | static uint32_t htonl(uint32_t x) |
|
32 | 28 | { |
|
33 | 29 | return ((x & 0x000000ffUL) << 24) | |
|
34 | 30 | ((x & 0x0000ff00UL) << 8) | |
|
35 | 31 | ((x & 0x00ff0000UL) >> 8) | |
|
36 | 32 | ((x & 0xff000000UL) >> 24); |
|
37 | 33 | } |
|
38 | 34 | #else |
|
39 | 35 | #include <sys/types.h> |
|
40 | 36 | #include <arpa/inet.h> |
|
41 | 37 | #include <inttypes.h> |
|
42 | 38 | #endif |
|
43 | 39 | |
|
44 | 40 | struct line { |
|
45 | 41 | int h, len, n, e; |
|
46 | 42 | const char *l; |
|
47 | 43 | }; |
|
48 | 44 | |
|
49 | 45 | struct pos { |
|
50 | 46 | int pos, len; |
|
51 | 47 | }; |
|
52 | 48 | |
|
53 | 49 | struct hunk { |
|
54 | 50 | int a1, a2, b1, b2; |
|
55 | 51 | }; |
|
56 | 52 | |
|
57 | 53 | struct hunklist { |
|
58 | 54 | struct hunk *base, *head; |
|
59 | 55 | }; |
|
60 | 56 | |
|
61 | 57 | static inline uint32_t rol32(uint32_t word, unsigned int shift) |
|
62 | 58 | { |
|
63 | 59 | return (word << shift) | (word >> (32 - shift)); |
|
64 | 60 | } |
|
65 | 61 | |
|
66 | 62 | int splitlines(const char *a, int len, struct line **lr) |
|
67 | 63 | { |
|
68 | 64 | int g, h, i; |
|
69 | 65 | const char *p, *b = a; |
|
70 | 66 | struct line *l; |
|
71 | 67 | |
|
72 | 68 | /* count the lines */ |
|
73 | 69 | i = 1; /* extra line for sentinel */ |
|
74 | 70 | for (p = a; p < a + len; p++) |
|
75 | 71 | if (*p == '\n' || p == a + len - 1) |
|
76 | 72 | i++; |
|
77 | 73 | |
|
78 | 74 | *lr = l = (struct line *)malloc(sizeof(struct line) * i); |
|
79 | 75 | if (!l) |
|
80 | 76 | return -1; |
|
81 | 77 | |
|
82 | 78 | /* build the line array and calculate hashes */ |
|
83 | 79 | h = 0; |
|
84 | 80 | for (p = a; p < a + len; p++) { |
|
85 | 81 | /* |
|
86 | 82 | * a simple hash from GNU diff, with better collision |
|
87 | 83 | * resistance from hashpjw. this slows down common |
|
88 | 84 | * case by 10%, but speeds up worst case by 100x. |
|
89 | 85 | */ |
|
90 | 86 | h = *p + rol32(h, 7); |
|
91 | 87 | if ((g = h & 0xf0000000)) { |
|
92 | 88 | h ^= g >> 24; |
|
93 | 89 | h ^= g; |
|
94 | 90 | } |
|
95 | 91 | if (*p == '\n' || p == a + len - 1) { |
|
96 | 92 | l->len = p - b + 1; |
|
97 | 93 | l->h = h * l->len; |
|
98 | 94 | l->l = b; |
|
99 | 95 | l->n = -1; |
|
100 | 96 | l++; |
|
101 | 97 | b = p + 1; |
|
102 | 98 | h = 0; |
|
103 | 99 | } |
|
104 | 100 | } |
|
105 | 101 | |
|
106 | 102 | /* set up a sentinel */ |
|
107 | 103 | l->h = l->len = 0; |
|
108 | 104 | l->l = a + len; |
|
109 | 105 | return i - 1; |
|
110 | 106 | } |
|
111 | 107 | |
|
112 | 108 | int inline cmp(struct line *a, struct line *b) |
|
113 | 109 | { |
|
114 | 110 | return a->h != b->h || a->len != b->len || memcmp(a->l, b->l, a->len); |
|
115 | 111 | } |
|
116 | 112 | |
|
117 | 113 | static int equatelines(struct line *a, int an, struct line *b, int bn) |
|
118 | 114 | { |
|
119 | 115 | int i, j, buckets = 1, t; |
|
120 | 116 | struct pos *h; |
|
121 | 117 | |
|
122 | 118 | /* build a hash table of the next highest power of 2 */ |
|
123 | 119 | while (buckets < bn + 1) |
|
124 | 120 | buckets *= 2; |
|
125 | 121 | |
|
126 | 122 | h = (struct pos *)malloc(buckets * sizeof(struct pos)); |
|
127 | 123 | buckets = buckets - 1; |
|
128 | 124 | if (!h) |
|
129 | 125 | return 0; |
|
130 | 126 | |
|
131 | 127 | /* clear the hash table */ |
|
132 | 128 | for (i = 0; i <= buckets; i++) { |
|
133 | 129 | h[i].pos = -1; |
|
134 | 130 | h[i].len = 0; |
|
135 | 131 | } |
|
136 | 132 | |
|
137 | 133 | /* add lines to the hash table chains */ |
|
138 | 134 | for (i = bn - 1; i >= 0; i--) { |
|
139 | 135 | /* find the equivalence class */ |
|
140 | 136 | for (j = b[i].h & buckets; h[j].pos != -1; |
|
141 | 137 | j = (j + 1) & buckets) |
|
142 | 138 | if (!cmp(b + i, b + h[j].pos)) |
|
143 | 139 | break; |
|
144 | 140 | |
|
145 | 141 | /* add to the head of the equivalence class */ |
|
146 | 142 | b[i].n = h[j].pos; |
|
147 | 143 | b[i].e = j; |
|
148 | 144 | h[j].pos = i; |
|
149 | 145 | h[j].len++; /* keep track of popularity */ |
|
150 | 146 | } |
|
151 | 147 | |
|
152 | 148 | /* compute popularity threshold */ |
|
153 | 149 | t = (bn >= 200) ? bn / 100 : bn + 1; |
|
154 | 150 | |
|
155 | 151 | /* match items in a to their equivalence class in b */ |
|
156 | 152 | for (i = 0; i < an; i++) { |
|
157 | 153 | /* find the equivalence class */ |
|
158 | 154 | for (j = a[i].h & buckets; h[j].pos != -1; |
|
159 | 155 | j = (j + 1) & buckets) |
|
160 | 156 | if (!cmp(a + i, b + h[j].pos)) |
|
161 | 157 | break; |
|
162 | 158 | |
|
163 | 159 | a[i].e = j; /* use equivalence class for quick compare */ |
|
164 | 160 | if (h[j].len <= t) |
|
165 | 161 | a[i].n = h[j].pos; /* point to head of match list */ |
|
166 | 162 | else |
|
167 | 163 | a[i].n = -1; /* too popular */ |
|
168 | 164 | } |
|
169 | 165 | |
|
170 | 166 | /* discard hash tables */ |
|
171 | 167 | free(h); |
|
172 | 168 | return 1; |
|
173 | 169 | } |
|
174 | 170 | |
|
175 | 171 | static int longest_match(struct line *a, struct line *b, struct pos *pos, |
|
176 | 172 | int a1, int a2, int b1, int b2, int *omi, int *omj) |
|
177 | 173 | { |
|
178 | 174 | int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k; |
|
179 | 175 | |
|
180 | 176 | for (i = a1; i < a2; i++) { |
|
181 | 177 | /* skip things before the current block */ |
|
182 | 178 | for (j = a[i].n; j != -1 && j < b1; j = b[j].n) |
|
183 | 179 | ; |
|
184 | 180 | |
|
185 | 181 | /* loop through all lines match a[i] in b */ |
|
186 | 182 | for (; j != -1 && j < b2; j = b[j].n) { |
|
187 | 183 | /* does this extend an earlier match? */ |
|
188 | 184 | if (i > a1 && j > b1 && pos[j - 1].pos == i - 1) |
|
189 | 185 | k = pos[j - 1].len + 1; |
|
190 | 186 | else |
|
191 | 187 | k = 1; |
|
192 | 188 | pos[j].pos = i; |
|
193 | 189 | pos[j].len = k; |
|
194 | 190 | |
|
195 | 191 | /* best match so far? */ |
|
196 | 192 | if (k > mk) { |
|
197 | 193 | mi = i; |
|
198 | 194 | mj = j; |
|
199 | 195 | mk = k; |
|
200 | 196 | } |
|
201 | 197 | } |
|
202 | 198 | } |
|
203 | 199 | |
|
204 | 200 | if (mk) { |
|
205 | 201 | mi = mi - mk + 1; |
|
206 | 202 | mj = mj - mk + 1; |
|
207 | 203 | } |
|
208 | 204 | |
|
209 | 205 | /* expand match to include neighboring popular lines */ |
|
210 | 206 | while (mi - mb > a1 && mj - mb > b1 && |
|
211 | 207 | a[mi - mb - 1].e == b[mj - mb - 1].e) |
|
212 | 208 | mb++; |
|
213 | 209 | while (mi + mk < a2 && mj + mk < b2 && |
|
214 | 210 | a[mi + mk].e == b[mj + mk].e) |
|
215 | 211 | mk++; |
|
216 | 212 | |
|
217 | 213 | *omi = mi - mb; |
|
218 | 214 | *omj = mj - mb; |
|
219 | 215 | return mk + mb; |
|
220 | 216 | } |
|
221 | 217 | |
|
222 | 218 | static void recurse(struct line *a, struct line *b, struct pos *pos, |
|
223 | 219 | int a1, int a2, int b1, int b2, struct hunklist *l) |
|
224 | 220 | { |
|
225 | 221 | int i, j, k; |
|
226 | 222 | |
|
227 | 223 | /* find the longest match in this chunk */ |
|
228 | 224 | k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); |
|
229 | 225 | if (!k) |
|
230 | 226 | return; |
|
231 | 227 | |
|
232 | 228 | /* and recurse on the remaining chunks on either side */ |
|
233 | 229 | recurse(a, b, pos, a1, i, b1, j, l); |
|
234 | 230 | l->head->a1 = i; |
|
235 | 231 | l->head->a2 = i + k; |
|
236 | 232 | l->head->b1 = j; |
|
237 | 233 | l->head->b2 = j + k; |
|
238 | 234 | l->head++; |
|
239 | 235 | recurse(a, b, pos, i + k, a2, j + k, b2, l); |
|
240 | 236 | } |
|
241 | 237 | |
|
242 | 238 | static struct hunklist diff(struct line *a, int an, struct line *b, int bn) |
|
243 | 239 | { |
|
244 | 240 | struct hunklist l; |
|
245 | 241 | struct pos *pos; |
|
246 | 242 | int t; |
|
247 | 243 | |
|
248 | 244 | /* allocate and fill arrays */ |
|
249 | 245 | t = equatelines(a, an, b, bn); |
|
250 | 246 | pos = (struct pos *)calloc(bn, sizeof(struct pos)); |
|
251 | 247 | /* we can't have more matches than lines in the shorter file */ |
|
252 | 248 | l.head = l.base = (struct hunk *)malloc(sizeof(struct hunk) * |
|
253 | 249 | ((an<bn ? an:bn) + 1)); |
|
254 | 250 | |
|
255 | 251 | if (pos && l.base && t) { |
|
256 | 252 | /* generate the matching block list */ |
|
257 | 253 | recurse(a, b, pos, 0, an, 0, bn, &l); |
|
258 | 254 | l.head->a1 = an; |
|
259 | 255 | l.head->b1 = bn; |
|
260 | 256 | l.head++; |
|
261 | 257 | } |
|
262 | 258 | |
|
263 | 259 | free(pos); |
|
264 | 260 | return l; |
|
265 | 261 | } |
|
266 | 262 | |
|
267 | 263 | static PyObject *blocks(PyObject *self, PyObject *args) |
|
268 | 264 | { |
|
269 | 265 | PyObject *sa, *sb, *rl = NULL, *m; |
|
270 | 266 | struct line *a, *b; |
|
271 | 267 | struct hunklist l = {NULL, NULL}; |
|
272 | 268 | struct hunk *h; |
|
273 | 269 | int an, bn, pos = 0; |
|
274 | 270 | |
|
275 | 271 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) |
|
276 | 272 | return NULL; |
|
277 | 273 | |
|
278 | 274 | an = splitlines(PyString_AsString(sa), PyString_Size(sa), &a); |
|
279 | 275 | bn = splitlines(PyString_AsString(sb), PyString_Size(sb), &b); |
|
280 | 276 | if (!a || !b) |
|
281 | 277 | goto nomem; |
|
282 | 278 | |
|
283 | 279 | l = diff(a, an, b, bn); |
|
284 | 280 | rl = PyList_New(l.head - l.base); |
|
285 | 281 | if (!l.head || !rl) |
|
286 | 282 | goto nomem; |
|
287 | 283 | |
|
288 | 284 | for (h = l.base; h != l.head; h++) { |
|
289 | 285 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); |
|
290 | 286 | PyList_SetItem(rl, pos, m); |
|
291 | 287 | pos++; |
|
292 | 288 | } |
|
293 | 289 | |
|
294 | 290 | nomem: |
|
295 | 291 | free(a); |
|
296 | 292 | free(b); |
|
297 | 293 | free(l.base); |
|
298 | 294 | return rl ? rl : PyErr_NoMemory(); |
|
299 | 295 | } |
|
300 | 296 | |
|
301 | 297 | static PyObject *bdiff(PyObject *self, PyObject *args) |
|
302 | 298 | { |
|
303 | 299 | char *sa, *sb; |
|
304 | 300 | PyObject *result = NULL; |
|
305 | 301 | struct line *al, *bl; |
|
306 | 302 | struct hunklist l = {NULL, NULL}; |
|
307 | 303 | struct hunk *h; |
|
308 | 304 | char encode[12], *rb; |
|
309 | 305 | int an, bn, len = 0, la, lb; |
|
310 | 306 | |
|
311 | 307 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) |
|
312 | 308 | return NULL; |
|
313 | 309 | |
|
314 | 310 | an = splitlines(sa, la, &al); |
|
315 | 311 | bn = splitlines(sb, lb, &bl); |
|
316 | 312 | if (!al || !bl) |
|
317 | 313 | goto nomem; |
|
318 | 314 | |
|
319 | 315 | l = diff(al, an, bl, bn); |
|
320 | 316 | if (!l.head) |
|
321 | 317 | goto nomem; |
|
322 | 318 | |
|
323 | 319 | /* calculate length of output */ |
|
324 | 320 | la = lb = 0; |
|
325 | 321 | for (h = l.base; h != l.head; h++) { |
|
326 | 322 | if (h->a1 != la || h->b1 != lb) |
|
327 | 323 | len += 12 + bl[h->b1].l - bl[lb].l; |
|
328 | 324 | la = h->a2; |
|
329 | 325 | lb = h->b2; |
|
330 | 326 | } |
|
331 | 327 | |
|
332 | 328 | result = PyString_FromStringAndSize(NULL, len); |
|
333 | 329 | if (!result) |
|
334 | 330 | goto nomem; |
|
335 | 331 | |
|
336 | 332 | /* build binary patch */ |
|
337 | 333 | rb = PyString_AsString(result); |
|
338 | 334 | la = lb = 0; |
|
339 | 335 | |
|
340 | 336 | for (h = l.base; h != l.head; h++) { |
|
341 | 337 | if (h->a1 != la || h->b1 != lb) { |
|
342 | 338 | len = bl[h->b1].l - bl[lb].l; |
|
343 | 339 | *(uint32_t *)(encode) = htonl(al[la].l - al->l); |
|
344 | 340 | *(uint32_t *)(encode + 4) = htonl(al[h->a1].l - al->l); |
|
345 | 341 | *(uint32_t *)(encode + 8) = htonl(len); |
|
346 | 342 | memcpy(rb, encode, 12); |
|
347 | 343 | memcpy(rb + 12, bl[lb].l, len); |
|
348 | 344 | rb += 12 + len; |
|
349 | 345 | } |
|
350 | 346 | la = h->a2; |
|
351 | 347 | lb = h->b2; |
|
352 | 348 | } |
|
353 | 349 | |
|
354 | 350 | nomem: |
|
355 | 351 | free(al); |
|
356 | 352 | free(bl); |
|
357 | 353 | free(l.base); |
|
358 | 354 | return result ? result : PyErr_NoMemory(); |
|
359 | 355 | } |
|
360 | 356 | |
|
361 | 357 | static char mdiff_doc[] = "Efficient binary diff."; |
|
362 | 358 | |
|
363 | 359 | static PyMethodDef methods[] = { |
|
364 | 360 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, |
|
365 | 361 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, |
|
366 | 362 | {NULL, NULL} |
|
367 | 363 | }; |
|
368 | 364 | |
|
369 | 365 | PyMODINIT_FUNC initbdiff(void) |
|
370 | 366 | { |
|
371 | 367 | Py_InitModule3("bdiff", methods, mdiff_doc); |
|
372 | 368 | } |
|
373 | 369 |
General Comments 0
You need to be logged in to leave comments.
Login now