Show More
@@ -1,471 +1,474 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 | #define PY_SSIZE_T_CLEAN | |
|
12 | 13 | #include <Python.h> |
|
13 | 14 | #include <stdlib.h> |
|
14 | 15 | #include <string.h> |
|
15 | 16 | #include <limits.h> |
|
16 | 17 | |
|
17 | 18 | #include "util.h" |
|
18 | 19 | |
|
19 | 20 | struct line { |
|
20 |
int hash |
|
|
21 | int hash, n, e; | |
|
22 | Py_ssize_t len; | |
|
21 | 23 | const char *l; |
|
22 | 24 | }; |
|
23 | 25 | |
|
24 | 26 | struct pos { |
|
25 | 27 | int pos, len; |
|
26 | 28 | }; |
|
27 | 29 | |
|
28 | 30 | struct hunk; |
|
29 | 31 | struct hunk { |
|
30 | 32 | int a1, a2, b1, b2; |
|
31 | 33 | struct hunk *next; |
|
32 | 34 | }; |
|
33 | 35 | |
|
34 |
static int splitlines(const char *a, |
|
|
36 | static int splitlines(const char *a, Py_ssize_t len, struct line **lr) | |
|
35 | 37 | { |
|
36 | 38 | unsigned hash; |
|
37 | 39 | int i; |
|
38 | 40 | const char *p, *b = a; |
|
39 | 41 | const char * const plast = a + len - 1; |
|
40 | 42 | struct line *l; |
|
41 | 43 | |
|
42 | 44 | /* count the lines */ |
|
43 | 45 | i = 1; /* extra line for sentinel */ |
|
44 | 46 | for (p = a; p < a + len; p++) |
|
45 | 47 | if (*p == '\n' || p == plast) |
|
46 | 48 | i++; |
|
47 | 49 | |
|
48 | 50 | *lr = l = (struct line *)malloc(sizeof(struct line) * i); |
|
49 | 51 | if (!l) |
|
50 | 52 | return -1; |
|
51 | 53 | |
|
52 | 54 | /* build the line array and calculate hashes */ |
|
53 | 55 | hash = 0; |
|
54 | 56 | for (p = a; p < a + len; p++) { |
|
55 | 57 | /* Leonid Yuriev's hash */ |
|
56 | 58 | hash = (hash * 1664525) + (unsigned char)*p + 1013904223; |
|
57 | 59 | |
|
58 | 60 | if (*p == '\n' || p == plast) { |
|
59 | 61 | l->hash = hash; |
|
60 | 62 | hash = 0; |
|
61 | 63 | l->len = p - b + 1; |
|
62 | 64 | l->l = b; |
|
63 | 65 | l->n = INT_MAX; |
|
64 | 66 | l++; |
|
65 | 67 | b = p + 1; |
|
66 | 68 | } |
|
67 | 69 | } |
|
68 | 70 | |
|
69 | 71 | /* set up a sentinel */ |
|
70 | 72 | l->hash = 0; |
|
71 | 73 | l->len = 0; |
|
72 | 74 | l->l = a + len; |
|
73 | 75 | return i - 1; |
|
74 | 76 | } |
|
75 | 77 | |
|
76 | 78 | static inline int cmp(struct line *a, struct line *b) |
|
77 | 79 | { |
|
78 | 80 | return a->hash != b->hash || a->len != b->len || memcmp(a->l, b->l, a->len); |
|
79 | 81 | } |
|
80 | 82 | |
|
81 | 83 | static int equatelines(struct line *a, int an, struct line *b, int bn) |
|
82 | 84 | { |
|
83 | 85 | int i, j, buckets = 1, t, scale; |
|
84 | 86 | struct pos *h = NULL; |
|
85 | 87 | |
|
86 | 88 | /* build a hash table of the next highest power of 2 */ |
|
87 | 89 | while (buckets < bn + 1) |
|
88 | 90 | buckets *= 2; |
|
89 | 91 | |
|
90 | 92 | /* try to allocate a large hash table to avoid collisions */ |
|
91 | 93 | for (scale = 4; scale; scale /= 2) { |
|
92 | 94 | h = (struct pos *)malloc(scale * buckets * sizeof(struct pos)); |
|
93 | 95 | if (h) |
|
94 | 96 | break; |
|
95 | 97 | } |
|
96 | 98 | |
|
97 | 99 | if (!h) |
|
98 | 100 | return 0; |
|
99 | 101 | |
|
100 | 102 | buckets = buckets * scale - 1; |
|
101 | 103 | |
|
102 | 104 | /* clear the hash table */ |
|
103 | 105 | for (i = 0; i <= buckets; i++) { |
|
104 | 106 | h[i].pos = INT_MAX; |
|
105 | 107 | h[i].len = 0; |
|
106 | 108 | } |
|
107 | 109 | |
|
108 | 110 | /* add lines to the hash table chains */ |
|
109 | 111 | for (i = bn - 1; i >= 0; i--) { |
|
110 | 112 | /* find the equivalence class */ |
|
111 | 113 | for (j = b[i].hash & buckets; h[j].pos != INT_MAX; |
|
112 | 114 | j = (j + 1) & buckets) |
|
113 | 115 | if (!cmp(b + i, b + h[j].pos)) |
|
114 | 116 | break; |
|
115 | 117 | |
|
116 | 118 | /* add to the head of the equivalence class */ |
|
117 | 119 | b[i].n = h[j].pos; |
|
118 | 120 | b[i].e = j; |
|
119 | 121 | h[j].pos = i; |
|
120 | 122 | h[j].len++; /* keep track of popularity */ |
|
121 | 123 | } |
|
122 | 124 | |
|
123 | 125 | /* compute popularity threshold */ |
|
124 | 126 | t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1); |
|
125 | 127 | |
|
126 | 128 | /* match items in a to their equivalence class in b */ |
|
127 | 129 | for (i = 0; i < an; i++) { |
|
128 | 130 | /* find the equivalence class */ |
|
129 | 131 | for (j = a[i].hash & buckets; h[j].pos != INT_MAX; |
|
130 | 132 | j = (j + 1) & buckets) |
|
131 | 133 | if (!cmp(a + i, b + h[j].pos)) |
|
132 | 134 | break; |
|
133 | 135 | |
|
134 | 136 | a[i].e = j; /* use equivalence class for quick compare */ |
|
135 | 137 | if (h[j].len <= t) |
|
136 | 138 | a[i].n = h[j].pos; /* point to head of match list */ |
|
137 | 139 | else |
|
138 | 140 | a[i].n = INT_MAX; /* too popular */ |
|
139 | 141 | } |
|
140 | 142 | |
|
141 | 143 | /* discard hash tables */ |
|
142 | 144 | free(h); |
|
143 | 145 | return 1; |
|
144 | 146 | } |
|
145 | 147 | |
|
146 | 148 | static int longest_match(struct line *a, struct line *b, struct pos *pos, |
|
147 | 149 | int a1, int a2, int b1, int b2, int *omi, int *omj) |
|
148 | 150 | { |
|
149 | 151 | int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k; |
|
150 | 152 | |
|
151 | 153 | for (i = a1; i < a2; i++) { |
|
152 | 154 | /* skip things before the current block */ |
|
153 | 155 | for (j = a[i].n; j < b1; j = b[j].n) |
|
154 | 156 | ; |
|
155 | 157 | |
|
156 | 158 | /* loop through all lines match a[i] in b */ |
|
157 | 159 | for (; j < b2; j = b[j].n) { |
|
158 | 160 | /* does this extend an earlier match? */ |
|
159 | 161 | if (i > a1 && j > b1 && pos[j - 1].pos == i - 1) |
|
160 | 162 | k = pos[j - 1].len + 1; |
|
161 | 163 | else |
|
162 | 164 | k = 1; |
|
163 | 165 | pos[j].pos = i; |
|
164 | 166 | pos[j].len = k; |
|
165 | 167 | |
|
166 | 168 | /* best match so far? */ |
|
167 | 169 | if (k > mk) { |
|
168 | 170 | mi = i; |
|
169 | 171 | mj = j; |
|
170 | 172 | mk = k; |
|
171 | 173 | } |
|
172 | 174 | } |
|
173 | 175 | } |
|
174 | 176 | |
|
175 | 177 | if (mk) { |
|
176 | 178 | mi = mi - mk + 1; |
|
177 | 179 | mj = mj - mk + 1; |
|
178 | 180 | } |
|
179 | 181 | |
|
180 | 182 | /* expand match to include neighboring popular lines */ |
|
181 | 183 | while (mi - mb > a1 && mj - mb > b1 && |
|
182 | 184 | a[mi - mb - 1].e == b[mj - mb - 1].e) |
|
183 | 185 | mb++; |
|
184 | 186 | while (mi + mk < a2 && mj + mk < b2 && |
|
185 | 187 | a[mi + mk].e == b[mj + mk].e) |
|
186 | 188 | mk++; |
|
187 | 189 | |
|
188 | 190 | *omi = mi - mb; |
|
189 | 191 | *omj = mj - mb; |
|
190 | 192 | |
|
191 | 193 | return mk + mb; |
|
192 | 194 | } |
|
193 | 195 | |
|
194 | 196 | static struct hunk *recurse(struct line *a, struct line *b, struct pos *pos, |
|
195 | 197 | int a1, int a2, int b1, int b2, struct hunk *l) |
|
196 | 198 | { |
|
197 | 199 | int i, j, k; |
|
198 | 200 | |
|
199 | 201 | while (1) { |
|
200 | 202 | /* find the longest match in this chunk */ |
|
201 | 203 | k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); |
|
202 | 204 | if (!k) |
|
203 | 205 | return l; |
|
204 | 206 | |
|
205 | 207 | /* and recurse on the remaining chunks on either side */ |
|
206 | 208 | l = recurse(a, b, pos, a1, i, b1, j, l); |
|
207 | 209 | if (!l) |
|
208 | 210 | return NULL; |
|
209 | 211 | |
|
210 | 212 | l->next = (struct hunk *)malloc(sizeof(struct hunk)); |
|
211 | 213 | if (!l->next) |
|
212 | 214 | return NULL; |
|
213 | 215 | |
|
214 | 216 | l = l->next; |
|
215 | 217 | l->a1 = i; |
|
216 | 218 | l->a2 = i + k; |
|
217 | 219 | l->b1 = j; |
|
218 | 220 | l->b2 = j + k; |
|
219 | 221 | l->next = NULL; |
|
220 | 222 | |
|
221 | 223 | /* tail-recursion didn't happen, so do equivalent iteration */ |
|
222 | 224 | a1 = i + k; |
|
223 | 225 | b1 = j + k; |
|
224 | 226 | } |
|
225 | 227 | } |
|
226 | 228 | |
|
227 | 229 | static int diff(struct line *a, int an, struct line *b, int bn, |
|
228 | 230 | struct hunk *base) |
|
229 | 231 | { |
|
230 | 232 | struct hunk *curr; |
|
231 | 233 | struct pos *pos; |
|
232 | 234 | int t, count = 0; |
|
233 | 235 | |
|
234 | 236 | /* allocate and fill arrays */ |
|
235 | 237 | t = equatelines(a, an, b, bn); |
|
236 | 238 | pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos)); |
|
237 | 239 | |
|
238 | 240 | if (pos && t) { |
|
239 | 241 | /* generate the matching block list */ |
|
240 | 242 | |
|
241 | 243 | curr = recurse(a, b, pos, 0, an, 0, bn, base); |
|
242 | 244 | if (!curr) |
|
243 | 245 | return -1; |
|
244 | 246 | |
|
245 | 247 | /* sentinel end hunk */ |
|
246 | 248 | curr->next = (struct hunk *)malloc(sizeof(struct hunk)); |
|
247 | 249 | if (!curr->next) |
|
248 | 250 | return -1; |
|
249 | 251 | curr = curr->next; |
|
250 | 252 | curr->a1 = curr->a2 = an; |
|
251 | 253 | curr->b1 = curr->b2 = bn; |
|
252 | 254 | curr->next = NULL; |
|
253 | 255 | } |
|
254 | 256 | |
|
255 | 257 | free(pos); |
|
256 | 258 | |
|
257 | 259 | /* normalize the hunk list, try to push each hunk towards the end */ |
|
258 | 260 | for (curr = base->next; curr; curr = curr->next) { |
|
259 | 261 | struct hunk *next = curr->next; |
|
260 | 262 | int shift = 0; |
|
261 | 263 | |
|
262 | 264 | if (!next) |
|
263 | 265 | break; |
|
264 | 266 | |
|
265 | 267 | if (curr->a2 == next->a1) |
|
266 | 268 | while (curr->a2 + shift < an && curr->b2 + shift < bn |
|
267 | 269 | && !cmp(a + curr->a2 + shift, |
|
268 | 270 | b + curr->b2 + shift)) |
|
269 | 271 | shift++; |
|
270 | 272 | else if (curr->b2 == next->b1) |
|
271 | 273 | while (curr->b2 + shift < bn && curr->a2 + shift < an |
|
272 | 274 | && !cmp(b + curr->b2 + shift, |
|
273 | 275 | a + curr->a2 + shift)) |
|
274 | 276 | shift++; |
|
275 | 277 | if (!shift) |
|
276 | 278 | continue; |
|
277 | 279 | curr->b2 += shift; |
|
278 | 280 | next->b1 += shift; |
|
279 | 281 | curr->a2 += shift; |
|
280 | 282 | next->a1 += shift; |
|
281 | 283 | } |
|
282 | 284 | |
|
283 | 285 | for (curr = base->next; curr; curr = curr->next) |
|
284 | 286 | count++; |
|
285 | 287 | return count; |
|
286 | 288 | } |
|
287 | 289 | |
|
288 | 290 | static void freehunks(struct hunk *l) |
|
289 | 291 | { |
|
290 | 292 | struct hunk *n; |
|
291 | 293 | for (; l; l = n) { |
|
292 | 294 | n = l->next; |
|
293 | 295 | free(l); |
|
294 | 296 | } |
|
295 | 297 | } |
|
296 | 298 | |
|
297 | 299 | static PyObject *blocks(PyObject *self, PyObject *args) |
|
298 | 300 | { |
|
299 | 301 | PyObject *sa, *sb, *rl = NULL, *m; |
|
300 | 302 | struct line *a, *b; |
|
301 | 303 | struct hunk l, *h; |
|
302 | 304 | int an, bn, count, pos = 0; |
|
303 | 305 | |
|
304 | 306 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) |
|
305 | 307 | return NULL; |
|
306 | 308 | |
|
307 | 309 | an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); |
|
308 | 310 | bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); |
|
309 | 311 | |
|
310 | 312 | if (!a || !b) |
|
311 | 313 | goto nomem; |
|
312 | 314 | |
|
313 | 315 | l.next = NULL; |
|
314 | 316 | count = diff(a, an, b, bn, &l); |
|
315 | 317 | if (count < 0) |
|
316 | 318 | goto nomem; |
|
317 | 319 | |
|
318 | 320 | rl = PyList_New(count); |
|
319 | 321 | if (!rl) |
|
320 | 322 | goto nomem; |
|
321 | 323 | |
|
322 | 324 | for (h = l.next; h; h = h->next) { |
|
323 | 325 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); |
|
324 | 326 | PyList_SetItem(rl, pos, m); |
|
325 | 327 | pos++; |
|
326 | 328 | } |
|
327 | 329 | |
|
328 | 330 | nomem: |
|
329 | 331 | free(a); |
|
330 | 332 | free(b); |
|
331 | 333 | freehunks(l.next); |
|
332 | 334 | return rl ? rl : PyErr_NoMemory(); |
|
333 | 335 | } |
|
334 | 336 | |
|
335 | 337 | static PyObject *bdiff(PyObject *self, PyObject *args) |
|
336 | 338 | { |
|
337 | 339 | char *sa, *sb, *rb; |
|
338 | 340 | PyObject *result = NULL; |
|
339 | 341 | struct line *al, *bl; |
|
340 | 342 | struct hunk l, *h; |
|
341 |
int an, bn, |
|
|
343 | int an, bn, count; | |
|
344 | Py_ssize_t len = 0, la, lb; | |
|
342 | 345 | PyThreadState *_save; |
|
343 | 346 | |
|
344 | 347 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) |
|
345 | 348 | return NULL; |
|
346 | 349 | |
|
347 | 350 | _save = PyEval_SaveThread(); |
|
348 | 351 | an = splitlines(sa, la, &al); |
|
349 | 352 | bn = splitlines(sb, lb, &bl); |
|
350 | 353 | if (!al || !bl) |
|
351 | 354 | goto nomem; |
|
352 | 355 | |
|
353 | 356 | l.next = NULL; |
|
354 | 357 | count = diff(al, an, bl, bn, &l); |
|
355 | 358 | if (count < 0) |
|
356 | 359 | goto nomem; |
|
357 | 360 | |
|
358 | 361 | /* calculate length of output */ |
|
359 | 362 | la = lb = 0; |
|
360 | 363 | for (h = l.next; h; h = h->next) { |
|
361 | 364 | if (h->a1 != la || h->b1 != lb) |
|
362 | 365 | len += 12 + bl[h->b1].l - bl[lb].l; |
|
363 | 366 | la = h->a2; |
|
364 | 367 | lb = h->b2; |
|
365 | 368 | } |
|
366 | 369 | PyEval_RestoreThread(_save); |
|
367 | 370 | _save = NULL; |
|
368 | 371 | |
|
369 | 372 | result = PyBytes_FromStringAndSize(NULL, len); |
|
370 | 373 | |
|
371 | 374 | if (!result) |
|
372 | 375 | goto nomem; |
|
373 | 376 | |
|
374 | 377 | /* build binary patch */ |
|
375 | 378 | rb = PyBytes_AsString(result); |
|
376 | 379 | la = lb = 0; |
|
377 | 380 | |
|
378 | 381 | for (h = l.next; h; h = h->next) { |
|
379 | 382 | if (h->a1 != la || h->b1 != lb) { |
|
380 | 383 | len = bl[h->b1].l - bl[lb].l; |
|
381 | 384 | putbe32(al[la].l - al->l, rb); |
|
382 | 385 | putbe32(al[h->a1].l - al->l, rb + 4); |
|
383 | 386 | putbe32(len, rb + 8); |
|
384 | 387 | memcpy(rb + 12, bl[lb].l, len); |
|
385 | 388 | rb += 12 + len; |
|
386 | 389 | } |
|
387 | 390 | la = h->a2; |
|
388 | 391 | lb = h->b2; |
|
389 | 392 | } |
|
390 | 393 | |
|
391 | 394 | nomem: |
|
392 | 395 | if (_save) |
|
393 | 396 | PyEval_RestoreThread(_save); |
|
394 | 397 | free(al); |
|
395 | 398 | free(bl); |
|
396 | 399 | freehunks(l.next); |
|
397 | 400 | return result ? result : PyErr_NoMemory(); |
|
398 | 401 | } |
|
399 | 402 | |
|
400 | 403 | /* |
|
401 | 404 | * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise, |
|
402 | 405 | * reduce whitespace sequences to a single space and trim remaining whitespace |
|
403 | 406 | * from end of lines. |
|
404 | 407 | */ |
|
405 | 408 | static PyObject *fixws(PyObject *self, PyObject *args) |
|
406 | 409 | { |
|
407 | 410 | PyObject *s, *result = NULL; |
|
408 | 411 | char allws, c; |
|
409 | 412 | const char *r; |
|
410 |
|
|
|
413 | Py_ssize_t i, rlen, wlen = 0; | |
|
411 | 414 | char *w; |
|
412 | 415 | |
|
413 | 416 | if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) |
|
414 | 417 | return NULL; |
|
415 | 418 | r = PyBytes_AsString(s); |
|
416 | 419 | rlen = PyBytes_Size(s); |
|
417 | 420 | |
|
418 | 421 | w = (char *)malloc(rlen ? rlen : 1); |
|
419 | 422 | if (!w) |
|
420 | 423 | goto nomem; |
|
421 | 424 | |
|
422 | 425 | for (i = 0; i != rlen; i++) { |
|
423 | 426 | c = r[i]; |
|
424 | 427 | if (c == ' ' || c == '\t' || c == '\r') { |
|
425 | 428 | if (!allws && (wlen == 0 || w[wlen - 1] != ' ')) |
|
426 | 429 | w[wlen++] = ' '; |
|
427 | 430 | } else if (c == '\n' && !allws |
|
428 | 431 | && wlen > 0 && w[wlen - 1] == ' ') { |
|
429 | 432 | w[wlen - 1] = '\n'; |
|
430 | 433 | } else { |
|
431 | 434 | w[wlen++] = c; |
|
432 | 435 | } |
|
433 | 436 | } |
|
434 | 437 | |
|
435 | 438 | result = PyBytes_FromStringAndSize(w, wlen); |
|
436 | 439 | |
|
437 | 440 | nomem: |
|
438 | 441 | free(w); |
|
439 | 442 | return result ? result : PyErr_NoMemory(); |
|
440 | 443 | } |
|
441 | 444 | |
|
442 | 445 | |
|
443 | 446 | static char mdiff_doc[] = "Efficient binary diff."; |
|
444 | 447 | |
|
445 | 448 | static PyMethodDef methods[] = { |
|
446 | 449 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, |
|
447 | 450 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, |
|
448 | 451 | {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"}, |
|
449 | 452 | {NULL, NULL} |
|
450 | 453 | }; |
|
451 | 454 | |
|
452 | 455 | #ifdef IS_PY3K |
|
453 | 456 | static struct PyModuleDef bdiff_module = { |
|
454 | 457 | PyModuleDef_HEAD_INIT, |
|
455 | 458 | "bdiff", |
|
456 | 459 | mdiff_doc, |
|
457 | 460 | -1, |
|
458 | 461 | methods |
|
459 | 462 | }; |
|
460 | 463 | |
|
461 | 464 | PyMODINIT_FUNC PyInit_bdiff(void) |
|
462 | 465 | { |
|
463 | 466 | return PyModule_Create(&bdiff_module); |
|
464 | 467 | } |
|
465 | 468 | #else |
|
466 | 469 | PyMODINIT_FUNC initbdiff(void) |
|
467 | 470 | { |
|
468 | 471 | Py_InitModule3("bdiff", methods, mdiff_doc); |
|
469 | 472 | } |
|
470 | 473 | #endif |
|
471 | 474 |
General Comments 0
You need to be logged in to leave comments.
Login now