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